network plugin: Don't blindly re-initialize everything in the init callback.
[collectd.git] / src / exec.c
index c6a58d1..5eae906 100644 (file)
@@ -325,6 +325,25 @@ static void exec_child (program_list_t *pl) /* {{{ */
     }
   } /* if (pl->group == NULL) */
 
+#if HAVE_SETGROUPS
+  if (getuid () == 0)
+  {
+    gid_t  glist[2];
+    size_t glist_len;
+
+    glist[0] = gid;
+    glist_len = 1;
+
+    if ((gid != egid) && (egid != -1))
+    {
+      glist[1] = egid;
+      glist_len = 2;
+    }
+
+    setgroups (glist_len, glist);
+  }
+#endif /* HAVE_SETGROUPS */
+
   status = setgid (gid);
   if (status != 0)
   {
@@ -402,8 +421,17 @@ static int fork_child (program_list_t *pl, int *fd_in, int *fd_out) /* {{{ */
   }
   else if (pid == 0)
   {
-    close (fd_pipe_in[1]);
-    close (fd_pipe_out[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]))
+       continue;
+      close (fd);
+    }
 
     /* If the `out' pipe has the filedescriptor STDIN we have to be careful
      * with the `dup's below. So, if this is the case we have to handle the