summaryrefslogtreecommitdiffstats
path: root/usr.sbin/amd/config/mtab_file.c
blob: 5a82cd7eb6271f27a638754464a672844dfe61d1 (plain)
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
/*
 * Copyright (c) 1990 Jan-Simon Pendry
 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
 * Copyright (c) 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Jan-Simon Pendry at Imperial College, London.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)mtab_file.c	8.1 (Berkeley) 6/6/93
 *
 * $Id: mtab_file.c,v 1.1.1.1 1994/05/26 05:22:07 rgrimes Exp $
 *
 */

#include "am.h"

#ifdef READ_MTAB_FROM_FILE

#ifdef USE_FCNTL
#include <fcntl.h>
#else
#include <sys/file.h>
#endif /* USE_FCNTL */

#ifdef UPDATE_MTAB

/*
 * Do strict /etc/mtab locking
 */
#define	MTAB_LOCKING

/*
 * Firewall mtab entries
 */
#define	MTAB_STRIPNL

#include <sys/stat.h>
static FILE *mnt_file;

/*
 * If the system is being trashed by something, then
 * opening mtab may fail with ENFILE.  So, go to sleep
 * for a second and try again. (Yes - this has happened to me.)
 *
 * Note that this *may* block the automounter, oh well.
 * If we get to this state then things are badly wrong anyway...
 *
 * Give the system 10 seconds to recover but then give up.
 * Hopefully something else will exit and free up some file
 * table slots in that time.
 */
#define	NFILE_RETRIES	10 /* seconds */

#ifdef MTAB_LOCKING
#ifdef LOCK_FCNTL
static int lock(fd)
{
	int rc;
	struct flock lk;

	lk.l_type = F_WRLCK;
	lk.l_whence = 0;
	lk.l_start = 0;
	lk.l_len = 0;

again:
	rc = fcntl(fd, F_SETLKW, (caddr_t) &lk);
	if (rc < 0 && (errno == EACCES || errno == EAGAIN)) {
#ifdef DEBUG
		dlog("Blocked, trying to obtain exclusive mtab lock");
#endif /* DEBUG */
		sleep(1);
		goto again;
	}
	return rc;
}
#else
#define lock(fd) (flock((fd), LOCK_EX))
#endif /* LOCK_FCNTL */
#endif /* MTAB_LOCKING */

static FILE *open_locked_mtab(mtab_file, mode, fs)
char *mtab_file;
char *mode;
char *fs;
{
	FILE *mfp = 0;

#ifdef UPDATE_MTAB
	/*
	 * There is a possible race condition if two processes enter
	 * this routine at the same time.  One will be blocked by the
	 * exclusive lock below (or by the shared lock in setmntent)
	 * and by the time the second process has the exclusive lock
	 * it will be on the wrong underlying object.  To check for this
	 * the mtab file is stat'ed before and after all the locking
	 * sequence, and if it is a different file then we assume that
	 * it may be the wrong file (only "may", since there is another
	 * race between the initial stat and the setmntent).
	 *
	 * Simpler solutions to this problem are invited...
	 */
	int racing = 2;
#ifdef MTAB_LOCKING
	int rc;
	int retries = 0;
	struct stat st_before, st_after;
#endif /* MTAB_LOCKING */

	if (mnt_file) {
#ifdef DEBUG
		dlog("Forced close on %s in read_mtab", mtab_file);
#endif /* DEBUG */
		endmntent(mnt_file);
		mnt_file = 0;
	}

#ifdef MTAB_LOCKING
again:
	if (mfp) {
		endmntent(mfp);
		mfp = 0;
	}

	clock_valid = 0;
	if (stat(mtab_file, &st_before) < 0) {
		plog(XLOG_ERROR, "%s: stat: %m", mtab_file);
		if (errno == ESTALE) {
			/* happens occasionally */
			sleep(1);
			goto again;
		}
		return 0;
	}
#endif /* MTAB_LOCKING */
#endif /* UPDATE_MTAB */

eacces:
	mfp = setmntent(mtab_file, mode);
	if (!mfp) {
		/*
		 * Since setmntent locks the descriptor, it
		 * is possible it can fail... so retry if
		 * needed.
		 */
		if (errno == EACCES || errno == EAGAIN) {
#ifdef DEBUG
			dlog("Blocked, trying to obtain exclusive mtab lock");
#endif /* DEBUG */
			goto eacces;
		} else if (errno == ENFILE && retries++ < NFILE_RETRIES) {
			sleep(1);
			goto eacces;
		}

		plog(XLOG_ERROR, "setmntent(\"%s\", \"%s\"): %m", mtab_file, mode);
		return 0;
	}

#ifdef MTAB_LOCKING
#ifdef UPDATE_MTAB
	/*
	 * At this point we have an exclusive lock on the mount list,
	 * but it may be the wrong one so...
	 */

	/*
	 * Need to get an exclusive lock on the current
	 * mount table until we have a new copy written
	 * out, when the lock is released in free_mntlist.
	 * flock is good enough since the mount table is
	 * not shared between machines.
	 */
	do
		rc = lock(fileno(mfp));
	while (rc < 0 && errno == EINTR);
	if (rc < 0) {
		plog(XLOG_ERROR, "Couldn't lock %s: %m", mtab_file);
		endmntent(mfp);
		return 0;
	}
	/*
	 * Now check whether the mtab file has changed under our feet
	 */
	if (stat(mtab_file, &st_after) < 0) {
		plog(XLOG_ERROR, "%s: stat", mtab_file);
		goto again;
	}

	if (st_before.st_dev != st_after.st_dev ||
		st_before.st_ino != st_after.st_ino) {
			struct timeval tv;
			if (racing == 0) {
				/* Sometimes print a warning */
				plog(XLOG_WARNING,
					"Possible mount table race - retrying %s", fs);
			}
			racing = (racing+1) & 3;
			/*
			 * Take a nap.  From: Doug Kingston <dpk@morgan.com>
			 */
			tv.tv_sec = 0;
			tv.tv_usec = (mypid & 0x07) << 17;
			if (tv.tv_usec)
				if (select(0, (voidp) 0, (voidp) 0, (voidp) 0, &tv) < 0)
					plog(XLOG_WARNING, "mtab nap failed: %m");

			goto again;
	}
#endif /* UPDATE_MTAB */
#endif /* MTAB_LOCKING */

	return mfp;
}

/*
 * Unlock the mount table
 */
void unlock_mntlist P((void));
void unlock_mntlist()
{
	/*
	 * Release file lock, by closing the file
	 */
	if (mnt_file) {
		endmntent(mnt_file);
		mnt_file = 0;
	}
}

/*
 * Write out a mount list
 */
void rewrite_mtab(mp)
mntlist *mp;
{
	FILE *mfp;
	int error = 0;

	/*
	 * Concoct a temporary name in the same
	 * directory as the target mount table
	 * so that rename() will work.
	 */
	char tmpname[64];
	int retries;
	int tmpfd;
	char *cp;
	char *mcp = mtab;
	cp = strrchr(mcp, '/');
	if (cp) {
		bcopy(mcp, tmpname, cp - mcp);
		tmpname[cp-mcp] = '\0';
	} else {
		plog(XLOG_WARNING, "No '/' in mtab (%s), using \".\" as tmp directory", mtab);
		tmpname[0] = '.'; tmpname[1] = '\0';
	}
	strcat(tmpname, "/mtabXXXXXX");
	mktemp(tmpname);
	retries = 0;
enfile1:
	if ((tmpfd = open(tmpname, O_RDWR|O_CREAT|O_TRUNC, 0644)) < 0) {
		if (errno == ENFILE && retries++ < NFILE_RETRIES) {
			sleep(1);
			goto enfile1;
		}
		plog(XLOG_ERROR, "%s: open: %m", tmpname);
		return;
	}
	if (close(tmpfd) < 0)
		plog(XLOG_ERROR, "Couldn't close tmp file descriptor: %m");

	retries = 0;
enfile2:
	mfp = setmntent(tmpname, "w");
	if (!mfp) {
		if (errno == ENFILE && retries++ < NFILE_RETRIES) {
			sleep(1);
			goto enfile2;
		}
		plog(XLOG_ERROR, "setmntent(\"%s\", \"w\"): %m", tmpname);
		error = 1;
		goto out;
	}

	while (mp) {
		if (mp->mnt) {
			if (addmntent(mfp, mp->mnt)) {
				plog(XLOG_ERROR, "Can't write entry to %s", tmpname);
				error = 1;
				goto out;
			}
		}
		mp = mp->mnext;
	}

	/*
	 * SunOS 4.1 manuals say that the return code from entmntent()
	 * is always 1 and to treat as a void.  That means we need to
	 * call fflush() to make sure the new mtab file got written.
	 */
	if (fflush(mfp)) {
		plog(XLOG_ERROR, "flush new mtab file: %m");
		error = 1;
		goto out;
	}

	(void) endmntent(mfp);

	/*
	 * Rename temporary mtab to real mtab
	 */
	if (rename(tmpname, mtab) < 0) {
		plog(XLOG_ERROR, "rename %s to %s: %m", tmpname, mtab);
		error = 1;
		goto out;
	}

out:
	if (error)
		(void) unlink(tmpname);
}

#ifdef MTAB_STRIPNL
static void mtab_stripnl(s)
char *s;
{
	do {
		s = strchr(s, '\n');
		if (s)
			*s++ = ' ';
	} while (s);
}
#endif /* MTAB_STRIPNL */

/*
 * Append a mntent structure to the
 * current mount table.
 */
void write_mntent(mp)
struct mntent *mp;
{
	int retries = 0;
	FILE *mfp;
enfile:
	mfp = open_locked_mtab(mtab, "a", mp->mnt_dir);
	if (mfp) {
#ifdef MTAB_STRIPNL
		mtab_stripnl(mp->mnt_opts);
#endif /* MTAB_STRIPNL */
		if (addmntent(mfp, mp))
			plog(XLOG_ERROR, "Couldn't write %s: %m", mtab);
		if (fflush(mfp))
			plog(XLOG_ERROR, "Couldn't flush %s: %m", mtab);
		(void) endmntent(mfp);
	} else {
		if (errno == ENFILE && retries < NFILE_RETRIES) {
			sleep(1);
			goto enfile;
		}
		plog(XLOG_ERROR, "setmntent(\"%s\", \"a\"): %m", mtab);
	}
}

#endif /* UPDATE_MTAB */

static struct mntent *mnt_dup(mp)
struct mntent *mp;
{
	struct mntent *new_mp = ALLOC(mntent);

	new_mp->mnt_fsname = strdup(mp->mnt_fsname);
	new_mp->mnt_dir = strdup(mp->mnt_dir);
	new_mp->mnt_type = strdup(mp->mnt_type);
	new_mp->mnt_opts = strdup(mp->mnt_opts);

	new_mp->mnt_freq = mp->mnt_freq;
	new_mp->mnt_passno = mp->mnt_passno;

#ifdef FIXUP_MNTENT_DUP
	/*
	 * Additional fields get dup'ed here
	 */
	FIXUP_MNTENT_DUP(new_mp, mp);
#endif

	return new_mp;
}

/*
 * Read a mount table into memory
 */
mntlist *read_mtab(fs)
char *fs;
{
	mntlist **mpp, *mhp;

	struct mntent *mep;
	FILE *mfp = open_locked_mtab(mtab, "r+", fs);

	if (!mfp)
		return 0;

	mpp = &mhp;

/*
 * XXX - In SunOS 4 there is (yet another) memory leak
 * which loses 1K the first time getmntent is called.
 * (jsp)
 */
	while (mep = getmntent(mfp)) {
		/*
		 * Allocate a new slot
		 */
		*mpp = ALLOC(mntlist);

		/*
		 * Copy the data returned by getmntent
		 */
		(*mpp)->mnt = mnt_dup(mep);

		/*
		 * Move to next pointer
		 */
		mpp = &(*mpp)->mnext;
	}
	*mpp = 0;

#ifdef UPDATE_MTAB
	/*
	 * If we are not updating the mount table then we
	 * can free the resources held here, otherwise they
	 * must be held until the mount table update is complete
	 */
	mnt_file = mfp;
#else
	endmntent(mfp);
#endif /* UPDATE_MTAB */

	return mhp;
}

#endif /* READ_MTAB_FROM_FILE */
OpenPOWER on IntegriCloud