xref: /onnv-gate/usr/src/cmd/ssh/libssh/common/log.c (revision 3102)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
30Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
40Sstevel@tonic-gate  *                    All rights reserved
50Sstevel@tonic-gate  *
60Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
70Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
80Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
90Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
100Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
110Sstevel@tonic-gate  */
120Sstevel@tonic-gate /*
130Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
160Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
170Sstevel@tonic-gate  * are met:
180Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
190Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
200Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
210Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
220Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
230Sstevel@tonic-gate  *
240Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
250Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
260Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
270Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
280Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
290Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
300Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
310Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
320Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
330Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate /*
36*3102Sjp161948  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
370Sstevel@tonic-gate  * Use is subject to license terms.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "includes.h"
410Sstevel@tonic-gate RCSID("$OpenBSD: log.c,v 1.24 2002/07/19 15:43:33 markus Exp $");
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include "log.h"
460Sstevel@tonic-gate #include "xmalloc.h"
470Sstevel@tonic-gate 
48*3102Sjp161948 #include <atomic.h>
490Sstevel@tonic-gate #include <syslog.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static LogLevel log_level = SYSLOG_LEVEL_INFO;
520Sstevel@tonic-gate static int log_on_stderr = 1;
530Sstevel@tonic-gate static int log_facility = LOG_AUTH;
540Sstevel@tonic-gate static char *argv0;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate extern char *__progname;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static const char *log_txt_prefix = "";
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* textual representation of log-facilities/levels */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static struct {
630Sstevel@tonic-gate 	const char *name;
640Sstevel@tonic-gate 	SyslogFacility val;
650Sstevel@tonic-gate } log_facilities[] = {
660Sstevel@tonic-gate 	{ "DAEMON",	SYSLOG_FACILITY_DAEMON },
670Sstevel@tonic-gate 	{ "USER",	SYSLOG_FACILITY_USER },
680Sstevel@tonic-gate 	{ "AUTH",	SYSLOG_FACILITY_AUTH },
690Sstevel@tonic-gate #ifdef LOG_AUTHPRIV
700Sstevel@tonic-gate 	{ "AUTHPRIV",	SYSLOG_FACILITY_AUTHPRIV },
710Sstevel@tonic-gate #endif
720Sstevel@tonic-gate 	{ "LOCAL0",	SYSLOG_FACILITY_LOCAL0 },
730Sstevel@tonic-gate 	{ "LOCAL1",	SYSLOG_FACILITY_LOCAL1 },
740Sstevel@tonic-gate 	{ "LOCAL2",	SYSLOG_FACILITY_LOCAL2 },
750Sstevel@tonic-gate 	{ "LOCAL3",	SYSLOG_FACILITY_LOCAL3 },
760Sstevel@tonic-gate 	{ "LOCAL4",	SYSLOG_FACILITY_LOCAL4 },
770Sstevel@tonic-gate 	{ "LOCAL5",	SYSLOG_FACILITY_LOCAL5 },
780Sstevel@tonic-gate 	{ "LOCAL6",	SYSLOG_FACILITY_LOCAL6 },
790Sstevel@tonic-gate 	{ "LOCAL7",	SYSLOG_FACILITY_LOCAL7 },
800Sstevel@tonic-gate 	{ NULL,		SYSLOG_FACILITY_NOT_SET }
810Sstevel@tonic-gate };
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static struct {
840Sstevel@tonic-gate 	const char *name;
850Sstevel@tonic-gate 	LogLevel val;
860Sstevel@tonic-gate } log_levels[] =
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	{ "QUIET",	SYSLOG_LEVEL_QUIET },
890Sstevel@tonic-gate 	{ "FATAL",	SYSLOG_LEVEL_FATAL },
900Sstevel@tonic-gate 	{ "ERROR",	SYSLOG_LEVEL_ERROR },
910Sstevel@tonic-gate 	{ "NOTICE",	SYSLOG_LEVEL_NOTICE },
920Sstevel@tonic-gate 	{ "INFO",	SYSLOG_LEVEL_INFO },
930Sstevel@tonic-gate 	{ "VERBOSE",	SYSLOG_LEVEL_VERBOSE },
940Sstevel@tonic-gate 	{ "DEBUG",	SYSLOG_LEVEL_DEBUG1 },
950Sstevel@tonic-gate 	{ "DEBUG1",	SYSLOG_LEVEL_DEBUG1 },
960Sstevel@tonic-gate 	{ "DEBUG2",	SYSLOG_LEVEL_DEBUG2 },
970Sstevel@tonic-gate 	{ "DEBUG3",	SYSLOG_LEVEL_DEBUG3 },
980Sstevel@tonic-gate 	{ NULL,		SYSLOG_LEVEL_NOT_SET }
990Sstevel@tonic-gate };
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate SyslogFacility
1020Sstevel@tonic-gate log_facility_number(char *name)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	int i;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (name != NULL)
1070Sstevel@tonic-gate 		for (i = 0; log_facilities[i].name; i++)
1080Sstevel@tonic-gate 			if (strcasecmp(log_facilities[i].name, name) == 0)
1090Sstevel@tonic-gate 				return log_facilities[i].val;
1100Sstevel@tonic-gate 	return SYSLOG_FACILITY_NOT_SET;
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate LogLevel
1140Sstevel@tonic-gate log_level_number(char *name)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	int i;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (name != NULL)
1190Sstevel@tonic-gate 		for (i = 0; log_levels[i].name; i++)
1200Sstevel@tonic-gate 			if (strcasecmp(log_levels[i].name, name) == 0)
1210Sstevel@tonic-gate 				return log_levels[i].val;
1220Sstevel@tonic-gate 	return SYSLOG_LEVEL_NOT_SET;
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate void
1260Sstevel@tonic-gate set_log_txt_prefix(const char *txt)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	log_txt_prefix = txt;
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /* Error messages that should be logged. */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate void
1340Sstevel@tonic-gate error(const char *fmt,...)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	va_list args;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	va_start(args, fmt);
1390Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_ERROR, fmt, args);
1400Sstevel@tonic-gate 	va_end(args);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate void
1440Sstevel@tonic-gate notice(const char *fmt,...)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	va_list args;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	va_start(args, fmt);
1490Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_NOTICE, fmt, args);
1500Sstevel@tonic-gate 	va_end(args);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /* Log this message (information that usually should go to the log). */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate void
1560Sstevel@tonic-gate log(const char *fmt,...)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	va_list args;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	va_start(args, fmt);
1610Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_INFO, fmt, args);
1620Sstevel@tonic-gate 	va_end(args);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /* More detailed messages (information that does not need to go to the log). */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate void
1680Sstevel@tonic-gate verbose(const char *fmt,...)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	va_list args;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	va_start(args, fmt);
1730Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
1740Sstevel@tonic-gate 	va_end(args);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /* Debugging messages that should not be logged during normal operation. */
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate void
1800Sstevel@tonic-gate debug(const char *fmt,...)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	va_list args;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	va_start(args, fmt);
1850Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
1860Sstevel@tonic-gate 	va_end(args);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate void
1900Sstevel@tonic-gate debug2(const char *fmt,...)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	va_list args;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	va_start(args, fmt);
1950Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
1960Sstevel@tonic-gate 	va_end(args);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate void
2000Sstevel@tonic-gate debug3(const char *fmt,...)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	va_list args;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	va_start(args, fmt);
2050Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
2060Sstevel@tonic-gate 	va_end(args);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /* Fatal cleanup */
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate struct fatal_cleanup {
2120Sstevel@tonic-gate 	struct fatal_cleanup *next;
2130Sstevel@tonic-gate 	void (*proc) (void *);
2140Sstevel@tonic-gate 	void *context;
2150Sstevel@tonic-gate };
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate static struct fatal_cleanup *fatal_cleanups = NULL;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /* Registers a cleanup function to be called by fatal() before exiting. */
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate void
2220Sstevel@tonic-gate fatal_add_cleanup(void (*proc) (void *), void *context)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	struct fatal_cleanup *cu;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	cu = xmalloc(sizeof(*cu));
2270Sstevel@tonic-gate 	cu->proc = proc;
2280Sstevel@tonic-gate 	cu->context = context;
2290Sstevel@tonic-gate 	cu->next = fatal_cleanups;
2300Sstevel@tonic-gate 	fatal_cleanups = cu;
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate /* Removes a cleanup frunction to be called at fatal(). */
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate void
2360Sstevel@tonic-gate fatal_remove_cleanup(void (*proc) (void *context), void *context)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	struct fatal_cleanup **cup, *cu;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	for (cup = &fatal_cleanups; *cup; cup = &cu->next) {
2410Sstevel@tonic-gate 		cu = *cup;
2420Sstevel@tonic-gate 		if (cu->proc == proc && cu->context == context) {
2430Sstevel@tonic-gate 			*cup = cu->next;
2440Sstevel@tonic-gate 			xfree(cu);
2450Sstevel@tonic-gate 			return;
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	debug3("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx",
2490Sstevel@tonic-gate 	    (u_long) proc, (u_long) context);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /* Remove all cleanups, to be called after fork() */
2530Sstevel@tonic-gate void
2540Sstevel@tonic-gate fatal_remove_all_cleanups(void)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	struct fatal_cleanup *cu, *next_cu;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	for (cu = fatal_cleanups; cu; cu = next_cu) {
2590Sstevel@tonic-gate 		next_cu = cu->next;
2600Sstevel@tonic-gate 		xfree(cu);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	fatal_cleanups = NULL;
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
266*3102Sjp161948 /* Cleanup and exit. Make sure each cleanup is called only once. */
2670Sstevel@tonic-gate void
2680Sstevel@tonic-gate fatal_cleanup(void)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	struct fatal_cleanup *cu, *next_cu;
271*3102Sjp161948 	static volatile u_int called = 0;
2720Sstevel@tonic-gate 
273*3102Sjp161948 	if (atomic_cas_uint(&called, 0, 1) == 1)
2740Sstevel@tonic-gate 		exit(255);
2750Sstevel@tonic-gate 	/* Call cleanup functions. */
2760Sstevel@tonic-gate 	for (cu = fatal_cleanups; cu; cu = next_cu) {
2770Sstevel@tonic-gate 		next_cu = cu->next;
2780Sstevel@tonic-gate 		debug("Calling cleanup 0x%lx(0x%lx)",
2790Sstevel@tonic-gate 		    (u_long) cu->proc, (u_long) cu->context);
2800Sstevel@tonic-gate 		(*cu->proc) (cu->context);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 	exit(255);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate  * Initialize the log.
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate void
2910Sstevel@tonic-gate log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	argv0 = av0;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	switch (level) {
2960Sstevel@tonic-gate 	case SYSLOG_LEVEL_QUIET:
2970Sstevel@tonic-gate 	case SYSLOG_LEVEL_FATAL:
2980Sstevel@tonic-gate 	case SYSLOG_LEVEL_ERROR:
2990Sstevel@tonic-gate 	case SYSLOG_LEVEL_NOTICE:
3000Sstevel@tonic-gate 	case SYSLOG_LEVEL_INFO:
3010Sstevel@tonic-gate 	case SYSLOG_LEVEL_VERBOSE:
3020Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG1:
3030Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG2:
3040Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG3:
3050Sstevel@tonic-gate 		log_level = level;
3060Sstevel@tonic-gate 		break;
3070Sstevel@tonic-gate 	default:
3080Sstevel@tonic-gate 		fprintf(stderr, "Unrecognized internal syslog level code %d\n",
3090Sstevel@tonic-gate 		    (int) level);
3100Sstevel@tonic-gate 		exit(1);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	log_on_stderr = on_stderr;
3140Sstevel@tonic-gate 	if (on_stderr)
3150Sstevel@tonic-gate 		return;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	switch (facility) {
3180Sstevel@tonic-gate 	case SYSLOG_FACILITY_DAEMON:
3190Sstevel@tonic-gate 		log_facility = LOG_DAEMON;
3200Sstevel@tonic-gate 		break;
3210Sstevel@tonic-gate 	case SYSLOG_FACILITY_USER:
3220Sstevel@tonic-gate 		log_facility = LOG_USER;
3230Sstevel@tonic-gate 		break;
3240Sstevel@tonic-gate 	case SYSLOG_FACILITY_AUTH:
3250Sstevel@tonic-gate 		log_facility = LOG_AUTH;
3260Sstevel@tonic-gate 		break;
3270Sstevel@tonic-gate #ifdef LOG_AUTHPRIV
3280Sstevel@tonic-gate 	case SYSLOG_FACILITY_AUTHPRIV:
3290Sstevel@tonic-gate 		log_facility = LOG_AUTHPRIV;
3300Sstevel@tonic-gate 		break;
3310Sstevel@tonic-gate #endif
3320Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL0:
3330Sstevel@tonic-gate 		log_facility = LOG_LOCAL0;
3340Sstevel@tonic-gate 		break;
3350Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL1:
3360Sstevel@tonic-gate 		log_facility = LOG_LOCAL1;
3370Sstevel@tonic-gate 		break;
3380Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL2:
3390Sstevel@tonic-gate 		log_facility = LOG_LOCAL2;
3400Sstevel@tonic-gate 		break;
3410Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL3:
3420Sstevel@tonic-gate 		log_facility = LOG_LOCAL3;
3430Sstevel@tonic-gate 		break;
3440Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL4:
3450Sstevel@tonic-gate 		log_facility = LOG_LOCAL4;
3460Sstevel@tonic-gate 		break;
3470Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL5:
3480Sstevel@tonic-gate 		log_facility = LOG_LOCAL5;
3490Sstevel@tonic-gate 		break;
3500Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL6:
3510Sstevel@tonic-gate 		log_facility = LOG_LOCAL6;
3520Sstevel@tonic-gate 		break;
3530Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL7:
3540Sstevel@tonic-gate 		log_facility = LOG_LOCAL7;
3550Sstevel@tonic-gate 		break;
3560Sstevel@tonic-gate 	default:
3570Sstevel@tonic-gate 		fprintf(stderr,
3580Sstevel@tonic-gate 		    "Unrecognized internal syslog facility code %d\n",
3590Sstevel@tonic-gate 		    (int) facility);
3600Sstevel@tonic-gate 		exit(1);
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate #define MSGBUFSIZ 1024
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /* PRINTFLIKE2 */
3670Sstevel@tonic-gate void
3680Sstevel@tonic-gate do_log(LogLevel level, const char *fmt, va_list args)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	char msgbuf[MSGBUFSIZ];
3710Sstevel@tonic-gate 	char fmtbuf[MSGBUFSIZ];
3720Sstevel@tonic-gate 	char *txt = NULL;
3730Sstevel@tonic-gate 	int pri = LOG_INFO;
3740Sstevel@tonic-gate 	int do_gettext = log_on_stderr; /*
3750Sstevel@tonic-gate 					 * Localize user messages - not
3760Sstevel@tonic-gate 					 * syslog()ed messages.
3770Sstevel@tonic-gate 					 */
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (level > log_level)
3800Sstevel@tonic-gate 		return;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	switch (level) {
3830Sstevel@tonic-gate 	case SYSLOG_LEVEL_FATAL:
3840Sstevel@tonic-gate 		if (!log_on_stderr)
3850Sstevel@tonic-gate 			txt = "fatal";
3860Sstevel@tonic-gate 		pri = LOG_CRIT;
3870Sstevel@tonic-gate 		break;
3880Sstevel@tonic-gate 	case SYSLOG_LEVEL_ERROR:
3890Sstevel@tonic-gate 		if (!log_on_stderr)
3900Sstevel@tonic-gate 			txt = "error";
3910Sstevel@tonic-gate 		pri = LOG_ERR;
3920Sstevel@tonic-gate 		break;
3930Sstevel@tonic-gate 	case SYSLOG_LEVEL_NOTICE:
3940Sstevel@tonic-gate 		pri = LOG_NOTICE;
3950Sstevel@tonic-gate 		break;
3960Sstevel@tonic-gate 	case SYSLOG_LEVEL_INFO:
3970Sstevel@tonic-gate 		pri = LOG_INFO;
3980Sstevel@tonic-gate 		break;
3990Sstevel@tonic-gate 	case SYSLOG_LEVEL_VERBOSE:
4000Sstevel@tonic-gate 		pri = LOG_INFO;
4010Sstevel@tonic-gate 		break;
4020Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG1:
4030Sstevel@tonic-gate 		txt = "debug1";
4040Sstevel@tonic-gate 		pri = LOG_DEBUG;
4050Sstevel@tonic-gate 		/*
4060Sstevel@tonic-gate 		 * Don't localize debug messages - such are not intended
4070Sstevel@tonic-gate 		 * for users but for support staff whose preferred
4080Sstevel@tonic-gate 		 * language is unknown, therefore we default to the
4090Sstevel@tonic-gate 		 * language used in the source code: English.
4100Sstevel@tonic-gate 		 */
4110Sstevel@tonic-gate 		do_gettext = 0;
4120Sstevel@tonic-gate 		break;
4130Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG2:
4140Sstevel@tonic-gate 		txt = "debug2";
4150Sstevel@tonic-gate 		pri = LOG_DEBUG;
4160Sstevel@tonic-gate 		do_gettext = 0;	    /* Don't localize debug messages. */
4170Sstevel@tonic-gate 		break;
4180Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG3:
4190Sstevel@tonic-gate 		txt = "debug3";
4200Sstevel@tonic-gate 		pri = LOG_DEBUG;
4210Sstevel@tonic-gate 		do_gettext = 0;	    /* Don't localize debug messages. */
4220Sstevel@tonic-gate 		break;
4230Sstevel@tonic-gate 	default:
4240Sstevel@tonic-gate 		txt = "internal error";
4250Sstevel@tonic-gate 		pri = LOG_ERR;
4260Sstevel@tonic-gate 		break;
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 	if (txt != NULL) {
4290Sstevel@tonic-gate 		snprintf(fmtbuf, sizeof(fmtbuf), "%s%s: %s", log_txt_prefix,
4300Sstevel@tonic-gate 			 do_gettext ? gettext(txt) : txt,
4310Sstevel@tonic-gate 			 do_gettext ? gettext(fmt) : fmt);
4320Sstevel@tonic-gate 		vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
4330Sstevel@tonic-gate 	} else {
4340Sstevel@tonic-gate 		vsnprintf(msgbuf, sizeof(msgbuf),
4350Sstevel@tonic-gate 			  do_gettext ? gettext(fmt) : fmt,
4360Sstevel@tonic-gate 			  args);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 	if (log_on_stderr) {
4390Sstevel@tonic-gate 		fprintf(stderr, "%s\r\n", msgbuf);
4400Sstevel@tonic-gate 	} else {
4410Sstevel@tonic-gate 		openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
4420Sstevel@tonic-gate 		syslog(pri, "%.500s", msgbuf);
4430Sstevel@tonic-gate 		closelog();
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate }
446