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
- [ ] Close the DB correctly in any quit case (also by SIGTERM)
- [ ] 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
+5 -3
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);
+20 -3
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,7 +63,8 @@ int check_and_create_tables() {
int res = 0;
char *err = NULL;
res = sqlite3_exec(db,
res =
sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS 'users' ("
" 'id' INTEGER NOT NULL UNIQUE,"
" 'username' VARCHAR(50) NOT NULL UNIQUE,"
@@ -82,7 +94,8 @@ int check_and_create_tables() {
");",
NULL, NULL, &err);
print_err(&err);
res = sqlite3_exec(db,
res =
sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS 'files' ("
" 'id'INTEGER NOT NULL UNIQUE,"
" 'user_id' INTEGER NULL,"
@@ -103,6 +116,7 @@ int check_and_create_tables() {
}
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;
}