|
John W. Linville |
6bf0486 |
From 89de07a9442072f88d49869d8ecd8d42bae050a0 Mon Sep 17 00:00:00 2001
|
|
John W. Linville |
6bf0486 |
From: Jouni Malinen <jouni@qca.qualcomm.com>
|
|
John W. Linville |
6bf0486 |
Date: Mon, 6 Oct 2014 16:27:44 +0300
|
|
John W. Linville |
6bf0486 |
Subject: [PATCH 1/3] Add os_exec() helper to run external programs
|
|
John W. Linville |
6bf0486 |
|
|
John W. Linville |
6bf0486 |
Signed-off-by: Jouni Malinen <jouni@qca.qualcomm.com>
|
|
John W. Linville |
6bf0486 |
---
|
|
John W. Linville |
6bf0486 |
src/utils/os.h | 9 +++++++++
|
|
John W. Linville |
6bf0486 |
src/utils/os_unix.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
John W. Linville |
6bf0486 |
src/utils/os_win32.c | 6 ++++++
|
|
John W. Linville |
6bf0486 |
3 files changed, 70 insertions(+)
|
|
John W. Linville |
6bf0486 |
|
|
John W. Linville |
6bf0486 |
diff -up hostapd-2.0/src/utils/os.h.helper hostapd-2.0/src/utils/os.h
|
|
John W. Linville |
6bf0486 |
--- hostapd-2.0/src/utils/os.h.helper 2013-01-12 10:42:53.000000000 -0500
|
|
John W. Linville |
6bf0486 |
+++ hostapd-2.0/src/utils/os.h 2014-10-23 16:04:33.241498961 -0400
|
|
John W. Linville |
6bf0486 |
@@ -506,6 +506,15 @@ static inline void * os_realloc_array(vo
|
|
John W. Linville |
6bf0486 |
*/
|
|
John W. Linville |
6bf0486 |
size_t os_strlcpy(char *dest, const char *src, size_t siz);
|
|
John W. Linville |
6bf0486 |
|
|
John W. Linville |
6bf0486 |
+/**
|
|
John W. Linville |
6bf0486 |
+ * os_exec - Execute an external program
|
|
John W. Linville |
6bf0486 |
+ * @program: Path to the program
|
|
John W. Linville |
6bf0486 |
+ * @arg: Command line argument string
|
|
John W. Linville |
6bf0486 |
+ * @wait_completion: Whether to wait until the program execution completes
|
|
John W. Linville |
6bf0486 |
+ * Returns: 0 on success, -1 on error
|
|
John W. Linville |
6bf0486 |
+ */
|
|
John W. Linville |
6bf0486 |
+int os_exec(const char *program, const char *arg, int wait_completion);
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
|
|
John W. Linville |
6bf0486 |
#ifdef OS_REJECT_C_LIB_FUNCTIONS
|
|
John W. Linville |
6bf0486 |
#define malloc OS_DO_NOT_USE_malloc
|
|
John W. Linville |
6bf0486 |
diff -up hostapd-2.0/src/utils/os_unix.c.helper hostapd-2.0/src/utils/os_unix.c
|
|
John W. Linville |
6bf0486 |
--- hostapd-2.0/src/utils/os_unix.c.helper 2013-01-12 10:42:53.000000000 -0500
|
|
John W. Linville |
6bf0486 |
+++ hostapd-2.0/src/utils/os_unix.c 2014-10-23 16:04:33.242498969 -0400
|
|
John W. Linville |
6bf0486 |
@@ -9,6 +9,7 @@
|
|
John W. Linville |
6bf0486 |
#include "includes.h"
|
|
John W. Linville |
6bf0486 |
|
|
John W. Linville |
6bf0486 |
#include <time.h>
|
|
John W. Linville |
6bf0486 |
+#include <sys/wait.h>
|
|
John W. Linville |
6bf0486 |
|
|
John W. Linville |
6bf0486 |
#ifdef ANDROID
|
|
John W. Linville |
6bf0486 |
#include <linux/capability.h>
|
|
John W. Linville |
6bf0486 |
@@ -486,3 +487,57 @@ char * os_strdup(const char *s)
|
|
John W. Linville |
6bf0486 |
}
|
|
John W. Linville |
6bf0486 |
|
|
John W. Linville |
6bf0486 |
#endif /* WPA_TRACE */
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+int os_exec(const char *program, const char *arg, int wait_completion)
|
|
John W. Linville |
6bf0486 |
+{
|
|
John W. Linville |
6bf0486 |
+ pid_t pid;
|
|
John W. Linville |
6bf0486 |
+ int pid_status;
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+ pid = fork();
|
|
John W. Linville |
6bf0486 |
+ if (pid < 0) {
|
|
John W. Linville |
6bf0486 |
+ perror("fork");
|
|
John W. Linville |
6bf0486 |
+ return -1;
|
|
John W. Linville |
6bf0486 |
+ }
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+ if (pid == 0) {
|
|
John W. Linville |
6bf0486 |
+ /* run the external command in the child process */
|
|
John W. Linville |
6bf0486 |
+ const int MAX_ARG = 30;
|
|
John W. Linville |
6bf0486 |
+ char *_program, *_arg, *pos;
|
|
John W. Linville |
6bf0486 |
+ char *argv[MAX_ARG + 1];
|
|
John W. Linville |
6bf0486 |
+ int i;
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+ _program = os_strdup(program);
|
|
John W. Linville |
6bf0486 |
+ _arg = os_strdup(arg);
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+ argv[0] = _program;
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+ i = 1;
|
|
John W. Linville |
6bf0486 |
+ pos = _arg;
|
|
John W. Linville |
6bf0486 |
+ while (i < MAX_ARG && pos && *pos) {
|
|
John W. Linville |
6bf0486 |
+ while (*pos == ' ')
|
|
John W. Linville |
6bf0486 |
+ pos++;
|
|
John W. Linville |
6bf0486 |
+ if (*pos == '\0')
|
|
John W. Linville |
6bf0486 |
+ break;
|
|
John W. Linville |
6bf0486 |
+ argv[i++] = pos;
|
|
John W. Linville |
6bf0486 |
+ pos = os_strchr(pos, ' ');
|
|
John W. Linville |
6bf0486 |
+ if (pos)
|
|
John W. Linville |
6bf0486 |
+ *pos++ = '\0';
|
|
John W. Linville |
6bf0486 |
+ }
|
|
John W. Linville |
6bf0486 |
+ argv[i] = NULL;
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+ execv(program, argv);
|
|
John W. Linville |
6bf0486 |
+ perror("execv");
|
|
John W. Linville |
6bf0486 |
+ os_free(_program);
|
|
John W. Linville |
6bf0486 |
+ os_free(_arg);
|
|
John W. Linville |
6bf0486 |
+ exit(0);
|
|
John W. Linville |
6bf0486 |
+ return -1;
|
|
John W. Linville |
6bf0486 |
+ }
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+ if (wait_completion) {
|
|
John W. Linville |
6bf0486 |
+ /* wait for the child process to complete in the parent */
|
|
John W. Linville |
6bf0486 |
+ waitpid(pid, &pid_status, 0);
|
|
John W. Linville |
6bf0486 |
+ }
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+ return 0;
|
|
John W. Linville |
6bf0486 |
+}
|
|
John W. Linville |
6bf0486 |
diff -up hostapd-2.0/src/utils/os_win32.c.helper hostapd-2.0/src/utils/os_win32.c
|
|
John W. Linville |
6bf0486 |
--- hostapd-2.0/src/utils/os_win32.c.helper 2013-01-12 10:42:53.000000000 -0500
|
|
John W. Linville |
6bf0486 |
+++ hostapd-2.0/src/utils/os_win32.c 2014-10-23 16:04:33.242498969 -0400
|
|
John W. Linville |
6bf0486 |
@@ -233,3 +233,9 @@ size_t os_strlcpy(char *dest, const char
|
|
John W. Linville |
6bf0486 |
|
|
John W. Linville |
6bf0486 |
return s - src - 1;
|
|
John W. Linville |
6bf0486 |
}
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+
|
|
John W. Linville |
6bf0486 |
+int os_exec(const char *program, const char *arg, int wait_completion)
|
|
John W. Linville |
6bf0486 |
+{
|
|
John W. Linville |
6bf0486 |
+ return -1;
|
|
John W. Linville |
6bf0486 |
+}
|