procd: initd: fix path allocation in early_insmod
[project/procd.git] / sysupgrade.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 * Copyright (C) 2017 Matthias Schiffer <mschiffer@universe-factory.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 2.1
8 * as published by the Free Software Foundation
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16
17 #include "watchdog.h"
18 #include "sysupgrade.h"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24
25 void sysupgrade_exec_upgraded(const char *prefix, char *path, char *command)
26 {
27 char *wdt_fd = watchdog_fd();
28 char *argv[] = { "/sbin/upgraded", NULL, NULL, NULL};
29 int ret;
30
31 ret = chroot(prefix);
32 if (ret < 0) {
33 fprintf(stderr, "Failed to chroot for upgraded exec.\n");
34 return;
35 }
36
37 argv[1] = path;
38 argv[2] = command;
39
40 if (wdt_fd) {
41 watchdog_set_cloexec(false);
42 setenv("WDTFD", wdt_fd, 1);
43 }
44 execvp(argv[0], argv);
45
46 /* Cleanup on failure */
47 fprintf(stderr, "Failed to exec upgraded.\n");
48 unsetenv("WDTFD");
49 watchdog_set_cloexec(true);
50 ret = chroot(".");
51 if (ret < 0) {
52 fprintf(stderr, "Failed to reset chroot, exiting.\n");
53 exit(EXIT_FAILURE);
54 }
55 }