Big refactor: Add request cache and collector

This commit is contained in:
topjohnwu
2017-05-29 18:54:33 +08:00
parent 3395c84560
commit 48ace3de57
6 changed files with 314 additions and 212 deletions

53
db.c
View File

@ -9,67 +9,56 @@
#include <sqlite3.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include "magisk.h"
#include "su.h"
struct callback_data_t {
struct su_context *ctx;
policy_t policy;
};
static int database_callback(void *v, int argc, char **argv, char **azColName){
struct callback_data_t *data = (struct callback_data_t *)v;
policy_t policy = INTERACTIVE;
int i;
static int database_callback(void *v, int argc, char **argv, char **azColName) {
struct su_context *ctx = (struct su_context *) v;
policy_t policy = QUERY;
time_t until = 0;
for(i = 0; i < argc; i++) {
for(int i = 0; i < argc; i++) {
if (strcmp(azColName[i], "policy") == 0) {
if (argv[i] != NULL) {
policy = atoi(argv[i]);
}
}
else if (strcmp(azColName[i], "until") == 0) {
} else if (strcmp(azColName[i], "until") == 0) {
if (argv[i] != NULL) {
until = atol(argv[i]);
}
}
}
if (policy == DENY) {
data->policy = DENY;
return -1;
} else if (policy == ALLOW && (until == 0 || until > time(NULL))) {
data->policy = ALLOW;
// even though we allow, continue, so we can see if there's another policy
// that denies...
}
if (policy == DENY)
ctx->info->policy = DENY;
else if (policy == ALLOW && (until == 0 || until > time(NULL)))
ctx->info->policy = ALLOW;
return 0;
}
policy_t database_check(struct su_context *ctx) {
void database_check(struct su_context *ctx) {
sqlite3 *db = NULL;
// Check if file is readable
if (access(ctx->user.database_path, R_OK) == -1)
return;
char query[512];
snprintf(query, sizeof(query), "select policy, until from policies where uid=%d", ctx->from.uid);
snprintf(query, sizeof(query), "SELECT policy, until FROM policies WHERE uid=%d", ctx->info->uid % 100000);
int ret = sqlite3_open_v2(ctx->user.database_path, &db, SQLITE_OPEN_READONLY, NULL);
if (ret) {
LOGE("sqlite3 open failure: %d", ret);
LOGD("sqlite3 open failure: %s\n", sqlite3_errstr(ret));
sqlite3_close(db);
return INTERACTIVE;
return;
}
char *err = NULL;
struct callback_data_t data;
data.ctx = ctx;
data.policy = INTERACTIVE;
ret = sqlite3_exec(db, query, database_callback, &data, &err);
ret = sqlite3_exec(db, query, database_callback, ctx, &err);
sqlite3_close(db);
if (err != NULL) {
LOGE("sqlite3_exec: %s", err);
return DENY;
LOGE("sqlite3_exec: %s\n", err);
ctx->info->policy = DENY;
}
return data.policy;
}