3b5b8e2df8cc1910f357a200198cdc3c1ced4d4d
[platform/core/base/syspopup.git] / tool / sp_initdb.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/smack.h>
24 #include <glib.h>
25 #include <sqlite3.h>
26 #include <tzplatform_config.h>
27 #include <system_info.h>
28
29 #define CREATE_SYSPOPUP_INFO_TABLE \
30 "PRAGMA journal_mode = PERSIST; " \
31 "CREATE TABLE IF NOT EXISTS syspopup_info ( " \
32 "       id INTEGER PRIMARY KEY, " \
33 "       name TEXT UNIQUE NOT NULL, " \
34 "       prio INTEGER, " \
35 "       focus INTEGER, " \
36 "       timeout INTEGER, " \
37 "       term_act INTEGER, " \
38 "       endkey_act INTEGER, " \
39 "       pkgname TEXT NOT NULL);"
40
41 #define QUERY_INSERT_SYSPOPUP_INFO \
42 "INSERT OR REPLACE INTO syspopup_info " \
43 "(name, prio, focus, timeout, term_act, endkey_act, pkgname) " \
44 "VALUES(?, ?, ?, ?, ?, ?, ?);"
45
46 #define PATH_DB tzplatform_mkpath(TZ_SYS_DB, ".syspopup.db")
47 #define PATH_DB_JOURNAL tzplatform_mkpath(TZ_SYS_DB, ".syspopup.db-journal")
48 #define ARRAY_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
49 #define DB_LABEL "User::Home"
50 #define ROOT_UID 0
51
52 struct syspopup_info {
53         const char *name;
54         int prio;
55         int focus;
56         int timeout;
57         int term_act;
58         int endkey_act;
59         const char *appid;
60 };
61
62 static sqlite3 *db;
63 static struct syspopup_info default_info[] = {
64         {"volume", 1, 1, -1, 1, 1, "org.tizen.volume"},
65         {"bt-syspopup", 0, 0, -1, 0, 0, "org.tizen.bt-syspopup"},
66         {"wifi-qs", 0, 0, -1, 0, 2, "net.wifi-qs"},
67         {"powerkey-syspopup", 3, 0, -1, 0, 0, "org.tizen.powerkey-syspopup"},
68         {"overheat-syspopup", 3, 0, -1, 0, 0, "org.tizen.overheat-syspopup"},
69         {"lowmem-syspopup", 0, 0, 5, 0, 0, "org.tizen.lowmem-syspopup"},
70         {"battery-syspopup", 0, 0, 5, 0, 0, "org.tizen.battery-syspopup"},
71         {"net-popup", 0, 0, -1, 0, 0, "net.netpopup"},
72         {"usb-syspopup", 1, 0, -1, 2, 2, "org.tizen.usb-syspopup"},
73         {"usbotg-syspopup", 0, 1, -1, 0, 0, "org.tizen.usbotg-syspopup"},
74         {"mmc-syspopup", 0, 0, -1, 0, 0, "org.tizen.mmc-syspopup"},
75         {"mobileap-syspopup", 0, 0, -1, 0, 0, "org.tizen.mobileap-syspopup"},
76         {"telephony-syspopup", 1, 1, 5, 0, 0, "org.tizen.telephony-syspopup"},
77         {"system-syspopup", 1, 0, -1, 0, 0, "org.tizen.system-syspopup"},
78         {"safetyvolume-syspopup", 1, 0, -1, 0, 0, "org.tizen.safetyvolume-syspopup"},
79         {"datausage-syspopup", 1, 0, -1, 0, 0, "org.tizen.datausage-syspopup"},
80         {"crash-syspopup", 1, 0, -1, 0, 0, "org.tizen.crash-syspopup"},
81         {"gps-syspopup", 0, 0, -1, 0, 0, "org.tizen.gps-syspopup"},
82         {"mode-syspopup", 1, 0, -1, 0, 0, "org.tizen.mode-syspopup"},
83         {"d2d-conv-syspopup", 3, 0, -1, 0, 0, "org.tizen.d2d-conv-syspopup"},
84         {"fido-syspopup", 3, 0, -1, 0, 0, "org.tizen.fido-syspopup"}
85 };
86 static struct syspopup_info wearable_info[] = {
87         {"volume", 1, 1, -1, 1, 1, "org.tizen.windicator"},
88         {"powerkey-syspopup", 3, 0, -1, 0, 0, "org.tizen.powerkey-syspopup"},
89         {"lowmem-syspopup", 0, 0, 5, 0, 0, "org.tizen.lowmem-syspopup"},
90         {"battery-syspopup", 0, 0, 5, 0, 0, "org.tizen.battery-syspopup"},
91         {"system-syspopup", 1, 0, -1, 0, 0, "org.tizen.system-syspopup"},
92         {"safetyvolume-syspopup", 3, 1, -1, 0, 0, "org.tizen.safetyvolume-syspopup"},
93         {"crash-syspopup", 1, 0, -1, 0, 0, "org.tizen.crash-syspopup"},
94         {"wc-syspopup", 0, 0, -1, 0, 0, "net.wc-syspopup"},
95         {"bt-syspopup", 0, 0, -1, 0, 0, "org.tizen.bt-syspopup"},
96         {"d2d-conv-syspopup", 3, 0, -1, 0, 0, "org.tizen.d2d-conv-syspopup"},
97         {"fido-syspopup", 3, 0, -1, 0, 0, "org.tizen.fido-syspopup"},
98         {"telephony-syspopup", 1, 1, 5, 0, 0, "org.tizen.telephony-syspopup"},
99         {"net-popup", 0, 0, -1, 0, 0, "net.netpopup"},
100 };
101
102 static int __unlink_db(void)
103 {
104         int ret;
105
106         ret = access(PATH_DB, F_OK);
107         if (ret == 0) {
108                 ret = unlink(PATH_DB);
109                 if (ret != 0) {
110                         printf("Failed to delete %s\n", PATH_DB);
111                         return -1;
112                 }
113         }
114
115         return 0;
116 }
117
118 static void __close_db(void)
119 {
120         if (db == NULL)
121                 return;
122
123         sqlite3_close(db);
124         db = NULL;
125 }
126
127 static int __open_db(void)
128 {
129         int ret;
130         const char *query = CREATE_SYSPOPUP_INFO_TABLE;
131         char *err_msg = NULL;
132
133         ret = sqlite3_open_v2(PATH_DB, &db,
134                         SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
135         if (ret != SQLITE_OK) {
136                 printf("Failed to create syspopup db - ret(%d), errmsg(%s)\n",
137                                 ret, sqlite3_errmsg(db));
138                 return -1;
139         }
140
141         ret = sqlite3_exec(db, query, NULL, NULL, &err_msg);
142         if (ret != SQLITE_OK) {
143                 printf("Failed to create syspopup info table(%s)\n", err_msg);
144                 sqlite3_free(err_msg);
145                 __close_db();
146                 return -1;
147         }
148
149         ret = smack_setlabel(PATH_DB, DB_LABEL, SMACK_LABEL_ACCESS);
150         if (ret < 0) {
151                 printf("Failed to set label\n");
152                 __close_db();
153                 return -1;
154         }
155
156         if (access(PATH_DB_JOURNAL, F_OK) == 0) {
157                 ret = smack_setlabel(PATH_DB_JOURNAL, DB_LABEL, SMACK_LABEL_ACCESS);
158                 if (ret < 0) {
159                         printf("Failed to set label of journal file\n");
160                         __close_db();
161                         return -1;
162                 }
163         }
164
165         return 0;
166 }
167
168 static int __insert_syspopup_info(struct syspopup_info *info)
169 {
170         int ret;
171         int result = 0;
172         sqlite3_stmt *stmt = NULL;
173         const char *query = QUERY_INSERT_SYSPOPUP_INFO;
174
175         if (info == NULL)
176                 return -1;
177
178         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
179         if (ret != SQLITE_OK)
180                 return -1;
181
182         ret = sqlite3_bind_text(stmt, 1, info->name, -1, SQLITE_TRANSIENT);
183         if (ret != SQLITE_OK) {
184                 result = -1;
185                 goto end;
186         }
187
188         ret = sqlite3_bind_int(stmt, 2, info->prio);
189         if (ret != SQLITE_OK) {
190                 result = -1;
191                 goto end;
192         }
193
194         ret = sqlite3_bind_int(stmt, 3, info->focus);
195         if (ret != SQLITE_OK) {
196                 result = -1;
197                 goto end;
198         }
199
200         ret = sqlite3_bind_int(stmt, 4, info->timeout);
201         if (ret != SQLITE_OK) {
202                 result = -1;
203                 goto end;
204         }
205
206         ret = sqlite3_bind_int(stmt, 5, info->term_act);
207         if (ret != SQLITE_OK) {
208                 result = -1;
209                 goto end;
210         }
211
212         ret = sqlite3_bind_int(stmt, 6, info->endkey_act);
213         if (ret != SQLITE_OK) {
214                 result = -1;
215                 goto end;
216         }
217
218         ret = sqlite3_bind_text(stmt, 7, info->appid, -1, SQLITE_TRANSIENT);
219         if (ret != SQLITE_OK) {
220                 result = -1;
221                 goto end;
222         }
223
224         ret = sqlite3_step(stmt);
225         if (ret != SQLITE_DONE)
226                 result = -1;
227
228 end:
229         sqlite3_finalize(stmt);
230
231         return result;
232 }
233
234 static int __init_db(void)
235 {
236         int ret;
237         struct syspopup_info *info;
238         unsigned int info_size;
239         unsigned int i;
240         char *profile = NULL;
241
242         system_info_get_platform_string("http://tizen.org/feature/profile",
243                         &profile);
244         if (profile == NULL) {
245                 printf("Failed to get profile info\n");
246                 return -1;
247         }
248
249         if (*profile == 'w' || *profile == 'W') {
250                 info = wearable_info;
251                 info_size = ARRAY_SIZE(wearable_info);
252         } else {
253                 info = default_info;
254                 info_size = ARRAY_SIZE(default_info);
255         }
256         free(profile);
257
258         ret = __open_db();
259         if (ret < 0)
260                 return -1;
261
262         for (i = 0; i < info_size; i++) {
263                 ret = __insert_syspopup_info(&info[i]);
264                 if (ret < 0)
265                         break;
266         }
267
268         __close_db();
269
270         return 0;
271 }
272
273 int main(int argc, char **argv)
274 {
275         int ret;
276         GOptionContext *context;
277         GError *error = NULL;
278         gpointer opt_force = NULL;
279         GOptionEntry entries[] = {
280                 {"force", 'f', 0, G_OPTION_ARG_NONE, &opt_force, "Remove an existing db file and initialize it", NULL},
281                 {NULL}
282         };
283
284         if (getuid() != ROOT_UID) {
285                 printf("This binary should be run as root user\n");
286                 return -1;
287         }
288
289         context = g_option_context_new(NULL);
290         g_option_context_add_main_entries(context, entries, NULL);
291         if (!g_option_context_parse(context, &argc, &argv, &error)) {
292                 printf("%s: %s\n", argv[0], error->message);
293                 g_option_context_free(context);
294                 g_clear_error(&error);
295                 return -1;
296         }
297         g_option_context_free(context);
298
299         if (opt_force) {
300                 ret = __unlink_db();
301                 if (ret < 0)
302                         return -1;
303         } else {
304                 ret = access(PATH_DB, F_OK);
305                 if (ret == 0) {
306                         printf("The db(%s) file already exists.\n", PATH_DB);
307                         return 0;
308                 }
309         }
310
311         return __init_db();
312 }