|
|
5a6a09 |
From 89de07a9442072f88d49869d8ecd8d42bae050a0 Mon Sep 17 00:00:00 2001
|
|
|
5a6a09 |
From: Jouni Malinen <jouni@qca.qualcomm.com>
|
|
|
5a6a09 |
Date: Mon, 6 Oct 2014 16:27:44 +0300
|
|
|
5a6a09 |
Subject: [PATCH 1/2] Add os_exec() helper to run external programs
|
|
|
5a6a09 |
|
|
|
5a6a09 |
Signed-off-by: Jouni Malinen <jouni@qca.qualcomm.com>
|
|
|
5a6a09 |
---
|
|
|
5a6a09 |
src/utils/os.h | 9 +++++++++
|
|
|
5a6a09 |
src/utils/os_unix.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
5a6a09 |
src/utils/os_win32.c | 6 ++++++
|
|
|
5a6a09 |
3 files changed, 70 insertions(+)
|
|
|
5a6a09 |
|
|
|
5a6a09 |
diff --git a/src/utils/os.h b/src/utils/os.h
|
|
|
5a6a09 |
index f196209..b9247d8 100644
|
|
|
5a6a09 |
--- a/src/utils/os.h
|
|
|
5a6a09 |
+++ b/src/utils/os.h
|
|
|
5a6a09 |
@@ -597,14 +597,23 @@ size_t os_strlcpy(char *dest, const char *src, size_t siz);
|
|
|
5a6a09 |
* Returns: Total length of the target string (length of src) (not including
|
|
|
5a6a09 |
* NUL-termination)
|
|
|
5a6a09 |
*
|
|
|
5a6a09 |
* This function matches in behavior with the strlcpy(3) function in OpenBSD.
|
|
|
5a6a09 |
*/
|
|
|
5a6a09 |
size_t os_strlcpy(char *dest, const char *src, size_t siz);
|
|
|
5a6a09 |
|
|
|
5a6a09 |
+/**
|
|
|
5a6a09 |
+ * os_exec - Execute an external program
|
|
|
5a6a09 |
+ * @program: Path to the program
|
|
|
5a6a09 |
+ * @arg: Command line argument string
|
|
|
5a6a09 |
+ * @wait_completion: Whether to wait until the program execution completes
|
|
|
5a6a09 |
+ * Returns: 0 on success, -1 on error
|
|
|
5a6a09 |
+ */
|
|
|
5a6a09 |
+int os_exec(const char *program, const char *arg, int wait_completion);
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
|
|
|
5a6a09 |
#ifdef OS_REJECT_C_LIB_FUNCTIONS
|
|
|
5a6a09 |
#define malloc OS_DO_NOT_USE_malloc
|
|
|
5a6a09 |
#define realloc OS_DO_NOT_USE_realloc
|
|
|
5a6a09 |
#define free OS_DO_NOT_USE_free
|
|
|
5a6a09 |
#define memcpy OS_DO_NOT_USE_memcpy
|
|
|
5a6a09 |
#define memmove OS_DO_NOT_USE_memmove
|
|
|
5a6a09 |
diff --git a/src/utils/os_unix.c b/src/utils/os_unix.c
|
|
|
5a6a09 |
index 7498967..523a4d0 100644
|
|
|
5a6a09 |
--- a/src/utils/os_unix.c
|
|
|
5a6a09 |
+++ b/src/utils/os_unix.c
|
|
|
5a6a09 |
@@ -5,14 +5,15 @@
|
|
|
5a6a09 |
* This software may be distributed under the terms of the BSD license.
|
|
|
5a6a09 |
* See README for more details.
|
|
|
5a6a09 |
*/
|
|
|
5a6a09 |
|
|
|
5a6a09 |
#include "includes.h"
|
|
|
5a6a09 |
|
|
|
5a6a09 |
#include <time.h>
|
|
|
5a6a09 |
+#include <sys/wait.h>
|
|
|
5a6a09 |
|
|
|
5a6a09 |
#ifdef ANDROID
|
|
|
5a6a09 |
#include <linux/capability.h>
|
|
|
5a6a09 |
#include <linux/prctl.h>
|
|
|
5a6a09 |
#include <private/android_filesystem_config.h>
|
|
|
5a6a09 |
#endif /* ANDROID */
|
|
|
5a6a09 |
|
|
|
5a6a09 |
@@ -550,7 +551,61 @@ char * os_strdup(const char *s)
|
|
|
5a6a09 |
return NULL;
|
|
|
5a6a09 |
os_memcpy(d, s, len);
|
|
|
5a6a09 |
d[len] = '\0';
|
|
|
5a6a09 |
return d;
|
|
|
5a6a09 |
}
|
|
|
5a6a09 |
|
|
|
5a6a09 |
#endif /* WPA_TRACE */
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+int os_exec(const char *program, const char *arg, int wait_completion)
|
|
|
5a6a09 |
+{
|
|
|
5a6a09 |
+ pid_t pid;
|
|
|
5a6a09 |
+ int pid_status;
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+ pid = fork();
|
|
|
5a6a09 |
+ if (pid < 0) {
|
|
|
5a6a09 |
+ perror("fork");
|
|
|
5a6a09 |
+ return -1;
|
|
|
5a6a09 |
+ }
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+ if (pid == 0) {
|
|
|
5a6a09 |
+ /* run the external command in the child process */
|
|
|
5a6a09 |
+ const int MAX_ARG = 30;
|
|
|
5a6a09 |
+ char *_program, *_arg, *pos;
|
|
|
5a6a09 |
+ char *argv[MAX_ARG + 1];
|
|
|
5a6a09 |
+ int i;
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+ _program = os_strdup(program);
|
|
|
5a6a09 |
+ _arg = os_strdup(arg);
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+ argv[0] = _program;
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+ i = 1;
|
|
|
5a6a09 |
+ pos = _arg;
|
|
|
5a6a09 |
+ while (i < MAX_ARG && pos && *pos) {
|
|
|
5a6a09 |
+ while (*pos == ' ')
|
|
|
5a6a09 |
+ pos++;
|
|
|
5a6a09 |
+ if (*pos == '\0')
|
|
|
5a6a09 |
+ break;
|
|
|
5a6a09 |
+ argv[i++] = pos;
|
|
|
5a6a09 |
+ pos = os_strchr(pos, ' ');
|
|
|
5a6a09 |
+ if (pos)
|
|
|
5a6a09 |
+ *pos++ = '\0';
|
|
|
5a6a09 |
+ }
|
|
|
5a6a09 |
+ argv[i] = NULL;
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+ execv(program, argv);
|
|
|
5a6a09 |
+ perror("execv");
|
|
|
5a6a09 |
+ os_free(_program);
|
|
|
5a6a09 |
+ os_free(_arg);
|
|
|
5a6a09 |
+ exit(0);
|
|
|
5a6a09 |
+ return -1;
|
|
|
5a6a09 |
+ }
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+ if (wait_completion) {
|
|
|
5a6a09 |
+ /* wait for the child process to complete in the parent */
|
|
|
5a6a09 |
+ waitpid(pid, &pid_status, 0);
|
|
|
5a6a09 |
+ }
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+ return 0;
|
|
|
5a6a09 |
+}
|
|
|
5a6a09 |
diff --git a/src/utils/os_win32.c b/src/utils/os_win32.c
|
|
|
5a6a09 |
index 55937de..57ee132 100644
|
|
|
5a6a09 |
--- a/src/utils/os_win32.c
|
|
|
5a6a09 |
+++ b/src/utils/os_win32.c
|
|
|
5a6a09 |
@@ -254,7 +254,13 @@ int os_memcmp_const(const void *a, const void *b, size_t len)
|
|
|
5a6a09 |
*dest = '\0';
|
|
|
5a6a09 |
while (*s++)
|
|
|
5a6a09 |
; /* determine total src string length */
|
|
|
5a6a09 |
}
|
|
|
5a6a09 |
|
|
|
5a6a09 |
return s - src - 1;
|
|
|
5a6a09 |
}
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+
|
|
|
5a6a09 |
+int os_exec(const char *program, const char *arg, int wait_completion)
|
|
|
5a6a09 |
+{
|
|
|
5a6a09 |
+ return -1;
|
|
|
5a6a09 |
+}
|
|
|
5a6a09 |
--
|
|
|
5a6a09 |
1.9.3
|
|
|
5a6a09 |
|