errwarn.c revision 147072
191094Sdes/*	$OpenBSD: errwarn.c,v 1.7 2004/05/04 22:23:01 mickey Exp $	*/
291094Sdes
391094Sdes/* Errors and warnings... */
491094Sdes
591094Sdes/*
691094Sdes * Copyright (c) 1996 The Internet Software Consortium.
791094Sdes * All Rights Reserved.
891094Sdes * Copyright (c) 1995 RadioMail Corporation.  All rights reserved.
991094Sdes *
1091094Sdes * Redistribution and use in source and binary forms, with or without
1191094Sdes * modification, are permitted provided that the following conditions
1291094Sdes * are met:
1391094Sdes *
1491094Sdes * 1. Redistributions of source code must retain the above copyright
1591094Sdes *    notice, this list of conditions and the following disclaimer.
1691094Sdes * 2. Redistributions in binary form must reproduce the above copyright
1791094Sdes *    notice, this list of conditions and the following disclaimer in the
1891094Sdes *    documentation and/or other materials provided with the distribution.
1991094Sdes * 3. Neither the name of RadioMail Corporation, the Internet Software
2091094Sdes *    Consortium nor the names of its contributors may be used to endorse
2191094Sdes *    or promote products derived from this software without specific
2291094Sdes *    prior written permission.
2391094Sdes *
2491094Sdes * THIS SOFTWARE IS PROVIDED BY RADIOMAIL CORPORATION, THE INTERNET
2591094Sdes * SOFTWARE CONSORTIUM AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
2691094Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2791094Sdes * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2891094Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL RADIOMAIL CORPORATION OR CONTRIBUTORS
2991094Sdes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3091094Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3191094Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3291094Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3391094Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3491094Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3591094Sdes * OF THE POSSIBILITY OF SUCH DAMAGE.
3691094Sdes *
3791094Sdes * This software was written for RadioMail Corporation by Ted Lemon
3891094Sdes * under a contract with Vixie Enterprises.   Further modifications have
3991094Sdes * been made for the Internet Software Consortium under a contract
4091094Sdes * with Vixie Laboratories.
4191094Sdes */
4291094Sdes
4391094Sdes#include <errno.h>
4491094Sdes
4591094Sdes#include "dhcpd.h"
4691094Sdes
4791094Sdesstatic void do_percentm(char *obuf, size_t size, char *ibuf);
4891094Sdes
4991094Sdesstatic char mbuf[1024];
5091094Sdesstatic char fbuf[1024];
5191094Sdes
5291094Sdesint warnings_occurred;
5391094Sdes
5491094Sdes/*
5591094Sdes * Log an error message, then exit.
5691094Sdes */
5791094Sdesvoid
5891094Sdeserror(char *fmt, ...)
5991094Sdes{
6091094Sdes	va_list list;
6191094Sdes
6291094Sdes	do_percentm(fbuf, sizeof(fbuf), fmt);
6391094Sdes
6491094Sdes	va_start(list, fmt);
6591094Sdes	vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
6691094Sdes	va_end(list);
6791094Sdes
6891094Sdes#ifndef DEBUG
6991094Sdes	syslog(log_priority | LOG_ERR, "%s", mbuf);
7091094Sdes#endif
7191094Sdes
7291094Sdes	/* Also log it to stderr? */
7391094Sdes	if (log_perror) {
7491094Sdes		write(2, mbuf, strlen(mbuf));
7591094Sdes		write(2, "\n", 1);
7691094Sdes	}
7791094Sdes
7891094Sdes	syslog(LOG_CRIT, "exiting.");
7991094Sdes	if (log_perror) {
8091094Sdes		fprintf(stderr, "exiting.\n");
8191094Sdes		fflush(stderr);
8291094Sdes	}
8391094Sdes	exit(1);
8491094Sdes}
8591094Sdes
8691094Sdes/*
8791094Sdes * Log a warning message...
8891094Sdes */
8991094Sdesint
9091094Sdeswarning(char *fmt, ...)
9191094Sdes{
9291094Sdes	va_list list;
9391094Sdes
9491094Sdes	do_percentm(fbuf, sizeof(fbuf), fmt);
9591094Sdes
9691094Sdes	va_start(list, fmt);
9791094Sdes	vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
9891094Sdes	va_end(list);
9991094Sdes
10091094Sdes#ifndef DEBUG
10191094Sdes	syslog(log_priority | LOG_ERR, "%s", mbuf);
10291094Sdes#endif
10391094Sdes
10491094Sdes	if (log_perror) {
10591094Sdes		write(2, mbuf, strlen(mbuf));
10691094Sdes		write(2, "\n", 1);
10791094Sdes	}
10891094Sdes
10991094Sdes	return (0);
11091094Sdes}
11191094Sdes
11291094Sdes/*
11391094Sdes * Log a note...
11491094Sdes */
11591094Sdesint
11691094Sdesnote(char *fmt, ...)
11791094Sdes{
11891094Sdes	va_list list;
11991094Sdes
12091094Sdes	do_percentm(fbuf, sizeof(fbuf), fmt);
12191094Sdes
12291094Sdes	va_start(list, fmt);
12391094Sdes	vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
12491094Sdes	va_end(list);
12591094Sdes
12691094Sdes#ifndef DEBUG
12791094Sdes	syslog(log_priority | LOG_INFO, "%s", mbuf);
12891094Sdes#endif
12991094Sdes
13091094Sdes	if (log_perror) {
13191094Sdes		write(2, mbuf, strlen(mbuf));
13291094Sdes		write(2, "\n", 1);
13391094Sdes	}
13491094Sdes
13591094Sdes	return (0);
13691094Sdes}
13791094Sdes
13891094Sdes/*
13991094Sdes * Log a debug message...
14091094Sdes */
14191094Sdesint
14291094Sdesdebug(char *fmt, ...)
14391094Sdes{
14491094Sdes	va_list list;
14591094Sdes
14691094Sdes	do_percentm(fbuf, sizeof(fbuf), fmt);
14791094Sdes
14891094Sdes	va_start(list, fmt);
14991094Sdes	vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
15091094Sdes	va_end(list);
15191094Sdes
15291094Sdes#ifndef DEBUG
15391094Sdes	syslog(log_priority | LOG_DEBUG, "%s", mbuf);
15491094Sdes#endif
15591094Sdes
15691094Sdes	if (log_perror) {
15791094Sdes		write(2, mbuf, strlen(mbuf));
15891094Sdes		write(2, "\n", 1);
15991094Sdes	}
16091094Sdes
16191094Sdes	return (0);
16291094Sdes}
16391094Sdes
16491094Sdes/*
16591094Sdes * Find %m in the input string and substitute an error message string.
16691094Sdes */
16791094Sdesstatic void
16891094Sdesdo_percentm(char *obuf, size_t size, char *ibuf)
16991094Sdes{
17091094Sdes	char ch;
17191094Sdes	char *s = ibuf;
17291094Sdes	char *t = obuf;
17391094Sdes	size_t prlen;
17491094Sdes	size_t fmt_left;
17591094Sdes	int saved_errno = errno;
17691094Sdes
17791094Sdes	/*
17891094Sdes	 * We wouldn't need this mess if printf handled %m, or if
17991094Sdes	 * strerror() had been invented before syslog().
18091094Sdes	 */
18191094Sdes	for (fmt_left = size; (ch = *s); ++s) {
18291094Sdes		if (ch == '%' && s[1] == 'm') {
18391094Sdes			++s;
18491094Sdes			prlen = snprintf(t, fmt_left, "%s",
18591094Sdes			    strerror(saved_errno));
18691094Sdes			if (prlen >= fmt_left)
18791094Sdes				prlen = fmt_left - 1;
18891094Sdes			t += prlen;
18991094Sdes			fmt_left -= prlen;
19091094Sdes		} else {
19191094Sdes			if (fmt_left > 1) {
19291094Sdes				*t++ = ch;
19391094Sdes				fmt_left--;
19491094Sdes			}
19591094Sdes		}
19691094Sdes	}
19791094Sdes	*t = '\0';
19891094Sdes}
19991094Sdes
20091094Sdesint
20191094Sdesparse_warn(char *fmt, ...)
20291094Sdes{
20391094Sdes	va_list list;
20491094Sdes	static char spaces[] =
20591094Sdes	    "                                        "
20691094Sdes	    "                                        "; /* 80 spaces */
20791094Sdes
20891094Sdes	do_percentm(mbuf, sizeof(mbuf), fmt);
20991094Sdes	snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf);
21091094Sdes	va_start(list, fmt);
21191094Sdes	vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
21291094Sdes	va_end(list);
21391094Sdes
21491094Sdes#ifndef DEBUG
21591094Sdes	syslog(log_priority | LOG_ERR, "%s", mbuf);
21691094Sdes	syslog(log_priority | LOG_ERR, "%s", token_line);
21791094Sdes	if (lexline < 81)
21891094Sdes		syslog(log_priority | LOG_ERR,
21991094Sdes		    "%s^", &spaces[sizeof(spaces) - lexchar]);
22091094Sdes#endif
22191094Sdes
22291094Sdes	if (log_perror) {
22391094Sdes		write(2, mbuf, strlen(mbuf));
22491094Sdes		write(2, "\n", 1);
22591094Sdes		write(2, token_line, strlen(token_line));
22691094Sdes		write(2, "\n", 1);
22791094Sdes		write(2, spaces, lexchar - 1);
22891094Sdes		write(2, "^\n", 2);
22991094Sdes	}
23091094Sdes
23191094Sdes	warnings_occurred = 1;
23291094Sdes
23391094Sdes	return (0);
23491094Sdes}
23591094Sdes