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.
29 #include <sys/types.h>
32 #include <sys/smack.h>
35 #include "ail_private.h"
41 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
46 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
48 #define SET_DEFAULT_LABEL(x) \
50 if (smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) \
51 _E("failed chsmack -a \"*\" %s", x); \
53 _D("chsmack -a \"*\" %s", x); \
56 char *_desktop_to_package(const char* desktop)
60 retv_if(!desktop, NULL);
62 package = strdup(desktop);
63 retv_if(!package, NULL);
65 tmp = strrchr(package, '.');
67 _E("[%s] is not a desktop file", package);
72 if (strcmp(tmp, ".desktop")) {
73 _E("%s is not a desktop file", desktop);
83 int initdb_load_directory(const char *directory)
86 struct dirent entry, *result;
93 dir = opendir(directory);
95 if (strerror_r(errno, buf, sizeof(buf)) == 0)
96 _E("Failed to access the [%s] because %s\n", directory, buf);
97 return AIL_ERROR_FAIL;
100 _D("Loading desktop files from %s", directory);
102 for (ret = readdir_r(dir, &entry, &result);
103 ret == 0 && result != NULL;
104 ret = readdir_r(dir, &entry, &result)) {
107 if (entry.d_name[0] == '.') continue;
109 package = _desktop_to_package(entry.d_name);
111 _E("Failed to convert file to package[%s]", entry.d_name);
115 if (ail_desktop_add(package) != AIL_ERROR_OK)
116 _E("Failed to add a package[%s]", package);
123 _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
129 static int initdb_change_perm(const char *db_file)
131 char journal_file[BUFSZE];
135 files[0] = (char *)db_file;
136 files[1] = journal_file;
139 retv_if(!db_file, AIL_ERROR_FAIL);
141 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
143 for (i = 0; files[i]; i++) {
144 ret = chown(files[i], GLOBAL_USER, OWNER_ROOT);
146 _E("FAIL : chown %s %d.%d, because %d", db_file, OWNER_ROOT, OWNER_ROOT, errno);
147 return AIL_ERROR_FAIL;
150 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
152 _E("FAIL : chmod %s 0664, because %d", db_file, errno);
153 return AIL_ERROR_FAIL;
160 static int __is_authorized()
162 /* ail_init db should be called by as root privilege. */
163 uid_t uid = getuid();
164 /* euid need to be root to allow smack label changes during initialization */
165 /* uid_t euid = geteuid(); */
166 if ((uid_t) OWNER_ROOT == uid)
172 int xsystem(const char *argv[])
179 perror("fork failed");
183 execvp(argv[0], (char *const *)argv);
189 if (waitpid(pid, &status, 0) == -1) {
190 perror("waitpid failed");
193 if (WIFSIGNALED(status)) {
197 if (!WIFEXITED(status)) {
198 /* shouldn't happen */
199 perror("should not happen");
202 return WEXITSTATUS(status);
205 int main(int argc, char *argv[])
209 if (!__is_authorized()) {
210 fprintf(stderr, "You are not an authorized user!\n");
211 _D("You are not root user!\n");
214 if (remove(APP_INFO_DB_FILE))
215 _E(" %s is not removed", APP_INFO_DB_FILE);
216 if (remove(APP_INFO_DB_FILE_JOURNAL))
217 _E(" %s is not removed", APP_INFO_DB_FILE_JOURNAL);
219 ret = setenv("AIL_INITDB", "1", 1);
220 _D("AIL_INITDB : %d", ret);
222 if (setresuid(GLOBAL_USER, GLOBAL_USER, OWNER_ROOT) != 0)
223 _E("setresuid() is failed.");
225 if (db_open(DB_OPEN_RW, GLOBAL_USER) != AIL_ERROR_OK) {
226 _E("Fail to create system databases");
227 return AIL_ERROR_DB_FAILED;
229 ret = initdb_load_directory(USR_DESKTOP_DIRECTORY);
230 if (ret == AIL_ERROR_FAIL)
231 _E("cannot load usr desktop directory.");
233 if (setuid(OWNER_ROOT) != 0)
234 _E("setuid() is failed.");
236 ret = initdb_change_perm(APP_INFO_DB_FILE);
237 if (ret == AIL_ERROR_FAIL)
240 SET_DEFAULT_LABEL(APP_INFO_DB_FILE);
241 SET_DEFAULT_LABEL(APP_INFO_DB_FILE_JOURNAL);