add ail_createdb & ail_syncdb
[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
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_db.h"
37 #include "ail_private.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
55 static int __is_authorized()
56 {
57         /* ail_init db should be called by an user. */
58
59         uid_t uid = getuid();
60         if ((uid_t) OWNER_ROOT != uid)
61                 return 1;
62         else
63                 return 0;
64 }
65
66
67 int main(int argc, char *argv[])
68 {
69         int ret;
70
71         if (!__is_authorized()) {
72                 fprintf(stderr, "You are not an authorized user!\n");
73                 _D("You are root user! Please switch to a regular user\n");
74                 return -1;
75         }
76         else {
77                 if(remove(ail_get_app_DB(getuid())))
78                         _E(" %s is not removed", ail_get_app_DB(getuid()));
79                 if(remove(ail_get_app_DB_journal(getuid())))
80                         _E(" %s is not removed", ail_get_app_DB_journal(getuid()));
81         }
82
83         ret = setenv("AIL_INITDB", "1", 1);
84         _D("AIL_INITDB : %d", ret);
85         if (db_open(DB_OPEN_RW, getuid()) != AIL_ERROR_OK) {
86                 _E("Fail to create system databases");
87                 return AIL_ERROR_DB_FAILED;
88         }
89
90         return AIL_ERROR_OK;
91 }
92
93
94
95 // END