src/utils_cmd_flush.c: Add the `identifier' option.
[collectd.git] / src / exec.c
index b0b716f..ac0175c 100644 (file)
@@ -170,10 +170,9 @@ static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
   {
     char *tmp = strrchr (ci->values[1].value.string, '/');
     if (tmp == NULL)
-      strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
+      sstrncpy (buffer, ci->values[1].value.string, sizeof (buffer));
     else
-      strncpy (buffer, tmp + 1, sizeof (buffer));
-    buffer[sizeof (buffer) - 1] = '\0';
+      sstrncpy (buffer, tmp + 1, sizeof (buffer));
   }
   pl->argv[0] = strdup (buffer);
   if (pl->argv[0] == NULL)
@@ -196,17 +195,16 @@ static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
     {
       if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
       {
-       snprintf (buffer, sizeof (buffer), "%lf",
+       ssnprintf (buffer, sizeof (buffer), "%lf",
            ci->values[i + 1].value.number);
       }
       else
       {
        if (ci->values[i + 1].value.boolean)
-         strncpy (buffer, "true", sizeof (buffer));
+         sstrncpy (buffer, "true", sizeof (buffer));
        else
-         strncpy (buffer, "false", sizeof (buffer));
+         sstrncpy (buffer, "false", sizeof (buffer));
       }
-      buffer[sizeof (buffer) - 1] = '\0';
 
       pl->argv[i] = strdup (buffer);
     }
@@ -380,7 +378,7 @@ static void exec_child (program_list_t *pl) /* {{{ */
 
 /*
  * Creates three pipes (one for reading, one for writing and one for errors),
- * forks a child, sets up the pipes so that fd_in is connected to STDIN of 
+ * forks a child, sets up the pipes so that fd_in is connected to STDIN of
  * the child and fd_out is connected to STDOUT and fd_err is connected to STDERR
  * of the child. Then is calls `exec_child'.
  */
@@ -429,9 +427,19 @@ static int fork_child (program_list_t *pl, int *fd_in, int *fd_out, int *fd_err)
   }
   else if (pid == 0)
   {
-    close (fd_pipe_in[1]);
-    close (fd_pipe_out[0]);
-    close (fd_pipe_err[0]);
+    int fd_num;
+    int fd;
+
+    /* Close all file descriptors but the pipe end we need. */
+    fd_num = getdtablesize ();
+    for (fd = 0; fd < fd_num; fd++)
+    {
+      if ((fd == fd_pipe_in[0])
+         || (fd == fd_pipe_out[1])
+         || (fd == fd_pipe_err[1]))
+       continue;
+      close (fd);
+    }
 
     /* Connect the `in' pipe to STDIN */
     if (fd_pipe_in[0] != STDIN_FILENO)
@@ -564,8 +572,8 @@ static void *exec_read_one (void *arg) /* {{{ */
         pbuffer = buffer + len;
       }
       else
-        pbuffer = buffer; 
-    } 
+        pbuffer = buffer;
+    }
     else if (FD_ISSET(fd_err, &copy))
     {
       char *pnl;