2 ** Copyright 2010, Adam Shanks (@ChainsDD)
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.
20 #include <cutils/log.h>
26 // { int* pint; pint=(int*)data; ++(*pint); }
28 sqlite3 *database_init()
33 rc = sqlite3_open_v2(REQUESTOR_DATABASE_PATH, &db, SQLITE_OPEN_READONLY, NULL);
35 LOGE("Couldn't open database: %s", sqlite3_errmsg(db));
39 // Create an automatic busy handler in case the db is locked
40 sqlite3_busy_timeout(db, 1000);
44 int database_check(sqlite3 *db, struct su_initiator *from, struct su_request *to)
50 int allow = DB_INTERACTIVE;
54 "SELECT _id,name,allow FROM apps WHERE uid=%u AND exec_uid=%u AND exec_cmd='%q';",
55 (unsigned)from->uid, to->uid, to->command
58 if (strlen(sql) >= sizeof(sql)-1)
61 int error = sqlite3_get_table(db, sql, &result, &nrow, &ncol, &zErrmsg);
62 if (error != SQLITE_OK) {
63 LOGE("Database check failed with error message %s", zErrmsg);
64 if (error == SQLITE_BUSY) {
65 LOGE("Specifically, the database is busy");
70 if (nrow == 0 || ncol != 3)
73 if (strcmp(result[0], "_id") == 0 && strcmp(result[2], "allow") == 0) {
74 if (strcmp(result[5], "1") == 0) {
76 } else if (strcmp(result[5], "-1") == 0){
77 allow = DB_INTERACTIVE;
84 sqlite3_free_table(result);