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"
36 #define GROUP_MENU 6010
38 #define OPT_DESKTOP_DIRECTORY tzplatform_getenv(TZ_SYS_RW_DESKTOP_APP)
39 #define USR_DESKTOP_DIRECTORY tzplatform_getenv(TZ_SYS_RO_DESKTOP_APP)
40 #define APP_INFO_DB_FILE tzplatform_mkpath(TZ_SYS_DB,".app_info.db")
41 #define APP_INFO_DB_FILE_JOURNAL tzplatform_mkpath(TZ_SYS_DB,".app_info.db-journal")
42 #define APP_INFO_DB_LABEL "ail::db"
47 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
52 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
54 static int initdb_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);
71 ret = ail_filter_count_appinfo(filter, &total);
72 if (ret != AIL_ERROR_OK) {
73 ail_filter_destroy(filter);
77 ail_filter_destroy(filter);
84 char* _desktop_to_package(const char* desktop)
88 retv_if(!desktop, NULL);
90 package = strdup(desktop);
91 retv_if(!package, NULL);
93 tmp = strrchr(package, '.');
95 _E("[%s] is not a desktop file", package);
100 if (strcmp(tmp, ".desktop")) {
101 _E("%s is not a desktop file", desktop);
113 int initdb_load_directory(const char *directory)
116 struct dirent entry, *result;
123 dir = opendir(directory);
125 if (strerror_r(errno, buf, sizeof(buf)) == 0)
126 _E("Failed to access the [%s] because %s\n", directory, buf);
127 return AIL_ERROR_FAIL;
130 len = strlen(directory) + 1;
131 _D("Loading desktop files from %s", directory);
133 for (ret = readdir_r(dir, &entry, &result);
134 ret == 0 && result != NULL;
135 ret = readdir_r(dir, &entry, &result)) {
138 if (entry.d_name[0] == '.') continue;
140 package = _desktop_to_package(entry.d_name);
142 _E("Failed to convert file to package[%s]", entry.d_name);
146 if (ail_desktop_add(package) != AIL_ERROR_OK) {
147 _E("Failed to add a package[%s]", package);
154 _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
162 static int initdb_change_perm(const char *db_file)
165 char journal_file[BUFSZE];
169 files[0] = (char *)db_file;
170 files[1] = journal_file;
173 retv_if(!db_file, AIL_ERROR_FAIL);
175 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
177 for (i = 0; files[i]; i++) {
178 ret = chown(files[i], OWNER_ROOT, OWNER_ROOT);
180 strerror_r(errno, buf, sizeof(buf));
181 _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf);
182 return AIL_ERROR_FAIL;
185 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
187 strerror_r(errno, buf, sizeof(buf));
188 _E("FAIL : chmod %s 0664, because %s", db_file, buf);
189 return AIL_ERROR_FAIL;
197 static int __is_authorized()
199 /* ail_init db should be called by as root privilege. */
201 uid_t uid = getuid();
202 if ((uid_t) 0 == uid)
208 int xsystem(const char *argv[])
215 perror("fork failed");
219 execvp(argv[0], (char *const *)argv);
225 if (waitpid(pid, &status, 0) == -1) {
226 perror("waitpid failed");
229 if (WIFSIGNALED(status)) {
233 if (!WIFEXITED(status)) {
234 /* shouldn't happen */
235 perror("should not happen");
238 return WEXITSTATUS(status);
241 int main(int argc, char *argv[])
245 if (!__is_authorized()) {
246 fprintf(stderr, "You are not an authorized user!\n");
247 _D("You are not an authorized user!\n");
248 return AIL_ERROR_FAIL;
251 const char *argv_rm[] = { "/bin/rm", APP_INFO_DB_FILE, NULL };
253 const char *argv_rmjn[] = { "/bin/rm", APP_INFO_DB_FILE_JOURNAL, NULL };
256 ret = setenv("AIL_INITDB", "1", 1);
257 _D("AIL_INITDB : %d", ret);
259 ret = initdb_count_app();
261 _D("Some Apps in the App Info DB.");
264 ret = initdb_load_directory(OPT_DESKTOP_DIRECTORY);
265 if (ret == AIL_ERROR_FAIL) {
266 _E("cannot load opt desktop directory.");
269 ret = initdb_load_directory(USR_DESKTOP_DIRECTORY);
270 if (ret == AIL_ERROR_FAIL) {
271 _E("cannot load usr desktop directory.");
274 ret = initdb_change_perm(APP_INFO_DB_FILE);
275 if (ret == AIL_ERROR_FAIL) {
278 #ifdef WRT_SMACK_ENABLED
279 const char *argv_smack[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE, NULL };
281 const char *argv_smackjn[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE_JOURNAL, NULL };
282 xsystem(argv_smackjn);