4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <app-checker-server.h>
29 #include <sys/types.h>
38 #include "simple_util.h"
41 #include "menu_db_util.h"
43 static gboolean __add_history_handler(gpointer user_data)
47 app_pkt_t *pkt = (app_pkt_t *)user_data;
48 struct history_data *hd = (struct history_data *)pkt->data;
50 memset(&rec, 0, sizeof(rec));
52 rec.pkg_name = hd->pkg_name;
53 rec.app_path = hd->app_path;
56 rec.arg = (char *)hd->data;
59 _D("add rua history %s %s", rec.pkg_name, rec.app_path);
61 ret = rua_add_history(&rec);
63 _D("rua add history error");
69 int __send_result_to_client(int fd, int res)
71 if (send(fd, &res, sizeof(int), MSG_NOSIGNAL) < 0) {
73 _E("send failed due to EPIPE.\n");
74 _E("send fail to client");
80 static int __get_pkginfo(const char *dname, const char *cmdline, void *priv)
82 app_info_from_db *menu_info;
85 r_info = (char *)priv;
87 if ((menu_info = _get_app_info_from_db_by_apppath(cmdline)) == NULL)
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);
99 if (menu_info != NULL)
100 _free_app_info_from_db(menu_info);
104 int __send_running_appinfo(int fd)
106 app_pkt_t *pkt = NULL;
109 pkt = (app_pkt_t *) malloc(sizeof(char) * AUL_SOCK_MAXBUFF);
115 memset(pkt, 0, AUL_SOCK_MAXBUFF);
117 __proc_iter_cmdline(__get_pkginfo, pkt->data);
119 pkt->cmd = RUNNING_INFO_RESULT;
120 pkt->len = strlen((char *)pkt->data) + 1;
122 if ((len = send(fd, pkt, pkt->len + 8, 0)) != pkt->len + 8) {
124 _E("send failed due to EPIPE.\n");
125 _E("send fail to client");
135 int __app_is_running(const char *pkgname)
137 char *apppath = NULL;
138 ail_appinfo_h handle;
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);
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");
162 /*truncate apppath if it includes default bundles */
163 while (apppath[i] != 0) {
164 if (apppath[i] == ' ' || apppath[i] == '\t') {
171 if (__proc_iter_cmdline(NULL, apppath) > 0)
177 if (ail_package_destroy_appinfo(handle) != AIL_ERROR_OK)
178 _E("ail_destroy_rs failed");
183 static gboolean __util_handler(gpointer data)
185 GPollFD *gpollfd = (GPollFD *) data;
186 int fd = gpollfd->fd;
190 struct history_data *hd;
192 char pkgname[MAX_PACKAGE_STR_SIZE];
194 if ((pkt = __app_recv_raw(fd, &clifd, &cr)) == NULL) {
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);
207 __send_running_appinfo(clifd);
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);
217 _E("no support packet");
223 static gboolean __au_glib_check(GSource *src)
228 fd_list = src->poll_fds;
230 tmp = (GPollFD *) fd_list->data;
231 if ((tmp->revents & (POLLIN | POLLPRI)))
233 fd_list = fd_list->next;
239 static gboolean __au_glib_dispatch(GSource *src, GSourceFunc callback,
246 static gboolean __au_glib_prepare(GSource *src, gint *timeout)
251 static GSourceFuncs funcs = {
252 .prepare = __au_glib_prepare,
253 .check = __au_glib_check,
254 .dispatch = __au_glib_dispatch,
269 _D("[APP %d] rua init error");
273 fd = __create_server_sock(AUL_UTIL_PID);
275 src = g_source_new(&funcs, sizeof(GSource));
277 gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
278 gpollfd->events = POLLIN;
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);
286 r = g_source_attach(src, NULL);
289 /* TODO: error handle*/
302 mainloop = g_main_loop_new(NULL, FALSE);
304 ret = ac_server_initailize();
306 ret = __initialize();
308 g_main_loop_run(mainloop);