1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*0Sstevel@tonic-gate /* All Rights Reserved */ 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate /* 27*0Sstevel@tonic-gate * Copyright 1996-2003 Sun Microsystems, Inc. All rights reserved. 28*0Sstevel@tonic-gate * Use is subject to license terms. 29*0Sstevel@tonic-gate */ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <stdio.h> 34*0Sstevel@tonic-gate #include <limits.h> 35*0Sstevel@tonic-gate #include <locale.h> 36*0Sstevel@tonic-gate #include <libintl.h> 37*0Sstevel@tonic-gate #include <sys/fstyp.h> 38*0Sstevel@tonic-gate #include <errno.h> 39*0Sstevel@tonic-gate #include <sys/vfstab.h> 40*0Sstevel@tonic-gate #include <sys/types.h> 41*0Sstevel@tonic-gate #include <sys/stat.h> 42*0Sstevel@tonic-gate #include <fcntl.h> 43*0Sstevel@tonic-gate #include <string.h> 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate #define FSTYPE_MAX 8 46*0Sstevel@tonic-gate #define ARGV_MAX 1024 47*0Sstevel@tonic-gate #define VFS_PATH "/usr/lib/fs" 48*0Sstevel@tonic-gate #define ALT_PATH "/etc/fs" 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate extern char *default_fstype(); 51*0Sstevel@tonic-gate void stat_snap(char *, char *, char *); 52*0Sstevel@tonic-gate char *special = NULL; /* device special name */ 53*0Sstevel@tonic-gate char *fstype = NULL; /* fstype name is filled in here */ 54*0Sstevel@tonic-gate char *cbasename; /* name of command */ 55*0Sstevel@tonic-gate char *newargv[ARGV_MAX]; /* args for the fstype specific command */ 56*0Sstevel@tonic-gate char vfstab[] = VFSTAB; 57*0Sstevel@tonic-gate int newargc = 2; 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate /* 60*0Sstevel@tonic-gate * TRANSLATION_NOTE - the usage strings in the c_usgstr[] of the 61*0Sstevel@tonic-gate * following structures should be given a translation; the call to gettext 62*0Sstevel@tonic-gate * is in the usage() function. The strings are the ones containing 63*0Sstevel@tonic-gate * "[-F FSType]". 64*0Sstevel@tonic-gate */ 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate struct commands { 67*0Sstevel@tonic-gate char *c_basename; 68*0Sstevel@tonic-gate char *c_optstr; 69*0Sstevel@tonic-gate char *c_usgstr[4]; /* make sure as large as largest array size */ 70*0Sstevel@tonic-gate } cmd_data[] = { 71*0Sstevel@tonic-gate "clri", "F:o:?V", 72*0Sstevel@tonic-gate { 73*0Sstevel@tonic-gate "[-F FSType] [-V] special inumber ...", 74*0Sstevel@tonic-gate NULL 75*0Sstevel@tonic-gate }, 76*0Sstevel@tonic-gate "mkfs", "F:o:mb:?V", 77*0Sstevel@tonic-gate { 78*0Sstevel@tonic-gate "[-F FSType] [-V] [-m] [-o specific_options] special ", 79*0Sstevel@tonic-gate "[operands]", NULL 80*0Sstevel@tonic-gate }, 81*0Sstevel@tonic-gate "dcopy", "F:o:?V", 82*0Sstevel@tonic-gate { 83*0Sstevel@tonic-gate "[-F FSType] [-V] special inumber ...", 84*0Sstevel@tonic-gate NULL 85*0Sstevel@tonic-gate }, 86*0Sstevel@tonic-gate "fsdb", "F:o:z:?V", 87*0Sstevel@tonic-gate { 88*0Sstevel@tonic-gate "[-F FSType] [-V] [-o specific_options] special", 89*0Sstevel@tonic-gate NULL 90*0Sstevel@tonic-gate }, 91*0Sstevel@tonic-gate "fssnap", "F:dio:?V", 92*0Sstevel@tonic-gate { 93*0Sstevel@tonic-gate "[-F FSType] [-V] -o special_options /mount/point", 94*0Sstevel@tonic-gate "-d [-F FSType] [-V] /mount/point | dev", 95*0Sstevel@tonic-gate "-i [-F FSType] [-V] [-o special-options] [/mount/point | dev]", 96*0Sstevel@tonic-gate NULL 97*0Sstevel@tonic-gate }, 98*0Sstevel@tonic-gate "labelit", "F:o:?nV", 99*0Sstevel@tonic-gate { 100*0Sstevel@tonic-gate "[-F FSType] [-V] [-o specific_options] special [operands]", 101*0Sstevel@tonic-gate NULL 102*0Sstevel@tonic-gate }, 103*0Sstevel@tonic-gate NULL, "F:o:?V", 104*0Sstevel@tonic-gate { 105*0Sstevel@tonic-gate "[-F FSType] [-V] [-o specific_options] special [operands]", 106*0Sstevel@tonic-gate NULL 107*0Sstevel@tonic-gate } 108*0Sstevel@tonic-gate }; 109*0Sstevel@tonic-gate struct commands *c_ptr; 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate main(argc, argv) 112*0Sstevel@tonic-gate int argc; 113*0Sstevel@tonic-gate char *argv[]; 114*0Sstevel@tonic-gate { 115*0Sstevel@tonic-gate register char *ptr; 116*0Sstevel@tonic-gate char full_path[PATH_MAX]; 117*0Sstevel@tonic-gate char *vfs_path = VFS_PATH; 118*0Sstevel@tonic-gate char *alt_path = ALT_PATH; 119*0Sstevel@tonic-gate int i; 120*0Sstevel@tonic-gate int verbose = 0; /* set if -V is specified */ 121*0Sstevel@tonic-gate int F_flg = 0; 122*0Sstevel@tonic-gate int mflag = 0; 123*0Sstevel@tonic-gate char *oopts = NULL; 124*0Sstevel@tonic-gate int iflag = 0; 125*0Sstevel@tonic-gate int usgflag = 0; 126*0Sstevel@tonic-gate int arg; /* argument from getopt() */ 127*0Sstevel@tonic-gate extern char *optarg; /* getopt specific */ 128*0Sstevel@tonic-gate extern int optind; 129*0Sstevel@tonic-gate extern int opterr; 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 134*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 135*0Sstevel@tonic-gate #endif 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate cbasename = ptr = argv[0]; 140*0Sstevel@tonic-gate while (*ptr) { 141*0Sstevel@tonic-gate if (*ptr++ == '/') 142*0Sstevel@tonic-gate cbasename = ptr; 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate if (argc == 1) { 147*0Sstevel@tonic-gate for (c_ptr = cmd_data; ((c_ptr->c_basename != NULL) && 148*0Sstevel@tonic-gate (strcmp(c_ptr->c_basename, cbasename) != 0)); c_ptr++) 149*0Sstevel@tonic-gate ; 150*0Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr); 151*0Sstevel@tonic-gate exit(2); 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate for (c_ptr = cmd_data; ((c_ptr->c_basename != NULL) && 155*0Sstevel@tonic-gate (strcmp(c_ptr->c_basename, cbasename) != 0)); c_ptr++) 156*0Sstevel@tonic-gate ; 157*0Sstevel@tonic-gate while ((arg = getopt(argc, argv, c_ptr->c_optstr)) != -1) { 158*0Sstevel@tonic-gate switch (arg) { 159*0Sstevel@tonic-gate case 'V': /* echo complete command line */ 160*0Sstevel@tonic-gate verbose = 1; 161*0Sstevel@tonic-gate break; 162*0Sstevel@tonic-gate case 'F': /* FSType specified */ 163*0Sstevel@tonic-gate F_flg++; 164*0Sstevel@tonic-gate fstype = optarg; 165*0Sstevel@tonic-gate break; 166*0Sstevel@tonic-gate case 'o': /* FSType specific arguments */ 167*0Sstevel@tonic-gate newargv[newargc++] = "-o"; 168*0Sstevel@tonic-gate newargv[newargc++] = optarg; 169*0Sstevel@tonic-gate oopts = optarg; 170*0Sstevel@tonic-gate break; 171*0Sstevel@tonic-gate case '?': /* print usage message */ 172*0Sstevel@tonic-gate newargv[newargc++] = "-?"; 173*0Sstevel@tonic-gate usgflag = 1; 174*0Sstevel@tonic-gate break; 175*0Sstevel@tonic-gate case 'm': /* FSType specific arguments */ 176*0Sstevel@tonic-gate mflag = 1; 177*0Sstevel@tonic-gate newargv[newargc] = (char *)malloc(3); 178*0Sstevel@tonic-gate sprintf(newargv[newargc++], "-%c", arg); 179*0Sstevel@tonic-gate if (optarg) 180*0Sstevel@tonic-gate newargv[newargc++] = optarg; 181*0Sstevel@tonic-gate break; 182*0Sstevel@tonic-gate case 'i': /* fssnap only */ 183*0Sstevel@tonic-gate iflag = 1; 184*0Sstevel@tonic-gate /*FALLTHROUGH*/ 185*0Sstevel@tonic-gate default: 186*0Sstevel@tonic-gate newargv[newargc] = (char *)malloc(3); 187*0Sstevel@tonic-gate sprintf(newargv[newargc++], "-%c", arg); 188*0Sstevel@tonic-gate if (optarg) 189*0Sstevel@tonic-gate newargv[newargc++] = optarg; 190*0Sstevel@tonic-gate break; 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate optarg = NULL; 193*0Sstevel@tonic-gate } 194*0Sstevel@tonic-gate if (F_flg > 1) { 195*0Sstevel@tonic-gate fprintf(stderr, gettext("%s: more than one FSType specified\n"), 196*0Sstevel@tonic-gate cbasename); 197*0Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr); 198*0Sstevel@tonic-gate exit(2); 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate if (fstype != NULL) { 201*0Sstevel@tonic-gate if (strlen(fstype) > FSTYPE_MAX) { 202*0Sstevel@tonic-gate fprintf(stderr, 203*0Sstevel@tonic-gate gettext("%s: FSType %s exceeds %d characters\n"), 204*0Sstevel@tonic-gate cbasename, fstype, FSTYPE_MAX); 205*0Sstevel@tonic-gate exit(2); 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate /* perform a lookup if fstype is not specified */ 210*0Sstevel@tonic-gate special = argv[optind]; 211*0Sstevel@tonic-gate optind++; 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate /* handle -i (fssnap command only) */ 214*0Sstevel@tonic-gate if (iflag) { 215*0Sstevel@tonic-gate int diff = argc - optind; 216*0Sstevel@tonic-gate /* 217*0Sstevel@tonic-gate * There is no reason to ever call a file system specific 218*0Sstevel@tonic-gate * version since its all in kstats. 219*0Sstevel@tonic-gate */ 220*0Sstevel@tonic-gate if (diff > 0) /* gave more than one mountpoint or device */ 221*0Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr); 222*0Sstevel@tonic-gate stat_snap(cbasename, diff == 0 ? argv[argc-1] : NULL, oopts); 223*0Sstevel@tonic-gate exit(0); 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate if ((special == NULL) && (!usgflag)) { 227*0Sstevel@tonic-gate fprintf(stderr, gettext("%s: special not specified\n"), 228*0Sstevel@tonic-gate cbasename); 229*0Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr); 230*0Sstevel@tonic-gate exit(2); 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate if ((fstype == NULL) && (usgflag)) 233*0Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr); 234*0Sstevel@tonic-gate if (fstype == NULL) 235*0Sstevel@tonic-gate lookup(); 236*0Sstevel@tonic-gate if (fstype == NULL) { 237*0Sstevel@tonic-gate fprintf(stderr, gettext("%s: FSType cannot be identified\n"), 238*0Sstevel@tonic-gate cbasename); 239*0Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr); 240*0Sstevel@tonic-gate exit(2); 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate newargv[newargc++] = special; 243*0Sstevel@tonic-gate for (; optind < argc; optind++) 244*0Sstevel@tonic-gate newargv[newargc++] = argv[optind]; 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate /* build the full pathname of the fstype dependent command */ 247*0Sstevel@tonic-gate sprintf(full_path, "%s/%s/%s", vfs_path, fstype, cbasename); 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate newargv[1] = cbasename; 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate if (verbose) { 252*0Sstevel@tonic-gate printf("%s -F %s ", cbasename, fstype); 253*0Sstevel@tonic-gate for (i = 2; newargv[i]; i++) 254*0Sstevel@tonic-gate printf("%s ", newargv[i]); 255*0Sstevel@tonic-gate printf("\n"); 256*0Sstevel@tonic-gate exit(0); 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate /* 260*0Sstevel@tonic-gate * Execute the FSType specific command. 261*0Sstevel@tonic-gate */ 262*0Sstevel@tonic-gate execv(full_path, &newargv[1]); 263*0Sstevel@tonic-gate if ((errno == ENOENT) || (errno == EACCES)) { 264*0Sstevel@tonic-gate /* build the alternate pathname */ 265*0Sstevel@tonic-gate sprintf(full_path, "%s/%s/%s", alt_path, fstype, cbasename); 266*0Sstevel@tonic-gate if (verbose) { 267*0Sstevel@tonic-gate printf("%s -F %s ", cbasename, fstype); 268*0Sstevel@tonic-gate for (i = 2; newargv[i]; i++) 269*0Sstevel@tonic-gate printf("%s ", newargv[i]); 270*0Sstevel@tonic-gate printf("\n"); 271*0Sstevel@tonic-gate exit(0); 272*0Sstevel@tonic-gate } 273*0Sstevel@tonic-gate execv(full_path, &newargv[1]); 274*0Sstevel@tonic-gate } 275*0Sstevel@tonic-gate if (errno == ENOEXEC) { 276*0Sstevel@tonic-gate newargv[0] = "sh"; 277*0Sstevel@tonic-gate newargv[1] = full_path; 278*0Sstevel@tonic-gate execv("/sbin/sh", &newargv[0]); 279*0Sstevel@tonic-gate } 280*0Sstevel@tonic-gate if (errno != ENOENT) { 281*0Sstevel@tonic-gate perror(cbasename); 282*0Sstevel@tonic-gate fprintf(stderr, gettext("%s: cannot execute %s\n"), cbasename, 283*0Sstevel@tonic-gate full_path); 284*0Sstevel@tonic-gate exit(2); 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate if (sysfs(GETFSIND, fstype) == (-1)) { 288*0Sstevel@tonic-gate fprintf(stderr, 289*0Sstevel@tonic-gate gettext("%s: FSType %s not installed in the kernel\n"), 290*0Sstevel@tonic-gate cbasename, fstype); 291*0Sstevel@tonic-gate exit(2); 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate fprintf(stderr, 294*0Sstevel@tonic-gate gettext("%s: Operation not applicable for FSType %s \n"), 295*0Sstevel@tonic-gate cbasename, fstype); 296*0Sstevel@tonic-gate exit(2); 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate usage(cmd, usg) 300*0Sstevel@tonic-gate char *cmd; 301*0Sstevel@tonic-gate char **usg; 302*0Sstevel@tonic-gate { 303*0Sstevel@tonic-gate int i; 304*0Sstevel@tonic-gate fprintf(stderr, gettext("Usage:\n")); 305*0Sstevel@tonic-gate for (i = 0; usg[i] != NULL; i++) 306*0Sstevel@tonic-gate fprintf(stderr, "%s %s\n", gettext(cmd), gettext(usg[i])); 307*0Sstevel@tonic-gate exit(2); 308*0Sstevel@tonic-gate } 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate /* 312*0Sstevel@tonic-gate * This looks up the /etc/vfstab entry given the device 'special'. 313*0Sstevel@tonic-gate * It is called when the fstype is not specified on the command line. 314*0Sstevel@tonic-gate * 315*0Sstevel@tonic-gate * The following global variables are used: 316*0Sstevel@tonic-gate * special, fstype 317*0Sstevel@tonic-gate */ 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate lookup() 320*0Sstevel@tonic-gate { 321*0Sstevel@tonic-gate FILE *fd; 322*0Sstevel@tonic-gate int ret; 323*0Sstevel@tonic-gate struct vfstab vget, vref; 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate if ((fd = fopen(vfstab, "r")) == NULL) { 326*0Sstevel@tonic-gate fprintf(stderr, gettext("%s: cannot open vfstab\n"), cbasename); 327*0Sstevel@tonic-gate exit(1); 328*0Sstevel@tonic-gate } 329*0Sstevel@tonic-gate vfsnull(&vref); 330*0Sstevel@tonic-gate vref.vfs_special = special; 331*0Sstevel@tonic-gate ret = getvfsany(fd, &vget, &vref); 332*0Sstevel@tonic-gate if (ret == -1) { 333*0Sstevel@tonic-gate rewind(fd); 334*0Sstevel@tonic-gate vfsnull(&vref); 335*0Sstevel@tonic-gate vref.vfs_fsckdev = special; 336*0Sstevel@tonic-gate ret = getvfsany(fd, &vget, &vref); 337*0Sstevel@tonic-gate } 338*0Sstevel@tonic-gate fclose(fd); 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gate switch (ret) { 341*0Sstevel@tonic-gate case -1: 342*0Sstevel@tonic-gate fstype = default_fstype(special); 343*0Sstevel@tonic-gate break; 344*0Sstevel@tonic-gate case 0: 345*0Sstevel@tonic-gate fstype = vget.vfs_fstype; 346*0Sstevel@tonic-gate break; 347*0Sstevel@tonic-gate case VFS_TOOLONG: 348*0Sstevel@tonic-gate fprintf(stderr, 349*0Sstevel@tonic-gate gettext("%s: line in vfstab exceeds %d characters\n"), 350*0Sstevel@tonic-gate cbasename, VFS_LINE_MAX-2); 351*0Sstevel@tonic-gate exit(1); 352*0Sstevel@tonic-gate break; 353*0Sstevel@tonic-gate case VFS_TOOFEW: 354*0Sstevel@tonic-gate fprintf(stderr, 355*0Sstevel@tonic-gate gettext("%s: line in vfstab has too few entries\n"), 356*0Sstevel@tonic-gate cbasename); 357*0Sstevel@tonic-gate exit(1); 358*0Sstevel@tonic-gate break; 359*0Sstevel@tonic-gate } 360*0Sstevel@tonic-gate } 361*0Sstevel@tonic-gate 362*0Sstevel@tonic-gate void 363*0Sstevel@tonic-gate stat_snap(cmd, mountpoint, opts) 364*0Sstevel@tonic-gate char *cmd; 365*0Sstevel@tonic-gate char *mountpoint; 366*0Sstevel@tonic-gate char *opts; 367*0Sstevel@tonic-gate { 368*0Sstevel@tonic-gate int fd; /* check mount point if given */ 369*0Sstevel@tonic-gate int en; 370*0Sstevel@tonic-gate char *errstr; 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate if (mountpoint) { 373*0Sstevel@tonic-gate if ((fd = open(mountpoint, O_RDONLY)) < 0) { 374*0Sstevel@tonic-gate en = errno; 375*0Sstevel@tonic-gate errstr = strerror(errno); 376*0Sstevel@tonic-gate if (errstr == NULL) 377*0Sstevel@tonic-gate errstr = gettext("Unknown error"); 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate fprintf(stderr, gettext("%s: %s: error %d: %s\n"), 380*0Sstevel@tonic-gate cmd, mountpoint, en, errstr); 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate exit(2); 383*0Sstevel@tonic-gate } 384*0Sstevel@tonic-gate close(fd); 385*0Sstevel@tonic-gate } 386*0Sstevel@tonic-gate fssnap_show_status(mountpoint, opts, 1, (opts ? 0 : 1)); 387*0Sstevel@tonic-gate } 388