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
10271171e0SMartin 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>
41*fd45b686SMartin Matuska #include <libzutil.h>
42eda14cbcSMatt Macy
43eda14cbcSMatt Macy #define BUFSIZE (MNT_LINE_MAX + 2)
44eda14cbcSMatt Macy
4516038816SMartin Matuska static __thread char buf[BUFSIZE];
46eda14cbcSMatt Macy
47eda14cbcSMatt Macy #define DIFF(xx) ( \
48eda14cbcSMatt Macy (mrefp->xx != NULL) && \
49eda14cbcSMatt Macy (mgetp->xx == NULL || strcmp(mrefp->xx, mgetp->xx) != 0))
50eda14cbcSMatt Macy
51eda14cbcSMatt Macy int
getmntany(FILE * fp,struct mnttab * mgetp,struct mnttab * mrefp)52eda14cbcSMatt Macy getmntany(FILE *fp, struct mnttab *mgetp, struct mnttab *mrefp)
53eda14cbcSMatt Macy {
54eda14cbcSMatt Macy int ret;
55eda14cbcSMatt Macy
56eda14cbcSMatt Macy while (
57eda14cbcSMatt Macy ((ret = _sol_getmntent(fp, mgetp)) == 0) && (
58eda14cbcSMatt Macy DIFF(mnt_special) || DIFF(mnt_mountp) ||
59eda14cbcSMatt Macy DIFF(mnt_fstype) || DIFF(mnt_mntopts))) { }
60eda14cbcSMatt Macy
61eda14cbcSMatt Macy return (ret);
62eda14cbcSMatt Macy }
63eda14cbcSMatt Macy
64eda14cbcSMatt Macy int
_sol_getmntent(FILE * fp,struct mnttab * mgetp)65eda14cbcSMatt Macy _sol_getmntent(FILE *fp, struct mnttab *mgetp)
66eda14cbcSMatt Macy {
67eda14cbcSMatt Macy struct mntent mntbuf;
68eda14cbcSMatt Macy struct mntent *ret;
69eda14cbcSMatt Macy
70eda14cbcSMatt Macy ret = getmntent_r(fp, &mntbuf, buf, BUFSIZE);
71eda14cbcSMatt Macy
72eda14cbcSMatt Macy if (ret != NULL) {
73eda14cbcSMatt Macy mgetp->mnt_special = mntbuf.mnt_fsname;
74eda14cbcSMatt Macy mgetp->mnt_mountp = mntbuf.mnt_dir;
75eda14cbcSMatt Macy mgetp->mnt_fstype = mntbuf.mnt_type;
76eda14cbcSMatt Macy mgetp->mnt_mntopts = mntbuf.mnt_opts;
77eda14cbcSMatt Macy return (0);
78eda14cbcSMatt Macy }
79eda14cbcSMatt Macy
80eda14cbcSMatt Macy if (feof(fp))
81eda14cbcSMatt Macy return (-1);
82eda14cbcSMatt Macy
83eda14cbcSMatt Macy return (MNT_TOOLONG);
84eda14cbcSMatt Macy }
85eda14cbcSMatt Macy
86eda14cbcSMatt Macy static int
getextmntent_impl(FILE * fp,struct extmnttab * mp)87681ce946SMartin Matuska getextmntent_impl(FILE *fp, struct extmnttab *mp)
88eda14cbcSMatt Macy {
89eda14cbcSMatt Macy int ret;
90eda14cbcSMatt Macy struct stat64 st;
91eda14cbcSMatt Macy
92eda14cbcSMatt Macy ret = _sol_getmntent(fp, (struct mnttab *)mp);
93eda14cbcSMatt Macy if (ret == 0) {
94eda14cbcSMatt Macy if (stat64(mp->mnt_mountp, &st) != 0) {
95eda14cbcSMatt Macy mp->mnt_major = 0;
96eda14cbcSMatt Macy mp->mnt_minor = 0;
97eda14cbcSMatt Macy return (ret);
98eda14cbcSMatt Macy }
99eda14cbcSMatt Macy mp->mnt_major = major(st.st_dev);
100eda14cbcSMatt Macy mp->mnt_minor = minor(st.st_dev);
101eda14cbcSMatt Macy }
102eda14cbcSMatt Macy
103eda14cbcSMatt Macy return (ret);
104eda14cbcSMatt Macy }
105eda14cbcSMatt Macy
106eda14cbcSMatt Macy int
getextmntent(const char * path,struct extmnttab * entry,struct stat64 * statbuf)107eda14cbcSMatt Macy getextmntent(const char *path, struct extmnttab *entry, struct stat64 *statbuf)
108eda14cbcSMatt Macy {
109eda14cbcSMatt Macy struct stat64 st;
110eda14cbcSMatt Macy FILE *fp;
111eda14cbcSMatt Macy int match;
112eda14cbcSMatt Macy
113eda14cbcSMatt Macy if (strlen(path) >= MAXPATHLEN) {
114eda14cbcSMatt Macy (void) fprintf(stderr, "invalid object; pathname too long\n");
115eda14cbcSMatt Macy return (-1);
116eda14cbcSMatt Macy }
117eda14cbcSMatt Macy
118eda14cbcSMatt Macy /*
119eda14cbcSMatt Macy * Search for the path in /proc/self/mounts. Rather than looking for the
120eda14cbcSMatt Macy * specific path, which can be fooled by non-standard paths (i.e. ".."
121eda14cbcSMatt Macy * or "//"), we stat() the path and search for the corresponding
122eda14cbcSMatt Macy * (major,minor) device pair.
123eda14cbcSMatt Macy */
124eda14cbcSMatt Macy if (stat64(path, statbuf) != 0) {
125eda14cbcSMatt Macy (void) fprintf(stderr, "cannot open '%s': %s\n",
126*fd45b686SMartin Matuska path, zfs_strerror(errno));
127eda14cbcSMatt Macy return (-1);
128eda14cbcSMatt Macy }
129eda14cbcSMatt Macy
130eda14cbcSMatt Macy
13116038816SMartin Matuska if ((fp = fopen(MNTTAB, "re")) == NULL) {
132eda14cbcSMatt Macy (void) fprintf(stderr, "cannot open %s\n", MNTTAB);
133eda14cbcSMatt Macy return (-1);
134eda14cbcSMatt Macy }
135eda14cbcSMatt Macy
136eda14cbcSMatt Macy /*
137eda14cbcSMatt Macy * Search for the given (major,minor) pair in the mount table.
138eda14cbcSMatt Macy */
139eda14cbcSMatt Macy
140eda14cbcSMatt Macy match = 0;
141681ce946SMartin Matuska while (getextmntent_impl(fp, entry) == 0) {
142eda14cbcSMatt Macy if (makedev(entry->mnt_major, entry->mnt_minor) ==
143eda14cbcSMatt Macy statbuf->st_dev) {
144eda14cbcSMatt Macy match = 1;
145eda14cbcSMatt Macy break;
146eda14cbcSMatt Macy }
147eda14cbcSMatt Macy }
14816038816SMartin Matuska (void) fclose(fp);
149eda14cbcSMatt Macy
150eda14cbcSMatt Macy if (!match) {
151eda14cbcSMatt Macy (void) fprintf(stderr, "cannot find mountpoint for '%s'\n",
152eda14cbcSMatt Macy path);
153eda14cbcSMatt Macy return (-1);
154eda14cbcSMatt Macy }
155eda14cbcSMatt Macy
156eda14cbcSMatt Macy if (stat64(entry->mnt_mountp, &st) != 0) {
157eda14cbcSMatt Macy entry->mnt_major = 0;
158eda14cbcSMatt Macy entry->mnt_minor = 0;
159eda14cbcSMatt Macy return (-1);
160eda14cbcSMatt Macy }
161eda14cbcSMatt Macy
162eda14cbcSMatt Macy return (0);
163eda14cbcSMatt Macy }
164