script.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 /**
00022  * @file
00023  *
00024  * gPXE scripts
00025  *
00026  */
00027 
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <ctype.h>
00031 #include <errno.h>
00032 #include <gpxe/image.h>
00033 
00034 struct image_type script_image_type __image_type ( PROBE_NORMAL );
00035 
00036 /**
00037  * Execute script
00038  *
00039  * @v image             Script
00040  * @ret rc              Return status code
00041  */
00042 static int script_exec ( struct image *image ) {
00043         size_t offset = 0;
00044         off_t eol;
00045         size_t len;
00046         int rc;
00047 
00048         /* Temporarily de-register image, so that a "boot" command
00049          * doesn't throw us into an execution loop.
00050          */
00051         unregister_image ( image );
00052 
00053         while ( offset < image->len ) {
00054         
00055                 /* Find length of next line, excluding any terminating '\n' */
00056                 eol = memchr_user ( image->data, offset, '\n',
00057                                     ( image->len - offset ) );
00058                 if ( eol < 0 )
00059                         eol = image->len;
00060                 len = ( eol - offset );
00061 
00062                 /* Copy line, terminate with NUL, and execute command */
00063                 {
00064                         char cmdbuf[ len + 1 ];
00065 
00066                         copy_from_user ( cmdbuf, image->data, offset, len );
00067                         cmdbuf[len] = '\0';
00068                         DBG ( "$ %s\n", cmdbuf );
00069                         if ( ( rc = system ( cmdbuf ) ) != 0 ) {
00070                                 DBG ( "Command \"%s\" failed: %s\n",
00071                                       cmdbuf, strerror ( rc ) );
00072                                 goto done;
00073                         }
00074                 }
00075                 
00076                 /* Move to next line */
00077                 offset += ( len + 1 );
00078         }
00079 
00080         rc = 0;
00081  done:
00082         /* Re-register image and return */
00083         register_image ( image );
00084         return rc;
00085 }
00086 
00087 /**
00088  * Load script into memory
00089  *
00090  * @v image             Script
00091  * @ret rc              Return status code
00092  */
00093 static int script_load ( struct image *image ) {
00094         static const char magic[] = "#!gpxe";
00095         char test[ sizeof ( magic ) - 1 /* NUL */ + 1 /* terminating space */];
00096 
00097         /* Sanity check */
00098         if ( image->len < sizeof ( test ) ) {
00099                 DBG ( "Too short to be a script\n" );
00100                 return -ENOEXEC;
00101         }
00102 
00103         /* Check for magic signature */
00104         copy_from_user ( test, image->data, 0, sizeof ( test ) );
00105         if ( ( memcmp ( test, magic, ( sizeof ( test ) - 1 ) ) != 0 ) ||
00106              ! isspace ( test[ sizeof ( test ) - 1 ] ) ) {
00107                 DBG ( "Invalid magic signature\n" );
00108                 return -ENOEXEC;
00109         }
00110 
00111         /* This is a script */
00112         image->type = &script_image_type;
00113 
00114         /* We don't actually load it anywhere; we will pick the lines
00115          * out of the image as we need them.
00116          */
00117 
00118         return 0;
00119 }
00120 
00121 /** Script image type */
00122 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
00123         .name = "script",
00124         .load = script_load,
00125         .exec = script_exec,
00126 };

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1