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.
23 #include <sys/types.h>
37 #include "simple_util.h"
42 static int aul_initialized = 0;
45 static int (*_aul_handler) (aul_type type, bundle *kb, void *data) = NULL;
46 static void *_aul_data;
50 static int __call_aul_handler(aul_type type, bundle *kb);
51 static int app_resume();
52 static int app_terminate();
53 static void __clear_internal_key(bundle *kb);
54 static inline void __set_stime(bundle *kb);
55 static int __app_start_internal(gpointer data);
56 static int __app_launch_local(bundle *b);
57 static int __send_result_to_launchpad(int fd, int res);
59 extern int aul_launch_fini();
61 int aul_is_initialized()
63 return aul_initialized;
66 static int __call_aul_handler(aul_type type, bundle *kb)
69 _aul_handler(type, kb, _aul_data);
73 int app_start(bundle *kb)
75 _app_start_res_prepare(kb);
76 __call_aul_handler(AUL_START, kb);
80 static int app_resume()
82 __call_aul_handler(AUL_RESUME, NULL);
86 static int app_terminate()
88 __call_aul_handler(AUL_TERMINATE, NULL);
96 * @brief encode kb and send it to 'pid'
97 * @param[in] pid receiver's pid
98 * @param[in] cmd message's status (APP_START | APP_RESULT)
101 SLPAPI int app_agent_send_cmd(int uid, int cmd, bundle *kb)
107 bundle_encode(kb, &kb_data, &datalen);
108 if ((res = __app_agent_send_raw(uid, cmd, kb_data, datalen)) < 0) {
117 res = AUL_R_ETIMEOUT;
119 case -ELOCALLAUNCH_ID:
122 case -EILLEGALACCESS:
126 res = AUL_R_ETERMINATING;
129 res = AUL_R_ENOLAUNCHPAD;
140 SLPAPI int app_agent_send_cmd_with_noreply(int uid, int cmd, bundle *kb)
146 bundle_encode(kb, &kb_data, &datalen);
147 if ((res = __app_send_raw_with_noreply(uid, cmd, kb_data, datalen)) < 0) {
156 res = AUL_R_ETIMEOUT;
158 case -ELOCALLAUNCH_ID:
161 case -EILLEGALACCESS:
182 * @brief encode kb and send it to 'pid'
183 * @param[in] pid receiver's pid
184 * @param[in] cmd message's status (APP_START | APP_RESULT)
187 SLPAPI int app_send_cmd(int pid, int cmd, bundle *kb)
193 bundle_encode(kb, &kb_data, &datalen);
194 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0) {
203 res = AUL_R_ETIMEOUT;
205 case -ELOCALLAUNCH_ID:
208 case -EILLEGALACCESS:
212 res = AUL_R_ETERMINATING;
215 res = AUL_R_ENOLAUNCHPAD;
226 SLPAPI int app_send_cmd_with_noreply(int pid, int cmd, bundle *kb)
232 bundle_encode(kb, &kb_data, &datalen);
233 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0) {
242 res = AUL_R_ETIMEOUT;
244 case -ELOCALLAUNCH_ID:
247 case -EILLEGALACCESS:
259 static void __clear_internal_key(bundle *kb)
261 bundle_del(kb, AUL_K_CALLER_PID);
262 bundle_del(kb, AUL_K_PKG_NAME);
263 bundle_del(kb, AUL_K_WAIT_RESULT);
264 bundle_del(kb, AUL_K_SEND_RESULT);
265 bundle_del(kb, AUL_K_ARGV0);
268 static inline void __set_stime(bundle *kb)
271 char tmp[MAX_LOCAL_BUFSZ];
273 gettimeofday(&tv, NULL);
274 snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
275 bundle_add(kb, AUL_K_STARTTIME, tmp);
278 static int __app_start_internal(gpointer data)
282 kb = (bundle *) data;
289 static int __app_launch_local(bundle *b)
291 if (!aul_is_initialized())
292 return AUL_R_ENOINIT;
295 _E("bundle for APP_START is NULL");
297 if (g_idle_add(__app_start_internal, b) > 0)
303 static int __app_resume_local()
305 if (!aul_is_initialized())
306 return AUL_R_ENOINIT;
314 * @brief start caller with kb
315 * @return callee's pid
317 int app_request_to_launchpad(int cmd, const char *pkgname, bundle *kb)
322 SECURE_LOGD("launch request : %s", pkgname);
324 kb = bundle_create();
327 __clear_internal_key(kb);
329 bundle_add(kb, AUL_K_PKG_NAME, pkgname);
331 ret = app_send_cmd(AUL_UTIL_PID, cmd, kb);
333 _D("launch request result : %d", ret);
334 if (ret == AUL_R_LOCAL) {
335 _E("app_request_to_launchpad : Same Process Send Local");
342 ret = __app_launch_local(b);
346 case APP_RESUME_BY_PID:
347 ret = __app_resume_local();
350 _E("no support packet");
362 static int __send_result_to_launchpad(int fd, int res)
364 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
365 if (errno == EPIPE) {
366 _E("send failed due to EPIPE.\n");
370 _E("send fail to client");
377 * @brief caller & callee's sock handler
379 int aul_sock_handler(int fd)
390 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
395 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL && cr.uid != 0) {
396 _E("security error");
397 __send_result_to_launchpad(clifd, -1);
402 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL) {
403 ret = __send_result_to_launchpad(clifd, 0);
413 case APP_START: /* run in callee */
415 kbundle = bundle_decode(pkt->data, pkt->len);
419 bundle_free(kbundle);
422 case APP_OPEN: /* run in callee */
424 case APP_RESUME_BY_PID:
428 case APP_TERM_BY_PID: /* run in callee */
432 case APP_TERM_REQ_BY_PID: /* run in callee */
433 app_subapp_terminate_request();
436 case APP_RESULT: /* run in caller */
438 kbundle = bundle_decode(pkt->data, pkt->len);
442 pid_str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
445 app_result(pkt->cmd, kbundle, pid);
446 bundle_free(kbundle);
449 case APP_KEY_EVENT: /* run in caller */
450 kbundle = bundle_decode(pkt->data, pkt->len);
453 app_key_event(kbundle);
454 bundle_free(kbundle);
458 _E("no support packet");
469 int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
475 *kb = bundle_create();
482 if ((argv != NULL) && (argv[0] != NULL)) {
483 buf = strdup(argv[0]);
489 bundle_add(*kb, AUL_K_ARGV0, buf);
491 if (buf) { /*Prevent FIX: ID 38717 */
497 if (ac + 1 == argc) {
498 if (bundle_add(*kb, argv[ac], "") < 0) {
499 _E("bundle add error pos - %d", ac);
500 return AUL_R_ECANCELED;
503 if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
504 _E("bundle add error pos - %d", ac);
505 return AUL_R_ECANCELED;
514 int aul_register_init_callback(
515 int (*aul_handler) (aul_type type, bundle *, void *), void *data)
517 /* Save start handler function in static var */
518 _aul_handler = aul_handler;
525 if (aul_initialized) {
526 //_E("aul already initialized");
527 return AUL_R_ECANCELED;
530 aul_fd = __create_server_sock(getpid());
532 _E("aul_init create sock failed");
540 SLPAPI void aul_finalize()
545 if (aul_initialized) {
553 SLPAPI int aul_launch_app(const char *appid, bundle *kb)
560 ret = app_request_to_launchpad(APP_START, appid, kb);
564 SLPAPI int aul_open_app(const char *appid)
571 ret = app_request_to_launchpad(APP_OPEN, appid, NULL);
575 SLPAPI int aul_resume_app(const char *appid)
582 ret = app_request_to_launchpad(APP_RESUME, appid, NULL);
586 SLPAPI int aul_resume_pid(int pid)
588 char pkgname[MAX_PID_STR_BUFSZ];
594 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
595 ret = app_request_to_launchpad(APP_RESUME_BY_PID, pkgname, NULL);
599 SLPAPI int aul_terminate_pid(int pid)
601 char pkgname[MAX_PID_STR_BUFSZ];
607 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
608 ret = app_request_to_launchpad(APP_TERM_BY_PID, pkgname, NULL);
612 SLPAPI int aul_kill_pid(int pid)
614 char pkgname[MAX_PID_STR_BUFSZ];
620 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
621 ret = app_request_to_launchpad(APP_KILL_BY_PID, pkgname, NULL);
625 /* vi: set ts=8 sts=8 sw=8: */