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>
36 #include "simple_util.h"
41 static int aul_initialized = 0;
44 static int (*_aul_handler) (aul_type type, bundle *kb, void *data) = NULL;
45 static void *_aul_data;
49 static int __call_aul_handler(aul_type type, bundle *kb);
50 static int app_resume();
51 static int app_terminate();
52 static void __clear_internal_key(bundle *kb);
53 static inline void __set_stime(bundle *kb);
54 static int __app_start_internal(gpointer data);
55 static int __app_launch_local(bundle *b);
56 static int __send_result_to_launchpad(int fd, int res);
59 #ifdef PROCESS_POOL_ENABLE
60 static void *__window_object = NULL;
63 extern int aul_launch_fini();
65 int aul_is_initialized()
67 return aul_initialized;
70 static int __call_aul_handler(aul_type type, bundle *kb)
73 _aul_handler(type, kb, _aul_data);
77 int app_start(bundle *kb)
79 _app_start_res_prepare(kb);
80 __call_aul_handler(AUL_START, kb);
84 static int app_resume()
86 __call_aul_handler(AUL_RESUME, NULL);
90 static int app_terminate()
92 __call_aul_handler(AUL_TERMINATE, NULL);
98 * @brief encode kb and send it to 'pid'
99 * @param[in] pid receiver's pid
100 * @param[in] cmd message's status (APP_START | APP_RESULT)
103 SLPAPI int app_send_cmd(int pid, int cmd, bundle *kb)
109 bundle_encode(kb, &kb_data, &datalen);
110 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0) {
119 res = AUL_R_ETIMEOUT;
121 case -ELOCALLAUNCH_ID:
124 case -EILLEGALACCESS:
128 res = AUL_R_ETERMINATING;
131 res = AUL_R_ENOLAUNCHPAD;
142 SLPAPI int app_send_cmd_with_noreply(int pid, int cmd, bundle *kb)
148 bundle_encode(kb, &kb_data, &datalen);
149 if ((res = __app_send_raw_with_noreply(pid, cmd, kb_data, datalen)) < 0) {
158 res = AUL_R_ETIMEOUT;
160 case -ELOCALLAUNCH_ID:
163 case -EILLEGALACCESS:
175 static void __clear_internal_key(bundle *kb)
177 bundle_del(kb, AUL_K_CALLER_PID);
178 bundle_del(kb, AUL_K_PKG_NAME);
179 bundle_del(kb, AUL_K_WAIT_RESULT);
180 bundle_del(kb, AUL_K_SEND_RESULT);
181 bundle_del(kb, AUL_K_ARGV0);
184 static inline void __set_stime(bundle *kb)
187 char tmp[MAX_LOCAL_BUFSZ];
189 gettimeofday(&tv, NULL);
190 snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
191 bundle_add(kb, AUL_K_STARTTIME, tmp);
194 static int __app_start_internal(gpointer data)
198 kb = (bundle *) data;
205 static int __app_launch_local(bundle *b)
207 if (!aul_is_initialized())
208 return AUL_R_ENOINIT;
211 _E("bundle for APP_START is NULL");
213 if (g_idle_add(__app_start_internal, b) > 0)
219 static int __app_resume_local()
221 if (!aul_is_initialized())
222 return AUL_R_ENOINIT;
230 * @brief start caller with kb
231 * @return callee's pid
233 int app_request_to_launchpad(int cmd, const char *pkgname, bundle *kb)
238 SECURE_LOGD("launch request : %s", pkgname);
240 kb = bundle_create();
243 __clear_internal_key(kb);
245 bundle_add(kb, AUL_K_PKG_NAME, pkgname);
247 if(cmd == APP_START_ASYNC)
248 ret = app_send_cmd_with_noreply(AUL_UTIL_PID, cmd, kb);
250 ret = app_send_cmd(AUL_UTIL_PID, cmd, kb);
252 _D("launch request result : %d", ret);
253 if (ret == AUL_R_LOCAL) {
254 _E("app_request_to_launchpad : Same Process Send Local");
261 ret = __app_launch_local(b);
265 case APP_RESUME_BY_PID:
266 ret = __app_resume_local();
269 _E("no support packet");
281 static int __send_result_to_launchpad(int fd, int res)
283 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
284 if (errno == EPIPE) {
285 _E("send failed due to EPIPE.\n");
289 _E("send fail to client");
296 * @brief caller & callee's sock handler
298 int aul_sock_handler(int fd)
305 const char *pid_str = NULL;
309 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
314 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL && cr.uid != 0) {
315 _E("security error");
316 __send_result_to_launchpad(clifd, -1);
321 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL) {
322 ret = __send_result_to_launchpad(clifd, 0);
332 case APP_START: /* run in callee */
334 case APP_START_ASYNC:
335 kbundle = bundle_decode(pkt->data, pkt->len);
339 bundle_free(kbundle);
342 case APP_OPEN: /* run in callee */
344 case APP_RESUME_BY_PID:
348 case APP_TERM_BY_PID: /* run in callee */
352 case APP_TERM_REQ_BY_PID: /* run in callee */
353 app_subapp_terminate_request();
356 case APP_RESULT: /* run in caller */
358 kbundle = bundle_decode(pkt->data, pkt->len);
362 pid_str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
365 app_result(pkt->cmd, kbundle, pid);
367 bundle_free(kbundle);
370 case APP_KEY_EVENT: /* run in caller */
371 kbundle = bundle_decode(pkt->data, pkt->len);
374 app_key_event(kbundle);
375 bundle_free(kbundle);
379 _E("no support packet");
390 int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
396 *kb = bundle_create();
403 if ((argv != NULL) && (argv[0] != NULL)) {
404 buf = strdup(argv[0]);
410 bundle_add(*kb, AUL_K_ARGV0, buf);
412 if (buf) { /*Prevent FIX: ID 38717 */
418 if (ac + 1 == argc) {
419 if (bundle_add(*kb, argv[ac], "") < 0) {
420 _E("bundle add error pos - %d", ac);
421 return AUL_R_ECANCELED;
424 if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
425 _E("bundle add error pos - %d", ac);
426 return AUL_R_ECANCELED;
435 int aul_register_init_callback(
436 int (*aul_handler) (aul_type type, bundle *, void *), void *data)
438 /* Save start handler function in static var */
439 _aul_handler = aul_handler;
446 if (aul_initialized) {
447 //_E("aul already initialized");
448 return AUL_R_ECANCELED;
451 aul_fd = __create_server_sock(getpid());
453 _E("aul_init create sock failed");
461 SLPAPI void aul_finalize()
466 if (aul_initialized) {
474 SLPAPI int aul_launch_app(const char *appid, bundle *kb)
481 ret = app_request_to_launchpad(APP_START, appid, kb);
485 SLPAPI int aul_launch_app_async(const char *appid, bundle *kb)
492 ret = app_request_to_launchpad(APP_START_ASYNC, appid, kb);
496 SLPAPI int aul_open_app(const char *appid)
503 ret = app_request_to_launchpad(APP_OPEN, appid, NULL);
507 SLPAPI int aul_resume_app(const char *appid)
514 ret = app_request_to_launchpad(APP_RESUME, appid, NULL);
518 SLPAPI int aul_resume_pid(int pid)
520 char pkgname[MAX_PID_STR_BUFSZ];
526 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
527 ret = app_request_to_launchpad(APP_RESUME_BY_PID, pkgname, NULL);
531 SLPAPI int aul_terminate_pid(int pid)
533 char pkgname[MAX_PID_STR_BUFSZ];
539 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
540 ret = app_request_to_launchpad(APP_TERM_BY_PID, pkgname, NULL);
544 SLPAPI int aul_kill_pid(int pid)
546 char pkgname[MAX_PID_STR_BUFSZ];
552 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
553 ret = app_request_to_launchpad(APP_KILL_BY_PID, pkgname, NULL);
557 #ifdef PROCESS_POOL_ENABLE
558 SLPAPI void aul_set_preinit_window(void *evas_object)
560 __window_object = evas_object;
563 SLPAPI void* aul_get_preinit_window(void)
565 return __window_object;
569 /* vi: set ts=8 sts=8 sw=8: */