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.
27 #include <sys/types.h>
34 #include <app-checker.h>
38 #define PLUGINS_PREFIX "/usr/lib/ac-plugins"
39 #define MAX_LOCAL_BUFSZ 512
42 #define SLPAPI __attribute__ ((visibility("default")))
45 GSList *pkg_type_list = NULL;
47 typedef struct _ac_type_list_t {
52 typedef struct _ac_so_list_t {
54 int (*ac_check)(const char*);
55 int (*ac_register)(const char*);
56 int (*ac_unregister)(const char*);
59 static int __send_to_sigkill(int pid)
67 if (killpg(pgid, SIGKILL) < 0)
73 static int __check_launch_privilege(const char *pkg_name, const char *pkg_type, int pid)
77 ac_type_list_t *type_t;
80 for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
82 if (strncmp(type_t->pkg_type, pkg_type, MAX_PACKAGE_TYPE_SIZE) == 0) {
83 for (iter2 = type_t->so_list; iter2 != NULL; iter2 = g_slist_next(iter2)) {
85 _D("type : %s / so name : %s / func : %x", type_t->pkg_type, so_t->so_name, so_t->ac_check);
86 if (so_t->ac_check && so_t->ac_check(pkg_name) < 0) {
88 __send_to_sigkill(pid);
96 return AC_R_ENOPULUGINS;
99 static int __register_launch_privilege(const char *pkg_name, const char *pkg_type)
102 GSList *iter2 = NULL;
103 ac_type_list_t *type_t;
107 for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
109 if (strncmp(type_t->pkg_type, pkg_type, MAX_PACKAGE_TYPE_SIZE) == 0) {
110 for (iter2 = type_t->so_list; iter2 != NULL; iter = g_slist_next(iter2)) {
112 if (so_t->ac_register && so_t->ac_register(pkg_name) < 0) {
120 return AC_R_ENOPULUGINS;
124 static int __unregister_launch_privilege(const char *pkg_name, const char *pkg_type)
127 GSList *iter2 = NULL;
128 ac_type_list_t *type_t;
132 for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
134 if (strncmp(type_t->pkg_type, pkg_type, MAX_PACKAGE_TYPE_SIZE) == 0) {
135 for (iter2 = type_t->so_list; iter2 != NULL; iter = g_slist_next(iter2)) {
137 if (so_t->ac_unregister && so_t->ac_unregister(pkg_name) < 0) {
145 return AC_R_ENOPULUGINS;;
149 static gboolean __ac_handler(gpointer data)
151 GPollFD *gpollfd = (GPollFD *) data;
152 int fd = gpollfd->fd;
160 if ((pkt = _app_recv_raw(fd, &clifd, &cr)) == NULL) {
165 ad = (struct ac_data *)g_base64_decode((const gchar*)pkt->data, (gsize *)&size);
167 _D("cmd : %d, pkgname : %s, pkgtype : %s", pkt->cmd, ad->pkg_name, ad->pkg_type);
171 _send_result_to_server(clifd, AC_R_OK);
172 ret = __check_launch_privilege(ad->pkg_name, ad->pkg_type, ad->pid);
178 ret = __register_launch_privilege(ad->pkg_name, ad->pkg_type);
181 ret = __unregister_launch_privilege(ad->pkg_name, ad->pkg_type);
184 _E("no support packet");
187 _send_result_to_server(clifd, ret);
194 static gboolean __ac_glib_check(GSource *src)
199 fd_list = src->poll_fds;
201 tmp = (GPollFD *) fd_list->data;
202 if ((tmp->revents & (POLLIN | POLLPRI)))
204 fd_list = fd_list->next;
210 static gboolean __ac_glib_dispatch(GSource *src, GSourceFunc callback,
217 static gboolean __ac_glib_prepare(GSource *src, gint *timeout)
222 static GSourceFuncs funcs = {
223 .prepare = __ac_glib_prepare,
224 .check = __ac_glib_check,
225 .dispatch = __ac_glib_dispatch,
229 static void __pkt_type_list_free()
232 GSList *iter2 = NULL;
233 ac_type_list_t *type_t;
236 for (iter = pkg_type_list; iter != NULL; iter = g_slist_next(iter)) {
239 for (iter2 = type_t->so_list; iter2 != NULL; iter2 = g_slist_next(iter2)) {
247 g_slist_free(type_t->so_list);
251 free(type_t->pkg_type);
255 g_slist_free(pkg_type_list);
266 _D("app checker server initialize");
268 fd = _create_server_sock();
270 src = g_source_new(&funcs, sizeof(GSource));
272 gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
273 gpollfd->events = POLLIN;
276 g_source_add_poll(src, gpollfd);
277 g_source_set_callback(src, (GSourceFunc) __ac_handler,
278 (gpointer) gpollfd, NULL);
279 g_source_set_priority(src, G_PRIORITY_DEFAULT);
281 ret = g_source_attach(src, NULL);
284 /* TODO: error handle*/
291 struct dirent *dentry;
293 struct dirent *sub_dentry;
294 char buf[MAX_LOCAL_BUFSZ];
295 char buf2[MAX_LOCAL_BUFSZ];
296 ac_type_list_t *type_t = NULL;
298 ac_so_list_t *so_t = NULL;
300 dp = opendir(PLUGINS_PREFIX);
304 while ((dentry = readdir(dp)) != NULL) {
306 if(dentry->d_type != DT_DIR)
308 if(strcmp(dentry->d_name,".") == 0 || strcmp(dentry->d_name,"..") == 0)
311 snprintf(buf,MAX_LOCAL_BUFSZ,"%s/%s",PLUGINS_PREFIX,dentry->d_name);
312 _D("type : %s", dentry->d_name);
314 type_t = malloc(sizeof(ac_type_list_t));
316 __pkt_type_list_free();
319 memset(type_t, 0, sizeof(ac_type_list_t));
320 type_t->pkg_type = strdup(dentry->d_name);
321 type_t->so_list = NULL;
323 pkg_type_list = g_slist_append(pkg_type_list, (void *)type_t);
325 sub_dp = opendir(buf);
327 while ((sub_dentry = readdir(sub_dp)) != NULL) {
329 if(sub_dentry->d_type == DT_DIR)
331 snprintf(buf2,MAX_LOCAL_BUFSZ,"%s/%s", buf, sub_dentry->d_name);
332 _D("so_name : %s", buf2);
334 handle = dlopen(buf2, RTLD_LAZY);
337 so_t = malloc(sizeof(ac_so_list_t));
339 __pkt_type_list_free();
342 memset(so_t, 0, sizeof(ac_so_list_t));
343 so_t->so_name = strdup(sub_dentry->d_name);
344 so_t->ac_check = dlsym(handle, "check_launch_privilege");
345 so_t->ac_register = dlsym(handle, "check_register_privilege");
346 so_t->ac_unregister = dlsym(handle, "check_unregister_privilege");
348 type_t->so_list = g_slist_append(type_t->so_list, (void *)so_t);
356 SLPAPI int ac_server_initailize()
360 ret = __initialize();