2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <syspopup_caller.h>
22 #include <sys/resource.h>
23 #include <bundle_internal.h>
24 #include <app_manager.h>
25 #include <app_control.h>
27 #include "process_mgr.h"
29 #include "dbus_util.h"
31 #define LOCKD_VOICE_CALL_PKG_NAME "org.tizen.call-ui"
32 #define LOCKD_VIDEO_CALL_PKG_NAME "org.tizen.vtmain"
34 #define NICE_VALUE_PWLOCK -5
35 #define NICE_VALUE_LOCKSCREEN -20
39 typedef struct _launch_info_s {
49 static int _try_to_launch(const char *appid, const char *key, const char *value, after_func afn)
56 app_control_h app_control_handle = NULL;
57 app_context_h app_context_handle = NULL;
59 ret = app_control_create(&app_control_handle);
60 if (ret != APP_CONTROL_ERROR_NONE) {
61 _E("Failed to create appcontrol");
65 ret = app_control_set_operation(app_control_handle, APP_CONTROL_OPERATION_DEFAULT);
66 if (ret != APP_CONTROL_ERROR_NONE) {
67 _E("Failed to set operation to appcontrol");
68 app_control_destroy(app_control_handle);
73 _D("key(%s), value(%s)", key, value);
74 ret = app_control_add_extra_data(app_control_handle, key, value);
75 if (ret != APP_CONTROL_ERROR_NONE) {
76 _E("Failed to add extra data to appcontrol");
77 app_control_destroy(app_control_handle);
82 ret = app_control_set_app_id(app_control_handle, appid);
83 if (ret != APP_CONTROL_ERROR_NONE) {
84 _E("Failed to set app id to appcontrol");
85 app_control_destroy(app_control_handle);
89 ret = app_control_send_launch_request(app_control_handle, NULL, NULL);
90 if (ret != APP_CONTROL_ERROR_NONE) {
91 _E("Failed to send launch request : %s", appid);
92 app_control_destroy(app_control_handle);
96 app_control_destroy(app_control_handle);
98 ret = app_manager_get_app_context(appid, &app_context_handle);
99 if (ret != APP_MANAGER_ERROR_NONE) {
100 _E("Failed to get app context : %s", appid);
104 ret = app_context_get_pid(app_context_handle, &pid);
105 if (ret != APP_MANAGER_ERROR_NONE) {
106 _E("Failed to get pid : %s", appid);
107 app_context_destroy(app_context_handle);
111 app_context_destroy(app_context_handle);
113 _D("Succeed to launch : %s(%d)", appid, pid);
121 static Eina_Bool _must_launch_cb(void *data)
123 launch_info_s *launch_info = data;
126 retv_if(!launch_info, ECORE_CALLBACK_CANCEL);
128 pid = _try_to_launch(launch_info->appid, launch_info->key, launch_info->value, launch_info->afn);
133 retv_if(pid == AUL_R_ECOMM, ECORE_CALLBACK_RENEW);
134 retv_if(pid == AUL_R_ETERMINATING, ECORE_CALLBACK_RENEW);
135 if (pid == AUL_R_ERROR) {
137 && 0 == launch_info->cfn(launch_info->appid, launch_info->key, launch_info->value, (void *) launch_info->cfn, (void *) launch_info->afn))
139 _D("change func has set the next appid");
141 _E("change func has returned error");
146 return ECORE_CALLBACK_RENEW;
149 free(launch_info->appid);
150 free(launch_info->key);
151 free(launch_info->value);
153 return ECORE_CALLBACK_CANCEL;
158 void process_mgr_must_launch(const char *appid, const char *key, const char *value, change_func cfn, after_func afn)
160 Ecore_Timer *timer = NULL;
161 launch_info_s *launch_info = NULL;
164 _D("Must launch : %s", appid);
166 pid = _try_to_launch(appid, key, value, afn);
169 _E("Failed the first try to launch %s", appid);
171 launch_info = calloc(1, sizeof(launch_info_s));
172 ret_if(!launch_info);
174 if (appid) launch_info->appid = strdup(appid);
175 if (key) launch_info->key = strdup(key);
176 if (value) launch_info->value = strdup(value);
177 launch_info->cfn = cfn;
178 launch_info->afn = afn;
180 timer = ecore_timer_add(0.1f, _must_launch_cb, launch_info);
182 _E("cannot add a timer for must_launch");
183 free(launch_info->appid);
184 free(launch_info->key);
185 free(launch_info->value);
192 static int _try_to_open(const char *appid, after_func afn)
198 pid = aul_open_app(appid);
200 _D("Succeed to open %s", appid);
209 static Eina_Bool _must_open_cb(void *data)
211 launch_info_s *launch_info = data;
214 retv_if(!launch_info, ECORE_CALLBACK_CANCEL);
216 pid = _try_to_open(launch_info->appid, launch_info->afn);
221 retv_if(pid == AUL_R_ECOMM, ECORE_CALLBACK_RENEW);
222 retv_if(pid == AUL_R_ETERMINATING, ECORE_CALLBACK_RENEW);
223 if (pid == AUL_R_ERROR) {
224 if (launch_info->cfn && 0 == launch_info->cfn(launch_info->appid, NULL, NULL, launch_info->cfn, launch_info->afn)) {
225 _D("change func has set the next appid");
227 _E("change func has returned error");
232 return ECORE_CALLBACK_RENEW;
235 free(launch_info->appid);
237 return ECORE_CALLBACK_CANCEL;
242 void process_mgr_must_open(const char *appid, change_func cfn, after_func afn)
244 Ecore_Timer *timer = NULL;
245 launch_info_s *launch_info = NULL;
248 _D("Must open %s", appid);
250 pid = _try_to_open(appid, afn);
253 _E("Failed the first try to open %s", appid);
255 launch_info = calloc(1, sizeof(launch_info_s));
256 ret_if(!launch_info);
258 if (appid) launch_info->appid = strdup(appid);
259 launch_info->cfn = cfn;
260 launch_info->afn = afn;
262 timer = ecore_timer_add(0.1f, _must_open_cb, launch_info);
264 _E("cannot add a timer for must_launch");
265 free(launch_info->appid);
272 static int _try_to_syspopup_launch(const char *appid, const char *key, const char *value, after_func afn)
281 bundle_add(b, key, value);
284 pid = aul_launch_app("org.tizen.volume", b);
285 if (b) bundle_free(b);
287 _D("Succeed to launch %s", appid);
296 static Eina_Bool _must_syspopup_launch_cb(void *data)
298 launch_info_s *launch_info = data;
301 retv_if(!launch_info, ECORE_CALLBACK_CANCEL);
303 pid = _try_to_syspopup_launch(launch_info->appid, launch_info->key, launch_info->value, launch_info->afn);
308 retv_if(pid == AUL_R_ECOMM, ECORE_CALLBACK_RENEW);
309 retv_if(pid == AUL_R_ETERMINATING, ECORE_CALLBACK_RENEW);
310 if (pid == AUL_R_ERROR) {
312 && 0 == launch_info->cfn(launch_info->appid, launch_info->key, launch_info->value, launch_info->cfn, launch_info->afn)) {
313 _D("change func has set the next appid");
315 _E("change func has returned error");
320 return ECORE_CALLBACK_RENEW;
323 free(launch_info->appid);
324 free(launch_info->key);
325 free(launch_info->value);
327 return ECORE_CALLBACK_CANCEL;
332 void process_mgr_must_syspopup_launch(const char *appid, const char *key, const char *value, change_func cfn, after_func afn)
334 Ecore_Timer *timer = NULL;
335 launch_info_s *launch_info = NULL;
338 _D("Must launch %s", appid);
340 pid = _try_to_syspopup_launch(appid, key, value, afn);
343 _E("Failed the first try to launch %s", appid);
345 launch_info = calloc(1, sizeof(launch_info_s));
346 ret_if(!launch_info);
348 if (appid) launch_info->appid = strdup(appid);
349 if (key) launch_info->key = strdup(key);
350 if (value) launch_info->value = strdup(value);
351 launch_info->cfn = cfn;
352 launch_info->afn = afn;
354 timer = ecore_timer_add(0.1f, _must_syspopup_launch_cb, launch_info);
356 _E("cannot add a timer for must_launch");
357 free(launch_info->appid);
358 free(launch_info->key);
359 free(launch_info->value);
366 static Eina_Bool _set_lock_priority_cb(void *data)
370 prio = getpriority(PRIO_PROCESS, (pid_t)data);
371 if (prio == NICE_VALUE_LOCKSCREEN) {
372 _D("%s (%d: %d)\n", "setpriority Success", (pid_t)data, prio);
373 return ECORE_CALLBACK_CANCEL;
376 if (setpriority(PRIO_PROCESS, (pid_t)data, NICE_VALUE_LOCKSCREEN) < 0 ) {
377 _D("error : %d", errno);
378 return ECORE_CALLBACK_CANCEL;
381 return ECORE_CALLBACK_RENEW;
386 static Eina_Bool _set_pwlock_priority_cb(void *data)
390 prio = getpriority(PRIO_PROCESS, (pid_t)data);
391 if (prio == NICE_VALUE_PWLOCK) {
392 _D("%s (%d: %d)\n", "setpriority Success", (pid_t)data, prio);
393 return ECORE_CALLBACK_CANCEL;
396 if (setpriority(PRIO_PROCESS, (pid_t)data, NICE_VALUE_PWLOCK) < 0 ) {
397 _D("error : %d", errno);
398 return ECORE_CALLBACK_CANCEL;
401 return ECORE_CALLBACK_RENEW;
406 int process_mgr_set_lock_priority(int pid)
408 return !ecore_timer_add(1.0f, _set_lock_priority_cb, (void *)pid);
413 int process_mgr_set_pwlock_priority(int pid)
415 return !ecore_timer_add(1.0f, _set_pwlock_priority_cb, (void *)pid);
420 void process_mgr_terminate_app(int pid, int state)
422 _D("process_mgr_terminate_app, state:%d\n", state);
426 _D("Terminate app(pid : %d)", pid);
427 aul_terminate_pid(pid);
434 extern int aul_kill_pid(int pid);
435 void process_mgr_kill_app(int pid)
437 _D ("process_mgr_kill_app [pid:%d]..", pid);
443 int process_mgr_validate_app(int pid)
445 char buf[BUF_SIZE_128] = {0, };
447 /* Check pid is invalid. */
448 if (aul_app_get_pkgname_bypid(pid, buf, sizeof(buf)) < 0) {
449 _D("no such pkg by pid %d\n", pid);
451 _SECURE_D("appid = %s, pid = %d\n", buf, pid);
452 if (aul_app_is_running(buf) == TRUE) {
453 _D("%s [pid = %d] is running\n", buf, pid);
456 _SECURE_D("[pid = %d] is exist but %s is not running\n", pid, buf);
465 int process_mgr_validate_call(int pid)
467 char buf[BUF_SIZE_128] = {0, };
469 /* Check pid is invalid. */
470 if (aul_app_get_pkgname_bypid(pid, buf, sizeof(buf)) < 0) {
471 _D("no such pkg by pid %d", pid);
473 _SECURE_D("appid = %s, pid = %d", buf, pid);
474 if ((!strncmp(buf, LOCKD_VOICE_CALL_PKG_NAME, strlen(buf)))
475 || (!strncmp(buf, LOCKD_VIDEO_CALL_PKG_NAME, strlen(buf)))) {