source: trunk/minix/commands/syslogd/syslog.c@ 22

Last change on this file since 22 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 5.5 KB
RevLine 
[9]1/* Copyright (c) 1983, 1988, 1993
2 * The Regents of the University of California. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by the University of
15 * California, Berkeley and its contributors.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * #if defined(LIBC_SCCS) && !defined(lint)
33 * static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94";
34 * #endif
35 *
36 * Author: Eric Allman
37 * Modified to use UNIX domain IPC by Ralph Campbell
38 * Patched March 12, 1996 by A. Ian Vogelesang <vogelesang@hdshq.com>
39 * Rewritten by Martin Mares <mj@atrey.karlin.mff.cuni.cz> on May 14, 1997
40 * Rewritten by G. Falzoni <gfalzoni@inwind.it> for porting to Minix
41 *
42 * $Id: syslog.c,v 1.1 2006/04/03 13:07:42 beng Exp $
43 */
44#include <sys/types.h>
45#include <stdlib.h>
46#include <stdio.h>
47#include <string.h>
48#include <stdarg.h>
49#include <time.h>
50#include <fcntl.h>
51#include <unistd.h>
52#include <syslog.h>
53#include <sys/ioctl.h>
54#include <net/netlib.h>
55#include <net/hton.h>
56#include <net/gen/in.h>
57#include <net/gen/udp.h>
58#include <net/gen/udp_io.h>
59#include <net/gen/netdb.h>
60#include <errno.h>
61#include <net/gen/inet.h>
62
63static int LogPid = (-1);
64static int nfd = (-1);
65static int LogFacility = LOG_USER;
66static int LogFlags = 0;
67static char TagBuffer[40] = "syslog";
68
69/*
70** OPENLOG -- open system log
71** - establishes a channel to syslogd using UDP device
72** (port 514 is used _ syslog/udp)
73** - stores program tag (if not NULL) and other options
74** for use by syslog
75*/
76void openlog(const char *ident, int option, int facility)
77{
78 struct nwio_udpopt udpopt;
79
80 /* Stores logging flags */
81 LogFlags = option & (LOG_PID | LOG_PERROR | LOG_CONS);
82 /* Stores process id. if LOG_PID was specified */
83 if (option & LOG_PID) LogPid = getpid();
84 /* Stores the requested facility */
85 LogFacility = facility;
86 /* Stores log tag if supplied */
87 if (ident != NULL && *ident != '0' && ident != TagBuffer) {
88 strncpy(TagBuffer, ident, sizeof(TagBuffer));
89 TagBuffer[sizeof(TagBuffer) - 1] = '0';
90 }
91
92 /* Opens channel to syslog daemon via UDP device */
93 /* Static values used to minimize code */
94 if (option & LOG_NDELAY) {
95 /* Opens UDP device */
96 if ((nfd = open(UDP_DEVICE, O_RDWR)) < 0) {
97 /* Report error */ ;
98 }
99 /* Sets options for UDP device */
100 udpopt.nwuo_flags = NWUO_SHARED | NWUO_LP_SET | NWUO_DI_LOC |
101 NWUO_DI_BROAD | NWUO_RP_SET | NWUO_RA_SET |
102 NWUO_RWDATONLY | NWUO_DI_IPOPT;
103 udpopt.nwuo_locaddr = udpopt.nwuo_remaddr = htonl(0x7F000001L);
104 udpopt.nwuo_locport = udpopt.nwuo_remport = htons(514);
105 if (ioctl(nfd, NWIOSUDPOPT, &udpopt) < 0 ||
106 ioctl(nfd, NWIOGUDPOPT, &udpopt) < 0) {
107 /* Report error */ ;
108 }
109 }
110 return;
111}
112
113/*
114** SYSLOG -- print message on log file
115**
116** This routine looks a lot like printf, except that it outputs to the
117** log file instead of the standard output. Also:
118** - adds a timestamp,
119** - prints the module name in front of the message,
120** - has some other formatting types (or will sometime),
121** - adds a newline on the end of the message.
122**
123** The output of this routine is intended to be read by syslogd(8).
124*/
125void syslog(int lprty, const char *msg,...)
126{
127 time_t now;
128 char buff[512];
129 int len, rc;
130 va_list ap;
131
132 /* First log message open chnnel to syslog */
133 if (nfd < 0) openlog(TagBuffer, LogFlags | LOG_NDELAY, LogFacility);
134 time(&now);
135 len = sprintf(buff, "<%d>%.15s %s: ",
136 LogFacility | lprty, ctime(&now) + 4, TagBuffer);
137 if (LogFlags & LOG_PID) {
138 len -= 2;
139 len += sprintf(buff + len, "[%d]: ", LogPid);
140 }
141 va_start(ap, msg);
142 len += vsprintf(buff + len, msg, ap);
143 va_end(ap);
144 rc = write(nfd, buff, len);
145 if ((rc != len && LogFlags & LOG_CONS) || LogFlags & LOG_PERROR) {
146 write(STDERR_FILENO, buff, len);
147 write(STDERR_FILENO, "\n", 1);
148 }
149 return;
150}
151
152/*
153** CLOSELOG -- close access to syslogd
154** - closes UDP channel
155** - restores default values
156*/
157void closelog(void)
158{
159
160 close(nfd);
161 LogPid = nfd = -1;
162 LogFacility = LOG_USER;
163 LogFlags = 0;
164 return;
165}
166
167/** syslog.c **/
Note: See TracBrowser for help on using the repository browser.