Blame manual/syslog.texi

Packit 6c4009
@node Syslog, Mathematics, Low-Level Terminal Interface, Top
Packit 6c4009
@c %MENU% System logging and messaging
Packit 6c4009
@chapter Syslog
Packit 6c4009
Packit 6c4009
Packit 6c4009
This chapter describes facilities for issuing and logging messages of
Packit 6c4009
system administration interest.  This chapter has nothing to do with
Packit 6c4009
programs issuing messages to their own users or keeping private logs
Packit 6c4009
(One would typically do that with the facilities described in
Packit 6c4009
@ref{I/O on Streams}).
Packit 6c4009
Packit 6c4009
Most systems have a facility called ``Syslog'' that allows programs to
Packit 6c4009
submit messages of interest to system administrators and can be
Packit 6c4009
configured to pass these messages on in various ways, such as printing
Packit 6c4009
on the console, mailing to a particular person, or recording in a log
Packit 6c4009
file for future reference.
Packit 6c4009
Packit 6c4009
A program uses the facilities in this chapter to submit such messages.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Overview of Syslog::           Overview of a system's Syslog facility
Packit 6c4009
* Submitting Syslog Messages::   Functions to submit messages to Syslog
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Overview of Syslog
Packit 6c4009
@section Overview of Syslog
Packit 6c4009
Packit 6c4009
System administrators have to deal with lots of different kinds of
Packit 6c4009
messages from a plethora of subsystems within each system, and usually
Packit 6c4009
lots of systems as well.  For example, an FTP server might report every
Packit 6c4009
connection it gets.  The kernel might report hardware failures on a disk
Packit 6c4009
drive.  A DNS server might report usage statistics at regular intervals.
Packit 6c4009
Packit 6c4009
Some of these messages need to be brought to a system administrator's
Packit 6c4009
attention immediately.  And it may not be just any system administrator
Packit 6c4009
-- there may be a particular system administrator who deals with a
Packit 6c4009
particular kind of message.  Other messages just need to be recorded for
Packit 6c4009
future reference if there is a problem.  Still others may need to have
Packit 6c4009
information extracted from them by an automated process that generates
Packit 6c4009
monthly reports.
Packit 6c4009
Packit 6c4009
To deal with these messages, most Unix systems have a facility called
Packit 6c4009
"Syslog."  It is generally based on a daemon called ``Syslogd''
Packit 6c4009
Syslogd listens for messages on a Unix domain socket named
Packit 6c4009
@file{/dev/log}.  Based on classification information in the messages
Packit 6c4009
and its configuration file (usually @file{/etc/syslog.conf}), Syslogd
Packit 6c4009
routes them in various ways.  Some of the popular routings are:
Packit 6c4009
Packit 6c4009
@itemize @bullet
Packit 6c4009
@item
Packit 6c4009
Write to the system console
Packit 6c4009
@item
Packit 6c4009
Mail to a specific user
Packit 6c4009
@item
Packit 6c4009
Write to a log file
Packit 6c4009
@item
Packit 6c4009
Pass to another daemon
Packit 6c4009
@item
Packit 6c4009
Discard
Packit 6c4009
@end itemize
Packit 6c4009
Packit 6c4009
Syslogd can also handle messages from other systems.  It listens on the
Packit 6c4009
@code{syslog} UDP port as well as the local socket for messages.
Packit 6c4009
Packit 6c4009
Syslog can handle messages from the kernel itself.  But the kernel
Packit 6c4009
doesn't write to @file{/dev/log}; rather, another daemon (sometimes
Packit 6c4009
called ``Klogd'') extracts messages from the kernel and passes them on to
Packit 6c4009
Syslog as any other process would (and it properly identifies them as
Packit 6c4009
messages from the kernel).
Packit 6c4009
Packit 6c4009
Syslog can even handle messages that the kernel issued before Syslogd or
Packit 6c4009
Klogd was running.  A Linux kernel, for example, stores startup messages
Packit 6c4009
in a kernel message ring and they are normally still there when Klogd
Packit 6c4009
later starts up.  Assuming Syslogd is running by the time Klogd starts,
Packit 6c4009
Klogd then passes everything in the message ring to it.
Packit 6c4009
Packit 6c4009
In order to classify messages for disposition, Syslog requires any process
Packit 6c4009
that submits a message to it to provide two pieces of classification
Packit 6c4009
information with it:
Packit 6c4009
Packit 6c4009
@table @asis
Packit 6c4009
@item facility
Packit 6c4009
This identifies who submitted the message.  There are a small number of
Packit 6c4009
facilities defined.  The kernel, the mail subsystem, and an FTP server
Packit 6c4009
are examples of recognized facilities.  For the complete list,
Packit 6c4009
@xref{syslog; vsyslog}.  Keep in mind that these are
Packit 6c4009
essentially arbitrary classifications.  "Mail subsystem" doesn't have any
Packit 6c4009
more meaning than the system administrator gives to it.
Packit 6c4009
Packit 6c4009
@item priority
Packit 6c4009
This tells how important the content of the message is.  Examples of
Packit 6c4009
defined priority values are: debug, informational, warning and critical.
Packit 6c4009
For the complete list, see @ref{syslog; vsyslog}.  Except for
Packit 6c4009
the fact that the priorities have a defined order, the meaning of each
Packit 6c4009
of these priorities is entirely determined by the system administrator.
Packit 6c4009
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
A ``facility/priority'' is a number that indicates both the facility
Packit 6c4009
and the priority.
Packit 6c4009
Packit 6c4009
@strong{Warning:} This terminology is not universal.  Some people use
Packit 6c4009
``level'' to refer to the priority and ``priority'' to refer to the
Packit 6c4009
combination of facility and priority.  A Linux kernel has a concept of a
Packit 6c4009
message ``level,'' which corresponds both to a Syslog priority and to a
Packit 6c4009
Syslog facility/priority (It can be both because the facility code for
Packit 6c4009
the kernel is zero, and that makes priority and facility/priority the
Packit 6c4009
same value).
Packit 6c4009
Packit 6c4009
@Theglibc{} provides functions to submit messages to Syslog.  They
Packit 6c4009
do it by writing to the @file{/dev/log} socket.  @xref{Submitting Syslog
Packit 6c4009
Messages}.
Packit 6c4009
Packit 6c4009
The @glibcadj{} functions only work to submit messages to the Syslog
Packit 6c4009
facility on the same system.  To submit a message to the Syslog facility
Packit 6c4009
on another system, use the socket I/O functions to write a UDP datagram
Packit 6c4009
to the @code{syslog} UDP port on that system.  @xref{Sockets}.
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Submitting Syslog Messages
Packit 6c4009
@section Submitting Syslog Messages
Packit 6c4009
Packit 6c4009
@Theglibc{} provides functions to submit messages to the Syslog
Packit 6c4009
facility:
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* openlog::                      Open connection to Syslog
Packit 6c4009
* syslog; vsyslog::              Submit message to Syslog
Packit 6c4009
* closelog::                     Close connection to Syslog
Packit 6c4009
* setlogmask::                   Cause certain messages to be ignored
Packit 6c4009
* Syslog Example::               Example of all of the above
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
These functions only work to submit messages to the Syslog facility on
Packit 6c4009
the same system.  To submit a message to the Syslog facility on another
Packit 6c4009
system, use the socket I/O functions to write a UDP datagram to the
Packit 6c4009
@code{syslog} UDP port on that system.  @xref{Sockets}.
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node openlog
Packit 6c4009
@subsection openlog
Packit 6c4009
Packit 6c4009
The symbols referred to in this section are declared in the file
Packit 6c4009
@file{syslog.h}.
Packit 6c4009
Packit 6c4009
@deftypefun void openlog (const char *@var{ident}, int @var{option}, int @var{facility})
Packit 6c4009
@standards{BSD, syslog.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
Packit 6c4009
@c openlog @asulock @aculock @acsfd
Packit 6c4009
@c  libc_lock_lock @asulock @aculock
Packit 6c4009
@c  openlog_internal @acsfd [always guarded by syslog_lock, so no race]
Packit 6c4009
@c   strncpy dup ok
Packit 6c4009
@c   socket dup @acsfd
Packit 6c4009
@c   fcntl dup ok
Packit 6c4009
@c   connect dup ok
Packit 6c4009
@c   close dup @acsfd
Packit 6c4009
@c  cancel_handler(NULL) @aculock
Packit 6c4009
@c   libc_lock_unlock @aculock
Packit 6c4009
Packit 6c4009
@code{openlog} opens or reopens a connection to Syslog in preparation
Packit 6c4009
for submitting messages.
Packit 6c4009
Packit 6c4009
@var{ident} is an arbitrary identification string which future
Packit 6c4009
@code{syslog} invocations will prefix to each message.  This is intended
Packit 6c4009
to identify the source of the message, and people conventionally set it
Packit 6c4009
to the name of the program that will submit the messages.
Packit 6c4009
Packit 6c4009
If @var{ident} is NULL, or if @code{openlog} is not called, the default
Packit 6c4009
identification string used in Syslog messages will be the program name,
Packit 6c4009
taken from argv[0].
Packit 6c4009
Packit 6c4009
Please note that the string pointer @var{ident} will be retained
Packit 6c4009
internally by the Syslog routines.  You must not free the memory that
Packit 6c4009
@var{ident} points to.  It is also dangerous to pass a reference to an
Packit 6c4009
automatic variable since leaving the scope would mean ending the
Packit 6c4009
lifetime of the variable.  If you want to change the @var{ident} string,
Packit 6c4009
you must call @code{openlog} again; overwriting the string pointed to by
Packit 6c4009
@var{ident} is not thread-safe.
Packit 6c4009
Packit 6c4009
You can cause the Syslog routines to drop the reference to @var{ident} and
Packit 6c4009
go back to the default string (the program name taken from argv[0]), by
Packit 6c4009
calling @code{closelog}: @xref{closelog}.
Packit 6c4009
Packit 6c4009
In particular, if you are writing code for a shared library that might get
Packit 6c4009
loaded and then unloaded (e.g. a PAM module), and you use @code{openlog},
Packit 6c4009
you must call @code{closelog} before any point where your library might
Packit 6c4009
get unloaded, as in this example:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
#include <syslog.h>
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
shared_library_function (void)
Packit 6c4009
@{
Packit 6c4009
  openlog ("mylibrary", option, priority);
Packit 6c4009
Packit 6c4009
  syslog (LOG_INFO, "shared library has been invoked");
Packit 6c4009
Packit 6c4009
  closelog ();
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
Without the call to @code{closelog}, future invocations of @code{syslog}
Packit 6c4009
by the program using the shared library may crash, if the library gets
Packit 6c4009
unloaded and the memory containing the string @code{"mylibrary"} becomes
Packit 6c4009
unmapped.  This is a limitation of the BSD syslog interface.
Packit 6c4009
Packit 6c4009
@code{openlog} may or may not open the @file{/dev/log} socket, depending
Packit 6c4009
on @var{option}.  If it does, it tries to open it and connect it as a
Packit 6c4009
stream socket.  If that doesn't work, it tries to open it and connect it
Packit 6c4009
as a datagram socket.  The socket has the ``Close on Exec'' attribute,
Packit 6c4009
so the kernel will close it if the process performs an exec.
Packit 6c4009
Packit 6c4009
You don't have to use @code{openlog}.  If you call @code{syslog} without
Packit 6c4009
having called @code{openlog}, @code{syslog} just opens the connection
Packit 6c4009
implicitly and uses defaults for the information in @var{ident} and
Packit 6c4009
@var{options}.
Packit 6c4009
Packit 6c4009
@var{options} is a bit string, with the bits as defined by the following
Packit 6c4009
single bit masks:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item LOG_PERROR
Packit 6c4009
If on, @code{openlog} sets up the connection so that any @code{syslog}
Packit 6c4009
on this connection writes its message to the calling process' Standard
Packit 6c4009
Error stream in addition to submitting it to Syslog.  If off, @code{syslog}
Packit 6c4009
does not write the message to Standard Error.
Packit 6c4009
Packit 6c4009
@item LOG_CONS
Packit 6c4009
If on, @code{openlog} sets up the connection so that a @code{syslog} on
Packit 6c4009
this connection that fails to submit a message to Syslog writes the
Packit 6c4009
message instead to system console.  If off, @code{syslog} does not write
Packit 6c4009
to the system console (but of course Syslog may write messages it
Packit 6c4009
receives to the console).
Packit 6c4009
Packit 6c4009
@item LOG_PID
Packit 6c4009
When on, @code{openlog} sets up the connection so that a @code{syslog}
Packit 6c4009
on this connection inserts the calling process' Process ID (PID) into
Packit 6c4009
the message.  When off, @code{openlog} does not insert the PID.
Packit 6c4009
Packit 6c4009
@item LOG_NDELAY
Packit 6c4009
When on, @code{openlog} opens and connects the @file{/dev/log} socket.
Packit 6c4009
When off, a future @code{syslog} call must open and connect the socket.
Packit 6c4009
Packit 6c4009
@strong{Portability note:}  In early systems, the sense of this bit was
Packit 6c4009
exactly the opposite.
Packit 6c4009
Packit 6c4009
@item LOG_ODELAY
Packit 6c4009
This bit does nothing.  It exists for backward compatibility.
Packit 6c4009
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
If any other bit in @var{options} is on, the result is undefined.
Packit 6c4009
Packit 6c4009
@var{facility} is the default facility code for this connection.  A
Packit 6c4009
@code{syslog} on this connection that specifies default facility causes
Packit 6c4009
this facility to be associated with the message.  See @code{syslog} for
Packit 6c4009
possible values.  A value of zero means the default, which is
Packit 6c4009
@code{LOG_USER}.
Packit 6c4009
Packit 6c4009
If a Syslog connection is already open when you call @code{openlog},
Packit 6c4009
@code{openlog} ``reopens'' the connection.  Reopening is like opening
Packit 6c4009
except that if you specify zero for the default facility code, the
Packit 6c4009
default facility code simply remains unchanged and if you specify
Packit 6c4009
LOG_NDELAY and the socket is already open and connected, @code{openlog}
Packit 6c4009
just leaves it that way.
Packit 6c4009
Packit 6c4009
@c There is a bug in closelog() (glibc 2.1.3) wherein it does not reset the
Packit 6c4009
@c default log facility to LOG_USER, which means the default default log
Packit 6c4009
@c facility could be whatever the default log facility was for a previous
Packit 6c4009
@c Syslog connection.  I have documented what the function should be rather
Packit 6c4009
@c than what it is because I think if anyone ever gets concerned, the code
Packit 6c4009
@c will change.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node syslog; vsyslog
Packit 6c4009
@subsection syslog, vsyslog
Packit 6c4009
Packit 6c4009
The symbols referred to in this section are declared in the file
Packit 6c4009
@file{syslog.h}.
Packit 6c4009
Packit 6c4009
@c syslog() is implemented as a call to vsyslog().
Packit 6c4009
@deftypefun void syslog (int @var{facility_priority}, const char *@var{format}, @dots{})
Packit 6c4009
@standards{BSD, syslog.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
Packit 6c4009
@c syslog @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c  va_start dup ok
Packit 6c4009
@c  vsyslog_chk @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   syslog(INTERNALLOG) dup @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   open_memstream @ascuheap @acsmem
Packit 6c4009
@c   stpcpy dup ok
Packit 6c4009
@c   getpid dup ok
Packit 6c4009
@c   mempcpy dup ok
Packit 6c4009
@c   fsetlocking [no @mtasurace:stream @asulock for exclusive stream]
Packit 6c4009
@c   fprintf @mtslocale @ascuheap @acsmem [no @asucorrupt @aculock @acucorrupt on temp memstream]
Packit 6c4009
@c   time dup ok
Packit 6c4009
@c   localtime_r dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
Packit 6c4009
@c   strftime_l(C) dup @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c   ftell dup ok [no @asucorrupt @aculock @acucorrupt on temp memstream]
Packit 6c4009
@c   fputs_unlocked dup ok [no @mtasurace:stream @asucorrupt @acucorrupt on temp memstream]
Packit 6c4009
@c   putc_unlocked dup ok [no @mtasurace:stream @asucorrupt @acucorrupt on temp memstream]
Packit 6c4009
@c   vfprintf/vfprintf_chk dup @mtslocale @ascuheap @acsmem [no @mtasurace:stream @asucorrupt @acucorrupt on temp memstream]
Packit 6c4009
@c   fclose dup @ascuheap @acsmem [no @asulock @aculock @acsfd on caller-locked memstream]
Packit 6c4009
@c   writev dup ok
Packit 6c4009
@c   libc_lock_lock dup @asulock @aculock
Packit 6c4009
@c   memset dup ok
Packit 6c4009
@c   sigemptyset dup ok
Packit 6c4009
@c   sigaction(SIGPIPE) dup @mtasusig:PIPE @acusig:PIPE
Packit 6c4009
@c   openlog_internal dup @acsfd
Packit 6c4009
@c   send dup ok
Packit 6c4009
@c   closelog_internal dup @acsfd
Packit 6c4009
@c   open dup @acsfd
Packit 6c4009
@c   dprintf dup ok
Packit 6c4009
@c   libc_lock_unlock @asulock @aculock
Packit 6c4009
@c   free dup @acsuheap @acsmem
Packit 6c4009
@c  va_end dup ok
Packit 6c4009
Packit 6c4009
@code{syslog} submits a message to the Syslog facility.  It does this by
Packit 6c4009
writing to the Unix domain socket @code{/dev/log}.
Packit 6c4009
Packit 6c4009
@code{syslog} submits the message with the facility and priority indicated
Packit 6c4009
by @var{facility_priority}.  The macro @code{LOG_MAKEPRI} generates a
Packit 6c4009
facility/priority from a facility and a priority, as in the following
Packit 6c4009
example:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
LOG_MAKEPRI(LOG_USER, LOG_WARNING)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The possible values for the facility code are (macros):
Packit 6c4009
Packit 6c4009
@c Internally, there is also LOG_KERN, but LOG_KERN == 0, which means
Packit 6c4009
@c if you try to use it here, just selects default.
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item LOG_USER
Packit 6c4009
A miscellaneous user process
Packit 6c4009
@item LOG_MAIL
Packit 6c4009
Mail
Packit 6c4009
@item LOG_DAEMON
Packit 6c4009
A miscellaneous system daemon
Packit 6c4009
@item LOG_AUTH
Packit 6c4009
Security (authorization)
Packit 6c4009
@item LOG_SYSLOG
Packit 6c4009
Syslog
Packit 6c4009
@item LOG_LPR
Packit 6c4009
Central printer
Packit 6c4009
@item LOG_NEWS
Packit 6c4009
Network news (e.g. Usenet)
Packit 6c4009
@item LOG_UUCP
Packit 6c4009
UUCP
Packit 6c4009
@item LOG_CRON
Packit 6c4009
Cron and At
Packit 6c4009
@item LOG_AUTHPRIV
Packit 6c4009
Private security (authorization)
Packit 6c4009
@item LOG_FTP
Packit 6c4009
Ftp server
Packit 6c4009
@item LOG_LOCAL0
Packit 6c4009
Locally defined
Packit 6c4009
@item LOG_LOCAL1
Packit 6c4009
Locally defined
Packit 6c4009
@item LOG_LOCAL2
Packit 6c4009
Locally defined
Packit 6c4009
@item LOG_LOCAL3
Packit 6c4009
Locally defined
Packit 6c4009
@item LOG_LOCAL4
Packit 6c4009
Locally defined
Packit 6c4009
@item LOG_LOCAL5
Packit 6c4009
Locally defined
Packit 6c4009
@item LOG_LOCAL6
Packit 6c4009
Locally defined
Packit 6c4009
@item LOG_LOCAL7
Packit 6c4009
Locally defined
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
Results are undefined if the facility code is anything else.
Packit 6c4009
Packit 6c4009
@strong{NB:} @code{syslog} recognizes one other facility code: that of
Packit 6c4009
the kernel.  But you can't specify that facility code with these
Packit 6c4009
functions.  If you try, it looks the same to @code{syslog} as if you are
Packit 6c4009
requesting the default facility.  But you wouldn't want to anyway,
Packit 6c4009
because any program that uses @theglibc{} is not the kernel.
Packit 6c4009
Packit 6c4009
You can use just a priority code as @var{facility_priority}.  In that
Packit 6c4009
case, @code{syslog} assumes the default facility established when the
Packit 6c4009
Syslog connection was opened.  @xref{Syslog Example}.
Packit 6c4009
Packit 6c4009
The possible values for the priority code are (macros):
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item LOG_EMERG
Packit 6c4009
The message says the system is unusable.
Packit 6c4009
@item LOG_ALERT
Packit 6c4009
Action on the message must be taken immediately.
Packit 6c4009
@item LOG_CRIT
Packit 6c4009
The message states a critical condition.
Packit 6c4009
@item LOG_ERR
Packit 6c4009
The message describes an error.
Packit 6c4009
@item LOG_WARNING
Packit 6c4009
The message is a warning.
Packit 6c4009
@item LOG_NOTICE
Packit 6c4009
The message describes a normal but important event.
Packit 6c4009
@item LOG_INFO
Packit 6c4009
The message is purely informational.
Packit 6c4009
@item LOG_DEBUG
Packit 6c4009
The message is only for debugging purposes.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
Results are undefined if the priority code is anything else.
Packit 6c4009
Packit 6c4009
If the process does not presently have a Syslog connection open (i.e.,
Packit 6c4009
it did not call @code{openlog}), @code{syslog} implicitly opens the
Packit 6c4009
connection the same as @code{openlog} would, with the following defaults
Packit 6c4009
for information that would otherwise be included in an @code{openlog}
Packit 6c4009
call: The default identification string is the program name.  The
Packit 6c4009
default default facility is @code{LOG_USER}.  The default for all the
Packit 6c4009
connection options in @var{options} is as if those bits were off.
Packit 6c4009
@code{syslog} leaves the Syslog connection open.
Packit 6c4009
Packit 6c4009
If the @file{/dev/log} socket is not open and connected, @code{syslog}
Packit 6c4009
opens and connects it, the same as @code{openlog} with the
Packit 6c4009
@code{LOG_NDELAY} option would.
Packit 6c4009
Packit 6c4009
@code{syslog} leaves @file{/dev/log} open and connected unless its attempt
Packit 6c4009
to send the message failed, in which case @code{syslog} closes it (with the
Packit 6c4009
hope that a future implicit open will restore the Syslog connection to a
Packit 6c4009
usable state).
Packit 6c4009
Packit 6c4009
Example:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
Packit 6c4009
#include <syslog.h>
Packit 6c4009
syslog (LOG_MAKEPRI(LOG_LOCAL1, LOG_ERROR),
Packit 6c4009
        "Unable to make network connection to %s.  Error=%m", host);
Packit 6c4009
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@deftypefun void vsyslog (int @var{facility_priority}, const char *@var{format}, va_list @var{arglist})
Packit 6c4009
@standards{BSD, syslog.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
Packit 6c4009
@c vsyslog @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
@c  vsyslog_chk dup @mtsenv @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
Packit 6c4009
Packit 6c4009
This is functionally identical to @code{syslog}, with the BSD style variable
Packit 6c4009
length argument.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node closelog
Packit 6c4009
@subsection closelog
Packit 6c4009
Packit 6c4009
The symbols referred to in this section are declared in the file
Packit 6c4009
@file{syslog.h}.
Packit 6c4009
Packit 6c4009
@deftypefun void closelog (void)
Packit 6c4009
@standards{BSD, syslog.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
Packit 6c4009
@c closelog @asulock @aculock @acsfd
Packit 6c4009
@c  libc_lock_lock @asulock @aculock
Packit 6c4009
@c  closelog_internal @acsfd [always guarded by syslog_lock, so no race]
Packit 6c4009
@c   close dup@acsfd
Packit 6c4009
@c  cancel_handler(NULL) @aculock
Packit 6c4009
@c   libc_lock_unlock @aculock
Packit 6c4009
Packit 6c4009
@code{closelog} closes the current Syslog connection, if there is one.
Packit 6c4009
This includes closing the @file{/dev/log} socket, if it is open.
Packit 6c4009
@code{closelog} also sets the identification string for Syslog messages
Packit 6c4009
back to the default, if @code{openlog} was called with a non-NULL argument
Packit 6c4009
to @var{ident}.  The default identification string is the program name
Packit 6c4009
taken from argv[0].
Packit 6c4009
Packit 6c4009
If you are writing shared library code that uses @code{openlog} to
Packit 6c4009
generate custom syslog output, you should use @code{closelog} to drop
Packit 6c4009
@theglibc{}'s internal reference to the @var{ident} pointer when you are
Packit 6c4009
done.  Please read the section on @code{openlog} for more information:
Packit 6c4009
@xref{openlog}.
Packit 6c4009
Packit 6c4009
@code{closelog} does not flush any buffers.  You do not have to call
Packit 6c4009
@code{closelog} before re-opening a Syslog connection with @code{openlog}.
Packit 6c4009
Syslog connections are automatically closed on exec or exit.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node setlogmask
Packit 6c4009
@subsection setlogmask
Packit 6c4009
Packit 6c4009
The symbols referred to in this section are declared in the file
Packit 6c4009
@file{syslog.h}.
Packit 6c4009
Packit 6c4009
@deftypefun int setlogmask (int @var{mask})
Packit 6c4009
@standards{BSD, syslog.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:LogMask}}@asunsafe{}@acsafe{}}
Packit 6c4009
@c Read and modify are not guarded by syslog_lock, so concurrent changes
Packit 6c4009
@c or even uses are undefined.  This should use an atomic swap instead,
Packit 6c4009
@c at least for modifications.
Packit 6c4009
Packit 6c4009
@code{setlogmask} sets a mask (the ``logmask'') that determines which
Packit 6c4009
future @code{syslog} calls shall be ignored.  If a program has not
Packit 6c4009
called @code{setlogmask}, @code{syslog} doesn't ignore any calls.  You
Packit 6c4009
can use @code{setlogmask} to specify that messages of particular
Packit 6c4009
priorities shall be ignored in the future.
Packit 6c4009
Packit 6c4009
A @code{setlogmask} call overrides any previous @code{setlogmask} call.
Packit 6c4009
Packit 6c4009
Note that the logmask exists entirely independently of opening and
Packit 6c4009
closing of Syslog connections.
Packit 6c4009
Packit 6c4009
Setting the logmask has a similar effect to, but is not the same as,
Packit 6c4009
configuring Syslog.  The Syslog configuration may cause Syslog to
Packit 6c4009
discard certain messages it receives, but the logmask causes certain
Packit 6c4009
messages never to get submitted to Syslog in the first place.
Packit 6c4009
Packit 6c4009
@var{mask} is a bit string with one bit corresponding to each of the
Packit 6c4009
possible message priorities.  If the bit is on, @code{syslog} handles
Packit 6c4009
messages of that priority normally.  If it is off, @code{syslog}
Packit 6c4009
discards messages of that priority.  Use the message priority macros
Packit 6c4009
described in @ref{syslog; vsyslog} and the @code{LOG_MASK} to construct
Packit 6c4009
an appropriate @var{mask} value, as in this example:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
LOG_MASK(LOG_EMERG) | LOG_MASK(LOG_ERROR)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
or
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
~(LOG_MASK(LOG_INFO))
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
There is also a @code{LOG_UPTO} macro, which generates a mask with the bits
Packit 6c4009
on for a certain priority and all priorities above it:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
LOG_UPTO(LOG_ERROR)
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
The unfortunate naming of the macro is due to the fact that internally,
Packit 6c4009
higher numbers are used for lower message priorities.
Packit 6c4009
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Packit 6c4009
@node Syslog Example
Packit 6c4009
@subsection Syslog Example
Packit 6c4009
Packit 6c4009
Here is an example of @code{openlog}, @code{syslog}, and @code{closelog}:
Packit 6c4009
Packit 6c4009
This example sets the logmask so that debug and informational messages
Packit 6c4009
get discarded without ever reaching Syslog.  So the second @code{syslog}
Packit 6c4009
in the example does nothing.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
#include <syslog.h>
Packit 6c4009
Packit 6c4009
setlogmask (LOG_UPTO (LOG_NOTICE));
Packit 6c4009
Packit 6c4009
openlog ("exampleprog", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
Packit 6c4009
Packit 6c4009
syslog (LOG_NOTICE, "Program started by User %d", getuid ());
Packit 6c4009
syslog (LOG_INFO, "A tree falls in a forest");
Packit 6c4009
Packit 6c4009
closelog ();
Packit 6c4009
Packit 6c4009
@end smallexample