From c763f15030565eef2e8b28fdf471ef3e7dd9b933 Mon Sep 17 00:00:00 2001 From: Geoff Levand Date: Thu, 9 Jul 2009 14:45:48 -0700 Subject: Delete kexec temporary files before rebooting Signed-off-by: Geoff Levand --- lib/system/system.c | 1 + lib/system/system.h | 1 + ui/common/loader.c | 21 +++++++++++-- ui/common/loader.h | 2 +- ui/common/ui-system.c | 84 +++++++++++++++++++++++++++++++++++++-------------- 5 files changed, 83 insertions(+), 26 deletions(-) diff --git a/lib/system/system.c b/lib/system/system.c index 65bd6bf..7371445 100644 --- a/lib/system/system.c +++ b/lib/system/system.c @@ -20,6 +20,7 @@ const struct pb_system_apps pb_system_apps = { .cp = "/bin/cp", .kexec = "/sbin/kexec", .mount = "/bin/mount", + .shutdown = "/sbin/shutdown", .sftp = "/usr/bin/sftp", .tftp = "/usr/bin/tftp", .umount = "/bin/umount", diff --git a/lib/system/system.h b/lib/system/system.h index 47c7c02..1918309 100644 --- a/lib/system/system.h +++ b/lib/system/system.h @@ -5,6 +5,7 @@ struct pb_system_apps { const char *cp; const char *kexec; const char *mount; + const char *shutdown; const char *sftp; const char *tftp; const char *umount; diff --git a/ui/common/loader.c b/ui/common/loader.c index 0fe62a0..5c69533 100644 --- a/ui/common/loader.c +++ b/ui/common/loader.c @@ -263,16 +263,22 @@ fail: /** * pb_load_file - Loads a remote file and returns the local file path. * @ctx: The talloc context to associate with the returned string. + * @remote: The remote file URL. + * @tempfile: An optional variable pointer to be set when a temporary local + * file is created. * * Returns the local file path in a talloc'ed character string on success, * or NULL on error. */ -char *pb_load_file(void *ctx, const char *remote) +char *pb_load_file(void *ctx, const char *remote, unsigned int *tempfile) { char *local; struct pb_url *url = pb_url_parse(NULL, remote); + if (tempfile) + *tempfile = 0; + if (!url) return NULL; @@ -280,19 +286,28 @@ char *pb_load_file(void *ctx, const char *remote) case pb_url_ftp: case pb_url_http: local = pb_load_wget(ctx, url, 0); + if (tempfile && local) + *tempfile = 1; break; case pb_url_https: - local = pb_load_wget(ctx, url, - wget_no_check_certificate); + local = pb_load_wget(ctx, url, wget_no_check_certificate); + if (tempfile && local) + *tempfile = 1; break; case pb_url_nfs: local = pb_load_nfs(ctx, url); + if (tempfile && local) + *tempfile = 1; break; case pb_url_sftp: local = pb_load_sftp(ctx, url); + if (tempfile && local) + *tempfile = 1; break; case pb_url_tftp: local = pb_load_tftp(ctx, url); + if (tempfile && local) + *tempfile = 1; break; default: local = talloc_strdup(ctx, url->full); diff --git a/ui/common/loader.h b/ui/common/loader.h index b06bb43..42d4d4b 100644 --- a/ui/common/loader.h +++ b/ui/common/loader.h @@ -19,6 +19,6 @@ #if !defined(_PB_FILE_LOADER_H) #define _PB_FILE_LOADER_H -char *pb_load_file(void *ctx, const char *remote); +char *pb_load_file(void *ctx, const char *remote, unsigned int *tempfile); #endif diff --git a/ui/common/ui-system.c b/ui/common/ui-system.c index bd6dd31..0140f0e 100644 --- a/ui/common/ui-system.c +++ b/ui/common/ui-system.c @@ -33,13 +33,13 @@ #include "ui-system.h" /** - * run_kexec_local - Final kexec helper. + * kexec_load - kexec load helper. * @l_image: The local image file for kexec to execute. * @l_initrd: Optional local initrd file for kexec --initrd, can be NULL. * @args: Optional command line args for kexec --append, can be NULL. */ -static int run_kexec_local(const char *l_image, const char *l_initrd, +static int kexec_load(const char *l_image, const char *l_initrd, const char *args) { int result; @@ -49,34 +49,64 @@ static int run_kexec_local(const char *l_image, const char *l_initrd, char *s_args = NULL; p = argv; - *p++ = pb_system_apps.kexec; /* 1 */ + *p++ = pb_system_apps.kexec; /* 1 */ + *p++ = "-l"; /* 2 */ if (l_initrd) { s_initrd = talloc_asprintf(NULL, "--initrd=%s", l_initrd); assert(s_initrd); - *p++ = s_initrd; /* 2 */ + *p++ = s_initrd; /* 3 */ } if (args) { s_args = talloc_asprintf(NULL, "--append=%s", args); assert(s_args); - *p++ = s_args; /* 3 */ + *p++ = s_args; /* 4 */ } - /* First try by telling kexec to run shutdown */ + *p++ = l_image; /* 5 */ + *p++ = NULL; /* 6 */ - *(p + 0) = l_image; - *(p + 1) = NULL; + result = pb_run_cmd(argv); + + if (result) + pb_log("%s: failed: (%d)\n", __func__, result); + + talloc_free(s_initrd); + talloc_free(s_args); + + return result; +} + +/** + * kexec_reboot - Helper to boot the new kernel. + * + * Must only be called after a successful call to kexec_load(). + */ + +static int kexec_reboot(void) +{ + int result; + const char *argv[4]; + const char **p; + + /* First try running shutdown. Init scripts should run 'exec -e' */ + + p = argv; + *p++ = pb_system_apps.shutdown; /* 1 */ + *p++ = "-r"; /* 2 */ + *p++ = "now"; /* 3 */ + *p++ = NULL; /* 4 */ result = pb_run_cmd(argv); - /* kexec will return zero on success */ - /* On error, force a kexec with the -f option */ + /* On error, force a kexec with the -e option */ if (result) { - *(p + 0) = "-f"; /* 4 */ - *(p + 1) = l_image; /* 5 */ - *(p + 2) = NULL; /* 6 */ + p = argv; + *p++ = pb_system_apps.kexec; /* 1 */ + *p++ = "-e"; /* 2 */ + *p++ = NULL; /* 3 */ result = pb_run_cmd(argv); } @@ -84,9 +114,6 @@ static int run_kexec_local(const char *l_image, const char *l_initrd, if (result) pb_log("%s: failed: (%d)\n", __func__, result); - talloc_free(s_initrd); - talloc_free(s_args); - return result; } @@ -99,31 +126,44 @@ int pb_run_kexec(const struct pb_kexec_data *kd) int result; char *l_image = NULL; char *l_initrd = NULL; + unsigned int clean_image = 0; + unsigned int clean_initrd = 0; pb_log("%s: image: '%s'\n", __func__, kd->image); pb_log("%s: initrd: '%s'\n", __func__, kd->initrd); pb_log("%s: args: '%s'\n", __func__, kd->args); + result = -1; + if (kd->image) { - l_image = pb_load_file(NULL, kd->image); + l_image = pb_load_file(NULL, kd->image, &clean_image); if (!l_image) - return -1; + goto no_load; } if (kd->initrd) { - l_initrd = pb_load_file(NULL, kd->initrd); + l_initrd = pb_load_file(NULL, kd->initrd, &clean_initrd); if (!l_initrd) - return -1; + goto no_load; } if (!l_image && !l_initrd) - return -1; + goto no_load; + + result = kexec_load(l_image, l_initrd, kd->args); - result = run_kexec_local(l_image, l_initrd, kd->args); +no_load: + if (clean_image) + unlink(l_image); + if (clean_initrd) + unlink(l_initrd); talloc_free(l_image); talloc_free(l_initrd); + if (!result) + result = kexec_reboot(); + return result; } -- cgit v1.2.1