]> git.ipfire.org Git - thirdparty/glibc.git/blame - misc/mntent_r.c
Use autofs "ignore" mount hint in getmntent_r/getmntent
[thirdparty/glibc.git] / misc / mntent_r.c
CommitLineData
845dcb57 1/* Utilities for reading/writing fstab, mtab, etc.
04277e02 2 Copyright (C) 1995-2019 Free Software Foundation, Inc.
df4ef2ab 3 This file is part of the GNU C Library.
845dcb57 4
df4ef2ab 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
845dcb57 9
df4ef2ab
UD
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
845dcb57 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
845dcb57 18
53308042 19#include <alloca.h>
845dcb57 20#include <mntent.h>
08b7e998 21#include <stdbool.h>
845dcb57 22#include <stdio.h>
2706ee38 23#include <stdio_ext.h>
845dcb57
UD
24#include <string.h>
25#include <sys/types.h>
26
3ce1f295
UD
27#define flockfile(s) _IO_flockfile (s)
28#define funlockfile(s) _IO_funlockfile (s)
50304ef0 29
6b87a564
UD
30#undef __setmntent
31#undef __endmntent
32#undef __getmntent_r
33
845dcb57
UD
34/* Prepare to begin reading and/or writing mount table entries from the
35 beginning of FILE. MODE is as for `fopen'. */
36FILE *
37__setmntent (const char *file, const char *mode)
38{
ee8449f7 39 /* Extend the mode parameter with "c" to disable cancellation in the
312be3f9 40 I/O functions and "e" to set FD_CLOEXEC. */
ee8449f7 41 size_t modelen = strlen (mode);
312be3f9
UD
42 char newmode[modelen + 3];
43 memcpy (mempcpy (newmode, mode, modelen), "ce", 3);
ee8449f7 44 FILE *result = fopen (file, newmode);
2706ee38
UD
45
46 if (result != NULL)
47 /* We do the locking ourselves. */
b4c4f776 48 __fsetlocking (result, FSETLOCKING_BYCALLER);
2706ee38
UD
49
50 return result;
845dcb57 51}
30917259 52libc_hidden_def (__setmntent)
845dcb57
UD
53weak_alias (__setmntent, setmntent)
54
55
56/* Close a stream opened with `setmntent'. */
57int
58__endmntent (FILE *stream)
59{
60 if (stream) /* SunOS 4.x allows for NULL stream */
61 fclose (stream);
62 return 1; /* SunOS 4.x says to always return 1 */
63}
30917259 64libc_hidden_def (__endmntent)
845dcb57
UD
65weak_alias (__endmntent, endmntent)
66
67
b0b422e8
UD
68/* Since the values in a line are separated by spaces, a name cannot
69 contain a space. Therefore some programs encode spaces in names
70 by the strings "\040". We undo the encoding when reading an entry.
71 The decoding happens in place. */
72static char *
73decode_name (char *buf)
74{
75 char *rp = buf;
76 char *wp = buf;
77
78 do
79 if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
80 {
81 /* \040 is a SPACE. */
82 *wp++ = ' ';
83 rp += 3;
84 }
37369d1c 85 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1')
b0b422e8 86 {
e796f92f 87 /* \011 is a TAB. */
b0b422e8
UD
88 *wp++ = '\t';
89 rp += 3;
90 }
37369d1c
UD
91 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
92 {
93 /* \012 is a NEWLINE. */
94 *wp++ = '\n';
95 rp += 3;
96 }
b0b422e8
UD
97 else if (rp[0] == '\\' && rp[1] == '\\')
98 {
99 /* We have to escape \\ to be able to represent all characters. */
100 *wp++ = '\\';
101 rp += 1;
102 }
21800751
UD
103 else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4')
104 {
105 /* \134 is also \\. */
106 *wp++ = '\\';
107 rp += 3;
108 }
b0b422e8
UD
109 else
110 *wp++ = *rp;
111 while (*rp++ != '\0');
112
113 return buf;
114}
115
08b7e998
IK
116static bool
117get_mnt_entry (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
845dcb57 118{
b0b422e8 119 char *cp;
845dcb57
UD
120 char *head;
121
122 do
123 {
124 char *end_ptr;
125
84e5e756 126 if (__fgets_unlocked (buffer, bufsiz, stream) == NULL)
08b7e998 127 return false;
845dcb57
UD
128
129 end_ptr = strchr (buffer, '\n');
130 if (end_ptr != NULL) /* chop newline */
fb87ee96 131 {
b0e805fa
MF
132 /* Do not walk past the start of buffer if it's all whitespace. */
133 while (end_ptr != buffer
134 && (end_ptr[-1] == ' ' || end_ptr[-1] == '\t'))
fb87ee96
VN
135 end_ptr--;
136 *end_ptr = '\0';
137 }
845dcb57
UD
138 else
139 {
140 /* Not the whole line was read. Do it now but forget it. */
141 char tmp[1024];
84e5e756 142 while (__fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
845dcb57
UD
143 if (strchr (tmp, '\n') != NULL)
144 break;
145 }
146
147 head = buffer + strspn (buffer, " \t");
148 /* skip empty lines and comment lines: */
b0b422e8
UD
149 }
150 while (head[0] == '\0' || head[0] == '#');
845dcb57 151
b0b422e8
UD
152 cp = __strsep (&head, " \t");
153 mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
845dcb57
UD
154 if (head)
155 head += strspn (head, " \t");
b0b422e8
UD
156 cp = __strsep (&head, " \t");
157 mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
845dcb57
UD
158 if (head)
159 head += strspn (head, " \t");
b0b422e8
UD
160 cp = __strsep (&head, " \t");
161 mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
845dcb57
UD
162 if (head)
163 head += strspn (head, " \t");
b0b422e8
UD
164 cp = __strsep (&head, " \t");
165 mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
845dcb57
UD
166 switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
167 {
168 case 0:
169 mp->mnt_freq = 0;
32db86d5 170 /* Fall through. */
845dcb57
UD
171 case 1:
172 mp->mnt_passno = 0;
32db86d5 173 /* Fall through. */
845dcb57 174 case 2:
49f3a758 175 break;
845dcb57 176 }
08b7e998
IK
177
178 return true;
179}
180
181/* Read one mount table entry from STREAM. Returns a pointer to storage
182 reused on the next call, or null for EOF or error (use feof/ferror to
183 check). */
184struct mntent *
185__getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
186{
187 struct mntent *result;
188
189 flockfile (stream);
190 while (true)
191 if (get_mnt_entry (stream, mp, buffer, bufsiz))
192 {
193 /* If the file system is autofs look for a mount option hint
194 ("ignore") to skip the entry. */
195 if (strcmp (mp->mnt_type, "autofs") == 0 && __hasmntopt (mp, "ignore"))
196 memset (mp, 0, sizeof (*mp));
197 else
198 {
199 result = mp;
200 break;
201 }
202 }
203 else
204 {
205 result = NULL;
206 break;
207 }
71412a8c 208 funlockfile (stream);
845dcb57 209
08b7e998 210 return result;
845dcb57 211}
30917259 212libc_hidden_def (__getmntent_r)
845dcb57
UD
213weak_alias (__getmntent_r, getmntent_r)
214
b0b422e8
UD
215
216/* We have to use an encoding for names if they contain spaces or tabs.
217 To be able to represent all characters we also have to escape the
218 backslash itself. This "function" must be a macro since we use
219 `alloca'. */
220#define encode_name(name) \
221 do { \
222 const char *rp = name; \
223 \
224 while (*rp != '\0') \
ab00f4ea 225 if (*rp == ' ' || *rp == '\t' || *rp == '\n' || *rp == '\\') \
b0b422e8 226 break; \
63f7cb44
UD
227 else \
228 ++rp; \
b0b422e8
UD
229 \
230 if (*rp != '\0') \
231 { \
232 /* In the worst case the length of the string can increase to \
ab00f4ea 233 four times the current length. */ \
63f7cb44 234 char *wp; \
b0b422e8
UD
235 \
236 rp = name; \
63f7cb44
UD
237 name = wp = (char *) alloca (strlen (name) * 4 + 1); \
238 \
b0b422e8
UD
239 do \
240 if (*rp == ' ') \
241 { \
242 *wp++ = '\\'; \
243 *wp++ = '0'; \
244 *wp++ = '4'; \
245 *wp++ = '0'; \
246 } \
247 else if (*rp == '\t') \
e796f92f
UD
248 { \
249 *wp++ = '\\'; \
250 *wp++ = '0'; \
251 *wp++ = '1'; \
252 *wp++ = '1'; \
253 } \
254 else if (*rp == '\n') \
b0b422e8
UD
255 { \
256 *wp++ = '\\'; \
257 *wp++ = '0'; \
258 *wp++ = '1'; \
259 *wp++ = '2'; \
260 } \
261 else if (*rp == '\\') \
262 { \
263 *wp++ = '\\'; \
264 *wp++ = '\\'; \
265 } \
266 else \
267 *wp++ = *rp; \
268 while (*rp++ != '\0'); \
b0b422e8
UD
269 } \
270 } while (0)
271
272
845dcb57
UD
273/* Write the mount table entry described by MNT to STREAM.
274 Return zero on success, nonzero on failure. */
275int
276__addmntent (FILE *stream, const struct mntent *mnt)
277{
b0b422e8 278 struct mntent mntcopy = *mnt;
845dcb57
UD
279 if (fseek (stream, 0, SEEK_END))
280 return 1;
281
b0b422e8
UD
282 /* Encode spaces and tabs in the names. */
283 encode_name (mntcopy.mnt_fsname);
284 encode_name (mntcopy.mnt_dir);
285 encode_name (mntcopy.mnt_type);
286 encode_name (mntcopy.mnt_opts);
287
845dcb57 288 return (fprintf (stream, "%s %s %s %s %d %d\n",
b0b422e8
UD
289 mntcopy.mnt_fsname,
290 mntcopy.mnt_dir,
291 mntcopy.mnt_type,
292 mntcopy.mnt_opts,
293 mntcopy.mnt_freq,
e1fb097f
UD
294 mntcopy.mnt_passno) < 0
295 || fflush (stream) != 0);
845dcb57
UD
296}
297weak_alias (__addmntent, addmntent)
298
299
300/* Search MNT->mnt_opts for an option matching OPT.
301 Returns the address of the substring, or null if none found. */
302char *
303__hasmntopt (const struct mntent *mnt, const char *opt)
304{
305 const size_t optlen = strlen (opt);
306 char *rest = mnt->mnt_opts, *p;
307
308 while ((p = strstr (rest, opt)) != NULL)
309 {
66f17705
UD
310 if ((p == rest || p[-1] == ',')
311 && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
845dcb57
UD
312 return p;
313
66f17705 314 rest = strchr (p, ',');
df4ef2ab
UD
315 if (rest == NULL)
316 break;
317 ++rest;
845dcb57
UD
318 }
319
320 return NULL;
321}
d1903329 322libc_hidden_def (__hasmntopt)
845dcb57 323weak_alias (__hasmntopt, hasmntopt)