Retrieve tizenglobalapp uid with tzplatform_getuid
[platform/core/appfw/ail.git] / tool / src / initdb.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <dirent.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31
32 #include "ail.h"
33 #include "ail_private.h"
34
35
36 #ifdef _E
37 #undef _E
38 #endif
39 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
40
41 #ifdef _D
42 #undef _D
43 #endif
44 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
45
46 static int initdb_count_app(void)
47 {
48         ail_filter_h filter;
49         ail_error_e ret;
50         int total = 0;
51
52         ret = ail_filter_new(&filter);
53         if (ret != AIL_ERROR_OK) {
54                 return -1;
55         }
56
57         ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
58         if (ret != AIL_ERROR_OK) {
59                 ail_filter_destroy(filter);
60                 return -1;
61         }
62         ret = ail_filter_count_appinfo(filter, &total);
63         if (ret != AIL_ERROR_OK) {
64                 ail_filter_destroy(filter);
65                 return -1;
66         }
67
68         ail_filter_destroy(filter);
69
70         return total;
71 }
72
73
74
75 char* _desktop_to_package(const char* desktop)
76 {
77         char *package, *tmp;
78
79         retv_if(!desktop, NULL);
80
81         package = strdup(desktop);
82         retv_if(!package, NULL);
83
84         tmp = strrchr(package, '.');
85         if(tmp == NULL) {
86                 _E("[%s] is not a desktop file", package);
87                 free(package);
88                 return NULL;
89         }
90
91         if (strcmp(tmp, ".desktop")) {
92                 _E("%s is not a desktop file", desktop);
93                 free(package);
94                 return NULL;
95         }
96
97         *tmp = '\0';
98
99         return package;
100 }
101
102
103
104 int initdb_load_directory(const char *directory)
105 {
106         DIR *dir;
107         struct dirent entry, *result;
108         int len, ret;
109         char buf[BUFSZE];
110         int total_cnt = 0;
111         int ok_cnt = 0;
112
113         // desktop file
114         dir = opendir(directory);
115         if (!dir) {
116                 if (strerror_r(errno, buf, sizeof(buf)) == 0)
117                         _E("Failed to access the [%s] because %s\n", directory, buf);
118                 return AIL_ERROR_FAIL;
119         }
120
121         len = strlen(directory) + 1;
122         _D("Loading desktop files from %s", directory);
123
124         for (ret = readdir_r(dir, &entry, &result);
125                         ret == 0 && result != NULL;
126                         ret = readdir_r(dir, &entry, &result)) {
127                 char *package;
128
129                 if (entry.d_name[0] == '.') continue;
130                 total_cnt++;
131                 package = _desktop_to_package(entry.d_name);
132                 if (!package) {
133                         _E("Failed to convert file to package[%s]", entry.d_name);
134                         continue;
135                 }
136
137                 if (ail_desktop_add(package) != AIL_ERROR_OK) {
138                         _E("Failed to add a package[%s]", package);
139                 } else {
140                         ok_cnt++;
141                 }
142                 free(package);
143         }
144
145         _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
146         closedir(dir);
147
148         return AIL_ERROR_OK;
149 }
150
151
152
153 static int initdb_change_perm(const char *db_file)
154 {
155         char buf[BUFSZE];
156         char journal_file[BUFSZE];
157         char *files[3];
158         int ret, i;
159
160         files[0] = (char *)db_file;
161         files[1] = journal_file;
162         files[2] = NULL;
163
164         retv_if(!db_file, AIL_ERROR_FAIL);
165
166         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
167
168         for (i = 0; files[i]; i++) {
169                 ret = chown(files[i], OWNER_ROOT, OWNER_ROOT);
170                 if (ret == -1) {
171                         strerror_r(errno, buf, sizeof(buf));
172                         _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf);
173                         return AIL_ERROR_FAIL;
174                 }
175
176                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
177                 if (ret == -1) {
178                         strerror_r(errno, buf, sizeof(buf));
179                         _E("FAIL : chmod %s 0664, because %s", db_file, buf);
180                         return AIL_ERROR_FAIL;
181                 }
182         }
183
184         return AIL_ERROR_OK;
185 }
186
187
188 static int __is_authorized()
189 {
190         /* ail_init db should be called by as root privilege. */
191
192         uid_t uid = getuid();
193         if ((uid_t) GLOBAL_USER == uid)
194                 return 1;
195         else
196                 return 0;
197 }
198
199 int xsystem(const char *argv[])
200 {
201         int status = 0;
202         pid_t pid;
203         pid = fork();
204         switch (pid) {
205         case -1:
206                 perror("fork failed");
207                 return -1;
208         case 0:
209                 /* child */
210                 execvp(argv[0], (char *const *)argv);
211                 _exit(-1);
212         default:
213                 /* parent */
214                 break;
215         }
216         if (waitpid(pid, &status, 0) == -1) {
217                 perror("waitpid failed");
218                 return -1;
219         }
220         if (WIFSIGNALED(status)) {
221                 perror("signal");
222                 return -1;
223         }
224         if (!WIFEXITED(status)) {
225                 /* shouldn't happen */
226                 perror("should not happen");
227                 return -1;
228         }
229         return WEXITSTATUS(status);
230 }
231
232 int main(int argc, char *argv[])
233 {
234         int ret;
235
236         if (!__is_authorized()) {
237                 fprintf(stderr, "You are not an authorized user!\n");
238                 _D("You are not root user!\n");
239     }
240     else {
241         const char *argv_rm[] = { "/bin/rm", APP_INFO_DB_FILE, NULL };
242         xsystem(argv_rm);
243         const char *argv_rmjn[] = { "/bin/rm", APP_INFO_DB_FILE_JOURNAL, NULL };
244         xsystem(argv_rmjn);
245     }
246
247         ret = setenv("AIL_INITDB", "1", 1);
248         _D("AIL_INITDB : %d", ret);
249
250         ret = initdb_count_app();
251         if (ret > 0) {
252                 _D("Some Apps in the App Info DB.");
253         }
254
255         ret = initdb_load_directory(OPT_DESKTOP_DIRECTORY);
256         if (ret == AIL_ERROR_FAIL) {
257                 _E("cannot load opt desktop directory.");
258         }
259
260         ret = initdb_load_directory(USR_DESKTOP_DIRECTORY);
261         if (ret == AIL_ERROR_FAIL) {
262                 _E("cannot load usr desktop directory.");
263         }
264
265         if (__is_authorized()) {
266                 ret = initdb_change_perm(APP_INFO_DB_FILE);
267                 if (ret == AIL_ERROR_FAIL) {
268                         _E("cannot chown.");
269                 }
270                 const char *argv_smack[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE, NULL };
271                 xsystem(argv_smack);
272                 const char *argv_smackjn[] = { "/usr/bin/chsmack", "-a", APP_INFO_DB_LABEL, APP_INFO_DB_FILE_JOURNAL, NULL };
273                 xsystem(argv_smackjn);
274         }
275         return AIL_ERROR_OK;
276 }
277
278
279
280 // END