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 #include <stdarg.h>
277c478bd9Sstevel@tonic-gate #include <errno.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <syslog.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <limits.h>
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include "dhcpmsg.h"
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate static boolean_t is_daemon = B_FALSE;
377c478bd9Sstevel@tonic-gate static boolean_t is_verbose = B_FALSE;
387c478bd9Sstevel@tonic-gate static char program[PATH_MAX] = "<unknown>";
397c478bd9Sstevel@tonic-gate static int debug_level;
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate static const char *err_to_string(int);
427c478bd9Sstevel@tonic-gate static int err_to_syslog(int);
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate * dhcpmsg(): logs a message to the console or to syslog
467c478bd9Sstevel@tonic-gate *
477c478bd9Sstevel@tonic-gate * input: int: the level to log the message at
487c478bd9Sstevel@tonic-gate * const char *: a printf-like format string
497c478bd9Sstevel@tonic-gate * ...: arguments to the format string
507c478bd9Sstevel@tonic-gate * output: void
517c478bd9Sstevel@tonic-gate */
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate void
dhcpmsg(int errlevel,const char * fmt,...)547c478bd9Sstevel@tonic-gate dhcpmsg(int errlevel, const char *fmt, ...)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate va_list ap;
577c478bd9Sstevel@tonic-gate char buf[512];
587c478bd9Sstevel@tonic-gate char *errmsg;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate if ((errlevel == MSG_DEBUG2 && (debug_level < 2)) ||
617c478bd9Sstevel@tonic-gate (errlevel == MSG_DEBUG && (debug_level < 1)) ||
627c478bd9Sstevel@tonic-gate (errlevel == MSG_VERBOSE && !is_verbose))
637c478bd9Sstevel@tonic-gate return;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate va_start(ap, fmt);
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate * either log to stderr, or log to syslog. print out unix
697c478bd9Sstevel@tonic-gate * error message if errlevel is MSG_ERR and errno is set
707c478bd9Sstevel@tonic-gate */
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate if (is_daemon) {
737c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), (errlevel == MSG_ERR &&
747c478bd9Sstevel@tonic-gate errno != 0) ? "%s: %%m\n" : "%s\n", gettext(fmt));
757c478bd9Sstevel@tonic-gate (void) vsyslog(err_to_syslog(errlevel), buf, ap);
767c478bd9Sstevel@tonic-gate } else {
777c478bd9Sstevel@tonic-gate errmsg = strerror(errno);
787c478bd9Sstevel@tonic-gate if (errmsg == NULL)
797c478bd9Sstevel@tonic-gate errmsg = dgettext(TEXT_DOMAIN, "<unknown error>");
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), (errlevel == MSG_ERR &&
827c478bd9Sstevel@tonic-gate errno != 0) ? "%s: %s: %s: %s\n" : "%s: %s: %s\n", program,
837c478bd9Sstevel@tonic-gate dgettext(TEXT_DOMAIN, err_to_string(errlevel)),
847c478bd9Sstevel@tonic-gate gettext(fmt), errmsg);
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, buf, ap);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate va_end(ap);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate * dhcpmsg_init(): opens and initializes the DHCP messaging facility
947c478bd9Sstevel@tonic-gate *
957c478bd9Sstevel@tonic-gate * input: const char *: the name of the executable
967c478bd9Sstevel@tonic-gate * boolean_t: whether the executable is a daemon
977c478bd9Sstevel@tonic-gate * boolean_t: whether the executable is running "verbosely"
987c478bd9Sstevel@tonic-gate * int: the debugging level the executable is being run at
997c478bd9Sstevel@tonic-gate * output: void
1007c478bd9Sstevel@tonic-gate */
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate void
dhcpmsg_init(const char * program_name,boolean_t daemon,boolean_t verbose,int level)1037c478bd9Sstevel@tonic-gate dhcpmsg_init(const char *program_name, boolean_t daemon, boolean_t verbose,
1047c478bd9Sstevel@tonic-gate int level)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate (void) strlcpy(program, program_name, sizeof (program));
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate debug_level = level;
1097c478bd9Sstevel@tonic-gate is_verbose = verbose;
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate if (daemon) {
1127c478bd9Sstevel@tonic-gate is_daemon = B_TRUE;
1137c478bd9Sstevel@tonic-gate (void) openlog(program, LOG_PID, LOG_DAEMON);
1147c478bd9Sstevel@tonic-gate if (is_verbose) {
1157c478bd9Sstevel@tonic-gate syslog(err_to_syslog(MSG_VERBOSE), "%s",
1167c478bd9Sstevel@tonic-gate dgettext(TEXT_DOMAIN, "Daemon started"));
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate * dhcpmsg_fini(): closes the DHCP messaging facility.
1237c478bd9Sstevel@tonic-gate *
1247c478bd9Sstevel@tonic-gate * input: void
1257c478bd9Sstevel@tonic-gate * output: void
1267c478bd9Sstevel@tonic-gate */
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate void
dhcpmsg_fini(void)1297c478bd9Sstevel@tonic-gate dhcpmsg_fini(void)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate if (is_daemon) {
1327c478bd9Sstevel@tonic-gate if (is_verbose) {
1337c478bd9Sstevel@tonic-gate syslog(err_to_syslog(MSG_VERBOSE), "%s",
1347c478bd9Sstevel@tonic-gate dgettext(TEXT_DOMAIN, "Daemon terminated"));
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate closelog();
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate * err_to_syslog(): converts a dhcpmsg log level into a syslog log level
1427c478bd9Sstevel@tonic-gate *
1437c478bd9Sstevel@tonic-gate * input: int: the dhcpmsg log level
1447c478bd9Sstevel@tonic-gate * output: int: the syslog log level
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate static int
err_to_syslog(int errlevel)1487c478bd9Sstevel@tonic-gate err_to_syslog(int errlevel)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate switch (errlevel) {
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate case MSG_DEBUG:
1537c478bd9Sstevel@tonic-gate case MSG_DEBUG2:
1547c478bd9Sstevel@tonic-gate return (LOG_DEBUG);
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate case MSG_ERROR:
1577c478bd9Sstevel@tonic-gate case MSG_ERR:
1587c478bd9Sstevel@tonic-gate return (LOG_ERR);
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate case MSG_WARNING:
1617c478bd9Sstevel@tonic-gate return (LOG_WARNING);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate case MSG_NOTICE:
1647c478bd9Sstevel@tonic-gate return (LOG_NOTICE);
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate case MSG_CRIT:
1677c478bd9Sstevel@tonic-gate return (LOG_CRIT);
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate case MSG_VERBOSE:
1707c478bd9Sstevel@tonic-gate case MSG_INFO:
1717c478bd9Sstevel@tonic-gate return (LOG_INFO);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate return (LOG_INFO);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate * err_to_string(): converts a log level into a string
1797c478bd9Sstevel@tonic-gate *
1807c478bd9Sstevel@tonic-gate * input: int: the log level
1817c478bd9Sstevel@tonic-gate * output: const char *: the stringified log level
1827c478bd9Sstevel@tonic-gate */
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate static const char *
err_to_string(int errlevel)1857c478bd9Sstevel@tonic-gate err_to_string(int errlevel)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate switch (errlevel) {
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate case MSG_DEBUG:
1907c478bd9Sstevel@tonic-gate case MSG_DEBUG2:
1917c478bd9Sstevel@tonic-gate return ("debug");
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate case MSG_ERR:
1947c478bd9Sstevel@tonic-gate case MSG_ERROR:
1957c478bd9Sstevel@tonic-gate return ("error");
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate case MSG_WARNING:
1987c478bd9Sstevel@tonic-gate return ("warning");
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate case MSG_NOTICE:
2017c478bd9Sstevel@tonic-gate return ("notice");
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate case MSG_CRIT:
2047c478bd9Sstevel@tonic-gate return ("CRITICAL");
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate case MSG_VERBOSE:
2077c478bd9Sstevel@tonic-gate case MSG_INFO:
2087c478bd9Sstevel@tonic-gate return ("info");
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate return ("<unknown>");
2127c478bd9Sstevel@tonic-gate }
213