You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

syslog.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Syslog protocol
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <byteswap.h>
  28. #include <ipxe/xfer.h>
  29. #include <ipxe/open.h>
  30. #include <ipxe/tcpip.h>
  31. #include <ipxe/dhcp.h>
  32. #include <ipxe/settings.h>
  33. #include <ipxe/console.h>
  34. #include <ipxe/lineconsole.h>
  35. #include <ipxe/syslog.h>
  36. #include <config/console.h>
  37. /* Set default console usage if applicable */
  38. #if ! ( defined ( CONSOLE_SYSLOG ) && CONSOLE_EXPLICIT ( CONSOLE_SYSLOG ) )
  39. #undef CONSOLE_SYSLOG
  40. #define CONSOLE_SYSLOG ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
  41. #endif
  42. /** The syslog server */
  43. static struct sockaddr_tcpip logserver = {
  44. .st_family = AF_INET,
  45. .st_port = htons ( SYSLOG_PORT ),
  46. };
  47. /** Syslog UDP interface operations */
  48. static struct interface_operation syslogger_operations[] = {};
  49. /** Syslog UDP interface descriptor */
  50. static struct interface_descriptor syslogger_desc =
  51. INTF_DESC_PURE ( syslogger_operations );
  52. /** The syslog UDP interface */
  53. static struct interface syslogger = INTF_INIT ( syslogger_desc );
  54. /******************************************************************************
  55. *
  56. * Console driver
  57. *
  58. ******************************************************************************
  59. */
  60. /** Host name (for log messages) */
  61. static char *syslog_hostname;
  62. /** Domain name (for log messages) */
  63. static char *syslog_domain;
  64. /**
  65. * Transmit formatted syslog message
  66. *
  67. * @v xfer Data transfer interface
  68. * @v severity Severity
  69. * @v message Message
  70. * @v terminator Message terminator
  71. * @ret rc Return status code
  72. */
  73. int syslog_send ( struct interface *xfer, unsigned int severity,
  74. const char *message, const char *terminator ) {
  75. return xfer_printf ( xfer, "<%d>%s%s%s%sipxe: %s%s",
  76. SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY,
  77. severity ),
  78. ( syslog_hostname ? syslog_hostname : "" ),
  79. ( syslog_domain ? "." : "" ),
  80. ( syslog_domain ? syslog_domain : "" ),
  81. ( ( syslog_hostname || syslog_domain ) ? " " : ""),
  82. message, terminator );
  83. }
  84. /******************************************************************************
  85. *
  86. * Console driver
  87. *
  88. ******************************************************************************
  89. */
  90. /** Syslog line buffer */
  91. static char syslog_buffer[SYSLOG_BUFSIZE];
  92. /** Syslog severity */
  93. static unsigned int syslog_severity = SYSLOG_DEFAULT_SEVERITY;
  94. /**
  95. * Handle ANSI set syslog priority (private sequence)
  96. *
  97. * @v ctx ANSI escape sequence context
  98. * @v count Parameter count
  99. * @v params List of graphic rendition aspects
  100. */
  101. static void syslog_handle_priority ( struct ansiesc_context *ctx __unused,
  102. unsigned int count __unused,
  103. int params[] ) {
  104. if ( params[0] >= 0 ) {
  105. syslog_severity = params[0];
  106. } else {
  107. syslog_severity = SYSLOG_DEFAULT_SEVERITY;
  108. }
  109. }
  110. /** Syslog ANSI escape sequence handlers */
  111. static struct ansiesc_handler syslog_handlers[] = {
  112. { ANSIESC_LOG_PRIORITY, syslog_handle_priority },
  113. { 0, NULL }
  114. };
  115. /** Syslog line console */
  116. static struct line_console syslog_line = {
  117. .buffer = syslog_buffer,
  118. .len = sizeof ( syslog_buffer ),
  119. .ctx = {
  120. .handlers = syslog_handlers,
  121. },
  122. };
  123. /** Syslog recursion marker */
  124. static int syslog_entered;
  125. /**
  126. * Print a character to syslog console
  127. *
  128. * @v character Character to be printed
  129. */
  130. static void syslog_putchar ( int character ) {
  131. int rc;
  132. /* Ignore if we are already mid-logging */
  133. if ( syslog_entered )
  134. return;
  135. /* Fill line buffer */
  136. if ( line_putchar ( &syslog_line, character ) == 0 )
  137. return;
  138. /* Guard against re-entry */
  139. syslog_entered = 1;
  140. /* Send log message */
  141. if ( ( rc = syslog_send ( &syslogger, syslog_severity,
  142. syslog_buffer, "" ) ) != 0 ) {
  143. DBG ( "SYSLOG could not send log message: %s\n",
  144. strerror ( rc ) );
  145. }
  146. /* Clear re-entry flag */
  147. syslog_entered = 0;
  148. }
  149. /** Syslog console driver */
  150. struct console_driver syslog_console __console_driver = {
  151. .putchar = syslog_putchar,
  152. .disabled = CONSOLE_DISABLED,
  153. .usage = CONSOLE_SYSLOG,
  154. };
  155. /******************************************************************************
  156. *
  157. * Settings
  158. *
  159. ******************************************************************************
  160. */
  161. /** Syslog server setting */
  162. const struct setting syslog_setting __setting ( SETTING_MISC ) = {
  163. .name = "syslog",
  164. .description = "Syslog server",
  165. .tag = DHCP_LOG_SERVERS,
  166. .type = &setting_type_ipv4,
  167. };
  168. /**
  169. * Apply syslog settings
  170. *
  171. * @ret rc Return status code
  172. */
  173. static int apply_syslog_settings ( void ) {
  174. struct sockaddr_in *sin_logserver =
  175. ( struct sockaddr_in * ) &logserver;
  176. struct in_addr old_addr;
  177. int len;
  178. int rc;
  179. /* Fetch hostname and domain name */
  180. free ( syslog_hostname );
  181. fetch_string_setting_copy ( NULL, &hostname_setting, &syslog_hostname );
  182. free ( syslog_domain );
  183. fetch_string_setting_copy ( NULL, &domain_setting, &syslog_domain );
  184. /* Fetch log server */
  185. syslog_console.disabled = CONSOLE_DISABLED;
  186. old_addr.s_addr = sin_logserver->sin_addr.s_addr;
  187. if ( ( len = fetch_ipv4_setting ( NULL, &syslog_setting,
  188. &sin_logserver->sin_addr ) ) >= 0 ) {
  189. syslog_console.disabled = 0;
  190. }
  191. /* Do nothing unless log server has changed */
  192. if ( sin_logserver->sin_addr.s_addr == old_addr.s_addr )
  193. return 0;
  194. /* Reset syslog connection */
  195. intf_restart ( &syslogger, 0 );
  196. /* Do nothing unless we have a log server */
  197. if ( syslog_console.disabled ) {
  198. DBG ( "SYSLOG has no log server\n" );
  199. return 0;
  200. }
  201. /* Connect to log server */
  202. if ( ( rc = xfer_open_socket ( &syslogger, SOCK_DGRAM,
  203. ( ( struct sockaddr * ) &logserver ),
  204. NULL ) ) != 0 ) {
  205. DBG ( "SYSLOG cannot connect to log server: %s\n",
  206. strerror ( rc ) );
  207. return rc;
  208. }
  209. DBG ( "SYSLOG using log server %s\n",
  210. inet_ntoa ( sin_logserver->sin_addr ) );
  211. return 0;
  212. }
  213. /** Syslog settings applicator */
  214. struct settings_applicator syslog_applicator __settings_applicator = {
  215. .apply = apply_syslog_settings,
  216. };