Prepare a server socket of an application 67/176467/4
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 19 Apr 2018 10:45:32 +0000 (19:45 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 20 Apr 2018 01:02:49 +0000 (10:02 +0900)
Before executing an application, the child process creates an
aul socket of the application for communication. And then, the
application uses the socket.

Requires:
 - https://review.tizen.org/gerrit/#/c/176470/
 - https://review.tizen.org/gerrit/#/c/176471/

Change-Id: I082608844f8b7fae5bd04aa9c6df3064b429273c
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
inc/launchpad_common.h
src/launchpad.c
src/launchpad_common.c
src/launchpad_lib.c

index 102fa5a..d0a33b2 100644 (file)
@@ -126,6 +126,7 @@ void _get_cpu_idle(unsigned long long *total, unsigned long long *idle);
 int _setup_stdio(const char *ident);
 int _set_priority(int prio);
 int _wait_tep_mount(bundle *b);
+void _prepare_app_socket(void);
 
 #endif /* __LAUNCHPAD_COMMON_H__ */
 
index ae0def3..6f81c77 100755 (executable)
@@ -854,6 +854,8 @@ static int __prepare_exec(const char *appid, const char *app_path,
        if (ret < 0)
                return PAD_ERR_FAILED;
 
+       _prepare_app_socket();
+
        return 0;
 }
 
index f2272b5..8cefb4c 100644 (file)
@@ -819,13 +819,21 @@ int _close_all_fds(void)
        struct dirent *dentry = NULL;
        int fd;
        int max_fd;
+       int aul_fd = -1;
+       const char *aul_listen_fd;
+
+       aul_listen_fd = getenv("AUL_LISTEN_FD");
+       if (aul_listen_fd)
+               aul_fd = atoi(aul_listen_fd);
 
        dp = opendir("/proc/self/fd");
        if (dp == NULL) {
                /* fallback */
                max_fd = sysconf(_SC_OPEN_MAX);
-               for (fd = 3; fd < max_fd; fd++)
-                       close(fd);
+               for (fd = 3; fd < max_fd; fd++) {
+                       if (fd != aul_fd)
+                               close(fd);
+               }
 
                return 0;
        }
@@ -838,7 +846,7 @@ int _close_all_fds(void)
                if (fd < 3)
                        continue;
 
-               if (fd == dirfd(dp))
+               if (fd == dirfd(dp) || fd == aul_fd)
                        continue;
 
                close(fd);
@@ -1015,3 +1023,76 @@ int _wait_tep_mount(bundle *b)
        _I("Mount has been done.");
        return 0;
 }
+
+static int __create_app_socket(int pid, uid_t uid)
+{
+       int fd;
+       char path[PATH_MAX];
+       struct sockaddr_un addr = {
+               .sun_family = AF_UNIX,
+       };
+
+       fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
+       if (fd < 0) {
+               if (errno == EINVAL) {
+                       fd = socket(AF_UNIX, SOCK_STREAM, 0);
+                       if (fd < 0) {
+                               _E("Second chance - socket create errno(%d)",
+                                               errno);
+                               return -1;
+                       }
+               } else {
+                       _E("Failed to create socket. errno(%d)", errno);
+                       return -1;
+               }
+       }
+
+       snprintf(path, sizeof(path), "/run/aul/apps/%u/%d", uid, pid);
+       if (mkdir(path, 0700) != 0) {
+               if (errno == EEXIST) {
+                       if (access(path, R_OK) != 0) {
+                               _E("Failed to access %s. errno(%d)",
+                                               path, errno);
+                               close(fd);
+                               return -1;
+                       }
+               } else {
+                       _E("Failed to access %s. errno(%d)", path, errno);
+                       close(fd);
+                       return -1;
+               }
+       }
+
+       snprintf(addr.sun_path, sizeof(addr.sun_path),
+                       "/run/aul/apps/%u/%d/.app-sock", uid, pid);
+       unlink(addr.sun_path);
+
+       if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+               _E("Failed to bind socket(%d), errno(%d)", fd, errno);
+               close(fd);
+               return -1;
+       }
+
+       _set_sock_option(fd, 0);
+
+       if (listen(fd, 128) < 0) {
+               _E("Failed to listen %d, errno(%d)", fd, errno);
+               close(fd);
+               return -1;
+       }
+
+       return fd;
+}
+
+void _prepare_app_socket(void)
+{
+       int fd;
+       char buf[12];
+
+       fd = __create_app_socket(getpid(), getuid());
+       if (fd < 0)
+               return;
+
+       snprintf(buf, sizeof(buf), "%d", fd);
+       setenv("AUL_LISTEN_FD", buf, 1);
+}
index b7a910f..60f6dac 100644 (file)
@@ -135,6 +135,8 @@ static int __prepare_exec(const char *appid, const char *app_path,
        if (ret < 0)
                return -1;
 
+       _prepare_app_socket();
+
        return 0;
 }