merge with master
[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 "aul.h"
28 #include "aul_api.h"
29 #include "menu_db_util.h"
30 #include "simple_util.h"
31 #include "app_sock.h"
32 #include "aul_util.h"
33
34 typedef struct _internal_param_t {
35         aul_app_info_iter_fn enum_fn;
36         void *user_param;
37 } internal_param_t;
38
39 static int __get_pkgname_bypid(int pid, char *pkgname, int len);
40
41 SLPAPI int aul_app_is_running(const char *appid)
42 {
43         int ret = 0;
44
45         if (appid == NULL)
46                 return 0;
47
48         ret = __app_send_raw(AUL_UTIL_PID, APP_IS_RUNNING, (unsigned char*)appid, strlen(appid));
49
50         if(ret > 0)
51                 return true;
52
53         return 0;
54 }
55
56 SLPAPI int aul_app_get_running_app_info(aul_app_info_iter_fn enum_fn,
57                                         void *user_param)
58 {
59         app_pkt_t *pkt = NULL;
60         char *saveptr1, *saveptr2;
61         char *token;
62         char *pkt_data;
63         aul_app_info info;
64
65         memset(&info, 0, sizeof(info));
66         if (enum_fn == NULL)
67                 return AUL_R_EINVAL;
68
69         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_RUNNING_INFO, NULL, 0);
70
71         if (pkt == NULL)
72                 return AUL_R_ERROR;
73
74         for( pkt_data = (char *)pkt->data; ; pkt_data = NULL) {
75                 token = strtok_r(pkt_data, ";", &saveptr1);
76                 if (token == NULL)
77                         break;
78                 info.pid = atoi(strtok_r(token, ":", &saveptr2));
79                 info.appid = strtok_r(NULL, ":", &saveptr2);
80                 info.app_path = strtok_r(NULL, ":", &saveptr2);
81                 info.pkg_name = strdup(info.appid);
82
83                 enum_fn(&info, user_param);
84         }
85
86         free(info.pkg_name);
87         free(pkt);
88
89         return AUL_R_OK;
90 }
91
92 static int __get_pkgname_bypid(int pid, char *pkgname, int len)
93 {
94         char *cmdline;
95         app_info_from_db *menu_info;
96
97         cmdline = __proc_get_cmdline_bypid(pid);
98         if (cmdline == NULL)
99                 return -1;
100
101         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL) {
102                 free(cmdline);
103                 return -1;
104         } else
105                 snprintf(pkgname, len, "%s", _get_pkgname(menu_info));
106
107         free(cmdline);
108         _free_app_info_from_db(menu_info);
109
110         return 0;
111 }
112
113 SLPAPI int aul_app_get_pkgname_bypid(int pid, char *pkgname, int len)
114 {
115         return aul_app_get_appid_bypid(pid, pkgname, len);
116 }
117
118 SLPAPI int aul_app_get_appid_bypid(int pid, char *appid, int len)
119 {
120         app_pkt_t *pkt = NULL;
121         int pgid;
122
123         if(pid == getpid() || getuid()==0) {
124                 if (__get_pkgname_bypid(pid, appid, len) == 0) {
125                         _D("appid for %d is %s", pid, appid);
126                         return AUL_R_OK;
127                 }
128                 /* support app launched by shell script*/
129                 _D("second chance");
130                 pgid = getpgid(pid);
131                 if (pgid <= 1)
132                         return AUL_R_ERROR;
133
134                 _D("second change pgid = %d, pid = %d", pgid, pid);
135                 if (__get_pkgname_bypid(pgid, appid, len) == 0)
136                         return AUL_R_OK;
137
138                 return AUL_R_ERROR;
139         }
140
141         if (appid == NULL)
142                 return AUL_R_EINVAL;
143
144         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_GET_APPID_BYPID, &pid, sizeof(pid));
145
146         if(pkt == NULL)
147                 return AUL_R_ERROR;
148         if(pkt->cmd == APP_GET_APPID_BYPID_ERROR) {
149                 free(pkt);
150                 return AUL_R_ERROR;
151         }
152
153         snprintf(appid, len, "%s", pkt->data);
154         free(pkt);
155         return AUL_R_OK;
156 }