move upgrade to its own subfolder
[project/procd.git] / upgraded / upgraded.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
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
8 *
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.
13 */
14
15 #include <sys/reboot.h>
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #include <libubox/uloop.h>
23
24 #include "../watchdog.h"
25
26 static struct uloop_process upgrade_proc;
27 unsigned int debug = 2;
28
29 static void upgrade_proc_cb(struct uloop_process *proc, int ret)
30 {
31 if (ret)
32 fprintf(stderr, "sysupgrade aborted with return code: %d\n", ret);
33 uloop_end();
34 }
35
36 static void sysupgarde(char *folder)
37 {
38 char *args[] = { "/sbin/sysupgrade", "nand", NULL, NULL };
39
40 args[2] = folder;
41 upgrade_proc.cb = upgrade_proc_cb;
42 upgrade_proc.pid = fork();
43 if (!upgrade_proc.pid) {
44 execvp(args[0], args);
45 fprintf(stderr, "Failed to fork sysupgrade\n");
46 exit(-1);
47 }
48 if (upgrade_proc.pid <= 0) {
49 fprintf(stderr, "Failed to start sysupgarde\n");
50 uloop_end();
51 }
52 }
53
54 int main(int argc, char **argv)
55 {
56 pid_t p = getpid();
57
58 chdir("/tmp");
59
60 if (p != 1) {
61 fprintf(stderr, "this tool needs to run as pid 1\n");
62 return -1;
63 }
64 if (argc != 2) {
65 fprintf(stderr, "sysupgrade stage 2 failed, no folder specified\n");
66 return -1;
67 }
68
69 uloop_init();
70 watchdog_init(0);
71 sysupgarde(argv[1]);
72 uloop_run();
73
74 reboot(RB_AUTOBOOT);
75
76 return 0;
77 }