xref: /titanic_50/usr/src/cmd/ssh/libssh/common/log.c (revision a6e0e77db3495a73e0c084496fedccf16413a311)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate  *                    All rights reserved
57c478bd9Sstevel@tonic-gate  *
67c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
77c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
87c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
97c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
107c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
117c478bd9Sstevel@tonic-gate  */
127c478bd9Sstevel@tonic-gate /*
137c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
167c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
177c478bd9Sstevel@tonic-gate  * are met:
187c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
197c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
207c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
217c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
227c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
257c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
267c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
277c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
287c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
297c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
307c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
317c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
327c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
337c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate /*
3660779adbSjp161948  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
377c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "includes.h"
417c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: log.c,v 1.24 2002/07/19 15:43:33 markus Exp $");
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "log.h"
467c478bd9Sstevel@tonic-gate #include "xmalloc.h"
477c478bd9Sstevel@tonic-gate 
4860779adbSjp161948 #include <atomic.h>
497c478bd9Sstevel@tonic-gate #include <syslog.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static LogLevel log_level = SYSLOG_LEVEL_INFO;
527c478bd9Sstevel@tonic-gate static int log_on_stderr = 1;
537c478bd9Sstevel@tonic-gate static int log_facility = LOG_AUTH;
547c478bd9Sstevel@tonic-gate static char *argv0;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate extern char *__progname;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static const char *log_txt_prefix = "";
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* textual representation of log-facilities/levels */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static struct {
637c478bd9Sstevel@tonic-gate 	const char *name;
647c478bd9Sstevel@tonic-gate 	SyslogFacility val;
657c478bd9Sstevel@tonic-gate } log_facilities[] = {
667c478bd9Sstevel@tonic-gate 	{ "DAEMON",	SYSLOG_FACILITY_DAEMON },
677c478bd9Sstevel@tonic-gate 	{ "USER",	SYSLOG_FACILITY_USER },
687c478bd9Sstevel@tonic-gate 	{ "AUTH",	SYSLOG_FACILITY_AUTH },
697c478bd9Sstevel@tonic-gate #ifdef LOG_AUTHPRIV
707c478bd9Sstevel@tonic-gate 	{ "AUTHPRIV",	SYSLOG_FACILITY_AUTHPRIV },
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate 	{ "LOCAL0",	SYSLOG_FACILITY_LOCAL0 },
737c478bd9Sstevel@tonic-gate 	{ "LOCAL1",	SYSLOG_FACILITY_LOCAL1 },
747c478bd9Sstevel@tonic-gate 	{ "LOCAL2",	SYSLOG_FACILITY_LOCAL2 },
757c478bd9Sstevel@tonic-gate 	{ "LOCAL3",	SYSLOG_FACILITY_LOCAL3 },
767c478bd9Sstevel@tonic-gate 	{ "LOCAL4",	SYSLOG_FACILITY_LOCAL4 },
777c478bd9Sstevel@tonic-gate 	{ "LOCAL5",	SYSLOG_FACILITY_LOCAL5 },
787c478bd9Sstevel@tonic-gate 	{ "LOCAL6",	SYSLOG_FACILITY_LOCAL6 },
797c478bd9Sstevel@tonic-gate 	{ "LOCAL7",	SYSLOG_FACILITY_LOCAL7 },
807c478bd9Sstevel@tonic-gate 	{ NULL,		SYSLOG_FACILITY_NOT_SET }
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static struct {
847c478bd9Sstevel@tonic-gate 	const char *name;
857c478bd9Sstevel@tonic-gate 	LogLevel val;
867c478bd9Sstevel@tonic-gate } log_levels[] =
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	{ "QUIET",	SYSLOG_LEVEL_QUIET },
897c478bd9Sstevel@tonic-gate 	{ "FATAL",	SYSLOG_LEVEL_FATAL },
907c478bd9Sstevel@tonic-gate 	{ "ERROR",	SYSLOG_LEVEL_ERROR },
917c478bd9Sstevel@tonic-gate 	{ "NOTICE",	SYSLOG_LEVEL_NOTICE },
927c478bd9Sstevel@tonic-gate 	{ "INFO",	SYSLOG_LEVEL_INFO },
937c478bd9Sstevel@tonic-gate 	{ "VERBOSE",	SYSLOG_LEVEL_VERBOSE },
947c478bd9Sstevel@tonic-gate 	{ "DEBUG",	SYSLOG_LEVEL_DEBUG1 },
957c478bd9Sstevel@tonic-gate 	{ "DEBUG1",	SYSLOG_LEVEL_DEBUG1 },
967c478bd9Sstevel@tonic-gate 	{ "DEBUG2",	SYSLOG_LEVEL_DEBUG2 },
977c478bd9Sstevel@tonic-gate 	{ "DEBUG3",	SYSLOG_LEVEL_DEBUG3 },
987c478bd9Sstevel@tonic-gate 	{ NULL,		SYSLOG_LEVEL_NOT_SET }
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate SyslogFacility
log_facility_number(char * name)1027c478bd9Sstevel@tonic-gate log_facility_number(char *name)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	int i;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (name != NULL)
1077c478bd9Sstevel@tonic-gate 		for (i = 0; log_facilities[i].name; i++)
1087c478bd9Sstevel@tonic-gate 			if (strcasecmp(log_facilities[i].name, name) == 0)
1097c478bd9Sstevel@tonic-gate 				return log_facilities[i].val;
1107c478bd9Sstevel@tonic-gate 	return SYSLOG_FACILITY_NOT_SET;
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate LogLevel
log_level_number(char * name)1147c478bd9Sstevel@tonic-gate log_level_number(char *name)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	int i;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (name != NULL)
1197c478bd9Sstevel@tonic-gate 		for (i = 0; log_levels[i].name; i++)
1207c478bd9Sstevel@tonic-gate 			if (strcasecmp(log_levels[i].name, name) == 0)
1217c478bd9Sstevel@tonic-gate 				return log_levels[i].val;
1227c478bd9Sstevel@tonic-gate 	return SYSLOG_LEVEL_NOT_SET;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate void
set_log_txt_prefix(const char * txt)1267c478bd9Sstevel@tonic-gate set_log_txt_prefix(const char *txt)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	log_txt_prefix = txt;
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /* Error messages that should be logged. */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate void
error(const char * fmt,...)1347c478bd9Sstevel@tonic-gate error(const char *fmt,...)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	va_list args;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
1397c478bd9Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_ERROR, fmt, args);
1407c478bd9Sstevel@tonic-gate 	va_end(args);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate void
notice(const char * fmt,...)1447c478bd9Sstevel@tonic-gate notice(const char *fmt,...)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	va_list args;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
1497c478bd9Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_NOTICE, fmt, args);
1507c478bd9Sstevel@tonic-gate 	va_end(args);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /* Log this message (information that usually should go to the log). */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate void
log(const char * fmt,...)1567c478bd9Sstevel@tonic-gate log(const char *fmt,...)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	va_list args;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
1617c478bd9Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_INFO, fmt, args);
1627c478bd9Sstevel@tonic-gate 	va_end(args);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /* More detailed messages (information that does not need to go to the log). */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate void
verbose(const char * fmt,...)1687c478bd9Sstevel@tonic-gate verbose(const char *fmt,...)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	va_list args;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
1737c478bd9Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
1747c478bd9Sstevel@tonic-gate 	va_end(args);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /* Debugging messages that should not be logged during normal operation. */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate void
debug(const char * fmt,...)1807c478bd9Sstevel@tonic-gate debug(const char *fmt,...)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	va_list args;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
1857c478bd9Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
1867c478bd9Sstevel@tonic-gate 	va_end(args);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate void
debug2(const char * fmt,...)1907c478bd9Sstevel@tonic-gate debug2(const char *fmt,...)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	va_list args;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
1957c478bd9Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
1967c478bd9Sstevel@tonic-gate 	va_end(args);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate void
debug3(const char * fmt,...)2007c478bd9Sstevel@tonic-gate debug3(const char *fmt,...)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	va_list args;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
2057c478bd9Sstevel@tonic-gate 	do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
2067c478bd9Sstevel@tonic-gate 	va_end(args);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /* Fatal cleanup */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate struct fatal_cleanup {
2127c478bd9Sstevel@tonic-gate 	struct fatal_cleanup *next;
2137c478bd9Sstevel@tonic-gate 	void (*proc) (void *);
2147c478bd9Sstevel@tonic-gate 	void *context;
2157c478bd9Sstevel@tonic-gate };
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static struct fatal_cleanup *fatal_cleanups = NULL;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /* Registers a cleanup function to be called by fatal() before exiting. */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate void
fatal_add_cleanup(void (* proc)(void *),void * context)2227c478bd9Sstevel@tonic-gate fatal_add_cleanup(void (*proc) (void *), void *context)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	struct fatal_cleanup *cu;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	cu = xmalloc(sizeof(*cu));
2277c478bd9Sstevel@tonic-gate 	cu->proc = proc;
2287c478bd9Sstevel@tonic-gate 	cu->context = context;
2297c478bd9Sstevel@tonic-gate 	cu->next = fatal_cleanups;
2307c478bd9Sstevel@tonic-gate 	fatal_cleanups = cu;
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
233*a6e0e77dSjp161948 /* Removes a cleanup function to be called at fatal(). */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate void
fatal_remove_cleanup(void (* proc)(void * context),void * context)2367c478bd9Sstevel@tonic-gate fatal_remove_cleanup(void (*proc) (void *context), void *context)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	struct fatal_cleanup **cup, *cu;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	for (cup = &fatal_cleanups; *cup; cup = &cu->next) {
2417c478bd9Sstevel@tonic-gate 		cu = *cup;
2427c478bd9Sstevel@tonic-gate 		if (cu->proc == proc && cu->context == context) {
2437c478bd9Sstevel@tonic-gate 			*cup = cu->next;
2447c478bd9Sstevel@tonic-gate 			xfree(cu);
2457c478bd9Sstevel@tonic-gate 			return;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	debug3("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx",
2497c478bd9Sstevel@tonic-gate 	    (u_long) proc, (u_long) context);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /* Remove all cleanups, to be called after fork() */
2537c478bd9Sstevel@tonic-gate void
fatal_remove_all_cleanups(void)2547c478bd9Sstevel@tonic-gate fatal_remove_all_cleanups(void)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	struct fatal_cleanup *cu, *next_cu;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	for (cu = fatal_cleanups; cu; cu = next_cu) {
2597c478bd9Sstevel@tonic-gate 		next_cu = cu->next;
2607c478bd9Sstevel@tonic-gate 		xfree(cu);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	fatal_cleanups = NULL;
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
26660779adbSjp161948 /* Cleanup and exit. Make sure each cleanup is called only once. */
2677c478bd9Sstevel@tonic-gate void
fatal_cleanup(void)2687c478bd9Sstevel@tonic-gate fatal_cleanup(void)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	struct fatal_cleanup *cu, *next_cu;
27160779adbSjp161948 	static volatile u_int called = 0;
2727c478bd9Sstevel@tonic-gate 
27360779adbSjp161948 	if (atomic_cas_uint(&called, 0, 1) == 1)
2747c478bd9Sstevel@tonic-gate 		exit(255);
2757c478bd9Sstevel@tonic-gate 	/* Call cleanup functions. */
2767c478bd9Sstevel@tonic-gate 	for (cu = fatal_cleanups; cu; cu = next_cu) {
2777c478bd9Sstevel@tonic-gate 		next_cu = cu->next;
2787c478bd9Sstevel@tonic-gate 		debug("Calling cleanup 0x%lx(0x%lx)",
2797c478bd9Sstevel@tonic-gate 		    (u_long) cu->proc, (u_long) cu->context);
2807c478bd9Sstevel@tonic-gate 		(*cu->proc) (cu->context);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 	exit(255);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * Initialize the log.
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate void
log_init(char * av0,LogLevel level,SyslogFacility facility,int on_stderr)2917c478bd9Sstevel@tonic-gate log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	argv0 = av0;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	switch (level) {
2967c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_QUIET:
2977c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_FATAL:
2987c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_ERROR:
2997c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_NOTICE:
3007c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_INFO:
3017c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_VERBOSE:
3027c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG1:
3037c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG2:
3047c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG3:
3057c478bd9Sstevel@tonic-gate 		log_level = level;
3067c478bd9Sstevel@tonic-gate 		break;
3077c478bd9Sstevel@tonic-gate 	default:
3087c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Unrecognized internal syslog level code %d\n",
3097c478bd9Sstevel@tonic-gate 		    (int) level);
3107c478bd9Sstevel@tonic-gate 		exit(1);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	log_on_stderr = on_stderr;
3147c478bd9Sstevel@tonic-gate 	if (on_stderr)
3157c478bd9Sstevel@tonic-gate 		return;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	switch (facility) {
3187c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_DAEMON:
3197c478bd9Sstevel@tonic-gate 		log_facility = LOG_DAEMON;
3207c478bd9Sstevel@tonic-gate 		break;
3217c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_USER:
3227c478bd9Sstevel@tonic-gate 		log_facility = LOG_USER;
3237c478bd9Sstevel@tonic-gate 		break;
3247c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_AUTH:
3257c478bd9Sstevel@tonic-gate 		log_facility = LOG_AUTH;
3267c478bd9Sstevel@tonic-gate 		break;
3277c478bd9Sstevel@tonic-gate #ifdef LOG_AUTHPRIV
3287c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_AUTHPRIV:
3297c478bd9Sstevel@tonic-gate 		log_facility = LOG_AUTHPRIV;
3307c478bd9Sstevel@tonic-gate 		break;
3317c478bd9Sstevel@tonic-gate #endif
3327c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL0:
3337c478bd9Sstevel@tonic-gate 		log_facility = LOG_LOCAL0;
3347c478bd9Sstevel@tonic-gate 		break;
3357c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL1:
3367c478bd9Sstevel@tonic-gate 		log_facility = LOG_LOCAL1;
3377c478bd9Sstevel@tonic-gate 		break;
3387c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL2:
3397c478bd9Sstevel@tonic-gate 		log_facility = LOG_LOCAL2;
3407c478bd9Sstevel@tonic-gate 		break;
3417c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL3:
3427c478bd9Sstevel@tonic-gate 		log_facility = LOG_LOCAL3;
3437c478bd9Sstevel@tonic-gate 		break;
3447c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL4:
3457c478bd9Sstevel@tonic-gate 		log_facility = LOG_LOCAL4;
3467c478bd9Sstevel@tonic-gate 		break;
3477c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL5:
3487c478bd9Sstevel@tonic-gate 		log_facility = LOG_LOCAL5;
3497c478bd9Sstevel@tonic-gate 		break;
3507c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL6:
3517c478bd9Sstevel@tonic-gate 		log_facility = LOG_LOCAL6;
3527c478bd9Sstevel@tonic-gate 		break;
3537c478bd9Sstevel@tonic-gate 	case SYSLOG_FACILITY_LOCAL7:
3547c478bd9Sstevel@tonic-gate 		log_facility = LOG_LOCAL7;
3557c478bd9Sstevel@tonic-gate 		break;
3567c478bd9Sstevel@tonic-gate 	default:
3577c478bd9Sstevel@tonic-gate 		fprintf(stderr,
3587c478bd9Sstevel@tonic-gate 		    "Unrecognized internal syslog facility code %d\n",
3597c478bd9Sstevel@tonic-gate 		    (int) facility);
3607c478bd9Sstevel@tonic-gate 		exit(1);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate #define MSGBUFSIZ 1024
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate /* PRINTFLIKE2 */
3677c478bd9Sstevel@tonic-gate void
do_log(LogLevel level,const char * fmt,va_list args)3687c478bd9Sstevel@tonic-gate do_log(LogLevel level, const char *fmt, va_list args)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	char msgbuf[MSGBUFSIZ];
3717c478bd9Sstevel@tonic-gate 	char fmtbuf[MSGBUFSIZ];
3727c478bd9Sstevel@tonic-gate 	char *txt = NULL;
3737c478bd9Sstevel@tonic-gate 	int pri = LOG_INFO;
3747c478bd9Sstevel@tonic-gate 	int do_gettext = log_on_stderr; /*
3757c478bd9Sstevel@tonic-gate 					 * Localize user messages - not
3767c478bd9Sstevel@tonic-gate 					 * syslog()ed messages.
3777c478bd9Sstevel@tonic-gate 					 */
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (level > log_level)
3807c478bd9Sstevel@tonic-gate 		return;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	switch (level) {
3837c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_FATAL:
3847c478bd9Sstevel@tonic-gate 		if (!log_on_stderr)
3857c478bd9Sstevel@tonic-gate 			txt = "fatal";
3867c478bd9Sstevel@tonic-gate 		pri = LOG_CRIT;
3877c478bd9Sstevel@tonic-gate 		break;
3887c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_ERROR:
3897c478bd9Sstevel@tonic-gate 		if (!log_on_stderr)
3907c478bd9Sstevel@tonic-gate 			txt = "error";
3917c478bd9Sstevel@tonic-gate 		pri = LOG_ERR;
3927c478bd9Sstevel@tonic-gate 		break;
3937c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_NOTICE:
3947c478bd9Sstevel@tonic-gate 		pri = LOG_NOTICE;
3957c478bd9Sstevel@tonic-gate 		break;
3967c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_INFO:
3977c478bd9Sstevel@tonic-gate 		pri = LOG_INFO;
3987c478bd9Sstevel@tonic-gate 		break;
3997c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_VERBOSE:
4007c478bd9Sstevel@tonic-gate 		pri = LOG_INFO;
4017c478bd9Sstevel@tonic-gate 		break;
4027c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG1:
4037c478bd9Sstevel@tonic-gate 		txt = "debug1";
4047c478bd9Sstevel@tonic-gate 		pri = LOG_DEBUG;
4057c478bd9Sstevel@tonic-gate 		/*
4067c478bd9Sstevel@tonic-gate 		 * Don't localize debug messages - such are not intended
4077c478bd9Sstevel@tonic-gate 		 * for users but for support staff whose preferred
4087c478bd9Sstevel@tonic-gate 		 * language is unknown, therefore we default to the
4097c478bd9Sstevel@tonic-gate 		 * language used in the source code: English.
4107c478bd9Sstevel@tonic-gate 		 */
4117c478bd9Sstevel@tonic-gate 		do_gettext = 0;
4127c478bd9Sstevel@tonic-gate 		break;
4137c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG2:
4147c478bd9Sstevel@tonic-gate 		txt = "debug2";
4157c478bd9Sstevel@tonic-gate 		pri = LOG_DEBUG;
4167c478bd9Sstevel@tonic-gate 		do_gettext = 0;	    /* Don't localize debug messages. */
4177c478bd9Sstevel@tonic-gate 		break;
4187c478bd9Sstevel@tonic-gate 	case SYSLOG_LEVEL_DEBUG3:
4197c478bd9Sstevel@tonic-gate 		txt = "debug3";
4207c478bd9Sstevel@tonic-gate 		pri = LOG_DEBUG;
4217c478bd9Sstevel@tonic-gate 		do_gettext = 0;	    /* Don't localize debug messages. */
4227c478bd9Sstevel@tonic-gate 		break;
4237c478bd9Sstevel@tonic-gate 	default:
4247c478bd9Sstevel@tonic-gate 		txt = "internal error";
4257c478bd9Sstevel@tonic-gate 		pri = LOG_ERR;
4267c478bd9Sstevel@tonic-gate 		break;
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 	if (txt != NULL) {
4297c478bd9Sstevel@tonic-gate 		snprintf(fmtbuf, sizeof(fmtbuf), "%s%s: %s", log_txt_prefix,
4307c478bd9Sstevel@tonic-gate 			 do_gettext ? gettext(txt) : txt,
4317c478bd9Sstevel@tonic-gate 			 do_gettext ? gettext(fmt) : fmt);
4327c478bd9Sstevel@tonic-gate 		vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args);
4337c478bd9Sstevel@tonic-gate 	} else {
4347c478bd9Sstevel@tonic-gate 		vsnprintf(msgbuf, sizeof(msgbuf),
4357c478bd9Sstevel@tonic-gate 			  do_gettext ? gettext(fmt) : fmt,
4367c478bd9Sstevel@tonic-gate 			  args);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 	if (log_on_stderr) {
4397c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s\r\n", msgbuf);
4407c478bd9Sstevel@tonic-gate 	} else {
4417c478bd9Sstevel@tonic-gate 		openlog(argv0 ? argv0 : __progname, LOG_PID, log_facility);
4427c478bd9Sstevel@tonic-gate 		syslog(pri, "%.500s", msgbuf);
4437c478bd9Sstevel@tonic-gate 		closelog();
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate }
446