lib: popen: Prepare to remove sys_popen().
authorJeremy Allison <jra@samba.org>
Fri, 17 May 2019 04:45:21 +0000 (21:45 -0700)
committerJeremy Allison <jra@samba.org>
Fri, 24 May 2019 19:00:05 +0000 (19:00 +0000)
Add sys_popenv(char * const argl[]) that uses a NULL
terminated vector array of args. Change sys_popen() to
split up its command string and call sys_popenv().

Once all callers are converted to sys_popenv() we
can remove sys_popen().

BUG: https://bugzilla.samba.org/show_bug.cgi?id=13964

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
lib/util/sys_popen.c
lib/util/sys_popen.h

index 709f1822f332109934fe0985abd626abe12b55d6..65040508c811d72d96c4121adb25d9cf4063b04b 100644 (file)
@@ -110,14 +110,19 @@ typedef struct _popen_list
 
 static popen_list *popen_chain;
 
-int sys_popen(const char *command)
+int sys_popenv(char * const argl[])
 {
        int parent_end, child_end;
        int pipe_fds[2];
        popen_list *entry = NULL;
-       char **argl = NULL;
+       const char *command = argl[0];
        int ret;
 
+       if (argl == NULL) {
+               errno = EINVAL;
+               return -1;
+       }
+
        if (!*command) {
                errno = EINVAL;
                return -1;
@@ -125,8 +130,8 @@ int sys_popen(const char *command)
 
        ret = pipe(pipe_fds);
        if (ret < 0) {
-               DEBUG(0, ("sys_popen: error opening pipe: %s\n",
-                         strerror(errno)));
+               DBG_ERR("error opening pipe: %s\n",
+                         strerror(errno));
                return -1;
        }
 
@@ -135,24 +140,14 @@ int sys_popen(const char *command)
 
        entry = talloc_zero(NULL, popen_list);
        if (entry == NULL) {
-               DEBUG(0, ("sys_popen: malloc failed\n"));
-               goto err_exit;
-       }
-
-       /*
-        * Extract the command and args into a NULL terminated array.
-        */
-
-       argl = extract_args(NULL, command);
-       if (argl == NULL) {
-               DEBUG(0, ("sys_popen: extract_args() failed: %s\n", strerror(errno)));
+               DBG_ERR("talloc failed\n");
                goto err_exit;
        }
 
        entry->child_pid = fork();
 
        if (entry->child_pid == -1) {
-               DEBUG(0, ("sys_popen: fork failed: %s\n", strerror(errno)));
+               DBG_ERR("fork failed: %s\n", strerror(errno));
                goto err_exit;
        }
 
@@ -182,8 +177,8 @@ int sys_popen(const char *command)
 
                ret = execv(argl[0], argl);
                if (ret == -1) {
-                       DEBUG(0, ("sys_popen: ERROR executing command "
-                                 "'%s': %s\n", command, strerror(errno)));
+                       DBG_ERR("ERROR executing command "
+                         "'%s': %s\n", command, strerror(errno));
                }
                _exit (127);
        }
@@ -193,7 +188,6 @@ int sys_popen(const char *command)
         */
 
        close (child_end);
-       TALLOC_FREE(argl);
 
        /* Link into popen_chain. */
        entry->next = popen_chain;
@@ -205,12 +199,35 @@ int sys_popen(const char *command)
 err_exit:
 
        TALLOC_FREE(entry);
-       TALLOC_FREE(argl);
        close(pipe_fds[0]);
        close(pipe_fds[1]);
        return -1;
 }
 
+int sys_popen(const char *command)
+{
+       char **argl = NULL;
+       int ret;
+
+       if (!*command) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       /*
+        * Extract the command and args into a NULL terminated array.
+        */
+
+       argl = extract_args(NULL, command);
+       if (argl == NULL) {
+               DBG_ERR("extract_args() failed: %s\n", strerror(errno));
+               return -1;
+       }
+       ret = sys_popenv(argl);
+       TALLOC_FREE(argl);
+       return ret;
+}
+
 /**************************************************************************
  Wrapper for pclose. Modified from the glibc sources.
 ****************************************************************************/
index 6807d3c50615381001a70062ffb81520ad232982..80ea70efa75514ac682b56f15b01b5bdf47437d1 100644 (file)
@@ -21,6 +21,7 @@
 #define __LIB_SYS_POPEN_H__
 
 int sys_popen(const char *command);
+int sys_popenv(char * const argl[]);
 int sys_pclose(int fd);
 
 #endif