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>
31 #ifdef _APPFW_FEATURE_APP_CONTROL_LITE
32 #include <pkgmgr-info.h>
39 #include "simple_util.h"
44 static int aul_initialized = 0;
47 static int (*_aul_handler) (aul_type type, bundle *kb, void *data) = NULL;
48 static void *_aul_data;
52 static int __call_aul_handler(aul_type type, bundle *kb);
53 static int app_resume();
54 static int app_terminate();
55 static void __clear_internal_key(bundle *kb);
56 static inline void __set_stime(bundle *kb);
57 static int __app_start_internal(gpointer data);
58 static int __app_launch_local(bundle *b);
59 static int __send_result_to_launchpad(int fd, int res);
61 #ifdef _APPFW_FEATURE_PROCESS_POOL
62 static void *__window_object = NULL;
63 static void *__bg_object = NULL;
64 static void *__conformant_object = NULL;
67 #ifdef _APPFW_FEATURE_DATA_CONTROL
68 static data_control_provider_handler_fn __dc_handler = NULL;
71 extern int aul_launch_fini();
73 int aul_is_initialized()
75 return aul_initialized;
78 static int __call_aul_handler(aul_type type, bundle *kb)
81 _aul_handler(type, kb, _aul_data);
85 int app_start(bundle *kb)
87 const char *str = NULL;
89 _app_start_res_prepare(kb);
90 __call_aul_handler(AUL_START, kb);
92 #ifdef _APPFW_FEATURE_DATA_CONTROL
93 // Handle the DataControl callback
94 str = bundle_get_val(kb, AUL_K_DATA_CONTROL_TYPE);
95 if (str != NULL && strcmp(str, "CORE") == 0)
97 if (__dc_handler != NULL)
99 __dc_handler(kb, 0, NULL); // bundle, request_id, data
106 static int app_resume()
108 __call_aul_handler(AUL_RESUME, NULL);
112 static int app_terminate()
114 __call_aul_handler(AUL_TERMINATE, NULL);
118 #ifdef _APPFW_FEATURE_VISIBILITY_CHECK_BY_LCD_STATUS
119 static int app_resume_lcd_on()
121 __call_aul_handler(AUL_RESUME_LCD_ON, NULL);
125 static int app_pause_lcd_off()
127 __call_aul_handler(AUL_PAUSE_LCD_OFF, NULL);
133 * @brief encode kb and send it to 'pid'
134 * @param[in] pid receiver's pid
135 * @param[in] cmd message's status (APP_START | APP_RESULT)
138 SLPAPI int app_send_cmd(int pid, int cmd, bundle *kb)
144 bundle_encode(kb, &kb_data, &datalen);
145 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0) {
154 res = AUL_R_ETIMEOUT;
156 case -ELOCALLAUNCH_ID:
159 case -EILLEGALACCESS:
163 res = AUL_R_ETERMINATING;
166 res = AUL_R_ENOLAUNCHPAD;
168 #ifdef _APPFW_FEATURE_APP_CONTROL_LITE
169 case -EUGLOCAL_LAUNCH:
170 res = AUL_R_UG_LOCAL;
174 res = AUL_R_EREJECTED;
185 SLPAPI int app_send_cmd_with_noreply(int pid, int cmd, bundle *kb)
191 bundle_encode(kb, &kb_data, &datalen);
192 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0) {
201 res = AUL_R_ETIMEOUT;
203 case -ELOCALLAUNCH_ID:
206 case -EILLEGALACCESS:
218 static void __clear_internal_key(bundle *kb)
220 bundle_del(kb, AUL_K_CALLER_PID);
221 bundle_del(kb, AUL_K_PKG_NAME);
222 bundle_del(kb, AUL_K_WAIT_RESULT);
223 bundle_del(kb, AUL_K_SEND_RESULT);
224 bundle_del(kb, AUL_K_ARGV0);
227 static inline void __set_stime(bundle *kb)
230 char tmp[MAX_LOCAL_BUFSZ];
232 gettimeofday(&tv, NULL);
233 snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
234 bundle_add(kb, AUL_K_STARTTIME, tmp);
237 static int __app_start_internal(gpointer data)
241 kb = (bundle *) data;
248 static int __app_launch_local(bundle *b)
250 if (!aul_is_initialized())
251 return AUL_R_ENOINIT;
254 _E("bundle for APP_START is NULL");
256 if (g_idle_add(__app_start_internal, b) > 0)
262 static int __app_resume_local()
264 if (!aul_is_initialized())
265 return AUL_R_ENOINIT;
273 * @brief start caller with kb
274 * @return callee's pid
276 int app_request_to_launchpad(int cmd, const char *pkgname, bundle *kb)
281 SECURE_LOGD("launch request : %s", pkgname);
283 kb = bundle_create();
286 __clear_internal_key(kb);
288 bundle_add(kb, AUL_K_PKG_NAME, pkgname);
290 if(cmd == APP_START_ASYNC)
291 ret = app_send_cmd_with_noreply(AUL_UTIL_PID, cmd, kb);
293 ret = app_send_cmd(AUL_UTIL_PID, cmd, kb);
295 _D("launch request result : %d", ret);
296 if (ret == AUL_R_LOCAL
297 #ifdef _APPFW_FEATURE_APP_CONTROL_LITE
298 || ret == AUL_R_UG_LOCAL
301 _E("app_request_to_launchpad : Same Process Send Local");
304 #ifdef _APPFW_FEATURE_APP_CONTROL_LITE
305 if(ret == AUL_R_UG_LOCAL) {
306 pkgmgrinfo_appinfo_h handle;
309 pkgmgrinfo_appinfo_get_appinfo(pkgname, &handle);
310 pkgmgrinfo_appinfo_get_exec(handle, &exec);
312 bundle_add(kb, "__AUL_UG_EXEC__", exec);
314 pkgmgrinfo_appinfo_destroy_appinfo(handle);
321 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
322 case APP_START_MULTI_INSTANCE:
325 ret = __app_launch_local(b);
329 case APP_RESUME_BY_PID:
330 ret = __app_resume_local();
333 _E("no support packet");
345 static int __send_result_to_launchpad(int fd, int res)
347 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
348 if (errno == EPIPE) {
349 _E("send failed due to EPIPE.\n");
353 _E("send fail to client");
360 * @brief caller & callee's sock handler
362 int aul_sock_handler(int fd)
369 const char *str = NULL;
373 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
378 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL && cr.uid != 0) {
379 _E("security error");
380 __send_result_to_launchpad(clifd, -1);
385 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL && pkt->cmd != APP_KEY_EVENT && pkt->cmd != APP_TERM_BY_PID_ASYNC
386 #ifdef _APPFW_FEATURE_VISIBILITY_CHECK_BY_LCD_STATUS
387 && pkt->cmd != APP_PAUSE_LCD_OFF && pkt->cmd != APP_RESUME_LCD_ON
390 ret = __send_result_to_launchpad(clifd, 0);
400 case APP_START: /* run in callee */
402 case APP_START_ASYNC:
403 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
404 case APP_START_MULTI_INSTANCE:
406 kbundle = bundle_decode(pkt->data, pkt->len);
410 bundle_free(kbundle);
413 case APP_OPEN: /* run in callee */
415 case APP_RESUME_BY_PID:
419 case APP_TERM_BY_PID: /* run in callee */
420 case APP_TERM_BY_PID_ASYNC:
424 case APP_TERM_REQ_BY_PID: /* run in callee */
425 app_subapp_terminate_request();
428 case APP_RESULT: /* run in caller */
430 kbundle = bundle_decode(pkt->data, pkt->len);
434 str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
437 app_result(pkt->cmd, kbundle, pid);
439 bundle_free(kbundle);
442 case APP_KEY_EVENT: /* run in caller */
443 kbundle = bundle_decode(pkt->data, pkt->len);
446 app_key_event(kbundle);
447 bundle_free(kbundle);
450 #ifdef _APPFW_FEATURE_VISIBILITY_CHECK_BY_LCD_STATUS
451 case APP_PAUSE_LCD_OFF:
455 case APP_RESUME_LCD_ON:
460 _E("no support packet");
471 int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
477 *kb = bundle_create();
484 if ((argv != NULL) && (argv[0] != NULL)) {
485 buf = strdup(argv[0]);
491 bundle_add(*kb, AUL_K_ARGV0, buf);
493 if (buf) { /*Prevent FIX: ID 38717 */
499 if (ac + 1 == argc) {
500 if (bundle_add(*kb, argv[ac], "") < 0) {
501 _E("bundle add error pos - %d", ac);
502 return AUL_R_ECANCELED;
505 if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
506 _E("bundle add error pos - %d", ac);
507 return AUL_R_ECANCELED;
516 int aul_register_init_callback(
517 int (*aul_handler) (aul_type type, bundle *, void *), void *data)
519 /* Save start handler function in static var */
520 _aul_handler = aul_handler;
527 if (aul_initialized) {
528 //_E("aul already initialized");
529 return AUL_R_ECANCELED;
532 aul_fd = __create_server_sock(getpid());
534 _E("aul_init create sock failed");
542 SLPAPI void aul_finalize()
547 if (aul_initialized) {
555 SLPAPI int aul_launch_app(const char *appid, bundle *kb)
560 return app_request_to_launchpad(APP_START, appid, kb);
563 SLPAPI int aul_launch_app_async(const char *appid, bundle *kb)
568 return app_request_to_launchpad(APP_START_ASYNC, appid, kb);
571 #ifdef _APPFW_FEATURE_MULTI_INSTANCE
572 SLPAPI int aul_launch_app_for_multi_instance(const char *appid, bundle *kb)
577 return app_request_to_launchpad(APP_START_MULTI_INSTANCE, appid, kb);
581 SLPAPI int aul_open_app(const char *appid)
586 return app_request_to_launchpad(APP_OPEN, appid, NULL);
589 SLPAPI int aul_resume_app(const char *appid)
594 return app_request_to_launchpad(APP_RESUME, appid, NULL);
597 SLPAPI int aul_resume_pid(int pid)
599 char pkgname[MAX_PID_STR_BUFSZ];
605 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
606 ret = app_request_to_launchpad(APP_RESUME_BY_PID, pkgname, NULL);
610 SLPAPI int aul_terminate_pid(int pid)
612 char pkgname[MAX_PID_STR_BUFSZ];
618 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
619 ret = app_request_to_launchpad(APP_TERM_BY_PID, pkgname, NULL);
623 SLPAPI int aul_terminate_pid_without_restart(int pid)
625 char pkgname[MAX_PID_STR_BUFSZ];
631 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
632 ret = app_request_to_launchpad(APP_TERM_BY_PID_WITHOUT_RESTART, pkgname, NULL);
636 SLPAPI int aul_terminate_pid_async(int pid)
638 char pkgname[MAX_PID_STR_BUFSZ];
644 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
645 ret = app_request_to_launchpad(APP_TERM_BY_PID_ASYNC, pkgname, NULL);
649 SLPAPI int aul_kill_pid(int pid)
651 char pkgname[MAX_PID_STR_BUFSZ];
657 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
658 ret = app_request_to_launchpad(APP_KILL_BY_PID, pkgname, NULL);
662 #ifdef _APPFW_FEATURE_PROCESS_POOL
663 SLPAPI void aul_set_preinit_window(void *evas_object)
665 __window_object = evas_object;
668 SLPAPI void* aul_get_preinit_window(const char *win_name)
670 return __window_object;
673 SLPAPI void aul_set_preinit_background(void *evas_object)
675 __bg_object = evas_object;
678 SLPAPI void* aul_get_preinit_background(void)
683 SLPAPI void aul_set_preinit_conformant(void *evas_object)
685 __conformant_object = evas_object;
688 SLPAPI void* aul_get_preinit_conformant(void)
690 return __conformant_object;
694 #ifdef _APPFW_FEATURE_DATA_CONTROL
695 SLPAPI int aul_set_data_control_provider_cb(data_control_provider_handler_fn handler)
697 __dc_handler = handler;
701 SLPAPI int aul_unset_data_control_provider_cb(void)
708 /* vi: set ts=8 sts=8 sw=8: */