summaryrefslogtreecommitdiff
path: root/src/t_mtab.c
blob: c0640a1951a4ceb0f9e36755ccc3c6c9df4c7443 (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
/*
 * Test program based on Linux mount(8) source attempting to
 * trigger a suspected problem in rename(2) code paths - its
 * symptoms have been multiple mtab entries in /etc... hence
 * use of the actual mount code here.
 */

#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <mntent.h>
#include <limits.h>

#define LOCK_TIMEOUT	10
#define _(x)		(x)

static char *mounted = "t_mtab";
static char *mounted_lock = "t_mtab~";
static char *mounted_temp = "t_mtab.tmp";

/* 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) {
    fprintf(stderr, "%s\n", strsignal(sig));
    exit(1);
}

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(). */

void
lock_mtab (void) {
#if 0	/* nathans: dont limit, we are forcing lots of parallel accesses */
	int tries = 3;
#endif
	char linktargetfile[PATH_MAX + 20];

	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;
	}

	/* use 20 as upper bound for the length of %d output */
	snprintf(linktargetfile, PATH_MAX+20, "%s%d", mounted_lock, getpid());

	/* Repeat until it was us who made the link */
	while (!we_created_lockfile) {
		struct flock flock;
		int fd, errsv, i, j;

		i = open (linktargetfile, O_WRONLY|O_CREAT, 0);
		if (i < 0) {
			int errsv = errno;
			/* linktargetfile does not exist (as a file)
			   and we cannot create it. Read-only filesystem?
			   Too many files open in the system?
			   Filesystem full? */
			fprintf(stderr, "can't create lock file %s: %s "
			     "(use -n flag to override)\n",
			     linktargetfile, strerror (errsv));
			exit(1);
		}
		close(i);

		j = link(linktargetfile, mounted_lock);
		errsv = errno;

		(void) unlink(linktargetfile);

		if (j < 0 && errsv != EEXIST) {
			fprintf(stderr, "can't link lock file %s: %s "
			     "(use -n flag to override)\n",
			     mounted_lock, strerror (errsv));
			exit(1);
		}

		fd = open (mounted_lock, O_WRONLY);

		if (fd < 0) {
			int errsv = errno;
			/* Strange... Maybe the file was just deleted? */
#if 0	/* nathans: dont limit, we are forcing lots of parallel accesses */
			if (errno == ENOENT && tries-- > 0)
#endif
			if (errno == ENOENT)
				continue;
			fprintf(stderr, "can't open lock file %s: %s\n",
			     mounted_lock, strerror (errsv));
			exit(1);
		}

		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 &&
			    errno != EBUSY && errno != EAGAIN) {
				int errsv = errno;
				printf(_("Can't lock lock file %s: %s\n"),
					   mounted_lock, strerror (errsv));
				/* proceed anyway */
			}
			we_created_lockfile = 1;
		} else {
#if 0	/* nathans: dont limit, we are forcing lots of parallel accesses */
			static int tries = 0;
#endif

			/* Someone else made the link. Wait. */
			alarm(LOCK_TIMEOUT);
			if (fcntl (fd, F_SETLKW, &flock) == -1 &&
			    errno != EBUSY && errno != EAGAIN) {
				int errsv = errno;
				fprintf(stderr, "can't lock lock file %s: %s\n",
				     mounted_lock, (errno == EINTR) ?
				     _("timed out") : strerror (errsv));
				exit(1);
			}
			alarm(0);
#if 0	/* nathans: dont limit, we are forcing lots of parallel accesses */
			/* Limit the number of iterations - maybe there
			   still is some old /etc/mtab~ */
			if (tries++ > 3) {
				if (tries > 5) {
					fprintf(stderr, "Cant create link %s\n"
					    "Perhaps there is a stale lock file?\n",
					     mounted_lock);
					exit(1);
				}
				sleep(1);
			}
#endif
		}
		close (fd);
	}
}

/* Remove lock file.  */
void
unlock_mtab (void) {
	int ret;
	if (we_created_lockfile) {
		ret = unlink (mounted_lock);
		if (ret) {
			fprintf(stderr, "Cannot remove lock file: %s\n", strerror(errno));
			exit(1);
		} else {
			we_created_lockfile = 0;
		}
	}
}

/*
 * Update the mtab.
 *  Used by umount with null INSTEAD: remove the last DIR entry.
 *  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 (void)
{
	FILE *mntent_fp, *mftmp;
	char buffer[4096];
	int size;

	lock_mtab();

	/* having locked mtab, read it again & write to mtemp */
	mntent_fp = fopen(mounted, "r");
	if (!mntent_fp) {
		fprintf(stderr, "cannot open %s for reading\n", mounted);
		exit(1);
	}
	mftmp = fopen(mounted_temp, "w");
	if (!mftmp) {
		fprintf(stderr, "cannot open %s for writing\n", mounted_temp);
		exit(1);
	}
	while ((size = read(fileno(mntent_fp), buffer, sizeof(buffer))) > 0) {
		if (write(fileno(mftmp), buffer, size) < 0) {
			fprintf(stderr, "write failure: %s\n", strerror(errno));
			exit(1);
		}
	}
	if (size < 0) {
		fprintf(stderr, "read failure: %s\n", strerror(errno));
		exit(1);
	}
	fclose(mntent_fp);

	if (fchmod (fileno (mftmp),
		    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) {
		int errsv = errno;
		fprintf(stderr, _("error changing mode of %s: %s\n"),
			mounted_temp, strerror (errsv));
	}
	fclose(mftmp);

	{ /*
	   * If mount is setuid and some non-root user mounts sth,
	   * then mtab.tmp might get the group of this user. Copy uid/gid
	   * from the present mtab before renaming.
	   */
	    struct stat sbuf;
	    if (stat (mounted, &sbuf) == 0)
		chown (mounted_temp, sbuf.st_uid, sbuf.st_gid);
	}

	/* rename mtemp to mtab */
	if (rename (mounted_temp, mounted) < 0) {
		int errsv = errno;
		fprintf(stderr, _("can't rename %s to %s: %s\n"),
			mounted_temp, mounted, strerror(errsv));
	}

	unlock_mtab();
}

int main(int argc, char **argv)
{
	int i, stop = 100000;
	FILE *fout = NULL;

	if (argc > 1)
		stop = atoi(argv[1]);

	for (i = 0; i < stop; i++) {
		update_mtab();
	}

	if (argc > 2)
		fout = fopen(argv[2],"a");
	if (!fout)
		fout = stdout;
	fprintf(fout, "completed %d iterations\n", stop);
	return 0;
}