37ff8acd13b6b41eb06012cb23c7293d32953e7a
[framework/appfw/aul-1.git] / test / ac_daemon.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 <stdio.h>
23 #include <glib.h>
24 #include <stdbool.h>
25 #include <app-checker-server.h>
26 #include <rua.h>
27 #include <bundle.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <stdlib.h>
34 #include <dlfcn.h>
35 #include <poll.h>
36 #include <ail.h>
37
38 #include "simple_util.h"
39 #include "app_sock.h"
40 #include "aul_util.h"
41 #include "menu_db_util.h"
42
43 static gboolean __add_history_handler(gpointer user_data)
44 {
45         struct rua_rec rec;
46         int ret;
47         app_pkt_t *pkt = (app_pkt_t *)user_data;
48         struct history_data *hd = (struct history_data *)pkt->data;
49
50         memset(&rec, 0, sizeof(rec));
51
52         rec.pkg_name = hd->pkg_name;
53         rec.app_path = hd->app_path;
54
55         if(hd->len > 0) {
56                 rec.arg = (char *)hd->data;
57         }
58
59         _D("add rua history %s %s", rec.pkg_name, rec.app_path);
60
61         ret = rua_add_history(&rec);
62         if (ret == -1)
63                 _D("rua add history error");
64
65         free(pkt);
66         return false;
67 }
68
69 int __send_result_to_client(int fd, int res)
70 {
71         if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
72                 if (errno == EPIPE)
73                         _E("send failed due to EPIPE.\n");
74                 _E("send fail to client");
75         }
76         close(fd);
77         return 0;
78 }
79
80 static int __get_pkginfo(const char *dname, const char *cmdline, void *priv)
81 {
82         app_info_from_db *menu_info;
83         char *r_info;
84
85         r_info = (char *)priv;
86
87         if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL)
88                 goto out;
89         else {
90                 strncat(r_info, dname, 8);
91                 strncat(r_info, ":", 1);
92                 strncat(r_info, _get_pkgname(menu_info), MAX_PACKAGE_STR_SIZE);
93                 strncat(r_info, ":", 1);
94                 strncat(r_info, _get_app_path(menu_info), MAX_PACKAGE_APP_PATH_SIZE);
95                 strncat(r_info, ";", 1);
96         }
97
98  out:
99         if (menu_info != NULL)
100                 _free_app_info_from_db(menu_info);
101         return 0;
102 }
103
104 int __send_running_appinfo(int fd)
105 {
106         app_pkt_t *pkt = NULL;
107         int len;
108
109         pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
110         if(!pkt) {
111                 _E("malloc fail");
112                 return 0;
113         }
114
115         memset(pkt, 0, AUL_SOCK_MAXBUFF);
116
117         __proc_iter_cmdline(__get_pkginfo, pkt->data);
118
119         pkt->cmd = RUNNING_INFO_RESULT;
120         pkt->len = strlen((char *)pkt->data) + 1;
121
122         if ((len = send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
123                 if (errno == EPIPE)
124                         _E("send failed due to EPIPE.\n");
125                 _E("send fail to client");
126         }
127
128         if(pkt)
129                 free(pkt);
130
131         close(fd);
132         return 0;
133 }
134
135 int __app_is_running(const char *pkgname)
136 {
137         char *apppath = NULL;
138         ail_appinfo_h handle;
139         ail_error_e ail_ret;
140
141         int ret = 0;
142         int i = 0;
143
144         if (pkgname == NULL)
145                 return 0;
146
147         ail_ret = ail_package_get_appinfo(pkgname, &handle);
148         if (ail_ret != AIL_ERROR_OK) {
149                 _E("ail_package_get_appinfo with %s failed", pkgname);
150                 return ret;
151         }
152
153         ail_ret = ail_appinfo_get_str(handle, AIL_PROP_EXEC_STR, &apppath);
154         if (ail_ret != AIL_ERROR_OK) {
155                 _E("ail_appinfo_get_str failed");
156                 goto out;
157         }
158
159         if (apppath == NULL)
160                 goto out;
161
162         /*truncate apppath if it includes default bundles */
163         while (apppath[i] != 0) {
164                 if (apppath[i] == ' ' || apppath[i] == '\t') {
165                         apppath[i]='\0';
166                         break;
167                 }
168                 i++;
169         }
170
171         if (__proc_iter_cmdline(NULL, apppath) > 0)
172                 ret = 1;
173         else
174                 ret = 0;
175
176  out:
177         if (ail_package_destroy_appinfo(handle) != AIL_ERROR_OK)
178                 _E("ail_destroy_rs failed");
179         return ret;
180 }
181
182
183 static gboolean __util_handler(gpointer data)
184 {
185         GPollFD *gpollfd = (GPollFD *) data;
186         int fd = gpollfd->fd;
187         app_pkt_t *pkt;
188         int clifd;
189         struct ucred cr;
190         struct history_data *hd;
191         int ret = -1;
192         char pkgname[MAX_PACKAGE_STR_SIZE];
193
194         if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
195                 _E("recv error");
196                 return FALSE;
197         }
198
199         switch (pkt->cmd) {
200         case ADD_HISTORY:
201                 hd = (struct history_data *)pkt->data;
202                 _D("cmd : %d, pkgname : %s, app_path : %s", pkt->cmd, hd->pkg_name, hd->app_path);
203                 __send_result_to_client(clifd, 0);
204                 g_timeout_add(1000, __add_history_handler, pkt);
205                 break;
206         case RUNNING_INFO:
207                 __send_running_appinfo(clifd);
208                 free(pkt);
209                 break;
210         case IS_RUNNING:
211                 strncpy(pkgname, (const char*)pkt->data, MAX_PACKAGE_STR_SIZE-1);
212                 ret = __app_is_running(pkgname);
213                 __send_result_to_client(clifd, ret);
214                 free(pkt);
215                 break;
216         default:
217                 _E("no support packet");
218         }
219
220         return TRUE;
221 }
222
223 static gboolean __au_glib_check(GSource *src)
224 {
225         GSList *fd_list;
226         GPollFD *tmp;
227
228         fd_list = src->poll_fds;
229         do {
230                 tmp = (GPollFD *) fd_list->data;
231                 if ((tmp->revents & (POLLIN | POLLPRI)))
232                         return TRUE;
233                 fd_list = fd_list->next;
234         } while (fd_list);
235
236         return FALSE;
237 }
238
239 static gboolean __au_glib_dispatch(GSource *src, GSourceFunc callback,
240                                   gpointer data)
241 {
242         callback(data);
243         return TRUE;
244 }
245
246 static gboolean __au_glib_prepare(GSource *src, gint *timeout)
247 {
248         return FALSE;
249 }
250
251 static GSourceFuncs funcs = {
252         .prepare = __au_glib_prepare,
253         .check = __au_glib_check,
254         .dispatch = __au_glib_dispatch,
255         .finalize = NULL
256 };
257
258
259 int __initialize()
260 {
261         int fd;
262         int r;
263         GPollFD *gpollfd;
264         GSource *src;
265
266         r = rua_init();
267
268         if (r == -1) {
269                 _D("[APP %d] rua init error");
270                 return AC_R_ERROR;
271         }
272
273         fd = __create_server_sock(AUL_UTIL_PID);
274
275         src = g_source_new(&funcs, sizeof(GSource));
276
277         gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
278         gpollfd->events = POLLIN;
279         gpollfd->fd = fd;
280
281         g_source_add_poll(src, gpollfd);
282         g_source_set_callback(src, (GSourceFunc) __util_handler,
283                               (gpointer) gpollfd, NULL);
284         g_source_set_priority(src, G_PRIORITY_DEFAULT);
285
286         r = g_source_attach(src, NULL);
287         if (r  == 0)
288         {
289                 /* TODO: error handle*/
290                 return AC_R_ERROR;
291         }
292
293         return AC_R_OK;
294 }
295
296 int main()
297 {
298         
299         GMainLoop *mainloop;
300         int ret;
301
302         mainloop = g_main_loop_new(NULL, FALSE);
303
304         ret = ac_server_initailize();
305
306         ret = __initialize();
307
308         g_main_loop_run(mainloop);
309
310         return 0;
311 }