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;
44 static void *__window_object = NULL;
45 static void *__bg_object = NULL;
46 static void *__conformant_object = NULL;
48 static int (*_aul_handler) (aul_type type, bundle *kb, void *data) = NULL;
49 static void *_aul_data;
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 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);
104 static int bgapp_terminate(void)
106 __call_aul_handler(AUL_TERMINATE_BGAPP, NULL);
110 static int app_pause(void)
112 __call_aul_handler(AUL_PAUSE, NULL);
116 static int __get_aul_error(int res)
122 ret = AUL_R_EREJECTED;
128 ret = AUL_R_ENOLAUNCHPAD;
131 ret = AUL_R_ETERMINATING;
133 case -EILLEGALACCESS:
136 case -ELOCALLAUNCH_ID:
140 ret = AUL_R_ETIMEOUT;
155 static int __app_send_cmd_with_fd(int pid, int uid, int cmd, bundle *kb, int *ret_fd)
158 bundle_raw *kb_data = NULL;
161 res = bundle_encode(kb, &kb_data, &datalen);
162 if (res != BUNDLE_ERROR_NONE) {
165 if ((res = __app_send_raw_with_fd_reply(pid, uid, cmd, kb_data, datalen, ret_fd)) < 0) {
174 res = AUL_R_ETIMEOUT;
176 case -ELOCALLAUNCH_ID:
179 case -EILLEGALACCESS:
183 res = AUL_R_ETERMINATING;
186 res = AUL_R_ENOLAUNCHPAD;
188 #ifdef _APPFW_FEATURE_APP_CONTROL_LITE
189 case -EUGLOCAL_LAUNCH:
190 res = AUL_R_UG_LOCAL;
194 res = AUL_R_EREJECTED;
206 * @brief encode kb and send it to 'pid'
207 * @param[in] pid receiver's pid
208 * @param[in] cmd message's status (APP_START | APP_RESULT)
211 SLPAPI int app_agent_send_cmd(int uid, int cmd, bundle *kb)
217 bundle_encode(kb, &kb_data, &datalen);
218 if ((res = __app_agent_send_raw(uid, cmd, kb_data, datalen)) < 0)
219 res = __get_aul_error(res);
225 SLPAPI int app_agent_send_cmd_with_noreply(int uid, int cmd, bundle *kb)
231 bundle_encode(kb, &kb_data, &datalen);
232 if ((res = __app_send_raw_with_noreply(uid, cmd, kb_data, datalen)) < 0)
233 res = __get_aul_error(res);
240 * @brief encode kb and send it to 'pid'
241 * @param[in] pid receiver's pid
242 * @param[in] cmd message's status (APP_START | APP_RESULT)
245 SLPAPI int app_send_cmd(int pid, int cmd, bundle *kb)
247 return app_send_cmd_for_uid(pid, getuid(), cmd, kb);
250 SLPAPI int app_send_cmd_for_uid(int pid, uid_t uid, int cmd, bundle *kb)
256 bundle_encode(kb, &kb_data, &datalen);
257 if ((res = __app_send_raw_for_uid(pid, uid, cmd, kb_data, datalen)) < 0)
258 res = __get_aul_error(res);
264 SLPAPI int app_send_cmd_with_noreply(int pid, int cmd, bundle *kb)
270 bundle_encode(kb, &kb_data, &datalen);
271 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0)
272 res = __get_aul_error(res);
278 static void __clear_internal_key(bundle *kb)
280 bundle_del(kb, AUL_K_CALLER_PID);
281 bundle_del(kb, AUL_K_APPID);
282 bundle_del(kb, AUL_K_WAIT_RESULT);
283 bundle_del(kb, AUL_K_SEND_RESULT);
284 bundle_del(kb, AUL_K_ARGV0);
287 static inline void __set_stime(bundle *kb)
290 char tmp[MAX_LOCAL_BUFSZ];
292 gettimeofday(&tv, NULL);
293 snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
294 bundle_add(kb, AUL_K_STARTTIME, tmp);
297 static int __app_start_internal(gpointer data)
301 kb = (bundle *) data;
308 static int __app_launch_local(bundle *b)
310 if (!aul_is_initialized())
311 return AUL_R_ENOINIT;
314 _E("bundle for APP_START is NULL");
316 if (g_idle_add(__app_start_internal, b) > 0)
322 static int __app_resume_local()
324 if (!aul_is_initialized())
325 return AUL_R_ENOINIT;
332 int app_request_to_launchpad_with_fd(int cmd, const char *appid, bundle *kb, int *fd, int uid)
337 SECURE_LOGD("launch request : %s", appid);
339 kb = bundle_create();
342 __clear_internal_key(kb);
344 ret = __app_send_cmd_with_fd(AUL_UTIL_PID, uid, cmd, kb, fd);
346 _D("launch request result : %d", ret);
347 if (ret == AUL_R_LOCAL) {
348 _E("app_request_to_launchpad : Same Process Send Local");
355 ret = __app_launch_local(b);
359 case APP_RESUME_BY_PID:
360 ret = __app_resume_local();
363 _E("no support packet");
377 * @brief start caller with kb
378 * @return callee's pid
380 int app_request_to_launchpad(int cmd, const char *appid, bundle *kb)
382 return app_request_to_launchpad_for_uid(cmd, appid, kb, getuid());
385 int app_request_to_launchpad_for_uid(int cmd, const char *appid, bundle *kb, uid_t uid)
390 SECURE_LOGD("launch request : %s", appid);
392 kb = bundle_create();
395 __clear_internal_key(kb);
397 bundle_add(kb, AUL_K_APPID, appid);
399 ret = app_send_cmd_for_uid(AUL_UTIL_PID, uid, cmd, kb);
401 _D("launch request result : %d", ret);
402 if (ret == AUL_R_LOCAL) {
403 _E("app_request_to_launchpad : Same Process Send Local");
410 ret = __app_launch_local(b);
414 case APP_RESUME_BY_PID:
415 ret = __app_resume_local();
418 _E("no support packet");
430 static int __send_result_to_launchpad(int fd, int res)
432 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
433 if (errno == EPIPE) {
434 _E("send failed due to EPIPE.\n");
438 _E("send fail to client");
445 * @brief caller & callee's sock handler
447 int aul_sock_handler(int fd)
458 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
463 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL && pkt->cmd != APP_TERM_BY_PID_ASYNC) {
464 ret = __send_result_to_launchpad(clifd, 0);
474 case APP_START: /* run in callee */
476 kbundle = bundle_decode(pkt->data, pkt->len);
480 bundle_free(kbundle);
483 case APP_OPEN: /* run in callee */
485 case APP_RESUME_BY_PID:
489 case APP_TERM_BY_PID: /* run in callee */
490 case APP_TERM_BY_PID_ASYNC:
494 case APP_TERM_BGAPP_BY_PID:
498 case APP_TERM_REQ_BY_PID: /* run in callee */
499 app_subapp_terminate_request();
502 case APP_RESULT: /* run in caller */
504 kbundle = bundle_decode(pkt->data, pkt->len);
508 pid_str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
511 app_result(pkt->cmd, kbundle, pid);
512 bundle_free(kbundle);
515 case APP_KEY_EVENT: /* run in caller */
516 kbundle = bundle_decode(pkt->data, pkt->len);
519 app_key_event(kbundle);
520 bundle_free(kbundle);
523 case APP_PAUSE_BY_PID:
528 _E("no support packet");
539 int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
545 *kb = bundle_create();
552 if ((argv != NULL) && (argv[0] != NULL)) {
553 buf = strdup(argv[0]);
559 bundle_add(*kb, AUL_K_ARGV0, buf);
561 if (buf) { /*Prevent FIX: ID 38717 */
567 if (ac + 1 == argc) {
568 if (bundle_add(*kb, argv[ac], "") < 0) {
569 _E("bundle add error pos - %d", ac);
570 return AUL_R_ECANCELED;
573 if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
574 _E("bundle add error pos - %d", ac);
575 return AUL_R_ECANCELED;
584 int aul_register_init_callback(
585 int (*aul_handler) (aul_type type, bundle *, void *), void *data)
587 /* Save start handler function in static var */
588 _aul_handler = aul_handler;
595 if (aul_initialized) {
596 //_E("aul already initialized");
597 return AUL_R_ECANCELED;
600 aul_fd = __create_server_sock(getpid());
602 _E("aul_init create sock failed");
610 SLPAPI void aul_finalize()
615 if (aul_initialized) {
622 SLPAPI int aul_request_data_control_socket_pair(bundle *kb, int *fd)
624 return app_request_to_launchpad_with_fd(APP_GET_SOCKET_PAIR, NULL, kb, fd, getuid());
627 SLPAPI int aul_launch_app(const char *appid, bundle *kb)
634 ret = app_request_to_launchpad(APP_START, appid, kb);
638 SLPAPI int aul_launch_app_for_uid(const char *appid, bundle *kb, uid_t uid)
641 char buf[MAX_PID_STR_BUFSZ];
644 snprintf(buf, MAX_UID_STR_BUFSZ, "%d", uid);
645 bundle_add(kb, AUL_K_TARGET_UID, buf);
647 ret = app_request_to_launchpad_for_uid(APP_START, appid, kb, uid);
651 SLPAPI int aul_open_app(const char *appid)
658 ret = app_request_to_launchpad(APP_OPEN, appid, NULL);
662 SLPAPI int aul_resume_app(const char *appid)
669 ret = app_request_to_launchpad(APP_RESUME, appid, NULL);
673 SLPAPI int aul_resume_pid(int pid)
675 char pkgname[MAX_PID_STR_BUFSZ];
681 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
682 ret = app_request_to_launchpad(APP_RESUME_BY_PID, pkgname, NULL);
686 SLPAPI int aul_terminate_pid(int pid)
688 char pkgname[MAX_PID_STR_BUFSZ];
694 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
695 ret = app_request_to_launchpad(APP_TERM_BY_PID, pkgname, NULL);
699 SLPAPI int aul_terminate_bgapp_pid(int pid)
701 char pkgname[MAX_PID_STR_BUFSZ];
707 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
708 ret = app_request_to_launchpad(APP_TERM_BGAPP_BY_PID, pkgname, NULL);
712 SLPAPI int aul_terminate_pid_without_restart(int pid)
714 char pkgname[MAX_PID_STR_BUFSZ];
720 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
721 ret = app_request_to_launchpad(APP_TERM_BY_PID_WITHOUT_RESTART, pkgname, NULL);
725 SLPAPI int aul_terminate_pid_async(int pid)
727 char pkgname[MAX_PID_STR_BUFSZ];
733 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
734 ret = app_request_to_launchpad(APP_TERM_BY_PID_ASYNC, pkgname, NULL);
738 SLPAPI int aul_kill_pid(int pid)
740 char pkgname[MAX_PID_STR_BUFSZ];
746 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
747 ret = app_request_to_launchpad(APP_KILL_BY_PID, pkgname, NULL);
751 SLPAPI int aul_set_data_control_provider_cb(data_control_provider_handler_fn handler)
753 __dc_handler = handler;
757 SLPAPI int aul_unset_data_control_provider_cb(void)
763 SLPAPI void aul_set_preinit_window(void *evas_object)
765 __window_object = evas_object;
768 SLPAPI void* aul_get_preinit_window(const char *win_name)
770 return __window_object;
773 SLPAPI void aul_set_preinit_background(void *evas_object)
775 __bg_object = evas_object;
778 SLPAPI void* aul_get_preinit_background(void)
783 SLPAPI void aul_set_preinit_conformant(void *evas_object)
785 __conformant_object = evas_object;
788 SLPAPI void* aul_get_preinit_conformant(void)
790 return __conformant_object;
793 SLPAPI int aul_pause_app(const char *appid)
800 ret = app_request_to_launchpad(APP_PAUSE, appid, NULL);
804 SLPAPI int aul_pause_pid(int pid)
806 char app_pid[MAX_PID_STR_BUFSZ];
812 snprintf(app_pid, MAX_PID_STR_BUFSZ, "%d", pid);
813 ret = app_request_to_launchpad(APP_PAUSE_BY_PID, app_pid, NULL);
817 SLPAPI int aul_reload_appinfo(void)
819 char pkgname[MAX_PID_STR_BUFSZ];
821 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", getpid());
823 return app_request_to_launchpad(AMD_RELOAD_APPINFO, pkgname, NULL);
826 /* vi: set ts=8 sts=8 sw=8: */