Blame manual/syslog.texi

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