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>
34 #ifdef WEARABLE_PROFILE
35 #include <proc_stat.h>
37 #include <security-server.h>
39 #include "amd_config.h"
40 #include "simple_util.h"
43 #include "amd_request.h"
45 #include "amd_launch.h"
46 #include "amd_appinfo.h"
47 #include "amd_cgutil.h"
48 #include "amd_status.h"
51 #define INHOUSE_UID 5000
53 struct appinfomgr *_raf;
56 static int __send_result_to_client(int fd, int res);
57 static gboolean __request_handler(gpointer data);
59 static int __send_result_to_client(int fd, int res)
61 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
63 _E("send failed due to EPIPE.\n");
64 _E("send fail to client");
70 static void __real_send(int clifd, int ret)
72 if (send(clifd, &ret, sizeof(int), MSG_NOSIGNAL) < 0) {
74 _E("send failed due to EPIPE.\n");
76 _E("send fail to client");
82 static int __get_caller_pid(bundle *kb)
87 pid_str = bundle_get_val(kb, AUL_K_ORG_CALLER_PID);
91 pid_str = bundle_get_val(kb, AUL_K_CALLER_PID);
103 static int __foward_cmd(int cmd, bundle *kb, int cr_pid)
107 char tmp_pid[MAX_PID_STR_BUFSZ];
112 if ((pid = __get_caller_pid(kb)) < 0)
115 pgid = getpgid(cr_pid);
117 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", pgid);
118 bundle_del(kb,AUL_K_CALLEE_PID);
119 bundle_add(kb, AUL_K_CALLEE_PID, tmp_pid);
122 bundle_encode(kb, &kb_data, &datalen);
123 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
131 static int __app_process_by_pid(int cmd,
132 const char *pkg_name, struct ucred *cr, int clifd)
138 if (pkg_name == NULL)
141 if ((cr->uid != 0) && (cr->uid != INHOUSE_UID)) {
142 _E("reject by security rule, your uid is %u\n", cr->uid);
146 pid = atoi(pkg_name);
151 #ifdef WEARABLE_PROFILE
152 if (cmd == APP_RESUME_BY_PID)
153 proc_group_change_status(PROC_CGROUP_SET_RESUME_REQUEST, pid, NULL);
155 proc_group_change_status(PROC_CGROUP_SET_TERMINATE_REQUEST, pid, NULL);
159 case APP_RESUME_BY_PID:
160 ret = _resume_app(pid);
161 __real_send(clifd, ret);
163 case APP_TERM_BY_PID:
164 ret = _term_app(pid, clifd);
166 case APP_KILL_BY_PID:
167 if ((ret = _send_to_sigkill(pid)) < 0)
168 _E("fail to killing - %d\n", pid);
169 __real_send(clifd, ret);
171 case APP_TERM_REQ_BY_PID:
172 if ((ret = __app_send_raw(pid, APP_TERM_REQ_BY_PID, (unsigned char *)&dummy, sizeof(int))) < 0) {
173 _D("terminate req packet send error");
175 __real_send(clifd, ret);
181 static gboolean __add_history_handler(gpointer user_data)
187 char *app_path = NULL;
189 app_pkt_t *pkt = (app_pkt_t *)user_data;
194 kb = bundle_decode(pkt->data, pkt->len);
195 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
197 ai = (struct appinfo *)appinfo_find(_raf, appid);
198 app_path = (char *)appinfo_get_value(ai, AIT_EXEC);
200 memset((void *)&rec, 0, sizeof(rec));
202 rec.pkg_name = appid;
203 rec.app_path = app_path;
206 rec.arg = (char *)pkt->data;
209 SECURE_LOGD("add rua history %s %s", rec.pkg_name, rec.app_path);
211 ret = rua_add_history(&rec);
213 _D("rua add history error");
222 static int __get_pid_cb(void *user_data, const char *group, pid_t pid)
226 _D("%s: %d : %d", *sz, pid);
227 *sz = 1; /* 1 is enough */
229 return -1; /* stop the iteration */
232 static int __releasable(const char *filename)
237 if (!filename || !*filename) {
238 _E("release service: name is empty");
242 r = cgutil_exist_group(_rcg, CTRL_MGR, filename);
244 SECURE_LOGE("release service: exist: %s", strerror(errno));
248 SECURE_LOGE("release service: '%s' already not exist", filename);
253 r = cgutil_group_foreach_pid(_rcg, CTRL_MGR, filename,
256 SECURE_LOGE("release service: '%s' read pid error", filename);
260 SECURE_LOGE("release service: '%s' group has process", filename);
267 int __release_srv(const char *filename)
270 const struct appinfo *ai;
272 r = __releasable(filename);
276 ai = (struct appinfo *)appinfo_find(_raf, filename);
278 SECURE_LOGE("release service: '%s' not found", filename);
282 r = appinfo_get_boolean(ai, AIT_RESTART);
284 SECURE_LOGD("Auto restart set: '%s'", filename);
285 return _start_srv(ai, NULL);
288 service_release(filename);
290 r = cgutil_remove_group(_rcg, CTRL_MGR, filename);
292 SECURE_LOGE("'%s' group remove error: %s", filename, strerror(errno));
299 static gboolean __request_handler(gpointer data)
301 GPollFD *gpollfd = (GPollFD *) data;
302 int fd = gpollfd->fd;
314 const struct appinfo *ai;
315 const char *pkg_status;
317 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
327 case APP_START_ASYNC:
328 ret = security_server_check_privilege_by_sockfd(clifd, "aul::launch", "x");
329 if(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
330 _E("launch request has been denied by smack");
331 ret = -EILLEGALACCESS;
332 __real_send(clifd, ret);
334 kb = bundle_decode(pkt->data, pkt->len);
335 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
336 ret = _start_app(appid, kb, pkt->cmd, cr.pid, cr.uid, clifd);
340 g_timeout_add(1000, __add_history_handler, pkt);
344 bundle_free(kb), kb = NULL;
349 kb = bundle_decode(pkt->data, pkt->len);
350 ret = __foward_cmd(pkt->cmd, kb, cr.pid);
351 //__real_send(clifd, ret);
354 case APP_RESUME_BY_PID:
355 case APP_TERM_REQ_BY_PID:
356 kb = bundle_decode(pkt->data, pkt->len);
357 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
358 ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
360 case APP_TERM_BY_PID:
361 case APP_KILL_BY_PID:
362 ret = security_server_check_privilege_by_sockfd(clifd, "aul::terminate", "x");
363 if(ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
364 _E("terminate request has been denied by smack");
365 ret = -EILLEGALACCESS;
366 __real_send(clifd, ret);
368 kb = bundle_decode(pkt->data, pkt->len);
369 appid = (char *)bundle_get_val(kb, AUL_K_PKG_NAME);
370 ret = __app_process_by_pid(pkt->cmd, appid, &cr, clifd);
373 case APP_RUNNING_INFO:
374 _status_send_running_appinfo_v2(clifd);
377 appid = malloc(MAX_PACKAGE_STR_SIZE);
378 strncpy(appid, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
379 ret = _status_app_is_running_v2(appid);
380 SECURE_LOGD("APP_IS_RUNNING : %s : %d",appid, ret);
381 __send_result_to_client(clifd, ret);
384 case APP_GET_APPID_BYPID:
385 memcpy(&pid, pkt->data, pkt->len);
386 ret = _status_get_appid_bypid(clifd, pid);
387 _D("APP_GET_APPID_BYPID : %d : %d", pid, ret);
389 case APP_KEY_RESERVE:
390 ret = _register_key_event(cr.pid);
391 __send_result_to_client(clifd, ret);
393 case APP_KEY_RELEASE:
394 ret = _unregister_key_event(cr.pid);
395 __send_result_to_client(clifd, ret);
397 case APP_STATUS_UPDATE:
398 status = (int *)pkt->data;
399 if(*status == STATUS_NORESTART) {
400 appid = _status_app_get_appid_bypid(cr.pid);
401 ai = appinfo_find(_raf, appid);
402 appinfo_set_value(ai, AIT_STATUS, "norestart");
404 ret = _status_update_app_info_list(cr.pid, *status);
406 //__send_result_to_client(clifd, ret);
410 appid = malloc(MAX_PACKAGE_STR_SIZE);
411 strncpy(appid, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
412 ai = appinfo_find(_raf, appid);
413 pkg_status = appinfo_get_value(ai, AIT_STATUS);
414 _D("pkg_status : %s", pkg_status);
415 if(pkg_status && strncmp(pkg_status, "upgrading", 9) == 0) {
416 appinfo_set_value(ai, AIT_STATUS, "restart");
417 } else if (pkg_status && strncmp(pkg_status, "norestart", 9) == 0) {
418 appinfo_set_value(ai, AIT_STATUS, "installed");
420 ret = __release_srv(appid);
422 __send_result_to_client(clifd, ret);
425 case APP_RUNNING_LIST_UPDATE:
426 /*kb = bundle_decode(pkt->data, pkt->len);
427 appid = (char *)bundle_get_val(kb, AUL_K_APPID);
428 app_path = (char *)bundle_get_val(kb, AUL_K_EXEC);
429 tmp_pid = (char *)bundle_get_val(kb, AUL_K_PID);
431 ret = _status_add_app_info_list(appid, app_path, pid);*/
433 __send_result_to_client(clifd, ret);
436 _E("no support packet");
444 bundle_free(kb), kb = NULL;
449 static gboolean __au_glib_check(GSource *src)
454 fd_list = src->poll_fds;
456 tmp = (GPollFD *) fd_list->data;
457 if ((tmp->revents & (POLLIN | POLLPRI)))
459 fd_list = fd_list->next;
465 static gboolean __au_glib_dispatch(GSource *src, GSourceFunc callback,
472 static gboolean __au_glib_prepare(GSource *src, gint *timeout)
477 static GSourceFuncs funcs = {
478 .prepare = __au_glib_prepare,
479 .check = __au_glib_check,
480 .dispatch = __au_glib_dispatch,
484 int _requset_init(struct amdmgr *amd)
491 fd = __create_server_sock(AUL_UTIL_PID);
492 src = g_source_new(&funcs, sizeof(GSource));
494 gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
495 gpollfd->events = POLLIN;
498 g_source_add_poll(src, gpollfd);
499 g_source_set_callback(src, (GSourceFunc) __request_handler,
500 (gpointer) gpollfd, NULL);
501 g_source_set_priority(src, G_PRIORITY_DEFAULT);
503 r = g_source_attach(src, NULL);
506 /* TODO: error handle*/
514 r = rua_clear_history();
516 _D("rua_clear_history : %d", r);