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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
36 #include <ipxe/xfer.h>
37 #include <ipxe/open.h>
38 #include <ipxe/tcpip.h>
39 #include <ipxe/dhcp.h>
40 #include <ipxe/dhcpv6.h>
41 #include <ipxe/settings.h>
42 #include <ipxe/console.h>
43 #include <ipxe/lineconsole.h>
44 #include <ipxe/syslog.h>
45 #include <config/console.h>
47 /* Set default console usage if applicable */
48 #if ! ( defined ( CONSOLE_SYSLOG ) && CONSOLE_EXPLICIT ( CONSOLE_SYSLOG ) )
50 #define CONSOLE_SYSLOG ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
53 /** The syslog server */
56 struct sockaddr_tcpip st
;
57 struct sockaddr_in sin
;
58 struct sockaddr_in6 sin6
;
61 .st_port
= htons ( SYSLOG_PORT
),
65 /** Syslog UDP interface operations */
66 static struct interface_operation syslogger_operations
[] = {};
68 /** Syslog UDP interface descriptor */
69 static struct interface_descriptor syslogger_desc
=
70 INTF_DESC_PURE ( syslogger_operations
);
72 /** The syslog UDP interface */
73 static struct interface syslogger
= INTF_INIT ( syslogger_desc
);
75 /******************************************************************************
79 ******************************************************************************
82 /** Host name (for log messages) */
83 static char *syslog_hostname
;
85 /** Domain name (for log messages) */
86 static char *syslog_domain
;
89 * Transmit formatted syslog message
91 * @v xfer Data transfer interface
92 * @v severity Severity
94 * @v terminator Message terminator
95 * @ret rc Return status code
97 int syslog_send ( struct interface
*xfer
, unsigned int severity
,
98 const char *message
, const char *terminator
) {
99 const char *hostname
= ( syslog_hostname ? syslog_hostname
: "" );
100 const char *domain
= ( ( hostname
[0] && syslog_domain
) ?
101 syslog_domain
: "" );
103 return xfer_printf ( xfer
, "<%d>%s%s%s%sipxe: %s%s",
104 SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY
,
105 severity
), hostname
,
106 ( domain
[0] ?
"." : "" ), domain
,
107 ( hostname
[0] ?
" " : "" ), message
, terminator
);
110 /******************************************************************************
114 ******************************************************************************
117 /** Syslog line buffer */
118 static char syslog_buffer
[SYSLOG_BUFSIZE
];
120 /** Syslog severity */
121 static unsigned int syslog_severity
= SYSLOG_DEFAULT_SEVERITY
;
124 * Handle ANSI set syslog priority (private sequence)
126 * @v ctx ANSI escape sequence context
127 * @v count Parameter count
128 * @v params List of graphic rendition aspects
130 static void syslog_handle_priority ( struct ansiesc_context
*ctx __unused
,
131 unsigned int count __unused
,
133 if ( params
[0] >= 0 ) {
134 syslog_severity
= params
[0];
136 syslog_severity
= SYSLOG_DEFAULT_SEVERITY
;
140 /** Syslog ANSI escape sequence handlers */
141 static struct ansiesc_handler syslog_handlers
[] = {
142 { ANSIESC_LOG_PRIORITY
, syslog_handle_priority
},
146 /** Syslog line console */
147 static struct line_console syslog_line
= {
148 .buffer
= syslog_buffer
,
149 .len
= sizeof ( syslog_buffer
),
151 .handlers
= syslog_handlers
,
155 /** Syslog recursion marker */
156 static int syslog_entered
;
159 * Print a character to syslog console
161 * @v character Character to be printed
163 static void syslog_putchar ( int character
) {
166 /* Ignore if we are already mid-logging */
167 if ( syslog_entered
)
170 /* Fill line buffer */
171 if ( line_putchar ( &syslog_line
, character
) == 0 )
174 /* Guard against re-entry */
177 /* Send log message */
178 if ( ( rc
= syslog_send ( &syslogger
, syslog_severity
,
179 syslog_buffer
, "" ) ) != 0 ) {
180 DBG ( "SYSLOG could not send log message: %s\n",
184 /* Clear re-entry flag */
188 /** Syslog console driver */
189 struct console_driver syslog_console __console_driver
= {
190 .putchar
= syslog_putchar
,
191 .disabled
= CONSOLE_DISABLED
,
192 .usage
= CONSOLE_SYSLOG
,
195 /******************************************************************************
199 ******************************************************************************
202 /** IPv4 syslog server setting */
203 const struct setting syslog_setting
__setting ( SETTING_MISC
, syslog
) = {
205 .description
= "Syslog server",
206 .tag
= DHCP_LOG_SERVERS
,
207 .type
= &setting_type_ipv4
,
210 /** IPv6 syslog server setting */
211 const struct setting syslog6_setting
__setting ( SETTING_MISC
, syslog6
) = {
213 .description
= "Syslog server",
214 .tag
= DHCPV6_LOG_SERVERS
,
215 .type
= &setting_type_ipv6
,
216 .scope
= &dhcpv6_scope
,
220 * Strip invalid characters from host/domain name
222 * @v name Name to strip
224 static void syslog_fix_name ( char *name
) {
228 /* Do nothing if name does not exist */
232 /* Strip any non-printable or whitespace characters from the name */
236 if ( isprint ( c
) && ! isspace ( c
) )
242 * Apply syslog settings
244 * @ret rc Return status code
246 static int apply_syslog_settings ( void ) {
247 struct sockaddr old_logserver
;
250 /* Fetch hostname and domain name */
251 free ( syslog_hostname
);
252 fetch_string_setting_copy ( NULL
, &hostname_setting
, &syslog_hostname
);
253 syslog_fix_name ( syslog_hostname
);
254 free ( syslog_domain
);
255 fetch_string_setting_copy ( NULL
, &domain_setting
, &syslog_domain
);
256 syslog_fix_name ( syslog_domain
);
258 /* Fetch log server */
259 syslog_console
.disabled
= CONSOLE_DISABLED
;
260 memcpy ( &old_logserver
, &logserver
, sizeof ( old_logserver
) );
261 logserver
.sa
.sa_family
= 0;
262 if ( fetch_ipv6_setting ( NULL
, &syslog6_setting
,
263 &logserver
.sin6
.sin6_addr
) >= 0 ) {
264 logserver
.sin6
.sin6_family
= AF_INET6
;
265 } else if ( fetch_ipv4_setting ( NULL
, &syslog_setting
,
266 &logserver
.sin
.sin_addr
) >= 0 ) {
267 logserver
.sin
.sin_family
= AF_INET
;
269 if ( logserver
.sa
.sa_family
) {
270 syslog_console
.disabled
= 0;
271 DBG ( "SYSLOG using log server %s\n",
272 sock_ntoa ( &logserver
.sa
) );
275 /* Do nothing unless log server has changed */
276 if ( memcmp ( &logserver
, &old_logserver
, sizeof ( logserver
) ) == 0 )
279 /* Reset syslog connection */
280 intf_restart ( &syslogger
, 0 );
282 /* Do nothing unless we have a log server */
283 if ( syslog_console
.disabled
) {
284 DBG ( "SYSLOG has no log server\n" );
288 /* Connect to log server */
289 if ( ( rc
= xfer_open_socket ( &syslogger
, SOCK_DGRAM
,
290 &logserver
.sa
, NULL
) ) != 0 ) {
291 DBG ( "SYSLOG cannot connect to log server: %s\n",
299 /** Syslog settings applicator */
300 struct settings_applicator syslog_applicator __settings_applicator
= {
301 .apply
= apply_syslog_settings
,