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/smack.h>
35 #include "ail_private.h"
40 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
45 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
47 #define SET_DEFAULT_LABEL(x) \
48 if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \
49 else _D("chsmack -a \"*\" %s", x)
51 static int syncdb_user_count_app(void)
57 ret = ail_filter_new(&filter);
58 if (ret != AIL_ERROR_OK) {
62 ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
63 if (ret != AIL_ERROR_OK) {
64 ail_filter_destroy(filter);
67 ret = ail_filter_count_usr_appinfo(filter, &total, getuid());
68 if (ret != AIL_ERROR_OK) {
69 ail_filter_destroy(filter);
73 ail_filter_destroy(filter);
78 char* _desktop_to_package(const char* desktop)
83 retv_if(!desktop, NULL);
85 package = strdup(desktop);
86 retv_if(!package, NULL);
88 tmp = strrchr(package, '.');
90 _E("[%s] is not a desktop file", package);
95 if (strcmp(tmp, ".desktop")) {
96 _E("%s is not a desktop file", desktop);
108 int syncdb_user_load_directory(const char *directory)
111 struct dirent entry, *result;
118 dir = opendir(directory);
120 if (strerror_r(errno, buf, sizeof(buf)) == 0)
121 _E("Failed to access the [%s] because %s\n", directory, buf);
122 return AIL_ERROR_FAIL;
125 len = strlen(directory) + 1;
126 _D("Loading desktop files from %s", directory);
128 for (ret = readdir_r(dir, &entry, &result);
129 ret == 0 && result != NULL;
130 ret = readdir_r(dir, &entry, &result)) {
133 if (entry.d_name[0] == '.') continue;
135 package = _desktop_to_package(entry.d_name);
137 _E("Failed to convert file to package[%s]", entry.d_name);
141 if (ail_usr_desktop_add(package, getuid()) != AIL_ERROR_OK) {
142 _E("Failed to add a package[%s]", package);
149 _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
155 static int __is_authorized()
157 /* ail_init db should be called by an user. */
158 uid_t uid = getuid();
159 if ((uid_t) OWNER_ROOT != uid)
165 int main(int argc, char *argv[])
171 if (!__is_authorized()) {
172 fprintf(stderr, "You are not an authorized user!\n");
173 _D("You are root user! Please switch to a regular user\n");
177 db = ail_get_app_DB(getuid());
179 fprintf(stderr, "Failed to get DB");
180 _E("Failed to get DB");
184 if (access(db, F_OK)) {
185 fprintf(stderr, "Application database is missing, please use ail_createdb to create one before\n");
187 return AIL_ERROR_FAIL;
190 ret = setenv("AIL_INITDB", "1", 1);
191 _D("AIL_INITDB : %d", ret);
193 ret = syncdb_user_count_app();
195 _D("Some Apps in the App Info DB.");
197 path = ail_get_desktop_path(getuid());
199 _E("Failed to get desktop path");
201 return AIL_ERROR_FAIL;
204 ret = syncdb_user_load_directory(path);
205 if (ret == AIL_ERROR_FAIL)
206 _E("cannot load usr desktop directory.");