2 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/types.h>
23 #include <sys/smack.h>
26 #include <tzplatform_config.h>
27 #include <system_info.h>
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, " \
36 " timeout INTEGER, " \
37 " term_act INTEGER, " \
38 " endkey_act INTEGER, " \
39 " pkgname TEXT NOT NULL);"
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(?, ?, ?, ?, ?, ?, ?);"
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"
52 struct syspopup_info {
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"}
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"},
102 static int __unlink_db(void)
106 ret = access(PATH_DB, F_OK);
108 ret = unlink(PATH_DB);
110 printf("Failed to delete %s\n", PATH_DB);
118 static void __close_db(void)
127 static int __open_db(void)
130 const char *query = CREATE_SYSPOPUP_INFO_TABLE;
131 char *err_msg = NULL;
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));
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);
149 ret = smack_setlabel(PATH_DB, DB_LABEL, SMACK_LABEL_ACCESS);
151 printf("Failed to set label\n");
156 if (access(PATH_DB_JOURNAL, F_OK) == 0) {
157 ret = smack_setlabel(PATH_DB_JOURNAL, DB_LABEL, SMACK_LABEL_ACCESS);
159 printf("Failed to set label of journal file\n");
168 static int __insert_syspopup_info(struct syspopup_info *info)
172 sqlite3_stmt *stmt = NULL;
173 const char *query = QUERY_INSERT_SYSPOPUP_INFO;
178 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
179 if (ret != SQLITE_OK)
182 ret = sqlite3_bind_text(stmt, 1, info->name, -1, SQLITE_TRANSIENT);
183 if (ret != SQLITE_OK) {
188 ret = sqlite3_bind_int(stmt, 2, info->prio);
189 if (ret != SQLITE_OK) {
194 ret = sqlite3_bind_int(stmt, 3, info->focus);
195 if (ret != SQLITE_OK) {
200 ret = sqlite3_bind_int(stmt, 4, info->timeout);
201 if (ret != SQLITE_OK) {
206 ret = sqlite3_bind_int(stmt, 5, info->term_act);
207 if (ret != SQLITE_OK) {
212 ret = sqlite3_bind_int(stmt, 6, info->endkey_act);
213 if (ret != SQLITE_OK) {
218 ret = sqlite3_bind_text(stmt, 7, info->appid, -1, SQLITE_TRANSIENT);
219 if (ret != SQLITE_OK) {
224 ret = sqlite3_step(stmt);
225 if (ret != SQLITE_DONE)
229 sqlite3_finalize(stmt);
234 static int __init_db(void)
237 struct syspopup_info *info;
238 unsigned int info_size;
240 char *profile = NULL;
242 system_info_get_platform_string("http://tizen.org/feature/profile",
244 if (profile == NULL) {
245 printf("Failed to get profile info\n");
249 if (*profile == 'w' || *profile == 'W') {
250 info = wearable_info;
251 info_size = ARRAY_SIZE(wearable_info);
254 info_size = ARRAY_SIZE(default_info);
262 for (i = 0; i < info_size; i++) {
263 ret = __insert_syspopup_info(&info[i]);
273 int main(int argc, char **argv)
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},
284 if (getuid() != ROOT_UID) {
285 printf("This binary should be run as root user\n");
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);
297 g_option_context_free(context);
304 ret = access(PATH_DB, F_OK);
306 printf("The db(%s) file already exists.\n", PATH_DB);