summaryrefslogtreecommitdiffstats
path: root/libnetdata/popen/popen.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2022-06-09 04:52:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2022-06-09 04:52:39 +0000
commit89f3604407aff8f4cb2ed958252c61e23c767e24 (patch)
tree7fbf408102cab051557d38193524d8c6e991d070 /libnetdata/popen/popen.c
parentAdding upstream version 1.34.1. (diff)
downloadnetdata-89f3604407aff8f4cb2ed958252c61e23c767e24.tar.xz
netdata-89f3604407aff8f4cb2ed958252c61e23c767e24.zip
Adding upstream version 1.35.0.upstream/1.35.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libnetdata/popen/popen.c')
-rw-r--r--libnetdata/popen/popen.c110
1 files changed, 85 insertions, 25 deletions
diff --git a/libnetdata/popen/popen.c b/libnetdata/popen/popen.c
index 33f4bd950..eaeffd32d 100644
--- a/libnetdata/popen/popen.c
+++ b/libnetdata/popen/popen.c
@@ -78,28 +78,34 @@ static void myp_del(pid_t pid) {
#define PIPE_READ 0
#define PIPE_WRITE 1
-/* custom_popene flag definitions */
-#define FLAG_CREATE_PIPE 1 // Create a pipe like popen() when set, otherwise set stdout to /dev/null
-#define FLAG_CLOSE_FD 2 // Close all file descriptors other than STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO
+static inline void convert_argv_to_string(char *dst, size_t size, const char *spawn_argv[]) {
+ int i;
+ for(i = 0; spawn_argv[i] ;i++) {
+ if(i == 0) snprintfz(dst, size, "%s", spawn_argv[i]);
+ else {
+ size_t len = strlen(dst);
+ snprintfz(&dst[len], size - len, " '%s'", spawn_argv[i]);
+ }
+ }
+}
/*
- * Returns -1 on failure, 0 on success. When FLAG_CREATE_PIPE is set, on success set the FILE *fp pointer.
+ * Returns -1 on failure, 0 on success. When POPEN_FLAG_CREATE_PIPE is set, on success set the FILE *fp pointer.
*/
-static inline int custom_popene(const char *command, volatile pid_t *pidptr, char **env, uint8_t flags, FILE **fpp) {
+static int custom_popene(volatile pid_t *pidptr, char **env, uint8_t flags, FILE **fpp, const char *command, const char *spawn_argv[]) {
+ // create a string to be logged about the command we are running
+ char command_to_be_logged[2048];
+ convert_argv_to_string(command_to_be_logged, sizeof(command_to_be_logged), spawn_argv);
+ // info("custom_popene() running command: %s", command_to_be_logged);
+
FILE *fp = NULL;
int ret = 0; // success by default
int pipefd[2], error;
pid_t pid;
- char *const spawn_argv[] = {
- "sh",
- "-c",
- (char *)command,
- NULL
- };
posix_spawnattr_t attr;
posix_spawn_file_actions_t fa;
- if (flags & FLAG_CREATE_PIPE) {
+ if (flags & POPEN_FLAG_CREATE_PIPE) {
if (pipe(pipefd) == -1)
return -1;
if ((fp = fdopen(pipefd[PIPE_READ], "r")) == NULL) {
@@ -107,7 +113,7 @@ static inline int custom_popene(const char *command, volatile pid_t *pidptr, cha
}
}
- if (flags & FLAG_CLOSE_FD) {
+ if (flags & POPEN_FLAG_CLOSE_FD) {
// Mark all files to be closed by the exec() stage of posix_spawn()
int i;
for (i = (int) (sysconf(_SC_OPEN_MAX) - 1); i >= 0; i--) {
@@ -117,7 +123,7 @@ static inline int custom_popene(const char *command, volatile pid_t *pidptr, cha
}
if (!posix_spawn_file_actions_init(&fa)) {
- if (flags & FLAG_CREATE_PIPE) {
+ if (flags & POPEN_FLAG_CREATE_PIPE) {
// move the pipe to stdout in the child
if (posix_spawn_file_actions_adddup2(&fa, pipefd[PIPE_WRITE], STDOUT_FILENO)) {
error("posix_spawn_file_actions_adddup2() failed");
@@ -150,22 +156,22 @@ static inline int custom_popene(const char *command, volatile pid_t *pidptr, cha
// Take the lock while we fork to ensure we don't race with SIGCHLD
// delivery on a process which exits quickly.
myp_add_lock();
- if (!posix_spawn(&pid, "/bin/sh", &fa, &attr, spawn_argv, env)) {
+ if (!posix_spawn(&pid, command, &fa, &attr, (char * const*)spawn_argv, env)) {
*pidptr = pid;
myp_add_locked(pid);
- debug(D_CHILDS, "Spawned command: '%s' on pid %d from parent pid %d.", command, pid, getpid());
+ debug(D_CHILDS, "Spawned command: \"%s\" on pid %d from parent pid %d.", command_to_be_logged, pid, getpid());
} else {
myp_add_unlock();
- error("Failed to spawn command: '%s' from parent pid %d.", command, getpid());
- if (flags & FLAG_CREATE_PIPE) {
+ error("Failed to spawn command: \"%s\" from parent pid %d.", command_to_be_logged, getpid());
+ if (flags & POPEN_FLAG_CREATE_PIPE) {
fclose(fp);
}
ret = -1;
}
- if (flags & FLAG_CREATE_PIPE) {
+ if (flags & POPEN_FLAG_CREATE_PIPE) {
close(pipefd[PIPE_WRITE]);
if (0 == ret) // on success set FILE * pointer
- *fpp = fp;
+ if(fpp) *fpp = fp;
}
if (!error) {
@@ -181,8 +187,9 @@ static inline int custom_popene(const char *command, volatile pid_t *pidptr, cha
error_after_posix_spawn_file_actions_init:
if (posix_spawn_file_actions_destroy(&fa))
error("posix_spawn_file_actions_destroy");
+
error_after_pipe:
- if (flags & FLAG_CREATE_PIPE) {
+ if (flags & POPEN_FLAG_CREATE_PIPE) {
if (fp)
fclose(fp);
else
@@ -193,6 +200,41 @@ error_after_pipe:
return -1;
}
+int custom_popene_variadic_internal_dont_use_directly(volatile pid_t *pidptr, char **env, uint8_t flags, FILE **fpp, const char *command, ...) {
+ // convert the variable list arguments into what posix_spawn() needs
+ // all arguments are expected strings
+ va_list args;
+ int args_count;
+
+ // count the number variable parameters
+ // the variable parameters are expected NULL terminated
+ {
+ const char *s;
+
+ va_start(args, command);
+ args_count = 0;
+ while ((s = va_arg(args, const char *))) args_count++;
+ va_end(args);
+ }
+
+ // create a string pointer array as needed by posix_spawn()
+ // variable array in the stack
+ const char *spawn_argv[args_count + 1];
+ {
+ const char *s;
+ va_start(args, command);
+ int i;
+ for (i = 0; i < args_count; i++) {
+ s = va_arg(args, const char *);
+ spawn_argv[i] = s;
+ }
+ spawn_argv[args_count] = NULL;
+ va_end(args);
+ }
+
+ return custom_popene(pidptr, env, flags, fpp, command, spawn_argv);
+}
+
// See man environ
extern char **environ;
@@ -252,19 +294,37 @@ int myp_reap(pid_t pid) {
FILE *mypopen(const char *command, volatile pid_t *pidptr) {
FILE *fp = NULL;
- (void)custom_popene(command, pidptr, environ, FLAG_CREATE_PIPE | FLAG_CLOSE_FD, &fp);
+ const char *spawn_argv[] = {
+ "sh",
+ "-c",
+ command,
+ NULL
+ };
+ (void)custom_popene(pidptr, environ, POPEN_FLAG_CREATE_PIPE|POPEN_FLAG_CLOSE_FD, &fp, "/bin/sh", spawn_argv);
return fp;
}
FILE *mypopene(const char *command, volatile pid_t *pidptr, char **env) {
FILE *fp = NULL;
- (void)custom_popene(command, pidptr, env, FLAG_CREATE_PIPE | FLAG_CLOSE_FD, &fp);
+ const char *spawn_argv[] = {
+ "sh",
+ "-c",
+ command,
+ NULL
+ };
+ (void)custom_popene( pidptr, env, POPEN_FLAG_CREATE_PIPE|POPEN_FLAG_CLOSE_FD, &fp, "/bin/sh", spawn_argv);
return fp;
}
// returns 0 on success, -1 on failure
int netdata_spawn(const char *command, volatile pid_t *pidptr) {
- return custom_popene(command, pidptr, environ, 0, NULL);
+ const char *spawn_argv[] = {
+ "sh",
+ "-c",
+ command,
+ NULL
+ };
+ return custom_popene( pidptr, environ, POPEN_FLAG_NONE, NULL, "/bin/sh", spawn_argv);
}
int custom_pclose(FILE *fp, pid_t pid) {
@@ -340,4 +400,4 @@ int mypclose(FILE *fp, pid_t pid)
int netdata_spawn_waitpid(pid_t pid)
{
return custom_pclose(NULL, pid);
-} \ No newline at end of file
+}