2003-10-13 11:03:14 -04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
* Generic error handling
|
|
|
|
*
|
2003-12-29 15:41:08 -05:00
|
|
|
* Copyright (C) 2003 Ron Pedde (ron@pedde.com)
|
2003-10-13 11:03:14 -04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* \file err.c
|
|
|
|
* Error handling, logging, and memory leak checking.
|
|
|
|
*
|
|
|
|
* Most of these functions should not be used directly. For the most
|
2004-11-12 02:27:05 -05:00
|
|
|
* part, they are hidden in macros like #DPRINTF and #MEMNOTIFY. The
|
|
|
|
* only function here that is really directly useable is log_setdest()
|
2004-11-12 01:38:05 -05:00
|
|
|
*/
|
|
|
|
|
2004-03-13 20:06:15 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2004-04-13 00:23:36 -04:00
|
|
|
#include <time.h>
|
2003-10-13 11:03:14 -04:00
|
|
|
#include <errno.h>
|
2003-11-23 01:10:25 -05:00
|
|
|
#include <pthread.h>
|
2003-10-13 11:03:14 -04:00
|
|
|
#include <stdlib.h>
|
2004-02-14 19:51:11 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/* don't want to redefine malloc -- if doing memory debugging,
|
|
|
|
* this is the only file that *shouldn't* be redefining malloc and
|
|
|
|
* friends. Hence the define.
|
|
|
|
*/
|
2003-11-23 01:10:25 -05:00
|
|
|
#define __IN_ERR__
|
2003-10-13 11:03:14 -04:00
|
|
|
#include "err.h"
|
|
|
|
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
int err_debuglevel=0; /**< current debuglevel, set from command line with -d */
|
|
|
|
static int err_logdestination=LOGDEST_STDERR; /**< current log destination */
|
|
|
|
static FILE *err_file=NULL; /**< if logging to file, the handle of that file */
|
|
|
|
static pthread_mutex_t err_mutex=PTHREAD_MUTEX_INITIALIZER; /**< for serializing log messages */
|
2004-02-14 19:51:11 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG_MEMORY
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Nodes for a linked list of in-use memory. Any malloc/strdup/etc
|
|
|
|
* calls get a new node of this type added to the ::err_leak list.
|
|
|
|
*/
|
2003-11-23 01:10:25 -05:00
|
|
|
typedef struct tag_err_leak {
|
|
|
|
void *ptr;
|
|
|
|
char *file;
|
|
|
|
int line;
|
|
|
|
int size;
|
|
|
|
struct tag_err_leak *next;
|
|
|
|
} ERR_LEAK;
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/** head of linked list of in-use memory */
|
2003-11-23 01:10:25 -05:00
|
|
|
ERR_LEAK err_leak = { NULL, NULL, 0, 0, NULL };
|
2003-12-01 10:27:40 -05:00
|
|
|
#endif
|
2003-11-23 01:10:25 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Forwards
|
|
|
|
*/
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
static int err_lock_mutex(void);
|
|
|
|
static int err_unlock_mutex(void);
|
2003-11-23 01:10:25 -05:00
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Write a printf-style formatted message to the log destination.
|
|
|
|
* This can be stderr, syslog, or a logfile, as determined by
|
2004-11-12 02:27:05 -05:00
|
|
|
* log_setdest(). Note that this function should not be directly
|
|
|
|
* used, rather it should be used via the #DPRINTF macro.
|
2004-11-12 01:38:05 -05:00
|
|
|
*
|
|
|
|
* \param level Level at which to log \ref log_levels
|
|
|
|
* \param fmt printf-style
|
|
|
|
*/
|
2004-04-13 00:23:36 -04:00
|
|
|
void log_err(int level, char *fmt, ...)
|
2003-10-13 11:03:14 -04:00
|
|
|
{
|
|
|
|
va_list ap;
|
2004-04-13 00:23:36 -04:00
|
|
|
char timebuf[256];
|
|
|
|
char errbuf[1024];
|
|
|
|
struct tm tm_now;
|
|
|
|
time_t tt_now;
|
|
|
|
|
|
|
|
if(level > err_debuglevel)
|
|
|
|
return;
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
|
|
|
|
va_end(ap);
|
2004-04-13 00:23:36 -04:00
|
|
|
|
|
|
|
err_lock_mutex(); /* atomic file writes */
|
2003-10-13 11:03:14 -04:00
|
|
|
|
|
|
|
switch(err_logdestination) {
|
2004-04-13 00:23:36 -04:00
|
|
|
case LOGDEST_LOGFILE:
|
|
|
|
tt_now=time(NULL);
|
|
|
|
gmtime_r(&tt_now,&tm_now);
|
|
|
|
strftime(timebuf,sizeof(timebuf),"%F %T",&tm_now);
|
2004-04-13 00:27:04 -04:00
|
|
|
fprintf(err_file,"%s: %s",timebuf,errbuf);
|
|
|
|
if(!level) fprintf(err_file,"%s: Aborting\n",timebuf);
|
2004-04-13 00:23:36 -04:00
|
|
|
fflush(err_file);
|
|
|
|
break;
|
2003-10-13 11:03:14 -04:00
|
|
|
case LOGDEST_STDERR:
|
2004-01-12 23:29:43 -05:00
|
|
|
fprintf(stderr, "%s", errbuf);
|
2004-04-13 00:23:36 -04:00
|
|
|
if(!level) fprintf(stderr,"Aborting\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
break;
|
|
|
|
case LOGDEST_SYSLOG:
|
2004-01-12 23:29:43 -05:00
|
|
|
syslog(LOG_INFO, "%s", errbuf);
|
2004-04-13 00:23:36 -04:00
|
|
|
if(!level) syslog(LOG_INFO, "Aborting\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-04-13 00:23:36 -04:00
|
|
|
err_unlock_mutex();
|
|
|
|
|
|
|
|
if(!level) {
|
2003-10-13 11:03:14 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Sets the log destination. (stderr, syslog, or logfile)
|
|
|
|
*
|
|
|
|
* \param app appname (used only for syslog destination)
|
|
|
|
* \param destination where to log to \ref log_dests "as defined in err.h"
|
2004-11-12 02:27:05 -05:00
|
|
|
*/
|
2003-10-13 11:03:14 -04:00
|
|
|
void log_setdest(char *app, int destination) {
|
2004-04-13 00:23:36 -04:00
|
|
|
if(err_logdestination == destination)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch(err_logdestination) {
|
2003-10-13 11:03:14 -04:00
|
|
|
case LOGDEST_SYSLOG:
|
2004-04-13 00:23:36 -04:00
|
|
|
closelog();
|
2003-10-13 11:03:14 -04:00
|
|
|
break;
|
2004-04-13 00:23:36 -04:00
|
|
|
case LOGDEST_LOGFILE:
|
|
|
|
fclose(err_file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(destination) {
|
|
|
|
case LOGDEST_LOGFILE:
|
2004-04-13 00:27:04 -04:00
|
|
|
err_file=fopen(app,"a");
|
2004-04-13 00:23:36 -04:00
|
|
|
if(err_file==NULL) {
|
|
|
|
fprintf(stderr,"Error opening %s: %s\n",app,strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
break;
|
2004-04-13 00:23:36 -04:00
|
|
|
case LOGDEST_SYSLOG:
|
|
|
|
openlog(app,LOG_PID,LOG_DAEMON);
|
|
|
|
break;
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
2004-04-06 23:51:01 -04:00
|
|
|
|
|
|
|
err_logdestination=destination;
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
2003-11-23 01:10:25 -05:00
|
|
|
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Lock the error mutex. This is used to serialize
|
|
|
|
* log messages, as well as protect access to the memory
|
|
|
|
* list, when memory debugging is enabled.
|
2003-11-23 01:10:25 -05:00
|
|
|
*
|
2004-11-12 01:38:05 -05:00
|
|
|
* \returns 0 on success, otherwise -1 with errno set
|
2003-11-23 01:10:25 -05:00
|
|
|
*/
|
|
|
|
int err_lock_mutex(void) {
|
|
|
|
int err;
|
|
|
|
|
2004-02-14 19:51:11 -05:00
|
|
|
if((err=pthread_mutex_lock(&err_mutex))) {
|
2003-11-23 01:10:25 -05:00
|
|
|
errno=err;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
2003-11-23 01:10:25 -05:00
|
|
|
* Unlock the error mutex
|
|
|
|
*
|
2004-11-12 01:38:05 -05:00
|
|
|
* \returns 0 on success, otherwise -1 with errno set
|
2003-11-23 01:10:25 -05:00
|
|
|
*/
|
|
|
|
int err_unlock_mutex(void) {
|
|
|
|
int err;
|
|
|
|
|
2004-02-14 19:51:11 -05:00
|
|
|
if((err=pthread_mutex_unlock(&err_mutex))) {
|
2003-11-23 01:10:25 -05:00
|
|
|
errno=err;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-13 00:23:36 -04:00
|
|
|
#ifdef DEBUG_MEMORY
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
2003-11-26 01:10:21 -05:00
|
|
|
* Let the leak detector know about a chunk of memory
|
2004-11-12 01:38:05 -05:00
|
|
|
* that needs to be freed, but came from an external library.
|
|
|
|
* Example: gdbm functions. Note that this should only
|
2004-11-12 02:27:05 -05:00
|
|
|
* be called via the #MEMNOTIFY macro.
|
2004-11-12 01:38:05 -05:00
|
|
|
*
|
2004-11-12 02:27:05 -05:00
|
|
|
* \param file filled in from the #MEMNOTIFY macro with __FILE__
|
|
|
|
* \param line filled in from the #MEMNOTIFY macro with __LINE__
|
2004-11-12 01:38:05 -05:00
|
|
|
* \param ptr ptr to block of memory which must be freed
|
2003-11-26 01:10:21 -05:00
|
|
|
*/
|
|
|
|
void err_notify(char *file, int line, void *ptr) {
|
|
|
|
ERR_LEAK *pnew;
|
|
|
|
|
2004-03-03 13:51:12 -05:00
|
|
|
if(!ptr)
|
|
|
|
return;
|
|
|
|
|
2003-11-26 01:10:21 -05:00
|
|
|
pnew=(ERR_LEAK*)malloc(sizeof(ERR_LEAK));
|
|
|
|
if(!pnew)
|
|
|
|
log_err(1,"Error: cannot allocate leak struct\n");
|
|
|
|
|
|
|
|
if(err_lock_mutex())
|
|
|
|
log_err(1,"Error: cannot lock error mutex\n");
|
|
|
|
|
|
|
|
pnew->file=file;
|
|
|
|
pnew->line=line;
|
|
|
|
pnew->size=0;
|
|
|
|
pnew->ptr=ptr;
|
|
|
|
|
|
|
|
pnew->next=err_leak.next;
|
|
|
|
err_leak.next=pnew;
|
|
|
|
|
|
|
|
err_unlock_mutex();
|
|
|
|
}
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* malloc wrapper for leak checking. This never gets
|
|
|
|
* called directly, only via malloc.
|
2003-11-23 01:10:25 -05:00
|
|
|
*
|
2004-11-12 01:38:05 -05:00
|
|
|
* \param file filled in via macro with __FILE__
|
|
|
|
* \param line filled in via macro with __LINE__
|
|
|
|
* \param size size of block to allocate
|
2003-11-23 01:10:25 -05:00
|
|
|
*/
|
|
|
|
void *err_malloc(char *file, int line, size_t size) {
|
|
|
|
ERR_LEAK *pnew;
|
|
|
|
|
2003-11-23 01:16:57 -05:00
|
|
|
pnew=(ERR_LEAK*)malloc(sizeof(ERR_LEAK));
|
2003-11-23 01:10:25 -05:00
|
|
|
if(!pnew)
|
|
|
|
log_err(1,"Error: cannot allocate leak struct\n");
|
|
|
|
|
|
|
|
if(err_lock_mutex())
|
|
|
|
log_err(1,"Error: cannot lock error mutex\n");
|
|
|
|
|
|
|
|
pnew->file=file;
|
|
|
|
pnew->line=line;
|
|
|
|
pnew->size=size;
|
|
|
|
pnew->ptr=malloc(size);
|
|
|
|
|
|
|
|
pnew->next=err_leak.next;
|
|
|
|
err_leak.next=pnew;
|
|
|
|
|
|
|
|
err_unlock_mutex();
|
|
|
|
|
|
|
|
return pnew->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Memory check wrapper for strdup. This should not
|
|
|
|
* be called directly
|
2003-11-23 01:10:25 -05:00
|
|
|
*
|
2004-11-12 01:38:05 -05:00
|
|
|
* \param file filled in via macro with __FILE__
|
|
|
|
* \param line filled in via macro with __LINE__
|
|
|
|
* \param str str to strdup
|
2003-11-23 01:10:25 -05:00
|
|
|
*/
|
|
|
|
char *err_strdup(char *file, int line, const char *str) {
|
|
|
|
void *pnew;
|
|
|
|
|
|
|
|
pnew=err_malloc(file,line,strlen(str) + 1);
|
|
|
|
if(!pnew)
|
|
|
|
log_err(1,"Cannot malloc enough space for strdup\n");
|
|
|
|
|
|
|
|
memcpy(pnew,str,strlen(str)+1);
|
|
|
|
return pnew;
|
|
|
|
}
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Memory checking wrapper for free. This should not be
|
|
|
|
* called direclty.
|
2003-11-23 01:10:25 -05:00
|
|
|
*
|
2004-11-12 01:38:05 -05:00
|
|
|
* \param file filled in by macro with __FILE__
|
|
|
|
* \param line filled in by macro with __LINE__
|
|
|
|
* \param ptr block of memory to free
|
2003-11-23 01:10:25 -05:00
|
|
|
*/
|
|
|
|
void err_free(char *file, int line, void *ptr) {
|
|
|
|
ERR_LEAK *current,*last;
|
|
|
|
|
|
|
|
if(err_lock_mutex())
|
|
|
|
log_err(1,"Error: cannot lock error mutex\n");
|
|
|
|
|
|
|
|
last=&err_leak;
|
|
|
|
current=last->next;
|
|
|
|
|
|
|
|
while((current) && (current->ptr != ptr)) {
|
|
|
|
last=current;
|
|
|
|
current=current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!current) {
|
2003-11-26 01:10:21 -05:00
|
|
|
log_err(1,"Attempt to free unallocated memory: %s, %d\n",file,line);
|
2003-11-23 01:10:25 -05:00
|
|
|
} else {
|
|
|
|
free(current->ptr);
|
|
|
|
last->next=current->next;
|
|
|
|
free(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
err_unlock_mutex();
|
|
|
|
}
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Dumps the list of in-use memory. This walks the linked
|
|
|
|
* list created by the malloc and strdup wrappers, and dumps
|
|
|
|
* them to stdout.
|
2003-11-23 01:10:25 -05:00
|
|
|
*/
|
|
|
|
void err_leakcheck(void) {
|
|
|
|
ERR_LEAK *current;
|
|
|
|
|
|
|
|
if(err_lock_mutex())
|
|
|
|
log_err(1,"Error: cannot lock error mutex\n");
|
|
|
|
|
|
|
|
current=err_leak.next;
|
|
|
|
while(current) {
|
2004-02-14 19:51:11 -05:00
|
|
|
printf("%s: %d - %d bytes at %p\n",current->file, current->line, current->size,
|
2003-11-23 01:10:25 -05:00
|
|
|
current->ptr);
|
|
|
|
current=current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
err_unlock_mutex();
|
|
|
|
}
|
|
|
|
#endif
|