xref: /openbsd-src/lib/libc/gen/syslog.3 (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1.\"	$OpenBSD: syslog.3,v 1.34 2017/08/29 18:23:01 millert 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. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: August 29 2017 $
31.Dt SYSLOG 3
32.Os
33.Sh NAME
34.Nm syslog ,
35.Nm syslog_r ,
36.Nm vsyslog ,
37.Nm vsyslog_r ,
38.Nm openlog ,
39.Nm openlog_r ,
40.Nm closelog ,
41.Nm closelog_r ,
42.Nm setlogmask ,
43.Nm setlogmask_r
44.Nd control system log
45.Sh SYNOPSIS
46.In syslog.h
47.In stdarg.h
48.Ft void
49.Fn syslog "int priority" "const char *message" "..."
50.Ft void
51.Fn syslog_r "int priority" "struct syslog_data *data" "const char *message" "..."
52.Ft void
53.Fn vsyslog "int priority" "const char *message" "va_list args"
54.Ft void
55.Fn vsyslog_r "int priority" "struct syslog_data *data" "const char *message" "va_list args"
56.Ft void
57.Fn openlog "const char *ident" "int logopt" "int facility"
58.Ft void
59.Fn openlog_r "const char *ident" "int logopt" "int facility" "struct syslog_data *data"
60.Ft void
61.Fn closelog void
62.Ft void
63.Fn closelog_r "struct syslog_data *data"
64.Ft int
65.Fn setlogmask "int maskpri"
66.Ft int
67.Fn setlogmask_r "int maskpri" "struct syslog_data *data"
68.Sh DESCRIPTION
69The
70.Fn syslog
71function writes
72.Fa message
73to the system message logger.
74The message is then written to the system console, log files,
75logged-in users, or forwarded to other machines as appropriate (see
76.Xr syslogd 8 ) .
77.Pp
78The message is identical to a
79.Xr printf 3
80format string, except that
81.Ql %m
82is replaced by the current error
83message (as denoted by the global variable
84.Va errno ;
85see
86.Xr strerror 3 ) .
87A trailing newline is added if none is present.
88.Pp
89The
90.Fn syslog_r
91function is a reentrant version of the
92.Fn syslog
93function.
94It takes a pointer to a
95.Fa syslog_data
96structure which is used to store
97information.
98This parameter must be initialized before
99.Fn syslog_r
100is called.
101The
102.Dv SYSLOG_DATA_INIT
103constant is used for this purpose.
104.Pp
105The
106.Fn vsyslog
107function is an alternate form in which the arguments have already been captured
108using the variable-length argument facilities of
109.Xr stdarg 3 .
110.Pp
111The message is tagged with
112.Fa priority .
113Priorities are encoded as a
114.Fa facility
115and a
116.Fa level .
117The
118.Fa facility
119describes the part of the system
120generating the message:
121.Bl -tag -width LOG_AUTHPRIV
122.It Dv LOG_AUTH
123The authorization system:
124.Xr login 1 ,
125.Xr su 1 ,
126.Xr getty 8 ,
127etc.
128.It Dv LOG_AUTHPRIV
129The same as
130.Dv LOG_AUTH ,
131but logged to a file readable only by
132selected individuals.
133.It Dv LOG_CRON
134The cron daemon,
135.Xr cron 8 .
136.It Dv LOG_DAEMON
137System daemons, such as
138.Xr dhcpd 8 ,
139that are not provided for explicitly by other facilities.
140.It Dv LOG_FTP
141The file transfer protocol daemon,
142.Xr ftpd 8 .
143.It Dv LOG_KERN
144Messages generated by the kernel.
145These cannot be generated by any user processes.
146.It Dv LOG_LPR
147The line printer spooling system:
148.Xr lpr 1 ,
149.Xr lpc 8 ,
150.Xr lpd 8 ,
151etc.
152.It Dv LOG_MAIL
153The mail system.
154.It Dv LOG_NEWS
155The network news system.
156.It Dv LOG_SYSLOG
157Messages generated internally by
158.Xr syslogd 8 .
159.It Dv LOG_USER
160Messages generated by random user processes.
161This is the default facility identifier if none is specified.
162.It Dv LOG_UUCP
163The UUCP system.
164.It Dv LOG_LOCAL0
165Reserved for local use.
166Similarly for
167.Dv LOG_LOCAL1
168through
169.Dv LOG_LOCAL7 .
170.El
171.Pp
172The
173.Fa level
174(ORed with the
175.Fa facility )
176is selected from the following list, ordered by decreasing importance:
177.Bl -tag -width LOG_AUTHPRIV
178.It Dv LOG_EMERG
179A panic condition.
180This is normally broadcast to all users.
181.It Dv LOG_ALERT
182A condition that should be corrected immediately, such as a corrupted
183system database.
184.It Dv LOG_CRIT
185Critical conditions, e.g., hard device errors.
186.It Dv LOG_ERR
187Errors.
188.It Dv LOG_WARNING
189Warning messages.
190.It Dv LOG_NOTICE
191Conditions that are not error conditions,
192but should possibly be handled specially.
193.It Dv LOG_INFO
194Informational messages.
195.It Dv LOG_DEBUG
196Messages that contain information
197normally of use only when debugging a program.
198.El
199.Pp
200The
201.Fn vsyslog_r
202function is used the same way as
203.Fn vsyslog
204except that it takes an additional pointer to a
205.Fa syslog_data
206structure.
207It is a reentrant version of the
208.Fn vsyslog
209function described above.
210.Pp
211The
212.Fn openlog
213function provides for more specialized processing of the messages sent by
214.Fn syslog
215and
216.Fn vsyslog .
217The parameter
218.Fa ident
219is a string that will be prepended to every message.
220The
221.Fa logopt
222argument
223is a bit field specifying logging options, which is formed by
224.Tn OR Ns 'ing
225one or more of the following values:
226.Bl -tag -width LOG_AUTHPRIV
227.It Dv LOG_CONS
228If
229.Fn syslog
230cannot pass the message to
231.Xr syslogd 8
232it will attempt to write the message to the console
233.Pq Pa /dev/console .
234.It Dv LOG_NDELAY
235Open the connection to
236.Xr syslogd 8
237immediately.
238Normally the open is delayed until the first message is logged.
239Useful for programs that need to manage the order in which file
240descriptors are allocated.
241This option must be used in programs that call
242.Xr chroot 2
243where the new root does not have its own log socket.
244.It Dv LOG_ODELAY
245Delay opening the connection to
246.Xr syslogd 8
247until the first message is logged.
248This is the opposite of
249.Dv LOG_NDELAY
250and is the default behaviour when neither option is specified.
251.It Dv LOG_PERROR
252Write the message to standard error output as well as to the system log.
253.It Dv LOG_PID
254Log the process ID with each message; useful for identifying
255instantiations of daemons.
256.El
257.Pp
258The
259.Fa facility
260parameter encodes a default facility to be assigned to all messages
261that do not have an explicit facility encoded.
262.Pp
263The
264.Fn openlog_r
265function is the reentrant version of the
266.Fn openlog
267function.
268It takes an additional pointer to a
269.Fa syslog_data
270structure.
271This function must be used in conjunction with the other
272reentrant functions.
273.Pp
274The
275.Fn closelog
276function can be used to close the log file.
277.Fn closelog_r
278does the same thing but in a reentrant way and takes an additional
279pointer to a
280.Fa syslog_data
281structure.
282.Pp
283The
284.Fn setlogmask
285function sets the log priority mask to
286.Fa maskpri
287and returns the previous mask.
288Calls to
289.Fn syslog
290with a priority not set in
291.Fa maskpri
292are rejected.
293The mask for an individual priority
294.Fa pri
295is calculated by the macro
296.Fn LOG_MASK pri ;
297the mask for all priorities up to and including
298.Fa toppri
299is given by the macro
300.Fn LOG_UPTO toppri .
301The default allows all priorities to be logged, which
302corresponds to
303.Li setlogmask(LOG_UPTO(LOG_DEBUG)) .
304.Pp
305The
306.Fn setlogmask_r
307function is the reentrant version of
308.Fn setlogmask .
309It takes an additional pointer to a
310.Fa syslog_data
311structure.
312.Sh RETURN VALUES
313The
314.Fn closelog ,
315.Fn closelog_r ,
316.Fn openlog ,
317.Fn openlog_r ,
318.Fn syslog ,
319.Fn syslog_r ,
320.Fn vsyslog ,
321and
322.Fn vsyslog_r
323functions return no value.
324.Pp
325The routines
326.Fn setlogmask
327and
328.Fn setlogmask_r
329always return the previous log mask level.
330.Sh EXAMPLES
331.Bd -literal -offset indent
332syslog(LOG_ALERT, "who: internal error 23");
333
334openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
335
336setlogmask(LOG_UPTO(LOG_ERR));
337
338syslog(LOG_INFO, "Connection from host %d", CallingHost);
339
340syslog(LOG_INFO|LOG_LOCAL2, "foobar error: %m");
341.Ed
342.Pp
343For the reentrant functions:
344.Bd -literal -offset indent
345struct syslog_data sdata = SYSLOG_DATA_INIT;
346
347syslog_r(LOG_INFO|LOG_LOCAL2, &sdata, "foobar error: %m");
348.Ed
349.Sh SEE ALSO
350.Xr logger 1 ,
351.Xr syslogd 8
352.Sh HISTORY
353These
354functions appeared in
355.Bx 4.2 .
356The reentrant functions appeared in
357.Ox 3.1 .
358.Sh CAVEATS
359It is important never to pass a string with user-supplied data as a
360format without using
361.Ql %s .
362An attacker can put format specifiers in the string to mangle the stack,
363leading to a possible security hole.
364This holds true even if the string has been built
365.Dq by hand
366using a function like
367.Fn snprintf ,
368as the resulting string may still contain user-supplied conversion specifiers
369for later interpolation by
370.Fn syslog .
371.Pp
372Always be sure to use the proper secure idiom:
373.Bd -literal -offset indent
374syslog(priority, "%s", string);
375.Ed
376.Pp
377.Fn syslog_r
378and the other reentrant functions should only be used where
379reentrancy is required (for instance, in a signal handler).
380.Fn syslog
381being not reentrant, only
382.Fn syslog_r
383should be used here.
384For more information about reentrancy and signal handlers, see
385.Xr signal 3 .
386