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:
parent
668c5b24a5
commit
a62b4ae3d3
@ -2,7 +2,7 @@
|
||||
* FUCK....lex overwrote this :-(
|
||||
* (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 <string.h>
|
||||
#include <sys/types.h>
|
||||
@ -12,13 +12,18 @@
|
||||
#define HAVE_INET_INCS
|
||||
#include <phoned.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
extern addrsll_t *top;
|
||||
struct conf cf;
|
||||
extern pthread_mutex_t addrmx;
|
||||
pthread_mutex_t cfmx;
|
||||
void read_config(void)
|
||||
{
|
||||
FILE* con;
|
||||
pthread_mutex_lock(&cfmx);
|
||||
con = fopen(cf.cfile, "r");
|
||||
pthread_mutex_unlock(&cfmx);
|
||||
if(!con) {
|
||||
perror("error opening config file");
|
||||
exit(-1);
|
||||
@ -29,6 +34,7 @@ void read_config(void)
|
||||
void addtoaddrs(const char* par)
|
||||
{
|
||||
addrsll_t *l;
|
||||
pthread_mutex_lock(&addrmx);
|
||||
l = getlast(top);
|
||||
#ifdef DEBUG
|
||||
if(l->next != 0x0) {
|
||||
@ -39,5 +45,6 @@ void addtoaddrs(const char* par)
|
||||
l->next = allocaddr();
|
||||
l = l->next;
|
||||
l->addr = inet_addr(par);
|
||||
pthread_mutex_unlock(&addrmx);
|
||||
}
|
||||
|
||||
|
@ -7,14 +7,16 @@
|
||||
#else
|
||||
#include "y.tab.h"
|
||||
#endif
|
||||
#include <pthread.h>
|
||||
#include <phoned.h>
|
||||
extern int chrcnt, lincnt;
|
||||
extern struct conf cf; /* ewwwww */
|
||||
extern pthread_mutex_t cfmx;
|
||||
%}
|
||||
%%
|
||||
. ++chrcnt; REJECT;
|
||||
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;
|
||||
debug yylval.number = LL_DEBUG; return LNUML;
|
||||
info yylval.number = LL_INFO; return LNUML;
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <phoned.h>
|
||||
#include <pthread.h>
|
||||
int chrcnt = 0;
|
||||
int lincnt = 1;
|
||||
int yylex(void);
|
||||
@ -17,6 +18,7 @@ int factn = 0x0;
|
||||
int ctflags = 0x0;
|
||||
extern char* yytext;
|
||||
extern struct conf cf;
|
||||
extern pthread_mutex_t cfmx;
|
||||
void yyerror(str)
|
||||
char* str;
|
||||
{
|
||||
@ -85,7 +87,9 @@ devpath:
|
||||
QUOTE PATH QUOTE
|
||||
{
|
||||
lprintf(debug, "Modem dev == %s\n", $2);
|
||||
pthread_mutex_lock(&cfmx);
|
||||
cf.modemdev = $2;
|
||||
pthread_mutex_unlock(&cfmx);
|
||||
}
|
||||
;
|
||||
/* filters */
|
||||
@ -182,12 +186,16 @@ llvls:
|
||||
llvl:
|
||||
LNUML OR
|
||||
{
|
||||
pthread_mutex_lock(&cfmx);
|
||||
cf.loglevels |= $1;
|
||||
pthread_mutex_unlock(&cfmx);
|
||||
}
|
||||
|
|
||||
LNUML
|
||||
{
|
||||
pthread_mutex_lock(&cfmx);
|
||||
cf.loglevels |= $1;
|
||||
pthread_mutex_unlock(&cfmx);
|
||||
}
|
||||
;
|
||||
%%
|
||||
|
@ -7,26 +7,34 @@
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include <phoned.h>
|
||||
|
||||
extern FILE* logf;
|
||||
extern pthread_mutex_t logfmx;
|
||||
extern short difflog;
|
||||
extern struct conf cf;
|
||||
extern pthread_mutex_t cfmx;
|
||||
extern cond_t* topcond;
|
||||
|
||||
void shutd(void)
|
||||
{
|
||||
lprintf(fatal, "phoned shutting down...\n");
|
||||
pthread_mutex_lock(&cfmx);
|
||||
close_modem(cf.modemdev);
|
||||
pthread_mutex_unlock(&cfmx);
|
||||
flush_lists();
|
||||
free_condition(topcond, 0x1);
|
||||
pthread_mutex_lock(&logfmx);
|
||||
fclose(logf);
|
||||
pthread_mutex_unlock(&logfmx);
|
||||
unlink(SOCKETFILE);
|
||||
}
|
||||
|
||||
void open_logs(void)
|
||||
{
|
||||
pthread_mutex_lock(&cfmx);
|
||||
if(strcmp(difflog ? cf.logfile : LOGFILE, "-") == 0) logf = stderr;
|
||||
else {
|
||||
logf = fopen(difflog ? cf.logfile : LOGFILE, "a");
|
||||
@ -35,6 +43,7 @@ void open_logs(void)
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&cfmx);
|
||||
lprintf(info, "phoned v" VERSION " starting..\n");
|
||||
}
|
||||
|
||||
@ -43,8 +52,10 @@ void initialize(void)
|
||||
open_logs();
|
||||
install_handlers();
|
||||
read_config();
|
||||
pthread_mutex_lock(&cfmx);
|
||||
if(init_modem(cf.modemdev) != 1) {
|
||||
lprintf(warn, "warning: modem didn't initialise properly; see previous messages\n");
|
||||
}
|
||||
pthread_mutex_unlock(&cfmx);
|
||||
}
|
||||
|
||||
|
@ -11,9 +11,12 @@
|
||||
#include <unistd.h>
|
||||
#include <phoned.h>
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
|
||||
FILE* logf;
|
||||
pthread_mutex_t logfmx = PTHREAD_MUTEX_INITIALIZER;
|
||||
extern struct conf cf;
|
||||
extern pthread_mutex_t cfmx;
|
||||
|
||||
int check_loglevel(lt, ll)
|
||||
enum ltype lt;
|
||||
@ -66,13 +69,16 @@ int lprintf(enum ltype logtype, const char* fmt, ...)
|
||||
time_t now;
|
||||
char tmt[128];
|
||||
now = time(NULL);
|
||||
pthread_mutex_lock(&cfmx);
|
||||
l = cf.loglevels;
|
||||
pthread_mutex_unlock(&cfmx);
|
||||
if(check_loglevel(logtype, l) != 1)
|
||||
return -1;
|
||||
strftime(tmt, 128, "%d%b %H:%M:%S: ", localtime(&now));
|
||||
fmtp = fmt;
|
||||
maxsize = strlen(fmt) + 1;
|
||||
ofmt = malloc(sizeof(char) * (strlen(fmt) + 2));
|
||||
pthread_mutex_lock(&logfmx);
|
||||
fputs(tmt, logf);
|
||||
va_start(ap, fmt);
|
||||
while(*fmtp) {
|
||||
@ -163,6 +169,7 @@ int lprintf(enum ltype logtype, const char* fmt, ...)
|
||||
if(fmt[strlen(fmt)-1] != '\n')
|
||||
fputc('\n', logf);
|
||||
fflush(logf);
|
||||
pthread_mutex_unlock(&logfmx);
|
||||
va_end(ap);
|
||||
free(ofmt); /* MUST do this */
|
||||
return cnt;
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include <phoned.h>
|
||||
extern struct conf cf;
|
||||
short difflog = 0;
|
||||
extern pthread_mutex_t cfmx;
|
||||
|
||||
void usage(argv)
|
||||
const char* argv;
|
||||
@ -25,6 +26,7 @@ int main(argc, argv)
|
||||
char* argv[];
|
||||
{
|
||||
int c;
|
||||
pthread_mutex_lock(&cfmx);
|
||||
cf.cfile = CONFIGFILE;
|
||||
#define OPTSTRING "dhc:l:"
|
||||
while((c = getopt(argc, argv, OPTSTRING)) != -1)
|
||||
@ -47,6 +49,7 @@ int main(argc, argv)
|
||||
return -2;
|
||||
}
|
||||
cf.loglevels = LL_ALL;
|
||||
pthread_mutex_unlock(&cfmx);
|
||||
initialize();
|
||||
network();
|
||||
shutd();
|
||||
|
@ -44,6 +44,7 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <phoned.h>
|
||||
#define INITSTRING "ATZ\r\nAT E0 #CID=2 V0\r\n"
|
||||
/* globals */
|
||||
@ -52,13 +53,18 @@ int modemfd;
|
||||
unsigned int cou = 0;
|
||||
char buffer[512];
|
||||
short doing_cid = 0;
|
||||
pthread_mutex_t modemmx = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_t buffermx = PTHREAD_MUTEX_INITIALIZER;
|
||||
void stmod(const char* str)
|
||||
{
|
||||
pthread_mutex_lock(&modemmx);
|
||||
fputs(str, modem);
|
||||
fflush(modem);
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
}
|
||||
int close_modem(char* dev)
|
||||
{
|
||||
pthread_mutex_lock(&modemmx);
|
||||
if(strlen(dev) < 7) {
|
||||
lprintf(error , "dev %s too short\n", dev);
|
||||
return -4;
|
||||
@ -69,34 +75,42 @@ int close_modem(char* dev)
|
||||
}
|
||||
uu_unlock((dev+(sizeof("/dev/")-1)));
|
||||
fclose(modem);
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
return 1;
|
||||
}
|
||||
int init_modem(char* dev)
|
||||
{
|
||||
int lres = 0;
|
||||
pthread_mutex_lock(&modemmx);
|
||||
if(strlen(dev) < 7) {
|
||||
lprintf(error , "dev %s too short\n", dev);
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
return -4;
|
||||
}
|
||||
if(strncmp(dev, "/dev/", strlen("/dev/")) != 0) {
|
||||
lprintf(error, "dev %s must begin with /dev/\n", dev);
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
return -5;
|
||||
}
|
||||
lres = uu_lock((dev+(sizeof("/dev/")-1)));
|
||||
if(lres != UU_LOCK_OK) {
|
||||
lprintf(error, "%s\n", uu_lockerr(lres));
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
return -1;
|
||||
}
|
||||
modemfd = open(dev, O_RDWR);
|
||||
if(modemfd == -1) {
|
||||
lprintf(error, "Error opening modem %s: %s\n", dev, strerror(errno));
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
return -2;
|
||||
}
|
||||
modem = fdopen(modemfd, "w+");
|
||||
if(!modem) {
|
||||
lprintf(error, "Error fdopening modemfd %d: %s\n", modemfd, strerror(errno));
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
return -3;
|
||||
}
|
||||
pthread_mutex_unlock(&modemmx);
|
||||
stmod(INITSTRING);
|
||||
return 1;
|
||||
}
|
||||
@ -126,6 +140,7 @@ int modem_evalrc(char* result)
|
||||
}
|
||||
void modem_hread(char* cbuf)
|
||||
{
|
||||
pthread_mutex_lock(&buffermx);
|
||||
if(cou < sizeof(buffer) - 2)
|
||||
buffer[cou] = cbuf[0];
|
||||
if(buffer[0] == '8' && buffer[1] == '0')
|
||||
@ -150,4 +165,5 @@ void modem_hread(char* cbuf)
|
||||
} else {
|
||||
cou++;
|
||||
}
|
||||
pthread_mutex_unlock(&buffermx);
|
||||
}
|
||||
|
@ -49,6 +49,8 @@
|
||||
|
||||
extern int modemfd;
|
||||
extern FILE* modem;
|
||||
extern pthread_mutex_t modemmx;
|
||||
extern pthread_mutex_t buffermx;
|
||||
|
||||
void *handclient(k)
|
||||
void* k;
|
||||
|
Loading…
Reference in New Issue
Block a user