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
* 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... */
#define VERSION "0.1"
#define LOGFILE "/var/log/phoned.log"
#define SOCKETFILE "/tmp/phoned.sock"
#define CONFIGFILE "phoned.conf"
#define DBFILE "phoned.sqlite"
#define _unused __attribute__((__unused__))
struct conf {
char* cfile;
@ -16,6 +17,7 @@ struct conf {
int loglevels;
short modem_tm;
char* modemdev;
char *dbfile;
};
#define LL_DEBUG 0x1
#define LL_GARBAGE 0x2
@ -137,8 +139,8 @@ void freeaddrl(addrsll_t* hd);
/* function prottypes */
void initialize(void);
void open_log(void);
void read_config(void);
void shutd(void);
short read_config(void);
void shutd(int whatdone);
void *network(void *b);
int lprintf(enum ltype logtype, const char* fmt, ...);
void handsig(int sig);

View File

@ -2,7 +2,7 @@
* FUCK....lex overwrote this :-(
* (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 <string.h>
#include <sys/types.h>
@ -14,22 +14,28 @@
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <errno.h>
extern addrsll_t *top;
struct conf cf;
extern pthread_mutex_t addrmx;
pthread_mutex_t cfmx;
void read_config(void)
short 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);
lprintf(error, "error opening config file: %s\n", strerror(errno));
return 0;
}
if(parse(&con) == -1) {
lprintf(error, "error parsing\n");
fclose(con);
return 0;
}
parse(&con);
fclose(con);
return 1;
}
void addtoaddrs(const char* par)
{

View File

@ -17,6 +17,7 @@ extern pthread_mutex_t cfmx;
. ++chrcnt; REJECT;
main return MAIN;
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;
debug yylval.number = LL_DEBUG; 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,
lincnt, chrcnt, yytext);
shutd();
shutd(0x1 | 0x2);
exit(-1);
}
int yywrap(void)
@ -33,7 +33,7 @@ int yywrap(void)
}
%}
%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 */
%token <number> LNUML ACTN FLAG
%token <string> IPADDR PATH REGEX FNAME
@ -63,6 +63,8 @@ directive:
modemdev
|
loglevel
|
database
;
notify:
NOTIFY iplist
@ -81,6 +83,18 @@ ipadr:
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:
MODDEV devpath
;

View File

@ -8,56 +8,77 @@
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.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;
#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");
pthread_mutex_lock(&cfmx);
close_modem(cf.modemdev);
if(whatdone & WD_MODEM) close_modem(cf.modemdev);
pthread_mutex_unlock(&cfmx);
flush_lists();
free_condition(topcond, 0x1);
pthread_mutex_lock(&logfmx);
fclose(logf);
if(whatdone & WD_LOGS) fclose(logf);
pthread_mutex_unlock(&logfmx);
unlink(SOCKETFILE);
}
void open_logs(void)
int open_logs(void)
{
pthread_mutex_lock(&cfmx);
if(strcmp(difflog ? cf.logfile : LOGFILE, "-") == 0) logf = stderr;
if(strcmp(cf.logfile, "-") == 0) logf = stderr;
else {
logf = fopen(difflog ? cf.logfile : LOGFILE, "a");
logf = fopen(cf.logfile, "a");
if(!logf) {
perror("logf open");
exit(-1);
lprintf(error, "logf open: %s\n", strerror(errno));
return 0;
}
}
pthread_mutex_unlock(&cfmx);
lprintf(info, "phoned v" VERSION " starting..\n");
return 1;
}
void initialize(void)
{
open_logs();
install_handlers();
read_config();
int whatdone = 0;
if(!open_logs()) {
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);
if(init_modem(cf.modemdev) != 1) {
lprintf(warn, "warning: modem didn't initialise properly; see previous messages\n");
shutd();
lprintf(fatal, "fatal error: modem didn't initialise properly; see previous messages\n");
shutd(whatdone);
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);
}

View File

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

View File

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

View File

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