2 * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
32 #include <ipxe/xfer.h>
33 #include <ipxe/open.h>
34 #include <ipxe/tcpip.h>
35 #include <ipxe/dhcp.h>
36 #include <ipxe/dhcpv6.h>
37 #include <ipxe/settings.h>
38 #include <ipxe/console.h>
39 #include <ipxe/lineconsole.h>
40 #include <ipxe/syslog.h>
41 #include <config/console.h>
43 /* Set default console usage if applicable */
44 #if ! ( defined ( CONSOLE_SYSLOG ) && CONSOLE_EXPLICIT ( CONSOLE_SYSLOG ) )
46 #define CONSOLE_SYSLOG ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
49 /** The syslog server */
52 struct sockaddr_tcpip st;
53 struct sockaddr_in sin;
54 struct sockaddr_in6 sin6;
57 .st_port = htons ( SYSLOG_PORT ),
61 /** Syslog UDP interface operations */
62 static struct interface_operation syslogger_operations[] = {};
64 /** Syslog UDP interface descriptor */
65 static struct interface_descriptor syslogger_desc =
66 INTF_DESC_PURE ( syslogger_operations );
68 /** The syslog UDP interface */
69 static struct interface syslogger = INTF_INIT ( syslogger_desc );
71 /******************************************************************************
75 ******************************************************************************
78 /** Host name (for log messages) */
79 static char *syslog_hostname;
81 /** Domain name (for log messages) */
82 static char *syslog_domain;
85 * Transmit formatted syslog message
87 * @v xfer Data transfer interface
88 * @v severity Severity
90 * @v terminator Message terminator
91 * @ret rc Return status code
93 int syslog_send ( struct interface *xfer, unsigned int severity,
94 const char *message, const char *terminator ) {
95 const char *hostname = ( syslog_hostname ? syslog_hostname : "" );
96 const char *domain = ( ( hostname[0] && syslog_domain ) ?
99 return xfer_printf ( xfer, "<%d>%s%s%s%sipxe: %s%s",
100 SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
101 severity ), hostname,
102 ( domain[0] ? "." : "" ), domain,
103 ( hostname[0] ? " " : "" ), message, terminator );
106 /******************************************************************************
110 ******************************************************************************
113 /** Syslog line buffer */
114 static char syslog_buffer[SYSLOG_BUFSIZE];
116 /** Syslog severity */
117 static unsigned int syslog_severity = SYSLOG_DEFAULT_SEVERITY;
120 * Handle ANSI set syslog priority (private sequence)
122 * @v ctx ANSI escape sequence context
123 * @v count Parameter count
124 * @v params List of graphic rendition aspects
126 static void syslog_handle_priority ( struct ansiesc_context *ctx __unused,
127 unsigned int count __unused,
129 if ( params[0] >= 0 ) {
130 syslog_severity = params[0];
132 syslog_severity = SYSLOG_DEFAULT_SEVERITY;
136 /** Syslog ANSI escape sequence handlers */
137 static struct ansiesc_handler syslog_handlers[] = {
138 { ANSIESC_LOG_PRIORITY, syslog_handle_priority },
142 /** Syslog line console */
143 static struct line_console syslog_line = {
144 .buffer = syslog_buffer,
145 .len = sizeof ( syslog_buffer ),
147 .handlers = syslog_handlers,
151 /** Syslog recursion marker */
152 static int syslog_entered;
155 * Print a character to syslog console
157 * @v character Character to be printed
159 static void syslog_putchar ( int character ) {
162 /* Ignore if we are already mid-logging */
163 if ( syslog_entered )
166 /* Fill line buffer */
167 if ( line_putchar ( &syslog_line, character ) == 0 )
170 /* Guard against re-entry */
173 /* Send log message */
174 if ( ( rc = syslog_send ( &syslogger, syslog_severity,
175 syslog_buffer, "" ) ) != 0 ) {
176 DBG ( "SYSLOG could not send log message: %s\n",
180 /* Clear re-entry flag */
184 /** Syslog console driver */
185 struct console_driver syslog_console __console_driver = {
186 .putchar = syslog_putchar,
187 .disabled = CONSOLE_DISABLED,
188 .usage = CONSOLE_SYSLOG,
191 /******************************************************************************
195 ******************************************************************************
198 /** IPv4 syslog server setting */
199 const struct setting syslog_setting __setting ( SETTING_MISC, syslog ) = {
201 .description = "Syslog server",
202 .tag = DHCP_LOG_SERVERS,
203 .type = &setting_type_ipv4,
206 /** IPv6 syslog server setting */
207 const struct setting syslog6_setting __setting ( SETTING_MISC, syslog6 ) = {
209 .description = "Syslog server",
210 .tag = DHCPV6_LOG_SERVERS,
211 .type = &setting_type_ipv6,
212 .scope = &ipv6_scope,
216 * Strip invalid characters from host/domain name
218 * @v name Name to strip
220 static void syslog_fix_name ( char *name ) {
224 /* Do nothing if name does not exist */
228 /* Strip any non-printable or whitespace characters from the name */
232 if ( isprint ( c ) && ! isspace ( c ) )
238 * Apply syslog settings
240 * @ret rc Return status code
242 static int apply_syslog_settings ( void ) {
243 struct sockaddr old_logserver;
246 /* Fetch hostname and domain name */
247 free ( syslog_hostname );
248 fetch_string_setting_copy ( NULL, &hostname_setting, &syslog_hostname );
249 syslog_fix_name ( syslog_hostname );
250 free ( syslog_domain );
251 fetch_string_setting_copy ( NULL, &domain_setting, &syslog_domain );
252 syslog_fix_name ( syslog_domain );
254 /* Fetch log server */
255 syslog_console.disabled = CONSOLE_DISABLED;
256 memcpy ( &old_logserver, &logserver, sizeof ( old_logserver ) );
257 logserver.sa.sa_family = 0;
258 if ( fetch_ipv6_setting ( NULL, &syslog6_setting,
259 &logserver.sin6.sin6_addr ) >= 0 ) {
260 logserver.sin6.sin6_family = AF_INET6;
261 } else if ( fetch_ipv4_setting ( NULL, &syslog_setting,
262 &logserver.sin.sin_addr ) >= 0 ) {
263 logserver.sin.sin_family = AF_INET;
265 if ( logserver.sa.sa_family ) {
266 syslog_console.disabled = 0;
267 DBG ( "SYSLOG using log server %s\n",
268 sock_ntoa ( &logserver.sa ) );
271 /* Do nothing unless log server has changed */
272 if ( memcmp ( &logserver, &old_logserver, sizeof ( logserver ) ) == 0 )
275 /* Reset syslog connection */
276 intf_restart ( &syslogger, 0 );
278 /* Do nothing unless we have a log server */
279 if ( syslog_console.disabled ) {
280 DBG ( "SYSLOG has no log server\n" );
284 /* Connect to log server */
285 if ( ( rc = xfer_open_socket ( &syslogger, SOCK_DGRAM,
286 &logserver.sa, NULL ) ) != 0 ) {
287 DBG ( "SYSLOG cannot connect to log server: %s\n",
295 /** Syslog settings applicator */
296 struct settings_applicator syslog_applicator __settings_applicator = {
297 .apply = apply_syslog_settings,