SIGINT test; not finished

This commit is contained in:
2026-05-08 16:27:58 +03:00
parent cf6bdee596
commit 67c4211b52
5 changed files with 58 additions and 38 deletions
+1 -2
View File
@@ -7,8 +7,7 @@
- [x] Switch to SQLite - [x] Switch to SQLite
- [ ] Close the DB correctly in any quit case (also by SIGTERM) - [ ] Close the DB correctly in any quit case (also by SIGTERM)
- [ ] Get rid of unsigned integers - [ ] Get rid of unsigned integers
- [ ] Check registration - [x] Check registration
- [ ] Common functions for both DBs, only db_query should be different
- [x] Auto create a database when one is missing. - [x] Auto create a database when one is missing.
## Features ## Features
+1
View File
@@ -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 db_get_files_data(i_get_files_db *arg, fl_t **fl_start,
uint64_t *full_count); uint64_t *full_count);
int32_t db_user_create(i_db_user_create *); int32_t db_user_create(i_db_user_create *);
int close_connection(void);
#endif #endif
+6 -4
View File
@@ -3,8 +3,9 @@
/* TODO: variadic function for preparing parameters */ /* TODO: variadic function for preparing parameters */
#include <db.h>
#include "../main.h" #include "../main.h"
#include "db_common.h"
#include <db.h>
#include <endian.h> #include <endian.h>
#include <fcntl.h> #include <fcntl.h>
#include <libpq-fe.h> #include <libpq-fe.h>
@@ -16,7 +17,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "db_common.h"
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
#define Q_LEN 128 #define Q_LEN 128
@@ -81,6 +81,8 @@ static void processNotice(void *arg, const char *message) {
// do nothing // do nothing
} }
int close_connection(void) { return 0; };
int init_db_connection() { int init_db_connection() {
// TODO: get from external configs // TODO: get from external configs
conn = PQconnectdb("user=postgres password=dobro host=127.0.0.1 dbname=bbs"); 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 */ /* Check email */
res = PQexecParams(conn, "SELECT id FROM users WHERE email=$1", 1, NULL, 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)) if (PQresultStatus(res) != PGRES_TUPLES_OK && !PQntuples(res))
return exit_query_2(-1); return exit_query_2(-1);
@@ -211,7 +213,7 @@ int32_t db_save_file(session *s) {
paramValues[1] = sf->name; paramValues[1] = sf->name;
paramValues[2] = (char *)&size_n; paramValues[2] = (char *)&size_n;
paramValues[3] = (char *)&hash_n; paramValues[3] = (char *)&hash_n;
if (sf->description != NULL) { if (sf->description != NULL) {
paramValues[4] = sf->description; paramValues[4] = sf->description;
} else { } else {
paramValues[4] = empty_str; paramValues[4] = empty_str;
+48 -31
View File
@@ -6,6 +6,7 @@
#include <db.h> #include <db.h>
#include <endian.h> #include <endian.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <signal.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
@@ -40,6 +41,16 @@ enum db_arg_type {
typedef enum db_cb_resp (*db_callback)(sqlite3_stmt *stmt, void *resp); 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) { void print_err(char **err) {
if (*err != NULL) { if (*err != NULL) {
printf("%s\n", *err); printf("%s\n", *err);
@@ -52,20 +63,21 @@ int check_and_create_tables() {
int res = 0; int res = 0;
char *err = NULL; char *err = NULL;
res = sqlite3_exec(db, res =
"CREATE TABLE IF NOT EXISTS 'users' (" sqlite3_exec(db,
" 'id' INTEGER NOT NULL UNIQUE," "CREATE TABLE IF NOT EXISTS 'users' ("
" 'username' VARCHAR(50) NOT NULL UNIQUE," " 'id' INTEGER NOT NULL UNIQUE,"
" 'password' CHARACTER(64) NOT NULL," " 'username' VARCHAR(50) NOT NULL UNIQUE,"
" 'email' VARCHAR(255) NOT NULL UNIQUE," " 'password' CHARACTER(64) NOT NULL,"
" 'created_at' TIMESTAMP NOT NULL," " 'email' VARCHAR(255) NOT NULL UNIQUE,"
" 'last_login' TIMESTAMP NULL," " 'created_at' TIMESTAMP NOT NULL,"
" 'privileges' SMALLINT NOT NULL," " 'last_login' TIMESTAMP NULL,"
" CONSTRAINT 'users_pkey' PRIMARY KEY ('id' AUTOINCREMENT)," " 'privileges' SMALLINT NOT NULL,"
" CONSTRAINT 'users_email_key' UNIQUE ('email')," " CONSTRAINT 'users_pkey' PRIMARY KEY ('id' AUTOINCREMENT),"
" CONSTRAINT 'users_username_key' UNIQUE ('username')" " CONSTRAINT 'users_email_key' UNIQUE ('email'),"
"); ", " CONSTRAINT 'users_username_key' UNIQUE ('username')"
NULL, NULL, &err); "); ",
NULL, NULL, &err);
print_err(&err); print_err(&err);
res = sqlite3_exec(db, res = sqlite3_exec(db,
@@ -82,27 +94,29 @@ int check_and_create_tables() {
");", ");",
NULL, NULL, &err); NULL, NULL, &err);
print_err(&err); print_err(&err);
res = sqlite3_exec(db, res =
"CREATE TABLE IF NOT EXISTS 'files' (" sqlite3_exec(db,
" 'id'INTEGER NOT NULL UNIQUE," "CREATE TABLE IF NOT EXISTS 'files' ("
" 'user_id' INTEGER NULL," " 'id'INTEGER NOT NULL UNIQUE,"
" 'name' VARCHAR(255) NOT NULL," " 'user_id' INTEGER NULL,"
" 'size' BIGINT NOT NULL," " 'name' VARCHAR(255) NOT NULL,"
" 'created_at' DATE NOT NULL," " 'size' BIGINT NOT NULL,"
" 'description' TEXT NULL," " 'created_at' DATE NOT NULL,"
" 'permissions' INTEGER NULL," " 'description' TEXT NULL,"
" 'hash' INTEGER NULL," " 'permissions' INTEGER NULL,"
" CONSTRAINT 'files_pkey' PRIMARY KEY('id' AUTOINCREMENT)," " 'hash' INTEGER NULL,"
" CONSTRAINT 'fk_user' FOREIGN KEY ('user_id')" " CONSTRAINT 'files_pkey' PRIMARY KEY('id' AUTOINCREMENT),"
" REFERENCES 'users' ('id') ON DELETE NO ACTION" " CONSTRAINT 'fk_user' FOREIGN KEY ('user_id')"
" ON UPDATE NO ACTION" " REFERENCES 'users' ('id') ON DELETE NO ACTION"
");", " ON UPDATE NO ACTION"
NULL, NULL, &err); ");",
NULL, NULL, &err);
print_err(&err); print_err(&err);
return res; return res;
} }
int init_db_connection(void) { int init_db_connection(void) {
signal(SIGINT, sigterm_handler);
int res = sqlite3_open("db.sqlite", &db); int res = sqlite3_open("db.sqlite", &db);
char *err = NULL; char *err = NULL;
if (!res) { if (!res) {
@@ -112,6 +126,8 @@ int init_db_connection(void) {
return 0; return 0;
} }
int close_connection(void) { return sqlite3_close_v2(db); }
// === /* BASE FUNCTIONS */ ========================================= // === /* BASE FUNCTIONS */ =========================================
/* ATTENTION: you need to make a copy of responses in callbacks. */ /* 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) { if (res == db_success) {
char u_buf[128]; 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); sqlite3_exec(db, u_buf, NULL, NULL, NULL);
return r->uid; return r->uid;
} }
+1
View File
@@ -16,5 +16,6 @@ int main(int argc, char *argv[]) {
/* TODO: display server port */ /* TODO: display server port */
printf("Server is started!\n"); printf("Server is started!\n");
server_main_loop(&server_data); server_main_loop(&server_data);
close_connection();
return 0; return 0;
} }