summaryrefslogtreecommitdiff
path: root/tool/sp_initdb.c
blob: 8083b935b82de87c69c202c1257320c5b8cb0d44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/smack.h>
#include <glib.h>
#include <sqlite3.h>
#include <tzplatform_config.h>
#include <system_info.h>

#define CREATE_SYSPOPUP_INFO_TABLE \
"PRAGMA journal_mode = PERSIST; " \
"CREATE TABLE IF NOT EXISTS syspopup_info ( " \
"	id INTEGER PRIMARY KEY, " \
"	name TEXT UNIQUE NOT NULL, " \
"	prio INTEGER, " \
"	focus INTEGER, " \
"	timeout INTEGER, " \
"	term_act INTEGER, " \
"	endkey_act INTEGER, " \
"	pkgname TEXT NOT NULL);"

#define QUERY_INSERT_SYSPOPUP_INFO \
"INSERT OR REPLACE INTO syspopup_info " \
"(name, prio, focus, timeout, term_act, endkey_act, pkgname) " \
"VALUES(?, ?, ?, ?, ?, ?, ?);"

#define PATH_DB tzplatform_mkpath(TZ_SYS_DB, ".syspopup.db")
#define PATH_DB_JOURNAL tzplatform_mkpath(TZ_SYS_DB, ".syspopup.db-journal")
#define ARRAY_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
#define DB_LABEL "User::Home"
#define ROOT_UID 0

struct syspopup_info {
	const char *name;
	int prio;
	int focus;
	int timeout;
	int term_act;
	int endkey_act;
	const char *appid;
};

static sqlite3 *db;
static struct syspopup_info default_info[] = {
	{"volume", 1, 1, -1, 1, 1, "org.tizen.volume"},
	{"bt-syspopup", 0, 0, -1, 0, 0, "org.tizen.bt-syspopup"},
	{"wifi-qs", 0, 0, -1, 0, 2, "net.wifi-qs"},
	{"powerkey-syspopup", 3, 0, -1, 0, 0, "org.tizen.powerkey-syspopup"},
	{"overheat-syspopup", 3, 0, -1, 0, 0, "org.tizen.overheat-syspopup"},
	{"lowmem-syspopup", 0, 0, 5, 0, 0, "org.tizen.lowmem-syspopup"},
	{"battery-syspopup", 0, 0, 5, 0, 0, "org.tizen.battery-syspopup"},
	{"net-popup", 0, 0, -1, 0, 0, "org.tizen.net-popup"},
	{"usb-syspopup", 1, 0, -1, 2, 2, "org.tizen.usb-syspopup"},
	{"usbotg-syspopup", 0, 1, -1, 0, 0, "org.tizen.usbotg-syspopup"},
	{"mmc-syspopup", 0, 0, -1, 0, 0, "org.tizen.mmc-syspopup"},
	{"mobileap-syspopup", 0, 0, -1, 0, 0, "org.tizen.mobileap-syspopup"},
	{"telephony-syspopup", 1, 1, 5, 0, 0, "org.tizen.telephony-syspopup"},
	{"system-syspopup", 1, 0, -1, 0, 0, "org.tizen.system-syspopup"},
	{"safetyvolume-syspopup", 1, 0, -1, 0, 0, "org.tizen.safetyvolume-syspopup"},
	{"datausage-syspopup", 1, 0, -1, 0, 0, "org.tizen.datausage-syspopup"},
	{"crash-syspopup", 1, 0, -1, 0, 0, "org.tizen.crash-syspopup"},
	{"gps-syspopup", 0, 0, -1, 0, 0, "org.tizen.gps-syspopup"},
	{"mode-syspopup", 1, 0, -1, 0, 0, "org.tizen.mode-syspopup"},
	{"d2d-conv-syspopup", 3, 0, -1, 0, 0, "org.tizen.d2d-conv-syspopup"},
	{"fido-syspopup", 3, 0, -1, 0, 0, "org.tizen.fido-syspopup"}
};
static struct syspopup_info wearable_info[] = {
	{"volume", 1, 1, -1, 1, 1, "org.tizen.windicator"},
	{"powerkey-syspopup", 3, 0, -1, 0, 0, "org.tizen.powerkey-syspopup"},
	{"lowmem-syspopup", 0, 0, 5, 0, 0, "org.tizen.lowmem-syspopup"},
	{"battery-syspopup", 0, 0, 5, 0, 0, "org.tizen.battery-syspopup"},
	{"system-syspopup", 1, 0, -1, 0, 0, "org.tizen.system-syspopup"},
	{"safetyvolume-syspopup", 3, 1, -1, 0, 0, "org.tizen.safetyvolume-syspopup"},
	{"crash-syspopup", 1, 0, -1, 0, 0, "org.tizen.crash-syspopup"},
	{"wc-syspopup", 0, 0, -1, 0, 0, "net.wc-syspopup"},
	{"bt-syspopup", 0, 0, -1, 0, 0, "org.tizen.bt-syspopup"},
	{"d2d-conv-syspopup", 3, 0, -1, 0, 0, "org.tizen.d2d-conv-syspopup"},
	{"fido-syspopup", 3, 0, -1, 0, 0, "org.tizen.fido-syspopup"},
	{"telephony-syspopup", 1, 1, 5, 0, 0, "org.tizen.telephony-syspopup"},
	{"net-popup", 0, 0, -1, 0, 0, "org.tizen.net-popup"},
};

static int __unlink_db(void)
{
	int ret;

	ret = access(PATH_DB, F_OK);
	if (ret == 0) {
		ret = unlink(PATH_DB);
		if (ret != 0) {
			printf("Failed to delete %s\n", PATH_DB);
			return -1;
		}
	}

	return 0;
}

static void __close_db(void)
{
	if (db == NULL)
		return;

	sqlite3_close(db);
	db = NULL;
}

static int __open_db(void)
{
	int ret;
	const char *query = CREATE_SYSPOPUP_INFO_TABLE;
	char *err_msg = NULL;

	ret = sqlite3_open_v2(PATH_DB, &db,
			SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
	if (ret != SQLITE_OK) {
		printf("Failed to create syspopup db - ret(%d), errmsg(%s)\n",
				ret, sqlite3_errmsg(db));
		return -1;
	}

	ret = sqlite3_exec(db, query, NULL, NULL, &err_msg);
	if (ret != SQLITE_OK) {
		printf("Failed to create syspopup info table(%s)\n", err_msg);
		sqlite3_free(err_msg);
		__close_db();
		return -1;
	}

	ret = smack_setlabel(PATH_DB, DB_LABEL, SMACK_LABEL_ACCESS);
	if (ret < 0) {
		printf("Failed to set label\n");
		__close_db();
		return -1;
	}

	if (access(PATH_DB_JOURNAL, F_OK) == 0) {
		ret = smack_setlabel(PATH_DB_JOURNAL, DB_LABEL, SMACK_LABEL_ACCESS);
		if (ret < 0) {
			printf("Failed to set label of journal file\n");
			__close_db();
			return -1;
		}
	}

	return 0;
}

static int __insert_syspopup_info(struct syspopup_info *info)
{
	int ret;
	int result = 0;
	sqlite3_stmt *stmt = NULL;
	const char *query = QUERY_INSERT_SYSPOPUP_INFO;

	if (info == NULL)
		return -1;

	ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
	if (ret != SQLITE_OK)
		return -1;

	ret = sqlite3_bind_text(stmt, 1, info->name, -1, SQLITE_TRANSIENT);
	if (ret != SQLITE_OK) {
		result = -1;
		goto end;
	}

	ret = sqlite3_bind_int(stmt, 2, info->prio);
	if (ret != SQLITE_OK) {
		result = -1;
		goto end;
	}

	ret = sqlite3_bind_int(stmt, 3, info->focus);
	if (ret != SQLITE_OK) {
		result = -1;
		goto end;
	}

	ret = sqlite3_bind_int(stmt, 4, info->timeout);
	if (ret != SQLITE_OK) {
		result = -1;
		goto end;
	}

	ret = sqlite3_bind_int(stmt, 5, info->term_act);
	if (ret != SQLITE_OK) {
		result = -1;
		goto end;
	}

	ret = sqlite3_bind_int(stmt, 6, info->endkey_act);
	if (ret != SQLITE_OK) {
		result = -1;
		goto end;
	}

	ret = sqlite3_bind_text(stmt, 7, info->appid, -1, SQLITE_TRANSIENT);
	if (ret != SQLITE_OK) {
		result = -1;
		goto end;
	}

	ret = sqlite3_step(stmt);
	if (ret != SQLITE_DONE)
		result = -1;

end:
	sqlite3_finalize(stmt);

	return result;
}

static int __init_db(void)
{
	int ret;
	struct syspopup_info *info;
	unsigned int info_size;
	unsigned int i;
	char *profile = NULL;

	system_info_get_platform_string("http://tizen.org/feature/profile",
			&profile);
	if (profile == NULL) {
		printf("Failed to get profile info\n");
		return -1;
	}

	if (*profile == 'w' || *profile == 'W') {
		info = wearable_info;
		info_size = ARRAY_SIZE(wearable_info);
	} else {
		info = default_info;
		info_size = ARRAY_SIZE(default_info);
	}
	free(profile);

	ret = __open_db();
	if (ret < 0)
		return -1;

	for (i = 0; i < info_size; i++) {
		ret = __insert_syspopup_info(&info[i]);
		if (ret < 0)
			break;
	}

	__close_db();

	return 0;
}

int main(int argc, char **argv)
{
	int ret;
	GOptionContext *context;
	GError *error = NULL;
	gpointer opt_force = NULL;
	GOptionEntry entries[] = {
		{"force", 'f', 0, G_OPTION_ARG_NONE, &opt_force, "Remove an existing db file and initialize it", NULL},
		{NULL}
	};

	if (getuid() != ROOT_UID) {
		printf("This binary should be run as root user\n");
		return -1;
	}

	context = g_option_context_new(NULL);
	g_option_context_add_main_entries(context, entries, NULL);
	if (!g_option_context_parse(context, &argc, &argv, &error)) {
		printf("%s: %s\n", argv[0], error->message);
		g_option_context_free(context);
		g_clear_error(&error);
		return -1;
	}
	g_option_context_free(context);

	if (opt_force) {
		ret = __unlink_db();
		if (ret < 0)
			return -1;
	} else {
		ret = access(PATH_DB, F_OK);
		if (ret == 0) {
			printf("The db(%s) file already exists.\n", PATH_DB);
			return 0;
		}
	}

	return __init_db();
}