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 ret = ail_filter_count_usr_appinfo(filter, &total, uid);
63 //ret = ail_filter_count_appinfo(filter, &total);
64 if (ret != AIL_ERROR_OK) {
65 ail_filter_destroy(filter);
69 ail_filter_destroy(filter);
76 char* _desktop_to_package(const char* desktop)
80 retv_if(!desktop, NULL);
82 package = strdup(desktop);
83 retv_if(!package, NULL);
85 tmp = strrchr(package, '.');
87 _E("[%s] is not a desktop file", package);
92 if (strcmp(tmp, ".desktop")) {
93 _E("%s is not a desktop file", desktop);
105 int initdb_load_directory(const char *directory)
108 struct dirent entry, *result;
115 dir = opendir(directory);
117 if (strerror_r(errno, buf, sizeof(buf)) == 0)
118 _E("Failed to access the [%s] because %s\n", directory, buf);
119 return AIL_ERROR_FAIL;
122 len = strlen(directory) + 1;
123 _D("Loading desktop files from %s", directory);
125 for (ret = readdir_r(dir, &entry, &result);
126 ret == 0 && result != NULL;
127 ret = readdir_r(dir, &entry, &result)) {
130 if (entry.d_name[0] == '.') continue;
132 package = _desktop_to_package(entry.d_name);
134 _E("Failed to convert file to package[%s]", entry.d_name);
138 if (ail_desktop_fota(package) != AIL_ERROR_OK) {
139 _E("Failed to add a package[%s]", package);
146 _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
154 static int initdb_change_perm(const char *db_file)
157 char journal_file[BUFSZE];
161 files[0] = (char *)db_file;
162 files[1] = journal_file;
165 retv_if(!db_file, AIL_ERROR_FAIL);
167 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
169 for (i = 0; files[i]; i++) {
170 ret = chown(files[i], OWNER_ROOT, OWNER_ROOT);
172 strerror_r(errno, buf, sizeof(buf));
173 _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf);
174 return AIL_ERROR_FAIL;
177 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
179 strerror_r(errno, buf, sizeof(buf));
180 _E("FAIL : chmod %s 0664, because %s", db_file, buf);
181 return AIL_ERROR_FAIL;
189 static int __is_authorized()
191 /* ail_init db should be called by as root privilege. */
193 uid_t uid = getuid();
194 if ((uid_t) 0 == uid)
200 int xsystem(const char *argv[])
207 perror("fork failed");
211 execvp(argv[0], (char *const *)argv);
217 if (waitpid(pid, &status, 0) == -1) {
218 perror("waitpid failed");
221 if (WIFSIGNALED(status)) {
225 if (!WIFEXITED(status)) {
226 /* shouldn't happen */
227 perror("should not happen");
230 return WEXITSTATUS(status);
233 int main(int argc, char *argv[])
237 if (!__is_authorized()) {
238 fprintf(stderr, "You are not an authorized user!\n");
239 _D("You are not root user!\n");
242 const char *argv_rm[] = { "/bin/rm", APP_INFO_DB_FILE, NULL };
244 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(getuid());
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);