summaryrefslogtreecommitdiffstats
path: root/preinit.c
blob: c015ebd28d91c8ece7907b42214468e43ee36b85 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
 *
 * 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 <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>

#include <unistd.h>
#include <unistd.h>

#include "procd.h"
#include "hotplug.h"
#include "watchdog.h"

static struct uloop_process preinit;

static void spawn_procd(struct uloop_process *proc, int ret)
{
	char *wdt_fd = watchdog_fd();
	char *argv[] = { "/sbin/procd", NULL };
	struct stat s;

	if (!stat("/tmp/sysupgrade", &s))
		while (true)
			sleep(1);

	unsetenv("INITRAMFS");
	unsetenv("PREINIT");
	DEBUG(1, "Exec to real procd now\n");
	if (wdt_fd)
		setenv("WDTFD", wdt_fd, 1);
	execvp(argv[0], argv);
}

void procd_preinit(void)
{
	char *argv[] = { "/bin/sh", "/etc/preinit", NULL };

	LOG("- preinit -\n");

	setenv("PREINIT", "1", 1);
	preinit.cb = spawn_procd;
	preinit.pid = fork();
	if (!preinit.pid) {
		execvp(argv[0], argv);
		ERROR("Failed to start preinit\n");
		exit(-1);
	}

	if (preinit.pid <= 0) {
		ERROR("Failed to start new preinit instance\n");
		return;
	}

	uloop_process_add(&preinit);
	DEBUG(2, "Launched preinit instance, pid=%d\n", (int) preinit.pid);
}