=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ssh/log.c,v retrieving revision 1.25.2.2 retrieving revision 1.26 diff -u -r1.25.2.2 -r1.26 --- src/usr.bin/ssh/log.c 2004/03/04 18:18:15 1.25.2.2 +++ src/usr.bin/ssh/log.c 2003/04/08 20:21:28 1.26 @@ -34,13 +34,12 @@ */ #include "includes.h" -RCSID("$OpenBSD: log.c,v 1.25.2.2 2004/03/04 18:18:15 brad Exp $"); +RCSID("$OpenBSD: log.c,v 1.26 2003/04/08 20:21:28 itojun Exp $"); #include "log.h" #include "xmalloc.h" #include -#include static LogLevel log_level = SYSLOG_LEVEL_INFO; static int log_on_stderr = 1; @@ -178,6 +177,83 @@ va_end(args); } +/* Fatal cleanup */ + +struct fatal_cleanup { + struct fatal_cleanup *next; + void (*proc) (void *); + void *context; +}; + +static struct fatal_cleanup *fatal_cleanups = NULL; + +/* Registers a cleanup function to be called by fatal() before exiting. */ + +void +fatal_add_cleanup(void (*proc) (void *), void *context) +{ + struct fatal_cleanup *cu; + + cu = xmalloc(sizeof(*cu)); + cu->proc = proc; + cu->context = context; + cu->next = fatal_cleanups; + fatal_cleanups = cu; +} + +/* Removes a cleanup frunction to be called at fatal(). */ + +void +fatal_remove_cleanup(void (*proc) (void *context), void *context) +{ + struct fatal_cleanup **cup, *cu; + + for (cup = &fatal_cleanups; *cup; cup = &cu->next) { + cu = *cup; + if (cu->proc == proc && cu->context == context) { + *cup = cu->next; + xfree(cu); + return; + } + } + fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx", + (u_long) proc, (u_long) context); +} + +/* Remove all cleanups, to be called after fork() */ +void +fatal_remove_all_cleanups(void) +{ + struct fatal_cleanup *cu, *next_cu; + + for (cu = fatal_cleanups; cu; cu = next_cu) { + next_cu = cu->next; + xfree(cu); + } + fatal_cleanups = NULL; +} + +/* Cleanup and exit */ +void +fatal_cleanup(void) +{ + struct fatal_cleanup *cu, *next_cu; + static int called = 0; + + if (called) + exit(255); + called = 1; + /* Call cleanup functions. */ + for (cu = fatal_cleanups; cu; cu = next_cu) { + next_cu = cu->next; + debug("Calling cleanup 0x%lx(0x%lx)", + (u_long) cu->proc, (u_long) cu->context); + (*cu->proc) (cu->context); + } + exit(255); +} + + /* * Initialize the log. */ @@ -255,7 +331,6 @@ void do_log(LogLevel level, const char *fmt, va_list args) { - struct syslog_data sdata = SYSLOG_DATA_INIT; char msgbuf[MSGBUFSIZ]; char fmtbuf[MSGBUFSIZ]; char *txt = NULL; @@ -304,13 +379,11 @@ } else { vsnprintf(msgbuf, sizeof(msgbuf), fmt, args); } - strnvis(fmtbuf, msgbuf, sizeof(fmtbuf), VIS_SAFE|VIS_OCTAL); if (log_on_stderr) { - snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf); - write(STDERR_FILENO, msgbuf, strlen(msgbuf)); + fprintf(stderr, "%s\r\n", msgbuf); } else { - openlog_r(argv0 ? argv0 : __progname, LOG_PID, log_facility, &sdata); - syslog_r(pri, &sdata, "%.500s", fmtbuf); - closelog_r(&sdata); + openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility); + syslog(pri, "%.500s", msgbuf); + closelog(); } }