Procházet zdrojové kódy

[image] Generalise "currently-running script" to "currently-running image"

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown před 13 roky
rodič
revize
ae92700fd4
3 změnil soubory, kde provedl 24 přidání a 23 odebrání
  1. 11
    1
      src/core/image.c
  2. 12
    22
      src/image/script.c
  3. 1
    0
      src/include/ipxe/image.h

+ 11
- 1
src/core/image.c Zobrazit soubor

39
 /** List of registered images */
39
 /** List of registered images */
40
 struct list_head images = LIST_HEAD_INIT ( images );
40
 struct list_head images = LIST_HEAD_INIT ( images );
41
 
41
 
42
+/** Currently-executing image */
43
+struct image *current_image;
44
+
42
 /**
45
 /**
43
  * Free executable image
46
  * Free executable image
44
  *
47
  *
200
  * @ret rc		Return status code
203
  * @ret rc		Return status code
201
  */
204
  */
202
 int image_exec ( struct image *image ) {
205
 int image_exec ( struct image *image ) {
206
+	struct image *saved_current_image;
203
 	struct image *replacement;
207
 	struct image *replacement;
204
 	struct uri *old_cwuri;
208
 	struct uri *old_cwuri;
205
 	int rc;
209
 	int rc;
212
 	old_cwuri = uri_get ( cwuri );
216
 	old_cwuri = uri_get ( cwuri );
213
 	churi ( image->uri );
217
 	churi ( image->uri );
214
 
218
 
219
+	/* Preserve record of any currently-running image */
220
+	saved_current_image = current_image;
221
+
215
 	/* Take out a temporary reference to the image.  This allows
222
 	/* Take out a temporary reference to the image.  This allows
216
 	 * the image to unregister itself if necessary, without
223
 	 * the image to unregister itself if necessary, without
217
 	 * automatically freeing itself.
224
 	 * automatically freeing itself.
218
 	 */
225
 	 */
219
-	image_get ( image );
226
+	current_image = image_get ( image );
220
 
227
 
221
 	/* Try executing the image */
228
 	/* Try executing the image */
222
 	if ( ( rc = image->type->exec ( image ) ) != 0 ) {
229
 	if ( ( rc = image->type->exec ( image ) ) != 0 ) {
233
 	/* Drop temporary reference to the original image */
240
 	/* Drop temporary reference to the original image */
234
 	image_put ( image );
241
 	image_put ( image );
235
 
242
 
243
+	/* Restore previous currently-running image */
244
+	current_image = saved_current_image;
245
+
236
 	/* Reset current working directory */
246
 	/* Reset current working directory */
237
 	churi ( old_cwuri );
247
 	churi ( old_cwuri );
238
 	uri_put ( old_cwuri );
248
 	uri_put ( old_cwuri );

+ 12
- 22
src/image/script.c Zobrazit soubor

38
 #include <usr/prompt.h>
38
 #include <usr/prompt.h>
39
 #include <ipxe/script.h>
39
 #include <ipxe/script.h>
40
 
40
 
41
-/** Currently running script
42
- *
43
- * This is a global in order to allow goto_exec() to update the
44
- * offset.
45
- */
46
-static struct image *script;
47
-
48
 /** Offset within current script
41
 /** Offset within current script
49
  *
42
  *
50
  * This is a global in order to allow goto_exec() to update the
43
  * This is a global in order to allow goto_exec() to update the
55
 /**
48
 /**
56
  * Process script lines
49
  * Process script lines
57
  *
50
  *
51
+ * @v image		Script
58
  * @v process_line	Line processor
52
  * @v process_line	Line processor
59
  * @v terminate		Termination check
53
  * @v terminate		Termination check
60
  * @ret rc		Return status code
54
  * @ret rc		Return status code
61
  */
55
  */
62
-static int process_script ( int ( * process_line ) ( const char *line ),
56
+static int process_script ( struct image *image,
57
+			    int ( * process_line ) ( const char *line ),
63
 			    int ( * terminate ) ( int rc ) ) {
58
 			    int ( * terminate ) ( int rc ) ) {
64
 	off_t eol;
59
 	off_t eol;
65
 	size_t len;
60
 	size_t len;
70
 	do {
65
 	do {
71
 	
66
 	
72
 		/* Find length of next line, excluding any terminating '\n' */
67
 		/* Find length of next line, excluding any terminating '\n' */
73
-		eol = memchr_user ( script->data, script_offset, '\n',
74
-				    ( script->len - script_offset ) );
68
+		eol = memchr_user ( image->data, script_offset, '\n',
69
+				    ( image->len - script_offset ) );
75
 		if ( eol < 0 )
70
 		if ( eol < 0 )
76
-			eol = script->len;
71
+			eol = image->len;
77
 		len = ( eol - script_offset );
72
 		len = ( eol - script_offset );
78
 
73
 
79
 		/* Copy line, terminate with NUL, and execute command */
74
 		/* Copy line, terminate with NUL, and execute command */
80
 		{
75
 		{
81
 			char cmdbuf[ len + 1 ];
76
 			char cmdbuf[ len + 1 ];
82
 
77
 
83
-			copy_from_user ( cmdbuf, script->data,
78
+			copy_from_user ( cmdbuf, image->data,
84
 					 script_offset, len );
79
 					 script_offset, len );
85
 			cmdbuf[len] = '\0';
80
 			cmdbuf[len] = '\0';
86
 			DBG ( "$ %s\n", cmdbuf );
81
 			DBG ( "$ %s\n", cmdbuf );
94
 				return rc;
89
 				return rc;
95
 		}
90
 		}
96
 
91
 
97
-	} while ( script_offset < script->len );
92
+	} while ( script_offset < image->len );
98
 
93
 
99
 	return rc;
94
 	return rc;
100
 }
95
 }
138
  * @ret rc		Return status code
133
  * @ret rc		Return status code
139
  */
134
  */
140
 static int script_exec ( struct image *image ) {
135
 static int script_exec ( struct image *image ) {
141
-	struct image *saved_script;
142
 	size_t saved_offset;
136
 	size_t saved_offset;
143
 	int rc;
137
 	int rc;
144
 
138
 
148
 	unregister_image ( image );
142
 	unregister_image ( image );
149
 
143
 
150
 	/* Preserve state of any currently-running script */
144
 	/* Preserve state of any currently-running script */
151
-	saved_script = script;
152
 	saved_offset = script_offset;
145
 	saved_offset = script_offset;
153
 
146
 
154
-	/* Initialise state for this script */
155
-	script = image;
156
-
157
 	/* Process script */
147
 	/* Process script */
158
-	rc = process_script ( script_exec_line, terminate_on_exit_or_failure );
148
+	rc = process_script ( image, script_exec_line,
149
+			      terminate_on_exit_or_failure );
159
 
150
 
160
 	/* Restore saved state, re-register image, and return */
151
 	/* Restore saved state, re-register image, and return */
161
 	script_offset = saved_offset;
152
 	script_offset = saved_offset;
162
-	script = saved_script;
163
 	register_image ( image );
153
 	register_image ( image );
164
 	return rc;
154
 	return rc;
165
 }
155
 }
262
 		return rc;
252
 		return rc;
263
 
253
 
264
 	/* Sanity check */
254
 	/* Sanity check */
265
-	if ( ! script ) {
255
+	if ( ! current_image ) {
266
 		rc = -ENOTTY;
256
 		rc = -ENOTTY;
267
 		printf ( "Not in a script: %s\n", strerror ( rc ) );
257
 		printf ( "Not in a script: %s\n", strerror ( rc ) );
268
 		return rc;
258
 		return rc;
273
 
263
 
274
 	/* Find label */
264
 	/* Find label */
275
 	saved_offset = script_offset;
265
 	saved_offset = script_offset;
276
-	if ( ( rc = process_script ( goto_find_label,
266
+	if ( ( rc = process_script ( current_image, goto_find_label,
277
 				     terminate_on_label_found ) ) != 0 ) {
267
 				     terminate_on_label_found ) ) != 0 ) {
278
 		script_offset = saved_offset;
268
 		script_offset = saved_offset;
279
 		return rc;
269
 		return rc;

+ 1
- 0
src/include/ipxe/image.h Zobrazit soubor

110
 #define __image_type( probe_order ) __table_entry ( IMAGE_TYPES, probe_order )
110
 #define __image_type( probe_order ) __table_entry ( IMAGE_TYPES, probe_order )
111
 
111
 
112
 extern struct list_head images;
112
 extern struct list_head images;
113
+extern struct image *current_image;
113
 
114
 
114
 /** Iterate over all registered images */
115
 /** Iterate over all registered images */
115
 #define for_each_image( image ) \
116
 #define for_each_image( image ) \

Načítá se…
Zrušit
Uložit