errwarn.c revision 147072
1259698Sdim/* $OpenBSD: errwarn.c,v 1.7 2004/05/04 22:23:01 mickey Exp $ */ 2259698Sdim 3259698Sdim/* Errors and warnings... */ 4259698Sdim 5259698Sdim/* 6259698Sdim * Copyright (c) 1996 The Internet Software Consortium. 7259698Sdim * All Rights Reserved. 8259698Sdim * Copyright (c) 1995 RadioMail Corporation. All rights reserved. 9259698Sdim * 10259698Sdim * Redistribution and use in source and binary forms, with or without 11259698Sdim * modification, are permitted provided that the following conditions 12276479Sdim * are met: 13259698Sdim * 14259698Sdim * 1. Redistributions of source code must retain the above copyright 15259698Sdim * notice, this list of conditions and the following disclaimer. 16259698Sdim * 2. Redistributions in binary form must reproduce the above copyright 17259698Sdim * notice, this list of conditions and the following disclaimer in the 18259698Sdim * documentation and/or other materials provided with the distribution. 19259698Sdim * 3. Neither the name of RadioMail Corporation, the Internet Software 20259698Sdim * Consortium nor the names of its contributors may be used to endorse 21259698Sdim * or promote products derived from this software without specific 22259698Sdim * prior written permission. 23259698Sdim * 24276479Sdim * THIS SOFTWARE IS PROVIDED BY RADIOMAIL CORPORATION, THE INTERNET 25259698Sdim * SOFTWARE CONSORTIUM AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR 26259698Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 27259698Sdim * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28276479Sdim * ARE DISCLAIMED. IN NO EVENT SHALL RADIOMAIL CORPORATION OR CONTRIBUTORS 29276479Sdim * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30259698Sdim * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31259698Sdim * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32259698Sdim * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33259698Sdim * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34259698Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 35280031Sdim * OF THE POSSIBILITY OF SUCH DAMAGE. 36280031Sdim * 37259698Sdim * This software was written for RadioMail Corporation by Ted Lemon 38259698Sdim * under a contract with Vixie Enterprises. Further modifications have 39276479Sdim * been made for the Internet Software Consortium under a contract 40259698Sdim * with Vixie Laboratories. 41259698Sdim */ 42296417Sdim 43259698Sdim#include <errno.h> 44259698Sdim 45259698Sdim#include "dhcpd.h" 46259698Sdim 47259698Sdimstatic void do_percentm(char *obuf, size_t size, char *ibuf); 48259698Sdim 49276479Sdimstatic char mbuf[1024]; 50259698Sdimstatic char fbuf[1024]; 51296417Sdim 52259698Sdimint warnings_occurred; 53259698Sdim 54259698Sdim/* 55259698Sdim * Log an error message, then exit. 56259698Sdim */ 57288943Sdimvoid 58259698Sdimerror(char *fmt, ...) 59259698Sdim{ 60276479Sdim va_list list; 61259698Sdim 62259698Sdim do_percentm(fbuf, sizeof(fbuf), fmt); 63259698Sdim 64259698Sdim va_start(list, fmt); 65296417Sdim vsnprintf(mbuf, sizeof(mbuf), fbuf, list); 66259698Sdim va_end(list); 67259698Sdim 68296417Sdim#ifndef DEBUG 69280031Sdim syslog(log_priority | LOG_ERR, "%s", mbuf); 70259698Sdim#endif 71296417Sdim 72296417Sdim /* Also log it to stderr? */ 73288943Sdim if (log_perror) { 74296417Sdim write(2, mbuf, strlen(mbuf)); 75259698Sdim write(2, "\n", 1); 76296417Sdim } 77296417Sdim 78296417Sdim syslog(LOG_CRIT, "exiting."); 79296417Sdim if (log_perror) { 80296417Sdim fprintf(stderr, "exiting.\n"); 81259698Sdim fflush(stderr); 82296417Sdim } 83296417Sdim exit(1); 84296417Sdim} 85259698Sdim 86288943Sdim/* 87288943Sdim * Log a warning message... 88259698Sdim */ 89296417Sdimint 90288943Sdimwarning(char *fmt, ...) 91296417Sdim{ 92296417Sdim va_list list; 93296417Sdim 94296417Sdim do_percentm(fbuf, sizeof(fbuf), fmt); 95296417Sdim 96296417Sdim va_start(list, fmt); 97296417Sdim vsnprintf(mbuf, sizeof(mbuf), fbuf, list); 98276479Sdim va_end(list); 99296417Sdim 100296417Sdim#ifndef DEBUG 101296417Sdim syslog(log_priority | LOG_ERR, "%s", mbuf); 102296417Sdim#endif 103296417Sdim 104259698Sdim if (log_perror) { 105259698Sdim write(2, mbuf, strlen(mbuf)); 106296417Sdim write(2, "\n", 1); 107296417Sdim } 108296417Sdim 109259698Sdim return (0); 110296417Sdim} 111296417Sdim 112296417Sdim/* 113296417Sdim * Log a note... 114296417Sdim */ 115296417Sdimint 116296417Sdimnote(char *fmt, ...) 117296417Sdim{ 118296417Sdim va_list list; 119296417Sdim 120259698Sdim do_percentm(fbuf, sizeof(fbuf), fmt); 121296417Sdim 122296417Sdim va_start(list, fmt); 123296417Sdim vsnprintf(mbuf, sizeof(mbuf), fbuf, list); 124296417Sdim va_end(list); 125296417Sdim 126296417Sdim#ifndef DEBUG 127296417Sdim syslog(log_priority | LOG_INFO, "%s", mbuf); 128296417Sdim#endif 129296417Sdim 130259698Sdim if (log_perror) { 131296417Sdim write(2, mbuf, strlen(mbuf)); 132296417Sdim write(2, "\n", 1); 133296417Sdim } 134288943Sdim 135296417Sdim return (0); 136296417Sdim} 137296417Sdim 138296417Sdim/* 139288943Sdim * Log a debug message... 140296417Sdim */ 141296417Sdimint 142296417Sdimdebug(char *fmt, ...) 143296417Sdim{ 144296417Sdim va_list list; 145296417Sdim 146296417Sdim do_percentm(fbuf, sizeof(fbuf), fmt); 147276479Sdim 148276479Sdim va_start(list, fmt); 149280031Sdim vsnprintf(mbuf, sizeof(mbuf), fbuf, list); 150280031Sdim va_end(list); 151296417Sdim 152296417Sdim#ifndef DEBUG 153259698Sdim syslog(log_priority | LOG_DEBUG, "%s", mbuf); 154259698Sdim#endif 155259698Sdim 156296417Sdim if (log_perror) { 157259698Sdim write(2, mbuf, strlen(mbuf)); 158280031Sdim write(2, "\n", 1); 159276479Sdim } 160280031Sdim 161276479Sdim return (0); 162296417Sdim} 163259698Sdim 164276479Sdim/* 165259698Sdim * Find %m in the input string and substitute an error message string. 166276479Sdim */ 167276479Sdimstatic void 168296417Sdimdo_percentm(char *obuf, size_t size, char *ibuf) 169296417Sdim{ 170276479Sdim char ch; 171276479Sdim char *s = ibuf; 172276479Sdim char *t = obuf; 173296417Sdim size_t prlen; 174296417Sdim size_t fmt_left; 175296417Sdim int saved_errno = errno; 176288943Sdim 177288943Sdim /* 178296417Sdim * We wouldn't need this mess if printf handled %m, or if 179259698Sdim * strerror() had been invented before syslog(). 180259698Sdim */ 181296417Sdim for (fmt_left = size; (ch = *s); ++s) { 182296417Sdim if (ch == '%' && s[1] == 'm') { 183259698Sdim ++s; 184276479Sdim prlen = snprintf(t, fmt_left, "%s", 185259698Sdim strerror(saved_errno)); 186276479Sdim if (prlen >= fmt_left) 187296417Sdim prlen = fmt_left - 1; 188288943Sdim t += prlen; 189288943Sdim fmt_left -= prlen; 190288943Sdim } else { 191288943Sdim if (fmt_left > 1) { 192288943Sdim *t++ = ch; 193296417Sdim fmt_left--; 194259698Sdim } 195276479Sdim } 196280031Sdim } 197 *t = '\0'; 198} 199 200int 201parse_warn(char *fmt, ...) 202{ 203 va_list list; 204 static char spaces[] = 205 " " 206 " "; /* 80 spaces */ 207 208 do_percentm(mbuf, sizeof(mbuf), fmt); 209 snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf); 210 va_start(list, fmt); 211 vsnprintf(mbuf, sizeof(mbuf), fbuf, list); 212 va_end(list); 213 214#ifndef DEBUG 215 syslog(log_priority | LOG_ERR, "%s", mbuf); 216 syslog(log_priority | LOG_ERR, "%s", token_line); 217 if (lexline < 81) 218 syslog(log_priority | LOG_ERR, 219 "%s^", &spaces[sizeof(spaces) - lexchar]); 220#endif 221 222 if (log_perror) { 223 write(2, mbuf, strlen(mbuf)); 224 write(2, "\n", 1); 225 write(2, token_line, strlen(token_line)); 226 write(2, "\n", 1); 227 write(2, spaces, lexchar - 1); 228 write(2, "^\n", 2); 229 } 230 231 warnings_occurred = 1; 232 233 return (0); 234} 235