#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);
}