1.\" $OpenBSD: syslog.3,v 1.33 2016/03/30 06:58:06 jmc 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: March 30 2016 $ 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 177.Em ordered 178(high to low) list: 179.Bl -tag -width LOG_AUTHPRIV 180.It Dv LOG_EMERG 181A panic condition. 182This is normally broadcast to all users. 183.It Dv LOG_ALERT 184A condition that should be corrected immediately, such as a corrupted 185system database. 186.It Dv LOG_CRIT 187Critical conditions, e.g., hard device errors. 188.It Dv LOG_ERR 189Errors. 190.It Dv LOG_WARNING 191Warning messages. 192.It Dv LOG_NOTICE 193Conditions that are not error conditions, 194but should possibly be handled specially. 195.It Dv LOG_INFO 196Informational messages. 197.It Dv LOG_DEBUG 198Messages that contain information 199normally of use only when debugging a program. 200.El 201.Pp 202The 203.Fn vsyslog_r 204function is used the same way as 205.Fn vsyslog 206except that it takes an additional pointer to a 207.Fa syslog_data 208structure. 209It is a reentrant version of the 210.Fn vsyslog 211function described above. 212.Pp 213The 214.Fn openlog 215function provides for more specialized processing of the messages sent by 216.Fn syslog 217and 218.Fn vsyslog . 219The parameter 220.Fa ident 221is a string that will be prepended to every message. 222The 223.Fa logopt 224argument 225is a bit field specifying logging options, which is formed by 226.Tn OR Ns 'ing 227one or more of the following values: 228.Bl -tag -width LOG_AUTHPRIV 229.It Dv LOG_CONS 230If 231.Fn syslog 232cannot pass the message to 233.Xr syslogd 8 234it will attempt to write the message to the console 235.Pq Pa /dev/console . 236.It Dv LOG_NDELAY 237Open the connection to 238.Xr syslogd 8 239immediately. 240Normally the open is delayed until the first message is logged. 241Useful for programs that need to manage the order in which file 242descriptors are allocated. 243This option must be used in programs that call 244.Xr chroot 2 245where the new root does not have its own log socket. 246.It Dv LOG_ODELAY 247Delay opening the connection to 248.Xr syslogd 8 249until the first message is logged. 250This is the opposite of 251.Dv LOG_NDELAY 252and is the default behaviour when neither option is specified. 253.It Dv LOG_PERROR 254Write the message to standard error output as well as to the system log. 255.It Dv LOG_PID 256Log the process ID with each message; useful for identifying 257instantiations of daemons. 258.El 259.Pp 260The 261.Fa facility 262parameter encodes a default facility to be assigned to all messages 263that do not have an explicit facility encoded. 264.Pp 265The 266.Fn openlog_r 267function is the reentrant version of the 268.Fn openlog 269function. 270It takes an additional pointer to a 271.Fa syslog_data 272structure. 273This function must be used in conjunction with the other 274reentrant functions. 275.Pp 276The 277.Fn closelog 278function can be used to close the log file. 279.Fn closelog_r 280does the same thing but in a reentrant way and takes an additional 281pointer to a 282.Fa syslog_data 283structure. 284.Pp 285The 286.Fn setlogmask 287function sets the log priority mask to 288.Fa maskpri 289and returns the previous mask. 290Calls to 291.Fn syslog 292with a priority not set in 293.Fa maskpri 294are rejected. 295The mask for an individual priority 296.Fa pri 297is calculated by the macro 298.Fn LOG_MASK pri ; 299the mask for all priorities up to and including 300.Fa toppri 301is given by the macro 302.Fn LOG_UPTO toppri . 303The default allows all priorities to be logged. 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