From 540530d0a4de9b46ee67eea5a4e967973b40850d Mon Sep 17 00:00:00 2001
From: ballen4705 <ballen4705@4ea69e1a-61f1-4043-bf83-b5c94c648137>
Date: Thu, 24 Oct 2002 11:38:11 +0000
Subject: [PATCH] smartd: close file descriptors of SCSI device if not SMART
 capable added new temperature attribute (231, temperature) smartd: now open
 ATA disks using O_RDONLY 
 ----------------------------------------------------------------------

git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@111 4ea69e1a-61f1-4043-bf83-b5c94c648137
---
 sm5/CHANGELOG  |  7 +++++-
 sm5/TODO       |  3 +--
 sm5/smartd.c   | 60 +++++++++++++++++++++++++++-----------------------
 sm5/smartd.cpp | 60 +++++++++++++++++++++++++++-----------------------
 4 files changed, 71 insertions(+), 59 deletions(-)

diff --git a/sm5/CHANGELOG b/sm5/CHANGELOG
index 80b93c6f9..d8085b55d 100644
--- a/sm5/CHANGELOG
+++ b/sm5/CHANGELOG
@@ -1,6 +1,6 @@
 CHANGELOG for smartmontools
 
-$Id: CHANGELOG,v 1.18 2002/10/24 10:53:50 ballen4705 Exp $
+$Id: CHANGELOG,v 1.19 2002/10/24 11:38:11 ballen4705 Exp $
 
 Copyright (C) 2002 Bruce Allen <smartmontools-support@lists.sourceforge.net>
 
@@ -26,6 +26,11 @@ NOTES FOR NEXT RELEASE:
     Next release: handle extended error and self-test logs gracefully.
     Parse and print attribute flag meanings
 
+smartmontools-5.0-12
+
+    smartd: close file descriptors of SCSI device if not SMART capable
+    added new temperature attribute (231, temperature)
+    smartd: now open ATA disks using O_RDONLY
 
 smartmontools-5.0-11
 
diff --git a/sm5/TODO b/sm5/TODO
index ee634a18a..38ec86547 100644
--- a/sm5/TODO
+++ b/sm5/TODO
@@ -4,7 +4,7 @@ Home page of code is: http://smartmontools.sourceforge.net
 
 Copyright (C) 2002 Bruce Allen <smartmontools-support@lists.sourceforge.net>
 
-$Id: TODO,v 1.12 2002/10/24 10:31:02 ballen4705 Exp $
+$Id: TODO,v 1.13 2002/10/24 11:38:11 ballen4705 Exp $
 
 This program is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the Free
@@ -50,7 +50,6 @@ Print flags meanings in Vendor Attribute list -- not hex value --
 
 Fixes
 -----
-SCSI code in smartd leaves file descriptors open if SMART not supported..
 
 Fix lots of syntax like if (a != 0)
 
diff --git a/sm5/smartd.c b/sm5/smartd.c
index 72772f34c..36262fa64 100644
--- a/sm5/smartd.c
+++ b/sm5/smartd.c
@@ -37,7 +37,7 @@
 #include "ataprint.h"
 
 extern const char *CVSid1, *CVSid2;
-const char *CVSid3="$Id: smartd.c,v 1.24 2002/10/24 10:56:10 ballen4705 Exp $" 
+const char *CVSid3="$Id: smartd.c,v 1.25 2002/10/24 11:38:11 ballen4705 Exp $" 
 CVSID1 CVSID4 CVSID7;
 
 int daemon_init(void){
@@ -113,8 +113,8 @@ void atadevicescan ( atadevices_t *devices){
     
     printout(LOG_INFO,"Reading Device %s\n", device);
     
-    fd = open ( device , O_RDWR );
-    if ( fd < 0)
+    fd = open(device, O_RDONLY);
+    if (fd < 0)
       // no such device
       continue;
     
@@ -162,38 +162,42 @@ void scsidevicescan ( scsidevices_t *devices){
     
     printout(LOG_INFO,"Reading Device %s\n", device);
     
-    fd = open ( device , O_RDWR );
+    fd=open (device, O_RDWR);
     
-    if ( fd >= 0 && !testunitready (fd)) {
-      if (modesense ( fd, 0x1c, (UINT8 *) &tBuf) != 0){
+    if (fd<0)
+      continue;
+
+    if (!testunitready (fd)) {
+      if (modesense(fd, 0x1c, (UINT8 *) &tBuf)){
 	printout(LOG_INFO,"Device: %s, Failed read of ModePage 1C \n", device);
 	close(fd);
       }
       else
-	if ( scsiSmartSupport( fd, (UINT8 *) &smartsupport) == 0){			
-	  if (!(smartsupport & DEXCPT_ENABLE)){
-	    devices[numscsidevices].fd = fd;
-	    strcpy(devices[numscsidevices].devicename,device);
-	    
-	    printout(LOG_INFO, "Device: %s, Found and is SMART capable\n",device);
-	    
-	    if (logsense ( fd , SUPPORT_LOG_PAGES, (UINT8 *) &tBuf) == 0){
-	      for ( i = 4; i < tBuf[3] + LOGPAGEHDRSIZE ; i++){
-		switch ( tBuf[i]){ 
-		case TEMPERATURE_PAGE:
-		  devices[numscsidevices].TempPageSupported = 1;
-		  break;
-		case SMART_PAGE:
-		  devices[numscsidevices].SmartPageSupported = 1;
-		  break;
-		default:
-		  break;
-		}
-	      }	
-	    }
-	    numscsidevices++;
+	if (!scsiSmartSupport( fd, (UINT8 *) &smartsupport) &&
+	    !(smartsupport & DEXCPT_ENABLE)){
+	  devices[numscsidevices].fd = fd;
+	  strcpy(devices[numscsidevices].devicename,device);
+	  
+	  printout(LOG_INFO, "Device: %s, Found and is SMART capable\n",device);
+	  
+	  if (logsense ( fd , SUPPORT_LOG_PAGES, (UINT8 *) &tBuf) == 0){
+	    for ( i = 4; i < tBuf[3] + LOGPAGEHDRSIZE ; i++){
+	      switch ( tBuf[i]){ 
+	      case TEMPERATURE_PAGE:
+		devices[numscsidevices].TempPageSupported = 1;
+		break;
+	      case SMART_PAGE:
+		devices[numscsidevices].SmartPageSupported = 1;
+		break;
+	      default:
+		break;
+	      }
+	    }	
 	  }
+	  numscsidevices++;
 	}
+	else
+	  close(fd);
     }
   }
 }
diff --git a/sm5/smartd.cpp b/sm5/smartd.cpp
index 5c45d7551..f7ac3107f 100644
--- a/sm5/smartd.cpp
+++ b/sm5/smartd.cpp
@@ -37,7 +37,7 @@
 #include "ataprint.h"
 
 extern const char *CVSid1, *CVSid2;
-const char *CVSid3="$Id: smartd.cpp,v 1.24 2002/10/24 10:56:10 ballen4705 Exp $" 
+const char *CVSid3="$Id: smartd.cpp,v 1.25 2002/10/24 11:38:11 ballen4705 Exp $" 
 CVSID1 CVSID4 CVSID7;
 
 int daemon_init(void){
@@ -113,8 +113,8 @@ void atadevicescan ( atadevices_t *devices){
     
     printout(LOG_INFO,"Reading Device %s\n", device);
     
-    fd = open ( device , O_RDWR );
-    if ( fd < 0)
+    fd = open(device, O_RDONLY);
+    if (fd < 0)
       // no such device
       continue;
     
@@ -162,38 +162,42 @@ void scsidevicescan ( scsidevices_t *devices){
     
     printout(LOG_INFO,"Reading Device %s\n", device);
     
-    fd = open ( device , O_RDWR );
+    fd=open (device, O_RDWR);
     
-    if ( fd >= 0 && !testunitready (fd)) {
-      if (modesense ( fd, 0x1c, (UINT8 *) &tBuf) != 0){
+    if (fd<0)
+      continue;
+
+    if (!testunitready (fd)) {
+      if (modesense(fd, 0x1c, (UINT8 *) &tBuf)){
 	printout(LOG_INFO,"Device: %s, Failed read of ModePage 1C \n", device);
 	close(fd);
       }
       else
-	if ( scsiSmartSupport( fd, (UINT8 *) &smartsupport) == 0){			
-	  if (!(smartsupport & DEXCPT_ENABLE)){
-	    devices[numscsidevices].fd = fd;
-	    strcpy(devices[numscsidevices].devicename,device);
-	    
-	    printout(LOG_INFO, "Device: %s, Found and is SMART capable\n",device);
-	    
-	    if (logsense ( fd , SUPPORT_LOG_PAGES, (UINT8 *) &tBuf) == 0){
-	      for ( i = 4; i < tBuf[3] + LOGPAGEHDRSIZE ; i++){
-		switch ( tBuf[i]){ 
-		case TEMPERATURE_PAGE:
-		  devices[numscsidevices].TempPageSupported = 1;
-		  break;
-		case SMART_PAGE:
-		  devices[numscsidevices].SmartPageSupported = 1;
-		  break;
-		default:
-		  break;
-		}
-	      }	
-	    }
-	    numscsidevices++;
+	if (!scsiSmartSupport( fd, (UINT8 *) &smartsupport) &&
+	    !(smartsupport & DEXCPT_ENABLE)){
+	  devices[numscsidevices].fd = fd;
+	  strcpy(devices[numscsidevices].devicename,device);
+	  
+	  printout(LOG_INFO, "Device: %s, Found and is SMART capable\n",device);
+	  
+	  if (logsense ( fd , SUPPORT_LOG_PAGES, (UINT8 *) &tBuf) == 0){
+	    for ( i = 4; i < tBuf[3] + LOGPAGEHDRSIZE ; i++){
+	      switch ( tBuf[i]){ 
+	      case TEMPERATURE_PAGE:
+		devices[numscsidevices].TempPageSupported = 1;
+		break;
+	      case SMART_PAGE:
+		devices[numscsidevices].SmartPageSupported = 1;
+		break;
+	      default:
+		break;
+	      }
+	    }	
 	  }
+	  numscsidevices++;
 	}
+	else
+	  close(fd);
     }
   }
 }
-- 
GitLab