d9b828a426c332de4f4a5b87ac1c0459d971eb13
[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
32 typedef struct _internal_param_t {
33         aul_app_info_iter_fn enum_fn;
34         void *user_param;
35 } internal_param_t;
36
37 static int __get_pkginfo(const char *dname, const char *cmdline, void *priv);
38 static int __get_pkgname_bypid(int pid, char *pkgname, int len);
39
40
41 SLPAPI int aul_app_is_running(const char *pkgname)
42 {
43         char *apppath = NULL;
44         ail_appinfo_h handle;
45         ail_error_e ail_ret;
46
47         int ret = 0;
48         int i = 0;
49
50         if (pkgname == NULL)
51                 return 0;
52
53         ail_ret = ail_package_get_appinfo(pkgname, &handle);
54         if (ail_ret != AIL_ERROR_OK) {
55                 _E("ail_package_get_appinfo with %s failed", pkgname);
56                 return ret;
57         }
58
59         ail_ret = ail_appinfo_get_str(handle, AIL_PROP_EXEC_STR, &apppath);
60         if (ail_ret != AIL_ERROR_OK) {
61                 _E("ail_appinfo_get_str failed");
62                 goto out;
63         }
64         
65         if (apppath == NULL)
66                 goto out;
67
68         /*truncate apppath if it includes default bundles */
69         while (apppath[i] != 0) {
70                 if (apppath[i] == ' ' || apppath[i] == '\t') {
71                         apppath[i]='\0';
72                         break;
73                 }
74                 i++;
75         }
76         
77         if (__proc_iter_cmdline(NULL, apppath) > 0)
78                 ret = 1;
79         else
80                 ret = 0;
81
82  out:
83         if (ail_package_destroy_appinfo(handle) != AIL_ERROR_OK)
84                 _E("ail_destroy_rs failed");
85         return ret;
86 }
87
88 static int __get_pkginfo(const char *dname, const char *cmdline, void *priv)
89 {
90         internal_param_t *p;
91         app_info_from_db *menu_info;
92         aul_app_info info;
93
94         p = (internal_param_t *) priv;
95         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL)
96                 goto out;
97         else {
98                 info.pid = atoi(dname);
99                 info.pkg_name = _get_pkgname(menu_info);
100                 info.app_path = _get_app_path(menu_info);
101                 _D("get pkginfo - %d %s", info.pid, info.app_path);
102                 p->enum_fn(&info, p->user_param);
103         }
104
105  out:
106         if (menu_info != NULL)
107                 _free_app_info_from_db(menu_info);
108         return 0;
109 }
110
111 SLPAPI int aul_app_get_running_app_info(aul_app_info_iter_fn enum_fn,
112                                         void *user_param)
113 {
114         internal_param_t param;
115
116         if (enum_fn == NULL)
117                 return AUL_R_EINVAL;
118
119         param.enum_fn = enum_fn;
120         param.user_param = user_param;
121
122         __proc_iter_cmdline(__get_pkginfo, &param);
123
124         return AUL_R_OK;
125 }
126
127 static int __get_pkgname_bypid(int pid, char *pkgname, int len)
128 {
129         char *cmdline;
130         app_info_from_db *menu_info;
131
132         cmdline = __proc_get_cmdline_bypid(pid);
133         if (cmdline == NULL)
134                 return -1;
135
136         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL) {
137                 free(cmdline);
138                 return -1;
139         } else
140                 snprintf(pkgname, len, "%s", _get_pkgname(menu_info));
141
142         free(cmdline);
143         _free_app_info_from_db(menu_info);
144
145         return 0;
146 }
147
148 SLPAPI int aul_app_get_pkgname_bypid(int pid, char *pkgname, int len)
149 {
150         int pgid;
151
152         if (pkgname == NULL)
153                 return AUL_R_EINVAL;
154
155         if (__get_pkgname_bypid(pid, pkgname, len) == 0) {
156                 _D("Pkg name for %d is %s", pid, pkgname);
157                 return AUL_R_OK;
158         }
159         /* support app launched by shell script*/
160         _D("second chance");
161         pgid = getpgid(pid);
162         if (pgid <= 1)
163                 return AUL_R_ERROR;
164
165         _D("second change pgid = %d, pid = %d", pgid, pid);
166         if (__get_pkgname_bypid(pgid, pkgname, len) == 0)
167                 return AUL_R_OK;
168
169         return AUL_R_ERROR;
170 }
171