monit-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[monit-dev] [monit] r246 committed - handle close errors


From: monit
Subject: [monit-dev] [monit] r246 committed - handle close errors
Date: Thu, 16 Sep 2010 10:09:50 +0000

Revision: 246
Author: martin2812
Date: Thu Sep 16 03:02:23 2010
Log: handle close errors
http://code.google.com/p/monit/source/detail?r=246

Modified:
 /trunk/net.c
 /trunk/process/process_common.c
 /trunk/process/sysdep_AIX.c
 /trunk/util.c
 /trunk/validate.c

=======================================
--- /trunk/net.c        Wed Aug 18 07:05:53 2010
+++ /trunk/net.c        Thu Sep 16 03:02:23 2010
@@ -373,10 +373,10 @@
 int create_server_socket(int port, int backlog, const char *bindAddr) {
   int s;
   int status;
-  int flag= 1;
+  int flag = 1;
   struct sockaddr_in myaddr;

-  if((s= socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+  if((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     LogError("%s: Cannot create socket -- %s\n", prog, STRERROR);
     return -1;
   }
@@ -429,7 +429,8 @@
   return s;

   error:
-  close(s);
+  if (close(s) < 0)
+    LogError("%s: Socket close failed -- %s\n", prog, STRERROR);

   return -1;

@@ -442,17 +443,19 @@
  * @return TRUE if the close succeed otherwise FALSE
  */
 int close_socket(int socket) {
-
   int r;

-  shutdown(socket, 2);
-
+  if ((r = shutdown(socket, 2)) < 0)
+    LogError("%s: Socket shutdown failed -- %s\n", prog, STRERROR);
+
+ /* Try to close even if shutdown failed so we won't leak file descriptors */
   do {
-    r= close(socket);
+    r = close(socket);
   } while(r == -1 && errno == EINTR);
+  if (r == -1)
+    LogError("%s: Socket close failed -- %s\n", prog, STRERROR);

   return r;
-
 }


=======================================
--- /trunk/process/process_common.c     Mon Jul 19 04:55:00 2010
+++ /trunk/process/process_common.c     Thu Sep 16 03:02:23 2010
@@ -81,7 +81,7 @@


 /**
- * Reads an process dependent entry or the proc files system
+ * Reads an process dependent entry or the proc filesystem
  * @param buf buffer to write to
  * @param buf_size size of buffer "buf"
  * @param name name of proc service
@@ -93,6 +93,7 @@
   int fd;
   char filename[STRLEN];
   int bytes;
+  int rv = FALSE;

   ASSERT(buf);
   ASSERT(name);
@@ -103,24 +104,26 @@
     snprintf(filename, STRLEN, "/proc/%d/%s", pid, name);

   if ((fd = open(filename, O_RDONLY)) < 0) {
-    DEBUG("cannot open file %s -- %s\n", filename, STRERROR);
-    return FALSE;
+ LogError("%s: Cannot open proc file %s -- %s\n", prog, filename, STRERROR);
+    return rv;
   }

   if ((bytes = read(fd, buf, buf_size-1)) < 0) {
-    close(fd);
-    DEBUG("cannot read file %s -- %s\n", filename, STRERROR);
-    return FALSE;
+ LogError("%s: Cannot read proc file %s -- %s\n", prog, filename, STRERROR);
+    goto error;
   }
   if (bytes_read)
     *bytes_read = bytes;

   /* In case it is a string we have to 0 terminate it our self */
   buf[bytes]='\0';
-
-  close(fd);
-
-  return TRUE;
+  rv = TRUE;
+
+error:
+  if (close(fd) < 0)
+    LogError("%s: Socket close failed -- %s\n", prog, STRERROR);
+
+  return rv;
 }

 /**
=======================================
--- /trunk/process/sysdep_AIX.c Fri May  7 13:08:37 2010
+++ /trunk/process/sysdep_AIX.c Thu Sep 16 03:02:23 2010
@@ -235,15 +235,17 @@

     snprintf(filename, sizeof(filename), "/proc/%d/psinfo", pt[i].pid);
     if ((fd = open(filename, O_RDONLY)) < 0) {
-      DEBUG("cannot open file %s -- %s\n", filename, STRERROR);
+ LogError("%s: Cannot open proc file %s -- %s\n", prog, filename, STRERROR);
       continue;
     }
     if (read(fd, &ps, sizeof(ps)) < 0) {
-      close(fd);
-      DEBUG("cannot read file %s -- %s\n", filename, STRERROR);
+ LogError("%s: Cannot read proc file %s -- %s\n", prog, filename, STRERROR);
+      if (close(fd) < 0)
+        LogError("%s: Socket close failed -- %s\n", prog, STRERROR);
       return FALSE;
     }
-    close(fd);
+    if (close(fd) < 0)
+      LogError("%s: Socket close failed -- %s\n", prog, STRERROR);
pt[i].cmdline = (ps.pr_psargs && *ps.pr_psargs) ? xstrdup(ps.pr_psargs) : xstrdup(procs[i].pi_comm);
   }

=======================================
--- /trunk/util.c       Wed Sep 15 15:04:09 2010
+++ /trunk/util.c       Thu Sep 16 03:02:23 2010
@@ -1162,48 +1162,35 @@
  * Print file hashes from stdin or from the given file
  */
 void Util_printHash(char *filename) {
-
   unsigned char buf[STRLEN], buf2[STRLEN];
-  FILE * fhandle;
+  FILE *fhandle = NULL;
   int fresult;
   int i;

-  if (filename == NULL) {
-    fhandle = stdin;
-  } else {
-    fhandle = fopen(filename, "r");
-    if ( fhandle == NULL ) {
-      goto fileerror;
-    }
-  }
-  fresult=Util_getStreamDigests(fhandle, buf, buf2);
-  if(fresult) {
+  if (! (fhandle = filename ? fopen(filename, "r") : stdin))
     goto fileerror;
-  }
-  if (filename==NULL) {
-    printf("SHA1(stdin) = ");
-  } else {
-    printf("SHA1(%s) = ", filename);
-    fclose(fhandle);
-  }
-  for(i= 0; i < 20; ++i) {
+  if ((fresult = Util_getStreamDigests(fhandle, buf, buf2)))
+    goto fileerror;
+  if (filename && fclose(fhandle))
+    goto fileerror;
+
+  /* SHA1 */
+  printf("SHA1(%s) = ", filename ? filename : "stdin");
+  for (i = 0; i < 20; i++)
     printf("%02x", buf[i]);
-  }
-  if (filename==NULL) {
-    printf("\nMD5(stdin)  = ");
-  } else {
-    printf("\nMD5(%s)  = ", filename);
-  }
-  for(i= 0; i < 16; ++i) {
+  printf("\n");
+
+  /* MD5 */
+  printf("MD5(%s)  = ", filename ? filename : "stdin");
+  for (i = 0; i < 16; i++)
     printf("%02x", buf2[i]);
-  }
   printf("\n");

   return;

 fileerror:

-  printf("monit: %s: %s\n", filename, strerror(errno));
+  printf("monit: %s: %s\n", filename, STRERROR);
   exit(1);
 }

@@ -1246,11 +1233,13 @@
     }
     if(fscanf(file, "%256s", Run.id) != 1) {
       LogError("%s: Error reading id from file '%s'\n", prog, idfile);
-      fclose(file);
+      if (fclose(file))
+ LogError("%s: Error closing file '%s' -- %s\n", prog, idfile, STRERROR);
       return NULL;
     }
   }
-  fclose(file);
+  if (fclose(file))
+ LogError("%s: Error closing file '%s' -- %s\n", prog, idfile, STRERROR);

   return Run.id;
 }
@@ -1275,16 +1264,18 @@
     LogError("%s: pidfile '%s' is not a regular file\n",prog, pidfile);
     return FALSE;
   }
-  if((file= fopen(pidfile,"r")) == (FILE *)NULL) {
+  if((file = fopen(pidfile,"r")) == (FILE *)NULL) {
LogError("%s: Error opening the pidfile '%s' -- %s\n", prog, pidfile, STRERROR);
     return FALSE;
   }
   if(fscanf(file, "%d", &pid) != 1) {
     LogError("%s: Error reading pid from file '%s'\n", prog, pidfile);
-    fclose(file);
+    if (fclose(file))
+ LogError("%s: Error closing file '%s' -- %s\n", prog, pidfile, STRERROR);
     return FALSE;
   }
-  fclose(file);
+  if (fclose(file))
+ LogError("%s: Error closing file '%s' -- %s\n", prog, pidfile, STRERROR);

   if(pid < 0)
     return(FALSE);
@@ -1471,7 +1462,9 @@
           break;
       }

-      fclose(f);
+      if (fclose(f))
+ LogError("%s: Error closing file '%s' -- %s\n", prog, file, STRERROR);
+
       if (fresult) {
         LogError("checksum: file %s stream error (0x%x)\n", file, fresult);
         return FALSE;
@@ -1685,7 +1678,7 @@
   int max_descriptors = 1024;
 #endif
   for(i = 3; i < max_descriptors; i++)
-    (void) close(i);
+    close(i);
   errno= 0;
 }

=======================================
--- /trunk/validate.c   Thu Sep 16 01:47:46 2010
+++ /trunk/validate.c   Thu Sep 16 03:02:23 2010
@@ -1109,7 +1109,8 @@
   }

   final:
-  fclose(file);
+  if (fclose(file))
+ LogError("'%s' cannot close file %s: %s\n", s->name, s->path, STRERROR);
 }

 /**



reply via email to

[Prev in Thread] Current Thread [Next in Thread]