Browse Source

[image] Add "--autofree" option

Allow images to be automatically freed after execution completes
(successfully or otherwise).

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
d3c660b671
4 changed files with 38 additions and 14 deletions
  1. 21
    11
      src/core/image.c
  2. 12
    3
      src/hci/commands/image_cmd.c
  3. 3
    0
      src/include/ipxe/image.h
  4. 2
    0
      src/usr/imgmgmt.c

+ 21
- 11
src/core/image.c View File

@@ -194,6 +194,10 @@ int register_image ( struct image *image ) {
194 194
  */
195 195
 void unregister_image ( struct image *image ) {
196 196
 
197
+	/* Do nothing unless image is registered */
198
+	if ( ! ( image->flags & IMAGE_REGISTERED ) )
199
+		return;
200
+
197 201
 	DBGC ( image, "IMAGE %s unregistered\n", image->name );
198 202
 	list_del ( &image->list );
199 203
 	image->flags &= ~IMAGE_REGISTERED;
@@ -259,23 +263,13 @@ int image_probe ( struct image *image ) {
259 263
  */
260 264
 int image_exec ( struct image *image ) {
261 265
 	struct image *saved_current_image;
262
-	struct image *replacement;
266
+	struct image *replacement = NULL;
263 267
 	struct uri *old_cwuri;
264 268
 	int rc;
265 269
 
266 270
 	/* Sanity check */
267 271
 	assert ( image->flags & IMAGE_REGISTERED );
268 272
 
269
-	/* Check that this image can be selected for execution */
270
-	if ( ( rc = image_select ( image ) ) != 0 )
271
-		return rc;
272
-
273
-	/* Check that image is trusted (if applicable) */
274
-	if ( require_trusted_images && ! ( image->flags & IMAGE_TRUSTED ) ) {
275
-		DBGC ( image, "IMAGE %s is not trusted\n", image->name );
276
-		return -EACCES_UNTRUSTED;
277
-	}
278
-
279 273
 	/* Switch current working directory to be that of the image itself */
280 274
 	old_cwuri = uri_get ( cwuri );
281 275
 	churi ( image->uri );
@@ -289,6 +283,17 @@ int image_exec ( struct image *image ) {
289 283
 	 */
290 284
 	current_image = image_get ( image );
291 285
 
286
+	/* Check that this image can be selected for execution */
287
+	if ( ( rc = image_select ( image ) ) != 0 )
288
+		goto err;
289
+
290
+	/* Check that image is trusted (if applicable) */
291
+	if ( require_trusted_images && ! ( image->flags & IMAGE_TRUSTED ) ) {
292
+		DBGC ( image, "IMAGE %s is not trusted\n", image->name );
293
+		rc = -EACCES_UNTRUSTED;
294
+		goto err;
295
+	}
296
+
292 297
 	/* Record boot attempt */
293 298
 	syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name );
294 299
 
@@ -317,6 +322,11 @@ int image_exec ( struct image *image ) {
317 322
 	if ( replacement )
318 323
 		assert ( replacement->flags & IMAGE_REGISTERED );
319 324
 
325
+ err:
326
+	/* Unregister image if applicable */
327
+	if ( image->flags & IMAGE_AUTO_UNREGISTER )
328
+		unregister_image ( image );
329
+
320 330
 	/* Drop temporary reference to the original image */
321 331
 	image_put ( image );
322 332
 

+ 12
- 3
src/hci/commands/image_cmd.c View File

@@ -38,19 +38,24 @@ FILE_LICENCE ( GPL2_OR_LATER );
38 38
 struct imgsingle_options {
39 39
 	/** Image name */
40 40
 	const char *name;
41
+	/** Free image after execution */
42
+	int autofree;
41 43
 };
42 44
 
43 45
 /** "img{single}" option list */
44 46
 static struct option_descriptor imgsingle_opts[] = {
45 47
 	OPTION_DESC ( "name", 'n', required_argument,
46 48
 		      struct imgsingle_options, name, parse_string ),
49
+	OPTION_DESC ( "autofree", 'a', no_argument,
50
+		      struct imgsingle_options, autofree, parse_flag ),
47 51
 };
48 52
 
49 53
 /** "img{single}" command descriptor */
50 54
 static struct command_descriptor imgsingle_cmd =
51 55
 	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
52 56
 		       1, MAX_ARGUMENTS,
53
-		       "[--name <name>] <uri|image> [<arguments>...]" );
57
+		       "[--name <name>] [--autofree] "
58
+		       "<uri|image> [<arguments>...]" );
54 59
 
55 60
 /** An "img{single}" family command descriptor */
56 61
 struct imgsingle_descriptor {
@@ -134,6 +139,10 @@ static int imgsingle_exec ( int argc, char **argv,
134 139
 		}
135 140
 	}
136 141
 
142
+	/* Set the auto-unregister flag, if applicable */
143
+	if ( opts.autofree )
144
+		image->flags |= IMAGE_AUTO_UNREGISTER;
145
+
137 146
 	/* Carry out command action, if applicable */
138 147
 	if ( desc->action ) {
139 148
 		if ( ( rc = desc->action ( image ) ) != 0 ) {
@@ -160,7 +169,7 @@ static int imgsingle_exec ( int argc, char **argv,
160 169
 static struct command_descriptor imgfetch_cmd =
161 170
 	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
162 171
 		       1, MAX_ARGUMENTS,
163
-		       "[--name <name>] <uri> [<arguments>...]" );
172
+		       "[--name <name>] [--autofree] <uri> [<arguments>...]" );
164 173
 
165 174
 /** "imgfetch" family command descriptor */
166 175
 struct imgsingle_descriptor imgfetch_desc = {
@@ -202,7 +211,7 @@ static int imgselect_exec ( int argc, char **argv ) {
202 211
 static struct command_descriptor imgexec_cmd =
203 212
 	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
204 213
 		       0, MAX_ARGUMENTS,
205
-		       "[--name <name>] [<uri|image> [<arguments>...]]" );
214
+		       "[--autofree] [<uri|image> [<arguments>...]]" );
206 215
 
207 216
 /** "imgexec" family command descriptor */
208 217
 struct imgsingle_descriptor imgexec_desc = {

+ 3
- 0
src/include/ipxe/image.h View File

@@ -67,6 +67,9 @@ struct image {
67 67
 /** Image is trusted */
68 68
 #define IMAGE_TRUSTED 0x0004
69 69
 
70
+/** Image will be automatically unregistered after execution */
71
+#define IMAGE_AUTO_UNREGISTER 0x0008
72
+
70 73
 /** An executable image type */
71 74
 struct image_type {
72 75
 	/** Name of this image type */

+ 2
- 0
src/usr/imgmgmt.c View File

@@ -146,6 +146,8 @@ void imgstat ( struct image *image ) {
146 146
 		printf ( " [TRUSTED]" );
147 147
 	if ( image->flags & IMAGE_SELECTED )
148 148
 		printf ( " [SELECTED]" );
149
+	if ( image->flags & IMAGE_AUTO_UNREGISTER )
150
+		printf ( " [AUTOFREE]" );
149 151
 	if ( image->cmdline )
150 152
 		printf ( " \"%s\"", image->cmdline );
151 153
 	printf ( "\n" );

Loading…
Cancel
Save