syslog.c2
00001 /* Copyright (c) 1983, 1988, 1993 00002 * The Regents of the University of California. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * 1. Redistributions of source code must retain the above copyright 00008 * notice, this list of conditions and the following disclaimer. 00009 * 2. Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * 3. All advertising materials mentioning features or use of this software 00013 * must display the following acknowledgement: 00014 * This product includes software developed by the University of 00015 * California, Berkeley and its contributors. 00016 * 4. Neither the name of the University nor the names of its contributors 00017 * may be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00021 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00024 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00025 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00026 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00027 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00028 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00029 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00030 * SUCH DAMAGE. 00031 * 00032 * #if defined(LIBC_SCCS) && !defined(lint) 00033 * static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; 00034 * #endif 00035 * 00036 * Author: Eric Allman 00037 * Modified to use UNIX domain IPC by Ralph Campbell 00038 * Patched March 12, 1996 by A. Ian Vogelesang <vogelesang@hdshq.com> 00039 * Rewritten by Martin Mares <mj@atrey.karlin.mff.cuni.cz> on May 14, 1997 00040 * Rewritten by G. Falzoni <gfalzoni@inwind.it> for porting to Minix 00041 * 00042 * $Id: syslog.c,v 1.1 2006/04/03 13:07:42 beng Exp $ 00043 */ 00044 #include <sys/types.h> 00045 #include <stdlib.h> 00046 #include <stdio.h> 00047 #include <string.h> 00048 #include <stdarg.h> 00049 #include <time.h> 00050 #include <fcntl.h> 00051 #include <unistd.h> 00052 #include <syslog.h> 00053 #include <sys/ioctl.h> 00054 #include <net/netlib.h> 00055 #include <net/hton.h> 00056 #include <net/gen/in.h> 00057 #include <net/gen/udp.h> 00058 #include <net/gen/udp_io.h> 00059 #include <net/gen/netdb.h> 00060 #include <errno.h> 00061 #include <net/gen/inet.h> 00062 00063 static int LogPid = (-1); 00064 static int nfd = (-1); 00065 static int LogFacility = LOG_USER; 00066 static int LogFlags = 0; 00067 static char TagBuffer[40] = "syslog"; 00068 00069 /* 00070 ** OPENLOG -- open system log 00071 ** - establishes a channel to syslogd using UDP device 00072 ** (port 514 is used _ syslog/udp) 00073 ** - stores program tag (if not NULL) and other options 00074 ** for use by syslog 00075 */ 00076 void openlog(const char *ident, int option, int facility) 00077 { 00078 struct nwio_udpopt udpopt; 00079 00080 /* Stores logging flags */ 00081 LogFlags = option & (LOG_PID | LOG_PERROR | LOG_CONS); 00082 /* Stores process id. if LOG_PID was specified */ 00083 if (option & LOG_PID) LogPid = getpid(); 00084 /* Stores the requested facility */ 00085 LogFacility = facility; 00086 /* Stores log tag if supplied */ 00087 if (ident != NULL && *ident != '0' && ident != TagBuffer) { 00088 strncpy(TagBuffer, ident, sizeof(TagBuffer)); 00089 TagBuffer[sizeof(TagBuffer) - 1] = '0'; 00090 } 00091 00092 /* Opens channel to syslog daemon via UDP device */ 00093 /* Static values used to minimize code */ 00094 if (option & LOG_NDELAY) { 00095 /* Opens UDP device */ 00096 if ((nfd = open(UDP_DEVICE, O_RDWR)) < 0) { 00097 /* Report error */ ; 00098 } 00099 /* Sets options for UDP device */ 00100 udpopt.nwuo_flags = NWUO_SHARED | NWUO_LP_SET | NWUO_DI_LOC | 00101 NWUO_DI_BROAD | NWUO_RP_SET | NWUO_RA_SET | 00102 NWUO_RWDATONLY | NWUO_DI_IPOPT; 00103 udpopt.nwuo_locaddr = udpopt.nwuo_remaddr = htonl(0x7F000001L); 00104 udpopt.nwuo_locport = udpopt.nwuo_remport = htons(514); 00105 if (ioctl(nfd, NWIOSUDPOPT, &udpopt) < 0 || 00106 ioctl(nfd, NWIOGUDPOPT, &udpopt) < 0) { 00107 /* Report error */ ; 00108 } 00109 } 00110 return; 00111 } 00112 00113 /* 00114 ** SYSLOG -- print message on log file 00115 ** 00116 ** This routine looks a lot like printf, except that it outputs to the 00117 ** log file instead of the standard output. Also: 00118 ** - adds a timestamp, 00119 ** - prints the module name in front of the message, 00120 ** - has some other formatting types (or will sometime), 00121 ** - adds a newline on the end of the message. 00122 ** 00123 ** The output of this routine is intended to be read by syslogd(8). 00124 */ 00125 void syslog(int lprty, const char *msg,...) 00126 { 00127 time_t now; 00128 char buff[512]; 00129 int len, rc; 00130 va_list ap; 00131 00132 /* First log message open chnnel to syslog */ 00133 if (nfd < 0) openlog(TagBuffer, LogFlags | LOG_NDELAY, LogFacility); 00134 time(&now); 00135 len = sprintf(buff, "<%d>%.15s %s: ", 00136 LogFacility | lprty, ctime(&now) + 4, TagBuffer); 00137 if (LogFlags & LOG_PID) { 00138 len -= 2; 00139 len += sprintf(buff + len, "[%d]: ", LogPid); 00140 } 00141 va_start(ap, msg); 00142 len += vsprintf(buff + len, msg, ap); 00143 va_end(ap); 00144 rc = write(nfd, buff, len); 00145 if ((rc != len && LogFlags & LOG_CONS) || LogFlags & LOG_PERROR) { 00146 write(STDERR_FILENO, buff, len); 00147 write(STDERR_FILENO, "\n", 1); 00148 } 00149 return; 00150 } 00151 00152 /* 00153 ** CLOSELOG -- close access to syslogd 00154 ** - closes UDP channel 00155 ** - restores default values 00156 */ 00157 void closelog(void) 00158 { 00159 00160 close(nfd); 00161 LogPid = nfd = -1; 00162 LogFacility = LOG_USER; 00163 LogFlags = 0; 00164 return; 00165 } 00166
posted on 2012-02-17 17:58 Richard.FreeBSD 阅读(2001) 评论(0) 编辑 收藏 举报