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