Blame SOURCES/0001-Add-os_exec-helper-to-run-external-programs.patch

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