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.
31 #include <sys/smack.h>
35 #include "ail_private.h"
40 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
45 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
47 #define SET_DEFAULT_LABEL(x) \
49 if (smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) \
50 _E("failed chsmack -a \"*\" %s", x); \
52 _D("chsmack -a \"*\" %s", x); \
55 static int initdb_user_count_app(void)
61 ret = ail_filter_new(&filter);
62 if (ret != AIL_ERROR_OK)
65 ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
66 if (ret != AIL_ERROR_OK) {
67 ail_filter_destroy(filter);
70 ret = ail_filter_count_usr_appinfo(filter, &total, getuid());
71 if (ret != AIL_ERROR_OK) {
72 ail_filter_destroy(filter);
76 ail_filter_destroy(filter);
81 char *_desktop_to_package(const char* desktop)
85 retv_if(!desktop, NULL);
87 package = strdup(desktop);
88 retv_if(!package, NULL);
90 tmp = strrchr(package, '.');
92 _E("[%s] is not a desktop file", package);
97 if (strcmp(tmp, ".desktop")) {
98 _E("%s is not a desktop file", desktop);
108 int initdb_user_load_directory(const char *directory)
111 struct dirent entry, *result;
118 dir = opendir(directory);
120 if (strerror_r(errno, buf, sizeof(buf)) == 0)
121 _E("Failed to access the [%s] because %s\n", directory, buf);
122 return AIL_ERROR_FAIL;
125 _D("Loading desktop files from %s", directory);
127 for (ret = readdir_r(dir, &entry, &result);
128 ret == 0 && result != NULL;
129 ret = readdir_r(dir, &entry, &result)) {
132 if (entry.d_name[0] == '.') continue;
134 package = _desktop_to_package(entry.d_name);
136 _E("Failed to convert file to package[%s]", entry.d_name);
140 if (ail_usr_desktop_add(package, getuid()) != AIL_ERROR_OK)
141 _E("Failed to add a package[%s]", package);
148 _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
154 static int __is_authorized()
156 /* ail_init db should be called by an user. */
157 uid_t uid = getuid();
158 if ((uid_t)OWNER_ROOT != uid)
164 int main(int argc, char *argv[])
170 if (!__is_authorized()) {
171 fprintf(stderr, "You are not an authorized user!\n");
172 _D("You are root user! Please switch to a regular user\n");
176 db = ail_get_app_DB(getuid());
179 _E(" %s is not removed", db);
184 db = ail_get_app_DB_journal(getuid());
187 _E(" %s is not removed", db);
192 ret = setenv("AIL_INITDB", "1", 1);
193 _D("AIL_INITDB : %d", ret);
195 ret = initdb_user_count_app();
197 _D("Some Apps in the App Info DB.");
199 path = ail_get_desktop_path(getuid());
201 _E("Failed to get desktop path");
205 ret = initdb_user_load_directory(path);
206 if (ret == AIL_ERROR_FAIL)
207 _E("cannot load usr desktop directory.");