xref: /onnv-gate/usr/src/lib/libdhcputil/common/dhcpmsg.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdarg.h>
30*0Sstevel@tonic-gate #include <errno.h>
31*0Sstevel@tonic-gate #include <stdio.h>
32*0Sstevel@tonic-gate #include <syslog.h>
33*0Sstevel@tonic-gate #include <libintl.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <limits.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "dhcpmsg.h"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate static boolean_t	is_daemon  = B_FALSE;
40*0Sstevel@tonic-gate static boolean_t	is_verbose = B_FALSE;
41*0Sstevel@tonic-gate static char		program[PATH_MAX] = "<unknown>";
42*0Sstevel@tonic-gate static int		debug_level;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static const char	*err_to_string(int);
45*0Sstevel@tonic-gate static int		err_to_syslog(int);
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate  * dhcpmsg(): logs a message to the console or to syslog
49*0Sstevel@tonic-gate  *
50*0Sstevel@tonic-gate  *   input: int: the level to log the message at
51*0Sstevel@tonic-gate  *	    const char *: a printf-like format string
52*0Sstevel@tonic-gate  *	    ...: arguments to the format string
53*0Sstevel@tonic-gate  *  output: void
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*PRINTFLIKE2*/
57*0Sstevel@tonic-gate void
58*0Sstevel@tonic-gate dhcpmsg(int errlevel, const char *fmt, ...)
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	va_list		ap;
61*0Sstevel@tonic-gate 	char		buf[512];
62*0Sstevel@tonic-gate 	char		*errmsg;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	if ((errlevel == MSG_DEBUG2 && (debug_level < 2)) ||
65*0Sstevel@tonic-gate 	    (errlevel == MSG_DEBUG && (debug_level < 1)) ||
66*0Sstevel@tonic-gate 	    (errlevel == MSG_VERBOSE && !is_verbose))
67*0Sstevel@tonic-gate 		return;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	va_start(ap, fmt);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	/*
72*0Sstevel@tonic-gate 	 * either log to stderr, or log to syslog.  print out unix
73*0Sstevel@tonic-gate 	 * error message if errlevel is MSG_ERR and errno is set
74*0Sstevel@tonic-gate 	 */
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	if (is_daemon) {
77*0Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), (errlevel == MSG_ERR &&
78*0Sstevel@tonic-gate 		    errno != 0) ? "%s: %%m\n" : "%s\n", gettext(fmt));
79*0Sstevel@tonic-gate 		(void) vsyslog(err_to_syslog(errlevel), buf, ap);
80*0Sstevel@tonic-gate 	} else {
81*0Sstevel@tonic-gate 		errmsg = strerror(errno);
82*0Sstevel@tonic-gate 		if (errmsg == NULL)
83*0Sstevel@tonic-gate 			errmsg = dgettext(TEXT_DOMAIN, "<unknown error>");
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), (errlevel == MSG_ERR &&
86*0Sstevel@tonic-gate 		    errno != 0) ? "%s: %s: %s: %s\n" : "%s: %s: %s\n", program,
87*0Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, err_to_string(errlevel)),
88*0Sstevel@tonic-gate 		    gettext(fmt), errmsg);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 		(void) vfprintf(stderr, buf, ap);
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	va_end(ap);
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate  * dhcpmsg_init(): opens and initializes the DHCP messaging facility
98*0Sstevel@tonic-gate  *
99*0Sstevel@tonic-gate  *   input: const char *: the name of the executable
100*0Sstevel@tonic-gate  *	    boolean_t: whether the executable is a daemon
101*0Sstevel@tonic-gate  *	    boolean_t: whether the executable is running "verbosely"
102*0Sstevel@tonic-gate  *	    int: the debugging level the executable is being run at
103*0Sstevel@tonic-gate  *  output: void
104*0Sstevel@tonic-gate  */
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate void
107*0Sstevel@tonic-gate dhcpmsg_init(const char *program_name, boolean_t daemon, boolean_t verbose,
108*0Sstevel@tonic-gate     int level)
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate 	(void) strlcpy(program, program_name, sizeof (program));
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	debug_level = level;
113*0Sstevel@tonic-gate 	is_verbose = verbose;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	if (daemon) {
116*0Sstevel@tonic-gate 		is_daemon = B_TRUE;
117*0Sstevel@tonic-gate 		(void) openlog(program, LOG_PID, LOG_DAEMON);
118*0Sstevel@tonic-gate 		if (is_verbose) {
119*0Sstevel@tonic-gate 			syslog(err_to_syslog(MSG_VERBOSE), "%s",
120*0Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN, "Daemon started"));
121*0Sstevel@tonic-gate 		}
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate  * dhcpmsg_fini(): closes the DHCP messaging facility.
127*0Sstevel@tonic-gate  *
128*0Sstevel@tonic-gate  *   input: void
129*0Sstevel@tonic-gate  *  output: void
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate void
133*0Sstevel@tonic-gate dhcpmsg_fini(void)
134*0Sstevel@tonic-gate {
135*0Sstevel@tonic-gate 	if (is_daemon) {
136*0Sstevel@tonic-gate 		if (is_verbose) {
137*0Sstevel@tonic-gate 			syslog(err_to_syslog(MSG_VERBOSE), "%s",
138*0Sstevel@tonic-gate 			    dgettext(TEXT_DOMAIN, "Daemon terminated"));
139*0Sstevel@tonic-gate 		}
140*0Sstevel@tonic-gate 		closelog();
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate /*
145*0Sstevel@tonic-gate  * err_to_syslog(): converts a dhcpmsg log level into a syslog log level
146*0Sstevel@tonic-gate  *
147*0Sstevel@tonic-gate  *   input: int: the dhcpmsg log level
148*0Sstevel@tonic-gate  *  output: int: the syslog log level
149*0Sstevel@tonic-gate  */
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate static int
152*0Sstevel@tonic-gate err_to_syslog(int errlevel)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	switch (errlevel) {
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	case MSG_DEBUG:
157*0Sstevel@tonic-gate 	case MSG_DEBUG2:
158*0Sstevel@tonic-gate 		return (LOG_DEBUG);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	case MSG_ERROR:
161*0Sstevel@tonic-gate 	case MSG_ERR:
162*0Sstevel@tonic-gate 		return (LOG_ERR);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	case MSG_WARNING:
165*0Sstevel@tonic-gate 		return (LOG_WARNING);
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	case MSG_NOTICE:
168*0Sstevel@tonic-gate 		return (LOG_NOTICE);
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	case MSG_CRIT:
171*0Sstevel@tonic-gate 		return (LOG_CRIT);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	case MSG_VERBOSE:
174*0Sstevel@tonic-gate 	case MSG_INFO:
175*0Sstevel@tonic-gate 		return (LOG_INFO);
176*0Sstevel@tonic-gate 	}
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	return (LOG_INFO);
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate /*
182*0Sstevel@tonic-gate  * err_to_string(): converts a log level into a string
183*0Sstevel@tonic-gate  *
184*0Sstevel@tonic-gate  *   input: int: the log level
185*0Sstevel@tonic-gate  *  output: const char *: the stringified log level
186*0Sstevel@tonic-gate  */
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate static const char *
189*0Sstevel@tonic-gate err_to_string(int errlevel)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate 	switch (errlevel) {
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	case MSG_DEBUG:
194*0Sstevel@tonic-gate 	case MSG_DEBUG2:
195*0Sstevel@tonic-gate 		return ("debug");
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	case MSG_ERR:
198*0Sstevel@tonic-gate 	case MSG_ERROR:
199*0Sstevel@tonic-gate 		return ("error");
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	case MSG_WARNING:
202*0Sstevel@tonic-gate 		return ("warning");
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	case MSG_NOTICE:
205*0Sstevel@tonic-gate 		return ("notice");
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	case MSG_CRIT:
208*0Sstevel@tonic-gate 		return ("CRITICAL");
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	case MSG_VERBOSE:
211*0Sstevel@tonic-gate 	case MSG_INFO:
212*0Sstevel@tonic-gate 		return ("info");
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	return ("<unknown>");
216*0Sstevel@tonic-gate }
217