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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /** @file
  20. *
  21. * Syslog protocol
  22. *
  23. */
  24. #include <stdint.h>
  25. #include <byteswap.h>
  26. #include <ipxe/xfer.h>
  27. #include <ipxe/open.h>
  28. #include <ipxe/tcpip.h>
  29. #include <ipxe/dhcp.h>
  30. #include <ipxe/settings.h>
  31. #include <ipxe/console.h>
  32. #include <ipxe/ansiesc.h>
  33. #include <ipxe/syslog.h>
  34. /** The syslog server */
  35. static struct sockaddr_tcpip logserver = {
  36. .st_family = AF_INET,
  37. .st_port = htons ( SYSLOG_PORT ),
  38. };
  39. /** Syslog UDP interface operations */
  40. static struct interface_operation syslogger_operations[] = {};
  41. /** Syslog UDP interface descriptor */
  42. static struct interface_descriptor syslogger_desc =
  43. INTF_DESC_PURE ( syslogger_operations );
  44. /** The syslog UDP interface */
  45. static struct interface syslogger = INTF_INIT ( syslogger_desc );
  46. /******************************************************************************
  47. *
  48. * Console driver
  49. *
  50. ******************************************************************************
  51. */
  52. /** Syslog line buffer */
  53. static char syslog_buffer[SYSLOG_BUFSIZE];
  54. /** Index into syslog line buffer */
  55. static unsigned int syslog_idx;
  56. /** Syslog recursion marker */
  57. static int syslog_entered;
  58. /** Syslog ANSI escape sequence handlers */
  59. static struct ansiesc_handler syslog_ansiesc_handlers[] = {
  60. { 0, NULL }
  61. };
  62. /** Syslog ANSI escape sequence context */
  63. static struct ansiesc_context syslog_ansiesc_ctx = {
  64. .handlers = syslog_ansiesc_handlers,
  65. };
  66. /**
  67. * Print a character to syslog console
  68. *
  69. * @v character Character to be printed
  70. */
  71. static void syslog_putchar ( int character ) {
  72. int rc;
  73. /* Ignore if we are already mid-logging */
  74. if ( syslog_entered )
  75. return;
  76. /* Strip ANSI escape sequences */
  77. character = ansiesc_process ( &syslog_ansiesc_ctx, character );
  78. if ( character < 0 )
  79. return;
  80. /* Ignore carriage return */
  81. if ( character == '\r' )
  82. return;
  83. /* Treat newline as a terminator */
  84. if ( character == '\n' )
  85. character = 0;
  86. /* Add character to buffer */
  87. syslog_buffer[syslog_idx++] = character;
  88. /* Do nothing more unless we reach end-of-line (or end-of-buffer) */
  89. if ( ( character != 0 ) &&
  90. ( syslog_idx < ( sizeof ( syslog_buffer ) - 1 /* NUL */ ) ) ) {
  91. return;
  92. }
  93. /* Reset to start of buffer */
  94. syslog_idx = 0;
  95. /* Guard against re-entry */
  96. syslog_entered = 1;
  97. /* Send log message */
  98. if ( ( rc = xfer_printf ( &syslogger, "<%d>ipxe: %s",
  99. SYSLOG_PRIORITY ( SYSLOG_FACILITY,
  100. SYSLOG_SEVERITY ),
  101. syslog_buffer ) ) != 0 ) {
  102. DBG ( "SYSLOG could not send log message: %s\n",
  103. strerror ( rc ) );
  104. }
  105. /* Clear re-entry flag */
  106. syslog_entered = 0;
  107. }
  108. /** Syslog console driver */
  109. struct console_driver syslog_console __console_driver = {
  110. .putchar = syslog_putchar,
  111. .disabled = 1,
  112. };
  113. /******************************************************************************
  114. *
  115. * Settings
  116. *
  117. ******************************************************************************
  118. */
  119. /** Syslog server setting */
  120. struct setting syslog_setting __setting ( SETTING_MISC ) = {
  121. .name = "syslog",
  122. .description = "Syslog server",
  123. .tag = DHCP_LOG_SERVERS,
  124. .type = &setting_type_ipv4,
  125. };
  126. /**
  127. * Apply syslog settings
  128. *
  129. * @ret rc Return status code
  130. */
  131. static int apply_syslog_settings ( void ) {
  132. struct sockaddr_in *sin_logserver =
  133. ( struct sockaddr_in * ) &logserver;
  134. struct in_addr old_addr;
  135. int len;
  136. int rc;
  137. /* Fetch log server */
  138. syslog_console.disabled = 1;
  139. old_addr.s_addr = sin_logserver->sin_addr.s_addr;
  140. if ( ( len = fetch_ipv4_setting ( NULL, &syslog_setting,
  141. &sin_logserver->sin_addr ) ) >= 0 ) {
  142. syslog_console.disabled = 0;
  143. }
  144. /* Do nothing unless log server has changed */
  145. if ( sin_logserver->sin_addr.s_addr == old_addr.s_addr )
  146. return 0;
  147. /* Reset syslog connection */
  148. intf_restart ( &syslogger, 0 );
  149. /* Do nothing unless we have a log server */
  150. if ( syslog_console.disabled ) {
  151. DBG ( "SYSLOG has no log server\n" );
  152. return 0;
  153. }
  154. /* Connect to log server */
  155. if ( ( rc = xfer_open_socket ( &syslogger, SOCK_DGRAM,
  156. ( ( struct sockaddr * ) &logserver ),
  157. NULL ) ) != 0 ) {
  158. DBG ( "SYSLOG cannot connect to log server: %s\n",
  159. strerror ( rc ) );
  160. return rc;
  161. }
  162. DBG ( "SYSLOG using log server %s\n",
  163. inet_ntoa ( sin_logserver->sin_addr ) );
  164. return 0;
  165. }
  166. /** Syslog settings applicator */
  167. struct settings_applicator syslog_applicator __settings_applicator = {
  168. .apply = apply_syslog_settings,
  169. };