2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 The DragonFly Project
5 * Copyright (c) 2014 The FreeBSD Foundation
8 * This software was developed by Edward Tomasz Napierala under sponsorship
9 * from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/types.h>
35 #include <sys/mount.h>
36 #include <sys/event.h>
48 #define AUTOUNMOUNTD_PIDFILE "/var/run/autounmountd.pid"
50 struct automounted_fs {
51 TAILQ_ENTRY(automounted_fs) af_next;
55 char af_mountpoint[MNAMELEN];
58 static TAILQ_HEAD(, automounted_fs) automounted;
60 static struct automounted_fs *
61 automounted_find(fsid_t fsid)
63 struct automounted_fs *af;
65 TAILQ_FOREACH(af, &automounted, af_next) {
66 if (fsidcmp(&af->af_fsid, &fsid) == 0)
73 static struct automounted_fs *
74 automounted_add(fsid_t fsid, const char *mountpoint)
76 struct automounted_fs *af;
78 af = calloc(1, sizeof(*af));
81 af->af_mount_time = time(NULL);
83 strlcpy(af->af_mountpoint, mountpoint, sizeof(af->af_mountpoint));
85 TAILQ_INSERT_TAIL(&automounted, af, af_next);
91 automounted_remove(struct automounted_fs *af)
94 TAILQ_REMOVE(&automounted, af, af_next);
99 refresh_automounted(void)
101 struct automounted_fs *af, *tmpaf;
102 struct statfs *mntbuf;
105 nitems = getmntinfo(&mntbuf, MNT_WAIT);
107 log_err(1, "getmntinfo");
109 log_debugx("refreshing list of automounted filesystems");
111 TAILQ_FOREACH(af, &automounted, af_next)
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);
121 if ((mntbuf[i].f_flags & MNT_AUTOMOUNTED) == 0) {
122 log_debugx("skipping %s, not automounted",
123 mntbuf[i].f_mntonname);
127 af = automounted_find(mntbuf[i].f_fsid);
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);
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]);
142 TAILQ_FOREACH_SAFE(af, &automounted, af_next, tmpaf) {
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);
152 do_unmount(const fsid_t fsid __unused, const char *mountpoint)
157 error = unmount(mountpoint, 0);
159 if (errno == EBUSY) {
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
166 if (stat(mountpoint, &sb))
167 log_warn("cannot stat %s after EBUSY",
169 log_debugx("cannot unmount %s: %s",
170 mountpoint, strerror(errno));
172 log_warn("cannot unmount %s", mountpoint);
180 expire_automounted(time_t expiration_time)
182 struct automounted_fs *af, *tmpaf;
184 time_t mounted_for, mounted_max = -1;
189 log_debugx("expiring automounted filesystems");
191 TAILQ_FOREACH_SAFE(af, &automounted, af_next, tmpaf) {
192 mounted_for = difftime(now, af->af_mount_time);
194 if (mounted_for < expiration_time) {
195 log_debugx("skipping %s (FSID:%d:%d), mounted "
196 "for %jd seconds", af->af_mountpoint,
197 af->af_fsid.val[0], af->af_fsid.val[1],
198 (intmax_t)mounted_for);
200 if (mounted_for > mounted_max)
201 mounted_max = mounted_for;
206 log_debugx("filesystem mounted on %s (FSID:%d:%d), "
207 "was mounted for %jd seconds; unmounting",
208 af->af_mountpoint, af->af_fsid.val[0], af->af_fsid.val[1],
209 (intmax_t)mounted_for);
210 error = do_unmount(af->af_fsid, af->af_mountpoint);
212 if (mounted_for > mounted_max)
213 mounted_max = mounted_for;
217 return (mounted_max);
221 usage_autounmountd(void)
224 fprintf(stderr, "usage: autounmountd [-r time][-t time][-dv]\n");
229 do_wait(int kq, time_t sleep_time)
231 struct timespec timeout;
232 struct kevent unused;
235 if (sleep_time != -1) {
236 assert(sleep_time > 0);
237 timeout.tv_sec = sleep_time;
240 log_debugx("waiting for filesystem event for %jd seconds",
241 (intmax_t)sleep_time);
242 nevents = kevent(kq, NULL, 0, &unused, 1, &timeout);
244 log_debugx("waiting for filesystem event");
245 nevents = kevent(kq, NULL, 0, &unused, 1, NULL);
250 log_err(1, "kevent");
254 log_debugx("timeout reached");
255 assert(sleep_time > 0);
257 log_debugx("got filesystem event");
262 main_autounmountd(int argc, char **argv)
267 const char *pidfile_path = AUTOUNMOUNTD_PIDFILE;
268 int ch, debug = 0, error, kq;
269 time_t expiration_time = 600, retry_time = 600, mounted_max, sleep_time;
270 bool dont_daemonize = false;
272 while ((ch = getopt(argc, argv, "dr:t:v")) != -1) {
275 dont_daemonize = true;
279 retry_time = atoi(optarg);
282 expiration_time = atoi(optarg);
289 usage_autounmountd();
294 usage_autounmountd();
297 log_errx(1, "retry time must be greater than zero");
298 if (expiration_time <= 0)
299 log_errx(1, "expiration time must be greater than zero");
303 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
305 if (errno == EEXIST) {
306 log_errx(1, "daemon already running, pid: %jd.",
309 log_err(1, "cannot open or create pidfile \"%s\"",
313 if (dont_daemonize == false) {
314 if (daemon(0, 0) == -1) {
315 log_warn("cannot daemonize");
316 pidfile_remove(pidfh);
321 pidfile_write(pidfh);
323 TAILQ_INIT(&automounted);
327 log_err(1, "kqueue");
329 EV_SET(&event, 0, EVFILT_FS, EV_ADD | EV_CLEAR, 0, 0, NULL);
330 error = kevent(kq, &event, 1, NULL, 0, NULL);
332 log_err(1, "kevent");
335 refresh_automounted();
336 mounted_max = expire_automounted(expiration_time);
337 if (mounted_max == -1) {
338 sleep_time = mounted_max;
339 log_debugx("no filesystems to expire");
340 } else if (mounted_max < expiration_time) {
341 sleep_time = difftime(expiration_time, mounted_max);
342 log_debugx("some filesystems expire in %jd seconds",
343 (intmax_t)sleep_time);
345 sleep_time = retry_time;
346 log_debugx("some expired filesystems remain mounted, "
347 "will retry in %jd seconds", (intmax_t)sleep_time);
350 do_wait(kq, sleep_time);