Demonize hostapd
authorsaerome.kim <saerome.kim@samsung.com>
Fri, 30 Jun 2017 03:33:26 +0000 (12:33 +0900)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 17 Jul 2017 02:35:37 +0000 (11:35 +0900)
- Run hostapd as separated daemon process with '-B' parameter
- Without it, hostapd doesn't work properly after restarted.
  (Terminated with SIGTERM and started again)

Signed-off-by: saerome.kim <saerome.kim@samsung.com>
src/mesh-request.c
src/mesh-softap.c

index 0c08314..63c1775 100644 (file)
@@ -193,8 +193,10 @@ int mesh_request_enable_softap(
 
        /* Add softAP interface into bridge */
        ret = mesh_request_add_bridge_interface(bridge_interface, softap_interface);
-       if (MESHD_ERROR_NONE != ret)
+       if (MESHD_ERROR_NONE != ret) {
+               mesh_softap_disable_softap();
                return ret;
+       }
 
        return ret;
 }
index f52eb19..678f432 100644 (file)
@@ -61,6 +61,7 @@
 #define HOSTAPD_ENTROPY_FILE   tzplatform_mkpath(TZ_SYS_VAR, "/lib/misc/hostapd.bin")
 #define HOSTAPD_MESH_CONF_FILE tzplatform_mkpath(TZ_SYS_VAR, "/lib/mesh/mesh_hostapd.conf")
 #define HOSTAPD_CTRL_INTF_DIR  tzplatform_mkpath(TZ_SYS_RUN, "/hostapd")
+#define HOSTAPD_PID_FILE               tzplatform_mkpath(TZ_SYS_RUN, "/.mesh_hostapd.pid")
 #define HOSTAPD_ALLOWED_LIST   tzplatform_mkpath(TZ_SYS_VAR, "/lib/hostapd/hostapd.accept")
 #define HOSTAPD_BLOCKED_LIST   tzplatform_mkpath(TZ_SYS_VAR, "/lib/hostapd/hostapd.deny")
 #define HOSTAPD_RETRY_MAX      5
@@ -71,7 +72,6 @@
 #define PSK_ITERATION_COUNT    4096
 #define MAX_BUF_SIZE           (256u)
 
-static pid_t hostapd_pid = 0;
 static int hostapd_ctrl_fd = 0;
 
 static int __get_psk_hexascii(const char *pass, const unsigned char *salt,
@@ -253,9 +253,55 @@ static int __close_hostapd_intf(int *fd)
        return MESHD_ERROR_NONE;
 }
 
+static int __get_pid_of_hostapd(pid_t *pid)
+{
+       FILE *fd = NULL;
+       int ret = -1;
+       int rv = 0;
+       char error_buf[256] = {0, };
+       char file_path[256] = {0, };
+
+       snprintf(file_path, 256, "%s", HOSTAPD_PID_FILE);
+
+       if (0 == access(file_path, F_OK)) {
+               fd = fopen(file_path, "r");
+               if (fd == NULL) {
+                       MESH_LOGE("Error! Could not open pid file");
+                       return MESHD_ERROR_IO_ERROR;
+               }
+       } else {
+               MESH_LOGE("Error! Could not access pid file");
+               return MESHD_ERROR_IO_ERROR;
+       }
+
+       errno = 0;
+       rv = fscanf(fd, "%d", &ret);
+       if (rv < 0) {
+               strerror_r(errno, error_buf, 256);
+               MESH_LOGE("Error! Failed to read from file, rv:[%d], error:[%s]",
+                               rv, error_buf);
+               fclose(fd);
+               return MESHD_ERROR_IO_ERROR;
+       }
+
+       *pid = (pid_t)ret;
+       MESH_LOGD("  PID: [%d]", ret);
+
+       fclose(fd);
+       return MESHD_ERROR_NONE;
+}
+
 static int __terminate_hostapd()
 {
        int ret;
+       pid_t hostapd_pid = 0;
+
+       /* Get pid */
+       ret = __get_pid_of_hostapd(&hostapd_pid);
+       if (MESHD_ERROR_NONE != ret) {
+               MESH_LOGE("There is no hostapd");
+               return MESHD_ERROR_NONE;
+       }
 
        if (hostapd_pid == 0) {
                MESH_LOGE("There is no hostapd");
@@ -267,19 +313,20 @@ static int __terminate_hostapd()
                MESH_LOGE("__close_hostapd_intf is failed");
 
        kill(hostapd_pid, SIGTERM);
-       waitpid(hostapd_pid, NULL, 0);
-       hostapd_pid = 0;
 
        return MESHD_ERROR_NONE;
 }
 
 static int __execute_hostapd()
 {
-       pid_t pid;
+       pid_t pid = 0;
+       int status;
+       int ret = MESHD_ERROR_NONE;
 
-       if (hostapd_pid) {
+       ret = __get_pid_of_hostapd(&pid);
+       if (0 != pid) {
                MESH_LOGE("hostapd is running already");
-               return MESHD_ERROR_OPERATION_FAILED;
+               return MESHD_ERROR_NONE;
        }
 
        pid = fork();
@@ -291,17 +338,21 @@ static int __execute_hostapd()
        if (pid == 0) {
                if (execl(HOSTAPD_BIN, HOSTAPD_BIN, "-e", HOSTAPD_ENTROPY_FILE,
                                        HOSTAPD_MESH_CONF_FILE,
-                                       "-f", HOSTAPD_DEBUG_FILE, "-ddd",
+                                       "-f", HOSTAPD_DEBUG_FILE,
+                                       "-P", HOSTAPD_PID_FILE,
+                                       "-ddd", "-B",
                                        (char *)NULL)) {
                        MESH_LOGE("execl failed");
                }
 
                MESH_LOGE("Should not get here!");
                exit(1);
+       } else {
+               /* Reap child */
+               waitpid(pid, &status, 0);
+               MESH_LOGD("  child [%d] reaped with status(%d)", pid, status);
        }
 
-       hostapd_pid = pid;
-
        return MESHD_ERROR_NONE;
 }