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"
38 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
43 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
45 static int initdb_count_app(uid_t uid)
51 ret = ail_filter_new(&filter);
52 if (ret != AIL_ERROR_OK) {
56 ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
57 if (ret != AIL_ERROR_OK) {
58 ail_filter_destroy(filter);
62 if (uid != GLOBAL_USER)
63 ret = ail_filter_count_usr_appinfo(filter, &total, uid);
65 ret = ail_filter_count_appinfo(filter, &total);
66 if (ret != AIL_ERROR_OK) {
67 ail_filter_destroy(filter);
71 ail_filter_destroy(filter);
78 char* _desktop_to_package(const char* desktop)
82 retv_if(!desktop, NULL);
84 package = strdup(desktop);
85 retv_if(!package, NULL);
87 tmp = strrchr(package, '.');
89 _E("[%s] is not a desktop file", package);
94 if (strcmp(tmp, ".desktop")) {
95 _E("%s is not a desktop file", desktop);
107 int initdb_load_directory(const char *directory)
110 struct dirent entry, *result;
117 dir = opendir(directory);
119 if (strerror_r(errno, buf, sizeof(buf)) == 0)
120 _E("Failed to access the [%s] because %s\n", directory, buf);
121 return AIL_ERROR_FAIL;
124 len = strlen(directory) + 1;
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_desktop_fota(package) != 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);
156 static int initdb_change_perm(const char *db_file)
159 char journal_file[BUFSZE];
163 files[0] = (char *)db_file;
164 files[1] = journal_file;
167 retv_if(!db_file, AIL_ERROR_FAIL);
169 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
171 for (i = 0; files[i]; i++) {
172 ret = chown(files[i], OWNER_ROOT, OWNER_ROOT);
174 strerror_r(errno, buf, sizeof(buf));
175 _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf);
176 return AIL_ERROR_FAIL;
179 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
181 strerror_r(errno, buf, sizeof(buf));
182 _E("FAIL : chmod %s 0664, because %s", db_file, buf);
183 return AIL_ERROR_FAIL;
191 static int __is_authorized()
193 /* ail_init db should be called by as root privilege. */
195 uid_t uid = getuid();
196 if ((uid_t) 0 == uid)
202 int xsystem(const char *argv[])
209 perror("fork failed");
213 execvp(argv[0], (char *const *)argv);
219 if (waitpid(pid, &status, 0) == -1) {
220 perror("waitpid failed");
223 if (WIFSIGNALED(status)) {
227 if (!WIFEXITED(status)) {
228 /* shouldn't happen */
229 perror("should not happen");
232 return WEXITSTATUS(status);
235 int main(int argc, char *argv[])
239 if (!__is_authorized()) {
240 fprintf(stderr, "You are not an authorized user!\n");
241 _D("You are not root user!\n");
244 const char *argv_rm[] = { "/bin/rm", APP_INFO_DB_FILE, NULL };
246 const char *argv_rmjn[] = { "/bin/rm", APP_INFO_DB_FILE_JOURNAL, NULL };
249 ret = setenv("AIL_INITDB", "1", 1);
250 _D("AIL_INITDB : %d", ret);
252 ret = initdb_count_app(getuid());
254 _D("Some Apps in the App Info DB.");
257 ret = initdb_load_directory(OPT_DESKTOP_DIRECTORY);
258 if (ret == AIL_ERROR_FAIL) {
259 _E("cannot load opt desktop directory.");
262 ret = initdb_load_directory(USR_DESKTOP_DIRECTORY);
263 if (ret == AIL_ERROR_FAIL) {
264 _E("cannot load usr desktop directory.");
267 if (__is_authorized()) {
268 ret = initdb_change_perm(APP_INFO_DB_FILE);
269 if (ret == AIL_ERROR_FAIL) {
272 const char *argv_smack[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE, NULL };
274 const char *argv_smackjn[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE_JOURNAL, NULL };
275 xsystem(argv_smackjn);