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.
33 #include "ail_private.h"
39 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
44 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
46 static int initdb_count_app(void)
52 ret = ail_filter_new(&filter);
53 if (ret != AIL_ERROR_OK) {
57 ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
58 if (ret != AIL_ERROR_OK) {
59 ail_filter_destroy(filter);
62 ret = ail_filter_count_appinfo(filter, &total);
63 if (ret != AIL_ERROR_OK) {
64 ail_filter_destroy(filter);
68 ail_filter_destroy(filter);
75 char* _desktop_to_package(const char* desktop)
79 retv_if(!desktop, NULL);
81 package = strdup(desktop);
82 retv_if(!package, NULL);
84 tmp = strrchr(package, '.');
86 _E("[%s] is not a desktop file", package);
91 if (strcmp(tmp, ".desktop")) {
92 _E("%s is not a desktop file", desktop);
104 int initdb_load_directory(const char *directory)
107 struct dirent entry, *result;
114 dir = opendir(directory);
116 if (strerror_r(errno, buf, sizeof(buf)) == 0)
117 _E("Failed to access the [%s] because %s\n", directory, buf);
118 return AIL_ERROR_FAIL;
121 len = strlen(directory) + 1;
122 _D("Loading desktop files from %s", directory);
124 for (ret = readdir_r(dir, &entry, &result);
125 ret == 0 && result != NULL;
126 ret = readdir_r(dir, &entry, &result)) {
129 if (entry.d_name[0] == '.') continue;
131 package = _desktop_to_package(entry.d_name);
133 _E("Failed to convert file to package[%s]", entry.d_name);
137 if (ail_desktop_add(package) != AIL_ERROR_OK) {
138 _E("Failed to add a package[%s]", package);
145 _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
153 static int initdb_change_perm(const char *db_file)
156 char journal_file[BUFSZE];
160 files[0] = (char *)db_file;
161 files[1] = journal_file;
164 retv_if(!db_file, AIL_ERROR_FAIL);
166 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
168 for (i = 0; files[i]; i++) {
169 ret = chown(files[i], OWNER_ROOT, OWNER_ROOT);
171 strerror_r(errno, buf, sizeof(buf));
172 _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf);
173 return AIL_ERROR_FAIL;
176 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
178 strerror_r(errno, buf, sizeof(buf));
179 _E("FAIL : chmod %s 0664, because %s", db_file, buf);
180 return AIL_ERROR_FAIL;
188 static int __is_authorized()
190 /* ail_init db should be called by as root privilege. */
192 uid_t uid = getuid();
193 if ((uid_t) GLOBAL_USER == uid)
199 int xsystem(const char *argv[])
206 perror("fork failed");
210 execvp(argv[0], (char *const *)argv);
216 if (waitpid(pid, &status, 0) == -1) {
217 perror("waitpid failed");
220 if (WIFSIGNALED(status)) {
224 if (!WIFEXITED(status)) {
225 /* shouldn't happen */
226 perror("should not happen");
229 return WEXITSTATUS(status);
232 int main(int argc, char *argv[])
236 if (!__is_authorized()) {
237 fprintf(stderr, "You are not an authorized user!\n");
238 _D("You are not root user!\n");
241 const char *argv_rm[] = { "/bin/rm", APP_INFO_DB_FILE, NULL };
243 const char *argv_rmjn[] = { "/bin/rm", APP_INFO_DB_FILE_JOURNAL, NULL };
247 ret = setenv("AIL_INITDB", "1", 1);
248 _D("AIL_INITDB : %d", ret);
250 ret = initdb_count_app();
252 _D("Some Apps in the App Info DB.");
255 ret = initdb_load_directory(OPT_DESKTOP_DIRECTORY);
256 if (ret == AIL_ERROR_FAIL) {
257 _E("cannot load opt desktop directory.");
260 ret = initdb_load_directory(USR_DESKTOP_DIRECTORY);
261 if (ret == AIL_ERROR_FAIL) {
262 _E("cannot load usr desktop directory.");
265 if (__is_authorized()) {
266 ret = initdb_change_perm(APP_INFO_DB_FILE);
267 if (ret == AIL_ERROR_FAIL) {
270 const char *argv_smack[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE, NULL };
272 const char *argv_smackjn[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE_JOURNAL, NULL };
273 xsystem(argv_smackjn);