summaryrefslogtreecommitdiffstats
path: root/sysupgrade.c
blob: 07e33f752d0c3be57962771cdf42f02576d42e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
 * Copyright (C) 2017 Matthias Schiffer <mschiffer@universe-factory.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */


#include "watchdog.h"
#include "sysupgrade.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


void sysupgrade_exec_upgraded(const char *prefix, char *path, char *command)
{
	char *wdt_fd = watchdog_fd();
	char *argv[] = { "/sbin/upgraded", NULL, NULL, NULL};
	int ret;

	ret = chroot(prefix);
	if (ret < 0) {
		fprintf(stderr, "Failed to chroot for upgraded exec.\n");
		return;
	}

	argv[1] = path;
	argv[2] = command;

	if (wdt_fd) {
		watchdog_set_cloexec(false);
		setenv("WDTFD", wdt_fd, 1);
	}
	execvp(argv[0], argv);

	/* Cleanup on failure */
	fprintf(stderr, "Failed to exec upgraded.\n");
	unsetenv("WDTFD");
	watchdog_set_cloexec(true);
	ret = chroot(".");
	if (ret < 0) {
		fprintf(stderr, "Failed to reset chroot, exiting.\n");
		exit(EXIT_FAILURE);
	}
}