2 * Copyright (c) 2011 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.
26 #include <cynara-client.h>
28 #include "app_manager.h"
29 #include "app_manager_internal.h"
35 #define LOG_TAG "CAPI_APPFW_APP_MANAGER"
37 #define SMACK_LABEL_LEN 255
39 static const char* app_manager_error_to_string(app_manager_error_e error)
42 case APP_MANAGER_ERROR_NONE:
44 case APP_MANAGER_ERROR_INVALID_PARAMETER:
45 return "Invalid parameter";
46 case APP_MANAGER_ERROR_OUT_OF_MEMORY:
47 return "Out of memory";
48 case APP_MANAGER_ERROR_IO_ERROR:
50 case APP_MANAGER_ERROR_NO_SUCH_APP:
51 return "No such application";
52 case APP_MANAGER_ERROR_DB_FAILED:
54 case APP_MANAGER_ERROR_INVALID_PACKAGE:
55 return "Invalid package";
56 case APP_MANAGER_ERROR_NOT_SUPPORTED:
57 return "Not supported";
58 case APP_MANAGER_ERROR_PERMISSION_DENIED:
59 return "Permission denied";
65 int app_manager_error(app_manager_error_e error, const char* function, const char *description)
68 LOGE("[%s] %s(0x%08x) : %s", function, app_manager_error_to_string(error), error, description);
70 LOGE("[%s] %s(0x%08x)", function, app_manager_error_to_string(error), error);
75 int app_manager_check_privilege(char *privilege)
81 char client[SMACK_LABEL_LEN + 1] = "";
83 char *client_session = "";
85 if (privilege == NULL) {
86 LOGE("invalid parameter");
87 return APP_MANAGER_ERROR_INVALID_PARAMETER;
90 ret = cynara_initialize(&p_cynara, NULL);
91 if (ret != CYNARA_API_SUCCESS) {
92 LOGE("cynara_initialize [%d] failed!", ret);
93 return APP_MANAGER_ERROR_IO_ERROR;
96 fd = open("/proc/self/attr/current", O_RDONLY);
98 LOGE("open [%d] failed!", errno);
99 ret = APP_MANAGER_ERROR_IO_ERROR;
103 ret = read(fd, client, SMACK_LABEL_LEN);
105 LOGE("read [%d] failed!", errno);
107 ret = APP_MANAGER_ERROR_IO_ERROR;
113 snprintf(uid, 10, "%d", getuid());
115 ret = cynara_check(p_cynara, client, client_session, uid, privilege);
116 if (ret != CYNARA_API_ACCESS_ALLOWED) {
117 LOGE("cynara access check [%d] failed!", ret);
119 if (ret == CYNARA_API_ACCESS_DENIED)
120 ret = APP_MANAGER_ERROR_PERMISSION_DENIED;
122 ret = APP_MANAGER_ERROR_IO_ERROR;
127 ret = APP_MANAGER_ERROR_NONE;
131 cynara_finish(p_cynara);
136 API int app_manager_set_app_context_event_cb(app_manager_app_context_event_cb callback, void *user_data)
138 int retval = app_context_set_event_cb(callback, user_data);
140 if (retval != APP_MANAGER_ERROR_NONE)
141 return app_manager_error(retval, __FUNCTION__, NULL);
143 return APP_MANAGER_ERROR_NONE;
146 API void app_manager_unset_app_context_event_cb(void)
148 app_context_unset_event_cb();
151 API int app_manager_foreach_app_context(app_manager_app_context_cb callback, void *user_data)
153 int retval = app_context_foreach_app_context(callback, user_data);
155 if (retval != APP_MANAGER_ERROR_NONE)
156 return app_manager_error(retval, __FUNCTION__, NULL);
158 return APP_MANAGER_ERROR_NONE;
161 API int app_manager_foreach_running_app_context(app_manager_app_context_cb callback, void *user_data)
163 int retval = app_context_foreach_running_app_context(callback, user_data);
165 if (retval != APP_MANAGER_ERROR_NONE)
166 return app_manager_error(retval, __FUNCTION__, NULL);
168 return APP_MANAGER_ERROR_NONE;
171 API int app_manager_get_app_context(const char *app_id, app_context_h *app_context)
173 int retval = app_context_get_app_context(app_id, app_context);
175 if (retval != APP_MANAGER_ERROR_NONE)
176 return app_manager_error(retval, __FUNCTION__, NULL);
178 return APP_MANAGER_ERROR_NONE;
181 API int app_manager_resume_app(app_context_h app_context)
184 int retval = APP_MANAGER_ERROR_NONE;
186 if (app_context == NULL)
187 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
189 if (app_context_get_app_id(app_context, &app_id) != APP_MANAGER_ERROR_NONE)
190 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the application ID");
192 if (aul_app_is_running(app_id) == 0) {
197 return app_manager_error(APP_MANAGER_ERROR_APP_NO_RUNNING, __FUNCTION__, NULL);
200 retval = aul_resume_app(app_id);
205 if (retval == AUL_R_EINVAL)
206 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
207 else if (retval == AUL_R_EILLACC)
208 return app_manager_error(APP_MANAGER_ERROR_PERMISSION_DENIED, __FUNCTION__, NULL);
210 return app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
212 return APP_MANAGER_ERROR_NONE;
215 API int app_manager_foreach_app_info(app_manager_app_info_cb callback, void *user_data)
219 retval = app_info_foreach_app_info(callback, user_data);
221 if (retval != APP_MANAGER_ERROR_NONE)
222 return app_manager_error(retval, __FUNCTION__, NULL);
224 return APP_MANAGER_ERROR_NONE;
227 API int app_manager_get_app_info(const char *app_id, app_info_h *app_info)
231 retval = app_info_create(app_id, app_info);
233 if (retval != APP_MANAGER_ERROR_NONE)
234 return app_manager_error(retval, __FUNCTION__, NULL);
236 return APP_MANAGER_ERROR_NONE;
239 API int app_manager_get_app_id(pid_t pid, char **app_id)
241 char buffer[256] = {0, };
242 char *app_id_dup = NULL;
245 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
247 if (aul_app_get_appid_bypid(pid, buffer, sizeof(buffer)) != AUL_R_OK)
248 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "Invalid process ID");
250 app_id_dup = strdup(buffer);
251 if (app_id_dup == NULL)
252 return app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
254 *app_id = app_id_dup;
256 return APP_MANAGER_ERROR_NONE;
259 API int app_manager_terminate_app(app_context_h app_context)
264 if (app_context == NULL)
265 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
267 if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE)
268 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
270 retval = aul_terminate_pid(pid);
271 if (retval == AUL_R_EINVAL) {
272 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
273 return APP_MANAGER_ERROR_INVALID_PARAMETER;
274 } else if (retval == AUL_R_EILLACC) {
275 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
276 return APP_MANAGER_ERROR_PERMISSION_DENIED;
277 } else if (retval < 0) {
278 return APP_MANAGER_ERROR_REQUEST_FAILED;
281 return APP_MANAGER_ERROR_NONE;
284 API int app_manager_request_terminate_bg_app(app_context_h app_context)
286 int retval = APP_MANAGER_ERROR_NONE;
289 if (app_context == NULL)
290 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
292 if (app_context_get_pid(app_context, &pid) != APP_MANAGER_ERROR_NONE)
293 return app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, "failed to get the process ID");
295 retval = aul_terminate_bgapp_pid(pid);
296 if (retval == AUL_R_EINVAL) {
297 LOGE("[%s] APP_MANAGER_ERROR_INVALID_PARAMETER(0x%08x) : Invalid param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
298 return APP_MANAGER_ERROR_INVALID_PARAMETER;
299 } else if (retval == AUL_R_EILLACC) {
300 LOGE("[%s] APP_MANAGER_ERROR_PERMISSION_DENIED(0x%08x) : Permission denied", __FUNCTION__, APP_MANAGER_ERROR_PERMISSION_DENIED);
301 return APP_MANAGER_ERROR_PERMISSION_DENIED;
302 } else if (retval < 0) {
303 return APP_MANAGER_ERROR_REQUEST_FAILED;
306 return APP_MANAGER_ERROR_NONE;
309 API int app_manager_is_running(const char *app_id, bool *running)
311 if (app_id == NULL) {
312 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid package", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
313 return APP_MANAGER_ERROR_INVALID_PARAMETER;
316 if (running == NULL) {
317 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid output param", __FUNCTION__, APP_MANAGER_ERROR_INVALID_PARAMETER);
318 return APP_MANAGER_ERROR_INVALID_PARAMETER;
321 *running = aul_app_is_running(app_id);
323 return APP_MANAGER_ERROR_NONE;
326 API int app_manager_get_shared_data_path(const char *app_id, char **path)
329 int retval = aul_get_app_shared_data_path_by_appid(app_id, path);
333 r = APP_MANAGER_ERROR_NONE;
336 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
339 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
342 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
344 case AUL_R_EREJECTED:
345 r = app_manager_error(APP_MANAGER_ERROR_NOT_SUPPORTED, __FUNCTION__, NULL);
348 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
355 API int app_manager_get_shared_resource_path(const char *app_id, char **path)
358 int retval = aul_get_app_shared_resource_path_by_appid(app_id, path);
362 r = APP_MANAGER_ERROR_NONE;
365 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
368 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
371 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
374 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
381 API int app_manager_get_shared_trusted_path(const char *app_id, char **path)
384 int retval = aul_get_app_shared_trusted_path_by_appid(app_id, path);
388 r = APP_MANAGER_ERROR_NONE;
391 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
394 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
397 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
400 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);
407 API int app_manager_get_external_shared_data_path(const char *app_id, char **path)
410 int retval = aul_get_app_external_shared_data_path_by_appid(app_id, path);
414 r = APP_MANAGER_ERROR_NONE;
417 r = app_manager_error(APP_MANAGER_ERROR_NO_SUCH_APP, __FUNCTION__, NULL);
420 r = app_manager_error(APP_MANAGER_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
423 r = app_manager_error(APP_MANAGER_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
426 r = app_manager_error(APP_MANAGER_ERROR_REQUEST_FAILED, __FUNCTION__, NULL);