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>
37 #include <tzplatform_config.h>
38 #include <cynara-client.h>
39 #include <cynara-creds-socket.h>
40 #include <cynara-session.h>
41 #include <systemd/sd-login.h>
43 #include "amd_config.h"
44 #include "simple_util.h"
47 #include "amd_request.h"
48 #include "amd_launch.h"
49 #include "amd_appinfo.h"
50 #include "amd_status.h"
51 #include "amd_app_group.h"
53 #define INHOUSE_UID tzplatform_getuid(TZ_USER_NAME)
54 #define REGULAR_UID_MIN 5000
56 #define PRIVILEGE_APPMANAGER_LAUNCH "http://tizen.org/privilege/appmanager.launch"
57 #define PRIVILEGE_APPMANAGER_KILL "http://tizen.org/privilege/appmanager.kill"
58 #define PRIVILEGE_APPMANAGER_KILL_BGAPP "http://tizen.org/privilege/appmanager.kill.bgapp"
60 #define MAX_NR_OF_DESCRIPTORS 2
61 static GHashTable *__socket_pair_hash = NULL;
63 typedef int (*app_cmd_dispatch_func)(int clifd, const app_pkt_t *pkt, struct ucred *cr);
65 static cynara *r_cynara = NULL;
67 static int __send_result_to_client(int fd, int res);
68 static gboolean __request_handler(gpointer data);
70 static int __send_message(int sock, const struct iovec *vec, int vec_size, const int *desc, int nr_desc)
72 struct msghdr msg = {0};
75 if (vec == NULL || vec_size < 1)
77 if (nr_desc < 0 || nr_desc > MAX_NR_OF_DESCRIPTORS)
82 msg.msg_iov = (struct iovec *)vec;
83 msg.msg_iovlen = vec_size;
85 /* sending ancillary data */
88 struct cmsghdr *cmsg = NULL;
89 char buff[CMSG_SPACE(sizeof(int) * MAX_NR_OF_DESCRIPTORS)] = {0};
91 msg.msg_control = buff;
92 msg.msg_controllen = sizeof(buff);
93 cmsg = CMSG_FIRSTHDR(&msg);
97 /* packing files descriptors */
99 cmsg->cmsg_level = SOL_SOCKET;
100 cmsg->cmsg_type = SCM_RIGHTS;
101 desclen = cmsg->cmsg_len = CMSG_LEN(sizeof(int) * nr_desc);
102 memcpy((int *)CMSG_DATA(cmsg), desc, sizeof(int) * nr_desc);
103 cmsg = CMSG_NXTHDR(&msg, cmsg);
105 _D("packing file descriptors done");
108 /* finished packing updating the corect length */
109 msg.msg_controllen = desclen;
111 msg.msg_control = NULL;
112 msg.msg_controllen = 0;
115 sndret = sendmsg(sock, &msg, 0);
117 _D("sendmsg ret : %d", sndret);
124 static int __send_result_data(int fd, int cmd, unsigned char *kb_data, int datalen)
129 app_pkt_t *pkt = NULL;
131 pkt = (app_pkt_t *)malloc(AUL_PKT_HEADER_SIZE + datalen);
133 _E("Malloc Failed!");
139 memcpy(pkt->data, kb_data, datalen);
141 while (sent != AUL_PKT_HEADER_SIZE + datalen) {
142 len = send(fd, pkt, AUL_PKT_HEADER_SIZE + datalen - sent, 0);
144 _E("send error fd:%d (errno %d)", fd, errno);
158 extern int __app_dead_handler(int pid, uid_t user);
159 extern int __agent_dead_handler(uid_t user);
161 static int __send_result_to_client(int fd, int res)
163 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
165 _E("send failed due to EPIPE.\n");
166 _E("send fail to client");
172 static void __real_send(int clifd, int ret)
174 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
176 _E("send failed due to EPIPE.\n");
178 _E("send fail to client");
184 static int __get_caller_pid(bundle *kb)
189 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
193 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
205 static int __foward_cmd(int cmd, bundle *kb, int cr_pid)
209 char tmp_pid[MAX_PID_STR_BUFSZ];
214 if ((pid = __get_caller_pid(kb)) < 0)
217 pgid = getpgid(cr_pid);
219 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", pgid);
220 bundle_del(kb, AUL_K_CALLEE_PID);
221 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
224 bundle_encode(kb, &kb_data, &datalen);
225 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
233 static int __app_process_by_pid(int cmd,
234 const char *pkg_name, struct ucred *cr, int clifd)
241 if (pkg_name == NULL)
244 pid = atoi(pkg_name);
250 appid = _status_app_get_appid_bypid(pid);
252 _E("pid %d is not an app", pid);
253 __real_send(clifd, -1);
258 case APP_RESUME_BY_PID:
259 ret = _resume_app(pid, clifd);
261 case APP_TERM_BY_PID:
262 case APP_TERM_BY_PID_WITHOUT_RESTART:
263 ret = _term_app(pid, clifd);
265 case APP_TERM_BGAPP_BY_PID:
266 ret = _term_bgapp(pid, clifd);
268 case APP_KILL_BY_PID:
269 if ((ret = _send_to_sigkill(pid)) < 0)
270 _E("fail to killing - %d\n", pid);
271 __real_send(clifd, ret);
273 case APP_TERM_REQ_BY_PID:
274 ret = _term_req_app(pid, clifd);
276 case APP_TERM_BY_PID_ASYNC:
277 if ((ret = __app_send_raw_with_noreply(pid, cmd, (unsigned char *)&dummy, sizeof(int))) < 0)
278 _D("terminate req packet send error");
280 __real_send(clifd, ret);
282 case APP_PAUSE_BY_PID:
283 ret = _pause_app(pid, clifd);
286 _E("unknown command: %d", cmd);
294 static gboolean __add_history_handler(gpointer user_data)
298 char *app_path = NULL;
301 rua_stat_pkt_t *pkt = (rua_stat_pkt_t *)user_data;
306 if (!pkt->is_group_app) {
308 ai = (struct appinfo *)appinfo_find(pkt->uid, pkt->appid);;
309 app_path = (char *)appinfo_get_value(ai, AIT_EXEC);
311 memset((void *)&rec, 0, sizeof(rec));
313 rec.pkg_name = pkt->appid;
314 rec.app_path = app_path;
319 SECURE_LOGD("add rua history %s %s", rec.pkg_name, rec.app_path);
321 ret = rua_add_history(&rec);
323 _D("rua add history error");
326 if (pkt->stat_caller != NULL && pkt->stat_tag != NULL) {
327 SECURE_LOGD("rua_stat_caller: %s, rua_stat_tag: %s", pkt->stat_caller, pkt->stat_tag);
328 rua_stat_update(pkt->stat_caller, pkt->stat_tag);
333 if (pkt->stat_caller)
334 free(pkt->stat_caller);
344 static int __release_srv(uid_t caller_uid, const char *appid)
347 const struct appinfo *ai;
349 ai = (struct appinfo *)appinfo_find(caller_uid, appid);
351 SECURE_LOGE("release service: '%s' not found", appid);
355 r = appinfo_get_boolean(ai, AIT_RESTART);
358 SECURE_LOGD("Auto restart set: '%s'", appid);
359 return _start_app_local(caller_uid, appid);
365 static void __handle_agent_dead_signal(struct ucred *pcr)
367 /* TODO: check the credentials from the caller: must be the amd agent */
369 _D("AGENT_DEAD_SIGNAL : %d", pcr->uid);
370 __agent_dead_handler(pcr->uid);
373 static int __dispatch_get_socket_pair(int clifd, const app_pkt_t *pkt, struct ucred *cr)
377 char *socket_pair_key;
378 int socket_pair_key_len;
383 struct sockaddr_un saddr;
384 char *datacontrol_type;
387 kb = bundle_decode(pkt->data, pkt->len);
388 caller = (char *)bundle_get_val(kb, AUL_K_CALLER_APPID);
389 callee = (char *)bundle_get_val(kb, AUL_K_CALLEE_APPID);
390 datacontrol_type = (char *)bundle_get_val(kb, "DATA_CONTOL_TYPE");
392 socket_pair_key_len = strlen(caller) + strlen(callee) + 2;
394 socket_pair_key = (char *)calloc(socket_pair_key_len, sizeof(char));
395 if (socket_pair_key == NULL) {
400 snprintf(socket_pair_key, socket_pair_key_len, "%s_%s", caller, callee);
401 _E("socket pair key : %s", socket_pair_key);
403 handles = g_hash_table_lookup(__socket_pair_hash, socket_pair_key);
404 if (handles == NULL) {
405 handles = (int *)calloc(2, sizeof(int));
406 if (socketpair(AF_UNIX, SOCK_STREAM, 0, handles) != 0) {
407 _E("error create socket pair");
408 __send_result_to_client(clifd, -1);
412 if (handles[0] == -1) {
413 _E("error socket open");
414 __send_result_to_client(clifd, -1);
417 g_hash_table_insert(__socket_pair_hash, strdup(socket_pair_key),
420 _E("New socket pair insert done.");
424 memset(&saddr, 0, sizeof(saddr));
425 saddr.sun_family = AF_UNIX;
427 SECURE_LOGD("amd send fd : [%d, %d]", handles[0], handles[1]);
428 vec[0].iov_base = buffer;
429 vec[0].iov_len = strlen(buffer) + 1;
431 if (datacontrol_type != NULL) {
432 _E("datacontrol_type : %s", datacontrol_type);
433 if (strcmp(datacontrol_type, "consumer") == 0) {
434 msglen = __send_message(clifd, vec, 1, &handles[0], 1);
436 _E("Error[%d]: while sending message\n", -msglen);
437 __send_result_to_client(clifd, -1);
442 if (handles[1] == -1) {
443 _E("remove from hash : %s", socket_pair_key);
444 g_hash_table_remove(__socket_pair_hash, socket_pair_key);
448 msglen = __send_message(clifd, vec, 1, &handles[1], 1);
450 _E("Error[%d]: while sending message\n", -msglen);
451 __send_result_to_client(clifd, -1);
456 if (handles[0] == -1) {
457 _E("remove from hash : %s", socket_pair_key);
458 g_hash_table_remove(__socket_pair_hash, socket_pair_key);
462 SECURE_LOGD("send_message msglen : [%d]\n", msglen);
470 free(socket_pair_key);
475 static int __dispatch_app_group_get_window(int clifd, const app_pkt_t *pkt, struct ucred *cr)
482 b = bundle_decode(pkt->data, pkt->len);
483 bundle_get_str(b, AUL_K_PID, &buf);
486 wid = app_group_get_window(pid);
487 __send_result_to_client(clifd, wid);
492 static int __dispatch_app_group_set_window(int clifd, const app_pkt_t *pkt, struct ucred *cr)
499 b = bundle_decode(pkt->data, pkt->len);
500 bundle_get_str(b, AUL_K_WID, &buf);
503 ret = app_group_set_window(cr->pid, wid);
504 __send_result_to_client(clifd, ret);
509 static int __dispatch_app_group_get_fg_flag(int clifd, const app_pkt_t *pkt, struct ucred *cr)
516 b = bundle_decode(pkt->data, pkt->len);
517 bundle_get_str(b, AUL_K_PID, &buf);
520 fg = app_group_get_fg_flag(pid);
521 __send_result_to_client(clifd, fg);
526 static int __dispatch_app_group_clear_top(int clifd, const app_pkt_t *pkt, struct ucred *cr)
528 app_group_clear_top(cr->pid);
529 __send_result_to_client(clifd, 0);
534 static int __dispatch_app_group_get_leader_pid(int clifd,
535 const app_pkt_t *pkt, struct ucred *cr)
542 b = bundle_decode(pkt->data, pkt->len);
543 bundle_get_str(b, AUL_K_PID, &buf);
546 lpid = app_group_get_leader_pid(pid);
547 __send_result_to_client(clifd, lpid);
552 static int __dispatch_app_group_get_leader_pids(int clifd,
553 const app_pkt_t *pkt, struct ucred *cr)
557 unsigned char empty[1] = { 0 };
559 app_group_get_leader_pids(&cnt, &pids);
561 if (pids == NULL || cnt == 0) {
562 __send_result_data(clifd, APP_GROUP_GET_LEADER_PIDS, empty, 0);
564 __send_result_data(clifd, APP_GROUP_GET_LEADER_PIDS,
565 (unsigned char *)pids, cnt * sizeof(int));
573 static int __dispatch_app_group_get_idle_pids(int clifd,
574 const app_pkt_t *pkt, struct ucred *cr)
578 unsigned char empty[1] = { 0 };
580 app_group_get_idle_pids(&cnt, &pids);
582 if (pids == NULL || cnt == 0) {
583 __send_result_data(clifd, APP_GROUP_GET_IDLE_PIDS, empty, 0);
585 __send_result_data(clifd, APP_GROUP_GET_IDLE_PIDS,
586 (unsigned char *)pids, cnt * sizeof(int));
594 static int __dispatch_app_group_get_group_pids(int clifd, const app_pkt_t *pkt, struct ucred *cr)
601 unsigned char empty[1] = { 0 };
603 b = bundle_decode(pkt->data, pkt->len);
604 bundle_get_str(b, AUL_K_LEADER_PID, &buf);
605 leader_pid = atoi(buf);
608 app_group_get_group_pids(leader_pid, &cnt, &pids);
609 if (pids == NULL || cnt == 0) {
610 __send_result_data(clifd, APP_GROUP_GET_GROUP_PIDS, empty, 0);
612 __send_result_data(clifd, APP_GROUP_GET_GROUP_PIDS,
613 (unsigned char *)pids, cnt * sizeof(int));
621 static int __dispatch_app_group_lower(int clifd, const app_pkt_t *pkt, struct ucred *cr)
625 app_group_lower(cr->pid, &ret);
626 __send_result_to_client(clifd, ret);
631 static int __dispatch_app_start(int clifd, const app_pkt_t *pkt, struct ucred *cr)
634 const char *target_uid;
639 item_pkt_t *item = NULL;
640 char *stat_caller = NULL;
641 char *stat_tag = NULL;
642 rua_stat_pkt_t *rua_stat_item = NULL;
644 kb = bundle_decode(pkt->data, pkt->len);
650 appid = bundle_get_val(kb, AUL_K_APPID);
651 if (cr->uid < REGULAR_UID_MIN) {
652 target_uid = bundle_get_val(kb, AUL_K_TARGET_UID);
653 if (target_uid != NULL) {
654 t_uid = atoi(target_uid);
655 sd_uid_get_state(t_uid, &state);
656 if (strcmp(state, "offline") &&
657 strcmp(state, "closing")) {
658 ret = _start_app(appid, kb, pkt->cmd, cr->pid,
661 _E("uid:%d session is %s", t_uid, state);
662 __real_send(clifd, AUL_R_ERROR);
666 _E("request from root, treat as global user");
667 ret = _start_app(appid, kb, pkt->cmd, cr->pid,
671 ret = _start_app(appid, kb, pkt->cmd, cr->pid, cr->uid, clifd);
674 item = calloc(1, sizeof(item_pkt_t));
681 strncpy(item->appid, appid, 511);
683 g_timeout_add(1200, __add_item_running_list, item);
685 rua_stat_item = calloc(1, sizeof(rua_stat_pkt_t));
686 if (rua_stat_item == NULL) {
692 rua_stat_item->data = (char *)calloc(pkt->len, sizeof(char));
693 if (rua_stat_item->data == NULL) {
697 memcpy(rua_stat_item->data, pkt->data, pkt->len);
699 stat_caller = (char *)bundle_get_val(kb, AUL_SVC_K_RUA_STAT_CALLER);
700 stat_tag = (char *)bundle_get_val(kb, AUL_SVC_K_RUA_STAT_TAG);
702 rua_stat_item->len = pkt->len;
703 if (stat_caller != NULL) {
704 rua_stat_item->stat_caller = strdup(stat_caller);
705 if (rua_stat_item->stat_caller == NULL) {
711 if (stat_tag != NULL) {
712 rua_stat_item->stat_tag = strdup(stat_tag);
713 if (rua_stat_item->stat_tag == NULL) {
719 rua_stat_item->uid = cr->uid;
720 rua_stat_item->is_group_app = app_group_is_group_app(kb);
721 strncpy(rua_stat_item->appid, appid, 511);
723 g_timeout_add(1500, __add_history_handler, rua_stat_item);
734 if (rua_stat_item->data)
735 free(rua_stat_item->data);
736 if (rua_stat_item->stat_caller)
737 free(rua_stat_item->stat_caller);
738 if (rua_stat_item->stat_tag)
739 free(rua_stat_item->stat_tag);
745 static int __dispatch_app_result(int clifd, const app_pkt_t *pkt, struct ucred *cr)
749 kb = bundle_decode(pkt->data, pkt->len);
755 __foward_cmd(pkt->cmd, kb, cr->pid);
762 static int __dispatch_app_pause(int clifd, const app_pkt_t *pkt, struct ucred *cr)
768 kb = bundle_decode(pkt->data, pkt->len);
774 appid = (char *)bundle_get_val(kb, AUL_K_APPID);
775 ret = _status_app_is_running_v2(appid, cr->uid);
777 ret = _pause_app(ret, clifd);
779 _E("%s is not running", appid);
787 static int __dispatch_app_process_by_pid(int clifd, const app_pkt_t *pkt, struct ucred *cr)
792 kb = bundle_decode(pkt->data, pkt->len);
798 appid = (char *)bundle_get_val(kb, AUL_K_APPID);
799 __app_process_by_pid(pkt->cmd, appid, cr, clifd);
805 static int __dispatch_app_term_async(int clifd, const app_pkt_t *pkt, struct ucred *cr)
812 kb = bundle_decode(pkt->data, pkt->len);
818 term_pid = (char *)bundle_get_val(kb, AUL_K_APPID);
819 appid = _status_app_get_appid_bypid(atoi(term_pid));
820 ai = appinfo_find(cr->uid, appid);
822 appinfo_set_value(ai, AIT_STATUS, "norestart");
823 __app_process_by_pid(pkt->cmd, term_pid, cr, clifd);
832 static int __dispatch_app_term(int clifd, const app_pkt_t *pkt, struct ucred *cr)
837 kb = bundle_decode(pkt->data, pkt->len);
843 appid = (char *)bundle_get_val(kb, AUL_K_APPID);
844 __app_process_by_pid(pkt->cmd, appid, cr, clifd);
850 static int __dispatch_app_running_info(int clifd, const app_pkt_t *pkt, struct ucred *cr)
852 _status_send_running_appinfo(clifd, cr->uid);
856 static int __dispatch_app_is_running(int clifd, const app_pkt_t *pkt, struct ucred *cr)
861 appid = malloc(MAX_PACKAGE_STR_SIZE);
864 __send_result_to_client(clifd, -1);
867 strncpy(appid, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
868 ret = _status_app_is_running(appid, cr->uid);
869 SECURE_LOGD("APP_IS_RUNNING : %s : %d", appid, ret);
870 __send_result_to_client(clifd, ret);
876 static int __dispatch_app_get_appid_by_pid(int clifd, const app_pkt_t *pkt, struct ucred *cr)
881 memcpy(&pid, pkt->data, pkt->len);
882 ret = _status_get_appid_bypid(clifd, pid);
883 _D("app_get_appid_bypid : %d : %d", pid, ret);
887 static int __dispatch_app_get_pkgid_by_pid(int clifd, const app_pkt_t *pkt, struct ucred *cr)
892 memcpy(&pid, pkt->data, sizeof(int));
893 ret = _status_get_pkgid_bypid(clifd, pid);
894 _D("APP_GET_PKGID_BYPID : %d : %d", pid, ret);
898 static int __dispatch_legacy_command(int clifd, const app_pkt_t *pkt, struct ucred *cr)
900 __send_result_to_client(clifd, 0);
904 static int __dispatch_app_status_update(int clifd, const app_pkt_t *pkt, struct ucred *cr)
910 status = (int *)pkt->data;
911 if (*status == STATUS_NORESTART) {
912 appid = _status_app_get_appid_bypid(cr->pid);
913 ai = appinfo_find(cr->uid, appid);
914 appinfo_set_value((struct appinfo *)ai, AIT_STATUS, "norestart");
916 _status_update_app_info_list(cr->pid, *status, cr->uid);
923 static int __dispatch_app_get_status(int clifd, const app_pkt_t *pkt, struct ucred *cr)
928 memcpy(&pid, pkt->data, sizeof(int));
929 ret = _status_get_app_info_status(pid, 0);
930 __send_result_to_client(clifd, ret);
935 static int __dispatch_app_released(int clifd, const app_pkt_t *pkt, struct ucred *cr)
940 appid = malloc(MAX_PACKAGE_STR_SIZE);
943 __send_result_to_client(clifd, -1);
946 strncpy(appid, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
947 ret = __release_srv(cr->uid, appid);
948 __send_result_to_client(clifd, ret);
954 static int __dispatch_agent_dead_signal(int clifd, const app_pkt_t *pkt, struct ucred *cr)
956 _D("AMD_AGENT_DEAD_SIGNAL");
957 __handle_agent_dead_signal(cr);
963 static int __dispatch_amd_reload_appinfo(int clifd, const app_pkt_t *pkt, struct ucred *cr)
965 _D("AMD_RELOAD_APPINFO");
967 __send_result_to_client(clifd, 0);
972 static int __get_caller_info_from_cynara(int sockfd, char **client, char **user, char **session)
976 char buf[MAX_LOCAL_BUFSZ] = {0,};
978 r = cynara_creds_socket_get_pid(sockfd, &pid);
979 if (r != CYNARA_API_SUCCESS) {
980 cynara_strerror(r, buf, MAX_LOCAL_BUFSZ);
981 _E("cynara_creds_socket_get_pid failed: %s", buf);
985 *session = cynara_session_from_pid(pid);
986 if (*session == NULL) {
987 _E("cynara_session_from_pid failed.");
991 r = cynara_creds_socket_get_user(sockfd, USER_METHOD_DEFAULT, user);
992 if (r != CYNARA_API_SUCCESS) {
993 cynara_strerror(r, buf, MAX_LOCAL_BUFSZ);
994 _E("cynara_cred_socket_get_user failed.");
998 r = cynara_creds_socket_get_client(sockfd, CLIENT_METHOD_DEFAULT, client);
999 if (r != CYNARA_API_SUCCESS) {
1000 cynara_strerror(r, buf, MAX_LOCAL_BUFSZ);
1001 _E("cynara_creds_socket_get_client failed.");
1008 static const char *__convert_cmd_to_privilege(int cmd)
1015 return PRIVILEGE_APPMANAGER_LAUNCH;
1016 case APP_TERM_BY_PID_WITHOUT_RESTART:
1017 case APP_TERM_BY_PID_ASYNC:
1018 case APP_TERM_BY_PID:
1019 case APP_KILL_BY_PID:
1020 return PRIVILEGE_APPMANAGER_KILL;
1021 case APP_TERM_BGAPP_BY_PID:
1022 return PRIVILEGE_APPMANAGER_KILL_BGAPP;
1028 static int __check_privilege_by_cynara(int sockfd, const char *privilege)
1032 char buf[MAX_LOCAL_BUFSZ] = {0,};
1033 char *client = NULL;
1034 char *session = NULL;
1037 r = __get_caller_info_from_cynara(sockfd, &client, &user, &session);
1043 r = cynara_check(r_cynara, client, session, user, privilege);
1045 case CYNARA_API_ACCESS_ALLOWED:
1046 _D("%s(%s) from user %s privilege %s allowed.", client, session, user, privilege);
1049 case CYNARA_API_ACCESS_DENIED:
1050 _E("%s(%s) from user %s privilege %s denied.", client, session, user, privilege);
1054 cynara_strerror(r, buf, MAX_LOCAL_BUFSZ);
1055 _E("cynara_check failed: %s", buf);
1068 static app_cmd_dispatch_func dispatch_table[APP_CMD_MAX] = {
1069 [APP_GET_SOCKET_PAIR] = __dispatch_get_socket_pair,
1070 [APP_START] = __dispatch_app_start,
1071 [APP_OPEN] = __dispatch_app_start,
1072 [APP_RESUME] = __dispatch_app_start,
1073 [APP_RESUME_BY_PID] = __dispatch_app_process_by_pid,
1074 [APP_TERM_BY_PID] = __dispatch_app_term,
1075 [APP_TERM_BY_PID_WITHOUT_RESTART] = __dispatch_app_term_async,
1076 [APP_RESULT] = __dispatch_app_result,
1077 [APP_START_RES] = __dispatch_app_start,
1078 [APP_CANCEL] = __dispatch_app_result,
1079 [APP_KILL_BY_PID] = __dispatch_app_term,
1080 [APP_ADD_HISTORY] = NULL,
1081 [APP_RUNNING_INFO] = __dispatch_app_running_info,
1082 [APP_RUNNING_INFO_RESULT] = NULL,
1083 [APP_IS_RUNNING] = __dispatch_app_is_running,
1084 [APP_GET_APPID_BYPID] = __dispatch_app_get_appid_by_pid,
1085 [APP_GET_PKGID_BYPID] = __dispatch_app_get_pkgid_by_pid,
1086 [APP_GET_INFO_OK] = NULL,
1087 [APP_GET_INFO_ERROR] = NULL,
1088 [APP_KEY_EVENT] = NULL,
1089 [APP_KEY_RESERVE] = __dispatch_legacy_command,
1090 [APP_KEY_RELEASE] = __dispatch_legacy_command,
1091 [APP_STATUS_UPDATE] = __dispatch_app_status_update,
1092 [APP_RELEASED] = __dispatch_app_released,
1093 [APP_RUNNING_LIST_UPDATE] = __dispatch_legacy_command,
1094 [APP_TERM_REQ_BY_PID] = __dispatch_app_process_by_pid,
1095 [APP_TERM_BY_PID_ASYNC] = __dispatch_app_term_async,
1096 [APP_TERM_BGAPP_BY_PID] = __dispatch_app_term,
1097 [APP_PAUSE] = __dispatch_app_pause,
1098 [APP_PAUSE_BY_PID] = __dispatch_app_process_by_pid,
1099 [APP_GROUP_GET_WINDOW] = __dispatch_app_group_get_window,
1100 [APP_GROUP_SET_WINDOW] = __dispatch_app_group_set_window,
1101 [APP_GROUP_GET_FG] = __dispatch_app_group_get_fg_flag,
1102 [APP_GROUP_GET_LEADER_PID] = __dispatch_app_group_get_leader_pid,
1103 [APP_GROUP_GET_LEADER_PIDS] = __dispatch_app_group_get_leader_pids,
1104 [APP_GROUP_GET_GROUP_PIDS] = __dispatch_app_group_get_group_pids,
1105 [APP_GROUP_GET_IDLE_PIDS] = __dispatch_app_group_get_idle_pids,
1106 [APP_GROUP_LOWER] = __dispatch_app_group_lower,
1107 [APP_GROUP_CLEAR_TOP] = __dispatch_app_group_clear_top,
1108 [APP_GET_STATUS] = __dispatch_app_get_status,
1109 [AMD_RELOAD_APPINFO] = __dispatch_amd_reload_appinfo,
1110 [AGENT_DEAD_SIGNAL] = __dispatch_agent_dead_signal,
1113 static gboolean __request_handler(gpointer data)
1115 GPollFD *gpollfd = (GPollFD *) data;
1116 int fd = gpollfd->fd;
1121 const char *privilege;
1123 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
1128 if (cr.uid >= REGULAR_UID_MIN) {
1129 privilege = __convert_cmd_to_privilege(pkt->cmd);
1131 ret = __check_privilege_by_cynara(clifd, privilege);
1133 _E("request has been denied by smack");
1134 ret = -EILLEGALACCESS;
1135 __real_send(clifd, ret);
1142 if (pkt->cmd >= 0 && pkt->cmd < APP_CMD_MAX && dispatch_table[pkt->cmd]) {
1143 if (dispatch_table[pkt->cmd](clifd, pkt, &cr) != 0)
1144 _E("callback returns FALSE : %d", pkt->cmd);
1146 _E("Invalid packet or not supported command");
1154 static gboolean __au_glib_check(GSource *src)
1159 fd_list = src->poll_fds;
1161 tmp = (GPollFD *) fd_list->data;
1162 if ((tmp->revents & (POLLIN | POLLPRI)))
1164 fd_list = fd_list->next;
1170 static gboolean __au_glib_dispatch(GSource *src, GSourceFunc callback,
1177 static gboolean __au_glib_prepare(GSource *src, gint *timeout)
1182 static GSourceFuncs funcs = {
1183 .prepare = __au_glib_prepare,
1184 .check = __au_glib_check,
1185 .dispatch = __au_glib_dispatch,
1189 int _request_init(void)
1196 __socket_pair_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1198 fd = __create_sock_activation();
1200 _D("Create server socket without socket activation");
1201 fd = __create_server_sock(AUL_UTIL_PID);
1203 _E("Create server socket failed.");
1208 r = cynara_initialize(&r_cynara, NULL);
1209 if (r != CYNARA_API_SUCCESS) {
1210 _E("cynara initialize failed.");
1215 src = g_source_new(&funcs, sizeof(GSource));
1217 _E("out of memory");
1218 cynara_finish(r_cynara);
1223 gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
1225 _E("out of memory");
1226 g_source_destroy(src);
1227 cynara_finish(r_cynara);
1232 gpollfd->events = POLLIN;
1235 g_source_add_poll(src, gpollfd);
1236 g_source_set_callback(src, (GSourceFunc) __request_handler,
1237 (gpointer) gpollfd, NULL);
1238 g_source_set_priority(src, G_PRIORITY_DEFAULT);
1240 r = g_source_attach(src, NULL);
1243 g_source_destroy(src);
1244 cynara_finish(r_cynara);
1249 r = rua_clear_history();
1251 _D("rua_clear_history : %d", r);