/* * libslack - https://libslack.org * * Copyright (C) 1999-2004, 2010, 2020-2023 raf * * 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, see . * * 20230824 raf */ /* =head1 NAME I - message/error/debug/verbosity/alert messaging module =head1 SYNOPSIS #include #include void msg(const char *format, ...); void vmsg(const char *format, va_list args); void verbose(size_t level, const char *format, ...); void vverbose(size_t level, const char *format, va_list args); void debugf(size_t level, const char *format, ...); void vdebugf(size_t level, const char *format, va_list args); int error(const char *format, ...); int verror(const char *format, va_list args); void fatal(const char *format, ...); void vfatal(const char *format, va_list args); void dump(const char *format, ...); void vdump(const char *format, va_list args); void alert(int priority, const char *format, ...); void valert(int priority, const char *format, va_list args); void debugsysf(size_t level, const char *format, ...); void vdebugsysf(size_t level, const char *format, va_list args); int errorsys(const char *format, ...); int verrorsys(const char *format, va_list args); void fatalsys(const char *format, ...); void vfatalsys(const char *format, va_list args); void dumpsys(const char *format, ...); void vdumpsys(const char *format, va_list args); void alertsys(int priority, const char *format, ...); void valertsys(int priority, const char *format, va_list args); int set_errno(int errnum); void *set_errnull(int errnum); void (*(set_errnullf)(int errnum))(); #define debug(args) #define vdebug(args) #define debugsys(args) #define vdebugsys(args) #define check(test, mesg) =head1 DESCRIPTION This module works with the I and I modules to provide functions for emitting various types of message with simple call syntax and flexible behaviour. The message types catered for are: normal, verbose, debug, error, fatal error, dump and alert messages. All messages are created and sent with I-like syntax. The destinations for these messages are configurable by the client. Calling I causes normal and verbose messages to be sent to standard output; and debug, error, fatal error, dump and alert messages to be sent to standard error. Calls to I, I, I, I, I and I cause normal and verbose messages to be sent to the specified destination. Calls to I, I, I, I, I and I cause error, fatal error and dump messages to be sent to the specified destination. Calls to I, I, I, I, I, I, I cause debug messages to be sent to the specified destination. Calls to I, I, I, I, I, I, I cause alert messages to be sent to the specified destination. Calls to the generic functions I, I, I and I cause their respective message types to be sent to the specified destination or destinations (multiplexing messages is only possible via these functions). See I for more details. =over 4 =cut */ #ifndef _BSD_SOURCE #define _BSD_SOURCE /* For snprintf() on OpenBSD-4.7 */ #endif #ifndef _DEFAULT_SOURCE #define _DEFAULT_SOURCE /* New name for _BSD_SOURCE */ #endif #include "config.h" #include "std.h" #include "msg.h" #include "prog.h" #include "err.h" #ifndef HAVE_SNPRINTF #include "snprintf.h" #endif #ifndef TEST /* =item C Outputs a message to the program's normal message destination. C is a I-like format string which processes any remaining arguments in the same way as I. B msg(buf); // EVIL msg("%s", buf); // GOOD =cut */ void msg(const char *format, ...) { va_list args; va_start(args, format); vmsg(format, args); va_end(args); } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ void vmsg(const char *format, va_list args) { vmsg_out(prog_out(), format, args); } /* =item C Outputs a verbose message to the program's normal message destination if C is less than or equal to the program's current verbosity level. If the program's name has been supplied using I, the message will be preceded by the name, a colon, and a space. The message is also preceded by as many spaces as the message level. This indents messages according to their verbosity. C is a I-like format string which processes any remaining arguments in the same way as I. The message is followed by a newline. =cut */ void verbose(size_t level, const char *format, ...) { if (prog_verbosity_level() >= level) { va_list args; va_start(args, format); vverbose(level, format, args); va_end(args); } } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ void vverbose(size_t level, const char *format, va_list args) { if (prog_verbosity_level() >= level) { char mesg[MSG_SIZE]; vsnprintf(mesg, MSG_SIZE, format, args); if (prog_name()) msg_out(prog_out(), "%s: %*s%s\n", prog_name(), level, "", mesg); else msg_out(prog_out(), "%*s%s\n", level, "", mesg); } } /* =item C Outputs a debug message to the program's debug message destination if C satisfies the program's current debug level. The debug level is broken into two components. The low byte specifies the level. The next three bytes specify a section within which the level applies. Debug messages with a section value whose bits overlap those of the program's current debug section, and with a level that is less than or equal to the program's current debug level, are emitted. As a convenience, if the program's current debug section is zero, debug messages with a sufficiently small level are emitted, regardless of the message section. See I for examples. If the program's name has been supplied using I, the message will be preceded by the name, a colon, and a space. This is followed by the string C<"debug:">. If C specifies a non-zero section, it is included in the debug message, after a space and surrounded by square brackets. This is followed by as many spaces as the debug level. This indents debug messages according to their debug level. C is a I-like format string which processes any remaining arguments in the same way as I. The message is followed by a newline. =cut */ static int debug_level_match(size_t level) { size_t debug_level, debug_section, section; debug_level = prog_debug_level(); debug_section = debug_level & 0xffffff00; debug_level &= 0x000000ff; section = level & 0xffffff00; level &= 0x000000ff; return (!debug_section || debug_section & section) && debug_level >= level; } void debugf(size_t level, const char *format, ...) { if (debug_level_match(level)) { va_list args; va_start(args, format); vdebugf(level, format, args); va_end(args); } } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ void vdebugf(size_t level, const char *format, va_list args) { if (debug_level_match(level)) { char mesg[MSG_SIZE], prefix[32] = ""; vsnprintf(mesg, MSG_SIZE, format, args); if (level & 0xffffff00) snprintf(prefix, 32, " [%d]", (int)((level & 0xffffff00) >> 8)); if (prog_name()) msg_out(prog_dbg(), "%s: debug:%s%*s%s\n", prog_name(), prefix, level & 0xff, "", mesg); else msg_out(prog_dbg(), "debug:%s%*s%s\n", prefix, level & 0xff, "", mesg); } } /* =item C Outputs an error message to the program's error message destination. If the program's name has been supplied using I, the message will be preceded by the name, a colon, and a space. C is a I-like format string which processes any remaining arguments in the same way as I. The message is followed by a newline. Returns -1. =cut */ int error(const char *format, ...) { va_list args; va_start(args, format); verror(format, args); va_end(args); return -1; } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ int verror(const char *format, va_list args) { char mesg[MSG_SIZE]; vsnprintf(mesg, MSG_SIZE, format, args); if (prog_name()) msg_out(prog_err(), "%s: %s\n", prog_name(), mesg); else msg_out(prog_err(), "%s\n", mesg); return -1; } /* =item C Outputs an error message to the program's error message destination, and then calls I with a return code of C. If the program's name was supplied using I, the message will be preceded by the name, a colon, and a space. This is followed by the string C<"fatal: ">. C is a I-like format string which processes any remaining arguments in the same way as I. The message is followed by a newline. B Never use this in a library. Only an application can decide which errors are fatal. =cut */ void fatal(const char *format, ...) { va_list args; va_start(args, format); vfatal(format, args); va_end(args); /* unreached */ } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ void vfatal(const char *format, va_list args) { char mesg[MSG_SIZE]; vsnprintf(mesg, MSG_SIZE, format, args); error("fatal: %s", mesg); exit(EXIT_FAILURE); } /* =item C Outputs an error message to the program's error message destination and then calls I. If the program's name was supplied using I, the message will be preceded by the name, a colon, and a space. This is followed by the string C<"dump: ">. C is a I-like format string which processes any remaining arguments in the same way as I. The message is followed by a newline. B Never use this in a library. Only an application can decide which errors are fatal. =cut */ void dump(const char *format, ...) { va_list args; va_start(args, format); vdump(format, args); va_end(args); /* unreached */ } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ void vdump(const char *format, va_list args) { char mesg[MSG_SIZE]; vsnprintf(mesg, MSG_SIZE, format, args); error("dump: %s", mesg); abort(); } /* =item C Outputs an alert message of the given C to the program's alert message destination. If the program's name has been supplied using I, the message will be preceded by the name, a colon, and a space. C is a I-like format string which processes any remaining arguments in the same way as I. The message is followed by a newline. Note that this only works when the program's alert message destination is a simple syslog destination. If the alert message destination is anything else (including a multiplexing message destination containing syslog destinations), C is ignored. =cut */ void alert(int priority, const char *format, ...) { va_list args; va_start(args, format); valert(priority, format, args); va_end(args); } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ void valert(int priority, const char *format, va_list args) { Msg *alert; char mesg[MSG_SIZE]; int err; vsnprintf(mesg, MSG_SIZE, format, args); alert = prog_alert(); if ((err = msg_wrlock(alert))) { set_errno(err); return; } msg_syslog_set_priority_unlocked(alert, priority); if (prog_name()) msg_out_unlocked(alert, "%s: %s\n", prog_name(), mesg); else msg_out_unlocked(alert, "%s\n", mesg); if ((err = msg_unlock(alert))) set_errno(err); } /* =item C Equivalent to I except that the message is followed by a colon, a space, the string representation of C, and a newline (rather than just a newline). =cut */ void debugsysf(size_t level, const char *format, ...) { if (debug_level_match(level)) { va_list args; va_start(args, format); vdebugsysf(level, format, args); va_end(args); } } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ void vdebugsysf(size_t level, const char *format, va_list args) { if (debug_level_match(level)) { char mesg[MSG_SIZE]; int errno_saved = errno; vsnprintf(mesg, MSG_SIZE, format, args); debugf(level, "%s: %s", mesg, strerror(errno_saved)); } } /* =item C Equivalent to I except that the message is followed by a colon, a space, the string representation of C, and a newline (rather than just a newline). =cut */ int errorsys(const char *format, ...) { va_list args; va_start(args, format); verrorsys(format, args); va_end(args); return -1; } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ int verrorsys(const char *format, va_list args) { char mesg[MSG_SIZE]; int errno_saved = errno; vsnprintf(mesg, MSG_SIZE, format, args); return error("%s: %s", mesg, strerror(errno_saved)); } /* =item C Equivalent to I except that the message is followed by a colon, a space, the string representation of C, and a newline (rather than just a newline). =cut */ void fatalsys(const char *format, ...) { va_list args; va_start(args, format); vfatalsys(format, args); va_end(args); /* unreached */ } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ void vfatalsys(const char *format, va_list args) { char mesg[MSG_SIZE]; int errno_saved = errno; vsnprintf(mesg, MSG_SIZE, format, args); fatal("%s: %s", mesg, strerror(errno_saved)); } /* =item C Equivalent to I except that the message is followed by a colon, a space, the string representation of C, and a newline (rather than just a newline). =cut */ void dumpsys(const char *format, ...) { va_list args; va_start(args, format); vdumpsys(format, args); va_end(args); /* unreached */ } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ void vdumpsys(const char *format, va_list args) { char mesg[MSG_SIZE]; int errno_saved = errno; vsnprintf(mesg, MSG_SIZE, format, args); dump("%s: %s", mesg, strerror(errno_saved)); } /* =item C Equivalent to I except that the message is followed by a colon, a space, the string representation of C, and a newline (rather than just a newline). =cut */ void alertsys(int priority, const char *format, ...) { va_list args; va_start(args, format); valertsys(priority, format, args); va_end(args); } /* =item C Equivalent to I with the variable argument list specified directly as for I. =cut */ void valertsys(int priority, const char *format, va_list args) { char mesg[MSG_SIZE]; int errno_saved = errno; vsnprintf(mesg, MSG_SIZE, format, args); alert(priority, "%s: %s", mesg, strerror(errno_saved)); } /* =item C Sets C to C and returns -1. =cut */ int (set_errno)(int errnum) { errno = errnum; return -1; } /* =item C Sets C to C and returns C. =cut */ void *(set_errnull)(int errnum) { errno = errnum; return NULL; } /* =item C Sets C to C and returns C as a function pointer. This is useful because I doesn't like casting normal pointers into function pointers. =cut */ void (*(set_errnullf)(int errnum))() { errno = errnum; return NULL; } /* =item C< #define debug(args)> Calls I unless C is defined. C must be supplied with extra parentheses. e.g. debug((1, "rc=%d", rc)) Note that the I macro always generates its own semicolon. This used to only be the case when C was not defined, but it is now consistent. =item C< #define vdebug(args)> Calls I unless C is defined. C must be supplied with extra parentheses. e.g. vdebug((1, format, args)) Note that the I macro always generates its own semicolon. This used to only be the case when C was not defined, but it is now consistent. =item C< #define debugsys(args)> Calls I unless C is defined. C must be supplied with extra parentheses. e.g. debugsys((1, "fd=%d", fd)) Note that the I macro always generates its own semicolon. This used to only be the case when C was not defined, but it is now consistent. =item C< #define vdebugsys(args)> Calls I unless C is defined. C must be supplied with extra parentheses. e.g. vdebugsys((1, format, args)) Note that the I macro always generates its own semicolon. This used to only be the case when C was not defined, but it is now consistent. =item C< #define check(cond, mesg)> Like I but includes a string argument, C, for including an explanation of the condition tested and then calls I to terminate the program. This means the message will be sent to the right place(s) rather than just to C. B Like I, this function is largely useless. It should never be left in production code (because it's rude), so you need to write code to handle error conditions properly anyway. You might as well not bother using I or I in the first place. =back =head1 MT-Level I =head1 EXAMPLES Send a range of messages to default locations: #include #include #include int main(int ac, char **av) { prog_init(); prog_set_debug_level(1); prog_set_verbosity_level(1); msg("This is a %s\n", "message"); verbose(0, "This is a %s message (level %d)", "verbose", 0); verbose(1, "This is a %s message (level %d)", "verbose", 1); verbose(2, "This is a %s message (level %d)", "verbose", 2); debug((0, "This is a %s message (level %d)", "debug", 0)) debug((1, "This is a %s message (level %d)", "debug", 1)) debug((2, "This is a %s message (level %d)", "debug", 2)) alert(LOG_ERR, "This is an %s message", "alert"); error("This is an %s message", "error"); fatal("This is a %s message", "fatal error"); return EXIT_SUCCESS; } =head1 SEE ALSO I, I, I, I =head1 AUTHOR 20230824 raf =cut */ #endif #ifdef TEST #include #include #include #include #include #include "str.h" int verify(int test, const char *name, const char *result) { char buf[BUFSIZ]; int fd; ssize_t bytes; if ((fd = open(name, O_RDONLY)) == -1) { printf("Test%d: failed to create err file: %s (%s)\n", test, name, strerror(errno)); return 1; } memset(buf, 0, BUFSIZ); bytes = read(fd, buf, BUFSIZ); close(fd); unlink(name); if (bytes == -1) { printf("Test%d: failed to read err file: %s (%s)\n", test, name, strerror(errno)); return 1; } if (!strstr(buf, result)) { printf("Test%d: err file produced incorrect input:\nshould contain:\n%s\nwas:\n%s\n", test, result, buf); return 1; } return 0; } int verifysys(int test, const char *name, const char *result, int err) { char buf[BUFSIZ]; snprintf(buf, BUFSIZ, result, strerror(err)); return verify(test, name, buf); } int msg_filter_prefix(void **mesgp, const void *mesg, size_t mesglen) { return asprintf((char **)mesgp, "[%d] %.*s", 12345, (int)mesglen, (char *)mesg); } int main(int ac, char **av) { const char * const out = "err.out"; const char * const err = "err.err"; const char * const dbg = "err.dbg"; const char * const alertfile = "err.alert"; const char * const core = "core"; const char * const core2 = "err.core"; /* OpenBSD */ char buf[BUFSIZ]; int rci; void *rcp; void (*rcfp)(); const char *results[12] = { "msg\n", "verbosemsg\n", "debug: debugmsg\n", "errormsg\n", "fatal: fatalmsg\n", "dump: dumpmsg\n", "debug: debugsysmsg: %s\n", "errorsysmsg: %s\n", "fatal: fatalsysmsg: %s\n", "dump: dumpsysmsg: %s\n", "debug: [1] lexer debugmsg\n" "debug: [2] parser debugmsg\n" "debug: [1] lexer debugmsg\n" "debug: [2] parser debugmsg\n" "debug: [4] interp debugmsg\n" "debug: global debugmsg\n", "alertmsg\n" "alertsysmsg: " /* followed by "Success" or "Error 0" */ }; pid_t pid; int errors = 0; if (ac == 2 && !strcmp(av[1], "help")) { printf("usage: %s\n", *av); return EXIT_SUCCESS; } printf("Testing: %s\n", "err"); /* Test debug, verbose and error */ prog_set_debug_level(1); prog_set_verbosity_level(1); prog_out_file(out); msg("msg\n"); errors += verify(1, out, results[0]); prog_out_file(out); verbose(1, "verbosemsg"); errors += verify(2, out, results[1]); prog_dbg_file(dbg); debugf(1, "debugmsg"); errors += verify(3, dbg, results[2]); prog_err_file(err); error("errormsg"); errors += verify(4, err, results[3]); /* Test filtered debug */ prog_dbg_file(dbg); if (prog_dbg_push_filter(msg_filter_prefix) == -1) ++errors, printf("Test5: prog_dbg_push_filter() failed\n"); else { debugf(1, "filteredmsg"); errors += verify(6, dbg, "[12345] debug: filteredmsg"); } /* Test fatal */ switch (pid = fork()) { case 0: { prog_err_file(err); fatal("fatalmsg"); } case -1: { ++errors; printf("Test7: failed to perform test - fork() failed (%s)\n", strerror(errno)); break; } default: { int status; if (waitpid(pid, &status, 0) == -1) { ++errors, printf("Test7: failed to wait for test - waitpid(%d) failed (%s)\n", (int)pid, strerror(errno)); break; } if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_FAILURE) ++errors, printf("Test7: failed: %s %d (expected: exit code %d)\n", WIFSIGNALED(status) ? "received signal" : "exit code", WIFSIGNALED(status) ? WTERMSIG(status) : WEXITSTATUS(status), EXIT_FAILURE); } } errors += verify(7, err, results[4]); /* Test dump */ switch (pid = fork()) { case 0: { prog_err_file(err); unlink(core); unlink(core2); dump("dumpmsg"); } case -1: { ++errors; printf("Test8: failed to perform test - fork() failed (%s)\n", strerror(errno)); break; } default: { int status; if (waitpid(pid, &status, 0) == -1) { ++errors, printf("Test8: failed to wait for test - waitpid(%d) failed (%s)\n", (int)pid, strerror(errno)); break; } if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) { ++errors, printf("Test8: failed: %s %d (expected: received signal %d)\n", WIFSIGNALED(status) ? "received signal" : "exit code", WIFSIGNALED(status) ? WTERMSIG(status) : WEXITSTATUS(status), SIGABRT); } /* Check for core dump, but ulimit might prevent them */ #ifdef WCOREDUMP if (WIFSIGNALED(status) && !WCOREDUMP(status)) { struct rlimit limit[1] = {{ 0, 0 }}; if (getrlimit(RLIMIT_CORE, limit) != -1 && limit->rlim_cur != 0) printf("Warning: dump() produced no core file (even though ulimit -c %d)\n", (int)limit->rlim_cur); } #endif /* Core files might be anywhere these days, but they might be here */ unlink(core); unlink(core2); } } errors += verify(8, err, results[5]); /* Test debugsys, errorsys */ prog_dbg_file(dbg); set_errno(EPERM); debugsysf(1, "debugsysmsg"); errors += verifysys(9, dbg, results[6], EPERM); prog_err_file(err); set_errno(ENOENT); errorsys("errorsysmsg"); errors += verifysys(10, err, results[7], ENOENT); /* Test fatalsys */ switch (pid = fork()) { case 0: { prog_err_file(err); set_errno(EPERM); fatalsys("fatalsysmsg"); } case -1: { ++errors; printf("Test11: failed to perform test - fork() failed (%s)\n", strerror(errno)); break; } default: { int status; if (waitpid(pid, &status, 0) == -1) { ++errors; printf("Test11: failed to wait for test - waitpid(%d) failed (%s)\n", (int)pid, strerror(errno)); break; } if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_FAILURE) ++errors, printf("Test11: failed: %s %d (expected: exit code %d)\n", WIFSIGNALED(status) ? "received signal" : "exit code", WIFSIGNALED(status) ? WTERMSIG(status) : WEXITSTATUS(status), EXIT_FAILURE); } } errors += verifysys(11, err, results[8], EPERM); /* Test dumpsys */ switch (pid = fork()) { case 0: { prog_err_file(err); unlink(core); unlink(core2); set_errno(ENOENT); dumpsys("dumpsysmsg"); } case -1: { ++errors; printf("Test12: failed to perform test - fork() failed (%s)\n", strerror(errno)); break; } default: { int status; if (waitpid(pid, &status, 0) == -1) { ++errors; printf("Test12: failed to wait for test - waitpid(%d) failed (%s)\n", (int)pid, strerror(errno)); break; } if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) ++errors, printf("Test12: failed: %s %d (expected: received signal %d)\n", WIFSIGNALED(status) ? "received signal" : "exit code", WIFSIGNALED(status) ? WTERMSIG(status) : WEXITSTATUS(status), SIGABRT); /* Check for core dump, but ulimit might prevent them */ #ifdef WCOREDUMP if (WIFSIGNALED(status) && !WCOREDUMP(status)) { struct rlimit limit[1] = {{ 0, 0 }}; if (getrlimit(RLIMIT_CORE, limit) != -1 && limit->rlim_cur != 0) printf("Warning: dump() produced no core file (even though ulimit -c %d)\n", (int)limit->rlim_cur); } #endif /* Core files might be anywhere these days, but they might be here */ unlink(core); unlink(core2); } } errors += verifysys(12, err, results[9], ENOENT); /* Test check true */ switch (pid = fork()) { case 0: { int i = 1; prog_err_file(err); unlink(core); unlink(core2); set_errno(ENOENT); check(i == 1, "checkmsg"); _exit(EXIT_SUCCESS); } case -1: { ++errors; printf("Test13: failed to perform test - fork() failed (%s)\n", strerror(errno)); break; } default: { int status; if (waitpid(pid, &status, 0) == -1) { ++errors; printf("Test13: failed to wait for test - waitpid(%d) failed (%s)\n", (int)pid, strerror(errno)); break; } if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) ++errors, printf("Test13: failed: %s %d (expected: exit code %d)\n", WIFSIGNALED(status) ? "received signal" : "exit code", WIFSIGNALED(status) ? WTERMSIG(status) : WEXITSTATUS(status), EXIT_SUCCESS); } } /* Test check false */ switch (pid = fork()) { case 0: { int i = 1; prog_err_file(err); unlink(core); unlink(core2); set_errno(ENOENT); check(i == 0, "checkmsg"); _exit(EXIT_SUCCESS); } case -1: { ++errors; printf("Test14: failed to perform test - fork() failed (%s)\n", strerror(errno)); break; } default: { int status; if (waitpid(pid, &status, 0) == -1) { ++errors; printf("Test14: failed to wait for test - waitpid(%d) failed (%s)\n", (int)pid, strerror(errno)); break; } if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) ++errors, printf("Test14: failed: %s %d (expected: received signal %d)\n", WIFSIGNALED(status) ? "received signal" : "exit code", WIFSIGNALED(status) ? WTERMSIG(status) : WEXITSTATUS(status), SIGABRT); /* Check for core dump, but ulimit might prevent them */ #ifdef WCOREDUMP if (WIFSIGNALED(status) && !WCOREDUMP(status)) { struct rlimit limit[1] = {{ 0, 0 }}; if (getrlimit(RLIMIT_CORE, limit) != -1 && limit->rlim_cur != 0) printf("Warning: dump() produced no core file (even though ulimit -c %d)\n", (int)limit->rlim_cur); } #endif /* Core files might be anywhere these days, but they might be here */ unlink(core); unlink(core2); } } snprintf(buf, BUFSIZ, "dump: Internal Error: %s: %s [", "i == 0", "checkmsg"); errors += verify(14, err, buf); /* Test debug sections */ #define LEXER_SECTION (1 << 8) #define PARSER_SECTION (2 << 8) #define INTERP_SECTION (4 << 8) prog_set_debug_level(LEXER_SECTION | PARSER_SECTION | 1); prog_dbg_file(dbg); msg_set_timestamp_format(""); debugf(LEXER_SECTION | 1, "lexer debugmsg"); /* yes */ debugf(LEXER_SECTION | 4, "lexer debugmsg"); /* no (level too high) */ debugf(PARSER_SECTION | 1, "parser debugmsg"); /* yes */ debugf(INTERP_SECTION | 1, "interp debugmsg"); /* no (wrong section) */ debugf(1, "global debugmsg"); /* no (no section to match) */ prog_set_debug_level(1); debugf(LEXER_SECTION | 1, "lexer debugmsg"); /* yes */ debugf(LEXER_SECTION | 4, "lexer debugmsg"); /* no (level too high) */ debugf(PARSER_SECTION | 1, "parser debugmsg"); /* yes */ debugf(INTERP_SECTION | 1, "interp debugmsg"); /* yes */ debugf(1, "global debugmsg"); /* yes */ debugf(4, "global debugmsg"); /* no (level too high) */ errors += verify(15, dbg, results[10]); #undef LEXER_SECTION #undef PARSER_SECTION #undef INTERP_SECTION /* Test alert() and alertsys() */ prog_alert_file(alertfile); alert(LOG_INFO, "alertmsg"); errno = 0; alertsys(LOG_INFO, "alertsysmsg"); errors += verify(16, alertfile, results[11]); prog_out_none(); prog_err_none(); prog_dbg_none(); prog_alert_none(); /* Test set_errno() and set_errnull() and set_errnullf() */ errno = 0; if ((rci = set_errno(EINVAL)) != -1) ++errors, printf("Test17: set_errno(EINVAL) failed (returned %d, not %d)\n", rci, -1); else if (errno != EINVAL) ++errors, printf("Test17: set_errno(EINVAL) failed (errno = %s, not %s)\n", strerror(errno), strerror(EINVAL)); errno = 0; if ((rcp = set_errnull(EINVAL)) != NULL) ++errors, printf("Test18: set_errnull(EINVAL) failed (returned %p, not %p)\n", rcp, (void *)NULL); else if (errno != EINVAL) ++errors, printf("Test18: set_errnull(EINVAL) failed (errno = %s, not %s)\n", strerror(errno), strerror(EINVAL)); errno = 0; if ((rcfp = set_errnullf(EINVAL)) != NULL) /* ++errors, printf("Test19: set_errnullf(EINVAL) failed (returned %p, not %p)\n", rcfp, (void *)NULL); */ ++errors, printf("Test19: set_errnullf(EINVAL) failed (returned something other than null)\n"); else if (errno != EINVAL) ++errors, printf("Test19: set_errnullf(EINVAL) failed (errno = %s, not %s)\n", strerror(errno), strerror(EINVAL)); if (errors) printf("%d/19 tests failed\n", errors); else printf("All tests passed\n"); return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } #endif /* vi:set ts=4 sw=4: */