17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*d04ccbb3Scarlsonj * Common Development and Distribution License (the "License").
6*d04ccbb3Scarlsonj * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*d04ccbb3Scarlsonj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdarg.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <syslog.h>
327c478bd9Sstevel@tonic-gate #include <libintl.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include "dhcpmsg.h"
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate static boolean_t is_daemon = B_FALSE;
397c478bd9Sstevel@tonic-gate static boolean_t is_verbose = B_FALSE;
407c478bd9Sstevel@tonic-gate static char program[PATH_MAX] = "<unknown>";
417c478bd9Sstevel@tonic-gate static int debug_level;
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate static const char *err_to_string(int);
447c478bd9Sstevel@tonic-gate static int err_to_syslog(int);
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * dhcpmsg(): logs a message to the console or to syslog
487c478bd9Sstevel@tonic-gate *
497c478bd9Sstevel@tonic-gate * input: int: the level to log the message at
507c478bd9Sstevel@tonic-gate * const char *: a printf-like format string
517c478bd9Sstevel@tonic-gate * ...: arguments to the format string
527c478bd9Sstevel@tonic-gate * output: void
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate void
dhcpmsg(int errlevel,const char * fmt,...)567c478bd9Sstevel@tonic-gate dhcpmsg(int errlevel, const char *fmt, ...)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate va_list ap;
597c478bd9Sstevel@tonic-gate char buf[512];
607c478bd9Sstevel@tonic-gate char *errmsg;
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate if ((errlevel == MSG_DEBUG2 && (debug_level < 2)) ||
637c478bd9Sstevel@tonic-gate (errlevel == MSG_DEBUG && (debug_level < 1)) ||
647c478bd9Sstevel@tonic-gate (errlevel == MSG_VERBOSE && !is_verbose))
657c478bd9Sstevel@tonic-gate return;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate va_start(ap, fmt);
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate * either log to stderr, or log to syslog. print out unix
717c478bd9Sstevel@tonic-gate * error message if errlevel is MSG_ERR and errno is set
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate if (is_daemon) {
757c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), (errlevel == MSG_ERR &&
767c478bd9Sstevel@tonic-gate errno != 0) ? "%s: %%m\n" : "%s\n", gettext(fmt));
777c478bd9Sstevel@tonic-gate (void) vsyslog(err_to_syslog(errlevel), buf, ap);
787c478bd9Sstevel@tonic-gate } else {
797c478bd9Sstevel@tonic-gate errmsg = strerror(errno);
807c478bd9Sstevel@tonic-gate if (errmsg == NULL)
817c478bd9Sstevel@tonic-gate errmsg = dgettext(TEXT_DOMAIN, "<unknown error>");
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), (errlevel == MSG_ERR &&
847c478bd9Sstevel@tonic-gate errno != 0) ? "%s: %s: %s: %s\n" : "%s: %s: %s\n", program,
857c478bd9Sstevel@tonic-gate dgettext(TEXT_DOMAIN, err_to_string(errlevel)),
867c478bd9Sstevel@tonic-gate gettext(fmt), errmsg);
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, buf, ap);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate va_end(ap);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate * dhcpmsg_init(): opens and initializes the DHCP messaging facility
967c478bd9Sstevel@tonic-gate *
977c478bd9Sstevel@tonic-gate * input: const char *: the name of the executable
987c478bd9Sstevel@tonic-gate * boolean_t: whether the executable is a daemon
997c478bd9Sstevel@tonic-gate * boolean_t: whether the executable is running "verbosely"
1007c478bd9Sstevel@tonic-gate * int: the debugging level the executable is being run at
1017c478bd9Sstevel@tonic-gate * output: void
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate void
dhcpmsg_init(const char * program_name,boolean_t daemon,boolean_t verbose,int level)1057c478bd9Sstevel@tonic-gate dhcpmsg_init(const char *program_name, boolean_t daemon, boolean_t verbose,
1067c478bd9Sstevel@tonic-gate int level)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate (void) strlcpy(program, program_name, sizeof (program));
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate debug_level = level;
1117c478bd9Sstevel@tonic-gate is_verbose = verbose;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate if (daemon) {
1147c478bd9Sstevel@tonic-gate is_daemon = B_TRUE;
1157c478bd9Sstevel@tonic-gate (void) openlog(program, LOG_PID, LOG_DAEMON);
1167c478bd9Sstevel@tonic-gate if (is_verbose) {
1177c478bd9Sstevel@tonic-gate syslog(err_to_syslog(MSG_VERBOSE), "%s",
1187c478bd9Sstevel@tonic-gate dgettext(TEXT_DOMAIN, "Daemon started"));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * dhcpmsg_fini(): closes the DHCP messaging facility.
1257c478bd9Sstevel@tonic-gate *
1267c478bd9Sstevel@tonic-gate * input: void
1277c478bd9Sstevel@tonic-gate * output: void
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate void
dhcpmsg_fini(void)1317c478bd9Sstevel@tonic-gate dhcpmsg_fini(void)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate if (is_daemon) {
1347c478bd9Sstevel@tonic-gate if (is_verbose) {
1357c478bd9Sstevel@tonic-gate syslog(err_to_syslog(MSG_VERBOSE), "%s",
1367c478bd9Sstevel@tonic-gate dgettext(TEXT_DOMAIN, "Daemon terminated"));
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate closelog();
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate * err_to_syslog(): converts a dhcpmsg log level into a syslog log level
1447c478bd9Sstevel@tonic-gate *
1457c478bd9Sstevel@tonic-gate * input: int: the dhcpmsg log level
1467c478bd9Sstevel@tonic-gate * output: int: the syslog log level
1477c478bd9Sstevel@tonic-gate */
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate static int
err_to_syslog(int errlevel)1507c478bd9Sstevel@tonic-gate err_to_syslog(int errlevel)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate switch (errlevel) {
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate case MSG_DEBUG:
1557c478bd9Sstevel@tonic-gate case MSG_DEBUG2:
1567c478bd9Sstevel@tonic-gate return (LOG_DEBUG);
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate case MSG_ERROR:
1597c478bd9Sstevel@tonic-gate case MSG_ERR:
1607c478bd9Sstevel@tonic-gate return (LOG_ERR);
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate case MSG_WARNING:
1637c478bd9Sstevel@tonic-gate return (LOG_WARNING);
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate case MSG_NOTICE:
1667c478bd9Sstevel@tonic-gate return (LOG_NOTICE);
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate case MSG_CRIT:
1697c478bd9Sstevel@tonic-gate return (LOG_CRIT);
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate case MSG_VERBOSE:
1727c478bd9Sstevel@tonic-gate case MSG_INFO:
1737c478bd9Sstevel@tonic-gate return (LOG_INFO);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate return (LOG_INFO);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate * err_to_string(): converts a log level into a string
1817c478bd9Sstevel@tonic-gate *
1827c478bd9Sstevel@tonic-gate * input: int: the log level
1837c478bd9Sstevel@tonic-gate * output: const char *: the stringified log level
1847c478bd9Sstevel@tonic-gate */
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate static const char *
err_to_string(int errlevel)1877c478bd9Sstevel@tonic-gate err_to_string(int errlevel)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate switch (errlevel) {
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate case MSG_DEBUG:
1927c478bd9Sstevel@tonic-gate case MSG_DEBUG2:
1937c478bd9Sstevel@tonic-gate return ("debug");
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate case MSG_ERR:
1967c478bd9Sstevel@tonic-gate case MSG_ERROR:
1977c478bd9Sstevel@tonic-gate return ("error");
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate case MSG_WARNING:
2007c478bd9Sstevel@tonic-gate return ("warning");
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate case MSG_NOTICE:
2037c478bd9Sstevel@tonic-gate return ("notice");
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate case MSG_CRIT:
2067c478bd9Sstevel@tonic-gate return ("CRITICAL");
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate case MSG_VERBOSE:
2097c478bd9Sstevel@tonic-gate case MSG_INFO:
2107c478bd9Sstevel@tonic-gate return ("info");
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate return ("<unknown>");
2147c478bd9Sstevel@tonic-gate }
215