Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / exec.c
index 973cd52..ea31b30 100644 (file)
@@ -1,6 +1,8 @@
 /**
  * collectd - src/exec.c
- * Copyright (C) 2007,2008  Florian octo Forster
+ * Copyright (C) 2007-2009  Florian octo Forster
+ * Copyright (C) 2007-2009  Sebastian Harl
+ * Copyright (C) 2008       Peter Holik
  *
  * 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
@@ -17,6 +19,8 @@
  *
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
+ *   Sebastian Harl <sh at tokkee.org>
+ *   Peter Holik <peter at holik.at>
  **/
 
 #include "collectd.h"
@@ -585,7 +589,17 @@ static void *exec_read_one (void *arg) /* {{{ */
         if (errno == EAGAIN || errno == EINTR)  continue;
         break;
       }
-      else if (len == 0) break;  /* We've reached EOF */
+      else if (len == 0)
+      {
+       /* We've reached EOF */
+       NOTICE ("exec plugin: Program `%s' has closed STDERR.",
+           pl->exec);
+       close (fd_err);
+       FD_CLR (fd_err, &fdset);
+       highest_fd = fd;
+       fd_err = -1;
+       continue;
+      }
 
       pbuffer_err[len] = '\0';
 
@@ -615,6 +629,7 @@ static void *exec_read_one (void *arg) /* {{{ */
     copy = fdset;
   }
 
+  DEBUG ("exec plugin: exec_read_one: Waiting for `%s' to exit.", pl->exec);
   if (waitpid (pl->pid, &status, 0) > 0)
     pl->status = status;
 
@@ -628,7 +643,8 @@ static void *exec_read_one (void *arg) /* {{{ */
   pthread_mutex_unlock (&pl_lock);
 
   close (fd);
-  close (fd_err);
+  if (fd_err >= 0)
+    close (fd_err);
 
   pthread_exit ((void *) 0);
   return (NULL);