]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Clean up the pipe closing in openvpn_popen()
authorDavid Sommerseth <davids@redhat.com>
Fri, 5 Sep 2014 15:04:58 +0000 (17:04 +0200)
committerDavid Sommerseth <davids@redhat.com>
Wed, 10 Sep 2014 09:56:18 +0000 (11:56 +0200)
Do the proper closing of the pipe ends which is not being used,
also in error situations.  Moved the closing of the parent side
before the waitpid(), to be consistent with the child side (as
early as possible).

Also improved the error messages with more details.

[v2 - Don't call close() if execve() fails]

Signed-off-by: David Sommerseth <davids@redhat.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: 20140909202408.GK1118@greenie.muc.de
URL: http://article.gmane.org/gmane.network.openvpn.devel/9036

src/openvpn/misc.c

index 19bbef7d4a41aee643b5a9671b16d46542e6e1a1..61bc523dda0907a8af2576a1f4c8da20fab10170 100644 (file)
@@ -365,27 +365,29 @@ openvpn_popen (const struct argv *a,  const struct env_set *es)
                      pid = fork ();
                      if (pid == (pid_t)0) /* child side */
                        {
-                         close (pipe_stdout[0]);
+                         close (pipe_stdout[0]);  /* Close read end */
                          dup2 (pipe_stdout[1],1);
                          execve (cmd, argv, envp);
                          exit (127);
                        }
-                     else if (pid < (pid_t)0) /* fork failed */
-                       {
-                         msg (M_ERR, "openvpn_popen: unable to fork");
-                       }
-                     else /* parent side */
+                     else if (pid > (pid_t)0) /* parent side */
                        {
                           int status = 0;
 
+                          close (pipe_stdout[1]); /* Close write end */
                           waitpid(pid, &status, 0);
                           ret = pipe_stdout[0];
+                       }
+                     else /* fork failed */
+                       {
+                          close (pipe_stdout[0]);
                           close (pipe_stdout[1]);
+                         msg (M_ERR, "openvpn_popen: unable to fork %s", cmd);
                        }
              }
              else {
-                     msg (M_WARN, "openvpn_popen: unable to create stdout pipe");
-                     ret = -1;
+                msg (M_WARN, "openvpn_popen: unable to create stdout pipe for %s", cmd);
+                ret = -1;
              }
        }
       else if (!warn_shown && (script_security < SSEC_SCRIPTS))