SIGINT test; not finished

This commit is contained in:
2026-05-08 16:41:31 +03:00
parent cf6bdee596
commit 8d958833ca
5 changed files with 59 additions and 38 deletions
+2 -2
View File
@@ -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
+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 *full_count);
int32_t db_user_create(i_db_user_create *);
int close_connection(void);
#endif
+6 -4
View File
@@ -3,8 +3,9 @@
/* TODO: variadic function for preparing parameters */
#include <db.h>
#include "../main.h"
#include "db_common.h"
#include <db.h>
#include <endian.h>
#include <fcntl.h>
#include <libpq-fe.h>
@@ -16,7 +17,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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;
+48 -31
View File
@@ -6,6 +6,7 @@
#include <db.h>
#include <endian.h>
#include <openssl/sha.h>
#include <signal.h>
#include <sqlite3.h>
#include <stdarg.h>
#include <stdbool.h>
@@ -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;
}
+1
View File
@@ -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;
}