diff options
author | Amar Tumballi <amar@gluster.com> | 2010-08-21 01:54:30 +0000 |
---|---|---|
committer | Anand V. Avati <avati@dev.gluster.com> | 2010-08-21 05:17:37 -0700 |
commit | a437f594608cd307ca2c25871b79966de54cc93f (patch) | |
tree | a1ef88de0020f9e01068f6d4c4a22a1387ea1a05 /libglusterfs/src | |
parent | ad0e7a86c256b6e49daddafd954809b6040804b2 (diff) |
gf_system: fork/exec instead of system
* most of the glusterd 'system()' are replaced with 'gf_system()'
Signed-off-by: Amar Tumballi <amar@gluster.com>
Signed-off-by: Anand V. Avati <avati@dev.gluster.com>
BUG: 1292 ()
URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=1292
Diffstat (limited to 'libglusterfs/src')
-rw-r--r-- | libglusterfs/src/common-utils.c | 56 | ||||
-rw-r--r-- | libglusterfs/src/common-utils.h | 2 |
2 files changed, 58 insertions, 0 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c index 3b904f5224a..eef865ec500 100644 --- a/libglusterfs/src/common-utils.c +++ b/libglusterfs/src/common-utils.c @@ -36,6 +36,7 @@ #include <time.h> #include <locale.h> #include <sys/socket.h> +#include <sys/wait.h> #include <netinet/in.h> #include <arpa/inet.h> #include <signal.h> @@ -1466,3 +1467,58 @@ get_checksum_for_file (int fd, uint32_t *checksum) return ret; } + + +/* One should pass the command here with command with full path, + otherwise, execv will fail */ +int +gf_system (const char *command) +{ + int ret = -1; + pid_t pid = 0; + int status = 0; + int idx = 0; + char *dupcmd = NULL; + char *arg = NULL; + char *tmp = NULL; + char *argv[100] = { NULL, }; + + dupcmd = gf_strdup (command); + if (!dupcmd) + goto out; + + pid = fork (); + if (pid < 0) { + /* failure */ + goto out; + } + if (pid == 0) { + /* Child process */ + /* Step 0: Prepare the argv */ + arg = strtok_r (dupcmd, " ", &tmp); + while (arg) { + argv[idx] = arg; + arg = strtok_r (NULL, " ", &tmp); + idx++; + } + /* Step 1: Close all 'fd' */ + for (idx = 3; idx < 65536; idx++) { + close (idx); + } + /* Step 2: execv (); */ + ret = execvp (argv[0], argv); + + /* Code will not come here at all */ + gf_log ("", GF_LOG_ERROR, "execv of (%s) failed", command); + } + if (pid > 0) { + /* Current, ie, parent process */ + pid = waitpid (pid, &status, 0); + ret = status; + } +out: + if (dupcmd) + GF_FREE (dupcmd); + + return ret; +} diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h index ab230993a5c..93d62f6cbc3 100644 --- a/libglusterfs/src/common-utils.h +++ b/libglusterfs/src/common-utils.h @@ -328,5 +328,7 @@ int gf_unlockfd (int fd); int get_checksum_for_file (int fd, uint32_t *checksum); int log_base2 (unsigned long x); +int gf_system (const char *command); + #endif /* _COMMON_UTILS_H */ |