Fix ail_createdb is exited after removing old db files.
[platform/core/appfw/ail.git] / tool / src / createdb.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
25
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <dirent.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <sys/smack.h>
34
35 #include "ail.h"
36 #include "ail_private.h"
37 #include "ail_db.h"
38
39
40 #ifdef _E
41 #undef _E
42 #endif
43 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
44
45 #ifdef _D
46 #undef _D
47 #endif
48 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
49
50 #define SET_DEFAULT_LABEL(x) \
51         if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \
52         else _D("chsmack -a \"*\" %s", x)
53
54 static int createb_count_app(void)
55 {
56         ail_filter_h filter;
57         ail_error_e ret;
58         int total = 0;
59
60         ret = ail_filter_new(&filter);
61         if (ret != AIL_ERROR_OK) {
62                 return -1;
63         }
64
65         ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
66         if (ret != AIL_ERROR_OK) {
67                 ail_filter_destroy(filter);
68                 return -1;
69         }
70         ret = ail_filter_count_appinfo(filter, &total);
71         if (ret != AIL_ERROR_OK) {
72                 ail_filter_destroy(filter);
73                 return -1;
74         }
75
76         ail_filter_destroy(filter);
77
78         return total;
79 }
80
81
82 static int createdb_change_perm(const char *db_file)
83 {
84         char buf[BUFSZE];
85         char journal_file[BUFSZE];
86         char *files[3];
87         int ret, i;
88
89         files[0] = (char *)db_file;
90         files[1] = journal_file;
91         files[2] = NULL;
92
93         retv_if(!db_file, AIL_ERROR_FAIL);
94
95         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
96
97         for (i = 0; files[i]; i++) {
98                 ret = chown(files[i], GLOBAL_USER, OWNER_ROOT);
99                 if (ret == -1) {
100                         strerror_r(errno, buf, sizeof(buf));
101                         _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf);
102                         return AIL_ERROR_FAIL;
103                 }
104
105                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
106                 if (ret == -1) {
107                         strerror_r(errno, buf, sizeof(buf));
108                         _E("FAIL : chmod %s 0664, because %s", db_file, buf);
109                         return AIL_ERROR_FAIL;
110                 }
111         }
112
113         return AIL_ERROR_OK;
114 }
115
116
117 static int __is_authorized()
118 {
119         /* ail_init db should be called by as root privilege. */
120
121         uid_t uid = getuid();
122         uid_t euid = geteuid();
123         //euid need to be root to allow smack label changes during initialization
124         if ((uid_t) OWNER_ROOT == uid)
125                 return 1;
126         else
127                 return 0;
128 }
129
130 int xsystem(const char *argv[])
131 {
132         int status = 0;
133         pid_t pid;
134         pid = fork();
135         switch (pid) {
136         case -1:
137                 perror("fork failed");
138                 return -1;
139         case 0:
140                 /* child */
141                 execvp(argv[0], (char *const *)argv);
142                 _exit(-1);
143         default:
144                 /* parent */
145                 break;
146         }
147         if (waitpid(pid, &status, 0) == -1) {
148                 perror("waitpid failed");
149                 return -1;
150         }
151         if (WIFSIGNALED(status)) {
152                 perror("signal");
153                 return -1;
154         }
155         if (!WIFEXITED(status)) {
156                 /* shouldn't happen */
157                 perror("should not happen");
158                 return -1;
159         }
160         return WEXITSTATUS(status);
161 }
162
163 int main(int argc, char *argv[])
164 {
165         int ret;
166
167         if (!__is_authorized()) {
168                 fprintf(stderr, "You are not an authorized user!\n");
169                 _D("You are not root user!\n");
170         }
171         else {
172                 if(remove(APP_INFO_DB_FILE))
173                         _E(" %s is not removed",APP_INFO_DB_FILE);
174                 if(remove(APP_INFO_DB_FILE_JOURNAL))
175                         _E(" %s is not removed",APP_INFO_DB_FILE_JOURNAL);
176         }
177         ret = setenv("AIL_INITDB", "1", 1);
178         _D("AIL_INITDB : %d", ret);
179         setresuid(GLOBAL_USER, GLOBAL_USER, OWNER_ROOT);
180
181         if (db_open(DB_OPEN_RW, GLOBAL_USER) != AIL_ERROR_OK) {
182                 _E("Fail to create system databases");
183                 return AIL_ERROR_DB_FAILED;
184         }
185
186         setuid(OWNER_ROOT);
187         ret = createdb_change_perm(APP_INFO_DB_FILE);
188         if (ret == AIL_ERROR_FAIL) {
189                 _E("cannot chown.");
190         }
191         SET_DEFAULT_LABEL(APP_INFO_DB_FILE);
192         SET_DEFAULT_LABEL(APP_INFO_DB_FILE_JOURNAL);
193
194         return AIL_ERROR_OK;
195 }
196
197
198
199 // END