Remove code that has become unnecessary after sysupgrade changes
[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 <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <errno.h>
23
24 #include <libubox/uloop.h>
25
26 #include "../watchdog.h"
27
28 #ifndef O_PATH
29 #define O_PATH 010000000
30 #endif
31
32 static struct uloop_process upgrade_proc;
33 unsigned int debug = 2;
34
35 static void upgrade_proc_cb(struct uloop_process *proc, int ret)
36 {
37 if (ret)
38 fprintf(stderr, "sysupgrade aborted with return code: %d\n", ret);
39 uloop_end();
40 }
41
42 static void sysupgrade(char *path, char *command)
43 {
44 char *args[] = { "/lib/upgrade/stage2", NULL, NULL, NULL };
45
46 args[1] = path;
47 args[2] = command;
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");
53 exit(-1);
54 }
55 if (upgrade_proc.pid <= 0) {
56 fprintf(stderr, "Failed to start sysupgrade\n");
57 uloop_end();
58 }
59 }
60
61 int main(int argc, char **argv)
62 {
63 pid_t p = getpid();
64
65 if (p != 1) {
66 fprintf(stderr, "this tool needs to run as pid 1\n");
67 return -1;
68 }
69
70 int fd = open("/", O_DIRECTORY|O_PATH);
71 if (fd < 0) {
72 fprintf(stderr, "unable to open prefix directory: %s\n", strerror(errno));
73 return -1;
74 }
75
76 chroot(".");
77
78 if (fchdir(fd) == -1) {
79 fprintf(stderr, "failed to chdir to prefix directory: %s\n", strerror(errno));
80 return -1;
81 }
82 close(fd);
83
84 if (argc != 3) {
85 fprintf(stderr, "sysupgrade stage 2 failed, invalid command line\n");
86 return -1;
87 }
88
89 uloop_init();
90 watchdog_init(0);
91 sysupgrade(argv[1], argv[2]);
92 uloop_run();
93
94 reboot(RB_AUTOBOOT);
95
96 return 0;
97 }