SIGINT test; not finished
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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");
|
||||||
|
|||||||
+20
-3
@@ -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,7 +63,8 @@ int check_and_create_tables() {
|
|||||||
int res = 0;
|
int res = 0;
|
||||||
char *err = NULL;
|
char *err = NULL;
|
||||||
|
|
||||||
res = sqlite3_exec(db,
|
res =
|
||||||
|
sqlite3_exec(db,
|
||||||
"CREATE TABLE IF NOT EXISTS 'users' ("
|
"CREATE TABLE IF NOT EXISTS 'users' ("
|
||||||
" 'id' INTEGER NOT NULL UNIQUE,"
|
" 'id' INTEGER NOT NULL UNIQUE,"
|
||||||
" 'username' VARCHAR(50) NOT NULL UNIQUE,"
|
" 'username' VARCHAR(50) NOT NULL UNIQUE,"
|
||||||
@@ -82,7 +94,8 @@ int check_and_create_tables() {
|
|||||||
");",
|
");",
|
||||||
NULL, NULL, &err);
|
NULL, NULL, &err);
|
||||||
print_err(&err);
|
print_err(&err);
|
||||||
res = sqlite3_exec(db,
|
res =
|
||||||
|
sqlite3_exec(db,
|
||||||
"CREATE TABLE IF NOT EXISTS 'files' ("
|
"CREATE TABLE IF NOT EXISTS 'files' ("
|
||||||
" 'id'INTEGER NOT NULL UNIQUE,"
|
" 'id'INTEGER NOT NULL UNIQUE,"
|
||||||
" 'user_id' INTEGER NULL,"
|
" 'user_id' INTEGER NULL,"
|
||||||
@@ -103,6 +116,7 @@ int check_and_create_tables() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user