4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
38 static boolean_t is_daemon
= B_FALSE
;
39 static boolean_t is_verbose
= B_FALSE
;
40 static char program
[PATH_MAX
] = "<unknown>";
41 static int debug_level
;
43 static const char *err_to_string(int);
44 static int err_to_syslog(int);
47 * dhcpmsg(): logs a message to the console or to syslog
49 * input: int: the level to log the message at
50 * const char *: a printf-like format string
51 * ...: arguments to the format string
56 dhcpmsg(int errlevel
, const char *fmt
, ...)
62 if ((errlevel
== MSG_DEBUG2
&& (debug_level
< 2)) ||
63 (errlevel
== MSG_DEBUG
&& (debug_level
< 1)) ||
64 (errlevel
== MSG_VERBOSE
&& !is_verbose
))
70 * either log to stderr, or log to syslog. print out unix
71 * error message if errlevel is MSG_ERR and errno is set
75 (void) snprintf(buf
, sizeof (buf
), (errlevel
== MSG_ERR
&&
76 errno
!= 0) ? "%s: %%m\n" : "%s\n", gettext(fmt
));
77 (void) vsyslog(err_to_syslog(errlevel
), buf
, ap
);
79 errmsg
= strerror(errno
);
81 errmsg
= dgettext(TEXT_DOMAIN
, "<unknown error>");
83 (void) snprintf(buf
, sizeof (buf
), (errlevel
== MSG_ERR
&&
84 errno
!= 0) ? "%s: %s: %s: %s\n" : "%s: %s: %s\n", program
,
85 dgettext(TEXT_DOMAIN
, err_to_string(errlevel
)),
86 gettext(fmt
), errmsg
);
88 (void) vfprintf(stderr
, buf
, ap
);
95 * dhcpmsg_init(): opens and initializes the DHCP messaging facility
97 * input: const char *: the name of the executable
98 * boolean_t: whether the executable is a daemon
99 * boolean_t: whether the executable is running "verbosely"
100 * int: the debugging level the executable is being run at
105 dhcpmsg_init(const char *program_name
, boolean_t daemon
, boolean_t verbose
,
108 (void) strlcpy(program
, program_name
, sizeof (program
));
111 is_verbose
= verbose
;
115 (void) openlog(program
, LOG_PID
, LOG_DAEMON
);
117 syslog(err_to_syslog(MSG_VERBOSE
), "%s",
118 dgettext(TEXT_DOMAIN
, "Daemon started"));
124 * dhcpmsg_fini(): closes the DHCP messaging facility.
135 syslog(err_to_syslog(MSG_VERBOSE
), "%s",
136 dgettext(TEXT_DOMAIN
, "Daemon terminated"));
143 * err_to_syslog(): converts a dhcpmsg log level into a syslog log level
145 * input: int: the dhcpmsg log level
146 * output: int: the syslog log level
150 err_to_syslog(int errlevel
)
163 return (LOG_WARNING
);
180 * err_to_string(): converts a log level into a string
182 * input: int: the log level
183 * output: const char *: the stringified log level
187 err_to_string(int errlevel
)
213 return ("<unknown>");