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