X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fprocd.git;a=blobdiff_plain;f=upgraded%2Fupgraded.c;h=09c623cf804b43c826190300a40e2c4bf9aabfa3;hp=d7433e76071524a5e69f0e153730b2801e5406b8;hb=76dcbee7fd3a90221619feb7e4c572e00b60af9b;hpb=91da63d3d3fd680c805dd1a1b78df5b8731a8173 diff --git a/upgraded/upgraded.c b/upgraded/upgraded.c index d7433e7..09c623c 100644 --- a/upgraded/upgraded.c +++ b/upgraded/upgraded.c @@ -12,8 +12,11 @@ * GNU General Public License for more details. */ +#define _GNU_SOURCE + #include +#include #include #include #include @@ -24,6 +27,10 @@ #include "../watchdog.h" +#ifndef O_PATH +#define O_PATH 010000000 +#endif + static struct uloop_process upgrade_proc; unsigned int debug = 2; @@ -34,22 +41,26 @@ static void upgrade_proc_cb(struct uloop_process *proc, int ret) uloop_end(); } -static void sysupgarde(char *folder) +static void sysupgrade(char *path, char *command) { - char *args[] = { "/sbin/sysupgrade", "nand", NULL, NULL }; + char *args[] = { "/lib/upgrade/stage2", path, command, NULL }; - args[2] = folder; upgrade_proc.cb = upgrade_proc_cb; upgrade_proc.pid = fork(); - if (!upgrade_proc.pid) { - execvp(args[0], args); + if (upgrade_proc.pid < 0) { fprintf(stderr, "Failed to fork sysupgrade\n"); - exit(-1); + return; } - if (upgrade_proc.pid <= 0) { - fprintf(stderr, "Failed to start sysupgarde\n"); - uloop_end(); + + if (!upgrade_proc.pid) { + /* Child */ + execvp(args[0], args); + fprintf(stderr, "Failed to exec sysupgrade\n"); + _exit(-1); } + + uloop_process_add(&upgrade_proc); + uloop_run(); } int main(int argc, char **argv) @@ -58,21 +69,31 @@ int main(int argc, char **argv) if (p != 1) { fprintf(stderr, "this tool needs to run as pid 1\n"); - return -1; + return 1; } - if (chdir("/tmp") == -1) { - fprintf(stderr, "failed to chdir to /tmp: %s\n", strerror(errno)); - return -1; + + int fd = open("/", O_DIRECTORY|O_PATH); + if (fd < 0) { + fprintf(stderr, "unable to open prefix directory: %s\n", strerror(errno)); + return 1; } - if (argc != 2) { - fprintf(stderr, "sysupgrade stage 2 failed, no folder specified\n"); - return -1; + + chroot("."); + + if (fchdir(fd) == -1) { + fprintf(stderr, "failed to chdir to prefix directory: %s\n", strerror(errno)); + return 1; + } + close(fd); + + if (argc != 3) { + fprintf(stderr, "sysupgrade stage 2 failed, invalid command line\n"); + return 1; } uloop_init(); watchdog_init(0); - sysupgarde(argv[1]); - uloop_run(); + sysupgrade(argv[1], argv[2]); reboot(RB_AUTOBOOT);