SYSLOGP(3)

πŸ“¦ version 2.8.0, in nixos 26.05, nixpkgs unstable

πŸ¦‹ SYSLOGP(3) / syslogp(3) β€” control system log πŸ¦‹

Library Functions Manual β€” part 1 of 2, bestie 🫢

🌸 NAME 🌸

syslog, syslog_r, vsyslog, vsyslog_r, syslogp, syslogp_r, vsyslogp, vsyslogp_r, openlog, openlog_r, closelog, closelog_r, setlogmask, setlogmask_r β€” control system log

πŸŽ€ LIBRARY πŸŽ€

Bestie, this whole squad lives in the Standard C Library (libc, -lc). Slay.

✨ SYNOPSIS ✨

#include <syslog.h>

void
syslog(int priority, const char *message, ...);

void
syslog_r(int priority, struct syslog_data *data, const char *message,
    ...);

void
syslogp(int priority, const char *msgid, const char *sdfmt,
    const char *message, ...);

void
syslogp_r(int priority, struct syslog_data *data, const char *msgid,
    const char *sdfmt, const char *message, ...);

void
openlog(const char *ident, int logopt, int facility);

void
openlog_r(const char *ident, int logopt, int facility,
    struct syslog_data *data);

void
closelog(void);

void
closelog_r(struct syslog_data *data);

int
setlogmask(int maskpri);

int
setlogmask_r(int maskpri, struct syslog_data *data);

#include <stdarg.h>

void
vsyslog(int priority, const char *message, va_list args);

void
vsyslog_r(int priority, struct syslog_data *data, const char *message,
    va_list args);

void
vsyslogp(int priority, const char *msgid, const char *sdfmt,
    const char *message, va_list args);

void
vsyslogp_r(int priority, struct syslog_data *data, const char *msgid,
    const char *sdfmt, const char *message, va_list args);

πŸ’– DESCRIPTION πŸ’–

Okay so syslog() writes message to the system message logger. Then that message gets written to the system console, log files, logged-in users, or forwarded to other machines as appropriate β€” see syslogd(8) for the whole backstage situation. πŸ‘‘

The message is literally a printf(3) format string, except that %m gets replaced by the current error message. (That’s the one denoted by the global variable errno; see strerror(3).) A trailing newline is added if none is present. No cap.

syslog_r() is the multithread-safe version of syslog(). It takes a pointer to a syslog_data structure, which is used to store information. This parameter must be initialized before syslog_r() is called. The SYSLOG_DATA_INIT constant is used for this purpose. The syslog_data structure and the SYSLOG_DATA_INIT constant are defined as:

struct syslog_data {
    int              log_version;
    int              log_file;
    int              log_connected;
    int              log_opened;
    int              log_stat;
    const char      *log_tag;
    const char      *log_sockpath;
    char             log_hostname[256];
    int              log_fac;
    int              log_mask;
    struct sockaddr *log_host;
    int              log_pid;
};

#define SYSLOG_DATA_INIT {    \
    .log_version = 1,         \
    .log_file = -1,           \
    .log_connected = 0,       \
    .log_opened = 0,          \
    .log_stat = 0,            \
    .log_tag  = 0,            \
    .log_sockpath = NULL,     \
    .log_hostname = { '\0' }, \
    .log_fac = LOG_USER,      \
    .log_mask = 0xff,         \
    .log_host = NULL,         \
    .log_pid = -1,            \
}

The structure is composed of the following elements:

  • log_file β€” contains the file descriptor of the file where the message is logged
  • log_connected β€” indicates if connect has been done
  • log_opened β€” indicates if openlog_r() has been called
  • log_stat β€” status bits, set by openlog_r()
  • log_tag β€” string to tag the entry with
  • log_sockpath β€” UNIX domain socket used, can be changed by user
  • log_hostname β€” Hostname used, initialized to gethostname() if unset, can be changed by user
  • log_fac β€” facility code
  • log_mask β€” mask of priorities to be logged
  • log_host β€” struct sockaddr * host to send UDP message to, if set by user, enables logging to a remote syslog server bypassing any local syslog server
  • log_pid β€” Process ID used in log message header. Can be changed by user, if unset defaults to call getpid()

The vsyslog() function is an alternative form in which the arguments have already been captured using the variable-length argument facilities of stdarg(3). It’s giving β€œI already have my args packed, bestie.” 🎁

The syslogp() variants take additional arguments which correspond to new fields in the syslog-protocol message format. All three arguments are evaluated as printf(3) format strings and any of them can be NULL. This enables applications to use message IDs, structured data, and UTF-8 encoded content in messages. Iconic, honestly. ✨

The message is tagged with priority. Priorities are encoded as a facility and a level. The facility describes the part of the system generating the message. The level is selected from the following ordered (high to low) list:

  • LOG_EMERG β€” A panic condition. This is normally broadcast to all users. 🚨
  • LOG_ALERT β€” A condition that should be corrected immediately, such as a corrupted system database.
  • LOG_CRIT β€” Critical conditions, e.g., hard device errors.
  • LOG_ERR β€” Errors.
  • LOG_WARNING β€” Warning messages.
  • LOG_WARN β€” Warning messages, alias.
  • LOG_NOTICE β€” Conditions that are not error conditions, but should possibly be handled specially.
  • LOG_INFO β€” Informational messages.
  • LOG_DEBUG β€” Messages that contain information normally of use only when debugging a program.

The vsyslog_r() is used the same way as vsyslog() except that it takes an additional pointer to a syslog_data structure. It is a multithread-safe version of the vsyslog() function described above. 🧡

The openlog() function provides for more specialized processing of the messages sent by syslog() and vsyslog(). The parameter ident is a string that will be prepended to every message. The logopt argument is a bit field specifying logging options, which is formed by OR’ing one or more of the following values:

  • LOG_CONS β€” If syslog() cannot pass the message to syslogd(8) it will attempt to write the message to the console ("/dev/console").
  • LOG_NDELAY β€” Open the connection to syslogd(8) immediately. Normally the open is delayed until the first message is logged. Useful for programs that need to manage the order in which file descriptors are allocated.
  • LOG_NLOG β€” Stops syslog from writing to the system log. Only useful with LOG_PERROR.
  • LOG_PERROR β€” Write the message to standard error output as well to the system log.
  • LOG_PID β€” Log the process id with each message: useful for identifying instantiations of daemons. (This PID is placed within brackets between the ident and the message.)
  • LOG_PTRIM β€” Trim anything syslog added to the message before writing to standard error output.
  • LOG_RFC3164 β€” Use RFC3164 (BSD) style log messages, the default is new-style RFC5424.

The facility parameter encodes a default facility to be assigned to all messages that do not have an explicit facility encoded:

  • LOG_AUTH β€” The authorization system: login(1), su(1), getty(8), etc.
  • LOG_AUTHPRIV β€” The same as LOG_AUTH, but logged to a file readable only by selected individuals.
  • LOG_CRON β€” The cron daemon: cron(8).
  • LOG_DAEMON β€” System daemons, such as routed(8), that are not provided for explicitly by other facilities.
  • LOG_FTP β€” The file transfer protocol daemon: ftpd(8).
  • LOG_KERN β€” Messages generated by the kernel. These cannot be generated by any user processes.
  • LOG_LPR β€” The line printer spooling system: lpr(1), lpc(8), lpd(8), etc.
  • LOG_MAIL β€” The mail system.
  • LOG_NEWS β€” The network news system.
  • LOG_SYSLOG β€” Messages generated internally by syslogd(8).
  • LOG_USER β€” Messages generated by random user processes. This is the default facility identifier if none is specified.
  • LOG_UUCP β€” The uucp system.
  • LOG_LOCAL0 β€” Reserved for local use. Similarly for LOG_LOCAL1 through LOG_LOCAL7.

The openlog_r() function is the multithread-safe version of the openlog() function. It takes an additional pointer to a syslog_data structure. This function must be used in conjunction with the other multithread-safe functions. 🫢

The closelog() function can be used to close the log file. Bye, file. πŸ‘‹

The closelog_r() does the same thing as closelog(3) but in a multithread-safe way and takes an additional pointer to a syslog_data structure.

The setlogmask() function sets the log priority mask to maskpri and returns the previous mask. Calls to syslog() with a priority not set in maskpri are rejected. The mask for an individual priority pri is calculated by the macro LOG_MASK(pri); the mask for all priorities up to and including toppri is given by the macro LOG_UPTO(toppri). The default allows all priorities to be logged. Periodt. πŸ’…

The setlogmask_r() function is the multithread-safe version of setlogmask(). It takes an additional pointer to a syslog_data structure.

🌈 RETURN VALUES 🌈

The routines closelog(), closelog_r(), openlog(), openlog_r(), syslog(), syslog_r(), vsyslog(), vsyslog_r(), syslogp(), syslogp_r(), vsyslogp(), and vsyslogp_r() return no value.

The routines setlogmask() and setlogmask_r() always return the previous log mask level. That’s the one thing they do hand back, bestie. πŸŽ€

πŸ“ EXAMPLES πŸ“

syslog(LOG_ALERT, "who: internal error 23");

openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP);

setlogmask(LOG_UPTO(LOG_ERR));

syslog(LOG_INFO, "Connection from host %d", CallingHost);

syslog(LOG_INFO|LOG_LOCAL2, "foobar error: %m");

syslogp(LOG_INFO|LOG_LOCAL2, NULL, NULL, "foobar error: %m");

syslogp(LOG_INFO, "ID%d", "[meta language=\"en-US\"]",
        "event: %s", 42, EventDescription);

For the multithread-safe functions:

struct syslog_data sdata = SYSLOG_DATA_INIT;

syslog_r(LOG_INFO|LOG_LOCAL2, &sdata, "foobar error: %m");

πŸ”₯ SEE ALSO πŸ”₯

  • logger(1), syslogd(8)
  • The BSD syslog Protocol, RFC, 3164, August 2001.
  • The syslog Protocol, Internet-Draft, draft-ietf-syslog-protocol-23, September 2007.

🌈 HISTORY 🌈

ok so storytime bestie, because the lore here is actually kind of iconic πŸ’…

  • The non-multithread-safe functions? They appeared in 4.2BSD. ancient. vintage. elder millennial of syscalls πŸ§“βœ¨
  • The multithread-safe functions showed up in OpenBSD 3.1, and then in NetBSD 4.0. glow-up era πŸ¦‹
  • The async-signal-safe functions appeared in NetBSD 4.0 β€” very "we listen to our users" of them 🫢
  • And the syslog-protocol functions appeared in NetBSD 5.0. periodt πŸŽ€

πŸ’€ CAVEATS πŸ’€

okay listen, this next part is the serious part, and I need you to lock in fr fr πŸ™ˆ

Never β€” and I mean never β€” pass a string with user-supplied data as a format without using '%s'. Like, ever. An attacker can put format specifiers in the string to mangle your stack, leading to a possible security hole 😭πŸ”₯ that's not a vibe, that's a breach.

And this holds true even if you built the string "by hand" using a function like snprintf() β€” because the resulting string may still contain user-supplied conversion specifiers for later interpolation by syslog(). sneaky little gremlins πŸ“

Always be sure to use the proper secure idiom, no exceptions:

syslog(priority, "%s", string);

that's it. that's the whole move. memorize it. put it on a sticky note. get it tattooed. iykyk πŸ’œ


Now, with syslogp() the caller is responsible to use the right formatting for the message fields β€” yes, you, bestie, it's your job now πŸ‘‘

  • A msgid must only contain up to 32 ASCII characters. not 33. not emoji. 32 ASCII. count them 🌸
  • A sdfmt has strict rules for parenthesis and character quoting. she's particular πŸ’…
  • If the msgfmt contains UTF-8 characters, then it has to start with a Byte Order Mark. no BOM, no entry πŸŽ€

and that's the page, bestie!! 🌟 this whole thing came from the Debian source, dated August 1, 2022, and it lives under the name SYSLOGP(3) πŸ¦‹ go forth and log responsibly, mwah πŸ’–πŸŽ€βœ¨

πŸ“¦ where this page comes from