tizen 2.4 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 #define _GNU_SOURCE
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <pkgmgr-info.h>
28 #include <pkgmgrinfo_zone.h>
29 #include "aul.h"
30 #include "aul_api.h"
31 #include "menu_db_util.h"
32 #include "simple_util.h"
33 #include "app_sock.h"
34 #include "aul_util.h"
35 #include "aul_zone.h"
36
37 #define METADATA_LEGACY_LIFECYCLE "http://developer.samsung.com/tizen/metadata/legacylifecycle"
38
39 static const char *__appid = NULL;
40 static const char *__pkgid = NULL;
41 static int __aul_support_legacy_lifecycle = -1;
42
43 static int __get_pkgname_bypid(int pid, char *pkgname, int len);
44
45 SLPAPI int aul_app_get_pid(const char *appid)
46 {
47         int ret = 0;
48
49         if (appid == NULL)
50                 return 0;
51
52         ret = __app_send_raw(AUL_UTIL_PID, APP_GET_PID, (unsigned char *)appid, strlen(appid));
53
54         return ret;
55 }
56
57 SLPAPI int aul_app_get_pid_cache(const char *appid)
58 {
59         int ret = 0;
60
61         if (appid == NULL)
62                 return 0;
63
64         ret = __app_send_raw(AUL_UTIL_PID, APP_GET_PID_CACHE, (unsigned char *)appid, strlen(appid));
65
66         return ret;
67 }
68
69 SLPAPI int aul_app_get_last_caller_pid(int pid)
70 {
71         int ret = 0;
72
73         if (pid < 0)
74                 return 0;
75
76         ret = __app_send_raw(AUL_UTIL_PID, APP_GET_LAST_CALLER_PID, (unsigned char *)&pid, sizeof(pid));
77
78         return ret;
79 }
80
81 SLPAPI int aul_app_is_running(const char *appid)
82 {
83         int ret = 0;
84
85         if (appid == NULL)
86                 return 0;
87
88         ret = __app_send_raw(AUL_UTIL_PID, APP_IS_RUNNING, (unsigned char*)appid, strlen(appid));
89
90         if(ret > 0)
91                 return true;
92
93         return 0;
94 }
95
96 SLPAPI int aul_app_get_running_app_info(aul_app_info_iter_fn iter_fn,
97                                         void *user_param)
98 {
99         return aul_get_running_app_info_from_memory(iter_fn, user_param);
100 }
101
102 SLPAPI int aul_get_running_app_info_from_proc(aul_app_info_iter_fn iter_fn,
103                                         void *user_param)
104 {
105         app_pkt_t *pkt = NULL;
106         char *saveptr1, *saveptr2;
107         char *token;
108         char *pkt_data;
109         aul_app_info info;
110
111         memset(&info, 0, sizeof(info));
112         if (iter_fn == NULL)
113                 return AUL_R_EINVAL;
114
115         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_RUNNING_INFO, NULL, 0);
116
117         if (pkt == NULL)
118                 return AUL_R_ERROR;
119
120         for( pkt_data = (char *)pkt->data; ; pkt_data = NULL) {
121                 token = strtok_r(pkt_data, ";", &saveptr1);
122                 if (token == NULL)
123                         break;
124                 info.pid = atoi(strtok_r(token, ":", &saveptr2));
125                 info.appid = strtok_r(NULL, ":", &saveptr2);
126                 info.app_path = strtok_r(NULL, ":", &saveptr2);
127                 info.pkg_name = strdup(info.appid);
128
129                 iter_fn(&info, user_param);
130                 free(info.pkg_name);
131         }
132
133         free(pkt);
134
135         return AUL_R_OK;
136 }
137
138 SLPAPI int aul_get_running_app_info_from_memory(aul_app_info_iter_fn iter_fn,
139                                         void *user_param)
140 {
141         app_pkt_t *pkt = NULL;
142         char *saveptr1, *saveptr2;
143         char *token;
144         char *pkt_data;
145         aul_app_info info;
146
147         memset(&info, 0, sizeof(info));
148         if (iter_fn == NULL)
149                 return AUL_R_EINVAL;
150
151         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_RUNNING_INFO_MEMORY, NULL, 0);
152
153         if (pkt == NULL)
154                 return AUL_R_ERROR;
155
156         for( pkt_data = (char *)pkt->data; ; pkt_data = NULL) {
157                 token = strtok_r(pkt_data, ";", &saveptr1);
158                 if (token == NULL)
159                         break;
160                 info.pid = atoi(strtok_r(token, ":", &saveptr2));
161                 info.appid = strtok_r(NULL, ":", &saveptr2);
162                 info.app_path = strtok_r(NULL, ":", &saveptr2);
163                 info.pkg_name = strdup(info.appid);
164
165                 iter_fn(&info, user_param);
166                 free(info.pkg_name);
167         }
168
169         free(pkt);
170
171         return AUL_R_OK;
172 }
173
174 SLPAPI void aul_set_preinit_appid(const char *appid)
175 {
176         __appid = (char *)appid;
177 }
178
179 static const char* __aul_get_preinit_appid(void)
180 {
181         return __appid;
182 }
183
184 SLPAPI void aul_set_preinit_pkgid(const char *pkgid)
185 {
186         __pkgid = (char *)pkgid;
187 }
188
189 SLPAPI const char* aul_get_preinit_pkgid(void)
190 {
191         return __pkgid;
192 }
193
194 static int __get_pkgname_bypid(int pid, char *pkgname, int len)
195 {
196         char *cmdline;
197         app_info_from_db *menu_info;
198
199         cmdline = __proc_get_cmdline_bypid(pid);
200         if (cmdline == NULL)
201                 return -1;
202
203         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL) {
204                 free(cmdline);
205                 return -1;
206         } else
207                 snprintf(pkgname, len, "%s", _get_pkgname(menu_info));
208
209         free(cmdline);
210         _free_app_info_from_db(menu_info);
211         return 0;
212 }
213
214 SLPAPI int aul_app_get_pkgname_bypid(int pid, char *pkgname, int len)
215 {
216         return aul_app_get_appid_bypid(pid, pkgname, len);
217 }
218
219 SLPAPI int aul_app_get_appid_bypid(int pid, char *appid, int len)
220 {
221         app_pkt_t *pkt = NULL;
222         int pgid;
223
224         char oldZone[64] = { 0, };
225         pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
226
227         if (_cur_zone == NULL) {
228                 if (pid == getpid()) {
229                         const char *preinit_appid = __aul_get_preinit_appid();
230
231                         if (preinit_appid != NULL)
232                         {
233                                 snprintf(appid, len > MAX_PACKAGE_STR_SIZE ? MAX_PACKAGE_STR_SIZE : len, "%s", preinit_appid);
234                                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
235                                 return AUL_R_OK;
236                         }
237                 }
238
239                 if (pid == getpid() || getuid()==0 || geteuid()==0) {
240                         if (__get_pkgname_bypid(pid, appid, len) == 0) {
241                                 SECURE_LOGD("appid for %d is %s", pid, appid);
242                                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
243                                 return AUL_R_OK;
244                         }
245                         /* support app launched by shell script*/
246                         if (pid <= 0)
247                                 return AUL_R_ERROR;
248
249                         pgid = getpgid(pid);
250                         if (pgid <= 1) {
251                                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
252                                 return AUL_R_ERROR;
253                         }
254
255                         _D("second change pgid = %d, pid = %d", pgid, pid);
256                         if (__get_pkgname_bypid(pgid, appid, len) == 0) {
257                                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
258                                 return AUL_R_OK;
259                         }
260
261                         pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
262                         return AUL_R_ERROR;
263                 }
264         }
265
266         if (appid == NULL) {
267                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
268                 return AUL_R_EINVAL;
269         }
270
271         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_GET_APPID_BYPID, (unsigned char *)&pid, sizeof(pid));
272
273         if(pkt == NULL) {
274                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
275                 return AUL_R_ERROR;
276         }
277         if(pkt->cmd == APP_GET_INFO_ERROR) {
278                 free(pkt);
279                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
280                 return AUL_R_ERROR;
281         }
282
283         snprintf(appid, len, "%s", pkt->data);
284         free(pkt);
285         pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
286         return AUL_R_OK;
287 }
288
289 static int __get_pkgid_bypid(int pid, char *pkgid, int len)
290 {
291         char *cmdline;
292         app_info_from_db *menu_info;
293
294         if (pid == getpid()) {
295                 const char *preinit_pkgid = aul_get_preinit_pkgid();
296                 if (preinit_pkgid != NULL) {
297                         snprintf(pkgid, len, "%s", preinit_pkgid);
298                         return AUL_R_OK;
299                 }
300         }
301
302         cmdline = __proc_get_cmdline_bypid(pid);
303         if (cmdline == NULL)
304                 return -1;
305
306         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL) {
307                 free(cmdline);
308                 return -1;
309         } else
310                 snprintf(pkgid, len, "%s", _get_pkgid(menu_info));
311
312         free(cmdline);
313         _free_app_info_from_db(menu_info);
314
315         return 0;
316 }
317
318 SLPAPI int aul_app_get_pkgid_bypid(int pid, char *pkgid, int len)
319 {
320         app_pkt_t *pkt = NULL;
321         int pgid;
322         char oldZone[64] = { 0, };
323
324         pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
325         if(pid == getpid() || getuid()==0 || geteuid()==0) {
326                 if (__get_pkgid_bypid(pid, pkgid, len) == 0) {
327                         SECURE_LOGD("pkgid for %d is %s", pid, pkgid);
328                         pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
329                         return AUL_R_OK;
330                 }
331                 /* support app launched by shell script*/
332
333                 pgid = getpgid(pid);
334                 if (pgid <= 1) {
335                         pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
336                         return AUL_R_ERROR;
337                 }
338
339                 _D("second change pgid = %d, pid = %d", pgid, pid);
340                 if (__get_pkgid_bypid(pgid, pkgid, len) == 0) {
341                         pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
342                         return AUL_R_OK;
343                 }
344
345                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
346                 return AUL_R_ERROR;
347         }
348
349         if (pkgid == NULL) {
350                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
351                 return AUL_R_EINVAL;
352         }
353
354         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_GET_PKGID_BYPID, (unsigned char *)&pid, sizeof(pid));
355
356         if(pkt == NULL) {
357                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
358                 return AUL_R_ERROR;
359         }
360         if(pkt->cmd == APP_GET_INFO_ERROR) {
361                 free(pkt);
362                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
363                 return AUL_R_ERROR;
364         }
365         snprintf(pkgid, len, "%s", pkt->data);
366         free(pkt);
367         pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
368
369         return AUL_R_OK;
370 }
371
372 SLPAPI int aul_app_get_cmdline_bypid(int pid, char *cmdline, int len)
373 {
374         app_pkt_t *pkt = NULL;
375
376         if (cmdline == NULL)
377                 return AUL_R_EINVAL;
378
379         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_GET_CMDLINE, (unsigned char *)&pid, sizeof(pid));
380
381         if(pkt == NULL)
382                 return AUL_R_ERROR;
383         if(pkt->cmd == APP_GET_INFO_ERROR) {
384                 free(pkt);
385                 return AUL_R_ERROR;
386         }
387
388         snprintf(cmdline, len, "%s", pkt->data);
389         _D("cmdline : %s", cmdline);
390         free(pkt);
391         return AUL_R_OK;
392 }
393
394 SLPAPI int aul_get_support_legacy_lifecycle(void)
395 {
396         char oldZone[64] = { 0, };
397
398         pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
399         if (__aul_support_legacy_lifecycle != -1) {
400                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
401                 return __aul_support_legacy_lifecycle;
402         }
403
404         int ret = 0;
405         pkgmgrinfo_appinfo_h handle = NULL;
406         char *metadata_value = NULL;
407
408         if (!__appid) {
409                 char appid[128] = {0,};
410
411                 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
412                 if (ret != AUL_R_OK) {
413                         _E("Failed to get appid");
414                         return 0;
415                 }
416
417                 __appid = strdup(appid);
418         }
419
420         ret = pkgmgrinfo_appinfo_get_appinfo(__appid, &handle);
421         if (ret != PMINFO_R_OK) {
422                 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
423                 return 0;
424         }
425
426         ret = pkgmgrinfo_appinfo_get_metadata_value(handle, METADATA_LEGACY_LIFECYCLE, &metadata_value);
427         if (ret != PMINFO_R_OK) {
428                 __aul_support_legacy_lifecycle = 0;
429         } else {
430                 __aul_support_legacy_lifecycle = 1;
431         }
432
433         pkgmgrinfo_appinfo_destroy_appinfo(handle);
434         pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
435         return __aul_support_legacy_lifecycle;
436 }
437
438 SLPAPI char *aul_get_group_info(void)
439 {
440         app_pkt_t *pkt = NULL;
441         char *result = NULL;
442         int len = 0;
443
444         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_GET_GROUP_INFO, NULL, 0);
445         if (pkt) {
446                 len = strlen((const char *)pkt->data);
447                 result = (char *)malloc(sizeof(char) * (len +1));
448                 if (result == NULL) {
449                         _E("Failed to allocate memory");
450                         free(pkt);
451                         return NULL;
452                 }
453                 strncpy(result, (const char *)pkt->data, len);
454                 result[len] = 0;
455                 free(pkt);
456         }
457
458         return result;
459 }