2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/event.h>
38 #include <sys/mount.h>
52 #define AUTOUNMOUNTD_PIDFILE "/var/run/autounmountd.pid"
54 struct automounted_fs {
55 TAILQ_ENTRY(automounted_fs) af_next;
59 char af_mountpoint[MNAMELEN];
62 static TAILQ_HEAD(, automounted_fs) automounted;
64 static struct automounted_fs *
65 automounted_find(fsid_t fsid)
67 struct automounted_fs *af;
69 TAILQ_FOREACH(af, &automounted, af_next) {
70 if (fsidcmp(&af->af_fsid, &fsid) == 0)
77 static struct automounted_fs *
78 automounted_add(fsid_t fsid, const char *mountpoint)
80 struct automounted_fs *af;
82 af = calloc(1, sizeof(*af));
85 af->af_mount_time = time(NULL);
87 strlcpy(af->af_mountpoint, mountpoint, sizeof(af->af_mountpoint));
89 TAILQ_INSERT_TAIL(&automounted, af, af_next);
95 automounted_remove(struct automounted_fs *af)
98 TAILQ_REMOVE(&automounted, af, af_next);
103 refresh_automounted(void)
105 struct automounted_fs *af, *tmpaf;
106 struct statfs *mntbuf;
109 nitems = getmntinfo(&mntbuf, MNT_WAIT);
111 log_err(1, "getmntinfo");
113 log_debugx("refreshing list of automounted filesystems");
115 TAILQ_FOREACH(af, &automounted, af_next)
118 for (i = 0; i < nitems; i++) {
119 if (strcmp(mntbuf[i].f_fstypename, "autofs") == 0) {
120 log_debugx("skipping %s, filesystem type is autofs",
121 mntbuf[i].f_mntonname);
125 if ((mntbuf[i].f_flags & MNT_AUTOMOUNTED) == 0) {
126 log_debugx("skipping %s, not automounted",
127 mntbuf[i].f_mntonname);
131 af = automounted_find(mntbuf[i].f_fsid);
133 log_debugx("new automounted filesystem found on %s "
134 "(FSID:%d:%d)", mntbuf[i].f_mntonname,
135 mntbuf[i].f_fsid.val[0], mntbuf[i].f_fsid.val[1]);
136 af = automounted_add(mntbuf[i].f_fsid,
137 mntbuf[i].f_mntonname);
139 log_debugx("already known automounted filesystem "
140 "found on %s (FSID:%d:%d)", mntbuf[i].f_mntonname,
141 mntbuf[i].f_fsid.val[0], mntbuf[i].f_fsid.val[1]);
146 TAILQ_FOREACH_SAFE(af, &automounted, af_next, tmpaf) {
149 log_debugx("lost filesystem mounted on %s (FSID:%d:%d)",
150 af->af_mountpoint, af->af_fsid.val[0], af->af_fsid.val[1]);
151 automounted_remove(af);
156 unmount_by_fsid(const fsid_t fsid, const char *mountpoint)
161 ret = asprintf(&fsid_str, "FSID:%d:%d", fsid.val[0], fsid.val[1]);
163 log_err(1, "asprintf");
165 error = unmount(fsid_str, MNT_NONBUSY | MNT_BYFSID);
167 if (errno == EBUSY) {
168 log_debugx("cannot unmount %s (%s): %s",
169 mountpoint, fsid_str, strerror(errno));
171 log_warn("cannot unmount %s (%s)",
172 mountpoint, fsid_str);
182 expire_automounted(time_t expiration_time)
184 struct automounted_fs *af, *tmpaf;
186 time_t mounted_for, mounted_max = -1;
191 log_debugx("expiring automounted filesystems");
193 TAILQ_FOREACH_SAFE(af, &automounted, af_next, tmpaf) {
194 mounted_for = difftime(now, af->af_mount_time);
196 if (mounted_for < expiration_time) {
197 log_debugx("skipping %s (FSID:%d:%d), mounted "
198 "for %jd seconds", af->af_mountpoint,
199 af->af_fsid.val[0], af->af_fsid.val[1],
200 (intmax_t)mounted_for);
202 if (mounted_for > mounted_max)
203 mounted_max = mounted_for;
208 log_debugx("filesystem mounted on %s (FSID:%d:%d), "
209 "was mounted for %ld seconds; unmounting",
210 af->af_mountpoint, af->af_fsid.val[0], af->af_fsid.val[1],
212 error = unmount_by_fsid(af->af_fsid, af->af_mountpoint);
214 if (mounted_for > mounted_max)
215 mounted_max = mounted_for;
219 return (mounted_max);
223 usage_autounmountd(void)
226 fprintf(stderr, "usage: autounmountd [-r time][-t time][-dv]\n");
231 do_wait(int kq, time_t sleep_time)
233 struct timespec timeout;
234 struct kevent unused;
237 if (sleep_time != -1) {
238 assert(sleep_time > 0);
239 timeout.tv_sec = sleep_time;
242 log_debugx("waiting for filesystem event for %ld seconds",
244 nevents = kevent(kq, NULL, 0, &unused, 1, &timeout);
246 log_debugx("waiting for filesystem event");
247 nevents = kevent(kq, NULL, 0, &unused, 1, NULL);
252 log_err(1, "kevent");
256 log_debugx("timeout reached");
257 assert(sleep_time > 0);
259 log_debugx("got filesystem event");
264 main_autounmountd(int argc, char **argv)
269 const char *pidfile_path = AUTOUNMOUNTD_PIDFILE;
270 int ch, debug = 0, error, kq;
271 time_t expiration_time = 600, retry_time = 600, mounted_max, sleep_time;
272 bool dont_daemonize = false;
274 while ((ch = getopt(argc, argv, "dr:t:v")) != -1) {
277 dont_daemonize = true;
281 retry_time = atoi(optarg);
284 expiration_time = atoi(optarg);
291 usage_autounmountd();
296 usage_autounmountd();
299 log_errx(1, "retry time must be greater than zero");
300 if (expiration_time <= 0)
301 log_errx(1, "expiration time must be greater than zero");
305 pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
307 if (errno == EEXIST) {
308 log_errx(1, "daemon already running, pid: %jd.",
311 log_err(1, "cannot open or create pidfile \"%s\"",
315 if (dont_daemonize == false) {
316 if (daemon(0, 0) == -1) {
317 log_warn("cannot daemonize");
318 pidfile_remove(pidfh);
323 pidfile_write(pidfh);
325 TAILQ_INIT(&automounted);
329 log_err(1, "kqueue");
331 EV_SET(&event, 0, EVFILT_FS, EV_ADD | EV_CLEAR, 0, 0, NULL);
332 error = kevent(kq, &event, 1, NULL, 0, NULL);
334 log_err(1, "kevent");
337 refresh_automounted();
338 mounted_max = expire_automounted(expiration_time);
339 if (mounted_max == -1) {
340 sleep_time = mounted_max;
341 log_debugx("no filesystems to expire");
342 } else if (mounted_max < expiration_time) {
343 sleep_time = difftime(expiration_time, mounted_max);
344 log_debugx("some filesystems expire in %ld seconds",
347 sleep_time = retry_time;
348 log_debugx("some expired filesystems remain mounted, "
349 "will retry in %ld seconds", (long)sleep_time);
352 do_wait(kq, sleep_time);