summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJeremy Allison <jra@samba.org>2019-05-16 21:45:21 -0700
committerKarolin Seeger <kseeger@samba.org>2019-06-13 08:52:29 +0000
commit7ce66c8ba7ae12d098714b1848f6981f41b4553e (patch)
treec936daa3c55db8c4ec0d597ce57c0d99145b003b /lib
parent21b99870445e832628a8e0f8c0619c8098e8314c (diff)
downloadsamba-7ce66c8ba7ae12d098714b1848f6981f41b4553e.tar.gz
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 <jra@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org> (cherry picked from commit f20538de041eed1cadbabe2149b2b7cfcb779cb5)
Diffstat (limited to 'lib')
-rw-r--r--lib/util/sys_popen.c57
-rw-r--r--lib/util/sys_popen.h1
2 files changed, 38 insertions, 20 deletions
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