/* $OpenBSD: errwarn.c,v 1.7 2004/05/04 22:23:01 mickey Exp $ */12/* Errors and warnings... */34/*-5* SPDX-License-Identifier: BSD-3-Clause6*7* Copyright (c) 1996 The Internet Software Consortium.8* All Rights Reserved.9* Copyright (c) 1995 RadioMail Corporation. All rights reserved.10*11* Redistribution and use in source and binary forms, with or without12* modification, are permitted provided that the following conditions13* are met:14*15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20* 3. Neither the name of RadioMail Corporation, the Internet Software21* Consortium nor the names of its contributors may be used to endorse22* or promote products derived from this software without specific23* prior written permission.24*25* THIS SOFTWARE IS PROVIDED BY RADIOMAIL CORPORATION, THE INTERNET26* SOFTWARE CONSORTIUM AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR27* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED28* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29* ARE DISCLAIMED. IN NO EVENT SHALL RADIOMAIL CORPORATION OR CONTRIBUTORS30* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED36* OF THE POSSIBILITY OF SUCH DAMAGE.37*38* This software was written for RadioMail Corporation by Ted Lemon39* under a contract with Vixie Enterprises. Further modifications have40* been made for the Internet Software Consortium under a contract41* with Vixie Laboratories.42*/4344#include <sys/cdefs.h>45#include <errno.h>4647#include "dhcpd.h"4849static void do_percentm(char *obuf, size_t size, const char *ibuf);5051static char mbuf[1024];52static char fbuf[1024];5354int warnings_occurred;5556/*57* Log an error message, then exit.58*/59void60error(const char *fmt, ...)61{62va_list list;6364do_percentm(fbuf, sizeof(fbuf), fmt);6566va_start(list, fmt);67vsnprintf(mbuf, sizeof(mbuf), fbuf, list);68va_end(list);6970#ifndef DEBUG71cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);72#endif7374/* Also log it to stderr? */75if (log_perror) {76write(2, mbuf, strlen(mbuf));77write(2, "\n", 1);78}7980cap_syslog(capsyslog, LOG_CRIT, "exiting.");81if (log_perror) {82fprintf(stderr, "exiting.\n");83fflush(stderr);84}85if (pidfile != NULL)86pidfile_remove(pidfile);87exit(1);88}8990/*91* Log a warning message...92*/93int94warning(const char *fmt, ...)95{96va_list list;9798do_percentm(fbuf, sizeof(fbuf), fmt);99100va_start(list, fmt);101vsnprintf(mbuf, sizeof(mbuf), fbuf, list);102va_end(list);103104#ifndef DEBUG105cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);106#endif107108if (log_perror) {109write(2, mbuf, strlen(mbuf));110write(2, "\n", 1);111}112113return (0);114}115116/*117* Log a note...118*/119int120note(const char *fmt, ...)121{122va_list list;123124do_percentm(fbuf, sizeof(fbuf), fmt);125126va_start(list, fmt);127vsnprintf(mbuf, sizeof(mbuf), fbuf, list);128va_end(list);129130#ifndef DEBUG131cap_syslog(capsyslog, log_priority | LOG_INFO, "%s", mbuf);132#endif133134if (log_perror) {135write(2, mbuf, strlen(mbuf));136write(2, "\n", 1);137}138139return (0);140}141142/*143* Log a debug message...144*/145int146debug(const char *fmt, ...)147{148va_list list;149150do_percentm(fbuf, sizeof(fbuf), fmt);151152va_start(list, fmt);153vsnprintf(mbuf, sizeof(mbuf), fbuf, list);154va_end(list);155156#ifndef DEBUG157cap_syslog(capsyslog, log_priority | LOG_DEBUG, "%s", mbuf);158#endif159160if (log_perror) {161write(2, mbuf, strlen(mbuf));162write(2, "\n", 1);163}164165return (0);166}167168/*169* Find %m in the input string and substitute an error message string.170*/171static void172do_percentm(char *obuf, size_t size, const char *ibuf)173{174char ch;175const char *s = ibuf;176char *t = obuf;177size_t prlen;178size_t fmt_left;179int saved_errno = errno;180181/*182* We wouldn't need this mess if printf handled %m, or if183* strerror() had been invented before syslog().184*/185for (fmt_left = size; (ch = *s); ++s) {186if (ch == '%' && s[1] == 'm') {187++s;188prlen = snprintf(t, fmt_left, "%s",189strerror(saved_errno));190if (prlen >= fmt_left)191prlen = fmt_left - 1;192t += prlen;193fmt_left -= prlen;194} else {195if (fmt_left > 1) {196*t++ = ch;197fmt_left--;198}199}200}201*t = '\0';202}203204int205parse_warn(const char *fmt, ...)206{207va_list list;208static char spaces[] =209" "210" "; /* 80 spaces */211212do_percentm(mbuf, sizeof(mbuf), fmt);213snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf);214va_start(list, fmt);215vsnprintf(mbuf, sizeof(mbuf), fbuf, list);216va_end(list);217218#ifndef DEBUG219cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);220cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", token_line);221if (lexline < 81)222cap_syslog(capsyslog, log_priority | LOG_ERR,223"%s^", &spaces[sizeof(spaces) - lexchar]);224#endif225226if (log_perror) {227write(2, mbuf, strlen(mbuf));228write(2, "\n", 1);229write(2, token_line, strlen(token_line));230write(2, "\n", 1);231write(2, spaces, lexchar - 1);232write(2, "^\n", 2);233}234235warnings_occurred = 1;236237return (0);238}239240241