1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2016 The DragonFly Project
5 * Copyright (c) 2014 The FreeBSD Foundation
6 * All rights reserved.
7 *
8 * This software was developed by Edward Tomasz Napierala under sponsorship
9 * from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 */
33
34 #include <sys/types.h>
35 #include <sys/mount.h>
36 #include <sys/event.h>
37 #include <sys/time.h>
38 #include <assert.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <libutil.h>
45
46 #include "common.h"
47
48 #define AUTOUNMOUNTD_PIDFILE "/var/run/autounmountd.pid"
49
50 struct automounted_fs {
51 TAILQ_ENTRY(automounted_fs) af_next;
52 time_t af_mount_time;
53 bool af_mark;
54 fsid_t af_fsid;
55 char af_mountpoint[MNAMELEN];
56 };
57
58 static TAILQ_HEAD(, automounted_fs) automounted;
59
60 static struct automounted_fs *
automounted_find(fsid_t fsid)61 automounted_find(fsid_t fsid)
62 {
63 struct automounted_fs *af;
64
65 TAILQ_FOREACH(af, &automounted, af_next) {
66 if (fsidcmp(&af->af_fsid, &fsid) == 0)
67 return (af);
68 }
69
70 return (NULL);
71 }
72
73 static struct automounted_fs *
automounted_add(fsid_t fsid,const char * mountpoint)74 automounted_add(fsid_t fsid, const char *mountpoint)
75 {
76 struct automounted_fs *af;
77
78 af = calloc(1, sizeof(*af));
79 if (af == NULL)
80 log_err(1, "calloc");
81 af->af_mount_time = time(NULL);
82 af->af_fsid = fsid;
83 strlcpy(af->af_mountpoint, mountpoint, sizeof(af->af_mountpoint));
84
85 TAILQ_INSERT_TAIL(&automounted, af, af_next);
86
87 return (af);
88 }
89
90 static void
automounted_remove(struct automounted_fs * af)91 automounted_remove(struct automounted_fs *af)
92 {
93
94 TAILQ_REMOVE(&automounted, af, af_next);
95 free(af);
96 }
97
98 static void
refresh_automounted(void)99 refresh_automounted(void)
100 {
101 struct automounted_fs *af, *tmpaf;
102 struct statfs *mntbuf;
103 int i, nitems;
104
105 nitems = getmntinfo(&mntbuf, MNT_WAIT);
106 if (nitems <= 0)
107 log_err(1, "getmntinfo");
108
109 log_debugx("refreshing list of automounted filesystems");
110
111 TAILQ_FOREACH(af, &automounted, af_next)
112 af->af_mark = false;
113
114 for (i = 0; i < nitems; i++) {
115 if (strcmp(mntbuf[i].f_fstypename, "autofs") == 0) {
116 log_debugx("skipping %s, filesystem type is autofs",
117 mntbuf[i].f_mntonname);
118 continue;
119 }
120
121 if ((mntbuf[i].f_flags & MNT_AUTOMOUNTED) == 0) {
122 log_debugx("skipping %s, not automounted",
123 mntbuf[i].f_mntonname);
124 continue;
125 }
126
127 af = automounted_find(mntbuf[i].f_fsid);
128 if (af == NULL) {
129 log_debugx("new automounted filesystem found on %s "
130 "(FSID:%d:%d)", mntbuf[i].f_mntonname,
131 mntbuf[i].f_fsid.val[0], mntbuf[i].f_fsid.val[1]);
132 af = automounted_add(mntbuf[i].f_fsid,
133 mntbuf[i].f_mntonname);
134 } else {
135 log_debugx("already known automounted filesystem "
136 "found on %s (FSID:%d:%d)", mntbuf[i].f_mntonname,
137 mntbuf[i].f_fsid.val[0], mntbuf[i].f_fsid.val[1]);
138 }
139 af->af_mark = true;
140 }
141
142 TAILQ_FOREACH_SAFE(af, &automounted, af_next, tmpaf) {
143 if (af->af_mark)
144 continue;
145 log_debugx("lost filesystem mounted on %s (FSID:%d:%d)",
146 af->af_mountpoint, af->af_fsid.val[0], af->af_fsid.val[1]);
147 automounted_remove(af);
148 }
149 }
150
151 static int
do_unmount(const fsid_t fsid __unused,const char * mountpoint)152 do_unmount(const fsid_t fsid __unused, const char *mountpoint)
153 {
154 struct stat sb;
155 int error;
156
157 error = unmount(mountpoint, 0);
158 if (error != 0) {
159 if (errno == EBUSY) {
160 /*
161 * XXX: Workaround a long-standing VFS bug.
162 * Need to stat(2) the mountpoint or parent directory
163 * to access fs after EBUSY (and possibly other errors).
164 * https://bugs.dragonflybsd.org/issues/2908
165 */
166 if (stat(mountpoint, &sb))
167 log_warn("cannot stat %s after EBUSY",
168 mountpoint);
169 log_debugx("cannot unmount %s: %s",
170 mountpoint, strerror(errno));
171 } else {
172 log_warn("cannot unmount %s", mountpoint);
173 }
174 } else
175 rpc_umntall();
176
177 return (error);
178 }
179
180 static time_t
expire_automounted(time_t expiration_time)181 expire_automounted(time_t expiration_time)
182 {
183 struct automounted_fs *af, *tmpaf;
184 time_t now;
185 time_t mounted_for, mounted_max = -1;
186 int error;
187
188 now = time(NULL);
189
190 log_debugx("expiring automounted filesystems");
191
192 TAILQ_FOREACH_SAFE(af, &automounted, af_next, tmpaf) {
193 mounted_for = difftime(now, af->af_mount_time);
194
195 if (mounted_for < expiration_time) {
196 log_debugx("skipping %s (FSID:%d:%d), mounted "
197 "for %jd seconds", af->af_mountpoint,
198 af->af_fsid.val[0], af->af_fsid.val[1],
199 (intmax_t)mounted_for);
200
201 if (mounted_for > mounted_max)
202 mounted_max = mounted_for;
203
204 continue;
205 }
206
207 log_debugx("filesystem mounted on %s (FSID:%d:%d), "
208 "was mounted for %jd seconds; unmounting",
209 af->af_mountpoint, af->af_fsid.val[0], af->af_fsid.val[1],
210 (intmax_t)mounted_for);
211 error = do_unmount(af->af_fsid, af->af_mountpoint);
212 if (error != 0) {
213 if (mounted_for > mounted_max)
214 mounted_max = mounted_for;
215 }
216 }
217
218 return (mounted_max);
219 }
220
221 static void
usage_autounmountd(void)222 usage_autounmountd(void)
223 {
224
225 fprintf(stderr, "usage: autounmountd [-r time][-t time][-dv]\n");
226 exit(1);
227 }
228
229 static void
do_wait(int kq,time_t sleep_time)230 do_wait(int kq, time_t sleep_time)
231 {
232 struct timespec timeout;
233 struct kevent unused;
234 int nevents;
235
236 if (sleep_time != -1) {
237 assert(sleep_time > 0);
238 timeout.tv_sec = sleep_time;
239 timeout.tv_nsec = 0;
240
241 log_debugx("waiting for filesystem event for %jd seconds",
242 (intmax_t)sleep_time);
243 nevents = kevent(kq, NULL, 0, &unused, 1, &timeout);
244 } else {
245 log_debugx("waiting for filesystem event");
246 nevents = kevent(kq, NULL, 0, &unused, 1, NULL);
247 }
248 if (nevents < 0) {
249 if (errno == EINTR)
250 return;
251 log_err(1, "kevent");
252 }
253
254 if (nevents == 0) {
255 log_debugx("timeout reached");
256 assert(sleep_time > 0);
257 } else {
258 log_debugx("got filesystem event");
259 }
260 }
261
262 void
main_autounmountd(int argc,char ** argv)263 main_autounmountd(int argc, char **argv)
264 {
265 struct kevent event;
266 struct pidfh *pidfh;
267 pid_t otherpid;
268 const char *pidfile_path = AUTOUNMOUNTD_PIDFILE;
269 int ch, debug = 0, error, kq;
270 time_t expiration_time = 600, retry_time = 600, mounted_max, sleep_time;
271 bool dont_daemonize = false;
272
273 while ((ch = getopt(argc, argv, "dr:t:v")) != -1) {
274 switch (ch) {
275 case 'd':
276 dont_daemonize = true;
277 debug++;
278 break;
279 case 'r':
280 retry_time = atoi(optarg);
281 break;
282 case 't':
283 expiration_time = atoi(optarg);
284 break;
285 case 'v':
286 debug++;
287 break;
288 case '?':
289 default:
290 usage_autounmountd();
291 }
292 }
293 argc -= optind;
294 if (argc != 0)
295 usage_autounmountd();
296
297 if (retry_time <= 0)
298 log_errx(1, "retry time must be greater than zero");
299 if (expiration_time <= 0)
300 log_errx(1, "expiration time must be greater than zero");
301
302 log_init(debug);
303
304 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
305 if (pidfh == NULL) {
306 if (errno == EEXIST) {
307 log_errx(1, "daemon already running, pid: %jd.",
308 (intmax_t)otherpid);
309 }
310 log_err(1, "cannot open or create pidfile \"%s\"",
311 pidfile_path);
312 }
313
314 if (dont_daemonize == false) {
315 if (daemon(0, 0) == -1) {
316 log_warn("cannot daemonize");
317 pidfile_remove(pidfh);
318 exit(1);
319 }
320 }
321
322 pidfile_write(pidfh);
323
324 TAILQ_INIT(&automounted);
325
326 kq = kqueue();
327 if (kq < 0)
328 log_err(1, "kqueue");
329
330 EV_SET(&event, 0, EVFILT_FS, EV_ADD | EV_CLEAR, 0, 0, NULL);
331 error = kevent(kq, &event, 1, NULL, 0, NULL);
332 if (error < 0)
333 log_err(1, "kevent");
334
335 for (;;) {
336 refresh_automounted();
337 mounted_max = expire_automounted(expiration_time);
338 if (mounted_max == -1) {
339 sleep_time = mounted_max;
340 log_debugx("no filesystems to expire");
341 } else if (mounted_max < expiration_time) {
342 sleep_time = difftime(expiration_time, mounted_max);
343 log_debugx("some filesystems expire in %jd seconds",
344 (intmax_t)sleep_time);
345 } else {
346 sleep_time = retry_time;
347 log_debugx("some expired filesystems remain mounted, "
348 "will retry in %jd seconds", (intmax_t)sleep_time);
349 }
350
351 do_wait(kq, sleep_time);
352 }
353 }
354