failures.c revision 65497fd5c3efa66319a1486addb272413403653a
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/*
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen failures.c : Failure manager
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen Copyright (c) 2001-2002 Timo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen Permission is hereby granted, free of charge, to any person obtaining
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen a copy of this software and associated documentation files (the
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen "Software"), to deal in the Software without restriction, including
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen without limitation the rights to use, copy, modify, merge, publish,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen distribute, sublicense, and/or sell copies of the Software, and to
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen permit persons to whom the Software is furnished to do so, subject to
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen the following conditions:
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen The above copyright notice and this permission notice shall be
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen included in all copies or substantial portions of the Software.
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40a5aeebf6b4858b93f0ddff0bf12fba769cf903Timo Sirainen MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
40a5aeebf6b4858b93f0ddff0bf12fba769cf903Timo Sirainen IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen*/
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen#include "lib.h"
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen#include "ioloop.h"
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen#include "fd-close-on-exec.h"
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen#include <stdio.h>
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen#include <stdlib.h>
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen#include <syslog.h>
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen#include <time.h>
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic void default_panic_handler(const char *format, va_list args)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen __attr_noreturn__;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic void default_fatal_handler(int status, const char *format, va_list args)
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen __attr_noreturn__;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic void default_error_handler(const char *format, va_list args);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic void default_warning_handler(const char *format, va_list args);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic void default_info_handler(const char *format, va_list args);
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen/* Initialize working defaults */
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic FailureFunc panic_handler __attr_noreturn__ = default_panic_handler;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic FatalFailureFunc fatal_handler __attr_noreturn__ = default_fatal_handler;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic FailureFunc error_handler = default_error_handler;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic FailureFunc warning_handler = default_warning_handler;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstatic FailureFunc info_handler = default_info_handler;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic FILE *log_fd = NULL, *log_info_fd = NULL;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic char *log_prefix = NULL, *log_stamp_format = NULL;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void write_prefix(FILE *f)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct tm *tm;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen char str[256];
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_prefix != NULL)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputs(log_prefix, f);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_stamp_format != NULL) {
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen tm = localtime(&ioloop_time);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (strftime(str, sizeof(str), log_stamp_format, tm) > 0)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputs(str, f);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen }
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void default_panic_handler(const char *format, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_fd == NULL) log_fd = stderr;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen write_prefix(log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputs("Panic: ", log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vfprintf(log_fd, printf_string_fix_format(format), args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputc('\n', log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen abort();
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void default_fatal_handler(int status, const char *format, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_fd == NULL) log_fd = stderr;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen write_prefix(log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputs("Fatal: ", log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vfprintf(log_fd, printf_string_fix_format(format), args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputc('\n', log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (fflush(log_fd) < 0 && status == FATAL_DEFAULT)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen status = FATAL_LOGWRITE;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen exit(status);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void default_error_handler(const char *format, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen int old_errno = errno;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_fd == NULL) log_fd = stderr;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen write_prefix(log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen t_push();
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputs("Error: ", log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vfprintf(log_fd, printf_string_fix_format(format), args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputc('\n', log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen t_pop();
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (fflush(log_fd) < 0)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen exit(FATAL_LOGWRITE);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen errno = old_errno;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void default_warning_handler(const char *format, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen int old_errno = errno;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_fd == NULL) log_fd = stderr;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen write_prefix(log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen t_push();
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputs("Warning: ", log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vfprintf(log_fd, printf_string_fix_format(format), args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputc('\n', log_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen t_pop();
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (fflush(log_fd) < 0)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen exit(FATAL_LOGWRITE);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen errno = old_errno;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void default_info_handler(const char *format, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen int old_errno = errno;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_info_fd == NULL) log_info_fd = stderr;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen write_prefix(log_info_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen t_push();
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vfprintf(log_info_fd, printf_string_fix_format(format), args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fputc('\n', log_info_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen t_pop();
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (fflush(log_info_fd) < 0)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen exit(FATAL_LOGWRITE);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen errno = old_errno;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_panic(const char *format, ...)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_list args;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_start(args, format);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen panic_handler(format, args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_end(args);
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen}
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainenvoid i_fatal(const char *format, ...)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_list args;
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_start(args, format);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fatal_handler(FATAL_DEFAULT, format, args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_end(args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_fatal_status(int status, const char *format, ...)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_list args;
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_start(args, format);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fatal_handler(status, format, args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_end(args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainenvoid i_error(const char *format, ...)
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen{
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen va_list args;
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen va_start(args, format);
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen error_handler(format, args);
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen va_end(args);
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_warning(const char *format, ...)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_list args;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_start(args, format);
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen warning_handler(format, args);
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen va_end(args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_info(const char *format, ...)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_list args;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_start(args, format);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen info_handler(format, args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen va_end(args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_panic_handler(FailureFunc func __attr_noreturn__)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (func == NULL)
8e361d2906b0e44f7175a20981f8d2280645b58bTimo Sirainen func = default_panic_handler;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen panic_handler = func;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_fatal_handler(FatalFailureFunc func __attr_noreturn__)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (func == NULL)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen func = default_fatal_handler;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fatal_handler = func;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_error_handler(FailureFunc func)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (func == NULL)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen func = default_error_handler;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen error_handler = func;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_warning_handler(FailureFunc func)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (func == NULL)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen func = default_warning_handler;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen warning_handler = func;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_info_handler(FailureFunc func)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (func == NULL)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen func = default_info_handler;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen info_handler = func;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_syslog_panic_handler(const char *fmt, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vsyslog(LOG_CRIT, fmt, args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen abort();
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
3281669db44d09a087a203201248abbc81b3cc1aTimo Sirainen
8e361d2906b0e44f7175a20981f8d2280645b58bTimo Sirainenvoid i_syslog_fatal_handler(int status, const char *fmt, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vsyslog(LOG_CRIT, fmt, args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen exit(status);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_syslog_error_handler(const char *fmt, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vsyslog(LOG_ERR, fmt, args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_syslog_warning_handler(const char *fmt, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vsyslog(LOG_WARNING, fmt, args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_syslog_info_handler(const char *fmt, va_list args)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen vsyslog(LOG_INFO, fmt, args);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_failure_syslog(const char *ident, int options, int facility)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
8e361d2906b0e44f7175a20981f8d2280645b58bTimo Sirainen openlog(ident, options, facility);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen i_set_panic_handler(i_syslog_panic_handler);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen i_set_fatal_handler(i_syslog_fatal_handler);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen i_set_error_handler(i_syslog_error_handler);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen i_set_warning_handler(i_syslog_warning_handler);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen i_set_info_handler(i_syslog_info_handler);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstatic void open_log_file(FILE **file, const char *path)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (*file != NULL && *file != stderr)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen (void)fclose(*file);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (path == NULL)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen *file = stderr;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen else {
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen *file = fopen(path, "a");
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (*file == NULL) {
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen i_fatal_status(FATAL_LOGOPEN,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen "Can't open log file %s: %m", path);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen }
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen fd_close_on_exec(fileno(*file), TRUE);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen }
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_failure_file(const char *path, const char *prefix)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen i_free(log_prefix);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen log_prefix = i_strconcat(prefix, ": ", NULL);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen open_log_file(&log_fd, path);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_info_fd != NULL && log_info_fd != stderr) {
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen (void)fclose(log_info_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen log_info_fd = log_fd;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen }
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid i_set_info_file(const char *path)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_info_fd == log_fd)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen log_info_fd = NULL;
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen
fad068d459cc7b04fedade4e0bb343be62e6d310Timo Sirainen open_log_file(&log_info_fd, path);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen info_handler = default_info_handler;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
fad068d459cc7b04fedade4e0bb343be62e6d310Timo Sirainenvoid i_set_failure_timestamp_format(const char *fmt)
fad068d459cc7b04fedade4e0bb343be62e6d310Timo Sirainen{
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen i_free(log_stamp_format);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen log_stamp_format = i_strdup(fmt);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainenvoid failures_deinit(void)
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen{
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen if (log_info_fd == log_fd)
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen log_info_fd = NULL;
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen
fad068d459cc7b04fedade4e0bb343be62e6d310Timo Sirainen if (log_fd != NULL && log_fd != stderr) {
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen (void)fclose(log_fd);
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen log_fd = stderr;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen }
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if (log_info_fd != NULL && log_info_fd != stderr) {
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen (void)fclose(log_info_fd);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen log_info_fd = stderr;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen }
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen}
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen