2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <sys/reboot.h>
24 #include <libubox/uloop.h>
26 #include "../watchdog.h"
29 #define O_PATH 010000000
32 static struct uloop_process upgrade_proc;
33 unsigned int debug = 2;
35 static void upgrade_proc_cb(struct uloop_process *proc, int ret)
38 fprintf(stderr, "sysupgrade aborted with return code: %d\n", ret);
42 static void sysupgrade(char *path, char *command)
44 char *args[] = { "/lib/upgrade/stage2", NULL, NULL, NULL };
48 upgrade_proc.cb = upgrade_proc_cb;
49 upgrade_proc.pid = fork();
50 if (!upgrade_proc.pid) {
51 execvp(args[0], args);
52 fprintf(stderr, "Failed to fork sysupgrade\n");
55 if (upgrade_proc.pid <= 0) {
56 fprintf(stderr, "Failed to start sysupgrade\n");
61 int main(int argc, char **argv)
66 fprintf(stderr, "this tool needs to run as pid 1\n");
70 int fd = open("/", O_DIRECTORY|O_PATH);
72 fprintf(stderr, "unable to open prefix directory: %s\n", strerror(errno));
78 if (fchdir(fd) == -1) {
79 fprintf(stderr, "failed to chdir to prefix directory: %s\n", strerror(errno));
85 fprintf(stderr, "sysupgrade stage 2 failed, invalid command line\n");
91 sysupgrade(argv[1], argv[2]);