Path: blob/main/sys/contrib/openzfs/lib/libspl/os/linux/mnttab.c
178701 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];4647int48_sol_getmntent(FILE *fp, struct mnttab *mgetp)49{50struct mntent mntbuf;51struct mntent *ret;5253ret = getmntent_r(fp, &mntbuf, buf, BUFSIZE);5455if (ret != NULL) {56mgetp->mnt_special = mntbuf.mnt_fsname;57mgetp->mnt_mountp = mntbuf.mnt_dir;58mgetp->mnt_fstype = mntbuf.mnt_type;59mgetp->mnt_mntopts = mntbuf.mnt_opts;60return (0);61}6263if (feof(fp))64return (-1);6566return (MNT_TOOLONG);67}6869static int70getextmntent_impl(FILE *fp, struct mnttab *mp, uint64_t *mnt_id, dev_t *dev)71{72int ret;73struct stat64 st;7475*mnt_id = 0;76ret = _sol_getmntent(fp, (struct mnttab *)mp);77if (ret == 0) {78#ifdef HAVE_STATX_MNT_ID79struct statx stx;80if (statx(AT_FDCWD, mp->mnt_mountp,81AT_STATX_SYNC_AS_STAT | AT_SYMLINK_NOFOLLOW,82STATX_MNT_ID, &stx) == 0 && (stx.stx_mask & STATX_MNT_ID))83*mnt_id = stx.stx_mnt_id;84#endif85if (stat64(mp->mnt_mountp, &st) != 0) {86*dev = 0;87return (ret);88}89*dev = st.st_dev;90}9192return (ret);93}9495int96getextmntent(const char *path, struct mnttab *entry, struct stat64 *statbuf)97{98struct stat64 st;99FILE *fp;100int match;101boolean_t have_mnt_id = B_FALSE;102uint64_t target_mnt_id = 0;103uint64_t entry_mnt_id;104dev_t dev;105#ifdef HAVE_STATX_MNT_ID106struct statx stx;107#endif108109if (strlen(path) >= MAXPATHLEN) {110(void) fprintf(stderr, "invalid object; pathname too long\n");111return (-1);112}113114/*115* Search for the path in /proc/self/mounts. Rather than looking for the116* specific path, which can be fooled by non-standard paths (i.e. ".."117* or "//"), we stat() the path and search for the corresponding118* (major,minor) device pair.119*/120if (stat64(path, statbuf) != 0) {121(void) fprintf(stderr, "cannot open '%s': %s\n",122path, zfs_strerror(errno));123return (-1);124}125126#ifdef HAVE_STATX_MNT_ID127if (statx(AT_FDCWD, path, AT_STATX_SYNC_AS_STAT | AT_SYMLINK_NOFOLLOW,128STATX_MNT_ID, &stx) == 0 && (stx.stx_mask & STATX_MNT_ID)) {129have_mnt_id = B_TRUE;130target_mnt_id = stx.stx_mnt_id;131}132#endif133134if ((fp = fopen(MNTTAB, "re")) == NULL) {135(void) fprintf(stderr, "cannot open %s\n", MNTTAB);136return (-1);137}138139/*140* Search for the given (major,minor) pair in the mount table.141*/142143match = 0;144while (getextmntent_impl(fp, entry, &entry_mnt_id, &dev) == 0) {145if (have_mnt_id) {146match = (entry_mnt_id == target_mnt_id);147} else {148match = (dev == statbuf->st_dev);149}150if (match)151break;152}153(void) fclose(fp);154155if (!match) {156(void) fprintf(stderr, "cannot find mountpoint for '%s'\n",157path);158return (-1);159}160161if (stat64(entry->mnt_mountp, &st) != 0)162return (-1);163164return (0);165}166167168