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 static data_control_provider_handler_fn __dc_handler = NULL;
60 extern int aul_launch_fini();
62 int aul_is_initialized()
64 return aul_initialized;
67 static int __call_aul_handler(aul_type type, bundle *kb)
70 _aul_handler(type, kb, _aul_data);
74 int app_start(bundle *kb)
76 const char *str = NULL;
78 _app_start_res_prepare(kb);
79 __call_aul_handler(AUL_START, kb);
80 // Handle the DataControl callback
81 str = bundle_get_val(kb, AUL_K_DATA_CONTROL_TYPE);
82 if (str != NULL && strcmp(str, "CORE") == 0)
84 if (__dc_handler != NULL)
86 __dc_handler(kb, 0, NULL); // bundle, request_id, data
92 static int app_resume()
94 __call_aul_handler(AUL_RESUME, NULL);
98 static int app_terminate()
100 __call_aul_handler(AUL_TERMINATE, NULL);
108 * @brief encode kb and send it to 'pid'
109 * @param[in] pid receiver's pid
110 * @param[in] cmd message's status (APP_START | APP_RESULT)
113 SLPAPI int app_agent_send_cmd(int uid, int cmd, bundle *kb)
119 bundle_encode(kb, &kb_data, &datalen);
120 if ((res = __app_agent_send_raw(uid, cmd, kb_data, datalen)) < 0) {
129 res = AUL_R_ETIMEOUT;
131 case -ELOCALLAUNCH_ID:
134 case -EILLEGALACCESS:
138 res = AUL_R_ETERMINATING;
141 res = AUL_R_ENOLAUNCHPAD;
152 SLPAPI int app_agent_send_cmd_with_noreply(int uid, int cmd, bundle *kb)
158 bundle_encode(kb, &kb_data, &datalen);
159 if ((res = __app_send_raw_with_noreply(uid, cmd, kb_data, datalen)) < 0) {
168 res = AUL_R_ETIMEOUT;
170 case -ELOCALLAUNCH_ID:
173 case -EILLEGALACCESS:
194 * @brief encode kb and send it to 'pid'
195 * @param[in] pid receiver's pid
196 * @param[in] cmd message's status (APP_START | APP_RESULT)
199 SLPAPI int app_send_cmd(int pid, int cmd, bundle *kb)
205 bundle_encode(kb, &kb_data, &datalen);
206 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0) {
215 res = AUL_R_ETIMEOUT;
217 case -ELOCALLAUNCH_ID:
220 case -EILLEGALACCESS:
224 res = AUL_R_ETERMINATING;
227 res = AUL_R_ENOLAUNCHPAD;
238 SLPAPI int app_send_cmd_with_noreply(int pid, int cmd, bundle *kb)
244 bundle_encode(kb, &kb_data, &datalen);
245 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0) {
254 res = AUL_R_ETIMEOUT;
256 case -ELOCALLAUNCH_ID:
259 case -EILLEGALACCESS:
271 static void __clear_internal_key(bundle *kb)
273 bundle_del(kb, AUL_K_CALLER_PID);
274 bundle_del(kb, AUL_K_APPID);
275 bundle_del(kb, AUL_K_WAIT_RESULT);
276 bundle_del(kb, AUL_K_SEND_RESULT);
277 bundle_del(kb, AUL_K_ARGV0);
280 static inline void __set_stime(bundle *kb)
283 char tmp[MAX_LOCAL_BUFSZ];
285 gettimeofday(&tv, NULL);
286 snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
287 bundle_add(kb, AUL_K_STARTTIME, tmp);
290 static int __app_start_internal(gpointer data)
294 kb = (bundle *) data;
301 static int __app_launch_local(bundle *b)
303 if (!aul_is_initialized())
304 return AUL_R_ENOINIT;
307 _E("bundle for APP_START is NULL");
309 if (g_idle_add(__app_start_internal, b) > 0)
315 static int __app_resume_local()
317 if (!aul_is_initialized())
318 return AUL_R_ENOINIT;
326 * @brief start caller with kb
327 * @return callee's pid
329 int app_request_to_launchpad(int cmd, const char *appid, bundle *kb)
334 SECURE_LOGD("launch request : %s", appid);
336 kb = bundle_create();
339 __clear_internal_key(kb);
341 bundle_add(kb, AUL_K_APPID, appid);
343 ret = app_send_cmd(AUL_UTIL_PID, cmd, kb);
345 _D("launch request result : %d", ret);
346 if (ret == AUL_R_LOCAL) {
347 _E("app_request_to_launchpad : Same Process Send Local");
354 ret = __app_launch_local(b);
358 case APP_RESUME_BY_PID:
359 ret = __app_resume_local();
362 _E("no support packet");
374 static int __send_result_to_launchpad(int fd, int res)
376 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
377 if (errno == EPIPE) {
378 _E("send failed due to EPIPE.\n");
382 _E("send fail to client");
389 * @brief caller & callee's sock handler
391 int aul_sock_handler(int fd)
402 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
407 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL && cr.uid != 0) {
408 _E("security error");
409 __send_result_to_launchpad(clifd, -1);
414 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL) {
415 ret = __send_result_to_launchpad(clifd, 0);
425 case APP_START: /* run in callee */
427 kbundle = bundle_decode(pkt->data, pkt->len);
431 bundle_free(kbundle);
434 case APP_OPEN: /* run in callee */
436 case APP_RESUME_BY_PID:
440 case APP_TERM_BY_PID: /* run in callee */
444 case APP_TERM_REQ_BY_PID: /* run in callee */
445 app_subapp_terminate_request();
448 case APP_RESULT: /* run in caller */
450 kbundle = bundle_decode(pkt->data, pkt->len);
454 pid_str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
457 app_result(pkt->cmd, kbundle, pid);
458 bundle_free(kbundle);
461 case APP_KEY_EVENT: /* run in caller */
462 kbundle = bundle_decode(pkt->data, pkt->len);
465 app_key_event(kbundle);
466 bundle_free(kbundle);
470 _E("no support packet");
481 int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
487 *kb = bundle_create();
494 if ((argv != NULL) && (argv[0] != NULL)) {
495 buf = strdup(argv[0]);
501 bundle_add(*kb, AUL_K_ARGV0, buf);
503 if (buf) { /*Prevent FIX: ID 38717 */
509 if (ac + 1 == argc) {
510 if (bundle_add(*kb, argv[ac], "") < 0) {
511 _E("bundle add error pos - %d", ac);
512 return AUL_R_ECANCELED;
515 if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
516 _E("bundle add error pos - %d", ac);
517 return AUL_R_ECANCELED;
526 int aul_register_init_callback(
527 int (*aul_handler) (aul_type type, bundle *, void *), void *data)
529 /* Save start handler function in static var */
530 _aul_handler = aul_handler;
537 if (aul_initialized) {
538 //_E("aul already initialized");
539 return AUL_R_ECANCELED;
542 aul_fd = __create_server_sock(getpid());
544 _E("aul_init create sock failed");
552 SLPAPI void aul_finalize()
557 if (aul_initialized) {
565 SLPAPI int aul_launch_app(const char *appid, bundle *kb)
572 ret = app_request_to_launchpad(APP_START, appid, kb);
576 SLPAPI int aul_open_app(const char *appid)
583 ret = app_request_to_launchpad(APP_OPEN, appid, NULL);
587 SLPAPI int aul_resume_app(const char *appid)
594 ret = app_request_to_launchpad(APP_RESUME, appid, NULL);
598 SLPAPI int aul_resume_pid(int pid)
600 char pkgname[MAX_PID_STR_BUFSZ];
606 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
607 ret = app_request_to_launchpad(APP_RESUME_BY_PID, pkgname, NULL);
611 SLPAPI int aul_terminate_pid(int pid)
613 char pkgname[MAX_PID_STR_BUFSZ];
619 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
620 ret = app_request_to_launchpad(APP_TERM_BY_PID, pkgname, NULL);
624 SLPAPI int aul_kill_pid(int pid)
626 char pkgname[MAX_PID_STR_BUFSZ];
632 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
633 ret = app_request_to_launchpad(APP_KILL_BY_PID, pkgname, NULL);
637 SLPAPI int aul_set_data_control_provider_cb(data_control_provider_handler_fn handler)
639 __dc_handler = handler;
643 SLPAPI int aul_unset_data_control_provider_cb(void)
649 /* vi: set ts=8 sts=8 sw=8: */