1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright 2021 Oxide Computer Company 14 */ 15 16 /* 17 * rootisramdisk: a helper program for smf_root_is_ramdisk() in 18 * "/lib/svc/share/smf_include.sh". Exits zero if the root file system is 19 * mounted from a ramdisk, or non-zero if not, or if we hit an error condition. 20 */ 21 22 #include <stdlib.h> 23 #include <stdio.h> 24 #include <stdbool.h> 25 #include <fcntl.h> 26 #include <err.h> 27 #include <limits.h> 28 #include <string.h> 29 #include <sys/modctl.h> 30 #include <sys/types.h> 31 #include <sys/mkdev.h> 32 #include <sys/stat.h> 33 34 #define EXIT_USAGE 2 35 #define EXIT_NOT_RAMDISK 3 36 37 bool g_verbose = false; 38 39 static bool 40 root_is_ramdisk(void) 41 { 42 struct stat st; 43 major_t maj; 44 char driver[PATH_MAX + 1]; 45 46 if (stat("/", &st) != 0) { 47 err(EXIT_FAILURE, "stat"); 48 } 49 50 maj = major(st.st_dev); 51 if (g_verbose) { 52 fprintf(stderr, "major = %lu\n", (long unsigned)maj); 53 } 54 55 if (modctl(MODGETNAME, driver, sizeof (driver), &maj) != 0) { 56 err(EXIT_FAILURE, "modctl"); 57 } 58 59 if (g_verbose) { 60 fprintf(stderr, "driver = %s\n", driver); 61 } 62 63 return (strcmp(driver, "ramdisk") == 0); 64 } 65 66 int 67 main(int argc, char *argv[]) 68 { 69 int c; 70 71 while ((c = getopt(argc, argv, ":v")) != -1) { 72 switch (c) { 73 case 'v': 74 g_verbose = true; 75 break; 76 case ':': 77 errx(EXIT_USAGE, "-%c requires an operand", optopt); 78 break; 79 case '?': 80 errx(EXIT_USAGE, "-%c unknown", optopt); 81 break; 82 } 83 } 84 85 return (root_is_ramdisk() ? EXIT_SUCCESS : EXIT_NOT_RAMDISK); 86 } 87