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"
40 static int aul_initialized = 0;
43 static int (*_aul_handler) (aul_type type, bundle *kb, void *data) = NULL;
44 static void *_aul_data;
48 static int __call_aul_handler(aul_type type, bundle *kb);
49 static int app_resume();
50 static int app_terminate();
51 static void __clear_internal_key(bundle *kb);
52 static inline void __set_stime(bundle *kb);
53 static int __app_start_internal(gpointer data);
54 static int __app_launch_local(bundle *b);
55 static int __send_result_to_launchpad(int fd, int res);
57 extern int aul_launch_fini();
59 int aul_is_initialized()
61 return aul_initialized;
64 static int __call_aul_handler(aul_type type, bundle *kb)
67 _aul_handler(type, kb, _aul_data);
71 int app_start(bundle *kb)
73 _app_start_res_prepare(kb);
74 __call_aul_handler(AUL_START, kb);
78 static int app_resume()
80 __call_aul_handler(AUL_RESUME, NULL);
84 static int app_terminate()
86 __call_aul_handler(AUL_TERMINATE, NULL);
91 * @brief encode kb and send it to 'pid'
92 * @param[in] pid receiver's pid
93 * @param[in] cmd message's status (APP_START | APP_RESULT)
96 SLPAPI int app_send_cmd(int pid, int cmd, bundle *kb)
102 bundle_encode(kb, &kb_data, &datalen);
103 if ((res = __app_send_raw(pid, cmd, kb_data, datalen)) < 0) {
112 res = AUL_R_ETIMEOUT;
114 case -ELOCALLAUNCH_ID:
126 static void __clear_internal_key(bundle *kb)
128 bundle_del(kb, AUL_K_CALLER_PID);
129 bundle_del(kb, AUL_K_PKG_NAME);
130 bundle_del(kb, AUL_K_WAIT_RESULT);
131 bundle_del(kb, AUL_K_SEND_RESULT);
132 bundle_del(kb, AUL_K_ARGV0);
135 static inline void __set_stime(bundle *kb)
138 char tmp[MAX_LOCAL_BUFSZ];
140 gettimeofday(&tv, NULL);
141 snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
142 bundle_add(kb, AUL_K_STARTTIME, tmp);
145 static int __app_start_internal(gpointer data)
149 kb = (bundle *) data;
156 static int __app_launch_local(bundle *b)
158 if (!aul_is_initialized())
159 return AUL_R_ENOINIT;
162 _E("bundle for APP_START is NULL");
164 if (g_idle_add(__app_start_internal, b) > 0)
170 static int __app_resume_local()
172 if (!aul_is_initialized())
173 return AUL_R_ENOINIT;
181 * @brief start caller with kb
182 * @return callee's pid
184 int app_request_to_launchpad(int cmd, const char *pkgname, bundle *kb)
190 kb = bundle_create();
193 __clear_internal_key(kb);
195 bundle_add(kb, AUL_K_PKG_NAME, pkgname);
197 ret = app_send_cmd(LAUNCHPAD_PID, cmd, kb);
199 if (ret == AUL_R_LOCAL) {
200 _E("app_request_to_launchpad : Same Process Send Local");
207 ret = __app_launch_local(b);
211 case APP_RESUME_BY_PID:
212 ret = __app_resume_local();
215 _E("no support packet");
227 static int __send_result_to_launchpad(int fd, int res)
229 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
231 _E("send failed due to EPIPE.\n");
232 _E("send fail to client");
239 * @brief caller & callee's sock handler
241 int aul_sock_handler(int fd)
251 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
256 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL && cr.uid != 0) {
257 _E("security error");
258 __send_result_to_launchpad(clifd, -1);
263 __send_result_to_launchpad(clifd, 0);
266 case APP_START: /* run in callee */
268 kbundle = bundle_decode(pkt->data, pkt->len);
272 bundle_free(kbundle);
275 case APP_OPEN: /* run in callee */
277 case APP_RESUME_BY_PID:
281 case APP_TERM_BY_PID: /* run in callee */
285 case APP_RESULT: /* run in caller */
287 kbundle = bundle_decode(pkt->data, pkt->len);
291 pid_str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
294 app_result(pkt->cmd, kbundle, pid);
295 bundle_free(kbundle);
298 case APP_KEY_EVENT: /* run in caller */
299 kbundle = bundle_decode(pkt->data, pkt->len);
302 app_key_event(kbundle);
303 bundle_free(kbundle);
307 _E("no support packet");
318 int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
324 *kb = bundle_create();
331 if ((argv != NULL) && (argv[0] != NULL)) {
332 buf = strdup(argv[0]);
338 bundle_add(*kb, AUL_K_ARGV0, buf);
340 if (buf) { /*Prevent FIX: ID 38717 */
346 if (ac + 1 == argc) {
347 if (bundle_add(*kb, argv[ac], "") < 0) {
348 _E("bundle add error pos - %d", ac);
349 return AUL_R_ECANCELED;
352 if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
353 _E("bundle add error pos - %d", ac);
354 return AUL_R_ECANCELED;
363 int aul_register_init_callback(
364 int (*aul_handler) (aul_type type, bundle *, void *), void *data)
366 /* Save start handler function in static var */
367 _aul_handler = aul_handler;
374 if (aul_initialized) {
375 _E("aul already initialized");
376 return AUL_R_ECANCELED;
379 aul_fd = __create_server_sock(getpid());
381 _E("aul_init create sock failed");
389 SLPAPI void aul_finalize()
395 if (aul_initialized) {
403 SLPAPI int aul_launch_app(const char *appid, bundle *kb)
410 ret = app_request_to_launchpad(APP_START, appid, kb);
414 SLPAPI int aul_open_app(const char *appid)
421 ret = app_request_to_launchpad(APP_OPEN, appid, NULL);
425 SLPAPI int aul_resume_app(const char *appid)
432 ret = app_request_to_launchpad(APP_RESUME, appid, NULL);
436 SLPAPI int aul_resume_pid(int pid)
438 char pkgname[MAX_PID_STR_BUFSZ];
444 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
445 ret = app_request_to_launchpad(APP_RESUME_BY_PID, pkgname, NULL);
449 SLPAPI int aul_terminate_pid(int pid)
451 char pkgname[MAX_PID_STR_BUFSZ];
457 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
458 ret = app_request_to_launchpad(APP_TERM_BY_PID, pkgname, NULL);
462 SLPAPI int aul_kill_pid(int pid)
464 char pkgname[MAX_PID_STR_BUFSZ];
470 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
471 ret = app_request_to_launchpad(APP_KILL_BY_PID, pkgname, NULL);
475 /* vi: set ts=8 sts=8 sw=8: */