138494Sobrien/* 2310490Scy * Copyright (c) 1997-2014 Erez Zadok 338494Sobrien * Copyright (c) 1990 Jan-Simon Pendry 438494Sobrien * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 538494Sobrien * Copyright (c) 1990 The Regents of the University of California. 638494Sobrien * All rights reserved. 738494Sobrien * 838494Sobrien * This code is derived from software contributed to Berkeley by 938494Sobrien * Jan-Simon Pendry at Imperial College, London. 1038494Sobrien * 1138494Sobrien * Redistribution and use in source and binary forms, with or without 1238494Sobrien * modification, are permitted provided that the following conditions 1338494Sobrien * are met: 1438494Sobrien * 1. Redistributions of source code must retain the above copyright 1538494Sobrien * notice, this list of conditions and the following disclaimer. 1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1738494Sobrien * notice, this list of conditions and the following disclaimer in the 1838494Sobrien * documentation and/or other materials provided with the distribution. 19310490Scy * 3. Neither the name of the University nor the names of its contributors 2038494Sobrien * may be used to endorse or promote products derived from this software 2138494Sobrien * without specific prior written permission. 2238494Sobrien * 2338494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2438494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2538494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2638494Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2738494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2838494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2938494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3038494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3138494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3238494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3338494Sobrien * SUCH DAMAGE. 3438494Sobrien * 3538494Sobrien * 36174294Sobrien * File: am-utils/conf/mtab/mtab_bsd.c 3738494Sobrien * 3838494Sobrien */ 3938494Sobrien 4038494Sobrien/* 4138494Sobrien * BSD 4.4 systems don't write their mount tables on a file. Instead, they 4238494Sobrien * use a (better) system where the kernel keeps this state, and you access 4338494Sobrien * the mount tables via a known interface. 4438494Sobrien */ 4538494Sobrien 4638494Sobrien#ifdef HAVE_CONFIG_H 4738494Sobrien# include <config.h> 4838494Sobrien#endif /* HAVE_CONFIG_H */ 4938494Sobrien#include <am_defs.h> 5038494Sobrien#include <amu.h> 5138494Sobrien 52310490Scy#if __NetBSD_Version__ > 200030000 53310490Scy#define statfs statvfs 54310490Scy#endif 5538494Sobrien 5638494Sobrienstatic mntent_t * 5738494Sobrienmnt_dup(struct statfs *mp) 5838494Sobrien{ 5938494Sobrien mntent_t *new_mp = ALLOC(mntent_t); 6038494Sobrien char *ty; 6138494Sobrien 62310490Scy new_mp->mnt_fsname = xstrdup(mp->f_mntfromname); 63310490Scy new_mp->mnt_dir = xstrdup(mp->f_mntonname); 6438494Sobrien 65119679Smbr#ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME 6638494Sobrien ty = mp->f_fstypename; 67119679Smbr#else /* not HAVE_STRUCT_STATFS_F_FSTYPENAME */ 6838494Sobrien switch (mp->f_type) { 6938494Sobrien 7038494Sobrien# if defined(MOUNT_UFS) && defined(MNTTAB_TYPE_UFS) 7138494Sobrien case MOUNT_UFS: 7238494Sobrien ty = MNTTAB_TYPE_UFS; 7338494Sobrien break; 7438494Sobrien# endif /* defined(MOUNT_UFS) && defined(MNTTAB_TYPE_UFS) */ 7538494Sobrien 7638494Sobrien# if defined(MOUNT_NFS) && defined(MNTTAB_TYPE_NFS) 7738494Sobrien case MOUNT_NFS: 7838494Sobrien ty = MNTTAB_TYPE_NFS; 7938494Sobrien break; 8038494Sobrien# endif /* defined(MOUNT_NFS) && defined(MNTTAB_TYPE_NFS) */ 8138494Sobrien 8238494Sobrien# if defined(MOUNT_MFS) && defined(MNTTAB_TYPE_MFS) 8338494Sobrien case MOUNT_MFS: 8438494Sobrien ty = MNTTAB_TYPE_MFS; 8538494Sobrien break; 8638494Sobrien# endif /* defined(MOUNT_MFS) && defined(MNTTAB_TYPE_MFS) */ 8738494Sobrien 8838494Sobrien default: 8938494Sobrien ty = "unknown"; 9038494Sobrien 9138494Sobrien break; 9238494Sobrien } 93119679Smbr#endif /* not HAVE_STRUCT_STATFS_F_FSTYPENAME */ 9438494Sobrien 95310490Scy new_mp->mnt_type = xstrdup(ty); 96310490Scy new_mp->mnt_opts = xstrdup("unset"); 9738494Sobrien new_mp->mnt_freq = 0; 9838494Sobrien new_mp->mnt_passno = 0; 9938494Sobrien 10038494Sobrien return new_mp; 10138494Sobrien} 10238494Sobrien 10338494Sobrien 10438494Sobrien/* 10538494Sobrien * Read a mount table into memory 10638494Sobrien */ 10738494Sobrienmntlist * 10838494Sobrienread_mtab(char *fs, const char *mnttabname) 10938494Sobrien{ 11038494Sobrien mntlist **mpp, *mhp; 11138494Sobrien struct statfs *mntbufp, *mntp; 11238494Sobrien 11338494Sobrien int nloc = getmntinfo(&mntbufp, MNT_NOWAIT); 11438494Sobrien 11538494Sobrien if (nloc == 0) { 11638494Sobrien plog(XLOG_ERROR, "Can't read mount table"); 11738494Sobrien return 0; 11838494Sobrien } 11938494Sobrien mpp = &mhp; 12038494Sobrien for (mntp = mntbufp; mntp < mntbufp + nloc; mntp++) { 12138494Sobrien /* 12238494Sobrien * Allocate a new slot 12338494Sobrien */ 12438494Sobrien *mpp = ALLOC(struct mntlist); 12538494Sobrien 12638494Sobrien /* 12738494Sobrien * Copy the data returned by getmntent 12838494Sobrien */ 12938494Sobrien (*mpp)->mnt = mnt_dup(mntp); 13038494Sobrien 13138494Sobrien /* 13238494Sobrien * Move to next pointer 13338494Sobrien */ 13438494Sobrien mpp = &(*mpp)->mnext; 13538494Sobrien } 13638494Sobrien 13738494Sobrien /* 13838494Sobrien * Terminate the list 13938494Sobrien */ 140310490Scy *mpp = NULL; 14138494Sobrien 14238494Sobrien return mhp; 14338494Sobrien} 144