1/* $NetBSD: log.c,v 1.9 2007/12/20 20:17:52 christos Exp $ */ 2 3/* 4 * Copyright (c) 1992 Carnegie Mellon University 5 * All Rights Reserved. 6 * 7 * Permission to use, copy, modify and distribute this software and its 8 * documentation is hereby granted, provided that both the copyright 9 * notice and this permission notice appear in all copies of the 10 * software, derivative works or modified versions, and any portions 11 * thereof, and that both notices appear in supporting documentation. 12 * 13 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 14 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 15 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 16 * 17 * Carnegie Mellon requests users of this software to return to 18 * 19 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 20 * School of Computer Science 21 * Carnegie Mellon University 22 * Pittsburgh PA 15213-3890 23 * 24 * any improvements or extensions that they make and grant Carnegie Mellon 25 * the rights to redistribute these changes. 26 */ 27/* 28 * Logging support for SUP 29 ********************************************************************** 30 * HISTORY 31 * Revision 1.5 92/08/11 12:03:43 mrt 32 * Brad's delinting and variable argument list usage 33 * changes. Added copyright. 34 * 35 * Revision 1.3 89/08/15 15:30:37 bww 36 * Updated to use v*printf() in place of _doprnt(). 37 * From "[89/04/19 mja]" at CMU. 38 * [89/08/15 bww] 39 * 40 * 27-Dec-87 Glenn Marcy (gm0w) at Carnegie-Mellon University 41 * Added check to allow logopen() to be called multiple times. 42 * 43 * 20-May-87 Glenn Marcy (gm0w) at Carnegie-Mellon University 44 * Created. 45 * 46 ********************************************************************** 47 */ 48 49#include <stdio.h> 50#include <sys/syslog.h> 51#include "supcdefs.h" 52#include "supextern.h" 53#include "c.h" 54 55static int opened = 0; 56 57void 58logopen(char *program) 59{ 60 if (opened) 61 return; 62 openlog(program, LOG_PID, LOG_DAEMON); 63 opened++; 64} 65 66void 67logquit(int retval, const char *fmt, ...) 68{ 69 char buf[STRINGLENGTH]; 70 va_list ap; 71 72 va_start(ap, fmt); 73 vsnprintf(buf, sizeof(buf), fmt, ap); 74 va_end(ap); 75 if (opened) { 76 syslog(LOG_ERR, "%s", buf); 77 closelog(); 78 exit(retval); 79 } 80 quit(retval, "SUP: %s\n", buf); 81} 82 83void 84logerr(const char *fmt, ...) 85{ 86 char buf[STRINGLENGTH]; 87 va_list ap; 88 89 va_start(ap, fmt); 90 vsnprintf(buf, sizeof(buf), fmt, ap); 91 va_end(ap); 92 if (opened) { 93 syslog(LOG_ERR, "%s", buf); 94 return; 95 } 96 fprintf(stderr, "SUP: %s\n", buf); 97 (void) fflush(stderr); 98} 99 100void 101loginfo(const char *fmt, ...) 102{ 103 char buf[STRINGLENGTH]; 104 va_list ap; 105 106 va_start(ap, fmt); 107 vsnprintf(buf, sizeof(buf), fmt, ap); 108 va_end(ap); 109 if (opened) { 110 syslog(LOG_INFO, "%s", buf); 111 return; 112 } 113 printf("%s\n", buf); 114 (void) fflush(stdout); 115} 116#ifdef LIBWRAP 117#include <tcpd.h> 118#ifndef LIBWRAP_ALLOW_FACILITY 119#define LIBWRAP_ALLOW_FACILITY LOG_AUTH 120#endif 121#ifndef LIBWRAP_ALLOW_SEVERITY 122#define LIBWRAP_ALLOW_SEVERITY LOG_INFO 123#endif 124#ifndef LIBWRAP_DENY_FACILITY 125#define LIBWRAP_DENY_FACILITY LOG_AUTH 126#endif 127#ifndef LIBWRAP_DENY_SEVERITY 128#define LIBWRAP_DENY_SEVERITY LOG_WARNING 129#endif 130int allow_severity = LIBWRAP_ALLOW_FACILITY | LIBWRAP_ALLOW_SEVERITY; 131int deny_severity = LIBWRAP_DENY_FACILITY | LIBWRAP_DENY_SEVERITY; 132 133void 134logdeny(const char *fmt, ...) 135{ 136 char buf[STRINGLENGTH]; 137 va_list ap; 138 139 va_start(ap, fmt); 140 vsnprintf(buf, sizeof(buf), fmt, ap); 141 va_end(ap); 142 if (opened) { 143 syslog(deny_severity, "%s", buf); 144 return; 145 } 146 printf("%s\n", buf); 147 (void) fflush(stdout); 148} 149 150void 151logallow(const char *fmt, ...) 152{ 153 char buf[STRINGLENGTH]; 154 va_list ap; 155 156 va_start(ap, fmt); 157 vsnprintf(buf, sizeof(buf), fmt, ap); 158 va_end(ap); 159 if (opened) { 160 syslog(allow_severity, "%s", buf); 161 return; 162 } 163 printf("%s\n", buf); 164 (void) fflush(stdout); 165} 166#endif /* LIBWRAP */ 167