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:
118 case -EILLEGALACCESS:
130 static void __clear_internal_key(bundle *kb)
132 bundle_del(kb, AUL_K_CALLER_PID);
133 bundle_del(kb, AUL_K_PKG_NAME);
134 bundle_del(kb, AUL_K_WAIT_RESULT);
135 bundle_del(kb, AUL_K_SEND_RESULT);
136 bundle_del(kb, AUL_K_ARGV0);
139 static inline void __set_stime(bundle *kb)
142 char tmp[MAX_LOCAL_BUFSZ];
144 gettimeofday(&tv, NULL);
145 snprintf(tmp, MAX_LOCAL_BUFSZ, "%ld/%ld", tv.tv_sec, tv.tv_usec);
146 bundle_add(kb, AUL_K_STARTTIME, tmp);
149 static int __app_start_internal(gpointer data)
153 kb = (bundle *) data;
160 static int __app_launch_local(bundle *b)
162 if (!aul_is_initialized())
163 return AUL_R_ENOINIT;
166 _E("bundle for APP_START is NULL");
168 if (g_idle_add(__app_start_internal, b) > 0)
174 static int __app_resume_local()
176 if (!aul_is_initialized())
177 return AUL_R_ENOINIT;
185 * @brief start caller with kb
186 * @return callee's pid
188 int app_request_to_launchpad(int cmd, const char *pkgname, bundle *kb)
193 _D("launch request : %s", pkgname);
195 kb = bundle_create();
198 __clear_internal_key(kb);
200 bundle_add(kb, AUL_K_PKG_NAME, pkgname);
202 ret = app_send_cmd(AUL_UTIL_PID, cmd, kb);
204 _D("launch request result : %d", ret);
205 if (ret == AUL_R_LOCAL) {
206 _E("app_request_to_launchpad : Same Process Send Local");
213 ret = __app_launch_local(b);
217 case APP_RESUME_BY_PID:
218 ret = __app_resume_local();
221 _E("no support packet");
233 static int __send_result_to_launchpad(int fd, int res)
235 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
237 _E("send failed due to EPIPE.\n");
238 _E("send fail to client");
245 * @brief caller & callee's sock handler
247 int aul_sock_handler(int fd)
257 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
262 if (pkt->cmd != APP_RESULT && pkt->cmd != APP_CANCEL && cr.uid != 0) {
263 _E("security error");
264 __send_result_to_launchpad(clifd, -1);
269 __send_result_to_launchpad(clifd, 0);
272 case APP_START: /* run in callee */
274 kbundle = bundle_decode(pkt->data, pkt->len);
278 bundle_free(kbundle);
281 case APP_OPEN: /* run in callee */
283 case APP_RESUME_BY_PID:
287 case APP_TERM_BY_PID: /* run in callee */
291 case APP_RESULT: /* run in caller */
293 kbundle = bundle_decode(pkt->data, pkt->len);
297 pid_str = bundle_get_val(kbundle, AUL_K_CALLEE_PID);
300 app_result(pkt->cmd, kbundle, pid);
301 bundle_free(kbundle);
304 case APP_KEY_EVENT: /* run in caller */
305 kbundle = bundle_decode(pkt->data, pkt->len);
308 app_key_event(kbundle);
309 bundle_free(kbundle);
313 _E("no support packet");
324 int aul_make_bundle_from_argv(int argc, char **argv, bundle **kb)
330 *kb = bundle_create();
337 if ((argv != NULL) && (argv[0] != NULL)) {
338 buf = strdup(argv[0]);
344 bundle_add(*kb, AUL_K_ARGV0, buf);
346 if (buf) { /*Prevent FIX: ID 38717 */
352 if (ac + 1 == argc) {
353 if (bundle_add(*kb, argv[ac], "") < 0) {
354 _E("bundle add error pos - %d", ac);
355 return AUL_R_ECANCELED;
358 if (bundle_add(*kb, argv[ac], argv[ac + 1]) < 0) {
359 _E("bundle add error pos - %d", ac);
360 return AUL_R_ECANCELED;
369 int aul_register_init_callback(
370 int (*aul_handler) (aul_type type, bundle *, void *), void *data)
372 /* Save start handler function in static var */
373 _aul_handler = aul_handler;
380 if (aul_initialized) {
381 _E("aul already initialized");
382 return AUL_R_ECANCELED;
385 aul_fd = __create_server_sock(getpid());
387 _E("aul_init create sock failed");
395 SLPAPI void aul_finalize()
401 if (aul_initialized) {
409 SLPAPI int aul_launch_app(const char *appid, bundle *kb)
416 ret = app_request_to_launchpad(APP_START, appid, kb);
420 SLPAPI int aul_open_app(const char *appid)
427 ret = app_request_to_launchpad(APP_OPEN, appid, NULL);
431 SLPAPI int aul_resume_app(const char *appid)
438 ret = app_request_to_launchpad(APP_RESUME, appid, NULL);
442 SLPAPI int aul_resume_pid(int pid)
444 char pkgname[MAX_PID_STR_BUFSZ];
450 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
451 ret = app_request_to_launchpad(APP_RESUME_BY_PID, pkgname, NULL);
455 SLPAPI int aul_terminate_pid(int pid)
457 char pkgname[MAX_PID_STR_BUFSZ];
463 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
464 ret = app_request_to_launchpad(APP_TERM_BY_PID, pkgname, NULL);
468 SLPAPI int aul_kill_pid(int pid)
470 char pkgname[MAX_PID_STR_BUFSZ];
476 snprintf(pkgname, MAX_PID_STR_BUFSZ, "%d", pid);
477 ret = app_request_to_launchpad(APP_KILL_BY_PID, pkgname, NULL);
481 /* vi: set ts=8 sts=8 sw=8: */