1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #pragma ident   "%Z%%M% %I%     %E% SMI"
  28 
  29 /*
  30  *
  31  *                      cfsfstype.c
  32  *
  33  * Cache FS admin utility.  Used to glean information out of the
  34  * rootfs, frontfs, and backfs variables in the kernel.
  35  */
  36 
  37 #include <locale.h>
  38 #include <stdio.h>
  39 #include <stdlib.h>
  40 #include <unistd.h>
  41 #include <string.h>
  42 #include <errno.h>
  43 #include <limits.h>
  44 #include <dirent.h>
  45 #include <ftw.h>
  46 #include <fcntl.h>
  47 #include <ctype.h>
  48 #include <stdarg.h>
  49 #include <sys/param.h>
  50 #include <sys/types.h>
  51 #include <sys/filio.h>
  52 #include <sys/stat.h>
  53 #include <sys/statvfs.h>
  54 #include <sys/fs/cachefs_fs.h>
  55 #include <sys/fs/cachefs_dir.h>
  56 
  57 
  58 static void pr_err(char *fmt, ...);
  59 static void usage(char *);
  60 
  61 /*
  62  *
  63  *                      main
  64  *
  65  * Description:
  66  *      Main routine for the cfsfstype program.
  67  * Arguments:
  68  *      argc    number of command line arguments
  69  *      argv    command line arguments
  70  * Returns:
  71  *      Returns 0 for failure, > 0 for an error.
  72  * Preconditions:
  73  */
  74 
  75 int
  76 main(int argc, char **argv)
  77 {
  78         int c;
  79         int nflag = 0;
  80         struct statvfs64 svb;
  81         int fd;
  82 
  83         /* verify root running command */
  84         if (getuid() != 0) {
  85                 pr_err(gettext("must be run by root"));
  86                 return (1);
  87         }
  88 
  89         (void) setlocale(LC_ALL, "");
  90 #if !defined(TEXT_DOMAIN)
  91 #define TEXT_DOMAIN     "SYS_TEST"
  92 #endif
  93         (void) textdomain(TEXT_DOMAIN);
  94 
  95         /* parse the command line arguments */
  96         while ((c = getopt(argc, argv, "n")) != EOF) {
  97                 switch (c) {
  98 
  99                 case 'n':
 100                         nflag = 1;
 101                         break;
 102 
 103                 default:
 104                         usage(gettext("illegal option"));
 105                         return (1);
 106                 }
 107         }
 108         argc -= optind;
 109         argv += optind;
 110         if (argc > 1) {
 111                 usage(gettext("too many file names specified"));
 112                 return (1);
 113         }
 114 
 115         /* if just path is specified, just statvfs it */
 116         if (!nflag) {
 117                 if (argc != 1) {
 118                         usage(gettext("no file name"));
 119                         return (1);
 120                 }
 121                 if (statvfs64(*argv, &svb) < 0) {
 122                         pr_err(gettext("Cannot open %s: %s"), *argv,
 123                                 strerror(errno));
 124                         return (1);
 125                 }
 126                 (void) printf("%s\n", svb.f_basetype);
 127                 return (0);
 128         }
 129 
 130         fd = open("/", O_RDONLY);
 131         if (fd < 0) {
 132                 perror(gettext("Open of root directory"));
 133                 return (1);
 134         }
 135 
 136         if (argc == 1) {
 137                 close(fd);
 138                 fd = open(*argv, O_RDONLY);
 139                 if (fd < 0) {
 140                         perror(gettext("open of specified directory"));
 141                         return (1);
 142                 }
 143         }
 144 
 145         if (ioctl(fd, _FIOSTOPCACHE)) {
 146                 perror(gettext("Convert ioctl fault"));
 147                 return (1);
 148         }
 149         return (0);
 150 }
 151 
 152 /*
 153  *
 154  *                      usage
 155  *
 156  * Description:
 157  *      Prints a usage message for this utility.
 158  * Arguments:
 159  *      msgp    message to include with the usage message
 160  * Returns:
 161  * Preconditions:
 162  *      precond(msgp)
 163  */
 164 
 165 static void
 166 usage(char *msgp)
 167 {
 168         fprintf(stderr, gettext("cfsfstype: %s\n"), msgp);
 169         fprintf(stderr, gettext("usage: cfsfstype file\n"));
 170 }
 171 
 172 /*
 173  *
 174  *                      pr_err
 175  *
 176  * Description:
 177  *      Prints an error message to stderr.
 178  * Arguments:
 179  *      fmt     printf style format
 180  *      ...     arguments for fmt
 181  * Returns:
 182  * Preconditions:
 183  *      precond(fmt)
 184  */
 185 
 186 static void
 187 pr_err(char *fmt, ...)
 188 {
 189         va_list ap;
 190 
 191         va_start(ap, fmt);
 192         (void) fprintf(stderr, gettext("cfsfstype: "));
 193         (void) vfprintf(stderr, fmt, ap);
 194         (void) fprintf(stderr, "\n");
 195         va_end(ap);
 196 }