aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/autofs/autounmountd.c
blob: 57375f04d7432f0ba5308589a1a31d7be3fb0f7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2014 The FreeBSD Foundation
 *
 * This software was developed by Edward Tomasz Napierala under sponsorship
 * from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/event.h>
#include <sys/mount.h>
#include <sys/time.h>
#include <assert.h>
#include <errno.h>
#include <libutil.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "common.h"

#define AUTOUNMOUNTD_PIDFILE	"/var/run/autounmountd.pid"

struct automounted_fs {
	TAILQ_ENTRY(automounted_fs)	af_next;
	time_t				af_mount_time;
	bool				af_mark;
	fsid_t				af_fsid;
	char				af_mountpoint[MNAMELEN];
};

static TAILQ_HEAD(, automounted_fs)	automounted;

static struct automounted_fs *
automounted_find(fsid_t fsid)
{
	struct automounted_fs *af;

	TAILQ_FOREACH(af, &automounted, af_next) {
		if (fsidcmp(&af->af_fsid, &fsid) == 0)
			return (af);
	}

	return (NULL);
}

static struct automounted_fs *
automounted_add(fsid_t fsid, const char *mountpoint)
{
	struct automounted_fs *af;

	af = calloc(1, sizeof(*af));
	if (af == NULL)
		log_err(1, "calloc");
	af->af_mount_time = time(NULL);
	af->af_fsid = fsid;
	strlcpy(af->af_mountpoint, mountpoint, sizeof(af->af_mountpoint));

	TAILQ_INSERT_TAIL(&automounted, af, af_next);

	return (af);
}

static void
automounted_remove(struct automounted_fs *af)
{

	TAILQ_REMOVE(&automounted, af, af_next);
	free(af);
}

static void
refresh_automounted(void)
{
	struct automounted_fs *af, *tmpaf;
	struct statfs *mntbuf;
	int i, nitems;

	nitems = getmntinfo(&mntbuf, MNT_WAIT);
	if (nitems <= 0)
		log_err(1, "getmntinfo");

	log_debugx("refreshing list of automounted filesystems");

	TAILQ_FOREACH(af, &automounted, af_next)
		af->af_mark = false;

	for (i = 0; i < nitems; i++) {
		if (strcmp(mntbuf[i].f_fstypename, "autofs") == 0) {
			log_debugx("skipping %s, filesystem type is autofs",
			    mntbuf[i].f_mntonname);
			continue;
		}

		if ((mntbuf[i].f_flags & MNT_AUTOMOUNTED) == 0) {
			log_debugx("skipping %s, not automounted",
			    mntbuf[i].f_mntonname);
			continue;
		}

		af = automounted_find(mntbuf[i].f_fsid);
		if (af == NULL) {
			log_debugx("new automounted filesystem found on %s "
			    "(FSID:%d:%d)", mntbuf[i].f_mntonname,
			    mntbuf[i].f_fsid.val[0], mntbuf[i].f_fsid.val[1]);
			af = automounted_add(mntbuf[i].f_fsid,
			    mntbuf[i].f_mntonname);
		} else {
			log_debugx("already known automounted filesystem "
			    "found on %s (FSID:%d:%d)", mntbuf[i].f_mntonname,
			    mntbuf[i].f_fsid.val[0], mntbuf[i].f_fsid.val[1]);
		}
		af->af_mark = true;
	}

	TAILQ_FOREACH_SAFE(af, &automounted, af_next, tmpaf) {
		if (af->af_mark)
			continue;
		log_debugx("lost filesystem mounted on %s (FSID:%d:%d)",
		    af->af_mountpoint, af->af_fsid.val[0], af->af_fsid.val[1]);
		automounted_remove(af);
	}
}

static int
unmount_by_fsid(const fsid_t fsid, const char *mountpoint)
{
	char *fsid_str;
	int error, ret;

	ret = asprintf(&fsid_str, "FSID:%d:%d", fsid.val[0], fsid.val[1]);
	if (ret < 0)
		log_err(1, "asprintf");

	error = unmount(fsid_str, MNT_NONBUSY | MNT_BYFSID);
	if (error != 0) {
		if (errno == EBUSY) {
			log_debugx("cannot unmount %s (%s): %s",
			    mountpoint, fsid_str, strerror(errno));
		} else {
			log_warn("cannot unmount %s (%s)",
			    mountpoint, fsid_str);
		}
	} else
		rpc_umntall();

	free(fsid_str);

	return (error);
}

static time_t
expire_automounted(time_t expiration_time)
{
	struct automounted_fs *af, *tmpaf;
	time_t now;
	time_t mounted_for, mounted_max = -1;
	int error;

	now = time(NULL);

	log_debugx("expiring automounted filesystems");

	TAILQ_FOREACH_SAFE(af, &automounted, af_next, tmpaf) {
		mounted_for = difftime(now, af->af_mount_time);

		if (mounted_for < expiration_time) {
			log_debugx("skipping %s (FSID:%d:%d), mounted "
			    "for %jd  seconds", af->af_mountpoint,
			    af->af_fsid.val[0], af->af_fsid.val[1],
			    (intmax_t)mounted_for);

			if (mounted_for > mounted_max)
				mounted_max = mounted_for;

			continue;
		}

		log_debugx("filesystem mounted on %s (FSID:%d:%d), "
		    "was mounted for %ld seconds; unmounting",
		    af->af_mountpoint, af->af_fsid.val[0], af->af_fsid.val[1],
		    (long)mounted_for);
		error = unmount_by_fsid(af->af_fsid, af->af_mountpoint);
		if (error != 0) {
			if (mounted_for > mounted_max)
				mounted_max = mounted_for;
		}
	}

	return (mounted_max);
}

static void
usage_autounmountd(void)
{

	fprintf(stderr, "usage: autounmountd [-r time][-t time][-dv]\n");
	exit(1);
}

static void
do_wait(int kq, time_t sleep_time)
{
	struct timespec timeout;
	struct kevent unused;
	int nevents;

	if (sleep_time != -1) {
		assert(sleep_time > 0);
		timeout.tv_sec = sleep_time;
		timeout.tv_nsec = 0;

		log_debugx("waiting for filesystem event for %ld seconds",
		    (long)sleep_time);
		nevents = kevent(kq, NULL, 0, &unused, 1, &timeout);
	} else {
		log_debugx("waiting for filesystem event");
		nevents = kevent(kq, NULL, 0, &unused, 1, NULL);
	}
	if (nevents < 0) {
		if (errno == EINTR)
			return;
		log_err(1, "kevent");
	}

	if (nevents == 0) {
		log_debugx("timeout reached");
		assert(sleep_time > 0);
	} else {
		log_debugx("got filesystem event");
	}
}

int
main_autounmountd(int argc, char **argv)
{
	struct kevent event;
	struct pidfh *pidfh;
	pid_t otherpid;
	const char *pidfile_path = AUTOUNMOUNTD_PIDFILE;
	int ch, debug = 0, error, kq;
	time_t expiration_time = 600, retry_time = 600, mounted_max, sleep_time;
	bool dont_daemonize = false;

	while ((ch = getopt(argc, argv, "dr:t:v")) != -1) {
		switch (ch) {
		case 'd':
			dont_daemonize = true;
			debug++;
			break;
		case 'r':
			retry_time = atoi(optarg);
			break;
		case 't':
			expiration_time = atoi(optarg);
			break;
		case 'v':
			debug++;
			break;
		case '?':
		default:
			usage_autounmountd();
		}
	}
	argc -= optind;
	if (argc != 0)
		usage_autounmountd();

	if (retry_time <= 0)
		log_errx(1, "retry time must be greater than zero");
	if (expiration_time <= 0)
		log_errx(1, "expiration time must be greater than zero");

	log_init(debug);

	pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
	if (pidfh == NULL) {
		if (errno == EEXIST) {
			log_errx(1, "daemon already running, pid: %jd.",
			    (intmax_t)otherpid);
		}
		log_err(1, "cannot open or create pidfile \"%s\"",
		    pidfile_path);
	}

	if (dont_daemonize == false) {
		if (daemon(0, 0) == -1) {
			log_warn("cannot daemonize");
			pidfile_remove(pidfh);
			exit(1);
		}
	}

	pidfile_write(pidfh);

	TAILQ_INIT(&automounted);

	kq = kqueue();
	if (kq < 0)
		log_err(1, "kqueue");

	EV_SET(&event, 0, EVFILT_FS, EV_ADD | EV_CLEAR, VQ_MOUNT | VQ_UNMOUNT, 0, NULL);
	error = kevent(kq, &event, 1, NULL, 0, NULL);
	if (error < 0)
		log_err(1, "kevent");

	for (;;) {
		refresh_automounted();
		mounted_max = expire_automounted(expiration_time);
		if (mounted_max == -1) {
			sleep_time = mounted_max;
			log_debugx("no filesystems to expire");
		} else if (mounted_max < expiration_time) {
			sleep_time = difftime(expiration_time, mounted_max);
			log_debugx("some filesystems expire in %ld  seconds",
			    (long)sleep_time);
		} else {
			sleep_time = retry_time;
			log_debugx("some expired filesystems remain mounted, "
			    "will retry in %ld  seconds", (long)sleep_time);
		}

		do_wait(kq, sleep_time);
	}

	return (0);
}