Path: blob/main/sys/contrib/openzfs/lib/libspl/os/linux/getmntany.c
48546 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License, Version 1.0 only6* (the "License"). You may not use this file except in compliance7* with the License.8*9* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE10* or https://opensource.org/licenses/CDDL-1.0.11* See the License for the specific language governing permissions12* and limitations under the License.13*14* When distributing Covered Code, include this CDDL HEADER in each15* file and include the License file at usr/src/OPENSOLARIS.LICENSE.16* If applicable, add the following below this CDDL HEADER, with the17* fields enclosed by brackets "[]" replaced with your own identifying18* information: Portions Copyright [yyyy] [name of copyright owner]19*20* CDDL HEADER END21*/22/*23* Copyright 2005 Sun Microsystems, Inc. All rights reserved.24* Copyright 2006 Ricardo Correia. All rights reserved.25* Use is subject to license terms.26*/2728/* Copyright (c) 1988 AT&T */29/* All Rights Reserved */3031#include <stdio.h>32#include <string.h>33#include <mntent.h>34#include <sys/errno.h>35#include <sys/mnttab.h>3637#include <sys/types.h>38#include <sys/sysmacros.h>39#include <sys/stat.h>40#include <unistd.h>41#include <libzutil.h>4243#define BUFSIZE (MNT_LINE_MAX + 2)4445static __thread char buf[BUFSIZE];4647#define DIFF(xx) ( \48(mrefp->xx != NULL) && \49(mgetp->xx == NULL || strcmp(mrefp->xx, mgetp->xx) != 0))5051int52getmntany(FILE *fp, struct mnttab *mgetp, struct mnttab *mrefp)53{54int ret;5556while (57((ret = _sol_getmntent(fp, mgetp)) == 0) && (58DIFF(mnt_special) || DIFF(mnt_mountp) ||59DIFF(mnt_fstype) || DIFF(mnt_mntopts))) { }6061return (ret);62}6364int65_sol_getmntent(FILE *fp, struct mnttab *mgetp)66{67struct mntent mntbuf;68struct mntent *ret;6970ret = getmntent_r(fp, &mntbuf, buf, BUFSIZE);7172if (ret != NULL) {73mgetp->mnt_special = mntbuf.mnt_fsname;74mgetp->mnt_mountp = mntbuf.mnt_dir;75mgetp->mnt_fstype = mntbuf.mnt_type;76mgetp->mnt_mntopts = mntbuf.mnt_opts;77return (0);78}7980if (feof(fp))81return (-1);8283return (MNT_TOOLONG);84}8586static int87getextmntent_impl(FILE *fp, struct extmnttab *mp, uint64_t *mnt_id)88{89int ret;90struct stat64 st;9192*mnt_id = 0;93ret = _sol_getmntent(fp, (struct mnttab *)mp);94if (ret == 0) {95#ifdef HAVE_STATX_MNT_ID96struct statx stx;97if (statx(AT_FDCWD, mp->mnt_mountp,98AT_STATX_SYNC_AS_STAT | AT_SYMLINK_NOFOLLOW,99STATX_MNT_ID, &stx) == 0 && (stx.stx_mask & STATX_MNT_ID))100*mnt_id = stx.stx_mnt_id;101#endif102if (stat64(mp->mnt_mountp, &st) != 0) {103mp->mnt_major = 0;104mp->mnt_minor = 0;105return (ret);106}107mp->mnt_major = major(st.st_dev);108mp->mnt_minor = minor(st.st_dev);109}110111return (ret);112}113114int115getextmntent(const char *path, struct extmnttab *entry, struct stat64 *statbuf)116{117struct stat64 st;118FILE *fp;119int match;120boolean_t have_mnt_id = B_FALSE;121uint64_t target_mnt_id = 0;122uint64_t entry_mnt_id;123#ifdef HAVE_STATX_MNT_ID124struct statx stx;125#endif126127if (strlen(path) >= MAXPATHLEN) {128(void) fprintf(stderr, "invalid object; pathname too long\n");129return (-1);130}131132/*133* Search for the path in /proc/self/mounts. Rather than looking for the134* specific path, which can be fooled by non-standard paths (i.e. ".."135* or "//"), we stat() the path and search for the corresponding136* (major,minor) device pair.137*/138if (stat64(path, statbuf) != 0) {139(void) fprintf(stderr, "cannot open '%s': %s\n",140path, zfs_strerror(errno));141return (-1);142}143144#ifdef HAVE_STATX_MNT_ID145if (statx(AT_FDCWD, path, AT_STATX_SYNC_AS_STAT | AT_SYMLINK_NOFOLLOW,146STATX_MNT_ID, &stx) == 0 && (stx.stx_mask & STATX_MNT_ID)) {147have_mnt_id = B_TRUE;148target_mnt_id = stx.stx_mnt_id;149}150#endif151152if ((fp = fopen(MNTTAB, "re")) == NULL) {153(void) fprintf(stderr, "cannot open %s\n", MNTTAB);154return (-1);155}156157/*158* Search for the given (major,minor) pair in the mount table.159*/160161match = 0;162while (getextmntent_impl(fp, entry, &entry_mnt_id) == 0) {163if (have_mnt_id) {164match = (entry_mnt_id == target_mnt_id);165} else {166match = makedev(entry->mnt_major, entry->mnt_minor) ==167statbuf->st_dev;168}169if (match)170break;171}172(void) fclose(fp);173174if (!match) {175(void) fprintf(stderr, "cannot find mountpoint for '%s'\n",176path);177return (-1);178}179180if (stat64(entry->mnt_mountp, &st) != 0) {181entry->mnt_major = 0;182entry->mnt_minor = 0;183return (-1);184}185186return (0);187}188189190