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.
17 #include <sys/reboot.h>
26 #include <libubox/uloop.h>
28 #include "../watchdog.h"
31 #define O_PATH 010000000
34 static struct uloop_process upgrade_proc;
35 unsigned int debug = 2;
37 static void upgrade_proc_cb(struct uloop_process *proc, int ret)
40 fprintf(stderr, "sysupgrade aborted with return code: %d\n", ret);
44 static void sysupgrade(char *path, char *command)
46 char *args[] = { "/lib/upgrade/stage2", NULL, NULL, NULL };
50 upgrade_proc.cb = upgrade_proc_cb;
51 upgrade_proc.pid = fork();
52 if (!upgrade_proc.pid) {
53 execvp(args[0], args);
54 fprintf(stderr, "Failed to fork sysupgrade\n");
57 if (upgrade_proc.pid <= 0) {
58 fprintf(stderr, "Failed to start sysupgrade\n");
63 int main(int argc, char **argv)
68 fprintf(stderr, "this tool needs to run as pid 1\n");
72 int fd = open("/", O_DIRECTORY|O_PATH);
74 fprintf(stderr, "unable to open prefix directory: %s\n", strerror(errno));
80 if (fchdir(fd) == -1) {
81 fprintf(stderr, "failed to chdir to prefix directory: %s\n", strerror(errno));
87 fprintf(stderr, "sysupgrade stage 2 failed, invalid command line\n");
93 sysupgrade(argv[1], argv[2]);