summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svc/rootisramdisk/rootisramdisk.c
blob: 353be0141a4164c99465120ba0b9828c4f4df735 (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
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2021 Oxide Computer Company
 */

/*
 * rootisramdisk: a helper program for smf_root_is_ramdisk() in
 * "/lib/svc/share/smf_include.sh".  Exits zero if the root file system is
 * mounted from a ramdisk, or non-zero if not, or if we hit an error condition.
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <fcntl.h>
#include <err.h>
#include <limits.h>
#include <string.h>
#include <sys/modctl.h>
#include <sys/types.h>
#include <sys/mkdev.h>
#include <sys/stat.h>

#define	EXIT_USAGE			2
#define	EXIT_NOT_RAMDISK		3

bool g_verbose = false;

static bool
root_is_ramdisk(void)
{
	struct stat st;
	major_t maj;
	char driver[PATH_MAX + 1];

	if (stat("/", &st) != 0) {
		err(EXIT_FAILURE, "stat");
	}

	maj = major(st.st_dev);
	if (g_verbose) {
		fprintf(stderr, "major = %lu\n", (long unsigned)maj);
	}

	if (modctl(MODGETNAME, driver, sizeof (driver), &maj) != 0) {
		err(EXIT_FAILURE, "modctl");
	}

	if (g_verbose) {
		fprintf(stderr, "driver = %s\n", driver);
	}

	return (strcmp(driver, "ramdisk") == 0);
}

int
main(int argc, char *argv[])
{
	int c;

	while ((c = getopt(argc, argv, ":v")) != -1) {
		switch (c) {
		case 'v':
			g_verbose = true;
			break;
		case ':':
			errx(EXIT_USAGE, "-%c requires an operand", optopt);
			break;
		case '?':
			errx(EXIT_USAGE, "-%c unknown", optopt);
			break;
		}
	}

	return (root_is_ramdisk() ? EXIT_SUCCESS : EXIT_NOT_RAMDISK);
}