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  *
  32  *                      cfstagchk.c
  33  *
  34  * Cache FS admin utility.  Used to read and/or write a
  35  * cache tag from/to the specified partition.
  36  */
  37 
  38 #include <locale.h>
  39 #include <stdio.h>
  40 #include <stdlib.h>
  41 #include <unistd.h>
  42 #include <string.h>
  43 #include <errno.h>
  44 #include <fcntl.h>
  45 #include <ctype.h>
  46 #include <stdarg.h>
  47 #include <sys/param.h>
  48 #include <sys/types.h>
  49 #include <sys/vtoc.h>
  50 
  51 void pr_err(char *fmt, ...);
  52 void usage(char *);
  53 
  54 /*
  55  * -----------------------------------------------------------------
  56  *
  57  *                      main
  58  *
  59  * Description:
  60  *      Main routine for the cfstagchk program
  61  * Arguments:
  62  *      argc    number of command line arguments
  63  *      argv    command line arguments
  64  * Returns:
  65  *      Returns 0 for failure, > 0 for an error.
  66  * Preconditions:
  67  */
  68 
  69 int
  70 main(int argc, char **argv)
  71 {
  72         int c;
  73         int which;
  74 
  75         char *path;
  76 
  77         int wflag;
  78         int fd, err, slice;
  79         struct vtoc vtoc;
  80         struct partition *p;
  81 
  82         /* verify root running command */
  83         if (getuid() != 0) {
  84                 pr_err(gettext("must be run by root"));
  85                 return (1);
  86         }
  87 
  88         (void) setlocale(LC_ALL, "");
  89 #if !defined(TEXT_DOMAIN)
  90 #define TEXT_DOMAIN     "SYS_TEST"
  91 #endif
  92         (void) textdomain(TEXT_DOMAIN);
  93 
  94         /* set defaults for command line options */
  95         wflag = 0;
  96 
  97         /* parse the command line arguments */
  98         while ((c = getopt(argc, argv, "w")) != EOF) {
  99                 switch (c) {
 100 
 101                 case 'w':               /* write */
 102                         wflag = 1;
 103                         break;
 104 
 105                 default:
 106                         usage(gettext("illegal option"));
 107                         return (1);
 108                 }
 109         }
 110         argc -= optind;
 111         argv += optind;
 112         if (argc != 1) {
 113                 usage(gettext("must specify a single device"));
 114                 return (1);
 115         }
 116 
 117         fd = open(*argv, O_RDWR);
 118         if (fd < 0) {
 119                 pr_err("can't open %s", *argv);
 120                 return (1);
 121         }
 122 
 123         slice = read_vtoc(fd, &vtoc);
 124         if (slice < 0) {
 125                 pr_err(gettext("can't read vtoc"));
 126                 return (1);
 127         }
 128         p = &vtoc.v_part[slice];
 129         if (!wflag) {
 130                 err = 0;
 131                 if (p->p_tag != V_CACHE)
 132                         err++;
 133         } else {
 134                 p->p_tag = V_CACHE;
 135                 err = write_vtoc(fd, &vtoc);
 136                 if (err < 0)
 137                         pr_err(gettext("write_vtoc failure"));
 138         }
 139         return (err);
 140 }
 141 
 142 /*
 143  * -----------------------------------------------------------------
 144  *
 145  *                      usage
 146  *
 147  * Description:
 148  *      Prints a usage message for this utility.
 149  * Arguments:
 150  *      msgp    message to include with the usage message
 151  * Returns:
 152  * Preconditions:
 153  *      precond(msgp)
 154  */
 155 
 156 void
 157 usage(char *msgp)
 158 {
 159         fprintf(stderr, gettext("cfstagchk: %s\n"), msgp);
 160         fprintf(stderr, gettext("usage: cfstagchk [-w] device\n"));
 161 }
 162 
 163 /*
 164  * -----------------------------------------------------------------
 165  *
 166  *                      pr_err
 167  *
 168  * Description:
 169  *      Prints an error message to stderr.
 170  * Arguments:
 171  *      fmt     printf style format
 172  *      ...     arguments for fmt
 173  * Returns:
 174  * Preconditions:
 175  *      precond(fmt)
 176  */
 177 
 178 void
 179 pr_err(char *fmt, ...)
 180 {
 181         va_list ap;
 182 
 183         va_start(ap, fmt);
 184         (void) fprintf(stderr, gettext("cfstagchk: "));
 185         (void) vfprintf(stderr, fmt, ap);
 186         (void) fprintf(stderr, "\n");
 187         va_end(ap);
 188 }