iPXE
vmconsole.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * VMware logfile console
00029  *
00030  */
00031 
00032 #include <string.h>
00033 #include <ipxe/console.h>
00034 #include <ipxe/lineconsole.h>
00035 #include <ipxe/init.h>
00036 #include <ipxe/guestrpc.h>
00037 #include <config/console.h>
00038 
00039 /** VMware logfile console buffer size */
00040 #define VMCONSOLE_BUFSIZE 128
00041 
00042 /* Set default console usage if applicable */
00043 #if ! ( defined ( CONSOLE_VMWARE ) && CONSOLE_EXPLICIT ( CONSOLE_VMWARE ) )
00044 #undef CONSOLE_VMWARE
00045 #define CONSOLE_VMWARE ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
00046 #endif
00047 
00048 /** VMware logfile console GuestRPC channel */
00049 static int vmconsole_channel;
00050 
00051 /** VMware logfile console line buffer */
00052 static struct {
00053         char prefix[4];
00054         char message[VMCONSOLE_BUFSIZE];
00055 } vmconsole_buffer = {
00056         .prefix = "log ",
00057 };
00058 
00059 /** VMware logfile console ANSI escape sequence handlers */
00060 static struct ansiesc_handler vmconsole_handlers[] = {
00061         { 0, NULL }
00062 };
00063 
00064 /** VMware logfile line console */
00065 static struct line_console vmconsole_line = {
00066         .buffer = vmconsole_buffer.message,
00067         .len = sizeof ( vmconsole_buffer.message ),
00068         .ctx = {
00069                 .handlers = vmconsole_handlers,
00070         },
00071 };
00072 
00073 /** VMware logfile console recursion marker */
00074 static int vmconsole_entered;
00075 
00076 /**
00077  * Print a character to VMware logfile console
00078  *
00079  * @v character         Character to be printed
00080  */
00081 static void vmconsole_putchar ( int character ) {
00082         int rc;
00083 
00084         /* Ignore if we are already mid-logging */
00085         if ( vmconsole_entered )
00086                 return;
00087 
00088         /* Fill line buffer */
00089         if ( line_putchar ( &vmconsole_line, character ) == 0 )
00090                 return;
00091 
00092         /* Guard against re-entry */
00093         vmconsole_entered = 1;
00094 
00095         /* Send log message */
00096         if ( ( rc = guestrpc_command ( vmconsole_channel,
00097                                        vmconsole_buffer.prefix, NULL, 0 ) ) <0){
00098                 DBG ( "VMware console could not send log message: %s\n",
00099                       strerror ( rc ) );
00100         }
00101 
00102         /* Clear re-entry flag */
00103         vmconsole_entered = 0;
00104 }
00105 
00106 /** VMware logfile console driver */
00107 struct console_driver vmconsole __console_driver = {
00108         .putchar = vmconsole_putchar,
00109         .disabled = CONSOLE_DISABLED,
00110         .usage = CONSOLE_VMWARE,
00111 };
00112 
00113 /**
00114  * Initialise VMware logfile console
00115  *
00116  */
00117 static void vmconsole_init ( void ) {
00118         int rc;
00119 
00120         /* Attempt to open console */
00121         vmconsole_channel = guestrpc_open();
00122         if ( vmconsole_channel < 0 ) {
00123                 rc = vmconsole_channel;
00124                 DBG ( "VMware console could not be initialised: %s\n",
00125                       strerror ( rc ) );
00126                 return;
00127         }
00128 
00129         /* Mark console as available */
00130         vmconsole.disabled = 0;
00131 }
00132 
00133 /**
00134  * VMware logfile console initialisation function
00135  */
00136 struct init_fn vmconsole_init_fn __init_fn ( INIT_CONSOLE ) = {
00137         .initialise = vmconsole_init,
00138 };