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