failures.h revision b547a4e327dc376b8fb1b50eb587d4f63d62365d
89a126810703c666309310d0f3189e9834d70b5bTimo Sirainen#ifndef FAILURES_H
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#define FAILURES_H
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
6157a322f2ac1ea1332d9003ecb0b11466aa8fe7Timo Sirainenstruct ip_addr;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen/* Default exit status codes that we could use. */
1098fc409a45e7603701dc94635927a673bee0c1Timo Sirainenenum fatal_exit_status {
8b681dae1e8fa564649e703ab17398dcfaf896e4Timo Sirainen FATAL_LOGOPEN = 80, /* Can't open log file */
4bbee99b3aef449a9a2a11a5b5cf1ca486915c49Timo Sirainen FATAL_LOGWRITE = 81, /* Can't write to log file */
c4cfee078c4a185b5ba8f0c55f51275b7e885b2cTimo Sirainen FATAL_LOGERROR = 82, /* Internal logging error */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen FATAL_OUTOFMEM = 83, /* Out of memory */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen FATAL_EXEC = 84, /* exec() failed */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen FATAL_DEFAULT = 89
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen};
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainenenum log_type {
8c02331f9f569d8b30e74b6bc8550734d65f9daeTimo Sirainen LOG_TYPE_INFO,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen LOG_TYPE_WARNING,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen LOG_TYPE_ERROR,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen LOG_TYPE_FATAL,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen LOG_TYPE_PANIC,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen LOG_TYPE_COUNT,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen LOG_TYPE_OPTION
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen};
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenstruct failure_line {
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen pid_t pid;
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen enum log_type log_type;
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen const char *text;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen};
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen#define DEFAULT_FAILURE_STAMP_FORMAT "%b %d %H:%M:%S "
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainentypedef void failure_callback_t(enum log_type type, const char *, va_list);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainentypedef void fatal_failure_callback_t(enum log_type type, int status,
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen const char *, va_list);
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainenextern const char *failure_log_type_prefixes[];
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainenvoid i_log_type(enum log_type type, const char *format, ...) ATTR_FORMAT(2, 3);
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainenvoid i_panic(const char *format, ...) ATTR_FORMAT(1, 2) ATTR_NORETURN;
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainenvoid i_fatal(const char *format, ...) ATTR_FORMAT(1, 2) ATTR_NORETURN;
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainenvoid i_error(const char *format, ...) ATTR_FORMAT(1, 2);
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainenvoid i_warning(const char *format, ...) ATTR_FORMAT(1, 2);
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainenvoid i_info(const char *format, ...) ATTR_FORMAT(1, 2);
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenvoid i_fatal_status(int status, const char *format, ...)
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen ATTR_FORMAT(2, 3) ATTR_NORETURN;
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen/* Change failure handlers. */
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen#ifndef __cplusplus
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenvoid i_set_fatal_handler(fatal_failure_callback_t *callback ATTR_NORETURN);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen#else
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen/* Older g++ doesn't like attributes in parameters */
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenvoid i_set_fatal_handler(fatal_failure_callback_t *callback);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen#endif
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenvoid i_set_error_handler(failure_callback_t *callback);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenvoid i_set_info_handler(failure_callback_t *callback);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenvoid i_get_failure_handlers(fatal_failure_callback_t **fatal_callback_r,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen failure_callback_t **error_callback_r,
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen failure_callback_t **info_callback_r);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen/* Send failures to file. */
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenvoid default_fatal_handler(enum log_type type, int status,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const char *format, va_list args)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen ATTR_NORETURN ATTR_FORMAT(3, 0);
9d3ccd79130199ffdb19a688027d49bf20a4aaaaTimo Sirainenvoid default_error_handler(enum log_type type, const char *format, va_list args)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen ATTR_FORMAT(2, 0);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
8d6cb44a0161d88743756733f83c4fb278485987Timo Sirainen/* Send failures to syslog() */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid i_syslog_fatal_handler(enum log_type type, int status,
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen const char *fmt, va_list args)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen ATTR_NORETURN ATTR_FORMAT(3, 0);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid i_syslog_error_handler(enum log_type type, const char *fmt, va_list args)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen ATTR_FORMAT(2, 0);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen/* Open syslog and set failure/info handlers to use it. */
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainenvoid i_set_failure_syslog(const char *ident, int options, int facility);
5c0034beb9933bca2a8b7d83d11dface1ea3b7faTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen/* Send failures to specified log file instead of stderr. */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid i_set_failure_file(const char *path, const char *prefix);
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainen/* Send errors to stderr using internal error protocol. */
9bf2dc275ec21bff3d468ab1bc4fddc8874f7d1bTimo Sirainenvoid i_set_failure_internal(void);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen/* If writing to log fails, ignore it instead of existing with
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen FATAL_LOGWRITE or FATAL_LOGERROR. */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid i_set_failure_ignore_errors(bool ignore);
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen/* Send informational messages to specified log file. i_set_failure_*()
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen functions modify the info file too, so call this function after them. */
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainenvoid i_set_info_file(const char *path);
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen/* Set the failure prefix. */
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainenvoid i_set_failure_prefix(const char *prefix);
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen/* Prefix failures with a timestamp. fmt is in strftime() format. */
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainenvoid i_set_failure_timestamp_format(const char *fmt);
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen/* When logging with internal error protocol, update the process's current
be889d9b142fbb5604a922c6955bd7f6ea32f163Timo Sirainen IP address. This is mainly used by the master process to log some IP
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen address if the process crash. */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid i_set_failure_ip(const struct ip_addr *ip);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen/* Call the callback before exit()ing. The callback may update the status. */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid i_set_failure_exit_callback(void (*callback)(int *status));
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen/* Call the exit callback and exit() */
0cea9b1f4fa0495a48f5f097e40492517d67e1baTimo Sirainenvoid failure_exit(int status) ATTR_NORETURN;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen/* Parse a line logged using internal failure handler */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid i_failure_parse_line(const char *line, struct failure_line *failure);
0cea9b1f4fa0495a48f5f097e40492517d67e1baTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenvoid failures_deinit(void);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#endif
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen