Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / mount / mtab.c
1
2 /*
3  * this code lifted from util-linux-ng, licensed GPLv2+,
4  *
5  *  git://git.kernel.org/pub/scm/utils/util-linux-ng/util-linux-ng.git
6  *
7  * whoever decided that each special mount program is responsible
8  * for updating /etc/mtab should be spanked.
9  *
10  * <sage@newdream.net>
11  */
12 #include <unistd.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/vfs.h>
22 #include <time.h>
23 #include <mntent.h>
24 #include <stdarg.h>
25
26 #include "mount/canonicalize.c"
27
28
29 /* Updating mtab ----------------------------------------------*/
30
31 /* Flag for already existing lock file. */
32 static int we_created_lockfile = 0;
33 static int lockfile_fd = -1;
34
35 /* Flag to indicate that signals have been set up. */
36 static int signals_have_been_setup = 0;
37
38 /* Ensure that the lock is released if we are interrupted.  */
39 extern char *strsignal(int sig);        /* not always in <string.h> */
40
41 static void
42 setlkw_timeout (int sig) {
43      /* nothing, fcntl will fail anyway */
44 }
45
46 #define _PATH_MOUNTED "/etc/mtab"
47 #define _PATH_MOUNTED_LOCK "/etc/mtab~"
48 #define PROC_SUPER_MAGIC      0x9fa0
49
50 /* exit status - bits below are ORed */
51 #define EX_USAGE        1       /* incorrect invocation or permission */
52 #define EX_SYSERR       2       /* out of memory, cannot fork, ... */
53 #define EX_SOFTWARE     4       /* internal mount bug or wrong version */
54 #define EX_USER         8       /* user interrupt */
55 #define EX_FILEIO      16       /* problems writing, locking, ... mtab/fstab */
56 #define EX_FAIL        32       /* mount failure */
57 #define EX_SOMEOK      64       /* some mount succeeded */
58
59 int die(int err, const char *fmt, ...) {
60         va_list args;
61
62         va_start(args, fmt);
63         vfprintf(stderr, fmt, args);
64         fprintf(stderr, "\n");
65         va_end(args);
66
67         exit(err);
68 }
69
70 static void
71 handler (int sig) {
72         die(EX_USER, "%s", strsignal(sig));
73 }
74
75 /* Remove lock file.  */
76 void
77 unlock_mtab (void) {
78         if (we_created_lockfile) {
79                 close(lockfile_fd);
80                 lockfile_fd = -1;
81                 unlink (_PATH_MOUNTED_LOCK);
82                 we_created_lockfile = 0;
83         }
84 }
85
86 /* Create the lock file.
87    The lock file will be removed if we catch a signal or when we exit. */
88 /* The old code here used flock on a lock file /etc/mtab~ and deleted
89    this lock file afterwards. However, as rgooch remarks, that has a
90    race: a second mount may be waiting on the lock and proceed as
91    soon as the lock file is deleted by the first mount, and immediately
92    afterwards a third mount comes, creates a new /etc/mtab~, applies
93    flock to that, and also proceeds, so that the second and third mount
94    now both are scribbling in /etc/mtab.
95    The new code uses a link() instead of a creat(), where we proceed
96    only if it was us that created the lock, and hence we always have
97    to delete the lock afterwards. Now the use of flock() is in principle
98    superfluous, but avoids an arbitrary sleep(). */
99
100 /* Where does the link point to? Obvious choices are mtab and mtab~~.
101    HJLu points out that the latter leads to races. Right now we use
102    mtab~.<pid> instead. Use 20 as upper bound for the length of %d. */
103 #define MOUNTLOCK_LINKTARGET            _PATH_MOUNTED_LOCK "%d"
104 #define MOUNTLOCK_LINKTARGET_LTH        (sizeof(_PATH_MOUNTED_LOCK)+20)
105
106 /*
107  * The original mount locking code has used sleep(1) between attempts and
108  * maximal number of attemps has been 5.
109  *
110  * There was very small number of attempts and extremely long waiting (1s)
111  * that is useless on machines with large number of concurret mount processes.
112  *
113  * Now we wait few thousand microseconds between attempts and we have global
114  * time limit (30s) rather than limit for number of attempts. The advantage
115  * is that this method also counts time which we spend in fcntl(F_SETLKW) and
116  * number of attempts is not so much restricted.
117  *
118  * -- kzak@redhat.com [2007-Mar-2007]
119  */
120
121 /* maximum seconds between first and last attempt */
122 #define MOUNTLOCK_MAXTIME               30
123
124 /* sleep time (in microseconds, max=999999) between attempts */
125 #define MOUNTLOCK_WAITTIME              5000
126
127 void
128 lock_mtab (void) {
129         int i;
130         struct timespec waittime;
131         struct timeval maxtime;
132         char linktargetfile[MOUNTLOCK_LINKTARGET_LTH];
133
134         if (!signals_have_been_setup) {
135                 int sig = 0;
136                 struct sigaction sa;
137
138                 sa.sa_handler = handler;
139                 sa.sa_flags = 0;
140                 sigfillset (&sa.sa_mask);
141
142                 while (sigismember (&sa.sa_mask, ++sig) != -1
143                        && sig != SIGCHLD) {
144                         if (sig == SIGALRM)
145                                 sa.sa_handler = setlkw_timeout;
146                         else
147                                 sa.sa_handler = handler;
148                         sigaction (sig, &sa, (struct sigaction *) 0);
149                 }
150                 signals_have_been_setup = 1;
151         }
152
153         snprintf(linktargetfile, sizeof(linktargetfile), MOUNTLOCK_LINKTARGET,
154                  getpid ());
155
156         i = open (linktargetfile, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
157         if (i < 0) {
158                 int errsv = errno;
159                 /* linktargetfile does not exist (as a file)
160                    and we cannot create it. Read-only filesystem?
161                    Too many files open in the system?
162                    Filesystem full? */
163                 die (EX_FILEIO, "can't create lock file %s: %s "
164                      "(use -n flag to override)",
165                      linktargetfile, strerror (errsv));
166         }
167         close(i);
168
169         gettimeofday(&maxtime, NULL);
170         maxtime.tv_sec += MOUNTLOCK_MAXTIME;
171
172         waittime.tv_sec = 0;
173         waittime.tv_nsec = (1000 * MOUNTLOCK_WAITTIME);
174
175         /* Repeat until it was us who made the link */
176         while (!we_created_lockfile) {
177                 struct timeval now;
178                 struct flock flock;
179                 int errsv, j;
180
181                 j = link(linktargetfile, _PATH_MOUNTED_LOCK);
182                 errsv = errno;
183
184                 if (j == 0)
185                         we_created_lockfile = 1;
186
187                 if (j < 0 && errsv != EEXIST) {
188                         (void) unlink(linktargetfile);
189                         die (EX_FILEIO, "can't link lock file %s: %s "
190                              "(use -n flag to override)",
191                              _PATH_MOUNTED_LOCK, strerror (errsv));
192                 }
193
194                 lockfile_fd = open (_PATH_MOUNTED_LOCK, O_WRONLY);
195
196                 if (lockfile_fd < 0) {
197                         /* Strange... Maybe the file was just deleted? */
198                         int errsv = errno;
199                         gettimeofday(&now, NULL);
200                         if (errno == ENOENT && now.tv_sec < maxtime.tv_sec) {
201                                 we_created_lockfile = 0;
202                                 continue;
203                         }
204                         (void) unlink(linktargetfile);
205                         die (EX_FILEIO, "can't open lock file %s: %s "
206                              "(use -n flag to override)",
207                              _PATH_MOUNTED_LOCK, strerror (errsv));
208                 }
209
210                 flock.l_type = F_WRLCK;
211                 flock.l_whence = SEEK_SET;
212                 flock.l_start = 0;
213                 flock.l_len = 0;
214
215                 if (j == 0) {
216                         /* We made the link. Now claim the lock. */
217                         if (fcntl (lockfile_fd, F_SETLK, &flock) == -1) {
218                                 /* proceed, since it was us who created the lockfile anyway */
219                         }
220                         (void) unlink(linktargetfile);
221                 } else {
222                         /* Someone else made the link. Wait. */
223                         gettimeofday(&now, NULL);
224                         if (now.tv_sec < maxtime.tv_sec) {
225                                 alarm(maxtime.tv_sec - now.tv_sec);
226                                 if (fcntl (lockfile_fd, F_SETLKW, &flock) == -1) {
227                                         int errsv = errno;
228                                         (void) unlink(linktargetfile);
229                                         die (EX_FILEIO, "can't lock lock file %s: %s",
230                                              _PATH_MOUNTED_LOCK, (errno == EINTR) ?
231                                              "timed out" : strerror (errsv));
232                                 }
233                                 alarm(0);
234
235                                 nanosleep(&waittime, NULL);
236                         } else {
237                                 (void) unlink(linktargetfile);
238                                 die (EX_FILEIO, "Cannot create link %s\n"
239                                      "Perhaps there is a stale lock file?\n",
240                                          _PATH_MOUNTED_LOCK);
241                         }
242                         close(lockfile_fd);
243                 }
244         }
245 }
246
247 static void
248 update_mtab_entry(const char *spec, const char *node, const char *type,
249                   const char *opts, int flags, int freq, int pass) {
250         struct statfs buf;
251         int err = statfs(_PATH_MOUNTED, &buf);
252         if (err) {
253                 printf("mount: can't statfs %s: %s", _PATH_MOUNTED,
254                        strerror (err));
255                 return;
256         }
257         /* /etc/mtab is symbol link to /proc/self/mounts? */
258         if (buf.f_type == PROC_SUPER_MAGIC)
259                 return;
260
261         if (!opts)
262                 opts = "rw";
263
264         struct mntent mnt;
265         mnt.mnt_fsname = strdup(spec);
266         mnt.mnt_dir = canonicalize_path(node);
267         mnt.mnt_type = strdup(type);
268         mnt.mnt_opts = strdup(opts);
269         mnt.mnt_freq = freq;
270         mnt.mnt_passno = pass;
271
272         FILE *fp;
273         
274         lock_mtab();
275         fp = setmntent(_PATH_MOUNTED, "a+");
276         if (fp == NULL) {
277                 int errsv = errno;
278                 printf("mount: can't open %s: %s", _PATH_MOUNTED,
279                        strerror (errsv));
280         } else {
281                 if ((addmntent (fp, &mnt)) == 1) {
282                         int errsv = errno;
283                         printf("mount: error writing %s: %s",
284                               _PATH_MOUNTED, strerror (errsv));
285                 }
286         }
287         endmntent(fp);
288         unlock_mtab();
289
290         free(mnt.mnt_fsname);
291         free(mnt.mnt_dir);
292         free(mnt.mnt_type);
293         free(mnt.mnt_opts);
294 }