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
  */
194
  */
195
 void unregister_image ( struct image *image ) {
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
 	DBGC ( image, "IMAGE %s unregistered\n", image->name );
201
 	DBGC ( image, "IMAGE %s unregistered\n", image->name );
198
 	list_del ( &image->list );
202
 	list_del ( &image->list );
199
 	image->flags &= ~IMAGE_REGISTERED;
203
 	image->flags &= ~IMAGE_REGISTERED;
259
  */
263
  */
260
 int image_exec ( struct image *image ) {
264
 int image_exec ( struct image *image ) {
261
 	struct image *saved_current_image;
265
 	struct image *saved_current_image;
262
-	struct image *replacement;
266
+	struct image *replacement = NULL;
263
 	struct uri *old_cwuri;
267
 	struct uri *old_cwuri;
264
 	int rc;
268
 	int rc;
265
 
269
 
266
 	/* Sanity check */
270
 	/* Sanity check */
267
 	assert ( image->flags & IMAGE_REGISTERED );
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
 	/* Switch current working directory to be that of the image itself */
273
 	/* Switch current working directory to be that of the image itself */
280
 	old_cwuri = uri_get ( cwuri );
274
 	old_cwuri = uri_get ( cwuri );
281
 	churi ( image->uri );
275
 	churi ( image->uri );
289
 	 */
283
 	 */
290
 	current_image = image_get ( image );
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
 	/* Record boot attempt */
297
 	/* Record boot attempt */
293
 	syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name );
298
 	syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name );
294
 
299
 
317
 	if ( replacement )
322
 	if ( replacement )
318
 		assert ( replacement->flags & IMAGE_REGISTERED );
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
 	/* Drop temporary reference to the original image */
330
 	/* Drop temporary reference to the original image */
321
 	image_put ( image );
331
 	image_put ( image );
322
 
332
 

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

38
 struct imgsingle_options {
38
 struct imgsingle_options {
39
 	/** Image name */
39
 	/** Image name */
40
 	const char *name;
40
 	const char *name;
41
+	/** Free image after execution */
42
+	int autofree;
41
 };
43
 };
42
 
44
 
43
 /** "img{single}" option list */
45
 /** "img{single}" option list */
44
 static struct option_descriptor imgsingle_opts[] = {
46
 static struct option_descriptor imgsingle_opts[] = {
45
 	OPTION_DESC ( "name", 'n', required_argument,
47
 	OPTION_DESC ( "name", 'n', required_argument,
46
 		      struct imgsingle_options, name, parse_string ),
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
 /** "img{single}" command descriptor */
53
 /** "img{single}" command descriptor */
50
 static struct command_descriptor imgsingle_cmd =
54
 static struct command_descriptor imgsingle_cmd =
51
 	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
55
 	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
52
 		       1, MAX_ARGUMENTS,
56
 		       1, MAX_ARGUMENTS,
53
-		       "[--name <name>] <uri|image> [<arguments>...]" );
57
+		       "[--name <name>] [--autofree] "
58
+		       "<uri|image> [<arguments>...]" );
54
 
59
 
55
 /** An "img{single}" family command descriptor */
60
 /** An "img{single}" family command descriptor */
56
 struct imgsingle_descriptor {
61
 struct imgsingle_descriptor {
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
 	/* Carry out command action, if applicable */
146
 	/* Carry out command action, if applicable */
138
 	if ( desc->action ) {
147
 	if ( desc->action ) {
139
 		if ( ( rc = desc->action ( image ) ) != 0 ) {
148
 		if ( ( rc = desc->action ( image ) ) != 0 ) {
160
 static struct command_descriptor imgfetch_cmd =
169
 static struct command_descriptor imgfetch_cmd =
161
 	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
170
 	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
162
 		       1, MAX_ARGUMENTS,
171
 		       1, MAX_ARGUMENTS,
163
-		       "[--name <name>] <uri> [<arguments>...]" );
172
+		       "[--name <name>] [--autofree] <uri> [<arguments>...]" );
164
 
173
 
165
 /** "imgfetch" family command descriptor */
174
 /** "imgfetch" family command descriptor */
166
 struct imgsingle_descriptor imgfetch_desc = {
175
 struct imgsingle_descriptor imgfetch_desc = {
202
 static struct command_descriptor imgexec_cmd =
211
 static struct command_descriptor imgexec_cmd =
203
 	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
212
 	COMMAND_DESC ( struct imgsingle_options, imgsingle_opts,
204
 		       0, MAX_ARGUMENTS,
213
 		       0, MAX_ARGUMENTS,
205
-		       "[--name <name>] [<uri|image> [<arguments>...]]" );
214
+		       "[--autofree] [<uri|image> [<arguments>...]]" );
206
 
215
 
207
 /** "imgexec" family command descriptor */
216
 /** "imgexec" family command descriptor */
208
 struct imgsingle_descriptor imgexec_desc = {
217
 struct imgsingle_descriptor imgexec_desc = {

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

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

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

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

Loading…
Cancel
Save