1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * CDDL HEADER START 3*eda14cbcSMatt Macy * 4*eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5*eda14cbcSMatt Macy * Common Development and Distribution License, Version 1.0 only 6*eda14cbcSMatt Macy * (the "License"). You may not use this file except in compliance 7*eda14cbcSMatt Macy * with the License. 8*eda14cbcSMatt Macy * 9*eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 11*eda14cbcSMatt Macy * See the License for the specific language governing permissions 12*eda14cbcSMatt Macy * and limitations under the License. 13*eda14cbcSMatt Macy * 14*eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 15*eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 17*eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 18*eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 19*eda14cbcSMatt Macy * 20*eda14cbcSMatt Macy * CDDL HEADER END 21*eda14cbcSMatt Macy */ 22*eda14cbcSMatt Macy /* 23*eda14cbcSMatt Macy * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*eda14cbcSMatt Macy * Copyright 2006 Ricardo Correia. All rights reserved. 25*eda14cbcSMatt Macy * Use is subject to license terms. 26*eda14cbcSMatt Macy */ 27*eda14cbcSMatt Macy 28*eda14cbcSMatt Macy /* Copyright (c) 1988 AT&T */ 29*eda14cbcSMatt Macy /* All Rights Reserved */ 30*eda14cbcSMatt Macy 31*eda14cbcSMatt Macy #include <stdio.h> 32*eda14cbcSMatt Macy #include <string.h> 33*eda14cbcSMatt Macy #include <mntent.h> 34*eda14cbcSMatt Macy #include <sys/errno.h> 35*eda14cbcSMatt Macy #include <sys/mnttab.h> 36*eda14cbcSMatt Macy 37*eda14cbcSMatt Macy #include <sys/types.h> 38*eda14cbcSMatt Macy #include <sys/sysmacros.h> 39*eda14cbcSMatt Macy #include <sys/stat.h> 40*eda14cbcSMatt Macy #include <unistd.h> 41*eda14cbcSMatt Macy 42*eda14cbcSMatt Macy #define BUFSIZE (MNT_LINE_MAX + 2) 43*eda14cbcSMatt Macy 44*eda14cbcSMatt Macy __thread char buf[BUFSIZE]; 45*eda14cbcSMatt Macy 46*eda14cbcSMatt Macy #define DIFF(xx) ( \ 47*eda14cbcSMatt Macy (mrefp->xx != NULL) && \ 48*eda14cbcSMatt Macy (mgetp->xx == NULL || strcmp(mrefp->xx, mgetp->xx) != 0)) 49*eda14cbcSMatt Macy 50*eda14cbcSMatt Macy int 51*eda14cbcSMatt Macy getmntany(FILE *fp, struct mnttab *mgetp, struct mnttab *mrefp) 52*eda14cbcSMatt Macy { 53*eda14cbcSMatt Macy int ret; 54*eda14cbcSMatt Macy 55*eda14cbcSMatt Macy while ( 56*eda14cbcSMatt Macy ((ret = _sol_getmntent(fp, mgetp)) == 0) && ( 57*eda14cbcSMatt Macy DIFF(mnt_special) || DIFF(mnt_mountp) || 58*eda14cbcSMatt Macy DIFF(mnt_fstype) || DIFF(mnt_mntopts))) { } 59*eda14cbcSMatt Macy 60*eda14cbcSMatt Macy return (ret); 61*eda14cbcSMatt Macy } 62*eda14cbcSMatt Macy 63*eda14cbcSMatt Macy int 64*eda14cbcSMatt Macy _sol_getmntent(FILE *fp, struct mnttab *mgetp) 65*eda14cbcSMatt Macy { 66*eda14cbcSMatt Macy struct mntent mntbuf; 67*eda14cbcSMatt Macy struct mntent *ret; 68*eda14cbcSMatt Macy 69*eda14cbcSMatt Macy ret = getmntent_r(fp, &mntbuf, buf, BUFSIZE); 70*eda14cbcSMatt Macy 71*eda14cbcSMatt Macy if (ret != NULL) { 72*eda14cbcSMatt Macy mgetp->mnt_special = mntbuf.mnt_fsname; 73*eda14cbcSMatt Macy mgetp->mnt_mountp = mntbuf.mnt_dir; 74*eda14cbcSMatt Macy mgetp->mnt_fstype = mntbuf.mnt_type; 75*eda14cbcSMatt Macy mgetp->mnt_mntopts = mntbuf.mnt_opts; 76*eda14cbcSMatt Macy return (0); 77*eda14cbcSMatt Macy } 78*eda14cbcSMatt Macy 79*eda14cbcSMatt Macy if (feof(fp)) 80*eda14cbcSMatt Macy return (-1); 81*eda14cbcSMatt Macy 82*eda14cbcSMatt Macy return (MNT_TOOLONG); 83*eda14cbcSMatt Macy } 84*eda14cbcSMatt Macy 85*eda14cbcSMatt Macy static int 86*eda14cbcSMatt Macy getextmntent_impl(FILE *fp, struct extmnttab *mp, int len) 87*eda14cbcSMatt Macy { 88*eda14cbcSMatt Macy int ret; 89*eda14cbcSMatt Macy struct stat64 st; 90*eda14cbcSMatt Macy 91*eda14cbcSMatt Macy ret = _sol_getmntent(fp, (struct mnttab *)mp); 92*eda14cbcSMatt Macy if (ret == 0) { 93*eda14cbcSMatt Macy if (stat64(mp->mnt_mountp, &st) != 0) { 94*eda14cbcSMatt Macy mp->mnt_major = 0; 95*eda14cbcSMatt Macy mp->mnt_minor = 0; 96*eda14cbcSMatt Macy return (ret); 97*eda14cbcSMatt Macy } 98*eda14cbcSMatt Macy mp->mnt_major = major(st.st_dev); 99*eda14cbcSMatt Macy mp->mnt_minor = minor(st.st_dev); 100*eda14cbcSMatt Macy } 101*eda14cbcSMatt Macy 102*eda14cbcSMatt Macy return (ret); 103*eda14cbcSMatt Macy } 104*eda14cbcSMatt Macy 105*eda14cbcSMatt Macy int 106*eda14cbcSMatt Macy getextmntent(const char *path, struct extmnttab *entry, struct stat64 *statbuf) 107*eda14cbcSMatt Macy { 108*eda14cbcSMatt Macy struct stat64 st; 109*eda14cbcSMatt Macy FILE *fp; 110*eda14cbcSMatt Macy int match; 111*eda14cbcSMatt Macy 112*eda14cbcSMatt Macy if (strlen(path) >= MAXPATHLEN) { 113*eda14cbcSMatt Macy (void) fprintf(stderr, "invalid object; pathname too long\n"); 114*eda14cbcSMatt Macy return (-1); 115*eda14cbcSMatt Macy } 116*eda14cbcSMatt Macy 117*eda14cbcSMatt Macy /* 118*eda14cbcSMatt Macy * Search for the path in /proc/self/mounts. Rather than looking for the 119*eda14cbcSMatt Macy * specific path, which can be fooled by non-standard paths (i.e. ".." 120*eda14cbcSMatt Macy * or "//"), we stat() the path and search for the corresponding 121*eda14cbcSMatt Macy * (major,minor) device pair. 122*eda14cbcSMatt Macy */ 123*eda14cbcSMatt Macy if (stat64(path, statbuf) != 0) { 124*eda14cbcSMatt Macy (void) fprintf(stderr, "cannot open '%s': %s\n", 125*eda14cbcSMatt Macy path, strerror(errno)); 126*eda14cbcSMatt Macy return (-1); 127*eda14cbcSMatt Macy } 128*eda14cbcSMatt Macy 129*eda14cbcSMatt Macy 130*eda14cbcSMatt Macy #ifdef HAVE_SETMNTENT 131*eda14cbcSMatt Macy if ((fp = setmntent(MNTTAB, "r")) == NULL) { 132*eda14cbcSMatt Macy #else 133*eda14cbcSMatt Macy if ((fp = fopen(MNTTAB, "r")) == NULL) { 134*eda14cbcSMatt Macy #endif 135*eda14cbcSMatt Macy (void) fprintf(stderr, "cannot open %s\n", MNTTAB); 136*eda14cbcSMatt Macy return (-1); 137*eda14cbcSMatt Macy } 138*eda14cbcSMatt Macy 139*eda14cbcSMatt Macy /* 140*eda14cbcSMatt Macy * Search for the given (major,minor) pair in the mount table. 141*eda14cbcSMatt Macy */ 142*eda14cbcSMatt Macy 143*eda14cbcSMatt Macy match = 0; 144*eda14cbcSMatt Macy while (getextmntent_impl(fp, entry, sizeof (*entry)) == 0) { 145*eda14cbcSMatt Macy if (makedev(entry->mnt_major, entry->mnt_minor) == 146*eda14cbcSMatt Macy statbuf->st_dev) { 147*eda14cbcSMatt Macy match = 1; 148*eda14cbcSMatt Macy break; 149*eda14cbcSMatt Macy } 150*eda14cbcSMatt Macy } 151*eda14cbcSMatt Macy 152*eda14cbcSMatt Macy if (!match) { 153*eda14cbcSMatt Macy (void) fprintf(stderr, "cannot find mountpoint for '%s'\n", 154*eda14cbcSMatt Macy path); 155*eda14cbcSMatt Macy return (-1); 156*eda14cbcSMatt Macy } 157*eda14cbcSMatt Macy 158*eda14cbcSMatt Macy if (stat64(entry->mnt_mountp, &st) != 0) { 159*eda14cbcSMatt Macy entry->mnt_major = 0; 160*eda14cbcSMatt Macy entry->mnt_minor = 0; 161*eda14cbcSMatt Macy return (-1); 162*eda14cbcSMatt Macy } 163*eda14cbcSMatt Macy 164*eda14cbcSMatt Macy return (0); 165*eda14cbcSMatt Macy } 166