errwarn.c revision 226345
1254562Scy/* $OpenBSD: errwarn.c,v 1.7 2004/05/04 22:23:01 mickey Exp $ */ 2254562Scy 3254562Scy/* Errors and warnings... */ 4254562Scy 5254562Scy/* 6254562Scy * Copyright (c) 1996 The Internet Software Consortium. 7254562Scy * All Rights Reserved. 8254562Scy * Copyright (c) 1995 RadioMail Corporation. All rights reserved. 9254562Scy * 10254562Scy * Redistribution and use in source and binary forms, with or without 11254562Scy * modification, are permitted provided that the following conditions 12254562Scy * are met: 13254562Scy * 14254562Scy * 1. Redistributions of source code must retain the above copyright 15254562Scy * notice, this list of conditions and the following disclaimer. 16254562Scy * 2. Redistributions in binary form must reproduce the above copyright 17254562Scy * notice, this list of conditions and the following disclaimer in the 18254562Scy * documentation and/or other materials provided with the distribution. 19254562Scy * 3. Neither the name of RadioMail Corporation, the Internet Software 20254562Scy * Consortium nor the names of its contributors may be used to endorse 21254562Scy * or promote products derived from this software without specific 22254562Scy * prior written permission. 23254562Scy * 24254562Scy * THIS SOFTWARE IS PROVIDED BY RADIOMAIL CORPORATION, THE INTERNET 25254562Scy * SOFTWARE CONSORTIUM AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR 26254562Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 27254562Scy * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28254562Scy * ARE DISCLAIMED. IN NO EVENT SHALL RADIOMAIL CORPORATION OR CONTRIBUTORS 29254562Scy * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30254562Scy * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31254562Scy * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32254562Scy * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33254562Scy * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34254562Scy * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 35254562Scy * OF THE POSSIBILITY OF SUCH DAMAGE. 36254562Scy * 37254562Scy * This software was written for RadioMail Corporation by Ted Lemon 38254562Scy * under a contract with Vixie Enterprises. Further modifications have 39254562Scy * been made for the Internet Software Consortium under a contract 40254562Scy * with Vixie Laboratories. 41254562Scy */ 42254562Scy 43254562Scy#include <sys/cdefs.h> 44254562Scy__FBSDID("$FreeBSD: head/sbin/dhclient/errwarn.c 226345 2011-10-13 17:20:45Z des $"); 45254562Scy 46254562Scy#include <errno.h> 47254562Scy 48254562Scy#include "dhcpd.h" 49254562Scy 50254562Scystatic void do_percentm(char *obuf, size_t size, char *ibuf); 51254562Scy 52254562Scystatic char mbuf[1024]; 53254562Scystatic char fbuf[1024]; 54254562Scy 55254562Scyint warnings_occurred; 56254562Scy 57254562Scy/* 58254562Scy * Log an error message, then exit. 59254562Scy */ 60254562Scyvoid 61254562Scyerror(char *fmt, ...) 62254562Scy{ 63254562Scy va_list list; 64254562Scy 65254562Scy do_percentm(fbuf, sizeof(fbuf), fmt); 66254562Scy 67254562Scy va_start(list, fmt); 68254562Scy vsnprintf(mbuf, sizeof(mbuf), fbuf, list); 69254562Scy va_end(list); 70254562Scy 71254562Scy#ifndef DEBUG 72254562Scy syslog(log_priority | LOG_ERR, "%s", mbuf); 73254562Scy#endif 74254562Scy 75254562Scy /* Also log it to stderr? */ 76254562Scy if (log_perror) { 77254562Scy write(2, mbuf, strlen(mbuf)); 78254562Scy write(2, "\n", 1); 79254562Scy } 80254562Scy 81254562Scy syslog(LOG_CRIT, "exiting."); 82254562Scy if (log_perror) { 83254562Scy fprintf(stderr, "exiting.\n"); 84254562Scy fflush(stderr); 85254562Scy } 86254562Scy if (pidfile != NULL) 87254562Scy pidfile_remove(pidfile); 88254562Scy exit(1); 89254562Scy} 90254562Scy 91254562Scy/* 92254562Scy * Log a warning message... 93254562Scy */ 94254562Scyint 95254562Scywarning(char *fmt, ...) 96{ 97 va_list list; 98 99 do_percentm(fbuf, sizeof(fbuf), fmt); 100 101 va_start(list, fmt); 102 vsnprintf(mbuf, sizeof(mbuf), fbuf, list); 103 va_end(list); 104 105#ifndef DEBUG 106 syslog(log_priority | LOG_ERR, "%s", mbuf); 107#endif 108 109 if (log_perror) { 110 write(2, mbuf, strlen(mbuf)); 111 write(2, "\n", 1); 112 } 113 114 return (0); 115} 116 117/* 118 * Log a note... 119 */ 120int 121note(char *fmt, ...) 122{ 123 va_list list; 124 125 do_percentm(fbuf, sizeof(fbuf), fmt); 126 127 va_start(list, fmt); 128 vsnprintf(mbuf, sizeof(mbuf), fbuf, list); 129 va_end(list); 130 131#ifndef DEBUG 132 syslog(log_priority | LOG_INFO, "%s", mbuf); 133#endif 134 135 if (log_perror) { 136 write(2, mbuf, strlen(mbuf)); 137 write(2, "\n", 1); 138 } 139 140 return (0); 141} 142 143/* 144 * Log a debug message... 145 */ 146int 147debug(char *fmt, ...) 148{ 149 va_list list; 150 151 do_percentm(fbuf, sizeof(fbuf), fmt); 152 153 va_start(list, fmt); 154 vsnprintf(mbuf, sizeof(mbuf), fbuf, list); 155 va_end(list); 156 157#ifndef DEBUG 158 syslog(log_priority | LOG_DEBUG, "%s", mbuf); 159#endif 160 161 if (log_perror) { 162 write(2, mbuf, strlen(mbuf)); 163 write(2, "\n", 1); 164 } 165 166 return (0); 167} 168 169/* 170 * Find %m in the input string and substitute an error message string. 171 */ 172static void 173do_percentm(char *obuf, size_t size, char *ibuf) 174{ 175 char ch; 176 char *s = ibuf; 177 char *t = obuf; 178 size_t prlen; 179 size_t fmt_left; 180 int saved_errno = errno; 181 182 /* 183 * We wouldn't need this mess if printf handled %m, or if 184 * strerror() had been invented before syslog(). 185 */ 186 for (fmt_left = size; (ch = *s); ++s) { 187 if (ch == '%' && s[1] == 'm') { 188 ++s; 189 prlen = snprintf(t, fmt_left, "%s", 190 strerror(saved_errno)); 191 if (prlen >= fmt_left) 192 prlen = fmt_left - 1; 193 t += prlen; 194 fmt_left -= prlen; 195 } else { 196 if (fmt_left > 1) { 197 *t++ = ch; 198 fmt_left--; 199 } 200 } 201 } 202 *t = '\0'; 203} 204 205int 206parse_warn(char *fmt, ...) 207{ 208 va_list list; 209 static char spaces[] = 210 " " 211 " "; /* 80 spaces */ 212 213 do_percentm(mbuf, sizeof(mbuf), fmt); 214 snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf); 215 va_start(list, fmt); 216 vsnprintf(mbuf, sizeof(mbuf), fbuf, list); 217 va_end(list); 218 219#ifndef DEBUG 220 syslog(log_priority | LOG_ERR, "%s", mbuf); 221 syslog(log_priority | LOG_ERR, "%s", token_line); 222 if (lexline < 81) 223 syslog(log_priority | LOG_ERR, 224 "%s^", &spaces[sizeof(spaces) - lexchar]); 225#endif 226 227 if (log_perror) { 228 write(2, mbuf, strlen(mbuf)); 229 write(2, "\n", 1); 230 write(2, token_line, strlen(token_line)); 231 write(2, "\n", 1); 232 write(2, spaces, lexchar - 1); 233 write(2, "^\n", 2); 234 } 235 236 warnings_occurred = 1; 237 238 return (0); 239} 240