ce4c42e0ff0a7ebe42478d2e6ea878ea7821a0b2
[platform/core/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 const char *__appid = NULL;
40 static const char *__pkgid = NULL;
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,
50                         strlen(appid));
51
52         if (ret > 0)
53                 return true;
54
55         return 0;
56 }
57
58 SLPAPI int aul_app_get_running_app_info(aul_app_info_iter_fn enum_fn,
59                                         void *user_param)
60 {
61         app_pkt_t *pkt = NULL;
62         char *saveptr1, *saveptr2;
63         char *token;
64         char *pkt_data;
65         aul_app_info info;
66
67         memset(&info, 0, sizeof(info));
68         if (enum_fn == NULL)
69                 return AUL_R_EINVAL;
70
71         pkt = __app_send_cmd_with_result(AUL_UTIL_PID, APP_RUNNING_INFO, NULL, 0);
72
73         if (pkt == NULL)
74                 return AUL_R_ERROR;
75
76         for ( pkt_data = (char *)pkt->data; ; pkt_data = NULL) {
77                 token = strtok_r(pkt_data, ";", &saveptr1);
78                 if (token == NULL)
79                         break;
80                 info.pid = atoi(strtok_r(token, ":", &saveptr2));
81                 info.appid = strtok_r(NULL, ":", &saveptr2);
82                 info.app_path = strtok_r(NULL, ":", &saveptr2);
83                 info.pkg_name = strdup(info.appid);
84
85                 enum_fn(&info, user_param);
86                 free(info.pkg_name);
87         }
88
89         free(pkt);
90
91         return AUL_R_OK;
92 }
93
94 SLPAPI void aul_set_preinit_appid(const char *appid)
95 {
96         __appid = appid;
97 }
98
99 SLPAPI void aul_set_preinit_pkgid(const char *pkgid)
100 {
101         __pkgid = pkgid;
102 }
103
104 static int __get_info_bypid(int pid, char *appid, int len, int cmd)
105 {
106         char *result;
107         app_info_from_db *menu_info;
108         uid_t uid;
109         result = __proc_get_appid_bypid(pid);
110         if (result == NULL)
111                 return -1;
112         uid = __proc_get_usr_bypid(pid);
113
114         if ((menu_info = _get_app_info_from_db_by_appid_user(result, uid)) == NULL) {
115                 free(result);
116                 return -1;
117         } else {
118                 if (cmd == APP_GET_APPID_BYPID)
119                         snprintf(appid, len, "%s", _get_appid(menu_info));
120                 else
121                         snprintf(appid, len, "%s", _get_pkgid(menu_info));
122         }
123
124         free(result);
125         _free_app_info_from_db(menu_info);
126
127         return 0;
128 }
129
130 SLPAPI int aul_app_get_pkgname_bypid(int pid, char *pkgname, int len)
131 {
132         return aul_app_get_appid_bypid(pid, pkgname, len);
133 }
134
135 static int __get_appid_bypid(int pid, char *appid, int len)
136 {
137         char *result;
138
139         result = __proc_get_appid_bypid(pid);
140         if (result == NULL)
141                 return -1;
142
143         snprintf(appid, len, "%s", result);
144         free(result);
145
146         return 0;
147 }
148
149 SLPAPI int aul_app_get_appid_bypid_for_uid(int pid, char *appid, int len, uid_t uid)
150 {
151         app_pkt_t *pkt;
152         int pgid;
153         int ret;
154
155         if (pid != getpid()) {
156                 pkt = __app_send_cmd_with_result_for_uid(AUL_UTIL_PID, uid,
157                         APP_GET_APPID_BYPID, (unsigned char *)&pid,
158                         sizeof(pid));
159                 if (pkt == NULL)
160                         return AUL_R_ERROR;
161                 if (pkt->cmd == APP_GET_INFO_ERROR) {
162                         free(pkt);
163                         return AUL_R_ERROR;
164                 }
165
166                 snprintf(appid, len, "%s", pkt->data);
167                 free(pkt);
168                 return AUL_R_OK;
169         } else {
170                 if (__appid) {
171                         snprintf(appid, len, "%s", __appid);
172                         return AUL_R_OK;
173                 }
174
175                 ret = __get_appid_bypid(pid, appid, len);
176                 if (ret == 0)
177                         return AUL_R_OK;
178
179                 pgid = getpgid(pid);
180                 if (pgid <= 1)
181                         return AUL_R_ERROR;
182                 return __get_appid_bypid(pid, appid, len);
183         }
184
185         return AUL_R_ERROR;
186 }
187
188 SLPAPI int aul_app_get_appid_bypid(int pid, char *appid, int len)
189 {
190         return aul_app_get_appid_bypid_for_uid(pid, appid, len, getuid());
191 }
192
193 SLPAPI int aul_app_get_pkgid_bypid_for_uid(int pid, char *pkgid, int len, uid_t uid)
194 {
195         app_pkt_t *pkt = NULL;
196         int pgid;
197         int cmd = APP_GET_PKGID_BYPID;
198         int cpid = getpid();
199
200         if (pid == cpid && __pkgid) {
201                 snprintf(pkgid, len, "%s", __pkgid);
202                 return AUL_R_OK;
203         }
204
205         if (pid == cpid || getuid() == 0 || geteuid() == 0) {
206                 if (__get_info_bypid(pid, pkgid, len, cmd) == 0) {
207                         SECURE_LOGD("pkgid for %d is %s", pid, pkgid);
208                         return AUL_R_OK;
209                 }
210                 /* support app launched by shell script*/
211
212                 pgid = getpgid(pid);
213                 if (pgid <= 1)
214                         return AUL_R_ERROR;
215
216                 _D("second change pgid = %d, pid = %d", pgid, pid);
217                 if (__get_info_bypid(pgid, pkgid, len, cmd) == 0)
218                         return AUL_R_OK;
219
220                 return AUL_R_ERROR;
221         }
222
223         if (pkgid == NULL)
224                 return AUL_R_EINVAL;
225
226         pkt = __app_send_cmd_with_result_for_uid(AUL_UTIL_PID, uid, cmd,
227                                         (unsigned char *)&pid, sizeof(pid));
228
229         if (pkt == NULL)
230                 return AUL_R_ERROR;
231         if (pkt->cmd == APP_GET_INFO_ERROR) {
232                 free(pkt);
233                 return AUL_R_ERROR;
234         }
235
236         snprintf(pkgid, len, "%s", pkt->data);
237         free(pkt);
238         return AUL_R_OK;
239 }
240
241 SLPAPI int aul_app_get_pkgid_bypid(int pid, char *pkgid, int len)
242 {
243         return aul_app_get_pkgid_bypid_for_uid(pid, pkgid, len, getuid());
244 }
245