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 int
_sol_getmntent(FILE * fp,struct mnttab * mgetp)49 _sol_getmntent(FILE *fp, struct mnttab *mgetp)
50 {
51 struct mntent mntbuf;
52 struct mntent *ret;
53
54 ret = getmntent_r(fp, &mntbuf, buf, BUFSIZE);
55
56 if (ret != NULL) {
57 mgetp->mnt_special = mntbuf.mnt_fsname;
58 mgetp->mnt_mountp = mntbuf.mnt_dir;
59 mgetp->mnt_fstype = mntbuf.mnt_type;
60 mgetp->mnt_mntopts = mntbuf.mnt_opts;
61 return (0);
62 }
63
64 if (feof(fp))
65 return (-1);
66
67 return (MNT_TOOLONG);
68 }
69
70 static int
getextmntent_impl(FILE * fp,struct mnttab * mp,uint64_t * mnt_id,dev_t * dev)71 getextmntent_impl(FILE *fp, struct mnttab *mp, uint64_t *mnt_id, dev_t *dev)
72 {
73 int ret;
74 struct stat64 st;
75
76 *mnt_id = 0;
77 ret = _sol_getmntent(fp, (struct mnttab *)mp);
78 if (ret == 0) {
79 #ifdef HAVE_STATX_MNT_ID
80 struct statx stx;
81 if (statx(AT_FDCWD, mp->mnt_mountp,
82 AT_STATX_SYNC_AS_STAT | AT_SYMLINK_NOFOLLOW,
83 STATX_MNT_ID, &stx) == 0 && (stx.stx_mask & STATX_MNT_ID))
84 *mnt_id = stx.stx_mnt_id;
85 #endif
86 if (stat64(mp->mnt_mountp, &st) != 0) {
87 *dev = 0;
88 return (ret);
89 }
90 *dev = st.st_dev;
91 }
92
93 return (ret);
94 }
95
96 int
getextmntent(const char * path,struct mnttab * entry,struct stat64 * statbuf)97 getextmntent(const char *path, struct mnttab *entry, struct stat64 *statbuf)
98 {
99 struct stat64 st;
100 FILE *fp;
101 int match;
102 boolean_t have_mnt_id = B_FALSE;
103 uint64_t target_mnt_id = 0;
104 uint64_t entry_mnt_id;
105 dev_t dev;
106 #ifdef HAVE_STATX_MNT_ID
107 struct statx stx;
108 #endif
109
110 if (strlen(path) >= MAXPATHLEN) {
111 (void) fprintf(stderr, "invalid object; pathname too long\n");
112 return (-1);
113 }
114
115 /*
116 * Search for the path in /proc/self/mounts. Rather than looking for the
117 * specific path, which can be fooled by non-standard paths (i.e. ".."
118 * or "//"), we stat() the path and search for the corresponding
119 * (major,minor) device pair.
120 */
121 if (stat64(path, statbuf) != 0) {
122 (void) fprintf(stderr, "cannot open '%s': %s\n",
123 path, zfs_strerror(errno));
124 return (-1);
125 }
126
127 #ifdef HAVE_STATX_MNT_ID
128 if (statx(AT_FDCWD, path, AT_STATX_SYNC_AS_STAT | AT_SYMLINK_NOFOLLOW,
129 STATX_MNT_ID, &stx) == 0 && (stx.stx_mask & STATX_MNT_ID)) {
130 have_mnt_id = B_TRUE;
131 target_mnt_id = stx.stx_mnt_id;
132 }
133 #endif
134
135 if ((fp = fopen(MNTTAB, "re")) == NULL) {
136 (void) fprintf(stderr, "cannot open %s\n", MNTTAB);
137 return (-1);
138 }
139
140 /*
141 * Search for the given (major,minor) pair in the mount table.
142 */
143
144 match = 0;
145 while (getextmntent_impl(fp, entry, &entry_mnt_id, &dev) == 0) {
146 if (have_mnt_id) {
147 match = (entry_mnt_id == target_mnt_id);
148 } else {
149 match = (dev == statbuf->st_dev);
150 }
151 if (match)
152 break;
153 }
154 (void) fclose(fp);
155
156 if (!match) {
157 (void) fprintf(stderr, "cannot find mountpoint for '%s'\n",
158 path);
159 return (-1);
160 }
161
162 if (stat64(entry->mnt_mountp, &st) != 0)
163 return (-1);
164
165 return (0);
166 }
167