4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 #include <sys/types.h>
36 #include <tzplatform_config.h>
37 #include <cynara-client.h>
38 #include <cynara-creds-socket.h>
39 #include <cynara-session.h>
40 #include <systemd/sd-login.h>
42 #include "amd_config.h"
43 #include "simple_util.h"
46 #include "amd_request.h"
47 #include "amd_launch.h"
48 #include "amd_appinfo.h"
49 #include "amd_status.h"
50 #include "amd_app_group.h"
52 #define INHOUSE_UID tzplatform_getuid(TZ_USER_NAME)
53 #define REGULAR_UID_MIN 5000
55 #define PRIVILEGE_APPMANAGER_LAUNCH "http://tizen.org/privilege/appmanager.launch"
56 #define PRIVILEGE_APPMANAGER_KILL "http://tizen.org/privilege/appmanager.kill"
57 #define PRIVILEGE_APPMANAGER_KILL_BGAPP "http://tizen.org/privilege/appmanager.kill.bgapp"
59 #define MAX_NR_OF_DESCRIPTORS 2
60 static GHashTable *__socket_pair_hash = NULL;
62 typedef int (*app_cmd_dispatch_func)(int clifd, const app_pkt_t *pkt, struct ucred *cr);
64 static cynara *r_cynara = NULL;
66 static int __send_result_to_client(int fd, int res);
67 static gboolean __request_handler(gpointer data);
69 static int __send_message(int sock, const struct iovec *vec, int vec_size, const int *desc, int nr_desc)
71 struct msghdr msg = {0};
74 if (vec == NULL || vec_size < 1)
76 if (nr_desc < 0 || nr_desc > MAX_NR_OF_DESCRIPTORS)
81 msg.msg_iov = (struct iovec *)vec;
82 msg.msg_iovlen = vec_size;
84 /* sending ancillary data */
87 struct cmsghdr *cmsg = NULL;
88 char buff[CMSG_SPACE(sizeof(int) * MAX_NR_OF_DESCRIPTORS)] = {0};
90 msg.msg_control = buff;
91 msg.msg_controllen = sizeof(buff);
92 cmsg = CMSG_FIRSTHDR(&msg);
96 /* packing files descriptors */
98 cmsg->cmsg_level = SOL_SOCKET;
99 cmsg->cmsg_type = SCM_RIGHTS;
100 desclen = cmsg->cmsg_len = CMSG_LEN(sizeof(int) * nr_desc);
101 memcpy((int *)CMSG_DATA(cmsg), desc, sizeof(int) * nr_desc);
102 cmsg = CMSG_NXTHDR(&msg, cmsg);
104 _D("packing file descriptors done");
107 /* finished packing updating the corect length */
108 msg.msg_controllen = desclen;
110 msg.msg_control = NULL;
111 msg.msg_controllen = 0;
114 sndret = sendmsg(sock, &msg, 0);
116 _D("sendmsg ret : %d", sndret);
123 static int __send_result_data(int fd, int cmd, unsigned char *kb_data, int datalen)
128 app_pkt_t *pkt = NULL;
130 pkt = (app_pkt_t *)malloc(AUL_PKT_HEADER_SIZE + datalen);
132 _E("Malloc Failed!");
138 memcpy(pkt->data, kb_data, datalen);
140 while (sent != AUL_PKT_HEADER_SIZE + datalen) {
141 len = send(fd, pkt, AUL_PKT_HEADER_SIZE + datalen - sent, 0);
143 _E("send error fd:%d (errno %d)", fd, errno);
157 extern int __app_dead_handler(int pid, uid_t user);
158 extern int __agent_dead_handler(uid_t user);
160 static int __send_result_to_client(int fd, int res)
162 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
164 _E("send failed due to EPIPE.\n");
165 _E("send fail to client");
171 static void __real_send(int clifd, int ret)
173 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
175 _E("send failed due to EPIPE.\n");
177 _E("send fail to client");
183 static int __get_caller_pid(bundle *kb)
188 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
192 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
204 static int __foward_cmd(int cmd, bundle *kb, int cr_pid)
208 char tmp_pid[MAX_PID_STR_BUFSZ];
213 if ((pid = __get_caller_pid(kb)) < 0)
216 pgid = getpgid(cr_pid);
218 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", pgid);
219 bundle_del(kb, AUL_K_CALLEE_PID);
220 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
223 bundle_encode(kb, &kb_data, &datalen);
224 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
232 static int __app_process_by_pid(int cmd,
233 const char *pkg_name, struct ucred *cr, int clifd)
240 if (pkg_name == NULL)
243 pid = atoi(pkg_name);
249 appid = _status_app_get_appid_bypid(pid);
251 _E("pid %d is not an app", pid);
252 __real_send(clifd, -1);
257 case APP_RESUME_BY_PID:
258 ret = _resume_app(pid, clifd);
260 case APP_TERM_BY_PID:
261 case APP_TERM_BY_PID_WITHOUT_RESTART:
262 ret = _term_app(pid, clifd);
264 case APP_TERM_BGAPP_BY_PID:
265 ret = _term_bgapp(pid, clifd);
267 case APP_KILL_BY_PID:
268 if ((ret = _send_to_sigkill(pid)) < 0)
269 _E("fail to killing - %d\n", pid);
270 __real_send(clifd, ret);
272 case APP_TERM_REQ_BY_PID:
273 ret = _term_req_app(pid, clifd);
275 case APP_TERM_BY_PID_ASYNC:
276 if ((ret = __app_send_raw_with_noreply(pid, cmd, (unsigned char *)&dummy, sizeof(int))) < 0)
277 _D("terminate req packet send error");
279 __real_send(clifd, ret);
281 case APP_PAUSE_BY_PID:
282 ret = _pause_app(pid, clifd);
285 _E("unknown command: %d", cmd);
293 static gboolean __add_history_handler(gpointer user_data)
297 char *app_path = NULL;
300 rua_stat_pkt_t *pkt = (rua_stat_pkt_t *)user_data;
305 if (!pkt->is_group_app) {
307 ai = (struct appinfo *)appinfo_find(pkt->uid, pkt->appid);;
308 app_path = (char *)appinfo_get_value(ai, AIT_EXEC);
310 memset((void *)&rec, 0, sizeof(rec));
312 rec.pkg_name = pkt->appid;
313 rec.app_path = app_path;
318 SECURE_LOGD("add rua history %s %s", rec.pkg_name, rec.app_path);
320 ret = rua_add_history(&rec);
322 _D("rua add history error");
325 if (pkt->stat_caller != NULL && pkt->stat_tag != NULL) {
326 SECURE_LOGD("rua_stat_caller: %s, rua_stat_tag: %s", pkt->stat_caller, pkt->stat_tag);
327 rua_stat_update(pkt->stat_caller, pkt->stat_tag);
332 if (pkt->stat_caller)
333 free(pkt->stat_caller);
343 static int __release_srv(uid_t caller_uid, const char *appid)
346 const struct appinfo *ai;
348 ai = (struct appinfo *)appinfo_find(caller_uid, appid);
350 SECURE_LOGE("release service: '%s' not found", appid);
354 r = appinfo_get_boolean(ai, AIT_RESTART);
357 SECURE_LOGD("Auto restart set: '%s'", appid);
358 return _start_app_local(caller_uid, appid);
364 static void __handle_agent_dead_signal(struct ucred *pcr)
366 /* TODO: check the credentials from the caller: must be the amd agent */
368 _D("AGENT_DEAD_SIGNAL : %d", pcr->uid);
369 __agent_dead_handler(pcr->uid);
372 static int __dispatch_get_socket_pair(int clifd, const app_pkt_t *pkt, struct ucred *cr)
376 char *socket_pair_key;
377 int socket_pair_key_len;
382 struct sockaddr_un saddr;
383 char *datacontrol_type;
386 kb = bundle_decode(pkt->data, pkt->len);
387 caller = (char *)bundle_get_val(kb, AUL_K_CALLER_APPID);
388 callee = (char *)bundle_get_val(kb, AUL_K_CALLEE_APPID);
389 datacontrol_type = (char *)bundle_get_val(kb, "DATA_CONTOL_TYPE");
391 socket_pair_key_len = strlen(caller) + strlen(callee) + 2;
393 socket_pair_key = (char *)calloc(socket_pair_key_len, sizeof(char));
394 if (socket_pair_key == NULL) {
399 snprintf(socket_pair_key, socket_pair_key_len, "%s_%s", caller, callee);
400 _E("socket pair key : %s", socket_pair_key);
402 handles = g_hash_table_lookup(__socket_pair_hash, socket_pair_key);
403 if (handles == NULL) {
404 handles = (int *)calloc(2, sizeof(int));
405 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) {
406 _E("error create socket pair");
407 __send_result_to_client(clifd, -1);
411 if (handles[0] == -1) {
412 _E("error socket open");
413 __send_result_to_client(clifd, -1);
416 g_hash_table_insert(__socket_pair_hash, strdup(socket_pair_key),
419 _E("New socket pair insert done.");
423 memset(&saddr, 0, sizeof(saddr));
424 saddr.sun_family = AF_UNIX;
426 SECURE_LOGD("amd send fd : [%d, %d]", handles[0], handles[1]);
427 vec[0].iov_base = buffer;
428 vec[0].iov_len = strlen(buffer) + 1;
430 if (datacontrol_type != NULL) {
431 _E("datacontrol_type : %s", datacontrol_type);
432 if (strcmp(datacontrol_type, "consumer") == 0) {
433 msglen = __send_message(clifd, vec, 1, &handles[0], 1);
435 _E("Error[%d]: while sending message\n", -msglen);
436 __send_result_to_client(clifd, -1);
441 if (handles[1] == -1) {
442 _E("remove from hash : %s", socket_pair_key);
443 g_hash_table_remove(__socket_pair_hash, socket_pair_key);
447 msglen = __send_message(clifd, vec, 1, &handles[1], 1);
449 _E("Error[%d]: while sending message\n", -msglen);
450 __send_result_to_client(clifd, -1);
455 if (handles[0] == -1) {
456 _E("remove from hash : %s", socket_pair_key);
457 g_hash_table_remove(__socket_pair_hash, socket_pair_key);
461 SECURE_LOGD("send_message msglen : [%d]\n", msglen);
469 free(socket_pair_key);
474 static int __dispatch_app_group_get_window(int clifd, const app_pkt_t *pkt, struct ucred *cr)
481 b = bundle_decode(pkt->data, pkt->len);
482 bundle_get_str(b, AUL_K_PID, &buf);
485 wid = app_group_get_window(pid);
486 __send_result_to_client(clifd, wid);
491 static int __dispatch_app_group_set_window(int clifd, const app_pkt_t *pkt, struct ucred *cr)
498 b = bundle_decode(pkt->data, pkt->len);
499 bundle_get_str(b, AUL_K_WID, &buf);
502 ret = app_group_set_window(cr->pid, wid);
503 __send_result_to_client(clifd, ret);
508 static int __dispatch_app_group_get_fg_flag(int clifd, const app_pkt_t *pkt, struct ucred *cr)
515 b = bundle_decode(pkt->data, pkt->len);
516 bundle_get_str(b, AUL_K_PID, &buf);
519 fg = app_group_get_fg_flag(pid);
520 __send_result_to_client(clifd, fg);
525 static int __dispatch_app_group_clear_top(int clifd, const app_pkt_t *pkt, struct ucred *cr)
527 app_group_clear_top(cr->pid);
528 __send_result_to_client(clifd, 0);
533 static int __dispatch_app_group_get_leader_pid(int clifd,
534 const app_pkt_t *pkt, struct ucred *cr)
541 b = bundle_decode(pkt->data, pkt->len);
542 bundle_get_str(b, AUL_K_PID, &buf);
545 lpid = app_group_get_leader_pid(pid);
546 __send_result_to_client(clifd, lpid);
551 static int __dispatch_app_group_get_leader_pids(int clifd,
552 const app_pkt_t *pkt, struct ucred *cr)
556 unsigned char empty[1] = { 0 };
558 app_group_get_leader_pids(&cnt, &pids);
560 if (pids == NULL || cnt == 0) {
561 __send_result_data(clifd, APP_GROUP_GET_LEADER_PIDS, empty, 0);
563 __send_result_data(clifd, APP_GROUP_GET_LEADER_PIDS,
564 (unsigned char *)pids, cnt * sizeof(int));
572 static int __dispatch_app_group_get_idle_pids(int clifd,
573 const app_pkt_t *pkt, struct ucred *cr)
577 unsigned char empty[1] = { 0 };
579 app_group_get_idle_pids(&cnt, &pids);
581 if (pids == NULL || cnt == 0) {
582 __send_result_data(clifd, APP_GROUP_GET_IDLE_PIDS, empty, 0);
584 __send_result_data(clifd, APP_GROUP_GET_IDLE_PIDS,
585 (unsigned char *)pids, cnt * sizeof(int));
593 static int __dispatch_app_group_get_group_pids(int clifd, const app_pkt_t *pkt, struct ucred *cr)
600 unsigned char empty[1] = { 0 };
602 b = bundle_decode(pkt->data, pkt->len);
603 bundle_get_str(b, AUL_K_LEADER_PID, &buf);
604 leader_pid = atoi(buf);
607 app_group_get_group_pids(leader_pid, &cnt, &pids);
608 if (pids == NULL || cnt == 0) {
609 __send_result_data(clifd, APP_GROUP_GET_GROUP_PIDS, empty, 0);
611 __send_result_data(clifd, APP_GROUP_GET_GROUP_PIDS,
612 (unsigned char *)pids, cnt * sizeof(int));
620 static int __dispatch_app_group_lower(int clifd, const app_pkt_t *pkt, struct ucred *cr)
624 app_group_lower(cr->pid, &ret);
625 __send_result_to_client(clifd, ret);
630 static int __dispatch_app_start(int clifd, const app_pkt_t *pkt, struct ucred *cr)
633 const char *target_uid;
638 item_pkt_t *item = NULL;
639 char *stat_caller = NULL;
640 char *stat_tag = NULL;
641 rua_stat_pkt_t *rua_stat_item = NULL;
643 kb = bundle_decode(pkt->data, pkt->len);
649 appid = bundle_get_val(kb, AUL_K_APPID);
650 if (cr->uid < REGULAR_UID_MIN) {
651 target_uid = bundle_get_val(kb, AUL_K_TARGET_UID);
652 if (target_uid != NULL) {
653 t_uid = atoi(target_uid);
654 sd_uid_get_state(t_uid, &state);
655 if (strcmp(state, "offline") &&
656 strcmp(state, "closing")) {
657 ret = _start_app(appid, kb, pkt->cmd, cr->pid,
660 _E("uid:%d session is %s", t_uid, state);
661 __real_send(clifd, AUL_R_ERROR);
665 _E("request from root, treat as global user");
666 ret = _start_app(appid, kb, pkt->cmd, cr->pid,
670 ret = _start_app(appid, kb, pkt->cmd, cr->pid, cr->uid, clifd);
673 item = calloc(1, sizeof(item_pkt_t));
680 strncpy(item->appid, appid, 511);
682 g_timeout_add(1200, __add_item_running_list, item);
684 rua_stat_item = calloc(1, sizeof(rua_stat_pkt_t));
685 if (rua_stat_item == NULL) {
691 rua_stat_item->data = (char *)calloc(pkt->len, sizeof(char));
692 if (rua_stat_item->data == NULL) {
696 memcpy(rua_stat_item->data, pkt->data, pkt->len);
698 stat_caller = (char *)bundle_get_val(kb, AUL_SVC_K_RUA_STAT_CALLER);
699 stat_tag = (char *)bundle_get_val(kb, AUL_SVC_K_RUA_STAT_TAG);
701 rua_stat_item->len = pkt->len;
702 if (stat_caller != NULL) {
703 rua_stat_item->stat_caller = strdup(stat_caller);
704 if (rua_stat_item->stat_caller == NULL) {
710 if (stat_tag != NULL) {
711 rua_stat_item->stat_tag = strdup(stat_tag);
712 if (rua_stat_item->stat_tag == NULL) {
718 rua_stat_item->uid = cr->uid;
719 rua_stat_item->is_group_app = app_group_is_group_app(kb);
720 strncpy(rua_stat_item->appid, appid, 511);
722 g_timeout_add(1500, __add_history_handler, rua_stat_item);
733 if (rua_stat_item->data)
734 free(rua_stat_item->data);
735 if (rua_stat_item->stat_caller)
736 free(rua_stat_item->stat_caller);
737 if (rua_stat_item->stat_tag)
738 free(rua_stat_item->stat_tag);
744 static int __dispatch_app_result(int clifd, const app_pkt_t *pkt, struct ucred *cr)
748 kb = bundle_decode(pkt->data, pkt->len);
754 __foward_cmd(pkt->cmd, kb, cr->pid);
761 static int __dispatch_app_pause(int clifd, const app_pkt_t *pkt, struct ucred *cr)
767 kb = bundle_decode(pkt->data, pkt->len);
773 appid = (char *)bundle_get_val(kb, AUL_K_APPID);
774 ret = _status_app_is_running_v2(appid, cr->uid);
776 ret = _pause_app(ret, clifd);
778 _E("%s is not running", appid);
786 static int __dispatch_app_process_by_pid(int clifd, const app_pkt_t *pkt, struct ucred *cr)
791 kb = bundle_decode(pkt->data, pkt->len);
797 appid = (char *)bundle_get_val(kb, AUL_K_APPID);
798 __app_process_by_pid(pkt->cmd, appid, cr, clifd);
804 static int __dispatch_app_term_async(int clifd, const app_pkt_t *pkt, struct ucred *cr)
811 kb = bundle_decode(pkt->data, pkt->len);
817 term_pid = (char *)bundle_get_val(kb, AUL_K_APPID);
818 appid = _status_app_get_appid_bypid(atoi(term_pid));
819 ai = appinfo_find(cr->uid, appid);
821 appinfo_set_value(ai, AIT_STATUS, "norestart");
822 __app_process_by_pid(pkt->cmd, term_pid, cr, clifd);
831 static int __dispatch_app_term(int clifd, const app_pkt_t *pkt, struct ucred *cr)
836 kb = bundle_decode(pkt->data, pkt->len);
842 appid = (char *)bundle_get_val(kb, AUL_K_APPID);
843 __app_process_by_pid(pkt->cmd, appid, cr, clifd);
849 static int __dispatch_app_running_info(int clifd, const app_pkt_t *pkt, struct ucred *cr)
851 _status_send_running_appinfo(clifd, cr->uid);
855 static int __dispatch_app_is_running(int clifd, const app_pkt_t *pkt, struct ucred *cr)
860 appid = malloc(MAX_PACKAGE_STR_SIZE);
863 __send_result_to_client(clifd, -1);
866 strncpy(appid, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
867 ret = _status_app_is_running(appid, cr->uid);
868 SECURE_LOGD("APP_IS_RUNNING : %s : %d", appid, ret);
869 __send_result_to_client(clifd, ret);
875 static int __dispatch_app_get_appid_by_pid(int clifd, const app_pkt_t *pkt, struct ucred *cr)
880 memcpy(&pid, pkt->data, pkt->len);
881 ret = _status_get_appid_bypid(clifd, pid);
882 _D("app_get_appid_bypid : %d : %d", pid, ret);
886 static int __dispatch_app_get_pkgid_by_pid(int clifd, const app_pkt_t *pkt, struct ucred *cr)
891 memcpy(&pid, pkt->data, sizeof(int));
892 ret = _status_get_pkgid_bypid(clifd, pid);
893 _D("APP_GET_PKGID_BYPID : %d : %d", pid, ret);
897 static int __dispatch_legacy_command(int clifd, const app_pkt_t *pkt, struct ucred *cr)
899 __send_result_to_client(clifd, 0);
903 static int __dispatch_app_status_update(int clifd, const app_pkt_t *pkt, struct ucred *cr)
909 status = (int *)pkt->data;
910 if (*status == STATUS_NORESTART) {
911 appid = _status_app_get_appid_bypid(cr->pid);
912 ai = appinfo_find(cr->uid, appid);
913 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "norestart");
915 _status_update_app_info_list(cr->pid, *status, cr->uid);
922 static int __dispatch_app_get_status(int clifd, const app_pkt_t *pkt, struct ucred *cr)
927 memcpy(&pid, pkt->data, sizeof(int));
928 ret = _status_get_app_info_status(pid, 0);
929 __send_result_to_client(clifd, ret);
934 static int __dispatch_app_released(int clifd, const app_pkt_t *pkt, struct ucred *cr)
939 appid = malloc(MAX_PACKAGE_STR_SIZE);
942 __send_result_to_client(clifd, -1);
945 strncpy(appid, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
946 ret = __release_srv(cr->uid, appid);
947 __send_result_to_client(clifd, ret);
953 static int __dispatch_agent_dead_signal(int clifd, const app_pkt_t *pkt, struct ucred *cr)
955 _D("AMD_AGENT_DEAD_SIGNAL");
956 __handle_agent_dead_signal(cr);
962 static int __dispatch_amd_reload_appinfo(int clifd, const app_pkt_t *pkt, struct ucred *cr)
964 _D("AMD_RELOAD_APPINFO");
966 __send_result_to_client(clifd, 0);
971 static int __get_caller_info_from_cynara(int sockfd, char **client, char **user, char **session)
975 char buf[MAX_LOCAL_BUFSZ] = {0,};
977 r = cynara_creds_socket_get_pid(sockfd, &pid);
978 if (r != CYNARA_API_SUCCESS) {
979 cynara_strerror(r, buf, MAX_LOCAL_BUFSZ);
980 _E("cynara_creds_socket_get_pid failed: %s", buf);
984 *session = cynara_session_from_pid(pid);
985 if (*session == NULL) {
986 _E("cynara_session_from_pid failed.");
990 r = cynara_creds_socket_get_user(sockfd, USER_METHOD_DEFAULT, user);
991 if (r != CYNARA_API_SUCCESS) {
992 cynara_strerror(r, buf, MAX_LOCAL_BUFSZ);
993 _E("cynara_cred_socket_get_user failed.");
997 r = cynara_creds_socket_get_client(sockfd, CLIENT_METHOD_DEFAULT, client);
998 if (r != CYNARA_API_SUCCESS) {
999 cynara_strerror(r, buf, MAX_LOCAL_BUFSZ);
1000 _E("cynara_creds_socket_get_client failed.");
1007 static const char *__convert_cmd_to_privilege(int cmd)
1014 return PRIVILEGE_APPMANAGER_LAUNCH;
1015 case APP_TERM_BY_PID_WITHOUT_RESTART:
1016 case APP_TERM_BY_PID_ASYNC:
1017 case APP_TERM_BY_PID:
1018 case APP_KILL_BY_PID:
1019 return PRIVILEGE_APPMANAGER_KILL;
1020 case APP_TERM_BGAPP_BY_PID:
1021 return PRIVILEGE_APPMANAGER_KILL_BGAPP;
1027 static int __check_privilege_by_cynara(int sockfd, const char *privilege)
1031 char buf[MAX_LOCAL_BUFSZ] = {0,};
1032 char *client = NULL;
1033 char *session = NULL;
1036 r = __get_caller_info_from_cynara(sockfd, &client, &user, &session);
1042 r = cynara_check(r_cynara, client, session, user, privilege);
1044 case CYNARA_API_ACCESS_ALLOWED:
1045 _D("%s(%s) from user %s privilege %s allowed.", client, session, user, privilege);
1048 case CYNARA_API_ACCESS_DENIED:
1049 _E("%s(%s) from user %s privilege %s denied.", client, session, user, privilege);
1053 cynara_strerror(r, buf, MAX_LOCAL_BUFSZ);
1054 _E("cynara_check failed: %s", buf);
1067 static app_cmd_dispatch_func dispatch_table[APP_CMD_MAX] = {
1068 [APP_GET_SOCKET_PAIR] = __dispatch_get_socket_pair,
1069 [APP_START] = __dispatch_app_start,
1070 [APP_OPEN] = __dispatch_app_start,
1071 [APP_RESUME] = __dispatch_app_start,
1072 [APP_RESUME_BY_PID] = __dispatch_app_process_by_pid,
1073 [APP_TERM_BY_PID] = __dispatch_app_term,
1074 [APP_TERM_BY_PID_WITHOUT_RESTART] = __dispatch_app_term_async,
1075 [APP_RESULT] = __dispatch_app_result,
1076 [APP_START_RES] = __dispatch_app_start,
1077 [APP_CANCEL] = __dispatch_app_result,
1078 [APP_KILL_BY_PID] = __dispatch_app_term,
1079 [APP_ADD_HISTORY] = NULL,
1080 [APP_RUNNING_INFO] = __dispatch_app_running_info,
1081 [APP_RUNNING_INFO_RESULT] = NULL,
1082 [APP_IS_RUNNING] = __dispatch_app_is_running,
1083 [APP_GET_APPID_BYPID] = __dispatch_app_get_appid_by_pid,
1084 [APP_GET_PKGID_BYPID] = __dispatch_app_get_pkgid_by_pid,
1085 [APP_GET_INFO_OK] = NULL,
1086 [APP_GET_INFO_ERROR] = NULL,
1087 [APP_KEY_EVENT] = NULL,
1088 [APP_KEY_RESERVE] = __dispatch_legacy_command,
1089 [APP_KEY_RELEASE] = __dispatch_legacy_command,
1090 [APP_STATUS_UPDATE] = __dispatch_app_status_update,
1091 [APP_RELEASED] = __dispatch_app_released,
1092 [APP_RUNNING_LIST_UPDATE] = __dispatch_legacy_command,
1093 [APP_TERM_REQ_BY_PID] = __dispatch_app_process_by_pid,
1094 [APP_TERM_BY_PID_ASYNC] = __dispatch_app_term_async,
1095 [APP_TERM_BGAPP_BY_PID] = __dispatch_app_term,
1096 [APP_PAUSE] = __dispatch_app_pause,
1097 [APP_PAUSE_BY_PID] = __dispatch_app_process_by_pid,
1098 [APP_GROUP_GET_WINDOW] = __dispatch_app_group_get_window,
1099 [APP_GROUP_SET_WINDOW] = __dispatch_app_group_set_window,
1100 [APP_GROUP_GET_FG] = __dispatch_app_group_get_fg_flag,
1101 [APP_GROUP_GET_LEADER_PID] = __dispatch_app_group_get_leader_pid,
1102 [APP_GROUP_GET_LEADER_PIDS] = __dispatch_app_group_get_leader_pids,
1103 [APP_GROUP_GET_GROUP_PIDS] = __dispatch_app_group_get_group_pids,
1104 [APP_GROUP_GET_IDLE_PIDS] = __dispatch_app_group_get_idle_pids,
1105 [APP_GROUP_LOWER] = __dispatch_app_group_lower,
1106 [APP_GROUP_CLEAR_TOP] = __dispatch_app_group_clear_top,
1107 [APP_GET_STATUS] = __dispatch_app_get_status,
1108 [AMD_RELOAD_APPINFO] = __dispatch_amd_reload_appinfo,
1109 [AGENT_DEAD_SIGNAL] = __dispatch_agent_dead_signal,
1112 static gboolean __request_handler(gpointer data)
1114 GPollFD *gpollfd = (GPollFD *) data;
1115 int fd = gpollfd->fd;
1120 const char *privilege;
1122 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
1127 if (cr.uid >= REGULAR_UID_MIN) {
1128 privilege = __convert_cmd_to_privilege(pkt->cmd);
1130 ret = __check_privilege_by_cynara(clifd, privilege);
1132 _E("request has been denied by smack");
1133 ret = -EILLEGALACCESS;
1134 __real_send(clifd, ret);
1141 if (pkt->cmd >= 0 && pkt->cmd < APP_CMD_MAX && dispatch_table[pkt->cmd]) {
1142 if (dispatch_table[pkt->cmd](clifd, pkt, &cr) != 0)
1143 _E("callback returns FALSE : %d", pkt->cmd);
1145 _E("Invalid packet or not supported command");
1153 static gboolean __au_glib_check(GSource *src)
1158 fd_list = src->poll_fds;
1160 tmp = (GPollFD *) fd_list->data;
1161 if ((tmp->revents & (POLLIN | POLLPRI)))
1163 fd_list = fd_list->next;
1169 static gboolean __au_glib_dispatch(GSource *src, GSourceFunc callback,
1176 static gboolean __au_glib_prepare(GSource *src, gint *timeout)
1181 static GSourceFuncs funcs = {
1182 .prepare = __au_glib_prepare,
1183 .check = __au_glib_check,
1184 .dispatch = __au_glib_dispatch,
1188 int _request_init(void)
1195 __socket_pair_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1197 fd = __create_sock_activation();
1199 _D("Create server socket without socket activation");
1200 fd = __create_server_sock(AUL_UTIL_PID);
1202 _E("Create server socket failed.");
1207 r = cynara_initialize(&r_cynara, NULL);
1208 if (r != CYNARA_API_SUCCESS) {
1209 _E("cynara initialize failed.");
1214 src = g_source_new(&funcs, sizeof(GSource));
1216 _E("out of memory");
1217 cynara_finish(r_cynara);
1222 gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
1224 _E("out of memory");
1225 g_source_destroy(src);
1226 cynara_finish(r_cynara);
1231 gpollfd->events = POLLIN;
1234 g_source_add_poll(src, gpollfd);
1235 g_source_set_callback(src, (GSourceFunc) __request_handler,
1236 (gpointer) gpollfd, NULL);
1237 g_source_set_priority(src, G_PRIORITY_DEFAULT);
1239 r = g_source_attach(src, NULL);
1242 g_source_destroy(src);
1243 cynara_finish(r_cynara);
1248 r = rua_clear_history();
1250 _D("rua_clear_history : %d", r);