Add sending wakeup wakeup signal to resourced
[platform/core/appfw/pkgmgr-info.git] / tool / pkg-db-creator.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact: Junghyun Yeon <jungh.yeon@samsung.com>,
5  * Sangyoon Jang <s89.jang@samsung.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #define _GNU_SOURCE
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include <tzplatform_config.h>
28
29 #include "pkgmgr-info.h"
30 #include "pkgmgr_parser_db.h"
31
32 #ifndef OWNER_ROOT
33 #define OWNER_ROOT 0
34 #endif
35 #ifndef GLOBAL_USER
36 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
37 #endif
38
39 static int _remove_file(const char *path)
40 {
41         if (!path)
42                 return 0;
43
44         if (access(path, F_OK) != 0)
45                 return 0;
46
47         return remove(path);
48 }
49
50 static int _remove_db(uid_t uid)
51 {
52         char *cert_db;
53         char *parser_db;
54         char journal_path[PATH_MAX];
55
56         parser_db = getUserPkgParserDBPathUID(uid);
57         if (!parser_db) {
58                 printf("Failed to get parser db path\n");
59                 return -1;
60         }
61
62         if (_remove_file(parser_db) != 0) {
63                 printf("Failed to remove parser db[%s]\n", parser_db);
64                 free(parser_db);
65                 return -1;
66         }
67
68         snprintf(journal_path, sizeof(journal_path), "%s-journal", parser_db);
69         free(parser_db);
70         if (_remove_file(journal_path) != 0) {
71                 printf("Failed to remove journal[%s]\n", journal_path);
72                 return -1;
73         }
74
75         if (uid != OWNER_ROOT && uid != GLOBAL_USER)
76                 return 0;
77
78         cert_db = getUserPkgCertDBPath();
79         if (!cert_db) {
80                 printf("Failed to get cet db path\n");
81                 return -1;
82         }
83
84         if (_remove_file(cert_db) != 0) {
85                 printf("Failed to remove parser db[%s]\n", cert_db);
86                 free(cert_db);
87                 return -1;
88         }
89
90         snprintf(journal_path, sizeof(journal_path), "%s-journal", cert_db);
91         free(cert_db);
92         if (_remove_file(journal_path) != 0) {
93                 printf("Failed to remove journal[%s]\n", journal_path);
94                 return -1;
95         }
96
97         return 0;
98 }
99
100 int main(int argc, char *argv[])
101 {
102         int uid;
103         int ret = 0;
104
105         if ((int)getuid() > OWNER_ROOT) {
106                 printf("This cmd is not allowed for regular user\n");
107                 return -1;
108         }
109
110         if (argc == 1) {
111                 printf("argument should be provided\n");
112                 return -1;
113         }
114
115         uid = atoi(argv[1]);
116         ret = _remove_db((uid_t)uid);
117         if (ret != 0)
118                 printf("failed to remove database for uid[%d]\n", uid);
119
120         ret = pkgmgr_parser_initialize_parser_db((uid_t)uid);
121         if (ret != 0) {
122                 printf("failed to create parser db for uid [%d], err[%d]\n", uid, ret);
123                 return -1;
124
125         }
126
127         if (uid == 0) {
128                 ret = pkgmgr_parser_initialize_cert_db();
129                 if (ret != 0) {
130                         printf("failed to create cert db for uid [%d], err[%d]\n", uid, ret);
131                         return -1;
132                 }
133         }
134
135         return 0;
136 }