summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMichael Brown2011-03-09 14:38:56 +0100
committerMichael Brown2011-03-09 14:38:56 +0100
commitae92700fd4594afc50f057ccb07690567327e508 (patch)
treed7e588d813e7f50211350408cbc801d806878c6f /src
parent[image] Move the register_and_{select|boot}_image() functions to imgmgmt.c (diff)
downloadipxe-ae92700fd4594afc50f057ccb07690567327e508.tar.gz
ipxe-ae92700fd4594afc50f057ccb07690567327e508.tar.xz
ipxe-ae92700fd4594afc50f057ccb07690567327e508.zip
[image] Generalise "currently-running script" to "currently-running image"
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src')
-rw-r--r--src/core/image.c12
-rw-r--r--src/image/script.c34
-rw-r--r--src/include/ipxe/image.h1
3 files changed, 24 insertions, 23 deletions
diff --git a/src/core/image.c b/src/core/image.c
index afb647d2..d205768a 100644
--- a/src/core/image.c
+++ b/src/core/image.c
@@ -39,6 +39,9 @@ FILE_LICENCE ( GPL2_OR_LATER );
/** List of registered images */
struct list_head images = LIST_HEAD_INIT ( images );
+/** Currently-executing image */
+struct image *current_image;
+
/**
* Free executable image
*
@@ -200,6 +203,7 @@ int image_probe ( struct image *image ) {
* @ret rc Return status code
*/
int image_exec ( struct image *image ) {
+ struct image *saved_current_image;
struct image *replacement;
struct uri *old_cwuri;
int rc;
@@ -212,11 +216,14 @@ int image_exec ( struct image *image ) {
old_cwuri = uri_get ( cwuri );
churi ( image->uri );
+ /* Preserve record of any currently-running image */
+ saved_current_image = current_image;
+
/* Take out a temporary reference to the image. This allows
* the image to unregister itself if necessary, without
* automatically freeing itself.
*/
- image_get ( image );
+ current_image = image_get ( image );
/* Try executing the image */
if ( ( rc = image->type->exec ( image ) ) != 0 ) {
@@ -233,6 +240,9 @@ int image_exec ( struct image *image ) {
/* Drop temporary reference to the original image */
image_put ( image );
+ /* Restore previous currently-running image */
+ current_image = saved_current_image;
+
/* Reset current working directory */
churi ( old_cwuri );
uri_put ( old_cwuri );
diff --git a/src/image/script.c b/src/image/script.c
index 572eac71..9749b097 100644
--- a/src/image/script.c
+++ b/src/image/script.c
@@ -38,13 +38,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <usr/prompt.h>
#include <ipxe/script.h>
-/** Currently running script
- *
- * This is a global in order to allow goto_exec() to update the
- * offset.
- */
-static struct image *script;
-
/** Offset within current script
*
* This is a global in order to allow goto_exec() to update the
@@ -55,11 +48,13 @@ static size_t script_offset;
/**
* Process script lines
*
+ * @v image Script
* @v process_line Line processor
* @v terminate Termination check
* @ret rc Return status code
*/
-static int process_script ( int ( * process_line ) ( const char *line ),
+static int process_script ( struct image *image,
+ int ( * process_line ) ( const char *line ),
int ( * terminate ) ( int rc ) ) {
off_t eol;
size_t len;
@@ -70,17 +65,17 @@ static int process_script ( int ( * process_line ) ( const char *line ),
do {
/* Find length of next line, excluding any terminating '\n' */
- eol = memchr_user ( script->data, script_offset, '\n',
- ( script->len - script_offset ) );
+ eol = memchr_user ( image->data, script_offset, '\n',
+ ( image->len - script_offset ) );
if ( eol < 0 )
- eol = script->len;
+ eol = image->len;
len = ( eol - script_offset );
/* Copy line, terminate with NUL, and execute command */
{
char cmdbuf[ len + 1 ];
- copy_from_user ( cmdbuf, script->data,
+ copy_from_user ( cmdbuf, image->data,
script_offset, len );
cmdbuf[len] = '\0';
DBG ( "$ %s\n", cmdbuf );
@@ -94,7 +89,7 @@ static int process_script ( int ( * process_line ) ( const char *line ),
return rc;
}
- } while ( script_offset < script->len );
+ } while ( script_offset < image->len );
return rc;
}
@@ -138,7 +133,6 @@ static int script_exec_line ( const char *line ) {
* @ret rc Return status code
*/
static int script_exec ( struct image *image ) {
- struct image *saved_script;
size_t saved_offset;
int rc;
@@ -148,18 +142,14 @@ static int script_exec ( struct image *image ) {
unregister_image ( image );
/* Preserve state of any currently-running script */
- saved_script = script;
saved_offset = script_offset;
- /* Initialise state for this script */
- script = image;
-
/* Process script */
- rc = process_script ( script_exec_line, terminate_on_exit_or_failure );
+ rc = process_script ( image, script_exec_line,
+ terminate_on_exit_or_failure );
/* Restore saved state, re-register image, and return */
script_offset = saved_offset;
- script = saved_script;
register_image ( image );
return rc;
}
@@ -262,7 +252,7 @@ static int goto_exec ( int argc, char **argv ) {
return rc;
/* Sanity check */
- if ( ! script ) {
+ if ( ! current_image ) {
rc = -ENOTTY;
printf ( "Not in a script: %s\n", strerror ( rc ) );
return rc;
@@ -273,7 +263,7 @@ static int goto_exec ( int argc, char **argv ) {
/* Find label */
saved_offset = script_offset;
- if ( ( rc = process_script ( goto_find_label,
+ if ( ( rc = process_script ( current_image, goto_find_label,
terminate_on_label_found ) ) != 0 ) {
script_offset = saved_offset;
return rc;
diff --git a/src/include/ipxe/image.h b/src/include/ipxe/image.h
index 539f8ca3..32844413 100644
--- a/src/include/ipxe/image.h
+++ b/src/include/ipxe/image.h
@@ -110,6 +110,7 @@ struct image_type {
#define __image_type( probe_order ) __table_entry ( IMAGE_TYPES, probe_order )
extern struct list_head images;
+extern struct image *current_image;
/** Iterate over all registered images */
#define for_each_image( image ) \