script.c File Reference

gPXE scripts More...

#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <gpxe/image.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
struct image_type script_image_type __image_type (PROBE_NORMAL)
 Script image type.
static int script_exec (struct image *image)
 Execute script.
static int script_load (struct image *image)
 Load script into memory.


Detailed Description

gPXE scripts

Definition in file script.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

struct image_type script_image_type __image_type ( PROBE_NORMAL   )  [read]

Script image type.

static int script_exec ( struct image image  )  [static]

Execute script.

Parameters:
image Script
Return values:
rc Return status code

Definition at line 42 of file script.c.

References copy_from_user(), image::data, DBG, image::len, memchr_user(), offset, register_image(), strerror(), system(), and unregister_image().

00042                                                {
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 }

static int script_load ( struct image image  )  [static]

Load script into memory.

Parameters:
image Script
Return values:
rc Return status code

Definition at line 93 of file script.c.

References copy_from_user(), image::data, DBG, ENOEXEC, isspace(), image::len, memcmp(), test, and image::type.

00093                                                {
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 }


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