Preliminary thread safety (nowhere NEAR complete; we have to move modem and network into a seperate thread from main)

Read: DON'T TRUST ANY OF THIS NOW
This commit is contained in:
dcp1990 2005-06-12 23:05:12 +00:00
parent 668c5b24a5
commit a62b4ae3d3
8 changed files with 58 additions and 2 deletions

View File

@ -2,7 +2,7 @@
* FUCK....lex overwrote this :-( * FUCK....lex overwrote this :-(
* (C)2005, Dan Ponte...again. * (C)2005, Dan Ponte...again.
*/ */
/* $Amigan: phoned/phoned/cfg.c,v 1.4 2005/06/02 02:44:27 dcp1990 Exp $ */ /* $Amigan: phoned/phoned/cfg.c,v 1.5 2005/06/12 23:05:12 dcp1990 Exp $ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
@ -12,13 +12,18 @@
#define HAVE_INET_INCS #define HAVE_INET_INCS
#include <phoned.h> #include <phoned.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
extern addrsll_t *top; extern addrsll_t *top;
struct conf cf; struct conf cf;
extern pthread_mutex_t addrmx;
pthread_mutex_t cfmx;
void read_config(void) void read_config(void)
{ {
FILE* con; FILE* con;
pthread_mutex_lock(&cfmx);
con = fopen(cf.cfile, "r"); con = fopen(cf.cfile, "r");
pthread_mutex_unlock(&cfmx);
if(!con) { if(!con) {
perror("error opening config file"); perror("error opening config file");
exit(-1); exit(-1);
@ -29,6 +34,7 @@ void read_config(void)
void addtoaddrs(const char* par) void addtoaddrs(const char* par)
{ {
addrsll_t *l; addrsll_t *l;
pthread_mutex_lock(&addrmx);
l = getlast(top); l = getlast(top);
#ifdef DEBUG #ifdef DEBUG
if(l->next != 0x0) { if(l->next != 0x0) {
@ -39,5 +45,6 @@ void addtoaddrs(const char* par)
l->next = allocaddr(); l->next = allocaddr();
l = l->next; l = l->next;
l->addr = inet_addr(par); l->addr = inet_addr(par);
pthread_mutex_unlock(&addrmx);
} }

View File

@ -7,14 +7,16 @@
#else #else
#include "y.tab.h" #include "y.tab.h"
#endif #endif
#include <pthread.h>
#include <phoned.h> #include <phoned.h>
extern int chrcnt, lincnt; extern int chrcnt, lincnt;
extern struct conf cf; /* ewwwww */ extern struct conf cf; /* ewwwww */
extern pthread_mutex_t cfmx;
%} %}
%% %%
. ++chrcnt; REJECT; . ++chrcnt; REJECT;
main return MAIN; main return MAIN;
loglevel cf.loglevels = LL_CRITICAL|LL_FATAL; return LLEVEL; loglevel pthread_mutex_lock(&cfmx); cf.loglevels = LL_CRITICAL|LL_FATAL; pthread_mutex_unlock(&cfmx); return LLEVEL;
all yylval.number = LL_ALL; return LNUML; all yylval.number = LL_ALL; return LNUML;
debug yylval.number = LL_DEBUG; return LNUML; debug yylval.number = LL_DEBUG; return LNUML;
info yylval.number = LL_INFO; return LNUML; info yylval.number = LL_INFO; return LNUML;

View File

@ -9,6 +9,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <phoned.h> #include <phoned.h>
#include <pthread.h>
int chrcnt = 0; int chrcnt = 0;
int lincnt = 1; int lincnt = 1;
int yylex(void); int yylex(void);
@ -17,6 +18,7 @@ int factn = 0x0;
int ctflags = 0x0; int ctflags = 0x0;
extern char* yytext; extern char* yytext;
extern struct conf cf; extern struct conf cf;
extern pthread_mutex_t cfmx;
void yyerror(str) void yyerror(str)
char* str; char* str;
{ {
@ -85,7 +87,9 @@ devpath:
QUOTE PATH QUOTE QUOTE PATH QUOTE
{ {
lprintf(debug, "Modem dev == %s\n", $2); lprintf(debug, "Modem dev == %s\n", $2);
pthread_mutex_lock(&cfmx);
cf.modemdev = $2; cf.modemdev = $2;
pthread_mutex_unlock(&cfmx);
} }
; ;
/* filters */ /* filters */
@ -182,12 +186,16 @@ llvls:
llvl: llvl:
LNUML OR LNUML OR
{ {
pthread_mutex_lock(&cfmx);
cf.loglevels |= $1; cf.loglevels |= $1;
pthread_mutex_unlock(&cfmx);
} }
| |
LNUML LNUML
{ {
pthread_mutex_lock(&cfmx);
cf.loglevels |= $1; cf.loglevels |= $1;
pthread_mutex_unlock(&cfmx);
} }
; ;
%% %%

View File

@ -7,26 +7,34 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h>
#include <phoned.h> #include <phoned.h>
extern FILE* logf; extern FILE* logf;
extern pthread_mutex_t logfmx;
extern short difflog; extern short difflog;
extern struct conf cf; extern struct conf cf;
extern pthread_mutex_t cfmx;
extern cond_t* topcond; extern cond_t* topcond;
void shutd(void) void shutd(void)
{ {
lprintf(fatal, "phoned shutting down...\n"); lprintf(fatal, "phoned shutting down...\n");
pthread_mutex_lock(&cfmx);
close_modem(cf.modemdev); close_modem(cf.modemdev);
pthread_mutex_unlock(&cfmx);
flush_lists(); flush_lists();
free_condition(topcond, 0x1); free_condition(topcond, 0x1);
pthread_mutex_lock(&logfmx);
fclose(logf); fclose(logf);
pthread_mutex_unlock(&logfmx);
unlink(SOCKETFILE); unlink(SOCKETFILE);
} }
void open_logs(void) void open_logs(void)
{ {
pthread_mutex_lock(&cfmx);
if(strcmp(difflog ? cf.logfile : LOGFILE, "-") == 0) logf = stderr; if(strcmp(difflog ? cf.logfile : LOGFILE, "-") == 0) logf = stderr;
else { else {
logf = fopen(difflog ? cf.logfile : LOGFILE, "a"); logf = fopen(difflog ? cf.logfile : LOGFILE, "a");
@ -35,6 +43,7 @@ void open_logs(void)
exit(-1); exit(-1);
} }
} }
pthread_mutex_unlock(&cfmx);
lprintf(info, "phoned v" VERSION " starting..\n"); lprintf(info, "phoned v" VERSION " starting..\n");
} }
@ -43,8 +52,10 @@ void initialize(void)
open_logs(); open_logs();
install_handlers(); install_handlers();
read_config(); read_config();
pthread_mutex_lock(&cfmx);
if(init_modem(cf.modemdev) != 1) { if(init_modem(cf.modemdev) != 1) {
lprintf(warn, "warning: modem didn't initialise properly; see previous messages\n"); lprintf(warn, "warning: modem didn't initialise properly; see previous messages\n");
} }
pthread_mutex_unlock(&cfmx);
} }

View File

@ -11,9 +11,12 @@
#include <unistd.h> #include <unistd.h>
#include <phoned.h> #include <phoned.h>
#include <time.h> #include <time.h>
#include <pthread.h>
FILE* logf; FILE* logf;
pthread_mutex_t logfmx = PTHREAD_MUTEX_INITIALIZER;
extern struct conf cf; extern struct conf cf;
extern pthread_mutex_t cfmx;
int check_loglevel(lt, ll) int check_loglevel(lt, ll)
enum ltype lt; enum ltype lt;
@ -66,13 +69,16 @@ int lprintf(enum ltype logtype, const char* fmt, ...)
time_t now; time_t now;
char tmt[128]; char tmt[128];
now = time(NULL); now = time(NULL);
pthread_mutex_lock(&cfmx);
l = cf.loglevels; l = cf.loglevels;
pthread_mutex_unlock(&cfmx);
if(check_loglevel(logtype, l) != 1) if(check_loglevel(logtype, l) != 1)
return -1; return -1;
strftime(tmt, 128, "%d%b %H:%M:%S: ", localtime(&now)); strftime(tmt, 128, "%d%b %H:%M:%S: ", localtime(&now));
fmtp = fmt; fmtp = fmt;
maxsize = strlen(fmt) + 1; maxsize = strlen(fmt) + 1;
ofmt = malloc(sizeof(char) * (strlen(fmt) + 2)); ofmt = malloc(sizeof(char) * (strlen(fmt) + 2));
pthread_mutex_lock(&logfmx);
fputs(tmt, logf); fputs(tmt, logf);
va_start(ap, fmt); va_start(ap, fmt);
while(*fmtp) { while(*fmtp) {
@ -163,6 +169,7 @@ int lprintf(enum ltype logtype, const char* fmt, ...)
if(fmt[strlen(fmt)-1] != '\n') if(fmt[strlen(fmt)-1] != '\n')
fputc('\n', logf); fputc('\n', logf);
fflush(logf); fflush(logf);
pthread_mutex_unlock(&logfmx);
va_end(ap); va_end(ap);
free(ofmt); /* MUST do this */ free(ofmt); /* MUST do this */
return cnt; return cnt;

View File

@ -13,6 +13,7 @@
#include <phoned.h> #include <phoned.h>
extern struct conf cf; extern struct conf cf;
short difflog = 0; short difflog = 0;
extern pthread_mutex_t cfmx;
void usage(argv) void usage(argv)
const char* argv; const char* argv;
@ -25,6 +26,7 @@ int main(argc, argv)
char* argv[]; char* argv[];
{ {
int c; int c;
pthread_mutex_lock(&cfmx);
cf.cfile = CONFIGFILE; cf.cfile = CONFIGFILE;
#define OPTSTRING "dhc:l:" #define OPTSTRING "dhc:l:"
while((c = getopt(argc, argv, OPTSTRING)) != -1) while((c = getopt(argc, argv, OPTSTRING)) != -1)
@ -47,6 +49,7 @@ int main(argc, argv)
return -2; return -2;
} }
cf.loglevels = LL_ALL; cf.loglevels = LL_ALL;
pthread_mutex_unlock(&cfmx);
initialize(); initialize();
network(); network();
shutd(); shutd();

View File

@ -44,6 +44,7 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/types.h> #include <sys/types.h>
#include <errno.h> #include <errno.h>
#include <pthread.h>
#include <phoned.h> #include <phoned.h>
#define INITSTRING "ATZ\r\nAT E0 #CID=2 V0\r\n" #define INITSTRING "ATZ\r\nAT E0 #CID=2 V0\r\n"
/* globals */ /* globals */
@ -52,13 +53,18 @@ int modemfd;
unsigned int cou = 0; unsigned int cou = 0;
char buffer[512]; char buffer[512];
short doing_cid = 0; short doing_cid = 0;
pthread_mutex_t modemmx = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t buffermx = PTHREAD_MUTEX_INITIALIZER;
void stmod(const char* str) void stmod(const char* str)
{ {
pthread_mutex_lock(&modemmx);
fputs(str, modem); fputs(str, modem);
fflush(modem); fflush(modem);
pthread_mutex_unlock(&modemmx);
} }
int close_modem(char* dev) int close_modem(char* dev)
{ {
pthread_mutex_lock(&modemmx);
if(strlen(dev) < 7) { if(strlen(dev) < 7) {
lprintf(error , "dev %s too short\n", dev); lprintf(error , "dev %s too short\n", dev);
return -4; return -4;
@ -69,34 +75,42 @@ int close_modem(char* dev)
} }
uu_unlock((dev+(sizeof("/dev/")-1))); uu_unlock((dev+(sizeof("/dev/")-1)));
fclose(modem); fclose(modem);
pthread_mutex_unlock(&modemmx);
return 1; return 1;
} }
int init_modem(char* dev) int init_modem(char* dev)
{ {
int lres = 0; int lres = 0;
pthread_mutex_lock(&modemmx);
if(strlen(dev) < 7) { if(strlen(dev) < 7) {
lprintf(error , "dev %s too short\n", dev); lprintf(error , "dev %s too short\n", dev);
pthread_mutex_unlock(&modemmx);
return -4; return -4;
} }
if(strncmp(dev, "/dev/", strlen("/dev/")) != 0) { if(strncmp(dev, "/dev/", strlen("/dev/")) != 0) {
lprintf(error, "dev %s must begin with /dev/\n", dev); lprintf(error, "dev %s must begin with /dev/\n", dev);
pthread_mutex_unlock(&modemmx);
return -5; return -5;
} }
lres = uu_lock((dev+(sizeof("/dev/")-1))); lres = uu_lock((dev+(sizeof("/dev/")-1)));
if(lres != UU_LOCK_OK) { if(lres != UU_LOCK_OK) {
lprintf(error, "%s\n", uu_lockerr(lres)); lprintf(error, "%s\n", uu_lockerr(lres));
pthread_mutex_unlock(&modemmx);
return -1; return -1;
} }
modemfd = open(dev, O_RDWR); modemfd = open(dev, O_RDWR);
if(modemfd == -1) { if(modemfd == -1) {
lprintf(error, "Error opening modem %s: %s\n", dev, strerror(errno)); lprintf(error, "Error opening modem %s: %s\n", dev, strerror(errno));
pthread_mutex_unlock(&modemmx);
return -2; return -2;
} }
modem = fdopen(modemfd, "w+"); modem = fdopen(modemfd, "w+");
if(!modem) { if(!modem) {
lprintf(error, "Error fdopening modemfd %d: %s\n", modemfd, strerror(errno)); lprintf(error, "Error fdopening modemfd %d: %s\n", modemfd, strerror(errno));
pthread_mutex_unlock(&modemmx);
return -3; return -3;
} }
pthread_mutex_unlock(&modemmx);
stmod(INITSTRING); stmod(INITSTRING);
return 1; return 1;
} }
@ -126,6 +140,7 @@ int modem_evalrc(char* result)
} }
void modem_hread(char* cbuf) void modem_hread(char* cbuf)
{ {
pthread_mutex_lock(&buffermx);
if(cou < sizeof(buffer) - 2) if(cou < sizeof(buffer) - 2)
buffer[cou] = cbuf[0]; buffer[cou] = cbuf[0];
if(buffer[0] == '8' && buffer[1] == '0') if(buffer[0] == '8' && buffer[1] == '0')
@ -150,4 +165,5 @@ void modem_hread(char* cbuf)
} else { } else {
cou++; cou++;
} }
pthread_mutex_unlock(&buffermx);
} }

View File

@ -49,6 +49,8 @@
extern int modemfd; extern int modemfd;
extern FILE* modem; extern FILE* modem;
extern pthread_mutex_t modemmx;
extern pthread_mutex_t buffermx;
void *handclient(k) void *handclient(k)
void* k; void* k;