1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "mntent.h"
#include "fstab.h"
#include "sundries.h" /* for xmalloc() etc */
#define streq(s, t) (strcmp ((s), (t)) == 0)
#define PROC_MOUNTS "/proc/mounts"
/* Information about mtab. ------------------------------------*/
static int have_mtab_info = 0;
static int var_mtab_does_not_exist = 0;
static int var_mtab_is_a_symlink = 0;
static void
get_mtab_info(void) {
struct stat mtab_stat;
if (!have_mtab_info) {
if (lstat(MOUNTED, &mtab_stat))
var_mtab_does_not_exist = 1;
else if (S_ISLNK(mtab_stat.st_mode))
var_mtab_is_a_symlink = 1;
have_mtab_info = 1;
}
}
int
mtab_does_not_exist(void) {
get_mtab_info();
return var_mtab_does_not_exist;
}
int
mtab_is_a_symlink(void) {
get_mtab_info();
return var_mtab_is_a_symlink;
}
int
mtab_is_writable() {
static int ret = -1;
/* Should we write to /etc/mtab upon an update?
Probably not if it is a symlink to /proc/mounts, since that
would create a file /proc/mounts in case the proc filesystem
is not mounted. */
if (mtab_is_a_symlink())
return 0;
if (ret == -1) {
int fd = open(MOUNTED, O_RDWR | O_CREAT, 0644);
if (fd >= 0) {
close(fd);
ret = 1;
} else
ret = 0;
}
return ret;
}
/* Contents of mtab and fstab ---------------------------------*/
struct mntentchn mounttable, fstab;
static int got_mtab = 0;
static int got_fstab = 0;
static void read_mounttable(void), read_fstab(void);
struct mntentchn *
mtab_head() {
if (!got_mtab)
read_mounttable();
return &mounttable;
}
struct mntentchn *
fstab_head() {
if (!got_fstab)
read_fstab();
return &fstab;
}
static void
read_mntentchn(mntFILE *mfp, const char *fnam, struct mntentchn *mc0) {
struct mntentchn *mc = mc0;
struct mntent *mnt;
while ((mnt = my_getmntent (mfp)) != NULL) {
if (!streq (mnt->mnt_type, MNTTYPE_IGNORE)) {
mc->nxt = (struct mntentchn *) xmalloc(sizeof(*mc));
mc->nxt->prev = mc;
mc = mc->nxt;
mc->mnt_fsname = mnt->mnt_fsname;
mc->mnt_dir = mnt->mnt_dir;
mc->mnt_type = mnt->mnt_type;
mc->mnt_opts = mnt->mnt_opts;
mc->nxt = NULL;
}
}
mc0->prev = mc;
if (ferror (mfp->mntent_fp)) {
error("warning: error reading %s: %s", fnam, strerror (errno));
mc0->nxt = mc0->prev = NULL;
}
my_endmntent(mfp);
}
/*
* Read /etc/mtab. If that fails, try /proc/mounts.
* This produces a linked list. The list head mounttable is a dummy.
* Return 0 on success.
*/
static void
read_mounttable() {
mntFILE *mfp;
const char *fnam;
struct mntentchn *mc = &mounttable;
got_mtab = 1;
mc->nxt = mc->prev = NULL;
fnam = MOUNTED;
mfp = my_setmntent (fnam, "r");
if (mfp == NULL || mfp->mntent_fp == NULL) {
int errsv = errno;
fnam = PROC_MOUNTS;
mfp = my_setmntent (fnam, "r");
if (mfp == NULL || mfp->mntent_fp == NULL) {
error("warning: can't open %s: %s", MOUNTED, strerror (errsv));
return;
}
if (verbose)
printf ("mount: could not open %s - using %s instead\n",
MOUNTED, PROC_MOUNTS);
}
read_mntentchn(mfp, fnam, mc);
}
static void
read_fstab() {
mntFILE *mfp = NULL;
const char *fnam;
struct mntentchn *mc = &fstab;
got_fstab = 1;
mc->nxt = mc->prev = NULL;
fnam = _PATH_FSTAB;
mfp = my_setmntent (fnam, "r");
if (mfp == NULL || mfp->mntent_fp == NULL) {
error("warning: can't open %s: %s", _PATH_FSTAB, strerror (errno));
return;
}
read_mntentchn(mfp, fnam, mc);
}
/* Given the name NAME, try to find it in mtab. */
struct mntentchn *
getmntfile (const char *name) {
struct mntentchn *mc;
for (mc = mtab_head()->nxt; mc; mc = mc->nxt)
if (streq (mc->mnt_dir, name) || (streq (mc->mnt_fsname, name)))
break;
return mc;
}
/*
* Given the name NAME, and the place MCPREV we found it last time,
* try to find more occurrences.
*/
struct mntentchn *
getmntfilesbackward (const char *name, struct mntentchn *mcprev) {
struct mntentchn *mc, *mh;
mh = mtab_head();
if (!mcprev)
mcprev = mh;
for (mc = mcprev->prev; mc && mc != mh; mc = mc->prev)
if (streq (mc->mnt_dir, name) || (streq (mc->mnt_fsname, name)))
return mc;
return NULL;
}
/* Given the name FILE, try to find the option "loop=FILE" in mtab. */
struct mntentchn *
getmntoptfile (const char *file)
{
struct mntentchn *mc;
char *opts, *s;
int l;
if (!file)
return NULL;
l = strlen(file);
for (mc = mtab_head()->nxt; mc; mc = mc->nxt)
if ((opts = mc->mnt_opts) != NULL
&& (s = strstr(opts, "loop="))
&& !strncmp(s+5, file, l)
&& (s == opts || s[-1] == ',')
&& (s[l+5] == 0 || s[l+5] == ','))
return mc;
return NULL;
}
/* Find the dir FILE in fstab. */
struct mntentchn *
getfsfile (const char *file) {
struct mntentchn *mc;
for (mc = fstab_head()->nxt; mc; mc = mc->nxt)
if (streq (mc->mnt_dir, file))
break;
return mc;
}
/* Find the device SPEC in fstab. */
struct mntentchn *
getfsspec (const char *spec)
{
struct mntentchn *mc;
for (mc = fstab_head()->nxt; mc; mc = mc->nxt)
if (streq (mc->mnt_fsname, spec))
break;
return mc;
}
/* Find the uuid UUID in fstab. */
struct mntentchn *
getfsuuidspec (const char *uuid)
{
struct mntentchn *mc;
for (mc = fstab_head()->nxt; mc; mc = mc->nxt)
if (strncmp (mc->mnt_fsname, "UUID=", 5) == 0
&& streq(mc->mnt_fsname + 5, uuid))
break;
return mc;
}
/* Find the label LABEL in fstab. */
struct mntentchn *
getfsvolspec (const char *label)
{
struct mntentchn *mc;
for (mc = fstab_head()->nxt; mc; mc = mc->nxt)
if (strncmp (mc->mnt_fsname, "LABEL=", 6) == 0
&& streq(mc->mnt_fsname + 6, label))
break;
return mc;
}
/* Updating mtab ----------------------------------------------*/
/* Flag for already existing lock file. */
static int we_created_lockfile = 0;
/* Flag to indicate that signals have been set up. */
static int signals_have_been_setup = 0;
/* Ensure that the lock is released if we are interrupted. */
static void
handler (int sig) {
die (EX_USER, "%s", sys_siglist[sig]);
}
static void
setlkw_timeout (int sig) {
/* nothing, fcntl will fail anyway */
}
/* Create the lock file.
The lock file will be removed if we catch a signal or when we exit. */
/* The old code here used flock on a lock file /etc/mtab~ and deleted
this lock file afterwards. However, as rgooch remarks, that has a
race: a second mount may be waiting on the lock and proceed as
soon as the lock file is deleted by the first mount, and immediately
afterwards a third mount comes, creates a new /etc/mtab~, applies
flock to that, and also proceeds, so that the second and third mount
now both are scribbling in /etc/mtab.
The new code uses a link() instead of a creat(), where we proceed
only if it was us that created the lock, and hence we always have
to delete the lock afterwards. Now the use of flock() is in principle
superfluous, but avoids an arbitrary sleep(). */
/* Where does the link point to? Obvious choices are mtab and mtab~~.
Maybe the latter is preferable. */
#define MOUNTLOCK_LINKTARGET MOUNTED_LOCK "~"
void
lock_mtab (void) {
int tries = 3;
if (!signals_have_been_setup) {
int sig = 0;
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sigfillset (&sa.sa_mask);
while (sigismember (&sa.sa_mask, ++sig) != -1
&& sig != SIGCHLD) {
if (sig == SIGALRM)
sa.sa_handler = setlkw_timeout;
else
sa.sa_handler = handler;
sigaction (sig, &sa, (struct sigaction *) 0);
}
signals_have_been_setup = 1;
}
/* Repeat until it was us who made the link */
while (!we_created_lockfile) {
struct flock flock;
int errsv, fd, i, j;
i = open (MOUNTLOCK_LINKTARGET, O_WRONLY|O_CREAT, 0);
if (i < 0) {
/* MOUNTLOCK_LINKTARGET does not exist (as a file)
and we cannot create it. Read-only filesystem?
Too many files open in the system? Filesystem full? */
die (EX_FILEIO, "can't create lock file %s: %s "
"(use -n flag to override)",
MOUNTLOCK_LINKTARGET, strerror (errno));
}
close(i);
j = link(MOUNTLOCK_LINKTARGET, MOUNTED_LOCK);
errsv = errno;
(void) unlink(MOUNTLOCK_LINKTARGET);
if (j < 0 && errsv != EEXIST) {
die (EX_FILEIO, "can't link lock file %s: %s "
"(use -n flag to override)",
MOUNTED_LOCK, strerror (errsv));
}
fd = open (MOUNTED_LOCK, O_WRONLY);
if (fd < 0) {
/* Strange... Maybe the file was just deleted? */
if (errno == ENOENT && tries-- > 0)
continue;
die (EX_FILEIO, "can't open lock file %s: %s "
"(use -n flag to override)",
MOUNTED_LOCK, strerror (errno));
}
flock.l_type = F_WRLCK;
flock.l_whence = SEEK_SET;
flock.l_start = 0;
flock.l_len = 0;
if (j == 0) {
/* We made the link. Now claim the lock. */
if (fcntl (fd, F_SETLK, &flock) == -1) {
if (verbose)
printf("Can't lock lock file %s: %s\n",
MOUNTED_LOCK, strerror (errno));
/* proceed anyway */
}
we_created_lockfile = 1;
} else {
/* Someone else made the link. Wait. */
alarm(LOCK_TIMEOUT);
if (fcntl (fd, F_SETLKW, &flock) == -1) {
die (EX_FILEIO, "can't lock lock file %s: %s",
MOUNTED_LOCK, (errno == EINTR) ?
"timed out" : strerror (errno));
}
alarm(0);
/* Maybe limit the number of iterations? */
}
close(fd);
}
}
/* Remove lock file. */
void
unlock_mtab (void) {
if (we_created_lockfile) {
unlink (MOUNTED_LOCK);
we_created_lockfile = 0;
}
}
/*
* Update the mtab.
* Used by umount with null INSTEAD: remove any DIR entries.
* Used by mount upon a remount: update option part,
* and complain if a wrong device or type was given.
* [Note that often a remount will be a rw remount of /
* where there was no entry before, and we'll have to believe
* the values given in INSTEAD.]
*/
void
update_mtab (const char *dir, struct mntent *instead) {
struct mntent *mnt;
struct mntent *next;
struct mntent remnt;
int added = 0;
mntFILE *mfp, *mftmp;
if (mtab_does_not_exist() || mtab_is_a_symlink())
return;
lock_mtab();
mfp = my_setmntent(MOUNTED, "r");
if (mfp == NULL || mfp->mntent_fp == NULL) {
error ("cannot open %s (%s) - mtab not updated",
MOUNTED, strerror (errno));
goto leave;
}
mftmp = my_setmntent (MOUNTED_TEMP, "w");
if (mftmp == NULL || mfp->mntent_fp == NULL) {
error ("can't open %s (%s) - mtab not updated",
MOUNTED_TEMP, strerror (errno));
goto leave;
}
while ((mnt = my_getmntent (mfp))) {
if (streq (mnt->mnt_dir, dir)
/* Matthew Wilcox <willy@odie.barnet.ac.uk> */
|| (instead && instead->mnt_fsname &&
(streq (mnt->mnt_fsname, instead->mnt_fsname)))) {
added++;
if (instead) { /* a remount */
remnt = *instead;
next = &remnt;
remnt.mnt_fsname = mnt->mnt_fsname;
remnt.mnt_type = mnt->mnt_type;
if (instead->mnt_fsname
&& !streq(mnt->mnt_fsname, instead->mnt_fsname))
printf("mount: warning: cannot change "
"mounted device with a remount\n");
else if (instead->mnt_type
&& !streq(instead->mnt_type, "unknown")
&& !streq(mnt->mnt_type, instead->mnt_type))
printf("mount: warning: cannot change "
"filesystem type with a remount\n");
} else
next = NULL;
} else
next = mnt;
if (next && my_addmntent(mftmp, next) == 1)
die (EX_FILEIO, "error writing %s: %s",
MOUNTED_TEMP, strerror (errno));
}
if (instead && !added && my_addmntent(mftmp, instead) == 1)
die (EX_FILEIO, "error writing %s: %s",
MOUNTED_TEMP, strerror (errno));
my_endmntent (mfp);
if (fchmod (fileno (mftmp->mntent_fp), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0)
fprintf(stderr, "error changing mode of %s: %s\n", MOUNTED_TEMP,
strerror (errno));
my_endmntent (mftmp);
if (rename (MOUNTED_TEMP, MOUNTED) < 0)
fprintf(stderr, "can't rename %s to %s: %s\n", MOUNTED_TEMP, MOUNTED,
strerror(errno));
leave:
unlock_mtab();
}
|