4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 * Copyright (C) 2013-2014 Intel Corporation.
7 * Contact: Sabera Djelti <sabera.djelti@open.eurogiciel.org>,
8 * Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
33 #include <sys/smack.h>
36 #include "ail_private.h"
43 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
48 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
50 #define SET_DEFAULT_LABEL(x) \
51 if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \
52 else _D("chsmack -a \"*\" %s", x)
54 static int syncdb_count_app(void)
60 ret = ail_filter_new(&filter);
61 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_appinfo(filter, &total);
71 if (ret != AIL_ERROR_OK) {
72 ail_filter_destroy(filter);
76 ail_filter_destroy(filter);
83 char* _desktop_to_package(const char* desktop)
87 retv_if(!desktop, NULL);
89 package = strdup(desktop);
90 retv_if(!package, NULL);
92 tmp = strrchr(package, '.');
94 _E("[%s] is not a desktop file", package);
99 if (strcmp(tmp, ".desktop")) {
100 _E("%s is not a desktop file", desktop);
112 int syncdb_load_directory(const char *directory)
115 struct dirent entry, *result;
122 dir = opendir(directory);
124 if (strerror_r(errno, buf, sizeof(buf)) == 0)
125 _E("Failed to access the [%s] because %s\n", directory, buf);
126 return AIL_ERROR_FAIL;
129 len = strlen(directory) + 1;
130 _D("Loading desktop files from %s", directory);
132 for (ret = readdir_r(dir, &entry, &result);
133 ret == 0 && result != NULL;
134 ret = readdir_r(dir, &entry, &result)) {
137 if (entry.d_name[0] == '.') continue;
139 package = _desktop_to_package(entry.d_name);
141 _E("Failed to convert file to package[%s]", entry.d_name);
145 if (ail_desktop_add(package) != AIL_ERROR_OK) {
146 _E("Failed to add a package[%s]", package);
153 _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
161 static int syncdb_change_perm(const char *db_file)
164 char journal_file[BUFSZE];
168 files[0] = (char *)db_file;
169 files[1] = journal_file;
172 retv_if(!db_file, AIL_ERROR_FAIL);
174 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
176 for (i = 0; files[i]; i++) {
177 ret = chown(files[i], GLOBAL_USER, OWNER_ROOT);
179 strerror_r(errno, buf, sizeof(buf));
180 _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf);
181 return AIL_ERROR_FAIL;
184 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
186 strerror_r(errno, buf, sizeof(buf));
187 _E("FAIL : chmod %s 0664, because %s", db_file, buf);
188 return AIL_ERROR_FAIL;
196 static int __is_authorized()
198 /* ail_init db should be called by as root privilege. */
200 uid_t uid = getuid();
201 uid_t euid = geteuid();
202 //euid need to be root to allow smack label changes during initialization
203 if ((uid_t) OWNER_ROOT == uid)
209 int xsystem(const char *argv[])
216 perror("fork failed");
220 execvp(argv[0], (char *const *)argv);
226 if (waitpid(pid, &status, 0) == -1) {
227 perror("waitpid failed");
230 if (WIFSIGNALED(status)) {
234 if (!WIFEXITED(status)) {
235 /* shouldn't happen */
236 perror("should not happen");
239 return WEXITSTATUS(status);
242 int main(int argc, char *argv[])
246 if (!__is_authorized()) {
247 fprintf(stderr, "You are not an authorized user!\n");
248 _D("You are not root user!\n");
251 if (access(APP_INFO_DB_FILE, F_OK)) {
252 fprintf(stderr, "Application database %s is missing, please use ail_createdb to create one before\n", APP_INFO_DB_FILE);
253 return AIL_ERROR_FAIL;
255 ret = setenv("AIL_INITDB", "1", 1);
256 _D("AIL_INITDB : %d", ret);
257 setresuid(GLOBAL_USER, GLOBAL_USER, OWNER_ROOT);
259 if (db_open(DB_OPEN_RW, GLOBAL_USER) != AIL_ERROR_OK) {
260 _E("Fail to create system databases");
261 return AIL_ERROR_DB_FAILED;
263 ret = syncdb_load_directory(USR_DESKTOP_DIRECTORY);
264 if (ret == AIL_ERROR_FAIL) {
265 _E("cannot load usr desktop directory.");