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