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

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