tizen 2.3 release
[framework/appfw/aul-1.git] / src / pkginfo.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <pkgmgr-info.h>
28 #include "aul.h"
29 #include "aul_api.h"
30 #include "menu_db_util.h"
31 #include "simple_util.h"
32 #include "app_sock.h"
33 #include "aul_util.h"
34
35 #define METADATA_LEGACY_LIFECYCLE "http://developer.samsung.com/tizen/metadata/legacylifecycle"
36
37 static char *__appid = NULL;
38 static int __aul_support_legacy_lifecycle = -1;
39
40 static int __get_pkgname_bypid(int pid, char *pkgname, int len);
41
42 SLPAPI int aul_app_is_running(const char *appid)
43 {
44         int ret = 0;
45
46         if (appid == NULL)
47                 return 0;
48
49         ret = __app_send_raw(AUL_UTIL_PID, APP_IS_RUNNING, (unsigned char*)appid, strlen(appid));
50
51         if(ret > 0)
52                 return true;
53
54         return 0;
55 }
56
57 SLPAPI int aul_app_get_running_app_info(aul_app_info_iter_fn iter_fn,
58                                         void *user_param)
59 {
60         return aul_get_running_app_info_from_memory(iter_fn, user_param);
61 }
62
63 SLPAPI int aul_get_running_app_info_from_proc(aul_app_info_iter_fn iter_fn,
64                                         void *user_param)
65 {
66         app_pkt_t *pkt = NULL;
67         char *saveptr1, *saveptr2;
68         char *token;
69         char *pkt_data;
70         aul_app_info info;
71
72         memset(&info, 0, sizeof(info));
73         if (iter_fn == NULL)
74                 return AUL_R_EINVAL;
75
76         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_RUNNING_INFO, NULL, 0);
77
78         if (pkt == NULL)
79                 return AUL_R_ERROR;
80
81         for( pkt_data = (char *)pkt->data; ; pkt_data = NULL) {
82                 token = strtok_r(pkt_data, ";", &saveptr1);
83                 if (token == NULL)
84                         break;
85                 info.pid = atoi(strtok_r(token, ":", &saveptr2));
86                 info.appid = strtok_r(NULL, ":", &saveptr2);
87                 info.app_path = strtok_r(NULL, ":", &saveptr2);
88                 info.pkg_name = strdup(info.appid);
89
90                 iter_fn(&info, user_param);
91                 free(info.pkg_name);
92         }
93
94         free(pkt);
95
96         return AUL_R_OK;
97 }
98
99 SLPAPI int aul_get_running_app_info_from_memory(aul_app_info_iter_fn iter_fn,
100                                         void *user_param)
101 {
102         app_pkt_t *pkt = NULL;
103         char *saveptr1, *saveptr2;
104         char *token;
105         char *pkt_data;
106         aul_app_info info;
107
108         memset(&info, 0, sizeof(info));
109         if (iter_fn == NULL)
110                 return AUL_R_EINVAL;
111
112         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_RUNNING_INFO_MEMORY, NULL, 0);
113
114         if (pkt == NULL)
115                 return AUL_R_ERROR;
116
117         for( pkt_data = (char *)pkt->data; ; pkt_data = NULL) {
118                 token = strtok_r(pkt_data, ";", &saveptr1);
119                 if (token == NULL)
120                         break;
121                 info.pid = atoi(strtok_r(token, ":", &saveptr2));
122                 info.appid = strtok_r(NULL, ":", &saveptr2);
123                 info.app_path = strtok_r(NULL, ":", &saveptr2);
124                 info.pkg_name = strdup(info.appid);
125
126                 iter_fn(&info, user_param);
127                 free(info.pkg_name);
128         }
129
130         free(pkt);
131
132         return AUL_R_OK;
133 }
134
135 SLPAPI void aul_set_preinit_appid(const char *appid)
136 {
137         __appid = appid;
138 }
139
140 static char* __aul_get_preinit_appid(void)
141 {
142         return __appid;
143 }
144
145 static int __get_pkgname_bypid(int pid, char *pkgname, int len)
146 {
147         char *cmdline;
148         app_info_from_db *menu_info;
149
150         cmdline = __proc_get_cmdline_bypid(pid);
151         if (cmdline == NULL)
152                 return -1;
153
154         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL) {
155                 free(cmdline);
156                 return -1;
157         } else
158                 snprintf(pkgname, len, "%s", _get_pkgname(menu_info));
159
160         free(cmdline);
161         _free_app_info_from_db(menu_info);
162
163         return 0;
164 }
165
166 SLPAPI int aul_app_get_pkgname_bypid(int pid, char *pkgname, int len)
167 {
168         return aul_app_get_appid_bypid(pid, pkgname, len);
169 }
170
171 SLPAPI int aul_app_get_appid_bypid(int pid, char *appid, int len)
172 {
173         app_pkt_t *pkt = NULL;
174         int pgid;
175
176         if (pid == getpid()) {
177                 char *preinit_appid = __aul_get_preinit_appid();
178
179                 if (preinit_appid != NULL)
180                 {
181 #ifdef _APPFW_FEATURE_CONTACT_PHONE_AS_ONE_APP
182                         if(strncmp(preinit_appid, "org.tizen.phone", MAX_PACKAGE_STR_SIZE) == 0) {
183                                 snprintf(appid, len > MAX_PACKAGE_STR_SIZE ? MAX_PACKAGE_STR_SIZE : len, "%s", "org.tizen.contacts");
184                         } else {
185 #endif
186                                 snprintf(appid, len > MAX_PACKAGE_STR_SIZE ? MAX_PACKAGE_STR_SIZE : len, "%s", preinit_appid);
187 #ifdef _APPFW_FEATURE_CONTACT_PHONE_AS_ONE_APP
188                         }
189 #endif
190                         return AUL_R_OK;
191                 }
192         }
193
194         if (pid == getpid() || getuid()==0 || geteuid()==0) {
195                 if (__get_pkgname_bypid(pid, appid, len) == 0) {
196                         SECURE_LOGD("appid for %d is %s", pid, appid);
197                         return AUL_R_OK;
198                 }
199                 /* support app launched by shell script*/
200
201                 pgid = getpgid(pid);
202                 if (pgid <= 1)
203                         return AUL_R_ERROR;
204
205                 _D("second change pgid = %d, pid = %d", pgid, pid);
206                 if (__get_pkgname_bypid(pgid, appid, len) == 0)
207                         return AUL_R_OK;
208
209                 return AUL_R_ERROR;
210         }
211
212         if (appid == NULL)
213                 return AUL_R_EINVAL;
214
215         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_GET_APPID_BYPID, (unsigned char *)&pid, sizeof(pid));
216
217         if(pkt == NULL)
218                 return AUL_R_ERROR;
219         if(pkt->cmd == APP_GET_INFO_ERROR) {
220                 free(pkt);
221                 return AUL_R_ERROR;
222         }
223
224         snprintf(appid, len, "%s", pkt->data);
225         free(pkt);
226         return AUL_R_OK;
227 }
228
229 static int __get_pkgid_bypid(int pid, char *pkgid, int len)
230 {
231         char *cmdline;
232         app_info_from_db *menu_info;
233
234         cmdline = __proc_get_cmdline_bypid(pid);
235         if (cmdline == NULL)
236                 return -1;
237
238         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL) {
239                 free(cmdline);
240                 return -1;
241         } else
242                 snprintf(pkgid, len, "%s", _get_pkgid(menu_info));
243
244         free(cmdline);
245         _free_app_info_from_db(menu_info);
246
247         return 0;
248 }
249
250 SLPAPI int aul_app_get_pkgid_bypid(int pid, char *pkgid, int len)
251 {
252         app_pkt_t *pkt = NULL;
253         int pgid;
254
255         if(pid == getpid() || getuid()==0 || geteuid()==0) {
256                 if (__get_pkgid_bypid(pid, pkgid, len) == 0) {
257                         SECURE_LOGD("appid for %d is %s", pid, pkgid);
258                         return AUL_R_OK;
259                 }
260                 /* support app launched by shell script*/
261
262                 pgid = getpgid(pid);
263                 if (pgid <= 1)
264                         return AUL_R_ERROR;
265
266                 _D("second change pgid = %d, pid = %d", pgid, pid);
267                 if (__get_pkgid_bypid(pgid, pkgid, len) == 0)
268                         return AUL_R_OK;
269
270                 return AUL_R_ERROR;
271         }
272
273         if (pkgid == NULL)
274                 return AUL_R_EINVAL;
275
276         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_GET_APPID_BYPID, (unsigned char *)&pid, sizeof(pid));
277
278         if(pkt == NULL)
279                 return AUL_R_ERROR;
280         if(pkt->cmd == APP_GET_INFO_ERROR) {
281                 free(pkt);
282                 return AUL_R_ERROR;
283         }
284
285         snprintf(pkgid, len, "%s", pkt->data);
286         free(pkt);
287         return AUL_R_OK;
288 }
289
290 SLPAPI int aul_get_support_legacy_lifecycle(void)
291 {
292         if (__aul_support_legacy_lifecycle != -1)
293                 return __aul_support_legacy_lifecycle;
294
295         int ret = 0;
296         pkgmgrinfo_appinfo_h handle = NULL;
297         char *metadata_value = NULL;
298
299         ret = pkgmgrinfo_appinfo_get_appinfo(__appid, &handle);
300         if (ret != PMINFO_R_OK)
301                 return 0;
302
303         ret = pkgmgrinfo_appinfo_get_metadata_value(handle, METADATA_LEGACY_LIFECYCLE, &metadata_value);
304         if (ret != PMINFO_R_OK) {
305                 __aul_support_legacy_lifecycle = 0;
306         } else {
307                 __aul_support_legacy_lifecycle = 1;
308         }
309
310         pkgmgrinfo_appinfo_destroy_appinfo(handle);
311
312         return __aul_support_legacy_lifecycle;
313 }
314