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