mnttab.c (185029) | mnttab.c (209962) |
---|---|
1/*- 2 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 16 unchanged lines hidden (view full) --- 25 */ 26 27/* 28 * This file implements Solaris compatible getmntany() and hasmntopt() 29 * functions. 30 */ 31 32#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 16 unchanged lines hidden (view full) --- 25 */ 26 27/* 28 * This file implements Solaris compatible getmntany() and hasmntopt() 29 * functions. 30 */ 31 32#include <sys/cdefs.h> |
33__FBSDID("$FreeBSD: head/cddl/compat/opensolaris/misc/mnttab.c 185029 2008-11-17 20:49:29Z pjd $"); | 33__FBSDID("$FreeBSD: head/cddl/compat/opensolaris/misc/mnttab.c 209962 2010-07-12 23:49:04Z mm $"); |
34 35#include <sys/param.h> 36#include <sys/mount.h> 37#include <sys/mntent.h> 38#include <sys/mnttab.h> | 34 35#include <sys/param.h> 36#include <sys/mount.h> 37#include <sys/mntent.h> 38#include <sys/mnttab.h> |
39 40#include <ctype.h> 41#include <errno.h> |
|
39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42 43static char * 44mntopt(char **p) 45{ 46 char *cp = *p; --- 36 unchanged lines hidden (view full) --- 83optadd(char *mntopts, size_t size, const char *opt) 84{ 85 86 if (mntopts[0] != '\0') 87 strlcat(mntopts, ",", size); 88 strlcat(mntopts, opt, size); 89} 90 | 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45 46static char * 47mntopt(char **p) 48{ 49 char *cp = *p; --- 36 unchanged lines hidden (view full) --- 86optadd(char *mntopts, size_t size, const char *opt) 87{ 88 89 if (mntopts[0] != '\0') 90 strlcat(mntopts, ",", size); 91 strlcat(mntopts, opt, size); 92} 93 |
91int 92getmntany(FILE *fd __unused, struct mnttab *mgetp, struct mnttab *mrefp) | 94void 95statfs2mnttab(struct statfs *sfs, struct mnttab *mp) |
93{ | 96{ |
94 static struct statfs *sfs = NULL; | |
95 static char mntopts[MNTMAXSTR]; | 97 static char mntopts[MNTMAXSTR]; |
96 struct opt *o; 97 long i, n, flags; | 98 long flags; |
98 | 99 |
99 if (sfs != NULL) { 100 free(sfs); 101 sfs = NULL; 102 } | |
103 mntopts[0] = '\0'; 104 | 100 mntopts[0] = '\0'; 101 |
105 n = getfsstat(NULL, 0, MNT_NOWAIT); 106 if (n == -1) 107 return (-1); 108 n = sizeof(*sfs) * (n + 8); 109 sfs = malloc(n); 110 if (sfs == NULL) 111 return (-1); 112 n = getfsstat(sfs, n, MNT_WAIT); 113 if (n == -1) { 114 free(sfs); 115 sfs = NULL; 116 return (-1); | 102 flags = sfs->f_flags; 103#define OPTADD(opt) optadd(mntopts, sizeof(mntopts), (opt)) 104 if (flags & MNT_RDONLY) 105 OPTADD(MNTOPT_RO); 106 else 107 OPTADD(MNTOPT_RW); 108 if (flags & MNT_NOSUID) 109 OPTADD(MNTOPT_NOSUID); 110 else 111 OPTADD(MNTOPT_SETUID); 112 if (flags & MNT_UPDATE) 113 OPTADD(MNTOPT_REMOUNT); 114 if (flags & MNT_NOATIME) 115 OPTADD(MNTOPT_NOATIME); 116 else 117 OPTADD(MNTOPT_ATIME); 118 OPTADD(MNTOPT_NOXATTR); 119 if (flags & MNT_NOEXEC) 120 OPTADD(MNTOPT_NOEXEC); 121 else 122 OPTADD(MNTOPT_EXEC); 123#undef OPTADD 124 mp->mnt_special = sfs->f_mntfromname; 125 mp->mnt_mountp = sfs->f_mntonname; 126 mp->mnt_fstype = sfs->f_fstypename; 127 mp->mnt_mntopts = mntopts; 128} 129 130static struct statfs *gsfs = NULL; 131static int allfs = 0; 132 133static int 134statfs_init(void) 135{ 136 struct statfs *sfs; 137 int error; 138 139 if (gsfs != NULL) { 140 free(gsfs); 141 gsfs = NULL; |
117 } | 142 } |
118 for (i = 0; i < n; i++) { | 143 allfs = getfsstat(NULL, 0, MNT_WAIT); 144 if (allfs == -1) 145 goto fail; 146 gsfs = malloc(sizeof(gsfs[0]) * allfs * 2); 147 if (gsfs == NULL) 148 goto fail; 149 allfs = getfsstat(gsfs, (long)(sizeof(gsfs[0]) * allfs * 2), 150 MNT_WAIT); 151 if (allfs == -1) 152 goto fail; 153 sfs = realloc(gsfs, allfs * sizeof(gsfs[0])); 154 if (sfs != NULL) 155 gsfs = sfs; 156 return (0); 157fail: 158 error = errno; 159 if (gsfs != NULL) 160 free(gsfs); 161 gsfs = NULL; 162 allfs = 0; 163 return (error); 164} 165 166int 167getmntany(FILE *fd __unused, struct mnttab *mgetp, struct mnttab *mrefp) 168{ 169 struct statfs *sfs; 170 int i, error; 171 172 error = statfs_init(); 173 if (error != 0) 174 return (error); 175 176 for (i = 0; i < allfs; i++) { |
119 if (mrefp->mnt_special != NULL && | 177 if (mrefp->mnt_special != NULL && |
120 strcmp(mrefp->mnt_special, sfs[i].f_mntfromname) != 0) { | 178 strcmp(mrefp->mnt_special, gsfs[i].f_mntfromname) != 0) { |
121 continue; 122 } 123 if (mrefp->mnt_mountp != NULL && | 179 continue; 180 } 181 if (mrefp->mnt_mountp != NULL && |
124 strcmp(mrefp->mnt_mountp, sfs[i].f_mntonname) != 0) { | 182 strcmp(mrefp->mnt_mountp, gsfs[i].f_mntonname) != 0) { |
125 continue; 126 } 127 if (mrefp->mnt_fstype != NULL && | 183 continue; 184 } 185 if (mrefp->mnt_fstype != NULL && |
128 strcmp(mrefp->mnt_fstype, sfs[i].f_fstypename) != 0) { | 186 strcmp(mrefp->mnt_fstype, gsfs[i].f_fstypename) != 0) { |
129 continue; 130 } | 187 continue; 188 } |
131 flags = sfs[i].f_flags; 132#define OPTADD(opt) optadd(mntopts, sizeof(mntopts), (opt)) 133 if (flags & MNT_RDONLY) 134 OPTADD(MNTOPT_RO); 135 else 136 OPTADD(MNTOPT_RW); 137 if (flags & MNT_NOSUID) 138 OPTADD(MNTOPT_NOSUID); 139 else 140 OPTADD(MNTOPT_SETUID); 141 if (flags & MNT_UPDATE) 142 OPTADD(MNTOPT_REMOUNT); 143 if (flags & MNT_NOATIME) 144 OPTADD(MNTOPT_NOATIME); 145 else 146 OPTADD(MNTOPT_ATIME); 147 OPTADD(MNTOPT_NOXATTR); 148 if (flags & MNT_NOEXEC) 149 OPTADD(MNTOPT_NOEXEC); 150 else 151 OPTADD(MNTOPT_EXEC); 152#undef OPTADD 153 mgetp->mnt_special = sfs[i].f_mntfromname; 154 mgetp->mnt_mountp = sfs[i].f_mntonname; 155 mgetp->mnt_fstype = sfs[i].f_fstypename; 156 mgetp->mnt_mntopts = mntopts; | 189 statfs2mnttab(&gsfs[i], mgetp); |
157 return (0); 158 } | 190 return (0); 191 } |
159 free(sfs); 160 sfs = NULL; | |
161 return (-1); 162} | 192 return (-1); 193} |
194 195int 196getmntent(FILE *fp, struct mnttab *mp) 197{ 198 struct statfs *sfs; 199 int error, nfs; 200 201 nfs = (int)lseek(fileno(fp), 0, SEEK_CUR); 202 if (nfs == -1) 203 return (errno); 204 /* If nfs is 0, we want to refresh out cache. */ 205 if (nfs == 0 || gsfs == NULL) { 206 error = statfs_init(); 207 if (error != 0) 208 return (error); 209 } 210 if (nfs >= allfs) 211 return (-1); 212 statfs2mnttab(&gsfs[nfs], mp); 213 if (lseek(fileno(fp), 1, SEEK_CUR) == -1) 214 return (errno); 215 return (0); 216} |
|