aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-07-06 15:31:44 +0200
committertomsmeding <tom.smeding@gmail.com>2017-07-06 15:31:44 +0200
commitd77281680b96dad90a774998cc759dd676954806 (patch)
tree100c686c843bc75f327974585e909805709f9e3a
parenta2cc9e73ca3e5d20418ccc2922576b9f5cee357e (diff)
server: Fix multithreading with firebase
-rw-r--r--db.c6
-rw-r--r--db.h1
-rw-r--r--firebase.c19
3 files changed, 25 insertions, 1 deletions
diff --git a/db.c b/db.c
index f48148e..32a8df4 100644
--- a/db.c
+++ b/db.c
@@ -22,6 +22,7 @@ void db_init(void){
SQLITE(config,SQLITE_CONFIG_SERIALIZED);
SQLITE(initialize);
SQLITE(open_v2,"db.db",&database,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE,NULL);
+ SQLITE(busy_timeout,database,500);
char *str=malloc(schema_sql_len+1,char);
memcpy(str,schema_sql,schema_sql_len);
str[schema_sql_len]='\0';
@@ -29,6 +30,11 @@ void db_init(void){
free(str);
}
+void db_reinit(void){
+ SQLITE(open_v2,"db.db",&database,SQLITE_OPEN_READWRITE,NULL);
+ SQLITE(busy_timeout,database,500);
+}
+
void db_close(void){
sqlite3_close(database);
SQLITE(shutdown);
diff --git a/db.h b/db.h
index 6b1d92b..a56bc70 100644
--- a/db.h
+++ b/db.h
@@ -35,6 +35,7 @@ struct db_strings_list{
};
void db_init(void);
+void db_reinit(void); // intended for forked threads
void db_close(void);
struct db_name_id db_create_room(void);
diff --git a/firebase.c b/firebase.c
index 5221af7..641dafe 100644
--- a/firebase.c
+++ b/firebase.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+#include <errno.h>
#include <assert.h>
#include "db.h"
#include "firebase.h"
@@ -33,7 +34,12 @@ static void script_output_listener(FILE *js_read){
char *line=NULL;
size_t linecap=0;
ssize_t linelen;
- while((linelen=getline(&line,&linecap,js_read))>0){
+ while(true){
+ linelen=getline(&line,&linecap,js_read);
+ if(linelen<0){
+ if(errno==EINTR)continue;
+ break;
+ }
if(line[linelen-1]!='\n'){
fprintf(stderr,"firebase thread: unexpected EOF from js script\n");
return;
@@ -60,8 +66,10 @@ static void script_output_listener(FILE *js_read){
}
if(strcmp(cmd,"add_token")==0){
+ fprintf(stderr,"firebase thread: adding token '%s' to %" PRIi64 "\n",token,userid);
db_add_token(userid,token);
} else {
+ fprintf(stderr,"firebase thread: deleting token '%s' from %" PRIi64 "\n",token,userid);
db_delete_token(userid,token);
}
}
@@ -71,6 +79,14 @@ static void script_output_listener(FILE *js_read){
invalid_line:
fprintf(stderr,"firebase thread: Invalid line from js script: <%s>\n",line);
}
+
+ if(linelen<0){
+ if(feof(js_read)){
+ fprintf(stderr,"firebase thread: feof(js_read)\n");
+ } else {
+ fprintf(stderr,"firebase thread: getline: errno = %d (%s)\n",errno,strerror(errno));
+ }
+ }
}
@@ -106,6 +122,7 @@ void firebase_init(void){
if(pid==0){
close(wrpipe[1]);
FILE *js_read=fdopen(rdpipe[0],"r");
+ db_reinit();
script_output_listener(js_read);
fclose(js_read);
exit(0);