xref: /openbsd-src/lib/libc/gen/syslog.3 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: syslog.3,v 1.12 2001/07/27 16:41:20 marc Exp $
2.\"
3.\" Copyright (c) 1985, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.Dd June 4, 1993
35.Dt SYSLOG 3
36.Os
37.Sh NAME
38.Nm syslog ,
39.Nm vsyslog ,
40.Nm openlog ,
41.Nm closelog ,
42.Nm setlogmask
43.Nd control system log
44.Sh SYNOPSIS
45.Fd #include <syslog.h>
46.Fd #include <stdarg.h>
47.Ft void
48.Fn syslog "int priority" "const char *message" "..."
49.Ft void
50.Fn vsyslog "int priority" "const char *message" "va_list args"
51.Ft void
52.Fn openlog "const char *ident" "int logopt" "int facility"
53.Ft void
54.Fn closelog void
55.Ft int
56.Fn setlogmask "int maskpri"
57.Sh DESCRIPTION
58The
59.Fn syslog
60function writes
61.Fa message
62to the system message logger.
63The message is then written to the system console, log files,
64logged-in users, or forwarded to other machines as appropriate (see
65.Xr syslogd 8 ) .
66.Pp
67The message is identical to a
68.Xr printf 3
69format string, except that
70.Ql %m
71is replaced by the current error
72message (as denoted by the global variable
73.Va errno ;
74see
75.Xr strerror 3 ) .
76A trailing newline is added if none is present.
77.Pp
78The
79.Fn vsyslog
80function is an alternate form in which the arguments have already been captured
81using the variable-length argument facilities of
82.Xr varargs 3 .
83.Pp
84The message is tagged with
85.Fa priority .
86Priorities are encoded as a
87.Fa facility
88and a
89.Dq level .
90The facility describes the part of the system
91generating the message.
92The level is selected from the following
93.Em ordered
94(high to low) list:
95.Bl -tag -width LOG_AUTHPRIV
96.It Dv LOG_EMERG
97A panic condition.
98This is normally broadcast to all users.
99.It Dv LOG_ALERT
100A condition that should be corrected immediately, such as a corrupted
101system database.
102.It Dv LOG_CRIT
103Critical conditions, e.g., hard device errors.
104.It Dv LOG_ERR
105Errors.
106.It Dv LOG_WARNING
107Warning messages.
108.It Dv LOG_NOTICE
109Conditions that are not error conditions,
110but should possibly be handled specially.
111.It Dv LOG_INFO
112Informational messages.
113.It Dv LOG_DEBUG
114Messages that contain information
115normally of use only when debugging a program.
116.El
117.Pp
118The
119.Fn openlog
120function provides for more specialized processing of the messages sent by
121.Fn syslog
122and
123.Fn vsyslog .
124The parameter
125.Fa ident
126is a string that will be prepended to every message.
127The
128.Fa logopt
129argument
130is a bit field specifying logging options, which is formed by
131.Tn OR Ns 'ing
132one or more of the following values:
133.Bl -tag -width LOG_AUTHPRIV
134.It Dv LOG_CONS
135If
136.Fn syslog
137cannot pass the message to
138.Xr syslogd
139it will attempt to write the message to the console
140.Pq Pa /dev/console .
141.It Dv LOG_NDELAY
142Open the connection to
143.Xr syslogd
144immediately.
145Normally the open is delayed until the first message is logged.
146Useful for programs that need to manage the order in which file
147descriptors are allocated.
148.It Dv LOG_PERROR
149Write the message to standard error output as well to the system log.
150.It Dv LOG_PID
151Log the process ID with each message; useful for identifying
152instantiations of daemons.
153.El
154.Pp
155The
156.Fa facility
157parameter encodes a default facility to be assigned to all messages
158that do not have an explicit facility encoded:
159.Bl -tag -width LOG_AUTHPRIV
160.It Dv LOG_AUTH
161The authorization system:
162.Xr login 1 ,
163.Xr su 1 ,
164.Xr getty 8 ,
165etc.
166.It Dv LOG_AUTHPRIV
167The same as
168.Dv LOG_AUTH ,
169but logged to a file readable only by
170selected individuals.
171.It Dv LOG_CRON
172The cron daemon,
173.Xr cron 8 .
174.It Dv LOG_DAEMON
175System daemons, such as
176.Xr routed 8 ,
177that are not provided for explicitly by other facilities.
178.It Dv LOG_FTP
179The file transfer protocol daemon,
180.Xr ftpd 8 .
181.It Dv LOG_KERN
182Messages generated by the kernel.
183These cannot be generated by any user processes.
184.It Dv LOG_LPR
185The line printer spooling system:
186.Xr lpr 1 ,
187.Xr lpc 8 ,
188.Xr lpd 8 ,
189etc.
190.It Dv LOG_MAIL
191The mail system.
192.It Dv LOG_NEWS
193The network news system.
194.It Dv LOG_SYSLOG
195Messages generated internally by
196.Xr syslogd 8 .
197.It Dv LOG_USER
198Messages generated by random user processes.
199This is the default facility identifier if none is specified.
200.It Dv LOG_UUCP
201The
202.Tn UUCP
203system.
204.It Dv LOG_LOCAL0
205Reserved for local use.
206Similarly for
207.Dv LOG_LOCAL1
208through
209.Dv LOG_LOCAL7 .
210.El
211.Pp
212The
213.Fn closelog
214function can be used to close the log file.
215.Pp
216The
217.Fn setlogmask
218function sets the log priority mask to
219.Fa maskpri
220and returns the previous mask.
221Calls to
222.Fn syslog
223with a priority not set in
224.Fa maskpri
225are rejected.
226The mask for an individual priority
227.Fa pri
228is calculated by the macro
229.Fn LOG_MASK pri ;
230the mask for all priorities up to and including
231.Fa toppri
232is given by the macro
233.Fn LOG_UPTO toppri ; .
234The default allows all priorities to be logged.
235.Sh RETURN VALUES
236The
237.Fn closelog ,
238.Fn openlog ,
239.Fn syslog ,
240and
241.Fn vsyslog
242functions return no value.
243.Pp
244The routine
245.Fn setlogmask
246always returns the previous log mask level.
247.Sh EXAMPLES
248.Bd -literal -offset indent -compact
249syslog(LOG_ALERT, "who: internal error 23");
250
251openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
252
253setlogmask(LOG_UPTO(LOG_ERR));
254
255syslog(LOG_INFO, "Connection from host %d", CallingHost);
256
257syslog(LOG_INFO|LOG_LOCAL2, "foobar error: %m");
258.Ed
259.Sh SEE ALSO
260.Xr logger 1 ,
261.Xr syslogd 8
262.Sh HISTORY
263These
264functions appeared in
265.Bx 4.2 .
266.Sh CAVEATS
267It is important never to pass a string with user-supplied data as a
268format without using
269.Ql %s .
270An attacker can put format specifiers in the string to mangle your stack,
271leading to a possible security hole.
272This holds true even if you have built the string
273.Dq by hand
274using a function like
275.Fn snprintf ,
276as the resulting string may still contain user-supplied conversion specifiers
277for later interpolation by
278.Fn syslog .
279.Pp
280Always be sure to use the proper secure idiom:
281.Bd -literal -offset indent
282syslog("%s", string);
283.Ed
284