From e9d01447026f12897a57cf1d7ed5ec5df451575e Mon Sep 17 00:00:00 2001 From: Jiung Yu Date: Wed, 15 Jan 2014 17:48:28 +0900 Subject: [PATCH] Replace file execution method Change-Id: Icb1ed25bee0f61d17bb4b1a77afcc6c2009bd8ff Signed-off-by: Yu jiung --- include/wifi-direct-util.h | 1 + packaging/wifi-direct-manager.changes | 3 + packaging/wifi-direct-manager.spec | 2 +- plugin/wpasupplicant/wfd-plugin-wpasupplicant.c | 45 +++++++++++ src/wifi-direct-util.c | 101 +++++++++++++++++++++--- 5 files changed, 139 insertions(+), 13 deletions(-) diff --git a/include/wifi-direct-util.h b/include/wifi-direct-util.h index 3dbc33d..505fbc5 100644 --- a/include/wifi-direct-util.h +++ b/include/wifi-direct-util.h @@ -71,6 +71,7 @@ #endif /* USE_DLOG */ +gboolean wfd_util_execute_file(const char *file_path, char *const args[], char *const envs[]); int wfd_util_freq_to_channel(int freq); int wfd_util_get_phone_name(char *phone_name); void wfd_util_set_dev_name_notification(); diff --git a/packaging/wifi-direct-manager.changes b/packaging/wifi-direct-manager.changes index db4e2c8..5114721 100644 --- a/packaging/wifi-direct-manager.changes +++ b/packaging/wifi-direct-manager.changes @@ -1,3 +1,6 @@ +Tue, 15 Jan 2014 Jiung Yu (1.0.7) + * Replace file execution method + Tue, 15 Jan 2014 Jiung Yu (1.0.6) * Add cancel connection fuction to manager * Add reject connection fuction to manager diff --git a/packaging/wifi-direct-manager.spec b/packaging/wifi-direct-manager.spec index fbd7612..d318e74 100644 --- a/packaging/wifi-direct-manager.spec +++ b/packaging/wifi-direct-manager.spec @@ -1,6 +1,6 @@ Name: wifi-direct-manager Summary: Wi-Fi Direct manger -Version: 1.0.6 +Version: 1.0.7 Release: 1 Group: Network & Connectivity/Wireless License: Apache-2.0 diff --git a/plugin/wpasupplicant/wfd-plugin-wpasupplicant.c b/plugin/wpasupplicant/wfd-plugin-wpasupplicant.c index 325faf9..378245a 100644 --- a/plugin/wpasupplicant/wfd-plugin-wpasupplicant.c +++ b/plugin/wpasupplicant/wfd-plugin-wpasupplicant.c @@ -32,9 +32,12 @@ #include #include #include +#include #define _GNU_SOURCE #include #include +#include +#include #include @@ -286,6 +289,48 @@ static int _ws_freq_to_channel(int freq) return -1; } +gboolean _ws_util_execute_file(const char *file_path, + char *const args[], char *const envs[]) +{ + pid_t pid = 0; + int rv = 0; + errno = 0; + register unsigned int index = 0; + + while (args[index] != NULL) { + WDP_LOGD("[%s]", args[index]); + index++; + } + + if (!(pid = fork())) { + WDP_LOGD("pid(%d), ppid(%d)", getpid(), getppid()); + WDP_LOGD("Inside child, exec (%s) command", file_path); + + errno = 0; + if (execve(file_path, args, envs) == -1) { + WDP_LOGE("Fail to execute command (%s)", strerror(errno)); + exit(1); + } + } else if (pid > 0) { + if (waitpid(pid, &rv, 0) == -1) + WDP_LOGD("wait pid (%u) rv (%d)", pid, rv); + if (WIFEXITED(rv)) { + WDP_LOGD("exited, rv=%d", WEXITSTATUS(rv)); + } else if (WIFSIGNALED(rv)) { + WDP_LOGD("killed by signal %d", WTERMSIG(rv)); + } else if (WIFSTOPPED(rv)) { + WDP_LOGD("stopped by signal %d", WSTOPSIG(rv)); + } else if (WIFCONTINUED(rv)) { + WDP_LOGD("continued"); + } + + return TRUE; + } + + WDP_LOGE("failed to fork (%s)", strerror(errno)); + return FALSE; +} + static int _ws_check_socket(int sock) { struct pollfd p_fd; diff --git a/src/wifi-direct-util.c b/src/wifi-direct-util.c index c3747a8..67d07f0 100644 --- a/src/wifi-direct-util.c +++ b/src/wifi-direct-util.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -81,6 +82,48 @@ static int _txt_to_ip(char *txt, unsigned char *ip) return 0; } +gboolean wfd_util_execute_file(const char *file_path, + char *const args[], char *const envs[]) +{ + pid_t pid = 0; + int rv = 0; + errno = 0; + register unsigned int index = 0; + + while (args[index] != NULL) { + WDS_LOGD("[%s]", args[index]); + index++; + } + + if (!(pid = fork())) { + WDS_LOGD("pid(%d), ppid(%d)", getpid(), getppid()); + WDS_LOGD("Inside child, exec (%s) command", file_path); + + errno = 0; + if (execve(file_path, args, envs) == -1) { + WDS_LOGE("Fail to execute command (%s)", strerror(errno)); + exit(1); + } + } else if (pid > 0) { + if (waitpid(pid, &rv, 0) == -1) + WDS_LOGD("wait pid (%u) rv (%d)", pid, rv); + if (WIFEXITED(rv)) { + WDS_LOGD("exited, rv=%d", WEXITSTATUS(rv)); + } else if (WIFSIGNALED(rv)) { + WDS_LOGD("killed by signal %d", WTERMSIG(rv)); + } else if (WIFSTOPPED(rv)) { + WDS_LOGD("stopped by signal %d", WSTOPSIG(rv)); + } else if (WIFCONTINUED(rv)) { + WDS_LOGD("continued"); + } + + return TRUE; + } + + WDS_LOGE("failed to fork (%s)", strerror(errno)); + return FALSE; +} + int wfd_util_freq_to_channel(int freq) { if (freq < 2412 || freq > 5825) { @@ -463,11 +506,20 @@ static gboolean _polling_ip(gpointer user_data) int wfd_util_dhcps_start() { __WDS_LOG_FUNC_ENTER__; - int res = 0; + gboolean rv = FALSE; + const char *path = "/usr/bin/wifi-direct-dhcp.sh"; + char *const args[] = { "/usr/bin/wifi-direct-dhcp.sh", "server", NULL }; + char *const envs[] = { NULL }; vconf_set_int(VCONFKEY_DHCPS_IP_LEASE, 0); - res = system("/usr/bin/wifi-direct-dhcp.sh server"); - WDS_LOGD("[/usr/bin/wifi-direct-dhcp.sh server] returns %d", res); + + rv = wfd_util_execute_file(path, args, envs); + + if (rv != TRUE) { + WDS_LOGE("Failed to start wifi-direct-dhcp.sh server"); + return -1; + } + WDS_LOGD("Successfully started wifi-direct-dhcp.sh server"); __WDS_LOG_FUNC_EXIT__; return 0; @@ -492,13 +544,21 @@ int wfd_util_dhcps_wait_ip_leased(wfd_device_s *peer) int wfd_util_dhcps_stop() { __WDS_LOG_FUNC_ENTER__; - int res = 0; + gboolean rv = FALSE; + const char *path = "/usr/bin/wifi-direct-dhcp.sh"; + char *const args[] = { "/usr/bin/wifi-direct-dhcp.sh", "stop", NULL }; + char *const envs[] = { NULL }; vconf_ignore_key_changed(VCONFKEY_DHCPS_IP_LEASE, _dhcps_ip_leased_cb); vconf_set_int(VCONFKEY_DHCPS_IP_LEASE, 0); - res = system("/usr/bin/wifi-direct-dhcp.sh stop"); - WDS_LOGD("[/usr/bin/wifi-direct-dhcp.sh stop] returns %d", res); + rv = wfd_util_execute_file(path, args, envs); + + if (rv != TRUE) { + WDS_LOGE("Failed to stop wifi-direct-dhcp.sh"); + return -1; + } + WDS_LOGD("Successfully stopped wifi-direct-dhcp.sh"); __WDS_LOG_FUNC_EXIT__; return 0; @@ -507,15 +567,24 @@ int wfd_util_dhcps_stop() int wfd_util_dhcpc_start(wfd_device_s *peer) { __WDS_LOG_FUNC_ENTER__; - int res = 0; + gboolean rv = FALSE; + const char *path = "/usr/bin/wifi-direct-dhcp.sh"; + char *const args[] = { "/usr/bin/wifi-direct-dhcp.sh", "client", NULL }; + char *const envs[] = { NULL }; if (!peer) { WDS_LOGE("Invalid parameter"); return -1; } - res = system("/usr/bin/wifi-direct-dhcp.sh client"); - WDS_LOGD("[/usr/bin/wifi-direct-dhcp.sh client] returns %d", res); + rv = wfd_util_execute_file(path, args, envs); + + if (rv != TRUE) { + WDS_LOGE("Failed to start wifi-direct-dhcp.sh client"); + return -1; + } + WDS_LOGD("Successfully started wifi-direct-dhcp.sh client"); + g_timeout_add(250, (GSourceFunc) _polling_ip, peer); __WDS_LOG_FUNC_EXIT__; @@ -525,10 +594,18 @@ int wfd_util_dhcpc_start(wfd_device_s *peer) int wfd_util_dhcpc_stop() { __WDS_LOG_FUNC_ENTER__; - int res = 0; + gboolean rv = FALSE; + const char *path = "/usr/bin/wifi-direct-dhcp.sh"; + char *const args[] = { "/usr/bin/wifi-direct-dhcp.sh", "stop", NULL }; + char *const envs[] = { NULL }; + + rv = wfd_util_execute_file(path, args, envs); - res = system("/usr/bin/wifi-direct-dhcp.sh stop"); - WDS_LOGD("[/usr/bin/wifi-direct-dhcp.sh stop] returns %d", res); + if (rv != TRUE) { + WDS_LOGE("Failed to stop wifi-direct-dhcp.sh"); + return -1; + } + WDS_LOGD("Successfully stopped wifi-direct-dhcp.sh"); __WDS_LOG_FUNC_EXIT__; return 0; -- 2.7.4