xref: /netbsd-src/lib/libc/gen/syslog.3 (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1.\"	$NetBSD: syslog.3,v 1.33 2017/07/03 21:32:49 wiz Exp $
2.\"	$OpenBSD: syslog.3,v 1.25 2005/07/22 03:16:58 jaredy Exp $
3.\"
4.\" Copyright (c) 1985, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)syslog.3	8.1 (Berkeley) 6/4/93
32.\"
33.Dd March 22, 2017
34.Dt SYSLOG 3
35.Os
36.Sh NAME
37.Nm syslog ,
38.Nm syslog_r ,
39.Nm vsyslog ,
40.Nm vsyslog_r ,
41.Nm syslogp ,
42.Nm syslogp_r ,
43.Nm vsyslogp ,
44.Nm vsyslogp_r ,
45.Nm openlog ,
46.Nm openlog_r ,
47.Nm closelog ,
48.Nm closelog_r ,
49.Nm setlogmask ,
50.Nm setlogmask_r
51.Nd control system log
52.Sh LIBRARY
53.Lb libc
54.Sh SYNOPSIS
55.In syslog.h
56.Ft void
57.Fn syslog "int priority" "const char *message" "..."
58.Ft void
59.Fn syslog_r "int priority" "struct syslog_data *data" "const char *message" "..."
60.Ft void
61.Fn syslogp "int priority" "const char *msgid" "const char *sdfmt" "const char *message" "..."
62.Ft void
63.Fn syslogp_r "int priority" "struct syslog_data *data" "const char *msgid" "const char *sdfmt" "const char *message" "..."
64.\" .Ft void
65.\" .Fn syslog_ss "int priority" "struct syslog_data *data" "const char *message" "..."
66.Ft void
67.Fn openlog "const char *ident" "int logopt" "int facility"
68.Ft void
69.Fn openlog_r "const char *ident" "int logopt" "int facility" "struct syslog_data *data"
70.Ft void
71.Fn closelog void
72.Ft void
73.Fn closelog_r "struct syslog_data *data"
74.Ft int
75.Fn setlogmask "int maskpri"
76.Ft int
77.Fn setlogmask_r "int maskpri" "struct syslog_data *data"
78.In stdarg.h
79.Ft void
80.Fn vsyslog "int priority" "const char *message" "va_list args"
81.Ft void
82.Fn vsyslog_r "int priority" "struct syslog_data *data" "const char *message" "va_list args"
83.Ft void
84.Fn vsyslogp "int priority" "const char *msgid" "const char *sdfmt" "const char *message" "va_list args"
85.Ft void
86.Fn vsyslogp_r "int priority" "struct syslog_data *data" "const char *msgid" "const char *sdfmt" "const char *message" "va_list args"
87.\" .Ft void
88.\" .Fn vsyslog_ss "int priority" "struct syslog_data *data" "const char *message" "va_list args"
89.Sh DESCRIPTION
90The
91.Fn syslog
92function
93writes
94.Fa message
95to the system message logger.
96The message is then written to the system console, log files,
97logged-in users, or forwarded to other machines as appropriate (see
98.Xr syslogd 8 ) .
99.Pp
100The message is identical to a
101.Xr printf 3
102format string, except that
103.Ql %m
104is replaced by the current error
105message.
106(As denoted by the global variable
107.Va errno ;
108see
109.Xr strerror 3 . )
110A trailing newline is added if none is present.
111.\" shouldn't the newline statement be removed?
112.\" when logging through a socket a newline is
113.\" not added nor is it required. -- ms
114.Pp
115The
116.Fn syslog_r
117function is a multithread-safe version of the
118.Fn syslog
119function.
120It takes a pointer to a
121.Fa syslog_data
122structure which is used to store
123information.
124This parameter must be initialized before
125.Fn syslog_r
126is called.
127The
128.Dv SYSLOG_DATA_INIT
129constant is used for this purpose.
130The
131.Fa syslog_data
132structure and the
133.Dv SYSLOG_DATA_INIT
134constant are defined as:
135.Bd -literal -offset indent
136struct syslog_data {
137        int             log_file;
138        int             connected;
139        int             opened;
140        int             log_stat;
141        const char     *log_tag;
142        int             log_fac;
143        int             log_mask;
144};
145
146#define SYSLOG_DATA_INIT { \e
147    .log_file = -1, \e
148    .log_fac = LOG_USER, \e
149    .log_mask = 0xff, \e
150}
151.Ed
152.Pp
153The structure is composed of the following elements:
154.Bl -tag -width connected -offset indent
155.It Va log_file
156contains the file descriptor of the file where the message is logged
157.It Va connected
158indicates if connect has been done
159.It Va opened
160indicates if
161.Fn openlog_r
162has been called
163.It Va log_stat
164status bits, set by
165.Fn openlog_r
166.It Va log_tag
167string to tag the entry with
168.It Va log_fac
169facility code
170.It Va log_mask
171mask of priorities to be logged
172.El
173.\" .Pp
174.\" The
175.\" .Fn syslog_ss
176.\" is the async-signal-safe version of
177.\" .Fn syslog_r
178.\" and is also multithread-safe.
179.\" It has the following limitations:
180.\" .Bl -enum -offset indent
181.\" .It
182.\" The format string cannot contain multi-byte character sequences.
183.\" .It
184.\" Floating point formats are not supported and print
185.\" .Dq UNK .
186.\" .It
187.\" The time of the event is not sent to
188.\" .Xr syslogd 8 .
189.\" .It
190.\" The error string in the %m format is not printed symbolically but as
191.\" .Dq Error %d .
192.\" .El
193.\" .Pp
194.\" For more information about async-signal-safe functions and signal handlers, see
195.\" .Xr signal 7 .
196.Pp
197The
198.Fn vsyslog
199function
200is an alternative form in which the arguments have already been captured
201using the variable-length argument facilities of
202.Xr stdarg 3 .
203.Pp
204The
205.Fn syslogp
206variants take additional arguments which correspond to new fields in the
207syslog-protocol message format.
208All three arguments are evaluated as
209.Xr printf 3
210format strings and any of them can be
211.Dv NULL .
212This enables applications to use message IDs, structured data, and UTF-8 encoded
213content in messages.
214.Pp
215The message is tagged with
216.Fa priority .
217Priorities are encoded as a
218.Fa facility
219and a
220.Em level .
221The facility describes the part of the system
222generating the message.
223The level is selected from the following
224.Em ordered
225(high to low) list:
226.Bl -tag -width LOG_AUTHPRIV
227.It Dv LOG_EMERG
228A panic condition.
229This is normally broadcast to all users.
230.It Dv LOG_ALERT
231A condition that should be corrected immediately, such as a corrupted
232system database.
233.It Dv LOG_CRIT
234Critical conditions, e.g., hard device errors.
235.It Dv LOG_ERR
236Errors.
237.It Dv LOG_WARNING
238Warning messages.
239.It Dv LOG_NOTICE
240Conditions that are not error conditions,
241but should possibly be handled specially.
242.It Dv LOG_INFO
243Informational messages.
244.It Dv LOG_DEBUG
245Messages that contain information
246normally of use only when debugging a program.
247.El
248.Pp
249The
250.Fn vsyslog_r
251is used the same way as
252.Fn vsyslog
253except that it takes an additional pointer to a
254.Fa syslog_data
255structure.
256It is a multithread-safe version of the
257.Fn vsyslog
258function described above.
259.\" The
260.\" .Fn vsyslog_ss
261.\" is the async-signal-safe version of
262.\" .Fn vsyslog_r ,
263.\" is also multithread-safe, and has the same limitations as
264.\" .Fn syslog_ss .
265.Pp
266The
267.Fn openlog
268function
269provides for more specialized processing of the messages sent
270by
271.Fn syslog
272and
273.Fn vsyslog .
274The parameter
275.Fa ident
276is a string that will be prepended to every message.
277The
278.Fa logopt
279argument
280is a bit field specifying logging options, which is formed by
281.Tn OR Ns 'ing
282one or more of the following values:
283.Bl -tag -width LOG_AUTHPRIV
284.It Dv LOG_CONS
285If
286.Fn syslog
287cannot pass the message to
288.Xr syslogd 8
289it will attempt to write the message to the console
290.Pq Dq Pa /dev/console .
291.It Dv LOG_NDELAY
292Open the connection to
293.Xr syslogd 8
294immediately.
295Normally the open is delayed until the first message is logged.
296Useful for programs that need to manage the order in which file
297descriptors are allocated.
298.It Dv LOG_NLOG
299Stops syslog from writing to the system log.
300Only useful with
301.Dv LOG_PERROR .
302.It Dv LOG_PERROR
303Write the message to standard error output as well to the system log.
304.It Dv LOG_PID
305Log the process id with each message: useful for identifying
306instantiations of daemons.
307(This PID is placed within brackets
308between the ident and the message.)
309.It Dv LOG_PTRIM
310Trim anything syslog added to the message before writing to
311standard error output.
312.El
313.Pp
314The
315.Fa facility
316parameter encodes a default facility to be assigned to all messages
317that do not have an explicit facility encoded:
318.Bl -tag -width LOG_AUTHPRIV
319.It Dv LOG_AUTH
320The authorization system:
321.Xr login 1 ,
322.Xr su 1 ,
323.Xr getty 8 ,
324etc.
325.It Dv LOG_AUTHPRIV
326The same as
327.Dv LOG_AUTH ,
328but logged to a file readable only by
329selected individuals.
330.It Dv LOG_CRON
331The cron daemon:
332.Xr cron 8 .
333.It Dv LOG_DAEMON
334System daemons, such as
335.Xr routed 8 ,
336that are not provided for explicitly by other facilities.
337.It Dv LOG_FTP
338The file transfer protocol daemon:
339.Xr ftpd 8 .
340.It Dv LOG_KERN
341Messages generated by the kernel.
342These cannot be generated by any user processes.
343.It Dv LOG_LPR
344The line printer spooling system:
345.Xr lpr 1 ,
346.Xr lpc 8 ,
347.Xr lpd 8 ,
348etc.
349.It Dv LOG_MAIL
350The mail system.
351.It Dv LOG_NEWS
352The network news system.
353.It Dv LOG_SYSLOG
354Messages generated internally by
355.Xr syslogd 8 .
356.It Dv LOG_USER
357Messages generated by random user processes.
358This is the default facility identifier if none is specified.
359.It Dv LOG_UUCP
360The uucp system.
361.It Dv LOG_LOCAL0
362Reserved for local use.
363Similarly for
364.Dv LOG_LOCAL1
365through
366.Dv LOG_LOCAL7 .
367.El
368.Pp
369The
370.Fn openlog_r
371function is the multithread-safe version of the
372.Fn openlog
373function.
374It takes an additional pointer to a
375.Fa syslog_data
376structure.
377This function must be used in conjunction with the other
378multithread-safe functions.
379.Pp
380The
381.Fn closelog
382function
383can be used to close the log file.
384.Pp
385The
386.Fn closelog_r
387does the same thing as
388.Xr closelog 3
389but in a multithread-safe way and takes an additional
390pointer to a
391.Fa syslog_data
392structure.
393.Pp
394The
395.Fn setlogmask
396function
397sets the log priority mask to
398.Fa maskpri
399and returns the previous mask.
400Calls to
401.Fn syslog
402with a priority not set in
403.Fa maskpri
404are rejected.
405The mask for an individual priority
406.Fa pri
407is calculated by the macro
408.Fn LOG_MASK pri ;
409the mask for all priorities up to and including
410.Fa toppri
411is given by the macro
412.Fn LOG_UPTO toppri .
413The default allows all priorities to be logged.
414.Pp
415The
416.Fn setlogmask_r
417function is the multithread-safe version of
418.Fn setlogmask .
419It takes an additional pointer to a
420.Fa syslog_data
421structure.
422.Sh RETURN VALUES
423The routines
424.Fn closelog ,
425.Fn closelog_r ,
426.Fn openlog ,
427.Fn openlog_r ,
428.Fn syslog ,
429.Fn syslog_r ,
430.Fn vsyslog ,
431.Fn vsyslog_r ,
432.Fn syslogp ,
433.Fn syslogp_r ,
434.Fn vsyslogp ,
435and
436.Fn vsyslogp_r
437return no value.
438.Pp
439The routines
440.Fn setlogmask
441and
442.Fn setlogmask_r
443always return the previous log mask level.
444.Sh EXAMPLES
445.Bd -literal -offset indent -compact
446syslog(LOG_ALERT, "who: internal error 23");
447
448openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
449
450setlogmask(LOG_UPTO(LOG_ERR));
451
452syslog(LOG_INFO, "Connection from host %d", CallingHost);
453
454syslog(LOG_INFO|LOG_LOCAL2, "foobar error: %m");
455
456syslogp(LOG_INFO|LOG_LOCAL2, NULL, NULL, "foobar error: %m");
457
458syslogp(LOG_INFO, "ID%d", "[meta language=\e"en-US\e"]",
459        "event: %s", 42, EventDescription);
460.Ed
461.Pp
462For the multithread-safe functions:
463.Bd -literal -offset indent
464struct syslog_data sdata = SYSLOG_DATA_INIT;
465
466syslog_r(LOG_INFO|LOG_LOCAL2, &sdata, "foobar error: %m");
467.Ed
468.Sh SEE ALSO
469.Xr logger 1 ,
470.Xr syslogd 8
471.Rs
472.%R RFC
473.%N 3164
474.%D August 2001
475.%T The BSD syslog Protocol
476.Re
477.Rs
478.%R Internet-Draft
479.%N draft-ietf-syslog-protocol-23
480.%D September 2007
481.%T The syslog Protocol
482.Re
483.Sh HISTORY
484These non-multithread-safe functions appeared in
485.Bx 4.2 .
486The multithread-safe functions appeared in
487.Ox 3.1
488and then in
489.Nx 4.0 .
490The async-signal-safe functions appeared in
491.Nx 4.0 .
492The syslog-protocol functions appeared in
493.Nx 5.0 .
494.Sh CAVEATS
495It is important never to pass a string with user-supplied data as a
496format without using
497.Ql %s .
498An attacker can put format specifiers in the string to mangle your stack,
499leading to a possible security hole.
500This holds true even if you have built the string
501.Dq by hand
502using a function like
503.Fn snprintf ,
504as the resulting string may still contain user-supplied conversion specifiers
505for later interpolation by
506.Fn syslog .
507.Pp
508Always be sure to use the proper secure idiom:
509.Bd -literal -offset indent
510syslog(priority, "%s", string);
511.Ed
512.Pp
513With
514.Fn syslogp
515the caller is responsible to use the right formatting for the message fields.
516A
517.Fa msgid
518must only contain up to 32 ASCII characters.
519A
520.Fa sdfmt
521has strict rules for parenthesis and character quoting.
522If the
523.Fa msgfmt
524contains UTF-8 characters, then it has to start with a Byte Order Mark.
525