2 * Test program based on Linux mount(8) source attempting to
3 * trigger a suspected problem in rename(2) code paths - its
4 * symptoms have been multiple mtab entries in /etc... hence
5 * use of the actual mount code here.
19 #define LOCK_TIMEOUT 10
22 static char *mounted = "t_mtab";
23 static char *mounted_lock = "t_mtab~";
24 static char *mounted_temp = "t_mtab.tmp";
26 /* Updating mtab ----------------------------------------------*/
28 /* Flag for already existing lock file. */
29 static int we_created_lockfile = 0;
31 /* Flag to indicate that signals have been set up. */
32 static int signals_have_been_setup = 0;
34 /* Ensure that the lock is released if we are interrupted. */
37 fprintf(stderr, "%s", sys_siglist[sig]);
42 setlkw_timeout (int sig) {
43 /* nothing, fcntl will fail anyway */
46 /* Create the lock file.
47 The lock file will be removed if we catch a signal or when we exit. */
48 /* The old code here used flock on a lock file /etc/mtab~ and deleted
49 this lock file afterwards. However, as rgooch remarks, that has a
50 race: a second mount may be waiting on the lock and proceed as
51 soon as the lock file is deleted by the first mount, and immediately
52 afterwards a third mount comes, creates a new /etc/mtab~, applies
53 flock to that, and also proceeds, so that the second and third mount
54 now both are scribbling in /etc/mtab.
55 The new code uses a link() instead of a creat(), where we proceed
56 only if it was us that created the lock, and hence we always have
57 to delete the lock afterwards. Now the use of flock() is in principle
58 superfluous, but avoids an arbitrary sleep(). */
63 char linktargetfile[PATH_MAX + 20];
65 if (!signals_have_been_setup) {
69 sa.sa_handler = handler;
71 sigfillset (&sa.sa_mask);
73 while (sigismember (&sa.sa_mask, ++sig) != -1
76 sa.sa_handler = setlkw_timeout;
78 sa.sa_handler = handler;
79 sigaction (sig, &sa, (struct sigaction *) 0);
81 signals_have_been_setup = 1;
84 /* use 20 as upper bound for the length of %d output */
85 snprintf(linktargetfile, PATH_MAX+20, "%s%d", mounted_lock, getpid());
87 /* Repeat until it was us who made the link */
88 while (!we_created_lockfile) {
92 i = open (linktargetfile, O_WRONLY|O_CREAT, 0);
95 /* linktargetfile does not exist (as a file)
96 and we cannot create it. Read-only filesystem?
97 Too many files open in the system?
99 fprintf(stderr, "can't create lock file %s: %s "
100 "(use -n flag to override)",
101 linktargetfile, strerror (errsv));
106 j = link(linktargetfile, mounted_lock);
109 (void) unlink(linktargetfile);
111 if (j < 0 && errsv != EEXIST) {
112 fprintf(stderr, "can't link lock file %s: %s "
113 "(use -n flag to override)",
114 mounted_lock, strerror (errsv));
118 fd = open (mounted_lock, O_WRONLY);
122 /* Strange... Maybe the file was just deleted? */
123 if (errno == ENOENT && tries-- > 0)
125 fprintf(stderr, "can't open lock file %s: %s "
126 "(use -n flag to override)",
127 mounted_lock, strerror (errsv));
131 flock.l_type = F_WRLCK;
132 flock.l_whence = SEEK_SET;
137 /* We made the link. Now claim the lock. */
138 if (fcntl (fd, F_SETLK, &flock) == -1) {
140 printf(_("Can't lock lock file %s: %s\n"),
141 mounted_lock, strerror (errsv));
144 we_created_lockfile = 1;
146 #if 0 /* nathans: dont limit, we are forcing lots of parallel accesses */
147 static int tries = 0;
150 /* Someone else made the link. Wait. */
152 if (fcntl (fd, F_SETLKW, &flock) == -1 &&
155 fprintf(stderr, "can't lock lock file %s: %s",
156 mounted_lock, (errno == EINTR) ?
157 _("timed out") : strerror (errsv));
161 #if 0 /* nathans: dont limit, we are forcing lots of parallel accesses */
162 /* Limit the number of iterations - maybe there
163 still is some old /etc/mtab~ */
166 fprintf(stderr, "Cant create link %s\n"
167 "Perhaps there is a stale lock file?\n",
179 /* Remove lock file. */
182 if (we_created_lockfile) {
183 unlink (mounted_lock);
184 we_created_lockfile = 0;
190 * Used by umount with null INSTEAD: remove the last DIR entry.
191 * Used by mount upon a remount: update option part,
192 * and complain if a wrong device or type was given.
193 * [Note that often a remount will be a rw remount of /
194 * where there was no entry before, and we'll have to believe
195 * the values given in INSTEAD.]
201 FILE *mntent_fp, *mftmp;
207 /* having locked mtab, read it again & write to mtemp */
208 mntent_fp = fopen(mounted, "r");
210 fprintf(stderr, "cannot open %s for reading\n", mounted);
213 mftmp = fopen(mounted_temp, "w");
215 fprintf(stderr, "cannot open %s for writing\n", mounted_temp);
218 while ((size = read(fileno(mntent_fp), buffer, sizeof(buffer))) > 0) {
219 if (write(fileno(mftmp), buffer, size) < 0) {
220 fprintf(stderr, "write failure: %s\n", strerror(errno));
225 fprintf(stderr, "read failure: %s\n", strerror(errno));
230 if (fchmod (fileno (mftmp),
231 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) {
233 fprintf(stderr, _("error changing mode of %s: %s\n"),
234 mounted_temp, strerror (errsv));
239 * If mount is setuid and some non-root user mounts sth,
240 * then mtab.tmp might get the group of this user. Copy uid/gid
241 * from the present mtab before renaming.
244 if (stat (mounted, &sbuf) == 0)
245 chown (mounted_temp, sbuf.st_uid, sbuf.st_gid);
248 /* rename mtemp to mtab */
249 if (rename (mounted_temp, mounted) < 0) {
251 fprintf(stderr, _("can't rename %s to %s: %s\n"),
252 mounted_temp, mounted, strerror(errsv));
258 int main(int argc, char **argv)
260 int i, stop = 100000;
263 stop = atoi(argv[1]);
265 for (i = 0; i < stop; i++) {
268 printf("completed %d iterations\n", stop);