iPXE
Defines | Functions | Variables
syslog.c File Reference

Syslog protocol. More...

#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <byteswap.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/tcpip.h>
#include <ipxe/dhcp.h>
#include <ipxe/dhcpv6.h>
#include <ipxe/settings.h>
#include <ipxe/console.h>
#include <ipxe/lineconsole.h>
#include <ipxe/syslog.h>
#include <config/console.h>

Go to the source code of this file.

Defines

#define CONSOLE_SYSLOG   ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
int syslog_send (struct interface *xfer, unsigned int severity, const char *message, const char *terminator)
 Transmit formatted syslog message.
static void syslog_handle_priority (struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[])
 Handle ANSI set syslog priority (private sequence)
static void syslog_putchar (int character)
 Print a character to syslog console.
struct setting syslog_setting __setting (SETTING_MISC, syslog)
 IPv4 syslog server setting.
struct setting syslog6_setting __setting (SETTING_MISC, syslog6)
 IPv6 syslog server setting.
static void syslog_fix_name (char *name)
 Strip invalid characters from host/domain name.
static int apply_syslog_settings (void)
 Apply syslog settings.

Variables

union {
   struct sockaddr   sa
   struct sockaddr_tcpip   st
   struct sockaddr_in   sin
   struct sockaddr_in6   sin6
logserver
 The syslog server.
static struct interface_operation syslogger_operations [] = {}
 Syslog UDP interface operations.
static struct interface_descriptor syslogger_desc
 Syslog UDP interface descriptor.
static struct interface syslogger = INTF_INIT ( syslogger_desc )
 The syslog UDP interface.
static char * syslog_hostname
 Host name (for log messages)
static char * syslog_domain
 Domain name (for log messages)
static char syslog_buffer [SYSLOG_BUFSIZE]
 Syslog line buffer.
static unsigned int syslog_severity = SYSLOG_DEFAULT_SEVERITY
 Syslog severity.
static struct ansiesc_handler syslog_handlers []
 Syslog ANSI escape sequence handlers.
static struct line_console syslog_line
 Syslog line console.
static int syslog_entered
 Syslog recursion marker.
struct console_driver
syslog_console 
__console_driver
 Syslog console driver.
struct settings_applicator
syslog_applicator 
__settings_applicator
 Syslog settings applicator.

Detailed Description

Syslog protocol.

Definition in file syslog.c.


Define Documentation

Definition at line 50 of file syslog.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
int syslog_send ( struct interface xfer,
unsigned int  severity,
const char *  message,
const char *  terminator 
)

Transmit formatted syslog message.

Parameters:
xferData transfer interface
severitySeverity
messageMessage
terminatorMessage terminator
Return values:
rcReturn status code

Definition at line 97 of file syslog.c.

References domain, SYSLOG_DEFAULT_FACILITY, syslog_domain, syslog_hostname, SYSLOG_PRIORITY, and xfer_printf().

Referenced by syslog_putchar(), and syslogs_putchar().

                                                                {
        const char *hostname = ( syslog_hostname ? syslog_hostname : "" );
        const char *domain = ( ( hostname[0] && syslog_domain ) ?
                               syslog_domain : "" );

        return xfer_printf ( xfer, "<%d>%s%s%s%sipxe: %s%s",
                             SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
                                               severity ), hostname,
                             ( domain[0] ? "." : "" ), domain,
                             ( hostname[0] ? " " : "" ), message, terminator );
}
static void syslog_handle_priority ( struct ansiesc_context *ctx  __unused,
unsigned int count  __unused,
int  params[] 
) [static]

Handle ANSI set syslog priority (private sequence)

Parameters:
ctxANSI escape sequence context
countParameter count
paramsList of graphic rendition aspects

Definition at line 130 of file syslog.c.

References SYSLOG_DEFAULT_SEVERITY, and syslog_severity.

                                                    {
        if ( params[0] >= 0 ) {
                syslog_severity = params[0];
        } else {
                syslog_severity = SYSLOG_DEFAULT_SEVERITY;
        }
}
static void syslog_putchar ( int  character) [static]

Print a character to syslog console.

Parameters:
characterCharacter to be printed

Definition at line 163 of file syslog.c.

References DBG, line_putchar(), rc, strerror(), syslog_buffer, syslog_entered, syslog_send(), and syslog_severity.

                                             {
        int rc;

        /* Ignore if we are already mid-logging */
        if ( syslog_entered )
                return;

        /* Fill line buffer */
        if ( line_putchar ( &syslog_line, character ) == 0 )
                return;

        /* Guard against re-entry */
        syslog_entered = 1;

        /* Send log message */
        if ( ( rc = syslog_send ( &syslogger, syslog_severity,
                                  syslog_buffer, "" ) ) != 0 ) {
                DBG ( "SYSLOG could not send log message: %s\n",
                      strerror ( rc ) );
        }

        /* Clear re-entry flag */
        syslog_entered = 0;
}
struct setting syslog_setting __setting ( SETTING_MISC  ,
syslog   
) [read]

IPv4 syslog server setting.

struct setting syslog6_setting __setting ( SETTING_MISC  ,
syslog6   
) [read]

IPv6 syslog server setting.

static void syslog_fix_name ( char *  name) [static]

Strip invalid characters from host/domain name.

Parameters:
nameName to strip

Definition at line 224 of file syslog.c.

References fixed, isprint(), isspace(), and name.

Referenced by apply_syslog_settings().

                                           {
        char *fixed = name;
        int c;

        /* Do nothing if name does not exist */
        if ( ! name )
                return;

        /* Strip any non-printable or whitespace characters from the name */
        do {
                c = *(name++);
                *fixed = c;
                if ( isprint ( c ) && ! isspace ( c ) )
                        fixed++;
        } while ( c );
}
static int apply_syslog_settings ( void  ) [static]

Apply syslog settings.

Return values:
rcReturn status code

Definition at line 246 of file syslog.c.

References AF_INET, AF_INET6, CONSOLE_DISABLED, DBG, fetch_ipv4_setting(), fetch_ipv6_setting(), fetch_string_setting_copy(), free, intf_restart(), logserver, memcmp(), memcpy(), NULL, rc, SOCK_DGRAM, sock_ntoa(), strerror(), syslog_domain, syslog_fix_name(), syslog_hostname, and xfer_open_socket().

                                          {
        struct sockaddr old_logserver;
        int rc;

        /* Fetch hostname and domain name */
        free ( syslog_hostname );
        fetch_string_setting_copy ( NULL, &hostname_setting, &syslog_hostname );
        syslog_fix_name ( syslog_hostname );
        free ( syslog_domain );
        fetch_string_setting_copy ( NULL, &domain_setting, &syslog_domain );
        syslog_fix_name ( syslog_domain );

        /* Fetch log server */
        syslog_console.disabled = CONSOLE_DISABLED;
        memcpy ( &old_logserver, &logserver, sizeof ( old_logserver ) );
        logserver.sa.sa_family = 0;
        if ( fetch_ipv6_setting ( NULL, &syslog6_setting,
                                  &logserver.sin6.sin6_addr ) >= 0 ) {
                logserver.sin6.sin6_family = AF_INET6;
        } else if ( fetch_ipv4_setting ( NULL, &syslog_setting,
                                         &logserver.sin.sin_addr ) >= 0 ) {
                logserver.sin.sin_family = AF_INET;
        }
        if ( logserver.sa.sa_family ) {
                syslog_console.disabled = 0;
                DBG ( "SYSLOG using log server %s\n",
                      sock_ntoa ( &logserver.sa ) );
        }

        /* Do nothing unless log server has changed */
        if ( memcmp ( &logserver, &old_logserver, sizeof ( logserver ) ) == 0 )
                return 0;

        /* Reset syslog connection */
        intf_restart ( &syslogger, 0 );

        /* Do nothing unless we have a log server */
        if ( syslog_console.disabled ) {
                DBG ( "SYSLOG has no log server\n" );
                return 0;
        }

        /* Connect to log server */
        if ( ( rc = xfer_open_socket ( &syslogger, SOCK_DGRAM,
                                       &logserver.sa, NULL ) ) != 0 ) {
                DBG ( "SYSLOG cannot connect to log server: %s\n",
                      strerror ( rc ) );
                return rc;
        }

        return 0;
}

Variable Documentation

struct sockaddr sa

Definition at line 55 of file syslog.c.

Definition at line 56 of file syslog.c.

struct sockaddr_in sin

Definition at line 57 of file syslog.c.

Definition at line 58 of file syslog.c.

union { ... } logserver [static]

The syslog server.

Referenced by apply_syslog_settings().

struct interface_operation syslogger_operations[] = {} [static]

Syslog UDP interface operations.

Definition at line 66 of file syslog.c.

Initial value:

Syslog UDP interface descriptor.

Definition at line 69 of file syslog.c.

The syslog UDP interface.

Definition at line 73 of file syslog.c.

char* syslog_hostname [static]

Host name (for log messages)

Definition at line 83 of file syslog.c.

Referenced by apply_syslog_settings(), and syslog_send().

char* syslog_domain [static]

Domain name (for log messages)

Definition at line 86 of file syslog.c.

Referenced by apply_syslog_settings(), and syslog_send().

char syslog_buffer[SYSLOG_BUFSIZE] [static]

Syslog line buffer.

Definition at line 118 of file syslog.c.

Referenced by syslog_putchar().

unsigned int syslog_severity = SYSLOG_DEFAULT_SEVERITY [static]

Syslog severity.

Definition at line 121 of file syslog.c.

Referenced by syslog_handle_priority(), and syslog_putchar().

struct ansiesc_handler syslog_handlers[] [static]
Initial value:

Syslog ANSI escape sequence handlers.

Definition at line 141 of file syslog.c.

struct line_console syslog_line [static]
Initial value:
 {
        .buffer = syslog_buffer,
        .len = sizeof ( syslog_buffer ),
        .ctx = {
                .handlers = syslog_handlers,
        },
}

Syslog line console.

Definition at line 147 of file syslog.c.

int syslog_entered [static]

Syslog recursion marker.

Definition at line 156 of file syslog.c.

Referenced by syslog_putchar().

struct console_driver syslog_console __console_driver
Initial value:
 {
        .putchar = syslog_putchar,
        .disabled = CONSOLE_DISABLED,
        .usage = CONSOLE_SYSLOG,
}

Syslog console driver.

Definition at line 189 of file syslog.c.

struct settings_applicator syslog_applicator __settings_applicator
Initial value:
 {
        .apply = apply_syslog_settings,
}

Syslog settings applicator.

Definition at line 300 of file syslog.c.