diff options
author | Jouni Malinen <jouni@qca.qualcomm.com> | 2014-10-06 16:27:44 +0300 |
---|---|---|
committer | Wolfgang Wiedmeyer <wolfgit@wiedmeyer.de> | 2015-10-20 22:05:59 +0200 |
commit | 2718efcea5d1e18faef7181533456c7ed58025c7 (patch) | |
tree | 3319078bb2899a8e4948881b9400b49264ffa93c | |
parent | ec2c8f6ee9d86f4127098083ca5fe3342b61bbbd (diff) | |
download | external_wpa_supplicant_8-2718efcea5d1e18faef7181533456c7ed58025c7.zip external_wpa_supplicant_8-2718efcea5d1e18faef7181533456c7ed58025c7.tar.gz external_wpa_supplicant_8-2718efcea5d1e18faef7181533456c7ed58025c7.tar.bz2 |
Add os_exec() helper to run external programs
Change-Id: I579af1fa8c2f85622ffddb186ba799dcb9ac4b6f
Signed-off-by: Jouni Malinen <jouni@qca.qualcomm.com>
Tested-by: Wolfgang Wiedmeyer <wolfgit@wiedmeyer.de>
-rw-r--r-- | src/utils/os.h | 9 | ||||
-rw-r--r-- | src/utils/os_unix.c | 54 | ||||
-rw-r--r-- | src/utils/os_win32.c | 5 |
3 files changed, 68 insertions, 0 deletions
diff --git a/src/utils/os.h b/src/utils/os.h index ad20834..aa04099 100644 --- a/src/utils/os.h +++ b/src/utils/os.h @@ -506,6 +506,15 @@ static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size) */ size_t os_strlcpy(char *dest, const char *src, size_t siz); +/** + * os_exec - Execute an external program + * @program: Path to the program + * @arg: Command line argument string + * @wait_completion: Whether to wait until the program execution completes + * Returns: 0 on success, -1 on error + */ +int os_exec(const char *program, const char *arg, int wait_completion); + #ifdef OS_REJECT_C_LIB_FUNCTIONS #define malloc OS_DO_NOT_USE_malloc diff --git a/src/utils/os_unix.c b/src/utils/os_unix.c index 23a93be..380ece8 100644 --- a/src/utils/os_unix.c +++ b/src/utils/os_unix.c @@ -9,6 +9,7 @@ #include "includes.h" #include <time.h> +#include <sys/wait.h> #ifdef ANDROID #include <linux/capability.h> @@ -486,3 +487,56 @@ char * os_strdup(const char *s) } #endif /* WPA_TRACE */ + +int os_exec(const char *program, const char *arg, int wait_completion) +{ + pid_t pid; + int pid_status; + + pid = fork(); + if (pid < 0) { + perror("fork"); + return -1; + } + + if (pid == 0) { + /* run the external command in the child process */ + const int MAX_ARG = 30; + char *_program, *_arg, *pos; + char *argv[MAX_ARG + 1]; + int i; + + _program = os_strdup(program); + _arg = os_strdup(arg); + + argv[0] = _program; + + i = 1; + pos = _arg; + while (i < MAX_ARG && pos && *pos) { + while (*pos == ' ') + pos++; + if (*pos == '\0') + break; + argv[i++] = pos; + pos = os_strchr(pos, ' '); + if (pos) + *pos++ = '\0'; + } + argv[i] = NULL; + + execv(program, argv); + perror("execv"); + os_free(_program); + os_free(_arg); + exit(0); + return -1; + } + + if (wait_completion) { + /* wait for the child process to complete in the parent */ + waitpid(pid, &pid_status, 0); + } + + return 0; +} diff --git a/src/utils/os_win32.c b/src/utils/os_win32.c index 163cebe..eedc941 100644 --- a/src/utils/os_win32.c +++ b/src/utils/os_win32.c @@ -233,3 +233,8 @@ size_t os_strlcpy(char *dest, const char *src, size_t siz) return s - src - 1; } + +int os_exec(const char *program, const char *arg, int wait_completion) +{ + return -1; +} |