10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3431Scarlsonj * Common Development and Distribution License (the "License").
6*3431Scarlsonj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*3431Scarlsonj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <stdarg.h>
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <syslog.h>
320Sstevel@tonic-gate #include <libintl.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <limits.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include "dhcpmsg.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate static boolean_t is_daemon = B_FALSE;
390Sstevel@tonic-gate static boolean_t is_verbose = B_FALSE;
400Sstevel@tonic-gate static char program[PATH_MAX] = "<unknown>";
410Sstevel@tonic-gate static int debug_level;
420Sstevel@tonic-gate
430Sstevel@tonic-gate static const char *err_to_string(int);
440Sstevel@tonic-gate static int err_to_syslog(int);
450Sstevel@tonic-gate
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate * dhcpmsg(): logs a message to the console or to syslog
480Sstevel@tonic-gate *
490Sstevel@tonic-gate * input: int: the level to log the message at
500Sstevel@tonic-gate * const char *: a printf-like format string
510Sstevel@tonic-gate * ...: arguments to the format string
520Sstevel@tonic-gate * output: void
530Sstevel@tonic-gate */
540Sstevel@tonic-gate
550Sstevel@tonic-gate void
dhcpmsg(int errlevel,const char * fmt,...)560Sstevel@tonic-gate dhcpmsg(int errlevel, const char *fmt, ...)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate va_list ap;
590Sstevel@tonic-gate char buf[512];
600Sstevel@tonic-gate char *errmsg;
610Sstevel@tonic-gate
620Sstevel@tonic-gate if ((errlevel == MSG_DEBUG2 && (debug_level < 2)) ||
630Sstevel@tonic-gate (errlevel == MSG_DEBUG && (debug_level < 1)) ||
640Sstevel@tonic-gate (errlevel == MSG_VERBOSE && !is_verbose))
650Sstevel@tonic-gate return;
660Sstevel@tonic-gate
670Sstevel@tonic-gate va_start(ap, fmt);
680Sstevel@tonic-gate
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate * either log to stderr, or log to syslog. print out unix
710Sstevel@tonic-gate * error message if errlevel is MSG_ERR and errno is set
720Sstevel@tonic-gate */
730Sstevel@tonic-gate
740Sstevel@tonic-gate if (is_daemon) {
750Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), (errlevel == MSG_ERR &&
760Sstevel@tonic-gate errno != 0) ? "%s: %%m\n" : "%s\n", gettext(fmt));
770Sstevel@tonic-gate (void) vsyslog(err_to_syslog(errlevel), buf, ap);
780Sstevel@tonic-gate } else {
790Sstevel@tonic-gate errmsg = strerror(errno);
800Sstevel@tonic-gate if (errmsg == NULL)
810Sstevel@tonic-gate errmsg = dgettext(TEXT_DOMAIN, "<unknown error>");
820Sstevel@tonic-gate
830Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), (errlevel == MSG_ERR &&
840Sstevel@tonic-gate errno != 0) ? "%s: %s: %s: %s\n" : "%s: %s: %s\n", program,
850Sstevel@tonic-gate dgettext(TEXT_DOMAIN, err_to_string(errlevel)),
860Sstevel@tonic-gate gettext(fmt), errmsg);
870Sstevel@tonic-gate
880Sstevel@tonic-gate (void) vfprintf(stderr, buf, ap);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate va_end(ap);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate * dhcpmsg_init(): opens and initializes the DHCP messaging facility
960Sstevel@tonic-gate *
970Sstevel@tonic-gate * input: const char *: the name of the executable
980Sstevel@tonic-gate * boolean_t: whether the executable is a daemon
990Sstevel@tonic-gate * boolean_t: whether the executable is running "verbosely"
1000Sstevel@tonic-gate * int: the debugging level the executable is being run at
1010Sstevel@tonic-gate * output: void
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate void
dhcpmsg_init(const char * program_name,boolean_t daemon,boolean_t verbose,int level)1050Sstevel@tonic-gate dhcpmsg_init(const char *program_name, boolean_t daemon, boolean_t verbose,
1060Sstevel@tonic-gate int level)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate (void) strlcpy(program, program_name, sizeof (program));
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate debug_level = level;
1110Sstevel@tonic-gate is_verbose = verbose;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if (daemon) {
1140Sstevel@tonic-gate is_daemon = B_TRUE;
1150Sstevel@tonic-gate (void) openlog(program, LOG_PID, LOG_DAEMON);
1160Sstevel@tonic-gate if (is_verbose) {
1170Sstevel@tonic-gate syslog(err_to_syslog(MSG_VERBOSE), "%s",
1180Sstevel@tonic-gate dgettext(TEXT_DOMAIN, "Daemon started"));
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate * dhcpmsg_fini(): closes the DHCP messaging facility.
1250Sstevel@tonic-gate *
1260Sstevel@tonic-gate * input: void
1270Sstevel@tonic-gate * output: void
1280Sstevel@tonic-gate */
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate void
dhcpmsg_fini(void)1310Sstevel@tonic-gate dhcpmsg_fini(void)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate if (is_daemon) {
1340Sstevel@tonic-gate if (is_verbose) {
1350Sstevel@tonic-gate syslog(err_to_syslog(MSG_VERBOSE), "%s",
1360Sstevel@tonic-gate dgettext(TEXT_DOMAIN, "Daemon terminated"));
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate closelog();
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate * err_to_syslog(): converts a dhcpmsg log level into a syslog log level
1440Sstevel@tonic-gate *
1450Sstevel@tonic-gate * input: int: the dhcpmsg log level
1460Sstevel@tonic-gate * output: int: the syslog log level
1470Sstevel@tonic-gate */
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate static int
err_to_syslog(int errlevel)1500Sstevel@tonic-gate err_to_syslog(int errlevel)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate switch (errlevel) {
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate case MSG_DEBUG:
1550Sstevel@tonic-gate case MSG_DEBUG2:
1560Sstevel@tonic-gate return (LOG_DEBUG);
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate case MSG_ERROR:
1590Sstevel@tonic-gate case MSG_ERR:
1600Sstevel@tonic-gate return (LOG_ERR);
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate case MSG_WARNING:
1630Sstevel@tonic-gate return (LOG_WARNING);
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate case MSG_NOTICE:
1660Sstevel@tonic-gate return (LOG_NOTICE);
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate case MSG_CRIT:
1690Sstevel@tonic-gate return (LOG_CRIT);
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate case MSG_VERBOSE:
1720Sstevel@tonic-gate case MSG_INFO:
1730Sstevel@tonic-gate return (LOG_INFO);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate return (LOG_INFO);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate * err_to_string(): converts a log level into a string
1810Sstevel@tonic-gate *
1820Sstevel@tonic-gate * input: int: the log level
1830Sstevel@tonic-gate * output: const char *: the stringified log level
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate static const char *
err_to_string(int errlevel)1870Sstevel@tonic-gate err_to_string(int errlevel)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate switch (errlevel) {
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate case MSG_DEBUG:
1920Sstevel@tonic-gate case MSG_DEBUG2:
1930Sstevel@tonic-gate return ("debug");
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate case MSG_ERR:
1960Sstevel@tonic-gate case MSG_ERROR:
1970Sstevel@tonic-gate return ("error");
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate case MSG_WARNING:
2000Sstevel@tonic-gate return ("warning");
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate case MSG_NOTICE:
2030Sstevel@tonic-gate return ("notice");
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate case MSG_CRIT:
2060Sstevel@tonic-gate return ("CRITICAL");
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate case MSG_VERBOSE:
2090Sstevel@tonic-gate case MSG_INFO:
2100Sstevel@tonic-gate return ("info");
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate return ("<unknown>");
2140Sstevel@tonic-gate }
215