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);
58 extern int aul_launch_fini();
60 int aul_is_initialized()
62 return aul_initialized;
65 static int __call_aul_handler(aul_type type, bundle *kb)
68 _aul_handler(type, kb, _aul_data);
72 int app_start(bundle *kb)
74 _app_start_res_prepare(kb);
75 __call_aul_handler(AUL_START, kb);
79 static int app_resume()
81 __call_aul_handler(AUL_RESUME, NULL);
85 static int app_terminate()
87 __call_aul_handler(AUL_TERMINATE, NULL);
92 * @brief encode kb and send it to 'pid'
93 * @param[in] pid receiver's pid
94 * @param[in] cmd message's status (APP_START | APP_RESULT)
97 SLPAPI int app_send_cmd(int pid, int cmd, bundle *kb)
103 bundle_encode(kb, &kb_data, &datalen);
104 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0) {
113 res = AUL_R_ETIMEOUT;
115 case -ELOCALLAUNCH_ID:
127 static void __clear_internal_key(bundle *kb)
129 bundle_del(kb, AUL_K_CALLER_PID);
130 bundle_del(kb, AUL_K_PKG_NAME);
131 bundle_del(kb, AUL_K_WAIT_RESULT);
132 bundle_del(kb, AUL_K_SEND_RESULT);
133 bundle_del(kb, AUL_K_ARGV0);
136 static inline void __set_stime(bundle *kb)
139 char tmp[MAX_LOCAL_BUFSZ];
141 gettimeofday(&tv, NULL);
142 snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
143 bundle_add(kb, AUL_K_STARTTIME, tmp);
146 static int __app_start_internal(gpointer data)
150 kb = (bundle *) data;
157 static int __app_launch_local(bundle *b)
159 if (!aul_is_initialized())
160 return AUL_R_ENOINIT;
163 _E("bundle for APP_START is NULL");
165 if (g_idle_add(__app_start_internal, b) > 0)
171 static int __app_resume_local()
173 if (!aul_is_initialized())
174 return AUL_R_ENOINIT;
182 * @brief start caller with kb
183 * @return callee's pid
185 int app_request_to_launchpad(int cmd, const char *pkgname, bundle *kb)
190 _D("launch request : %s", pkgname);
192 kb = bundle_create();
195 __clear_internal_key(kb);
197 bundle_add(kb, AUL_K_PKG_NAME, pkgname);
199 ret = app_send_cmd(AUL_UTIL_PID, cmd, kb);
201 _D("launch request result : %d", ret);
202 if (ret == AUL_R_LOCAL) {
203 _E("app_request_to_launchpad : Same Process Send Local");
210 ret = __app_launch_local(b);
214 case APP_RESUME_BY_PID:
215 ret = __app_resume_local();
218 _E("no support packet");
230 static int __send_result_to_launchpad(int fd, int res)
232 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
234 _E("send failed due to EPIPE.\n");
235 _E("send fail to client");
242 * @brief caller & callee's sock handler
244 int aul_sock_handler(int fd)
254 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
259 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL && cr.uid != 0) {
260 _E("security error");
261 __send_result_to_launchpad(clifd, -1);
266 __send_result_to_launchpad(clifd, 0);
269 case APP_START: /* run in callee */
271 kbundle = bundle_decode(pkt->data, pkt->len);
275 bundle_free(kbundle);
278 case APP_OPEN: /* run in callee */
280 case APP_RESUME_BY_PID:
284 case APP_TERM_BY_PID: /* run in callee */
288 case APP_RESULT: /* run in caller */
290 kbundle = bundle_decode(pkt->data, pkt->len);
294 pid_str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
297 app_result(pkt->cmd, kbundle, pid);
298 bundle_free(kbundle);
301 case APP_KEY_EVENT: /* run in caller */
302 kbundle = bundle_decode(pkt->data, pkt->len);
305 app_key_event(kbundle);
306 bundle_free(kbundle);
310 _E("no support packet");
321 int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
327 *kb = bundle_create();
334 if ((argv != NULL) && (argv[0] != NULL)) {
335 buf = strdup(argv[0]);
341 bundle_add(*kb, AUL_K_ARGV0, buf);
343 if (buf) { /*Prevent FIX: ID 38717 */
349 if (ac + 1 == argc) {
350 if (bundle_add(*kb, argv[ac], "") < 0) {
351 _E("bundle add error pos - %d", ac);
352 return AUL_R_ECANCELED;
355 if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
356 _E("bundle add error pos - %d", ac);
357 return AUL_R_ECANCELED;
366 int aul_register_init_callback(
367 int (*aul_handler) (aul_type type, bundle *, void *), void *data)
369 /* Save start handler function in static var */
370 _aul_handler = aul_handler;
377 if (aul_initialized) {
378 _E("aul already initialized");
379 return AUL_R_ECANCELED;
382 aul_fd = __create_server_sock(getpid());
384 _E("aul_init create sock failed");
392 SLPAPI void aul_finalize()
398 if (aul_initialized) {
406 SLPAPI int aul_launch_app(const char *appid, bundle *kb)
413 ret = app_request_to_launchpad(APP_START, appid, kb);
417 SLPAPI int aul_open_app(const char *appid)
424 ret = app_request_to_launchpad(APP_OPEN, appid, NULL);
428 SLPAPI int aul_resume_app(const char *appid)
435 ret = app_request_to_launchpad(APP_RESUME, appid, NULL);
439 SLPAPI int aul_resume_pid(int pid)
441 char pkgname[MAX_PID_STR_BUFSZ];
447 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
448 ret = app_request_to_launchpad(APP_RESUME_BY_PID, pkgname, NULL);
452 SLPAPI int aul_terminate_pid(int pid)
454 char pkgname[MAX_PID_STR_BUFSZ];
460 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
461 ret = app_request_to_launchpad(APP_TERM_BY_PID, pkgname, NULL);
465 SLPAPI int aul_kill_pid(int pid)
467 char pkgname[MAX_PID_STR_BUFSZ];
473 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
474 ret = app_request_to_launchpad(APP_KILL_BY_PID, pkgname, NULL);
478 /* vi: set ts=8 sts=8 sw=8: */