Fix Prevent and remove compile warning messages
[platform/core/appfw/ail.git] / tool / src / syncdb.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  * Copyright (C) 2013-2014 Intel Corporation.
6  *
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>
9  *
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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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.
21  *
22  */
23
24 #define _GNU_SOURCE
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <dirent.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <errno.h>
34 #include <sys/smack.h>
35
36 #include "ail.h"
37 #include "ail_private.h"
38 #include "ail_db.h"
39
40
41 #ifdef _E
42 #undef _E
43 #endif
44 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
45
46 #ifdef _D
47 #undef _D
48 #endif
49 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
50
51 #define SET_DEFAULT_LABEL(x) \
52         do { \
53                 if (smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) \
54                         _E("failed chsmack -a \"*\" %s", x); \
55                 else \
56                         _D("chsmack -a \"*\" %s", x); \
57         } while (0)
58
59 char* _desktop_to_package(const char* desktop)
60 {
61         char *package, *tmp;
62
63         retv_if(!desktop, NULL);
64
65         package = strdup(desktop);
66         retv_if(!package, NULL);
67
68         tmp = strrchr(package, '.');
69         if (tmp == NULL) {
70                 _E("[%s] is not a desktop file", package);
71                 free(package);
72                 return NULL;
73         }
74
75         if (strcmp(tmp, ".desktop")) {
76                 _E("%s is not a desktop file", desktop);
77                 free(package);
78                 return NULL;
79         }
80
81         *tmp = '\0';
82
83         return package;
84 }
85
86 int syncdb_load_directory(const char *directory)
87 {
88         DIR *dir;
89         struct dirent entry, *result;
90         int ret;
91         char buf[BUFSZE];
92         int total_cnt = 0;
93         int ok_cnt = 0;
94
95         /* desktop file */
96         dir = opendir(directory);
97         if (!dir) {
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;
101         }
102
103         _D("Loading desktop files from %s", directory);
104
105         for (ret = readdir_r(dir, &entry, &result);
106                         ret == 0 && result != NULL;
107                         ret = readdir_r(dir, &entry, &result)) {
108                 char *package;
109
110                 if (entry.d_name[0] == '.') continue;
111                 total_cnt++;
112                 package = _desktop_to_package(entry.d_name);
113                 if (!package) {
114                         _E("Failed to convert file to package[%s]", entry.d_name);
115                         continue;
116                 }
117
118                 if (ail_desktop_add(package) != AIL_ERROR_OK)
119                         _E("Failed to add a package[%s]", package);
120                 else
121                         ok_cnt++;
122                 free(package);
123         }
124
125         _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
126         closedir(dir);
127
128         return AIL_ERROR_OK;
129 }
130
131 static int __is_authorized(void)
132 {
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)
138                 return 1;
139         else
140                 return 0;
141 }
142
143 int xsystem(const char *argv[])
144 {
145         int status = 0;
146         pid_t pid;
147         pid = fork();
148         switch (pid) {
149         case -1:
150                 perror("fork failed");
151                 return -1;
152         case 0:
153                 /* child */
154                 execvp(argv[0], (char *const *)argv);
155                 _exit(-1);
156         default:
157                 /* parent */
158                 break;
159         }
160         if (waitpid(pid, &status, 0) == -1) {
161                 perror("waitpid failed");
162                 return -1;
163         }
164         if (WIFSIGNALED(status)) {
165                 perror("signal");
166                 return -1;
167         }
168         if (!WIFEXITED(status)) {
169                 /* shouldn't happen */
170                 perror("should not happen");
171                 return -1;
172         }
173         return WEXITSTATUS(status);
174 }
175
176 int main(int argc, char *argv[])
177 {
178         int ret;
179
180         if (!__is_authorized()) {
181                 fprintf(stderr, "You are not an authorized user!\n");
182                 _D("You are not root user!\n");
183                 return -1;
184         }
185
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;
189         }
190
191         ret = setenv("AIL_INITDB", "1", 1);
192         _D("AIL_INITDB : %d", ret);
193
194         if (setresuid(GLOBAL_USER, GLOBAL_USER, OWNER_ROOT) != 0)
195                 _E("setresuid() is failed.");
196
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;
200         }
201
202         ret = syncdb_load_directory(USR_DESKTOP_DIRECTORY);
203         if (ret == AIL_ERROR_FAIL)
204                 _E("cannot load usr desktop directory.");
205
206         return AIL_ERROR_OK;
207 }