Fix Prevent and remove compile warning messages
[platform/core/appfw/ail.git] / tool / src / createdb_user.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 #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 __is_authorized(void)
56 {
57         /* ail_init db should be called by an user. */
58         uid_t uid = getuid();
59         if ((uid_t)OWNER_ROOT != uid)
60                 return 1;
61         else
62                 return 0;
63 }
64
65 int main(int argc, char *argv[])
66 {
67         int ret;
68         char *db;
69
70         if (!__is_authorized()) {
71                 fprintf(stderr, "You are not an authorized user!\n");
72                 _E("You are root user! Please switch to a regular user");
73                 return -1;
74         }
75
76         db = ail_get_app_DB(getuid());
77         if (db) {
78                 if (remove(db))
79                         _E("%s is not removed", db);
80
81                 free(db);
82         }
83
84         db = ail_get_app_DB_journal(getuid());
85         if (db) {
86                 if (remove(db))
87                         _E("%s is not removed", db);
88
89                 free(db);
90         }
91
92         ret = setenv("AIL_INITDB", "1", 1);
93         _D("AIL_INITDB : %d", ret);
94
95         if (db_open(DB_OPEN_RW, getuid()) != AIL_ERROR_OK) {
96                 _E("Fail to create system databases");
97                 return AIL_ERROR_DB_FAILED;
98         }
99
100         return AIL_ERROR_OK;
101 }