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", path
, command
, NULL
};
48 upgrade_proc
.cb
= upgrade_proc_cb
;
49 upgrade_proc
.pid
= fork();
50 if (upgrade_proc
.pid
< 0) {
51 fprintf(stderr
, "Failed to fork sysupgrade\n");
55 if (!upgrade_proc
.pid
) {
57 execvp(args
[0], args
);
58 fprintf(stderr
, "Failed to exec sysupgrade\n");
62 uloop_process_add(&upgrade_proc
);
66 int main(int argc
, char **argv
)
71 fprintf(stderr
, "this tool needs to run as pid 1\n");
75 int fd
= open("/", O_DIRECTORY
|O_PATH
);
77 fprintf(stderr
, "unable to open prefix directory: %m\n");
81 if (chroot(".") < 0) {
82 fprintf(stderr
, "failed to chroot: %m\n");
86 if (fchdir(fd
) == -1) {
87 fprintf(stderr
, "failed to chdir to prefix directory: %m\n");
93 fprintf(stderr
, "sysupgrade stage 2 failed, invalid command line\n");
99 sysupgrade(argv
[1], argv
[2]);