1 /* 2 * Copyright (c) 1989, 1990 Jan-Simon Pendry 3 * Copyright (c) 1989, 1990 Imperial College of Science, Technology & Medicine 4 * Copyright (c) 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Jan-Simon Pendry at Imperial College, London. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * from: @(#)mtab.c 8.1 (Berkeley) 6/6/93 35 * $Id: mtab.c,v 1.7 2014/10/20 06:55:59 guenther Exp $ 36 */ 37 38 #include "am.h" 39 40 /* 41 * Firewall /etc/mtab entries 42 */ 43 void 44 mnt_free(struct mntent *mp) 45 { 46 free(mp->mnt_fsname); 47 free(mp->mnt_dir); 48 free(mp->mnt_type); 49 free(mp->mnt_opts); 50 free(mp); 51 } 52 53 /* 54 * Discard memory allocated for mount list 55 */ 56 void 57 discard_mntlist(mntlist *mp) 58 { 59 mntlist *mp2; 60 61 while ((mp2 = mp)) { 62 mp = mp->mnext; 63 if (mp2->mnt) 64 mnt_free(mp2->mnt); 65 free(mp2); 66 } 67 } 68 69 /* 70 * Throw away a mount list 71 */ 72 void 73 free_mntlist(mntlist *mp) 74 { 75 discard_mntlist(mp); 76 } 77 78 /* 79 * Utility routine which determines the value of a 80 * numeric option in the mount options (such as port=%d). 81 * Returns 0 if the option is not specified. 82 */ 83 int 84 hasmntval(struct mntent *mnt, char *opt) 85 { 86 char *str = hasmntopt(mnt, opt); 87 if (str) { 88 char *eq = strchr(str, '='); 89 if (eq) 90 return atoi(eq+1); 91 else 92 plog(XLOG_USER, "bad numeric option \"%s\" in \"%s\"", opt, str); 93 } 94 95 return 0; 96 } 97 98 static struct mntent * 99 mnt_dup(struct statfs *mp) 100 { 101 struct mntent *new_mp = ALLOC(mntent); 102 103 new_mp->mnt_fsname = strdup(mp->f_mntfromname); 104 new_mp->mnt_dir = strdup(mp->f_mntonname); 105 new_mp->mnt_type = strdup(mp->f_fstypename); 106 new_mp->mnt_opts = strdup("unset"); 107 new_mp->mnt_freq = 0; 108 new_mp->mnt_passno = 0; 109 110 return new_mp; 111 } 112 113 /* 114 * Read a mount table into memory 115 */ 116 mntlist * 117 read_mtab(char *fs) 118 { 119 mntlist **mpp, *mhp; 120 struct statfs *mntbufp, *mntp; 121 122 int nloc = getmntinfo(&mntbufp, MNT_NOWAIT); 123 124 if (nloc == 0) { 125 plog(XLOG_ERROR, "Can't read mount table"); 126 return 0; 127 } 128 129 mpp = &mhp; 130 for (mntp = mntbufp; mntp < mntbufp + nloc; mntp++) { 131 /* 132 * Allocate a new slot 133 */ 134 *mpp = ALLOC(mntlist); 135 136 /* 137 * Copy the data returned by getmntent 138 */ 139 (*mpp)->mnt = mnt_dup(mntp); 140 141 /* 142 * Move to next pointer 143 */ 144 mpp = &(*mpp)->mnext; 145 } 146 147 /* 148 * Terminate the list 149 */ 150 *mpp = 0; 151 152 return mhp; 153 } 154