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 <string.h> 36*0Sstevel@tonic-gate #include <sys/fstyp.h> 37*0Sstevel@tonic-gate #include <errno.h> 38*0Sstevel@tonic-gate #include <sys/vfstab.h> 39*0Sstevel@tonic-gate #include <sys/wait.h> 40*0Sstevel@tonic-gate #include <sys/types.h> 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #define FSTYPE_MAX 8 43*0Sstevel@tonic-gate #define FULLPATH_MAX 64 44*0Sstevel@tonic-gate #define ARGV_MAX 1024 45*0Sstevel@tonic-gate #define VFS_PATH "/usr/lib/fs" 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate extern char *default_fstype(); 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate char *special = NULL; /* device special name */ 50*0Sstevel@tonic-gate char *fstype = NULL; /* fstype name is filled in here */ 51*0Sstevel@tonic-gate char *cbasename; /* name of command */ 52*0Sstevel@tonic-gate char *newargv[ARGV_MAX]; /* args for the fstype specific command */ 53*0Sstevel@tonic-gate char vfstab[] = VFSTAB; 54*0Sstevel@tonic-gate char full_path[FULLPATH_MAX]; 55*0Sstevel@tonic-gate char *vfs_path = VFS_PATH; 56*0Sstevel@tonic-gate int newargc = 2; 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate struct commands { 59*0Sstevel@tonic-gate char *c_basename; 60*0Sstevel@tonic-gate char *c_optstr; 61*0Sstevel@tonic-gate char *c_usgstr; 62*0Sstevel@tonic-gate } cmd_data[] = { 63*0Sstevel@tonic-gate "ff", "F:o:p:a:m:c:n:i:?IlsuV", 64*0Sstevel@tonic-gate "[-F FSType] [-V] [current_options] [-o specific_options] special ...", 65*0Sstevel@tonic-gate "ncheck", "F:o:?i:asV", 66*0Sstevel@tonic-gate "[-F FSType] [-V] [current_options] [-o specific_options] [special ...]", 67*0Sstevel@tonic-gate NULL, "F:o:?V", 68*0Sstevel@tonic-gate "[-F FSType] [-V] [current_options] [-o specific_options] special ..." 69*0Sstevel@tonic-gate }; 70*0Sstevel@tonic-gate struct commands *c_ptr; 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate main(argc, argv) 73*0Sstevel@tonic-gate int argc; 74*0Sstevel@tonic-gate char *argv[]; 75*0Sstevel@tonic-gate { 76*0Sstevel@tonic-gate FILE *fp; 77*0Sstevel@tonic-gate struct vfstab vfsbuf; 78*0Sstevel@tonic-gate register char *ptr; 79*0Sstevel@tonic-gate int i; 80*0Sstevel@tonic-gate int verbose = 0; /* set if -V is specified */ 81*0Sstevel@tonic-gate int F_flg = 0; 82*0Sstevel@tonic-gate int usgflag = 0; 83*0Sstevel@tonic-gate int fs_flag = 0; 84*0Sstevel@tonic-gate int arg; /* argument from getopt() */ 85*0Sstevel@tonic-gate extern char *optarg; /* getopt specific */ 86*0Sstevel@tonic-gate extern int optind; 87*0Sstevel@tonic-gate extern int opterr; 88*0Sstevel@tonic-gate size_t strlen(); 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate cbasename = ptr = argv[0]; 91*0Sstevel@tonic-gate while (*ptr) { 92*0Sstevel@tonic-gate if (*ptr++ == '/') 93*0Sstevel@tonic-gate cbasename = ptr; 94*0Sstevel@tonic-gate } 95*0Sstevel@tonic-gate /* 96*0Sstevel@tonic-gate * If there are no arguments and command is ncheck then the generic 97*0Sstevel@tonic-gate * reads the VFSTAB and executes the specific module of 98*0Sstevel@tonic-gate * each entry which has a numeric fsckpass field. 99*0Sstevel@tonic-gate */ 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate if (argc == 1) { /* no arguments or options */ 102*0Sstevel@tonic-gate if (strcmp(cbasename, "ncheck") == 0) { 103*0Sstevel@tonic-gate /* open VFSTAB */ 104*0Sstevel@tonic-gate if ((fp = fopen(VFSTAB, "r")) == NULL) { 105*0Sstevel@tonic-gate fprintf(stderr, "%s: cannot open vfstab\n", 106*0Sstevel@tonic-gate cbasename); 107*0Sstevel@tonic-gate exit(2); 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate while ((i = getvfsent(fp, &vfsbuf)) == 0) { 110*0Sstevel@tonic-gate if (numbers(vfsbuf.vfs_fsckpass)) { 111*0Sstevel@tonic-gate fstype = vfsbuf.vfs_fstype; 112*0Sstevel@tonic-gate newargv[newargc] = vfsbuf.vfs_special; 113*0Sstevel@tonic-gate exec_specific(); 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate exit(0); 117*0Sstevel@tonic-gate } 118*0Sstevel@tonic-gate fprintf(stderr, "Usage:\n"); 119*0Sstevel@tonic-gate fprintf(stderr, 120*0Sstevel@tonic-gate "%s [-F FSType] [-V] [current_options] [-o specific_options] special ...\n", 121*0Sstevel@tonic-gate cbasename); 122*0Sstevel@tonic-gate exit(2); 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate for (c_ptr = cmd_data; ((c_ptr->c_basename != NULL) && 126*0Sstevel@tonic-gate (strcmp(c_ptr->c_basename, cbasename) != 0)); c_ptr++) 127*0Sstevel@tonic-gate ; 128*0Sstevel@tonic-gate while ((arg = getopt(argc, argv, c_ptr->c_optstr)) != -1) { 129*0Sstevel@tonic-gate switch (arg) { 130*0Sstevel@tonic-gate case 'V': /* echo complete command line */ 131*0Sstevel@tonic-gate verbose = 1; 132*0Sstevel@tonic-gate break; 133*0Sstevel@tonic-gate case 'F': /* FSType specified */ 134*0Sstevel@tonic-gate F_flg++; 135*0Sstevel@tonic-gate fstype = optarg; 136*0Sstevel@tonic-gate break; 137*0Sstevel@tonic-gate case 'o': /* FSType specific arguments */ 138*0Sstevel@tonic-gate newargv[newargc++] = "-o"; 139*0Sstevel@tonic-gate newargv[newargc++] = optarg; 140*0Sstevel@tonic-gate break; 141*0Sstevel@tonic-gate case '?': /* print usage message */ 142*0Sstevel@tonic-gate newargv[newargc++] = "-?"; 143*0Sstevel@tonic-gate usgflag = 1; 144*0Sstevel@tonic-gate break; 145*0Sstevel@tonic-gate default: 146*0Sstevel@tonic-gate newargv[newargc] = (char *)malloc(3); 147*0Sstevel@tonic-gate sprintf(newargv[newargc++], "-%c", arg); 148*0Sstevel@tonic-gate if (optarg) 149*0Sstevel@tonic-gate newargv[newargc++] = optarg; 150*0Sstevel@tonic-gate break; 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate optarg = NULL; 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate if (F_flg > 1) { 155*0Sstevel@tonic-gate fprintf(stderr, "%s: more than one FSType specified\n", 156*0Sstevel@tonic-gate cbasename); 157*0Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr); 158*0Sstevel@tonic-gate } 159*0Sstevel@tonic-gate if (F_flg && (strlen(fstype) > (size_t)FSTYPE_MAX)) { 160*0Sstevel@tonic-gate fprintf(stderr, "%s: FSType %s exceeds %d characters\n", 161*0Sstevel@tonic-gate cbasename, fstype, FSTYPE_MAX); 162*0Sstevel@tonic-gate exit(2); 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate if (optind == argc) { 165*0Sstevel@tonic-gate /* all commands except ncheck must exit now */ 166*0Sstevel@tonic-gate if (strcmp(cbasename, "ncheck") != 0) { 167*0Sstevel@tonic-gate if ((F_flg) && (usgflag)) { 168*0Sstevel@tonic-gate exec_specific(); 169*0Sstevel@tonic-gate exit(0); 170*0Sstevel@tonic-gate } 171*0Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr); 172*0Sstevel@tonic-gate } 173*0Sstevel@tonic-gate if ((F_flg) && (usgflag)) { 174*0Sstevel@tonic-gate exec_specific(); 175*0Sstevel@tonic-gate exit(0); 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate if (usgflag) 178*0Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr); 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate /* open VFSTAB */ 181*0Sstevel@tonic-gate if ((fp = fopen(VFSTAB, "r")) == NULL) { 182*0Sstevel@tonic-gate fprintf(stderr, "%s: cannot open vfstab\n", cbasename); 183*0Sstevel@tonic-gate exit(2); 184*0Sstevel@tonic-gate } 185*0Sstevel@tonic-gate while ((i = getvfsent(fp, &vfsbuf)) == 0) { 186*0Sstevel@tonic-gate if (!numbers(vfsbuf.vfs_fsckpass)) 187*0Sstevel@tonic-gate continue; 188*0Sstevel@tonic-gate if ((F_flg) && (strcmp(fstype, vfsbuf.vfs_fstype) != 0)) 189*0Sstevel@tonic-gate continue; 190*0Sstevel@tonic-gate fs_flag++; 191*0Sstevel@tonic-gate fstype = vfsbuf.vfs_fstype; 192*0Sstevel@tonic-gate newargv[newargc] = vfsbuf.vfs_special; 193*0Sstevel@tonic-gate if (verbose) { 194*0Sstevel@tonic-gate printf("%s -F %s ", cbasename, 195*0Sstevel@tonic-gate vfsbuf.vfs_fstype); 196*0Sstevel@tonic-gate for (i = 2; newargv[i]; i++) 197*0Sstevel@tonic-gate printf("%s\n", newargv[i]); 198*0Sstevel@tonic-gate continue; 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate exec_specific(); 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate /* 203*0Sstevel@tonic-gate * if (! fs_flag) { 204*0Sstevel@tonic-gate * if (sysfs(GETFSIND, fstype) == (-1)) { 205*0Sstevel@tonic-gate * fprintf(stderr, 206*0Sstevel@tonic-gate * "%s: FSType %s not installed in the kernel\n", 207*0Sstevel@tonic-gate * cbasename, fstype); 208*0Sstevel@tonic-gate * exit(1); 209*0Sstevel@tonic-gate * } 210*0Sstevel@tonic-gate * } 211*0Sstevel@tonic-gate */ 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate exit(0); 214*0Sstevel@tonic-gate } 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate /* All other arguments must be specials */ 217*0Sstevel@tonic-gate /* perform a lookup if fstype is not specified */ 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate for (; optind < argc; optind++) { 220*0Sstevel@tonic-gate newargv[newargc] = argv[optind]; 221*0Sstevel@tonic-gate special = newargv[newargc]; 222*0Sstevel@tonic-gate if ((F_flg) && (usgflag)) { 223*0Sstevel@tonic-gate exec_specific(); 224*0Sstevel@tonic-gate exit(0); 225*0Sstevel@tonic-gate } 226*0Sstevel@tonic-gate if (usgflag) 227*0Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr); 228*0Sstevel@tonic-gate if (fstype == NULL) 229*0Sstevel@tonic-gate lookup(); 230*0Sstevel@tonic-gate if (verbose) { 231*0Sstevel@tonic-gate printf("%s -F %s ", cbasename, fstype); 232*0Sstevel@tonic-gate for (i = 2; newargv[i]; i++) 233*0Sstevel@tonic-gate printf("%s ", newargv[i]); 234*0Sstevel@tonic-gate printf("\n"); 235*0Sstevel@tonic-gate continue; 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate exec_specific(); 238*0Sstevel@tonic-gate if (!F_flg) 239*0Sstevel@tonic-gate fstype = NULL; 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate exit(0); 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate /* see if all numbers */ 245*0Sstevel@tonic-gate numbers(yp) 246*0Sstevel@tonic-gate char *yp; 247*0Sstevel@tonic-gate { 248*0Sstevel@tonic-gate if (yp == NULL) 249*0Sstevel@tonic-gate return (0); 250*0Sstevel@tonic-gate while ('0' <= *yp && *yp <= '9') 251*0Sstevel@tonic-gate yp++; 252*0Sstevel@tonic-gate if (*yp) 253*0Sstevel@tonic-gate return (0); 254*0Sstevel@tonic-gate return (1); 255*0Sstevel@tonic-gate } 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate usage(cmd, usg) 259*0Sstevel@tonic-gate char *cmd, *usg; 260*0Sstevel@tonic-gate { 261*0Sstevel@tonic-gate fprintf(stderr, "Usage:\n"); 262*0Sstevel@tonic-gate fprintf(stderr, "%s %s\n", cmd, usg); 263*0Sstevel@tonic-gate exit(2); 264*0Sstevel@tonic-gate } 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate /* 268*0Sstevel@tonic-gate * This looks up the /etc/vfstab entry given the device 'special'. 269*0Sstevel@tonic-gate * It is called when the fstype is not specified on the command line. 270*0Sstevel@tonic-gate * 271*0Sstevel@tonic-gate * The following global variables are used: 272*0Sstevel@tonic-gate * special, fstype 273*0Sstevel@tonic-gate */ 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate lookup() 276*0Sstevel@tonic-gate { 277*0Sstevel@tonic-gate FILE *fd; 278*0Sstevel@tonic-gate int ret; 279*0Sstevel@tonic-gate struct vfstab vget, vref; 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate if ((fd = fopen(vfstab, "r")) == NULL) { 282*0Sstevel@tonic-gate fprintf(stderr, "%s: cannot open vfstab\n", cbasename); 283*0Sstevel@tonic-gate exit(1); 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate vfsnull(&vref); 286*0Sstevel@tonic-gate vref.vfs_special = special; 287*0Sstevel@tonic-gate ret = getvfsany(fd, &vget, &vref); 288*0Sstevel@tonic-gate if (ret == -1) { 289*0Sstevel@tonic-gate rewind(fd); 290*0Sstevel@tonic-gate vfsnull(&vref); 291*0Sstevel@tonic-gate vref.vfs_fsckdev = special; 292*0Sstevel@tonic-gate ret = getvfsany(fd, &vget, &vref); 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate fclose(fd); 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate switch (ret) { 297*0Sstevel@tonic-gate case -1: 298*0Sstevel@tonic-gate fstype = default_fstype(special); 299*0Sstevel@tonic-gate break; 300*0Sstevel@tonic-gate case 0: 301*0Sstevel@tonic-gate fstype = vget.vfs_fstype; 302*0Sstevel@tonic-gate break; 303*0Sstevel@tonic-gate case VFS_TOOLONG: 304*0Sstevel@tonic-gate fprintf(stderr, "%s: line in vfstab exceeds %d characters\n", 305*0Sstevel@tonic-gate cbasename, VFS_LINE_MAX-2); 306*0Sstevel@tonic-gate exit(1); 307*0Sstevel@tonic-gate break; 308*0Sstevel@tonic-gate case VFS_TOOFEW: 309*0Sstevel@tonic-gate fprintf(stderr, "%s: line in vfstab has too few entries\n", 310*0Sstevel@tonic-gate cbasename); 311*0Sstevel@tonic-gate exit(1); 312*0Sstevel@tonic-gate break; 313*0Sstevel@tonic-gate case VFS_TOOMANY: 314*0Sstevel@tonic-gate fprintf(stderr, "%s: line in vfstab has too many entries\n", 315*0Sstevel@tonic-gate cbasename); 316*0Sstevel@tonic-gate exit(1); 317*0Sstevel@tonic-gate break; 318*0Sstevel@tonic-gate } 319*0Sstevel@tonic-gate } 320*0Sstevel@tonic-gate exec_specific() 321*0Sstevel@tonic-gate { 322*0Sstevel@tonic-gate int status, pid, ret; 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate sprintf(full_path, "%s/%s/%s", vfs_path, fstype, cbasename); 325*0Sstevel@tonic-gate newargv[1] = &full_path[FULLPATH_MAX]; 326*0Sstevel@tonic-gate while (*newargv[1]-- != '/'); 327*0Sstevel@tonic-gate newargv[1] += 2; 328*0Sstevel@tonic-gate switch (pid = fork()) { 329*0Sstevel@tonic-gate case 0: 330*0Sstevel@tonic-gate execv(full_path, &newargv[1]); 331*0Sstevel@tonic-gate if (errno == ENOEXEC) { 332*0Sstevel@tonic-gate newargv[0] = "sh"; 333*0Sstevel@tonic-gate newargv[1] = full_path; 334*0Sstevel@tonic-gate execv("/sbin/sh", &newargv[0]); 335*0Sstevel@tonic-gate } 336*0Sstevel@tonic-gate if (errno != ENOENT) { 337*0Sstevel@tonic-gate perror(cbasename); 338*0Sstevel@tonic-gate fprintf(stderr, "%s: cannot execute %s\n", cbasename, 339*0Sstevel@tonic-gate full_path); 340*0Sstevel@tonic-gate exit(1); 341*0Sstevel@tonic-gate } 342*0Sstevel@tonic-gate if (sysfs(GETFSIND, fstype) == (-1)) { 343*0Sstevel@tonic-gate fprintf(stderr, 344*0Sstevel@tonic-gate "%s: FSType %s not installed in the kernel\n", 345*0Sstevel@tonic-gate cbasename, fstype); 346*0Sstevel@tonic-gate exit(1); 347*0Sstevel@tonic-gate } 348*0Sstevel@tonic-gate fprintf(stderr, "%s: operation not applicable for FSType %s\n", 349*0Sstevel@tonic-gate cbasename, fstype); 350*0Sstevel@tonic-gate exit(1); 351*0Sstevel@tonic-gate case -1: 352*0Sstevel@tonic-gate fprintf(stderr, "%s: cannot fork process\n", cbasename); 353*0Sstevel@tonic-gate exit(2); 354*0Sstevel@tonic-gate default: 355*0Sstevel@tonic-gate /* 356*0Sstevel@tonic-gate * if cannot exec specific, or fstype is not installed, exit 357*0Sstevel@tonic-gate * after first 'exec_specific' to avoid printing duplicate 358*0Sstevel@tonic-gate * error messages 359*0Sstevel@tonic-gate */ 360*0Sstevel@tonic-gate 361*0Sstevel@tonic-gate if (wait(&status) == pid) { 362*0Sstevel@tonic-gate ret = WHIBYTE(status); 363*0Sstevel@tonic-gate if (ret > 0) { 364*0Sstevel@tonic-gate exit(ret); 365*0Sstevel@tonic-gate } 366*0Sstevel@tonic-gate } 367*0Sstevel@tonic-gate } 368*0Sstevel@tonic-gate } 369