]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - lib/libc/gen/fstab.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / lib / libc / gen / fstab.c
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)
31 static 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$");
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 <fstab.h>
43 #include <paths.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <vis.h>
49 #include "un-namespace.h"
50
51 static FILE *_fs_fp;
52 static struct fstab _fs_fstab;
53 static int LineNo = 0;
54 static char *path_fstab;
55 static char fstab_path[PATH_MAX];
56 static int fsp_set = 0;
57
58 static void error(int);
59 static void fixfsfile(void);
60 static int fstabscan(void);
61
62 void
63 setfstab(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
78 const char *
79 getfstab(void)
80 {
81
82         if (fsp_set)
83                 return (path_fstab);
84         else
85                 return (_PATH_FSTAB);
86 }
87
88 static void
89 fixfsfile(void)
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
110 static int
111 fstabscan(void)
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 == NULL || *_fs_fstab.fs_spec == '#')
154                         continue;
155                 if (strunvis(_fs_fstab.fs_spec, _fs_fstab.fs_spec) < 0)
156                         goto bad;
157                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
158                         ;
159                 _fs_fstab.fs_file = cp;
160                 if (_fs_fstab.fs_file == NULL)
161                         goto bad;
162                 if (strunvis(_fs_fstab.fs_file, _fs_fstab.fs_file) < 0)
163                         goto bad;
164                 fixfsfile();
165                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
166                         ;
167                 _fs_fstab.fs_vfstype = cp;
168                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
169                         ;
170                 _fs_fstab.fs_mntops = cp;
171                 if (_fs_fstab.fs_mntops == NULL)
172                         goto bad;
173                 _fs_fstab.fs_freq = 0;
174                 _fs_fstab.fs_passno = 0;
175                 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
176                         ;
177                 if (cp != NULL) {
178                         _fs_fstab.fs_freq = atoi(cp);
179                         while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0')
180                                 ;
181                         if (cp != NULL)
182                                 _fs_fstab.fs_passno = atoi(cp);
183                 }
184                 strcpy(subline, _fs_fstab.fs_mntops);
185                 p = subline;
186                 for (typexx = 0, cp = strsep(&p, ","); cp;
187                      cp = strsep(&p, ",")) {
188                         if (strlen(cp) != 2)
189                                 continue;
190                         if (!strcmp(cp, FSTAB_RW)) {
191                                 _fs_fstab.fs_type = FSTAB_RW;
192                                 break;
193                         }
194                         if (!strcmp(cp, FSTAB_RQ)) {
195                                 _fs_fstab.fs_type = FSTAB_RQ;
196                                 break;
197                         }
198                         if (!strcmp(cp, FSTAB_RO)) {
199                                 _fs_fstab.fs_type = FSTAB_RO;
200                                 break;
201                         }
202                         if (!strcmp(cp, FSTAB_SW)) {
203                                 _fs_fstab.fs_type = FSTAB_SW;
204                                 break;
205                         }
206                         if (!strcmp(cp, FSTAB_XX)) {
207                                 _fs_fstab.fs_type = FSTAB_XX;
208                                 typexx++;
209                                 break;
210                         }
211                 }
212                 if (typexx)
213                         continue;
214                 if (cp != NULL)
215                         return (1);
216
217 bad:            /* no way to distinguish between EOF and syntax error */
218                 error(EFTYPE);
219         }
220         /* NOTREACHED */
221 }
222
223 struct fstab *
224 getfsent(void)
225 {
226
227         if ((!_fs_fp && !setfsent()) || !fstabscan())
228                 return (NULL);
229         return (&_fs_fstab);
230 }
231
232 struct fstab *
233 getfsspec(const char *name)
234 {
235
236         if (setfsent())
237                 while (fstabscan())
238                         if (!strcmp(_fs_fstab.fs_spec, name))
239                                 return (&_fs_fstab);
240         return (NULL);
241 }
242
243 struct fstab *
244 getfsfile(const char *name)
245 {
246
247         if (setfsent())
248                 while (fstabscan())
249                         if (!strcmp(_fs_fstab.fs_file, name))
250                                 return (&_fs_fstab);
251         return (NULL);
252 }
253
254 int
255 setfsent(void)
256 {
257         if (_fs_fp) {
258                 rewind(_fs_fp);
259                 LineNo = 0;
260                 return (1);
261         }
262         if (fsp_set == 0) {
263                 if (issetugid())
264                         setfstab(NULL);
265                 else
266                         setfstab(getenv("PATH_FSTAB"));
267         }
268         if ((_fs_fp = fopen(path_fstab, "re")) != NULL) {
269                 LineNo = 0;
270                 return (1);
271         }
272         error(errno);
273         return (0);
274 }
275
276 void
277 endfsent(void)
278 {
279
280         if (_fs_fp) {
281                 (void)fclose(_fs_fp);
282                 _fs_fp = NULL;
283         }
284
285         fsp_set = 0;
286 }
287
288 static void
289 error(int err)
290 {
291         char *p;
292         char num[30];
293
294         (void)_write(STDERR_FILENO, "fstab: ", 7);
295         (void)_write(STDERR_FILENO, path_fstab, strlen(path_fstab));
296         (void)_write(STDERR_FILENO, ":", 1);
297         sprintf(num, "%d: ", LineNo);
298         (void)_write(STDERR_FILENO, num, strlen(num));
299         p = strerror(err);
300         (void)_write(STDERR_FILENO, p, strlen(p));
301         (void)_write(STDERR_FILENO, "\n", 1);
302 }