fstab.c revision 237061
1/* 2 * Copyright (c) 1980, 1988, 1993 3 * The Regents of the University of California. 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 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#if defined(LIBC_SCCS) && !defined(lint) 31static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93"; 32#endif /* LIBC_SCCS and not lint */ 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/lib/libc/gen/fstab.c 237061 2012-06-14 12:28:43Z kib $"); 35 36#include "namespace.h" 37#include <sys/param.h> 38#include <sys/mount.h> 39#include <sys/stat.h> 40 41#include <errno.h> 42#include <fcntl.h> 43#include <fstab.h> 44#include <paths.h> 45#include <stdio.h> 46#include <stdlib.h> 47#include <string.h> 48#include <unistd.h> 49#include "un-namespace.h" 50 51static FILE *_fs_fp; 52static struct fstab _fs_fstab; 53static int LineNo = 0; 54static char *path_fstab; 55static char fstab_path[PATH_MAX]; 56static int fsp_set = 0; 57 58static void error(int); 59static void fixfsfile(void); 60static int fstabscan(void); 61 62void 63setfstab(const char *file) 64{ 65 66 if (file == NULL) { 67 path_fstab = _PATH_FSTAB; 68 } else { 69 strncpy(fstab_path, file, PATH_MAX); 70 fstab_path[PATH_MAX - 1] = '\0'; 71 path_fstab = fstab_path; 72 } 73 fsp_set = 1; 74 75 return; 76} 77 78const char * 79getfstab (void) 80{ 81 82 if (fsp_set) 83 return (path_fstab); 84 else 85 return (_PATH_FSTAB); 86} 87 88static void 89fixfsfile() 90{ 91 static char buf[sizeof(_PATH_DEV) + MNAMELEN]; 92 struct stat sb; 93 struct statfs sf; 94 95 if (_fs_fstab.fs_file != NULL && strcmp(_fs_fstab.fs_file, "/") != 0) 96 return; 97 if (statfs("/", &sf) != 0) 98 return; 99 if (sf.f_mntfromname[0] == '/') 100 buf[0] = '\0'; 101 else 102 strcpy(buf, _PATH_DEV); 103 strcat(buf, sf.f_mntfromname); 104 if (stat(buf, &sb) != 0 || 105 (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode))) 106 return; 107 _fs_fstab.fs_spec = buf; 108} 109 110static int 111fstabscan() 112{ 113 char *cp, *p; 114#define MAXLINELENGTH 1024 115 static char line[MAXLINELENGTH]; 116 char subline[MAXLINELENGTH]; 117 int typexx; 118 119 for (;;) { 120 121 if (!(p = fgets(line, sizeof(line), _fs_fp))) 122 return(0); 123/* OLD_STYLE_FSTAB */ 124 ++LineNo; 125 if (*line == '#' || *line == '\n') 126 continue; 127 if (!strpbrk(p, " \t")) { 128 _fs_fstab.fs_spec = strsep(&p, ":\n"); 129 _fs_fstab.fs_file = strsep(&p, ":\n"); 130 fixfsfile(); 131 _fs_fstab.fs_type = strsep(&p, ":\n"); 132 if (_fs_fstab.fs_type) { 133 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX)) 134 continue; 135 _fs_fstab.fs_mntops = _fs_fstab.fs_type; 136 _fs_fstab.fs_vfstype = 137 strcmp(_fs_fstab.fs_type, FSTAB_SW) ? 138 "ufs" : "swap"; 139 if ((cp = strsep(&p, ":\n")) != NULL) { 140 _fs_fstab.fs_freq = atoi(cp); 141 if ((cp = strsep(&p, ":\n")) != NULL) { 142 _fs_fstab.fs_passno = atoi(cp); 143 return(1); 144 } 145 } 146 } 147 goto bad; 148 } 149/* OLD_STYLE_FSTAB */ 150 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 151 ; 152 _fs_fstab.fs_spec = cp; 153 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') 154 continue; 155 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 156 ; 157 _fs_fstab.fs_file = cp; 158 fixfsfile(); 159 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 160 ; 161 _fs_fstab.fs_vfstype = cp; 162 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 163 ; 164 _fs_fstab.fs_mntops = cp; 165 if (_fs_fstab.fs_mntops == NULL) 166 goto bad; 167 _fs_fstab.fs_freq = 0; 168 _fs_fstab.fs_passno = 0; 169 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 170 ; 171 if (cp != NULL) { 172 _fs_fstab.fs_freq = atoi(cp); 173 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 174 ; 175 if (cp != NULL) 176 _fs_fstab.fs_passno = atoi(cp); 177 } 178 strcpy(subline, _fs_fstab.fs_mntops); 179 p = subline; 180 for (typexx = 0, cp = strsep(&p, ","); cp; 181 cp = strsep(&p, ",")) { 182 if (strlen(cp) != 2) 183 continue; 184 if (!strcmp(cp, FSTAB_RW)) { 185 _fs_fstab.fs_type = FSTAB_RW; 186 break; 187 } 188 if (!strcmp(cp, FSTAB_RQ)) { 189 _fs_fstab.fs_type = FSTAB_RQ; 190 break; 191 } 192 if (!strcmp(cp, FSTAB_RO)) { 193 _fs_fstab.fs_type = FSTAB_RO; 194 break; 195 } 196 if (!strcmp(cp, FSTAB_SW)) { 197 _fs_fstab.fs_type = FSTAB_SW; 198 break; 199 } 200 if (!strcmp(cp, FSTAB_XX)) { 201 _fs_fstab.fs_type = FSTAB_XX; 202 typexx++; 203 break; 204 } 205 } 206 if (typexx) 207 continue; 208 if (cp != NULL) 209 return(1); 210 211bad: /* no way to distinguish between EOF and syntax error */ 212 error(EFTYPE); 213 } 214 /* NOTREACHED */ 215} 216 217struct fstab * 218getfsent() 219{ 220 if ((!_fs_fp && !setfsent()) || !fstabscan()) 221 return((struct fstab *)NULL); 222 return(&_fs_fstab); 223} 224 225struct fstab * 226getfsspec(name) 227 const char *name; 228{ 229 if (setfsent()) 230 while (fstabscan()) 231 if (!strcmp(_fs_fstab.fs_spec, name)) 232 return(&_fs_fstab); 233 return((struct fstab *)NULL); 234} 235 236struct fstab * 237getfsfile(name) 238 const char *name; 239{ 240 if (setfsent()) 241 while (fstabscan()) 242 if (!strcmp(_fs_fstab.fs_file, name)) 243 return(&_fs_fstab); 244 return((struct fstab *)NULL); 245} 246 247int 248setfsent() 249{ 250 int fd; 251 252 if (_fs_fp) { 253 rewind(_fs_fp); 254 LineNo = 0; 255 return(1); 256 } 257 if (fsp_set == 0) { 258 if (issetugid()) 259 setfstab(NULL); 260 else 261 setfstab(getenv("PATH_FSTAB")); 262 } 263 fd = _open(path_fstab, O_RDONLY | O_CLOEXEC); 264 if (fd == -1) { 265 error(errno); 266 return (0); 267 } 268 _fs_fp = fdopen(fd, "r"); 269 if (_fs_fp != NULL) { 270 LineNo = 0; 271 return(1); 272 } 273 error(errno); 274 _close(fd); 275 return(0); 276} 277 278void 279endfsent() 280{ 281 if (_fs_fp) { 282 (void)fclose(_fs_fp); 283 _fs_fp = NULL; 284 } 285 286 fsp_set = 0; 287} 288 289static void 290error(err) 291 int err; 292{ 293 char *p; 294 char num[30]; 295 296 (void)_write(STDERR_FILENO, "fstab: ", 7); 297 (void)_write(STDERR_FILENO, path_fstab, strlen(path_fstab)); 298 (void)_write(STDERR_FILENO, ":", 1); 299 sprintf(num, "%d: ", LineNo); 300 (void)_write(STDERR_FILENO, num, strlen(num)); 301 p = strerror(err); 302 (void)_write(STDERR_FILENO, p, strlen(p)); 303 (void)_write(STDERR_FILENO, "\n", 1); 304} 305