Tizen 2.1 base
[framework/appfw/aul-1.git] / am_daemon / amd_status.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 #include <stdlib.h>
23 #include <glib.h>
24 #include <aul.h>
25 #include <string.h>
26
27 #include "amd_config.h"
28 #include "amd_status.h"
29 #include "amd_appinfo.h"
30 #include "aul_util.h"
31 #include "simple_util.h"
32 #include "app_sock.h"
33 #include "menu_db_util.h"
34
35 GSList *app_status_info_list = NULL;
36
37 struct appinfomgr *_saf = NULL;
38
39 int _status_add_app_info_list(char *appid, char *app_path, int pid)
40 {
41         GSList *iter = NULL;
42         app_status_info_t *info_t = NULL;
43
44         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
45         {
46                 info_t = (app_status_info_t *)iter->data;
47                 if(pid == info_t->pid) {
48                         return 0;
49                 }
50         }
51
52         info_t = malloc(sizeof(app_status_info_t));
53         strncpy(info_t->appid, appid, MAX_PACKAGE_STR_SIZE-1);
54         strncpy(info_t->app_path, app_path, MAX_PACKAGE_APP_PATH_SIZE-1);
55         info_t->status = STATUS_LAUNCHING;
56         info_t->pid = pid;
57         app_status_info_list = g_slist_append(app_status_info_list, info_t);
58
59         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
60         {
61                 info_t = (app_status_info_t *)iter->data;
62
63                 _D("%s, %d, %d", info_t->appid, info_t->pid, info_t->status);
64         }
65
66         return 0;
67 }
68
69 int _status_update_app_info_list(int pid, int status)
70 {
71         GSList *iter = NULL;
72         app_status_info_t *info_t = NULL;
73
74         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
75         {
76                 info_t = (app_status_info_t *)iter->data;
77                 if(pid == info_t->pid) {
78                         info_t->status = status;
79                         break;
80                 }
81         }
82
83         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
84         {
85                 info_t = (app_status_info_t *)iter->data;
86
87                 _D("%s, %d, %d", info_t->appid, info_t->pid, info_t->status);
88         }
89
90         return 0;
91 }
92
93 int _status_remove_app_info_list(int pid)
94 {
95         GSList *iter = NULL;
96         app_status_info_t *info_t = NULL;
97
98         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
99         {
100                 info_t = (app_status_info_t *)iter->data;
101                 if(pid == info_t->pid) {
102                         app_status_info_list = g_slist_remove(app_status_info_list, info_t);
103                         free(info_t);
104                         break;
105                 }
106         }
107
108         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
109         {
110                 info_t = (app_status_info_t *)iter->data;
111
112                 _D("%s, %d, %d", info_t->appid, info_t->pid, info_t->status);
113         }
114
115         return 0;
116 }
117
118 int _status_app_is_running(char *appid)
119 {
120         GSList *iter = NULL;
121         app_status_info_t *info_t = NULL;
122
123         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
124         {
125                 info_t = (app_status_info_t *)iter->data;
126                 if( strncmp(appid, info_t->appid, MAX_PACKAGE_STR_SIZE) == 0 ) {
127                         return info_t->pid;
128                 }
129         }
130         return -1;
131 }
132
133 int _status_send_running_appinfo(int fd)
134 {
135         GSList *iter = NULL;
136         app_status_info_t *info_t = NULL;
137         app_pkt_t *pkt = NULL;
138         int len;
139         char tmp_pid[MAX_PID_STR_BUFSZ];
140
141         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
142         if(!pkt) {
143                 _E("malloc fail");
144                 return 0;
145         }
146
147         memset(pkt, 0, AUL_SOCK_MAXBUFF);
148
149         for (iter = app_status_info_list; iter != NULL; iter = g_slist_next(iter))
150         {
151                 info_t = (app_status_info_t *)iter->data;
152                 snprintf(tmp_pid, MAX_PID_STR_BUFSZ, "%d", info_t->pid);
153                 strncat(pkt->data, tmp_pid, MAX_PID_STR_BUFSZ);
154                 strncat(pkt->data, ":", 1);
155                 strncat(pkt->data, info_t->appid, MAX_PACKAGE_STR_SIZE);
156                 strncat(pkt->data, ":", 1);
157                 strncat(pkt->data, info_t->app_path, MAX_PACKAGE_APP_PATH_SIZE);
158                 strncat(pkt->data, ";", 1);
159         }
160
161         pkt->cmd = APP_RUNNING_INFO_RESULT;
162         pkt->len = strlen((char *)pkt->data) + 1;
163
164         if ((len = send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
165                 if (errno == EPIPE)
166                         _E("send failed due to EPIPE.\n");
167                 _E("send fail to client");
168         }
169
170         if(pkt)
171                 free(pkt);
172
173         close(fd);
174
175         return 0;
176 }
177
178 int _status_app_is_running_v2(char *appid)
179 {
180         char *apppath = NULL;
181         int ret = 0;
182         int i = 0;
183         struct appinfo *ai;
184
185         if(appid == NULL)
186                 return -1;
187
188         ai = appinfo_find(_saf, appid);
189
190         if(ai == NULL)
191                 return -1;
192
193         apppath = strdup(appinfo_get_value(ai, AIT_EXEC));
194
195         /*truncate apppath if it includes default bundles */
196         while (apppath[i] != 0) {
197                 if (apppath[i] == ' ' || apppath[i] == '\t') {
198                         apppath[i]='\0';
199                         break;
200                 }
201                 i++;
202         }
203
204         ret = __proc_iter_cmdline(NULL, apppath);
205
206         free(apppath);
207
208         return ret;
209 }
210
211 static int __get_pkginfo(const char *dname, const char *cmdline, void *priv)
212 {
213         app_info_from_db *menu_info;
214         char *r_info;
215
216         r_info = (char *)priv;
217
218         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL)
219                 goto out;
220         else {
221                 strncat(r_info, dname, 8);
222                 strncat(r_info, ":", 1);
223                 strncat(r_info, _get_pkgname(menu_info), MAX_PACKAGE_STR_SIZE);
224                 strncat(r_info, ":", 1);
225                 strncat(r_info, _get_app_path(menu_info), MAX_PACKAGE_APP_PATH_SIZE);
226                 strncat(r_info, ";", 1);
227         }
228
229  out:
230         if (menu_info != NULL)
231                 _free_app_info_from_db(menu_info);
232         return 0;
233 }
234
235 int _status_send_running_appinfo_v2(int fd)
236 {
237         app_pkt_t *pkt = NULL;
238         int len;
239
240         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
241         if(!pkt) {
242                 _E("malloc fail");
243                 close(fd);
244                 return 0;
245         }
246
247         memset(pkt, 0, AUL_SOCK_MAXBUFF);
248
249         __proc_iter_cmdline(__get_pkginfo, pkt->data);
250
251         pkt->cmd = APP_RUNNING_INFO_RESULT;
252         pkt->len = strlen((char *)pkt->data) + 1;
253
254         if ((len = send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
255                 if (errno == EPIPE)
256                         _E("send failed due to EPIPE.\n");
257                 _E("send fail to client");
258         }
259
260         if(pkt)
261                 free(pkt);
262
263         close(fd);
264
265         return 0;
266 }
267
268 int _status_init(struct amdmgr* amd)
269 {
270         _saf = amd->af;
271
272         return 0;
273 }
274