Fix Prevent and remove compile warning messages
[platform/core/appfw/ail.git] / tool / src / initdb_user.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 #include <sys/smack.h>
32
33 #include "ail.h"
34 #include "ail_db.h"
35 #include "ail_private.h"
36
37 #ifdef _E
38 #undef _E
39 #endif
40 #define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
41
42 #ifdef _D
43 #undef _D
44 #endif
45 #define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg)
46
47 #define SET_DEFAULT_LABEL(x) \
48         do { \
49                 if (smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) \
50                         _E("failed chsmack -a \"*\" %s", x); \
51                 else \
52                         _D("chsmack -a \"*\" %s", x); \
53         } while (0)
54
55 static int initdb_user_count_app(void)
56 {
57         ail_filter_h filter;
58         ail_error_e ret;
59         int total = 0;
60
61         ret = ail_filter_new(&filter);
62         if (ret != AIL_ERROR_OK)
63                 return -1;
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_usr_appinfo(filter, &total, getuid());
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 char *_desktop_to_package(const char* desktop)
82 {
83         char *package, *tmp;
84
85         retv_if(!desktop, NULL);
86
87         package = strdup(desktop);
88         retv_if(!package, NULL);
89
90         tmp = strrchr(package, '.');
91         if (tmp == NULL) {
92                 _E("[%s] is not a desktop file", package);
93                 free(package);
94                 return NULL;
95         }
96
97         if (strcmp(tmp, ".desktop")) {
98                 _E("%s is not a desktop file", desktop);
99                 free(package);
100                 return NULL;
101         }
102
103         *tmp = '\0';
104
105         return package;
106 }
107
108 int initdb_user_load_directory(const char *directory)
109 {
110         DIR *dir;
111         struct dirent entry, *result;
112         int ret;
113         char buf[BUFSZE];
114         int total_cnt = 0;
115         int ok_cnt = 0;
116
117         /* desktop file */
118         dir = opendir(directory);
119         if (!dir) {
120                 if (strerror_r(errno, buf, sizeof(buf)) == 0)
121                         _E("Failed to access the [%s] because %s\n", directory, buf);
122                 return AIL_ERROR_FAIL;
123         }
124
125         _D("Loading desktop files from %s", directory);
126
127         for (ret = readdir_r(dir, &entry, &result);
128                         ret == 0 && result != NULL;
129                         ret = readdir_r(dir, &entry, &result)) {
130                 char *package;
131
132                 if (entry.d_name[0] == '.') continue;
133                 total_cnt++;
134                 package = _desktop_to_package(entry.d_name);
135                 if (!package) {
136                         _E("Failed to convert file to package[%s]", entry.d_name);
137                         continue;
138                 }
139
140                 if (ail_usr_desktop_add(package, getuid()) != AIL_ERROR_OK)
141                         _E("Failed to add a package[%s]", package);
142                 else
143                         ok_cnt++;
144
145                 free(package);
146         }
147
148         _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt);
149         closedir(dir);
150
151         return AIL_ERROR_OK;
152 }
153
154 static int __is_authorized()
155 {
156         /* ail_init db should be called by an user. */
157         uid_t uid = getuid();
158         if ((uid_t)OWNER_ROOT != uid)
159                 return 1;
160         else
161                 return 0;
162 }
163
164 int main(int argc, char *argv[])
165 {
166         int ret;
167         char *db;
168         char *path;
169
170         if (!__is_authorized()) {
171                 fprintf(stderr, "You are not an authorized user!\n");
172                 _D("You are root user! Please switch to a regular user\n");
173                 return -1;
174         }
175
176         db = ail_get_app_DB(getuid());
177         if (db) {
178                 if (remove(db))
179                         _E(" %s is not removed", db);
180
181                 free(db);
182         }
183
184         db = ail_get_app_DB_journal(getuid());
185         if (db) {
186                 if (remove(db))
187                         _E(" %s is not removed", db);
188
189                 free(db);
190         }
191
192         ret = setenv("AIL_INITDB", "1", 1);
193         _D("AIL_INITDB : %d", ret);
194
195         ret = initdb_user_count_app();
196         if (ret > 0)
197                 _D("Some Apps in the App Info DB.");
198
199         path = ail_get_desktop_path(getuid());
200         if (path == NULL) {
201                 _E("Failed to get desktop path");
202                 return -1;
203         }
204
205         ret = initdb_user_load_directory(path);
206         if (ret == AIL_ERROR_FAIL)
207                 _E("cannot load usr desktop directory.");
208
209         free(path);
210
211         return AIL_ERROR_OK;
212 }