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.
31 #include <sys/types.h>
34 #include <sys/smack.h>
37 #include "ail_private.h"
44 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
49 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
51 #define SET_DEFAULT_LABEL(x) \
53 if (smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) \
54 _E("failed chsmack -a \"*\" %s", x); \
56 _D("chsmack -a \"*\" %s", x); \
59 char* _desktop_to_package(const char* desktop)
63 retv_if(!desktop, NULL);
65 package = strdup(desktop);
66 retv_if(!package, NULL);
68 tmp = strrchr(package, '.');
70 _E("[%s] is not a desktop file", package);
75 if (strcmp(tmp, ".desktop")) {
76 _E("%s is not a desktop file", desktop);
86 int syncdb_load_directory(const char *directory)
89 struct dirent entry, *result;
96 dir = opendir(directory);
98 if (strerror_r(errno, buf, sizeof(buf)) == 0)
99 _E("Failed to access the [%s] because %s\n", directory, buf);
100 return AIL_ERROR_FAIL;
103 _D("Loading desktop files from %s", directory);
105 for (ret = readdir_r(dir, &entry, &result);
106 ret == 0 && result != NULL;
107 ret = readdir_r(dir, &entry, &result)) {
110 if (entry.d_name[0] == '.') continue;
112 package = _desktop_to_package(entry.d_name);
114 _E("Failed to convert file to package[%s]", entry.d_name);
118 if (ail_desktop_add(package) != AIL_ERROR_OK)
119 _E("Failed to add a package[%s]", package);
125 _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
131 static int __is_authorized(void)
133 /* ail_init db should be called by as root privilege. */
134 uid_t uid = getuid();
135 /* euid need to be root to allow smack label changes during initialization */
136 /* uid_t euid = geteuid(); */
137 if ((uid_t) OWNER_ROOT == uid)
143 int xsystem(const char *argv[])
150 perror("fork failed");
154 execvp(argv[0], (char *const *)argv);
160 if (waitpid(pid, &status, 0) == -1) {
161 perror("waitpid failed");
164 if (WIFSIGNALED(status)) {
168 if (!WIFEXITED(status)) {
169 /* shouldn't happen */
170 perror("should not happen");
173 return WEXITSTATUS(status);
176 int main(int argc, char *argv[])
180 if (!__is_authorized()) {
181 fprintf(stderr, "You are not an authorized user!\n");
182 _D("You are not root user!\n");
186 if (access(APP_INFO_DB_FILE, F_OK)) {
187 fprintf(stderr, "Application database %s is missing, please use ail_createdb to create one before\n", APP_INFO_DB_FILE);
188 return AIL_ERROR_FAIL;
191 ret = setenv("AIL_INITDB", "1", 1);
192 _D("AIL_INITDB : %d", ret);
194 if (setresuid(GLOBAL_USER, GLOBAL_USER, OWNER_ROOT) != 0)
195 _E("setresuid() is failed.");
197 if (db_open(DB_OPEN_RW, GLOBAL_USER) != AIL_ERROR_OK) {
198 _E("Fail to create system databases");
199 return AIL_ERROR_DB_FAILED;
202 ret = syncdb_load_directory(USR_DESKTOP_DIRECTORY);
203 if (ret == AIL_ERROR_FAIL)
204 _E("cannot load usr desktop directory.");