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