More errorchecking, get rid of perror()!

This commit is contained in:
dcp1990 2005-06-19 00:04:02 +00:00
parent b27026d0cb
commit ba95bb2f33
8 changed files with 73 additions and 28 deletions

View file

@ -3,12 +3,13 @@
* (C)2005, Dan Ponte * (C)2005, Dan Ponte
* BSDL w/ advert. * BSDL w/ advert.
*/ */
/* $Amigan: phoned/include/phoned.h,v 1.15 2005/06/18 20:13:05 dcp1990 Exp $ */ /* $Amigan: phoned/include/phoned.h,v 1.16 2005/06/19 00:04:02 dcp1990 Exp $ */
#include <pcre.h> /* fugly, I know... */ #include <pcre.h> /* fugly, I know... */
#define VERSION "0.1" #define VERSION "0.1"
#define LOGFILE "/var/log/phoned.log" #define LOGFILE "/var/log/phoned.log"
#define SOCKETFILE "/tmp/phoned.sock" #define SOCKETFILE "/tmp/phoned.sock"
#define CONFIGFILE "phoned.conf" #define CONFIGFILE "phoned.conf"
#define DBFILE "phoned.sqlite"
#define _unused __attribute__((__unused__)) #define _unused __attribute__((__unused__))
struct conf { struct conf {
char* cfile; char* cfile;
@ -16,6 +17,7 @@ struct conf {
int loglevels; int loglevels;
short modem_tm; short modem_tm;
char* modemdev; char* modemdev;
char *dbfile;
}; };
#define LL_DEBUG 0x1 #define LL_DEBUG 0x1
#define LL_GARBAGE 0x2 #define LL_GARBAGE 0x2
@ -137,8 +139,8 @@ void freeaddrl(addrsll_t* hd);
/* function prottypes */ /* function prottypes */
void initialize(void); void initialize(void);
void open_log(void); void open_log(void);
void read_config(void); short read_config(void);
void shutd(void); void shutd(int whatdone);
void *network(void *b); void *network(void *b);
int lprintf(enum ltype logtype, const char* fmt, ...); int lprintf(enum ltype logtype, const char* fmt, ...);
void handsig(int sig); void handsig(int sig);

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.5 2005/06/12 23:05:12 dcp1990 Exp $ */ /* $Amigan: phoned/phoned/cfg.c,v 1.6 2005/06/19 00:04:06 dcp1990 Exp $ */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
@ -14,22 +14,28 @@
#include <unistd.h> #include <unistd.h>
#include <pthread.h> #include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h>
extern addrsll_t *top; extern addrsll_t *top;
struct conf cf; struct conf cf;
extern pthread_mutex_t addrmx; extern pthread_mutex_t addrmx;
pthread_mutex_t cfmx; pthread_mutex_t cfmx;
void read_config(void) short read_config(void)
{ {
FILE* con; FILE* con;
pthread_mutex_lock(&cfmx); pthread_mutex_lock(&cfmx);
con = fopen(cf.cfile, "r"); con = fopen(cf.cfile, "r");
pthread_mutex_unlock(&cfmx); pthread_mutex_unlock(&cfmx);
if(!con) { if(!con) {
perror("error opening config file"); lprintf(error, "error opening config file: %s\n", strerror(errno));
exit(-1); return 0;
} }
parse(&con); if(parse(&con) == -1) {
lprintf(error, "error parsing\n");
fclose(con); fclose(con);
return 0;
}
fclose(con);
return 1;
} }
void addtoaddrs(const char* par) void addtoaddrs(const char* par)
{ {

View file

@ -17,6 +17,7 @@ extern pthread_mutex_t cfmx;
. ++chrcnt; REJECT; . ++chrcnt; REJECT;
main return MAIN; main return MAIN;
loglevel pthread_mutex_lock(&cfmx); cf.loglevels = LL_CRITICAL|LL_FATAL; pthread_mutex_unlock(&cfmx); return LLEVEL; loglevel pthread_mutex_lock(&cfmx); cf.loglevels = LL_CRITICAL|LL_FATAL; pthread_mutex_unlock(&cfmx); return LLEVEL;
database return DB;
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

@ -24,7 +24,7 @@ void yyerror(str)
{ {
lprintf(fatal, "parser: error: %s at line %d chr %d (near %s)\n", str, lprintf(fatal, "parser: error: %s at line %d chr %d (near %s)\n", str,
lincnt, chrcnt, yytext); lincnt, chrcnt, yytext);
shutd(); shutd(0x1 | 0x2);
exit(-1); exit(-1);
} }
int yywrap(void) int yywrap(void)
@ -33,7 +33,7 @@ int yywrap(void)
} }
%} %}
%token NOTIFY OBRACE CBRACE SCOLON QUOTE MODDEV MAIN LLEVEL OR %token NOTIFY OBRACE CBRACE SCOLON QUOTE MODDEV MAIN LLEVEL OR
%token FILTERS ACTION NAME PHNUM FILTER FLAGS %token FILTERS ACTION NAME PHNUM FILTER FLAGS DB
/* HANGUP IGNOREIT PLAY RECORD */ /* HANGUP IGNOREIT PLAY RECORD */
%token <number> LNUML ACTN FLAG %token <number> LNUML ACTN FLAG
%token <string> IPADDR PATH REGEX FNAME %token <string> IPADDR PATH REGEX FNAME
@ -63,6 +63,8 @@ directive:
modemdev modemdev
| |
loglevel loglevel
|
database
; ;
notify: notify:
NOTIFY iplist NOTIFY iplist
@ -81,6 +83,18 @@ ipadr:
addtoaddrs($1); addtoaddrs($1);
} }
; ;
database:
DB dbpath
;
dbpath:
QUOTE PATH QUOTE
{
lprintf(debug, "Database path == %s\n", $2);
pthread_mutex_lock(&cfmx);
cf.dbfile = $2;
pthread_mutex_unlock(&cfmx);
}
;
modemdev: modemdev:
MODDEV devpath MODDEV devpath
; ;

View file

@ -8,56 +8,77 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h> #include <pthread.h>
#include <errno.h>
#include <phoned.h> #include <phoned.h>
extern FILE* logf; extern FILE* logf;
extern pthread_mutex_t logfmx; extern pthread_mutex_t logfmx;
extern short difflog;
extern struct conf cf; extern struct conf cf;
extern pthread_mutex_t cfmx; extern pthread_mutex_t cfmx;
extern cond_t* topcond; extern cond_t* topcond;
#define WD_LOGS 0x1
#define WD_HANDLERS 0x2
#define WD_CONFIG 0x4
#define WD_MODEM 0x10
#define WD_DBINIT 0x20
void shutd(void) void shutd(whatdone)
int whatdone;
{ {
lprintf(fatal, "phoned shutting down...\n"); lprintf(fatal, "phoned shutting down...\n");
pthread_mutex_lock(&cfmx); pthread_mutex_lock(&cfmx);
close_modem(cf.modemdev); if(whatdone & WD_MODEM) close_modem(cf.modemdev);
pthread_mutex_unlock(&cfmx); pthread_mutex_unlock(&cfmx);
flush_lists(); flush_lists();
free_condition(topcond, 0x1); free_condition(topcond, 0x1);
pthread_mutex_lock(&logfmx); pthread_mutex_lock(&logfmx);
fclose(logf); if(whatdone & WD_LOGS) fclose(logf);
pthread_mutex_unlock(&logfmx); pthread_mutex_unlock(&logfmx);
unlink(SOCKETFILE); unlink(SOCKETFILE);
} }
void open_logs(void) int open_logs(void)
{ {
pthread_mutex_lock(&cfmx); pthread_mutex_lock(&cfmx);
if(strcmp(difflog ? cf.logfile : LOGFILE, "-") == 0) logf = stderr; if(strcmp(cf.logfile, "-") == 0) logf = stderr;
else { else {
logf = fopen(difflog ? cf.logfile : LOGFILE, "a"); logf = fopen(cf.logfile, "a");
if(!logf) { if(!logf) {
perror("logf open"); lprintf(error, "logf open: %s\n", strerror(errno));
exit(-1); return 0;
} }
} }
pthread_mutex_unlock(&cfmx); pthread_mutex_unlock(&cfmx);
lprintf(info, "phoned v" VERSION " starting..\n"); lprintf(info, "phoned v" VERSION " starting..\n");
return 1;
} }
void initialize(void) void initialize(void)
{ {
open_logs(); int whatdone = 0;
install_handlers(); if(!open_logs()) {
read_config(); fprintf(stderr, "Logs won't open!\n");
shutd(whatdone);
exit(-1);
} else whatdone |= WD_LOGS;
install_handlers(); whatdone |= WD_HANDLERS;
if(!read_config()) {
lprintf(fatal, "fatal: error reading config, see earlier messages\n");
shutd(whatdone);
exit(-1);
} else whatdone |= WD_CONFIG; /* XXX: if you put anything before this, edit config.y's bitmask on shutd() */
pthread_mutex_lock(&cfmx); 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(fatal, "fatal error: modem didn't initialise properly; see previous messages\n");
shutd(); shutd(whatdone);
exit(-1); exit(-1);
} } else whatdone |= WD_MODEM;
if(!db_init(cf.dbfile)) {
lprintf(fatal, "fatal error: database did not open, see previous messages\n");
shutd(whatdone);
exit(-1);
} else whatdone |= WD_DBINIT;
pthread_mutex_unlock(&cfmx); pthread_mutex_unlock(&cfmx);
} }

View file

@ -12,7 +12,6 @@
#include <phoned.h> #include <phoned.h>
extern struct conf cf; extern struct conf cf;
short difflog = 0;
extern pthread_mutex_t cfmx; extern pthread_mutex_t cfmx;
extern pthread_t networkth; extern pthread_t networkth;
extern pthread_t modemth; extern pthread_t modemth;
@ -29,6 +28,8 @@ int main(argc, argv)
int c; int c;
pthread_mutex_lock(&cfmx); pthread_mutex_lock(&cfmx);
cf.cfile = CONFIGFILE; cf.cfile = CONFIGFILE;
cf.dbfile = DBFILE;
cf.logfile = LOGFILE;
#define OPTSTRING "dhc:l:" #define OPTSTRING "dhc:l:"
while((c = getopt(argc, argv, OPTSTRING)) != -1) while((c = getopt(argc, argv, OPTSTRING)) != -1)
switch(c) { switch(c) {
@ -40,7 +41,6 @@ int main(argc, argv)
return 0; return 0;
case 'l': case 'l':
cf.logfile = strdup(optarg); cf.logfile = strdup(optarg);
difflog = 1;
break; break;
case 'd': case 'd':
daemon(1, 0); daemon(1, 0);

View file

@ -1,6 +1,7 @@
main { main {
modemdev "/dev/cuaa2"; modemdev "/dev/cuaa2";
loglevel all; loglevel all;
database "phoned.db";
}; };
filters { filters {
filter test1 { filter test1 {

View file

@ -43,7 +43,7 @@ void handsig(sig)
case SIGQUIT: case SIGQUIT:
case SIGTERM: case SIGTERM:
lprintf(fatal, "Received signal %d, cleaning up...\n", sig); lprintf(fatal, "Received signal %d, cleaning up...\n", sig);
shutd(); shutd(0x1 | 0x2 | 0x4 | 0x10 | 0x20);
exit(0); exit(0);
break; break;
case SIGHUP: case SIGHUP: