diff --git a/TODO.md b/TODO.md index 4eec8e3..defa23a 100644 --- a/TODO.md +++ b/TODO.md @@ -6,9 +6,9 @@ - [x] Switch to SQLite - [ ] Close the DB correctly in any quit case (also by SIGTERM) + process signals through pselect - [ ] Get rid of unsigned integers -- [ ] Check registration -- [ ] Common functions for both DBs, only db_query should be different +- [x] Check registration - [x] Auto create a database when one is missing. ## Features diff --git a/src/db/db.h b/src/db/db.h index b7c5ce2..9837b2d 100644 --- a/src/db/db.h +++ b/src/db/db.h @@ -53,4 +53,5 @@ s_file_t *db_get_file(i_get_file_db *arg); uint64_t db_get_files_data(i_get_files_db *arg, fl_t **fl_start, uint64_t *full_count); int32_t db_user_create(i_db_user_create *); +int close_connection(void); #endif \ No newline at end of file diff --git a/src/db/db_postgresql.c b/src/db/db_postgresql.c index 0fefc5d..340375b 100644 --- a/src/db/db_postgresql.c +++ b/src/db/db_postgresql.c @@ -3,8 +3,9 @@ /* TODO: variadic function for preparing parameters */ -#include #include "../main.h" +#include "db_common.h" +#include #include #include #include @@ -16,7 +17,6 @@ #include #include #include -#include "db_common.h" #define UNUSED(x) (void)(x) #define Q_LEN 128 @@ -81,6 +81,8 @@ static void processNotice(void *arg, const char *message) { // do nothing } +int close_connection(void) { return 0; }; + int init_db_connection() { // TODO: get from external configs conn = PQconnectdb("user=postgres password=dobro host=127.0.0.1 dbname=bbs"); @@ -169,7 +171,7 @@ int32_t db_user_create(i_db_user_create *args) { /* Check email */ res = PQexecParams(conn, "SELECT id FROM users WHERE email=$1", 1, NULL, - paramValues+2, paramLengths+2, NULL, TEXT); + paramValues + 2, paramLengths + 2, NULL, TEXT); if (PQresultStatus(res) != PGRES_TUPLES_OK && !PQntuples(res)) return exit_query_2(-1); @@ -211,7 +213,7 @@ int32_t db_save_file(session *s) { paramValues[1] = sf->name; paramValues[2] = (char *)&size_n; paramValues[3] = (char *)&hash_n; - if (sf->description != NULL) { + if (sf->description != NULL) { paramValues[4] = sf->description; } else { paramValues[4] = empty_str; @@ -228,7 +230,7 @@ int32_t db_save_file(session *s) { paramLengths[4] = 0; } paramLengths[5] = sizeof(int32_t); - + paramFormats[0] = BIN; paramFormats[1] = TEXT; paramFormats[2] = BIN; diff --git a/src/db/db_sqlite.c b/src/db/db_sqlite.c index 45a36cf..f412038 100644 --- a/src/db/db_sqlite.c +++ b/src/db/db_sqlite.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,16 @@ enum db_arg_type { typedef enum db_cb_resp (*db_callback)(sqlite3_stmt *stmt, void *resp); +const char message[] = "The program is closing!\n"; + +#define SIG130 130 + +void sigterm_handler(int s) { + signal(SIGINT, sigterm_handler); + write(STDOUT_FILENO, message, sizeof(message) - 1); + /* sqlite3_close_v2(db); */ +} + void print_err(char **err) { if (*err != NULL) { printf("%s\n", *err); @@ -52,20 +63,21 @@ int check_and_create_tables() { int res = 0; char *err = NULL; - res = sqlite3_exec(db, - "CREATE TABLE IF NOT EXISTS 'users' (" - " 'id' INTEGER NOT NULL UNIQUE," - " 'username' VARCHAR(50) NOT NULL UNIQUE," - " 'password' CHARACTER(64) NOT NULL," - " 'email' VARCHAR(255) NOT NULL UNIQUE," - " 'created_at' TIMESTAMP NOT NULL," - " 'last_login' TIMESTAMP NULL," - " 'privileges' SMALLINT NOT NULL," - " CONSTRAINT 'users_pkey' PRIMARY KEY ('id' AUTOINCREMENT)," - " CONSTRAINT 'users_email_key' UNIQUE ('email')," - " CONSTRAINT 'users_username_key' UNIQUE ('username')" - "); ", - NULL, NULL, &err); + res = + sqlite3_exec(db, + "CREATE TABLE IF NOT EXISTS 'users' (" + " 'id' INTEGER NOT NULL UNIQUE," + " 'username' VARCHAR(50) NOT NULL UNIQUE," + " 'password' CHARACTER(64) NOT NULL," + " 'email' VARCHAR(255) NOT NULL UNIQUE," + " 'created_at' TIMESTAMP NOT NULL," + " 'last_login' TIMESTAMP NULL," + " 'privileges' SMALLINT NOT NULL," + " CONSTRAINT 'users_pkey' PRIMARY KEY ('id' AUTOINCREMENT)," + " CONSTRAINT 'users_email_key' UNIQUE ('email')," + " CONSTRAINT 'users_username_key' UNIQUE ('username')" + "); ", + NULL, NULL, &err); print_err(&err); res = sqlite3_exec(db, @@ -82,27 +94,29 @@ int check_and_create_tables() { ");", NULL, NULL, &err); print_err(&err); - res = sqlite3_exec(db, - "CREATE TABLE IF NOT EXISTS 'files' (" - " 'id'INTEGER NOT NULL UNIQUE," - " 'user_id' INTEGER NULL," - " 'name' VARCHAR(255) NOT NULL," - " 'size' BIGINT NOT NULL," - " 'created_at' DATE NOT NULL," - " 'description' TEXT NULL," - " 'permissions' INTEGER NULL," - " 'hash' INTEGER NULL," - " CONSTRAINT 'files_pkey' PRIMARY KEY('id' AUTOINCREMENT)," - " CONSTRAINT 'fk_user' FOREIGN KEY ('user_id')" - " REFERENCES 'users' ('id') ON DELETE NO ACTION" - " ON UPDATE NO ACTION" - ");", - NULL, NULL, &err); + res = + sqlite3_exec(db, + "CREATE TABLE IF NOT EXISTS 'files' (" + " 'id'INTEGER NOT NULL UNIQUE," + " 'user_id' INTEGER NULL," + " 'name' VARCHAR(255) NOT NULL," + " 'size' BIGINT NOT NULL," + " 'created_at' DATE NOT NULL," + " 'description' TEXT NULL," + " 'permissions' INTEGER NULL," + " 'hash' INTEGER NULL," + " CONSTRAINT 'files_pkey' PRIMARY KEY('id' AUTOINCREMENT)," + " CONSTRAINT 'fk_user' FOREIGN KEY ('user_id')" + " REFERENCES 'users' ('id') ON DELETE NO ACTION" + " ON UPDATE NO ACTION" + ");", + NULL, NULL, &err); print_err(&err); return res; } int init_db_connection(void) { + signal(SIGINT, sigterm_handler); int res = sqlite3_open("db.sqlite", &db); char *err = NULL; if (!res) { @@ -112,6 +126,8 @@ int init_db_connection(void) { return 0; } +int close_connection(void) { return sqlite3_close_v2(db); } + // === /* BASE FUNCTIONS */ ========================================= /* ATTENTION: you need to make a copy of responses in callbacks. */ @@ -234,7 +250,8 @@ int32_t db_user_auth(i_auth_t *c, o_auth_t *r) { if (res == db_success) { char u_buf[128]; - sprintf(u_buf, "UPDATE users SET last_login = date() WHERE id = %u", r->uid); + sprintf(u_buf, "UPDATE users SET last_login = date() WHERE id = %u", + r->uid); sqlite3_exec(db, u_buf, NULL, NULL, NULL); return r->uid; } diff --git a/src/main.c b/src/main.c index d766925..071a236 100644 --- a/src/main.c +++ b/src/main.c @@ -16,5 +16,6 @@ int main(int argc, char *argv[]) { /* TODO: display server port */ printf("Server is started!\n"); server_main_loop(&server_data); + close_connection(); return 0; } \ No newline at end of file