syslog_r, openlog_r, closelog_r, or setlogmask_r Subroutine

Purpose

Controls the system log.

Library

Standard C Library (libc.a)

Syntax

#include <syslog.h>

int syslog_r (Priority, SysLogData, Format, . . .)
int  Priority;
struct syslog_data * SysLogData;
const char * Format;

int openlog_r (ID, LogOption, Facility, SysLogData)
const char * ID;
int LogOption;
int Facility;

struct syslog_data *SysLogData;
void closelog_r (SysLogData)
struct syslog_data *SysLogData;

int setlogmask_r ( MaskPriority, SysLogData)
int MaskPriority;
struct syslog_data *SysLogData;

Description

The syslog_r subroutine writes messages onto the system log maintained by the syslogd daemon.

The messages are similar to the Format parameter in the printf subroutine, except that the %m field is replaced by the current error message obtained from the errno global variable. A trailing new-line character can be added to the message if needed.

Messages are read by the syslogd daemon and written to the system console or log file, or forwarded to the syslogd daemon on the appropriate host.

If a program requires special processing, you can use the openlog_r subroutine to initialize the log file.

The syslog_r subroutine takes as a second parameter a variable of the type struct syslog_data, which should be provided by the caller. When that variable is declared, it should be set to the SYSLOG_DATA_INIT value, which specifies an initialization macro defined in the sys/syslog.h file. Without initialization, the data structure used to support the thread safety is not set up and the syslog_r subroutine does not work properly.

Messages are tagged with codes indicating the type of Priority for each. A Priority is encoded as a Facility, which describes the part of the system generating the message, and as a level, which indicates the severity of the message.

If the syslog_r subroutine cannot pass the message to the syslogd daemon, it writes the message the /dev/console file, provided the LOG_CONS option is set.

The closelog_r subroutine closes the log file.

The setlogmask_r subroutine uses the bit mask in the MaskPriority parameter to set the new log priority mask and returns the previous mask.

The LOG_MASK and LOG_UPTO macros in the sys/syslog.h file are used to create the priority mask. Calls to the syslog_r subroutine with a priority mask that does not allow logging of that particular level of message causes the subroutine to return without logging the message.

Programs using this subroutine must link to the libpthreads.a library.

Parameters

Item Description
Priority Specifies the part of the system generating the message and indicates the level of severity of the message. The level of severity is selected from the following list:
  • A condition that should be corrected immediately, such as a corrupted database.
  • A critical condition, such as hard device errors.
  • A message containing information useful to debug a program.
  • A panic condition reported to all users, such as an unusable system.
  • An error condition.
  • A general information message.
  • A condition requiring special handling, other than an error condition.
  • A warning message.
SysLogData Specifies a structure that contains the following information:
  • The file descriptor for the log file.
  • The status bits for the log file.
  • A string for tagging the log entry.
  • The mask of priorities to be logged.
  • The default facility code.
  • The address of the local logger.
Format Specifies the format, given in the same format as for the printf subroutine.
ID Contains a string attached to the beginning of every message. The Facility parameter encodes a default facility from the previous list to be assigned to messages that do not have an explicit facility encoded.
LogOption Specifies a bit field that indicates logging options. The values of LogOption are:
LOG_CONS
Sends messages to the console if unable to send them to the syslogd command. This option is useful in daemon processes that have no controlling terminal.
LOG_NDELAY
Opens the connection to the syslogd command immediately, instead of when the first message is logged. This option is useful for programs that need to manage the order in which file descriptors are allocated.
LOG_NOWAIT
Logs messages to the console without waiting for forked children. Use this option for processes that enable notification of child termination through SIGCHLD; otherwise, the syslog subroutine may block, waiting for a child process whose exit status has already been collected.
LOG_ODELAY
Delays opening until the syslog subroutine is called.
LOG_PID
Logs the process ID with each message. This option is useful for identifying daemons.
Facility Specifies which of the following values generated the message:
LOG_AUTH
Indicates the security authorization system: the login command, the su command, and so on.
LOG_DAEMON
Logs system daemons.
LOG_KERN
Logs messages generated by the kernel. Kernel processes should use the bsdlog routine to generate syslog messages. The syntax of bsdlog is identical to syslog. The bsdlog messages can only be created by kernel processes and must be of LOG_KERN priority.
LOG_LPR
Logs the line printer spooling system.
LOG_LOCAL0 through LOG_LOCAL7
Reserved for local use.
LOG_MAIL
Logs the mail system.
LOG_NEWS
Logs the news subsystem.
LOG_UUCP
Logs the UUCP subsystem.
LOG_USER
Logs messages generated by user processes. This is the default facility when none is specified.
  • Remote file systems, such as the Andrew File System (AFS®).
  • The UUCP subsystem.
  • Messages generated by user processes. This is the default facility when none is specified.
MaskPriority Enables logging for the levels indicated by the bits in the mask that are set, and disables logging where the bits are not set. The default mask allows all priorities to be logged.

Return Values

Item Description
0 Indicates that the subroutine was successful.
-1 Indicates that the subroutine was not successful. Moves an error code, indicating the specific error, into the errno global variable.

Error Codes

When the syslog_r subroutine is unsuccessful, the errno global variable can be set to the following values:

Item Description
EAGAIN Exceeds the limit on the total number of processes running either system-wide or by a single user, or the system does not have the resources necessary to create another process.
EBADF The syslogd daemon is not active.
ECONNRESET The syslogd daemon stopped during the operation.
ENOBUFS Buffer resources were not available.
ENOMEM Not enough space exists for this process.
ENOTCONN The syslogd daemon stopped during the operation.
EPROCLIM If WLM is running, the limit on the number of processes or threads in the class might have been met.
EINVAL The Priority parameter is not a valid parameter.

Examples

  1. To log an error message concerning a possible security breach, enter:
    syslog_r (LOG_ALERT, syslog_data_struct, "%s", "who:internal error 23");
  2. To initialize the log file, set the log priority mask, and log an error message, enter:
    openlog_r ("ftpd", LOG_PID, LOG_DAEMON, syslog_data_struct);
    setlogmask_r (LOG_UPTO (LOG_ERR), syslog_data_struct);
    syslog_r (LOG_INFO, syslog_data_struct, "");
  3. To log an error message from the system, enter:
    syslog_r (LOG_INFO | LOG_LOCAL2, syslog_data_struct, "system error: %m");