From 7ce66c8ba7ae12d098714b1848f6981f41b4553e Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 16 May 2019 21:45:21 -0700 Subject: lib: popen: Prepare to remove sys_popen(). 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 Reviewed-by: Ralph Boehme (cherry picked from commit f20538de041eed1cadbabe2149b2b7cfcb779cb5) --- lib/util/sys_popen.c | 57 ++++++++++++++++++++++++++++++++++------------------ lib/util/sys_popen.h | 1 + 2 files changed, 38 insertions(+), 20 deletions(-) (limited to 'lib') diff --git a/lib/util/sys_popen.c b/lib/util/sys_popen.c index 709f1822f33..65040508c81 100644 --- a/lib/util/sys_popen.c +++ b/lib/util/sys_popen.c @@ -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. ****************************************************************************/ diff --git a/lib/util/sys_popen.h b/lib/util/sys_popen.h index 6807d3c5061..80ea70efa75 100644 --- a/lib/util/sys_popen.h +++ b/lib/util/sys_popen.h @@ -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 -- cgit v1.2.1