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"
39 static int aul_initialized = 0;
42 static int (*_aul_handler) (aul_type type, bundle *kb, void *data) = NULL;
43 static void *_aul_data;
47 static int __call_aul_handler(aul_type type, bundle *kb);
48 static int app_resume();
49 static int app_terminate();
50 static void __clear_internal_key(bundle *kb);
51 static inline void __set_stime(bundle *kb);
52 static int __app_start_internal(gpointer data);
53 static int __app_launch_local(bundle *b);
54 static int __send_result_to_launchpad(int fd, int res);
57 int aul_is_initialized()
59 return aul_initialized;
62 static int __call_aul_handler(aul_type type, bundle *kb)
65 _aul_handler(type, kb, _aul_data);
69 int app_start(bundle *kb)
71 _app_start_res_prepare(kb);
72 __call_aul_handler(AUL_START, kb);
76 static int app_resume()
78 __call_aul_handler(AUL_RESUME, NULL);
82 static int app_terminate()
84 __call_aul_handler(AUL_TERMINATE, NULL);
89 * @brief encode kb and send it to 'pid'
90 * @param[in] pid receiver's pid
91 * @param[in] cmd message's status (APP_START | APP_RESULT)
94 int app_send_cmd(int pid, int cmd, bundle *kb)
100 bundle_encode(kb, &kb_data, &datalen);
101 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0) {
110 res = AUL_R_ETIMEOUT;
112 case -ELOCALLAUNCH_ID:
124 static void __clear_internal_key(bundle *kb)
126 bundle_del(kb, AUL_K_CALLER_PID);
127 bundle_del(kb, AUL_K_PKG_NAME);
128 bundle_del(kb, AUL_K_WAIT_RESULT);
129 bundle_del(kb, AUL_K_SEND_RESULT);
130 bundle_del(kb, AUL_K_ARGV0);
133 static inline void __set_stime(bundle *kb)
136 char tmp[MAX_LOCAL_BUFSZ];
138 gettimeofday(&tv, NULL);
139 snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
140 bundle_add(kb, AUL_K_STARTTIME, tmp);
143 static int __app_start_internal(gpointer data)
147 kb = (bundle *) data;
154 static int __app_launch_local(bundle *b)
156 if (!aul_is_initialized())
157 return AUL_R_ENOINIT;
160 _E("bundle for APP_START is NULL");
162 if (g_idle_add(__app_start_internal, b) > 0)
168 static int __app_resume_local()
170 if (!aul_is_initialized())
171 return AUL_R_ENOINIT;
179 * @brief start caller with kb
180 * @return callee's pid
182 int app_request_to_launchpad(int cmd, const char *pkgname, bundle *kb)
188 kb = bundle_create();
191 __clear_internal_key(kb);
193 bundle_add(kb, AUL_K_PKG_NAME, pkgname);
195 ret = app_send_cmd(LAUNCHPAD_PID, cmd, kb);
197 if (ret == AUL_R_LOCAL) {
198 _E("app_request_to_launchpad : Same Process Send Local");
205 ret = __app_launch_local(b);
208 case APP_RESUME_BY_PID:
209 ret = __app_resume_local();
212 _E("no support packet");
224 static int __send_result_to_launchpad(int fd, int res)
226 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
228 _E("send failed due to EPIPE.\n");
229 _E("send fail to client");
236 * @brief caller & callee's sock handler
238 int aul_sock_handler(int fd)
248 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
253 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL && cr.uid != 0) {
254 _E("security error");
255 __send_result_to_launchpad(clifd, -1);
260 __send_result_to_launchpad(clifd, 0);
263 case APP_START: /* run in callee */
265 kbundle = bundle_decode(pkt->data, pkt->len);
269 bundle_free(kbundle);
272 case APP_RESUME: /* run in callee */
273 case APP_RESUME_BY_PID:
277 case APP_TERM_BY_PID: /* run in callee */
281 case APP_RESULT: /* run in caller */
283 kbundle = bundle_decode(pkt->data, pkt->len);
287 pid_str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
290 app_result(pkt->cmd, kbundle, pid);
291 bundle_free(kbundle);
295 _E("no support packet");
306 int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
312 *kb = bundle_create();
319 if ((argv != NULL) && (argv[0] != NULL)) {
320 buf = strdup(argv[0]);
326 bundle_add(*kb, AUL_K_ARGV0, buf);
328 if (buf) { /*Prevent FIX: ID 38717 */
334 if (ac + 1 == argc) {
335 if (bundle_add(*kb, argv[ac], "") < 0) {
336 _E("bundle add error pos - %d", ac);
337 return AUL_R_ECANCELED;
340 if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
341 _E("bundle add error pos - %d", ac);
342 return AUL_R_ECANCELED;
351 int aul_register_init_callback(
352 int (*aul_handler) (aul_type type, bundle *, void *), void *data)
354 /* Save start handler function in static var */
355 _aul_handler = aul_handler;
362 if (aul_initialized) {
363 _E("aul already initialized");
364 return AUL_R_ECANCELED;
367 aul_fd = __create_server_sock(getpid());
369 _E("aul_init create sock failed");
377 SLPAPI void aul_finalize()
381 if (aul_initialized) {
389 SLPAPI int aul_launch_app(const char *pkgname, bundle *kb)
396 ret = app_request_to_launchpad(APP_START, pkgname, kb);
400 SLPAPI int aul_open_app(const char *pkgname)
407 ret = app_request_to_launchpad(APP_RESUME, pkgname, NULL);
411 SLPAPI int aul_resume_app(const char *pkgname, bundle *kb)
418 ret = app_request_to_launchpad(APP_RESUME, pkgname, kb);
422 SLPAPI int aul_resume_pid(int pid)
424 char pkgname[MAX_PID_STR_BUFSZ];
430 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
431 ret = app_request_to_launchpad(APP_RESUME_BY_PID, pkgname, NULL);
435 SLPAPI int aul_terminate_pid(int pid)
437 char pkgname[MAX_PID_STR_BUFSZ];
443 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
444 ret = app_request_to_launchpad(APP_TERM_BY_PID, pkgname, NULL);
448 SLPAPI int aul_kill_pid(int pid)
450 char pkgname[MAX_PID_STR_BUFSZ];
456 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
457 ret = app_request_to_launchpad(APP_KILL_BY_PID, pkgname, NULL);
461 /* vi: set ts=8 sts=8 sw=8: */