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