xref: /freebsd/lib/libc/gen/syslog.3 (revision 84ee9401a3fc8d3c22424266f421a928989cd692)
1.\" Copyright (c) 1985, 1991, 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.\"     @(#)syslog.3	8.1 (Berkeley) 6/4/93
33.\" $FreeBSD$
34.\"
35.Dd December 30, 2004
36.Dt SYSLOG 3
37.Os
38.Sh NAME
39.Nm syslog ,
40.Nm vsyslog ,
41.Nm openlog ,
42.Nm closelog ,
43.Nm setlogmask
44.Nd control system log
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.In syslog.h
49.In stdarg.h
50.Ft void
51.Fn syslog "int priority" "const char *message" "..."
52.Ft void
53.Fn vsyslog "int priority" "const char *message" "va_list args"
54.Ft void
55.Fn openlog "const char *ident" "int logopt" "int facility"
56.Ft void
57.Fn closelog void
58.Ft int
59.Fn setlogmask "int maskpri"
60.Sh DESCRIPTION
61The
62.Fn syslog
63function
64writes
65.Fa message
66to the system message logger.
67The message is then written to the system console, log files,
68logged-in users, or forwarded to other machines as appropriate.
69(See
70.Xr syslogd 8 . )
71.Pp
72The message is identical to a
73.Xr printf 3
74format string, except that
75.Ql %m
76is replaced by the current error
77message.
78(As denoted by the global variable
79.Va errno ;
80see
81.Xr strerror 3 . )
82A trailing newline is added if none is present.
83.Pp
84The
85.Fn vsyslog
86function
87is an alternate form in which the arguments have already been captured
88using the variable-length argument facilities of
89.Xr stdarg 3 .
90.Pp
91The message is tagged with
92.Fa priority .
93Priorities are encoded as a
94.Fa facility
95and a
96.Em level .
97The facility describes the part of the system
98generating the message.
99The level is selected from the following
100.Em ordered
101(high to low) list:
102.Bl -tag -width LOG_AUTHPRIV
103.It Dv LOG_EMERG
104A panic condition.
105This is normally broadcast to all users.
106.It Dv LOG_ALERT
107A condition that should be corrected immediately, such as a corrupted
108system database.
109.It Dv LOG_CRIT
110Critical conditions, e.g., hard device errors.
111.It Dv LOG_ERR
112Errors.
113.It Dv LOG_WARNING
114Warning messages.
115.It Dv LOG_NOTICE
116Conditions that are not error conditions,
117but should possibly be handled specially.
118.It Dv LOG_INFO
119Informational messages.
120.It Dv LOG_DEBUG
121Messages that contain information
122normally of use only when debugging a program.
123.El
124.Pp
125The
126.Fn openlog
127function
128provides for more specialized processing of the messages sent
129by
130.Fn syslog
131and
132.Fn vsyslog .
133The
134.Fa ident
135argument
136is a string that will be prepended to every message.
137The
138.Fa logopt
139argument
140is a bit field specifying logging options, which is formed by
141.Tn OR Ns 'ing
142one or more of the following values:
143.Bl -tag -width LOG_AUTHPRIV
144.It Dv LOG_CONS
145If
146.Fn syslog
147cannot pass the message to
148.Xr syslogd 8
149it will attempt to write the message to the console
150.Pq Dq Pa /dev/console .
151.It Dv LOG_NDELAY
152Open the connection to
153.Xr syslogd 8
154immediately.
155Normally the open is delayed until the first message is logged.
156Useful for programs that need to manage the order in which file
157descriptors are allocated.
158.It Dv LOG_PERROR
159Write the message to standard error output as well to the system log.
160.It Dv LOG_PID
161Log the process id with each message: useful for identifying
162instantiations of daemons.
163.El
164.Pp
165The
166.Fa facility
167argument encodes a default facility to be assigned to all messages
168that do not have an explicit facility encoded:
169.Bl -tag -width LOG_AUTHPRIV
170.It Dv LOG_AUTH
171The authorization system:
172.Xr login 1 ,
173.Xr su 1 ,
174.Xr getty 8 ,
175etc.
176.It Dv LOG_AUTHPRIV
177The same as
178.Dv LOG_AUTH ,
179but logged to a file readable only by
180selected individuals.
181.It Dv LOG_CONSOLE
182Messages written to
183.Pa /dev/console
184by the kernel console output driver.
185.It Dv LOG_CRON
186The cron daemon:
187.Xr cron 8 .
188.It Dv LOG_DAEMON
189System daemons, such as
190.Xr routed 8 ,
191that are not provided for explicitly by other facilities.
192.It Dv LOG_FTP
193The file transfer protocol daemons:
194.Xr ftpd 8 ,
195.Xr tftpd 8 .
196.It Dv LOG_KERN
197Messages generated by the kernel.
198These cannot be generated by any user processes.
199.It Dv LOG_LPR
200The line printer spooling system:
201.Xr lpr 1 ,
202.Xr lpc 8 ,
203.Xr lpd 8 ,
204etc.
205.It Dv LOG_MAIL
206The mail system.
207.It Dv LOG_NEWS
208The network news system.
209.It Dv LOG_NTP
210The network time protocol system.
211.It Dv LOG_SECURITY
212Security subsystems, such as
213.Xr ipfw 4 .
214.It Dv LOG_SYSLOG
215Messages generated internally by
216.Xr syslogd 8 .
217.It Dv LOG_USER
218Messages generated by random user processes.
219This is the default facility identifier if none is specified.
220.It Dv LOG_UUCP
221The uucp system.
222.It Dv LOG_LOCAL0
223Reserved for local use.
224Similarly for
225.Dv LOG_LOCAL1
226through
227.Dv LOG_LOCAL7 .
228.El
229.Pp
230The
231.Fn closelog
232function
233can be used to close the log file.
234.Pp
235The
236.Fn setlogmask
237function
238sets the log priority mask to
239.Fa maskpri
240and returns the previous mask.
241Calls to
242.Fn syslog
243with a priority not set in
244.Fa maskpri
245are rejected.
246The mask for an individual priority
247.Fa pri
248is calculated by the macro
249.Fn LOG_MASK pri ;
250the mask for all priorities up to and including
251.Fa toppri
252is given by the macro
253.Fn LOG_UPTO toppri ; .
254The default allows all priorities to be logged.
255.Sh RETURN VALUES
256The routines
257.Fn closelog ,
258.Fn openlog ,
259.Fn syslog
260and
261.Fn vsyslog
262return no value.
263.Pp
264The routine
265.Fn setlogmask
266always returns the previous log mask level.
267.Sh EXAMPLES
268.Bd -literal -offset indent -compact
269syslog(LOG_ALERT, "who: internal error 23");
270
271openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
272
273setlogmask(LOG_UPTO(LOG_ERR));
274
275syslog(LOG_INFO, "Connection from host %d", CallingHost);
276
277syslog(LOG_INFO|LOG_LOCAL2, "foobar error: %m");
278.Ed
279.Sh SEE ALSO
280.Xr logger 1 ,
281.Xr syslogd 8
282.Sh HISTORY
283These
284functions appeared in
285.Bx 4.2 .
286.Sh BUGS
287Never pass a string with user-supplied data as a format without using
288.Ql %s .
289An attacker can put format specifiers in the string to mangle your stack,
290leading to a possible security hole.
291This holds true even if the string was built using a function like
292.Fn snprintf ,
293as the resulting string may still contain user-supplied conversion specifiers
294for later interpolation by
295.Fn syslog .
296.Pp
297Always use the proper secure idiom:
298.Pp
299.Dl syslog("%s", string);
300