1 /*	$OpenBSD: errwarn.c,v 1.4 2005/04/19 16:13:44 moritz Exp $	*/
2 
3 /* Errors and warnings... */
4 
5 /*
6  * Copyright (c) 1996 The Internet Software Consortium.
7  * All Rights Reserved.
8  * Copyright (c) 1995 RadioMail Corporation.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of RadioMail Corporation, the Internet Software
20  *    Consortium nor the names of its contributors may be used to endorse
21  *    or promote products derived from this software without specific
22  *    prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY RADIOMAIL CORPORATION, THE INTERNET
25  * SOFTWARE CONSORTIUM AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL RADIOMAIL CORPORATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35  * OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * This software was written for RadioMail Corporation by Ted Lemon
38  * under a contract with Vixie Enterprises.   Further modifications have
39  * been made for the Internet Software Consortium under a contract
40  * with Vixie Laboratories.
41  */
42 
43 #include <errno.h>
44 
45 #include "dhcpd.h"
46 
47 static void do_percentm(char *obuf, size_t size, char *ibuf);
48 
49 static char mbuf[1024];
50 static char fbuf[1024];
51 
52 /*
53  * Log an error message, then exit.
54  */
55 void
error(char * fmt,...)56 error(char *fmt, ...)
57 {
58 	va_list list;
59 
60 	do_percentm(fbuf, sizeof(fbuf), fmt);
61 
62 	va_start(list, fmt);
63 	vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
64 	va_end(list);
65 
66 #ifndef DEBUG
67 	syslog(log_priority | LOG_ERR, "%s", mbuf);
68 #endif
69 
70 	/* Also log it to stderr? */
71 	if (log_perror) {
72 		write(STDERR_FILENO, mbuf, strlen(mbuf));
73 		write(STDERR_FILENO, "\n", 1);
74 	}
75 
76 	syslog(LOG_CRIT, "exiting.");
77 	if (log_perror) {
78 		fprintf(stderr, "exiting.\n");
79 		fflush(stderr);
80 	}
81 	exit(1);
82 }
83 
84 /*
85  * Log a warning message...
86  */
87 int
warn(char * fmt,...)88 warn(char *fmt, ...)
89 {
90 	va_list list;
91 
92 	do_percentm(fbuf, sizeof(fbuf), fmt);
93 
94 	va_start(list, fmt);
95 	vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
96 	va_end(list);
97 
98 #ifndef DEBUG
99 	syslog(log_priority | LOG_ERR, "%s", mbuf);
100 #endif
101 
102 	if (log_perror) {
103 		write(STDERR_FILENO, mbuf, strlen(mbuf));
104 		write(STDERR_FILENO, "\n", 1);
105 	}
106 
107 	return (0);
108 }
109 
110 /*
111  * Log a note...
112  */
113 int
note(char * fmt,...)114 note(char *fmt, ...)
115 {
116 	va_list list;
117 
118 	do_percentm(fbuf, sizeof(fbuf), fmt);
119 
120 	va_start(list, fmt);
121 	vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
122 	va_end(list);
123 
124 #ifndef DEBUG
125 	syslog(log_priority | LOG_INFO, "%s", mbuf);
126 #endif
127 
128 	if (log_perror) {
129 		write(STDERR_FILENO, mbuf, strlen(mbuf));
130 		write(STDERR_FILENO, "\n", 1);
131 	}
132 
133 	return (0);
134 }
135 
136 /*
137  * Log a debug message...
138  */
139 int
debug(char * fmt,...)140 debug(char *fmt, ...)
141 {
142 	va_list list;
143 
144 	do_percentm(fbuf, sizeof(fbuf), fmt);
145 
146 	va_start(list, fmt);
147 	vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
148 	va_end(list);
149 
150 #ifndef DEBUG
151 	syslog(log_priority | LOG_DEBUG, "%s", mbuf);
152 #endif
153 
154 	if (log_perror) {
155 		write(STDERR_FILENO, mbuf, strlen(mbuf));
156 		write(STDERR_FILENO, "\n", 1);
157 	}
158 
159 	return (0);
160 }
161 
162 /*
163  * Find %m in the input string and substitute an error message string.
164  */
165 static void
do_percentm(char * obuf,size_t size,char * ibuf)166 do_percentm(char *obuf, size_t size, char *ibuf)
167 {
168 	char ch;
169 	char *s = ibuf;
170 	char *t = obuf;
171 	size_t prlen;
172 	size_t fmt_left;
173 	int saved_errno = errno;
174 
175 	/*
176 	 * We wouldn't need this mess if printf handled %m, or if
177 	 * strerror() had been invented before syslog().
178 	 */
179 	for (fmt_left = size; (ch = *s); ++s) {
180 		if (ch == '%' && s[1] == 'm') {
181 			++s;
182 			prlen = snprintf(t, fmt_left, "%s",
183 			    strerror(saved_errno));
184 			if (prlen == -1)
185 				prlen = 0;
186 			if (prlen >= fmt_left)
187 				prlen = fmt_left - 1;
188 			t += prlen;
189 			fmt_left -= prlen;
190 		} else {
191 			if (fmt_left > 1) {
192 				*t++ = ch;
193 				fmt_left--;
194 			}
195 		}
196 	}
197 	*t = '\0';
198 }
199 
200