2 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <sys/types.h>
22 #include <sys/mount.h>
27 #include <sys/xattr.h>
29 #include <sys/socket.h>
31 #include <linux/limits.h>
33 #include <sys/capability.h>
34 #include <tzplatform_config.h>
37 #include <systemd/sd-journal.h>
39 #include <sys/resource.h>
40 #include <sys/personality.h>
42 #include <dbus/dbus.h>
44 #include "launchpad_common.h"
47 #define MAX_PATH_LEN 1024
48 #define MAX_CMD_BUFSZ 1024
50 #define MAX_PENDING_CONNECTIONS 10
51 #define CONNECT_RETRY_TIME (100 * 1000)
52 #define CONNECT_RETRY_COUNT 3
53 #define AUL_PKT_HEADER_SIZE (sizeof(int) + sizeof(int) + sizeof(int))
54 #define PATH_AMD_SOCK "/run/aul/daemons/.amd-sock"
55 #define PATH_DEV_NULL "/dev/null"
56 #define MAX_TEP_IS_MOUNT_RETRY_CNT 100
57 #define MAX_PAYLOAD_SIZE (1024 * 1024 * 1)
59 #define TEP_BUS_NAME "org.tizen.system.deviced"
60 #define TEP_OBJECT_PATH "/Org/Tizen/System/DeviceD/Tzip"
61 #define TEP_INTERFACE_NAME "org.tizen.system.deviced.Tzip"
62 #define TEP_IS_MOUNTED_METHOD "IsMounted"
64 #define APP2SD_BUS_NAME "org.tizen.app2sd"
65 #define APP2SD_OBJECT_PATH "/org/tizen/app2sd"
66 #define APP2SD_INTERFACE_NAME "org.tizen.app2sd"
67 #define APP2SD_ONDEMANDSETUPINIT_METHOD "OndemandSetupInit"
68 #define APP2SD_RETRY_MAX 5
69 #define APP2SD_WAIT_USEC (1000000 / 2) /* 0.5 sec */
71 static int __read_proc(const char *path, char *buf, int size)
76 if (buf == NULL || path == NULL)
79 fd = open(path, O_RDONLY);
83 ret = read(fd, buf, size - 1);
95 void _get_cpu_idle(unsigned long long *total, unsigned long long *idle)
99 unsigned long long sum = 0;
100 unsigned long long val;
101 unsigned long long iv = 0;
102 char buf[4] = { 0, };
104 fp = fopen("/proc/stat", "rt");
109 if (fscanf(fp, "%3s", buf) == -1) {
114 for (i = 0; i < 10; i++) {
115 if (fscanf(fp, "%lld", &val) == -1) {
120 if (sum + val < sum) {
127 if (i == 3) /* idle */
137 int _set_sock_option(int fd, int cli)
139 struct timeval tv = { 5, 200 * 1000 }; /* 5.2 sec */
144 size = AUL_SOCK_MAXBUFF;
145 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
147 _E("Failed to set SO_SNDBUF option on socket. errno(%d)",
152 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
154 _E("Failed to set SO_RCVBUF option on socket. errno(%d)",
160 ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
162 _E("Failed to set SO_RCVTIMEO option on socket. " \
167 flag = fcntl(fd, F_GETFD);
169 ret = fcntl(fd, F_SETFD, flag);
171 _E("Failed to manipulate fd(F_SETFD), errno(%d)",
180 static int __parse_app_path(const char *arg, char *out, int out_size)
184 char *start_out = out;
186 if (arg == NULL || out == NULL) {
187 /* Handles null buffer*/
191 for (i = 0; out_size > 1; i++) {
215 case 2: /* escape start*/
230 case 4: /* character escape*/
231 if (arg[i] == '\0') {
241 if (out != start_out) {
250 return -1; /* error*/
251 case 7: /* terminate*/
268 static int __create_client_socket(const char *path)
270 struct sockaddr_un addr = { 0, };
271 int retry = CONNECT_RETRY_COUNT;
274 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
276 _E("Failed to create socket(%s). errno(%d)", path, errno);
280 addr.sun_family = AF_UNIX;
281 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
282 while (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
283 if (errno != ETIMEDOUT || retry <= 0) {
284 _E("Failed to connect socket(%s). errno(%d)",
290 usleep(CONNECT_RETRY_TIME);
292 _W("Retry(%d) to connect %s", retry, path);
298 int _send_cmd_to_amd(int cmd)
300 app_pkt_t pkt = {0,};
304 fd = __create_client_socket(PATH_AMD_SOCK);
309 ret = send(fd, &pkt, sizeof(app_pkt_t), MSG_NOSIGNAL);
311 _E("Failed to send cmd(%d), errno(%d)", cmd, errno);
320 int _create_server_sock(const char *name)
322 struct sockaddr_un saddr;
325 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
326 /* support above version 2.6.27*/
328 if (errno == EINVAL) {
329 fd = socket(AF_UNIX, SOCK_STREAM, 0);
331 _E("second chance - socket create error");
340 memset(&saddr, 0, sizeof(saddr));
341 saddr.sun_family = AF_UNIX;
343 snprintf(saddr.sun_path, sizeof(saddr.sun_path),
345 SOCKET_PATH, getuid(), name);
346 unlink(saddr.sun_path);
348 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
354 if (_set_sock_option(fd, 0) < 0) {
355 _E("Failed to set sock option");
360 if (listen(fd, 128) == -1) {
369 app_pkt_t *_recv_pkt_raw(int fd)
373 unsigned char buf[AUL_SOCK_MAXBUFF];
380 /* receive header(cmd, datalen) */
381 len = recv(fd, buf, AUL_PKT_HEADER_SIZE, 0);
386 if (len < AUL_PKT_HEADER_SIZE) {
390 memcpy(&cmd, buf, sizeof(int));
391 memcpy(&datalen, buf + sizeof(int), sizeof(int));
392 memcpy(&opt, buf + sizeof(int) + sizeof(int), sizeof(int));
394 if (datalen < 0 || datalen > MAX_PAYLOAD_SIZE) {
395 _E("Invalid protocol. datalen(%d)", datalen);
399 /* allocate for a null byte */
400 pkt = (app_pkt_t *)calloc(1, AUL_PKT_HEADER_SIZE + datalen + 1);
402 _E("failed to alloc app_pkt_t");
410 while (len != pkt->len) {
411 ret = recv(fd, pkt->data + len, pkt->len - len, 0);
413 _E("recv error %d %d", len, pkt->len);
418 _D("recv len %d %d", len, pkt->len);
424 app_pkt_t *_accept_recv_pkt_raw(int fd, int *clifd, struct ucred *cr)
426 struct sockaddr_un aul_addr = { 0, };
430 int cl = sizeof(struct ucred);
432 sun_size = sizeof(struct sockaddr_un);
434 newfd = accept(fd, (struct sockaddr *)&aul_addr,
435 (socklen_t *) &sun_size);
442 if (getsockopt(newfd, SOL_SOCKET, SO_PEERCRED, cr,
443 (socklen_t *) &cl) < 0) {
444 _E("peer information error");
449 if (_set_sock_option(newfd, 1) < 0) {
450 _E("Failed to set sock option");
455 pkt = _recv_pkt_raw(newfd);
457 _E("failed to receive pkt from client");
466 int _send_pkt_raw(int client_fd, app_pkt_t *pkt)
472 if (client_fd == -1 || pkt == NULL) {
473 _E("arguments error!");
477 pkt_size = AUL_PKT_HEADER_SIZE + pkt->len;
479 while (sent != pkt_size) {
480 send_ret = send(client_fd, (char *)pkt + sent,
481 pkt_size - sent, MSG_NOSIGNAL);
482 if (send_ret == -1) {
483 _E("send error! (%d)", errno);
488 _D("send(%d: ret: %d) : %d / %d",
489 client_fd, send_ret, sent, pkt_size);
495 appinfo_t *_appinfo_create(bundle *kb)
497 appinfo_t *menu_info;
498 const char *ptr = NULL;
500 menu_info = calloc(1, sizeof(appinfo_t));
501 if (menu_info == NULL)
504 ptr = bundle_get_val(kb, AUL_K_APPID);
506 menu_info->appid = strdup(ptr);
507 ptr = bundle_get_val(kb, AUL_K_EXEC);
509 menu_info->app_path = strdup(ptr);
510 if (menu_info->app_path != NULL)
511 menu_info->original_app_path = strdup(menu_info->app_path);
512 ptr = bundle_get_val(kb, AUL_K_PACKAGETYPE);
514 menu_info->pkg_type = strdup(ptr);
515 ptr = bundle_get_val(kb, AUL_K_APP_TYPE);
517 menu_info->app_type = strdup(ptr);
518 ptr = bundle_get_val(kb, AUL_K_HWACC);
520 menu_info->hwacc = strdup(ptr);
521 ptr = bundle_get_val(kb, AUL_K_TASKMANAGE);
523 menu_info->taskmanage = strdup(ptr);
524 ptr = bundle_get_val(kb, AUL_K_PKGID);
526 menu_info->pkgid = strdup(ptr);
527 ptr = bundle_get_val(kb, AUL_K_COMP_TYPE);
529 menu_info->comp_type = strdup(ptr);
530 ptr = bundle_get_val(kb, AUL_K_INTERNAL_POOL);
532 menu_info->internal_pool = strdup(ptr);
533 ptr = bundle_get_val(kb, AUL_K_ROOT_PATH);
535 menu_info->root_path = strdup(ptr);
537 ptr = bundle_get_val(kb, AUL_K_LOADER_NAME);
539 menu_info->loader_name = strdup(ptr);
541 ptr = bundle_get_val(kb, AUL_K_IS_GLOBAL);
542 if (ptr && strcmp(ptr, "true") == 0)
543 menu_info->global = true;
545 menu_info->global = false;
547 if (!_appinfo_get_app_path(menu_info)) {
548 _appinfo_free(menu_info);
555 char *_appinfo_get_app_path(appinfo_t *menu_info)
561 if (!menu_info || menu_info->app_path == NULL)
564 while (menu_info->app_path[i] != 0) {
565 if (menu_info->app_path[i] == ' '
566 || menu_info->app_path[i] == '\t') {
574 free(menu_info->app_path);
575 menu_info->app_path = NULL;
576 } else if (path_len > 0) {
577 tmp_app_path = malloc(sizeof(char) * (path_len + 1));
578 if (tmp_app_path == NULL)
580 snprintf(tmp_app_path, path_len + 1, "%s", menu_info->app_path);
581 free(menu_info->app_path);
582 menu_info->app_path = tmp_app_path;
585 return menu_info->app_path;
588 void _appinfo_free(appinfo_t *menu_info)
590 if (menu_info == NULL)
593 if (menu_info->appid != NULL)
594 free(menu_info->appid);
595 if (menu_info->app_path != NULL)
596 free(menu_info->app_path);
597 if (menu_info->original_app_path != NULL)
598 free(menu_info->original_app_path);
599 if (menu_info->pkg_type != NULL)
600 free(menu_info->pkg_type);
601 if (menu_info->app_type != NULL)
602 free(menu_info->app_type);
603 if (menu_info->hwacc != NULL)
604 free(menu_info->hwacc);
605 if (menu_info->taskmanage != NULL)
606 free(menu_info->taskmanage);
607 if (menu_info->pkgid != NULL)
608 free(menu_info->pkgid);
609 if (menu_info->comp_type != NULL)
610 free(menu_info->comp_type);
611 if (menu_info->internal_pool != NULL)
612 free(menu_info->internal_pool);
613 if (menu_info->root_path != NULL)
614 free(menu_info->root_path);
615 if (menu_info->loader_name != NULL)
616 free(menu_info->loader_name);
621 static bool __validate_bundle_key(const char *key)
626 if (!strncmp(key, "__AUL_", strlen("__AUL_")))
629 if (!strncmp(key, "__APP_SVC_", strlen("__APP_SVC_")))
635 void _modify_bundle(bundle *kb, int caller_pid, appinfo_t *menu_info, int cmd)
638 char exe[MAX_PATH_LEN];
643 bundle_del(kb, AUL_K_APPID);
644 bundle_del(kb, AUL_K_EXEC);
645 bundle_del(kb, AUL_K_APP_TYPE);
646 bundle_del(kb, AUL_K_PACKAGETYPE);
647 bundle_del(kb, AUL_K_TASKMANAGE);
648 bundle_del(kb, AUL_K_PKGID);
649 bundle_del(kb, AUL_K_COMP_TYPE);
651 /* Parse app_path to retrieve default bundle*/
652 if (cmd == PAD_CMD_LAUNCH) {
653 ptr = menu_info->original_app_path;
654 flag = __parse_app_path(ptr, exe, sizeof(exe));
657 SECURE_LOGD("parsing app_path: EXEC - %s", exe);
660 flag = __parse_app_path(ptr, key, sizeof(key));
665 flag = __parse_app_path(ptr, value,
671 /*bundle_del(kb, key);*/
672 if (__validate_bundle_key(key))
673 bundle_add(kb, key, value);
675 } else if (flag == 0)
676 _D("parsing app_path: No arguments");
678 _D("parsing app_path: Invalid argument");
682 int _connect_to_launchpad(int type, int id)
689 _D("[launchpad] enter, type: %d", type);
691 snprintf(path, sizeof(path), "%s/daemons/%d/%s%d-%d",
692 SOCKET_PATH, getuid(), LAUNCHPAD_LOADER_SOCKET_NAME,
694 fd = __create_client_socket(path);
698 client_pid = getpid();
699 send_ret = send(fd, &client_pid, sizeof(client_pid), MSG_NOSIGNAL);
700 _D("send(%d) : %d", client_pid, send_ret);
701 if (send_ret == -1) {
707 SECURE_LOGD("[launchpad] done, connect fd: %d", fd);
711 #ifdef TIZEN_FEATURE_SET_PERSONALITY_32
712 static void __set_execution_domain(void)
717 res = personality(PER_LINUX32);
719 _E("personality() failed, error: %d (%s)",
721 strerror_r(errno, err_buf, sizeof(err_buf)));
724 #endif /* TIZEN_FEATURE_SET_PERSONALITY_32 */
726 void _set_env(appinfo_t *menu_info, bundle *kb)
729 char buf[MAX_LOCAL_BUFSZ];
731 str = bundle_get_val(kb, AUL_K_STARTTIME);
733 setenv("APP_START_TIME", str, 1);
735 if (menu_info->hwacc != NULL)
736 setenv("HWACC", menu_info->hwacc, 1);
737 if (menu_info->taskmanage != NULL)
738 setenv("TASKMANAGE", menu_info->taskmanage, 1);
739 if (menu_info->root_path != NULL)
740 setenv("AUL_ROOT_PATH", menu_info->root_path, 1);
741 if (menu_info->appid != NULL)
742 setenv("AUL_APPID", menu_info->appid, 1);
743 if (menu_info->pkgid != NULL)
744 setenv("AUL_PKGID", menu_info->pkgid, 1);
745 if (menu_info->app_type != NULL)
746 setenv("RUNTIME_TYPE", menu_info->app_type, 1);
748 str = bundle_get_val(kb, AUL_K_WAYLAND_DISPLAY);
750 setenv("WAYLAND_DISPLAY", str, 1);
752 str = bundle_get_val(kb, AUL_K_WAYLAND_WORKING_DIR);
754 setenv("XDG_RUNTIME_DIR", str, 1);
756 str = bundle_get_val(kb, AUL_K_API_VERSION);
758 setenv("TIZEN_API_VERSION", str, 1);
760 snprintf(buf, sizeof(buf), "%d", getpid());
761 setenv("AUL_PID", buf, 1);
763 #ifdef TIZEN_FEATURE_SET_PERSONALITY_32
764 __set_execution_domain();
765 #endif /* TIZEN_FEATURE_SET_PERSONALITY_32 */
768 char **_create_argc_argv(bundle *kb, int *margc)
773 argc = bundle_export_to_argv(kb, &argv);
779 char *_get_libdir(const char *path)
787 path_dup = strdup(path);
788 if (path_dup == NULL)
790 ptr = strrchr(path_dup, '/');
798 snprintf(buf, sizeof(buf), "%s/../lib/", path_dup);
801 if (access(buf, F_OK) == -1)
807 int _proc_get_attr_by_pid(int pid, char *buf, int size)
809 char path[PATH_MAX] = { 0, };
812 snprintf(path, sizeof(path), "/proc/%d/attr/current", pid);
813 ret = __read_proc(path, buf, size);
820 static int __delete_dir(const char *path)
823 struct dirent *dentry = NULL;
835 while ((dentry = readdir(dp)) != NULL) {
836 if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, ".."))
839 snprintf(buf, sizeof(buf), "%s/%s", path, dentry->d_name);
840 ret = stat(buf, &statbuf);
842 if (S_ISDIR(statbuf.st_mode))
855 int _delete_sock_path(int pid, uid_t uid)
860 snprintf(path, sizeof(path), "/run/aul/apps/%d/%d", uid, pid);
861 r = access(path, F_OK);
864 _E("Failed to access %s. errno(%d)", path, errno);
869 if (access(path, F_OK) == 0) {
870 _E("Failed to delete %s", path);
877 int _close_all_fds(void)
880 struct dirent *dentry = NULL;
884 const char *aul_listen_fd;
886 aul_listen_fd = getenv("AUL_LISTEN_FD");
888 aul_fd = atoi(aul_listen_fd);
890 dp = opendir("/proc/self/fd");
893 max_fd = sysconf(_SC_OPEN_MAX);
894 for (fd = 3; fd < max_fd; fd++) {
902 while ((dentry = readdir(dp)) != NULL) {
903 if (!isdigit(dentry->d_name[0]))
906 fd = atoi(dentry->d_name);
910 if (fd == dirfd(dp) || fd == aul_fd)
920 int _setup_stdio(const char *ident)
925 fd = open(PATH_DEV_NULL, O_RDONLY | O_NOCTTY);
927 _W("Failed to open /dev/null - err(%d)", errno);
930 if (dup2(fd, STDIN_FILENO) < 0) {
931 _W("Failed to duplicate fd - oldfd(%d), newfd(%d)",
937 fd = sd_journal_stream_fd(ident, LOG_INFO, false);
939 _W("Failed to connect journal socket - err(%d)", errno);
940 fd = open(PATH_DEV_NULL, O_WRONLY | O_NOCTTY);
942 _W("Failed to open /dev/null - err(%d)", errno);
946 if (dup2(fd, STDOUT_FILENO) < 0) {
947 _W("Failed to duplicate fd - oldfd(%d), newfd(%d)",
953 fd = sd_journal_stream_fd(ident, LOG_INFO, false);
955 _W("Failed to connect journal socket - err(%d)", errno);
956 fd = open(PATH_DEV_NULL, O_WRONLY | O_NOCTTY);
958 _W("Failed to open /dev/null - err(%d)", errno);
962 if (dup2(fd, STDERR_FILENO) < 0) {
963 _W("Failed to duplicate fd - oldfd(%d), newfd(%d)",
971 int _set_priority(int prio)
975 r = setpriority(PRIO_PGRP, 0, prio);
977 SECURE_LOGE("Failed to set process(%d) priority(%d) - err(%d)",
978 getpid(), prio, errno);
984 static int __dbus_send_message(DBusMessage *callback(void *), void *user_data,
985 int timeout, int *result)
988 DBusConnection *conn;
989 DBusMessage *msg = NULL;
990 DBusMessageIter args;
991 DBusPendingCall *pending = NULL;
996 dbus_error_init(&err);
997 conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
999 _E("Failed to connect to D-Bus Daemon");
1004 msg = callback(user_data);
1006 _E("Message is nullptr");
1010 r = dbus_connection_send_with_reply(conn, msg, &pending, timeout);
1011 if (!r || !pending) {
1012 _E("Failed to send message");
1017 dbus_connection_flush(conn);
1018 dbus_message_unref(msg);
1020 dbus_pending_call_block(pending);
1021 msg = dbus_pending_call_steal_reply(pending);
1023 _E("Failed to get reply message");
1028 dbus_pending_call_unref(pending);
1030 if (!dbus_message_iter_init(msg, &args)) {
1031 _E("Message has no arguments!");
1033 } else if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_INT32) {
1034 _E("Argument is not integer! type(%d)",
1035 dbus_message_iter_get_arg_type(&args));
1038 dbus_message_iter_get_basic(&args, &i);
1042 _D("Result: %d", *result);
1045 dbus_message_unref(msg);
1048 dbus_connection_close(conn);
1050 if (dbus_error_is_set(&err)) {
1051 _E("D-Bus error(%s)", err.message);
1052 dbus_error_free(&err);
1058 static DBusMessage *__create_tep_mount_message(void *user_data)
1060 DBusMessage *message;
1061 DBusMessageIter args;
1063 const char *tep_path = (const char *)user_data;
1065 message = dbus_message_new_method_call(TEP_BUS_NAME,
1068 TEP_IS_MOUNTED_METHOD);
1070 _E("Message is nullptr");
1074 dbus_message_iter_init_append(message, &args);
1075 r = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &tep_path);
1077 _E("Out of memory");
1078 dbus_message_unref(message);
1085 static int __is_tep_mount_done(const char *tep_path)
1090 ret = __dbus_send_message(__create_tep_mount_message,
1091 (void *)tep_path, 500, &result);
1093 _E("Failed to send message");
1098 static int __check_tep_mount(const char *tep_path)
1106 while (cnt < MAX_TEP_IS_MOUNT_RETRY_CNT) {
1107 r = __is_tep_mount_done(tep_path);
1115 _E("Not able to mount within 5 sec: %s", tep_path);
1122 int _wait_tep_mount(bundle *b)
1127 const char **tep_paths;
1129 tep_paths = bundle_get_str_array(b, AUL_K_TEP_PATH, &len);
1133 for (i = 0; i < len; ++i) {
1134 r = __check_tep_mount(tep_paths[i]);
1139 _I("Mount has been done.");
1143 static int __create_app_socket(int pid, uid_t uid)
1146 char path[PATH_MAX];
1147 struct sockaddr_un addr = {
1148 .sun_family = AF_UNIX,
1151 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
1153 if (errno == EINVAL) {
1154 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1156 _E("Second chance - socket create errno(%d)",
1161 _E("Failed to create socket. errno(%d)", errno);
1166 snprintf(path, sizeof(path), "/run/aul/apps/%u/%d", uid, pid);
1167 if (mkdir(path, 0700) != 0) {
1168 if (errno == EEXIST) {
1169 if (access(path, R_OK) != 0) {
1170 _E("Failed to access %s. errno(%d)",
1176 _E("Failed to access %s. errno(%d)", path, errno);
1182 snprintf(addr.sun_path, sizeof(addr.sun_path),
1183 "/run/aul/apps/%u/%d/.app-sock", uid, pid);
1184 unlink(addr.sun_path);
1186 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
1187 _E("Failed to bind socket(%d), errno(%d)", fd, errno);
1192 if (_set_sock_option(fd, 0) < 0) {
1193 _E("Failed to set sock option");
1198 if (listen(fd, 128) < 0) {
1199 _E("Failed to listen %d, errno(%d)", fd, errno);
1207 int _prepare_app_socket(void)
1212 fd = __create_app_socket(getpid(), getuid());
1216 snprintf(buf, sizeof(buf), "%d", fd);
1217 setenv("AUL_LISTEN_FD", buf, 1);
1222 struct package_info_s {
1227 static DBusMessage *__create_app2sd_message(void *user_data)
1229 struct package_info_s *pkg_info = (struct package_info_s *)user_data;
1230 DBusMessage *message;
1231 DBusMessageIter args;
1234 _D("[__APP2SD__] package(%s), uid(%u)", pkg_info->id, pkg_info->uid);
1236 message = dbus_message_new_method_call(APP2SD_BUS_NAME,
1238 APP2SD_INTERFACE_NAME,
1239 APP2SD_ONDEMANDSETUPINIT_METHOD);
1241 _E("Message is nullptr");
1245 dbus_message_iter_init_append(message, &args);
1246 r = dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,
1249 _E("Failed to append pkgid");
1250 dbus_message_unref(message);
1254 r = dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32,
1257 _E("Failed to append uid");
1258 dbus_message_unref(message);
1265 int _enable_external_pkg(bundle *b, const char *pkgid, uid_t pkg_uid)
1267 const char *installed_storage;
1271 struct package_info_s pkg_info = {
1276 installed_storage = bundle_get_val(b, AUL_K_INSTALLED_STORAGE);
1277 if (!installed_storage || strcmp(installed_storage, "external") != 0)
1281 ret = __dbus_send_message(__create_app2sd_message,
1282 &pkg_info, 500, &result);
1284 _E("Failed to send message");
1286 usleep(APP2SD_WAIT_USEC);
1291 } while (retry_cnt <= APP2SD_RETRY_MAX);
1293 _D("[__APP2SD__] result(%d)", result);
1298 int _verify_proc_caps(void)
1301 cap_flag_value_t eff_state;
1302 cap_flag_value_t inh_state;
1303 cap_value_t value = CAP_SETGID;
1306 cap_d = cap_get_proc();
1308 _E("Failed to get cap from proc. pid(%d)", getpid());
1312 r = cap_get_flag(cap_d, value, CAP_INHERITABLE, &inh_state);
1314 _E("Failed to get cap inh - errno(%d)", errno);
1319 r = cap_get_flag(cap_d, value, CAP_EFFECTIVE, &eff_state);
1321 _E("Failed to get cap eff - errno(%d)", errno);
1326 if ((inh_state != CAP_SET) || (eff_state != CAP_SET)) {
1327 _E("The process(%d) doesn't have %d cap",
1337 int _prepare_id_file(void)
1339 char path[PATH_MAX];
1342 snprintf(path, sizeof(path), "/run/aul/apps/%u/%d/%s",
1343 getuid(), getpid(), getenv("AUL_APPID"));
1344 fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0600);
1346 _E("Failed to create %s. errno(%d)", path, errno);
1354 static int __send_raw(int fd, int cmd, unsigned char *data, int datalen)
1359 pkt = (app_pkt_t *)calloc(1, sizeof(app_pkt_t) + datalen);
1361 _E("Out of memory");
1368 memcpy(pkt->data, data, pkt->len);
1370 ret = _send_pkt_raw(fd, pkt);
1381 int _send_message_to_logger(const char *tag, const char *format, ...)
1389 snprintf(buf, sizeof(buf), "%s/daemons/%u/.launchpad-logger-sock",
1390 SOCKET_PATH, getuid());
1391 fd = __create_client_socket(buf);
1395 va_start(ap, format);
1396 vsnprintf(fmt, sizeof(fmt), format, ap);
1399 snprintf(buf, sizeof(buf), "%s: %s", tag, fmt);
1400 ret = __send_raw(fd, 0, (unsigned char *)buf, strlen(buf) + 1);