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

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