summaryrefslogtreecommitdiff
path: root/pkg/sinit/shutdown.c
blob: 87ddc010b4b5f0a8d8c5f06a810a53c3e2e5a8ec (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* See LICENSE file for copyright and license details. */
#define _XOPEN_SOURCE 700
#include <errno.h>
#include <mntent.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <unistd.h>

static noreturn void
usage(void)
{
	fprintf(stderr, "usage: shutdown [-hpr]\n");
	exit(2);
}

int
main(int argc, char *argv[])
{
	FILE *fp;
	char **dirs = NULL;
	size_t n = 0;
	struct mntent *mnt;
	int cmd = RB_POWER_OFF;

	while (*++argv && (*argv)[0] == '-' && (*argv)[1]) {
		switch ((*argv)[1]) {
		case 'h':
			cmd = RB_HALT_SYSTEM;
			break;
		case 'p':
			cmd = RB_POWER_OFF;
			break;
		case 'r':
			cmd = RB_AUTOBOOT;
			break;
		default:
			usage();
		}
	}
	if (*argv)
		usage();

	if (getsid(0) != getpid()) {
		fprintf(stderr, "must be session leader\n");
		return 1;
	}

	sync();
	kill(-1, SIGTERM);
	sleep(2);
	kill(-1, SIGKILL);

	sync();
	fp = setmntent("/proc/mounts", "r");
	if (!fp) {
		perror("setmntent");
		goto reboot;
	}
	while ((mnt = getmntent(fp))) {
		if (!(dirs = realloc(dirs, ++n * sizeof(*dirs)))) {
			perror("realloc");
			break;
		}
		if (!(dirs[n - 1] = strdup(mnt->mnt_dir))) {
			perror("strdup");
			break;
		}
	}
	endmntent(fp);
	while (n) {
		if (umount(dirs[--n]) < 0)
			fprintf(stderr, "umount %s: %s\n", dirs[n], strerror(errno));
		free(dirs[n]);
	}
	free(dirs);

reboot:
	if (reboot(cmd) < 0) {
		perror("reboot");
		return 1;
	}
}