autofs.c revision 1.1 1 1.1 christos /* $NetBSD: autofs.c,v 1.1 2018/01/09 03:31:14 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 1.1 christos * Copyright (c) 2016 The DragonFly Project
6 1.1 christos * Copyright (c) 2014 The FreeBSD Foundation
7 1.1 christos * All rights reserved.
8 1.1 christos *
9 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
10 1.1 christos * by Tomohiro Kusumi <kusumi.tomohiro (at) gmail.com>.
11 1.1 christos *
12 1.1 christos * This software was developed by Edward Tomasz Napierala under sponsorship
13 1.1 christos * from the FreeBSD Foundation.
14 1.1 christos *
15 1.1 christos * Redistribution and use in source and binary forms, with or without
16 1.1 christos * modification, are permitted provided that the following conditions
17 1.1 christos * are met:
18 1.1 christos * 1. Redistributions of source code must retain the above copyright
19 1.1 christos * notice, this list of conditions and the following disclaimer.
20 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
21 1.1 christos * notice, this list of conditions and the following disclaimer in the
22 1.1 christos * documentation and/or other materials provided with the distribution.
23 1.1 christos *
24 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 christos * SUCH DAMAGE.
35 1.1 christos *
36 1.1 christos */
37 1.1 christos /*-
38 1.1 christos * Copyright (c) 1989, 1991, 1993, 1995
39 1.1 christos * The Regents of the University of California. All rights reserved.
40 1.1 christos *
41 1.1 christos * This code is derived from software contributed to Berkeley by
42 1.1 christos * Rick Macklem at The University of Guelph.
43 1.1 christos *
44 1.1 christos * Redistribution and use in source and binary forms, with or without
45 1.1 christos * modification, are permitted provided that the following conditions
46 1.1 christos * are met:
47 1.1 christos * 1. Redistributions of source code must retain the above copyright
48 1.1 christos * notice, this list of conditions and the following disclaimer.
49 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 christos * notice, this list of conditions and the following disclaimer in the
51 1.1 christos * documentation and/or other materials provided with the distribution.
52 1.1 christos * 3. Neither the name of the University nor the names of its contributors
53 1.1 christos * may be used to endorse or promote products derived from this software
54 1.1 christos * without specific prior written permission.
55 1.1 christos *
56 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 1.1 christos * SUCH DAMAGE.
67 1.1 christos *
68 1.1 christos */
69 1.1 christos
70 1.1 christos #include <sys/cdefs.h>
71 1.1 christos __KERNEL_RCSID(0, "$NetBSD: autofs.c,v 1.1 2018/01/09 03:31:14 christos Exp $");
72 1.1 christos
73 1.1 christos #include "autofs.h"
74 1.1 christos
75 1.1 christos #include <sys/queue.h>
76 1.1 christos #include <sys/signalvar.h>
77 1.1 christos
78 1.1 christos static int autofs_open(dev_t dev, int flags, int mode, struct lwp *l);
79 1.1 christos static int autofs_close(dev_t dev, int flags, int mode, struct lwp *l);
80 1.1 christos static int autofs_ioctl(dev_t dev, const u_long cmd, void *data, int flag,
81 1.1 christos struct lwp *l);
82 1.1 christos
83 1.1 christos struct cdevsw autofs_ops = {
84 1.1 christos .d_open = autofs_open,
85 1.1 christos .d_close = autofs_close,
86 1.1 christos .d_ioctl = autofs_ioctl,
87 1.1 christos };
88 1.1 christos
89 1.1 christos /*
90 1.1 christos * List of signals that can interrupt an autofs trigger.
91 1.1 christos */
92 1.1 christos static int autofs_sig_set[] = {
93 1.1 christos SIGINT,
94 1.1 christos SIGTERM,
95 1.1 christos SIGHUP,
96 1.1 christos SIGKILL,
97 1.1 christos SIGQUIT,
98 1.1 christos };
99 1.1 christos
100 1.1 christos struct pool autofs_request_pool;
101 1.1 christos struct pool autofs_node_pool;
102 1.1 christos struct autofs_softc *autofs_softc = NULL;
103 1.1 christos struct workqueue *autofs_tmo_wq = NULL;
104 1.1 christos
105 1.1 christos int autofs_debug = 1;
106 1.1 christos int autofs_mount_on_stat = 0;
107 1.1 christos int autofs_timeout = 30;
108 1.1 christos int autofs_cache = 600;
109 1.1 christos int autofs_retry_attempts = 3;
110 1.1 christos int autofs_retry_delay = 1;
111 1.1 christos int autofs_interruptible = 1;
112 1.1 christos
113 1.1 christos static int
114 1.1 christos autofs_node_cmp(const struct autofs_node *a, const struct autofs_node *b)
115 1.1 christos {
116 1.1 christos
117 1.1 christos return strcmp(a->an_name, b->an_name);
118 1.1 christos }
119 1.1 christos
120 1.1 christos RB_GENERATE(autofs_node_tree, autofs_node, an_entry, autofs_node_cmp);
121 1.1 christos
122 1.1 christos bool
123 1.1 christos autofs_ignore_thread(void)
124 1.1 christos {
125 1.1 christos
126 1.1 christos if (autofs_softc->sc_dev_opened == false)
127 1.1 christos return false;
128 1.1 christos
129 1.1 christos mutex_enter(proc_lock);
130 1.1 christos if (autofs_softc->sc_dev_sid == curproc->p_pgrp->pg_id) {
131 1.1 christos mutex_exit(proc_lock);
132 1.1 christos return true;
133 1.1 christos }
134 1.1 christos mutex_exit(proc_lock);
135 1.1 christos
136 1.1 christos return false;
137 1.1 christos }
138 1.1 christos
139 1.1 christos static char *
140 1.1 christos autofs_path(struct autofs_node *anp)
141 1.1 christos {
142 1.1 christos struct autofs_mount *amp = anp->an_mount;
143 1.1 christos size_t len;
144 1.1 christos char *path, *tmp;
145 1.1 christos
146 1.1 christos path = kmem_strdup("", KM_SLEEP);
147 1.1 christos for (; anp->an_parent; anp = anp->an_parent) {
148 1.1 christos len = strlen(anp->an_name) + strlen(path) + 2;
149 1.1 christos tmp = kmem_alloc(len, KM_SLEEP);
150 1.1 christos snprintf(tmp, len, "%s/%s", anp->an_name, path);
151 1.1 christos kmem_strfree(path);
152 1.1 christos path = tmp;
153 1.1 christos }
154 1.1 christos
155 1.1 christos len = strlen(amp->am_on) + strlen(path) + 2;
156 1.1 christos tmp = kmem_alloc(len, KM_SLEEP);
157 1.1 christos snprintf(tmp, len, "%s/%s", amp->am_on, path);
158 1.1 christos kmem_strfree(path);
159 1.1 christos
160 1.1 christos return tmp;
161 1.1 christos }
162 1.1 christos
163 1.1 christos void
164 1.1 christos autofs_timeout_wq(struct work *wk, void *arg)
165 1.1 christos {
166 1.1 christos struct autofs_request *ar = (void *)wk;
167 1.1 christos
168 1.1 christos mutex_enter(&autofs_softc->sc_lock);
169 1.1 christos AUTOFS_WARN("request %d for %s timed out after %d seconds",
170 1.1 christos ar->ar_id, ar->ar_path, autofs_timeout);
171 1.1 christos
172 1.1 christos ar->ar_error = ETIMEDOUT;
173 1.1 christos ar->ar_wildcards = true;
174 1.1 christos ar->ar_done = true;
175 1.1 christos ar->ar_in_progress = false;
176 1.1 christos cv_broadcast(&autofs_softc->sc_cv);
177 1.1 christos mutex_exit(&autofs_softc->sc_lock);
178 1.1 christos }
179 1.1 christos
180 1.1 christos static void
181 1.1 christos autofs_timeout_callout(void *context)
182 1.1 christos {
183 1.1 christos struct autofs_request *ar = context;
184 1.1 christos
185 1.1 christos workqueue_enqueue(autofs_tmo_wq, &ar->ar_wk, NULL);
186 1.1 christos }
187 1.1 christos
188 1.1 christos bool
189 1.1 christos autofs_cached(struct autofs_node *anp, const char *component, int componentlen)
190 1.1 christos {
191 1.1 christos struct autofs_mount *amp = anp->an_mount;
192 1.1 christos
193 1.1 christos KASSERT(!mutex_owned(&->am_lock));
194 1.1 christos
195 1.1 christos /*
196 1.1 christos * For root node we need to request automountd(8) assistance even
197 1.1 christos * if the node is marked as cached, but the requested top-level
198 1.1 christos * directory does not exist. This is necessary for wildcard indirect
199 1.1 christos * map keys to work. We don't do this if we know that there are
200 1.1 christos * no wildcards.
201 1.1 christos */
202 1.1 christos if (!anp->an_parent && componentlen && anp->an_wildcards) {
203 1.1 christos int error;
204 1.1 christos KASSERT(amp->am_root == anp);
205 1.1 christos mutex_enter(&->am_lock);
206 1.1 christos error = autofs_node_find(anp, component, componentlen, NULL);
207 1.1 christos mutex_exit(&->am_lock);
208 1.1 christos if (error)
209 1.1 christos return false;
210 1.1 christos }
211 1.1 christos
212 1.1 christos return anp->an_cached;
213 1.1 christos }
214 1.1 christos
215 1.1 christos static void
216 1.1 christos autofs_cache_callout(void *context)
217 1.1 christos {
218 1.1 christos struct autofs_node *anp = context;
219 1.1 christos
220 1.1 christos autofs_node_uncache(anp);
221 1.1 christos }
222 1.1 christos
223 1.1 christos void
224 1.1 christos autofs_flush(struct autofs_mount *amp)
225 1.1 christos {
226 1.1 christos struct autofs_node *anp = amp->am_root;
227 1.1 christos struct autofs_node *child;
228 1.1 christos
229 1.1 christos mutex_enter(&->am_lock);
230 1.1 christos RB_FOREACH(child, autofs_node_tree, &anp->an_children) {
231 1.1 christos autofs_node_uncache(child);
232 1.1 christos }
233 1.1 christos autofs_node_uncache(amp->am_root);
234 1.1 christos mutex_exit(&->am_lock);
235 1.1 christos
236 1.1 christos AUTOFS_DEBUG("%s flushed", amp->am_on);
237 1.1 christos }
238 1.1 christos
239 1.1 christos /*
240 1.1 christos * The set/restore sigmask functions are used to (temporarily) overwrite
241 1.1 christos * the thread sigmask during triggering.
242 1.1 christos */
243 1.1 christos static void
244 1.1 christos autofs_set_sigmask(sigset_t *oldset)
245 1.1 christos {
246 1.1 christos sigset_t newset;
247 1.1 christos int i;
248 1.1 christos
249 1.1 christos sigfillset(&newset);
250 1.1 christos /* Remove the autofs set of signals from newset */
251 1.1 christos mutex_enter(proc_lock);
252 1.1 christos mutex_enter(curproc->p_lock);
253 1.1 christos
254 1.1 christos for (i = 0; i < __arraycount(autofs_sig_set); i++) {
255 1.1 christos /*
256 1.1 christos * But make sure we leave the ones already masked
257 1.1 christos * by the process, i.e. remove the signal from the
258 1.1 christos * temporary signalmask only if it wasn't already
259 1.1 christos * in sigmask.
260 1.1 christos */
261 1.1 christos if (!sigismasked(curlwp, autofs_sig_set[i]))
262 1.1 christos sigdelset(&newset, autofs_sig_set[i]);
263 1.1 christos }
264 1.1 christos sigprocmask1(curlwp, SIG_SETMASK, &newset, oldset);
265 1.1 christos
266 1.1 christos mutex_exit(curproc->p_lock);
267 1.1 christos mutex_exit(proc_lock);
268 1.1 christos }
269 1.1 christos
270 1.1 christos static void
271 1.1 christos autofs_restore_sigmask(sigset_t *set)
272 1.1 christos {
273 1.1 christos
274 1.1 christos mutex_enter(proc_lock);
275 1.1 christos mutex_enter(curproc->p_lock);
276 1.1 christos
277 1.1 christos sigprocmask1(curlwp, SIG_SETMASK, set, NULL);
278 1.1 christos
279 1.1 christos mutex_exit(curproc->p_lock);
280 1.1 christos mutex_exit(proc_lock);
281 1.1 christos }
282 1.1 christos
283 1.1 christos static int
284 1.1 christos autofs_trigger_one(struct autofs_node *anp, const char *component,
285 1.1 christos int componentlen)
286 1.1 christos {
287 1.1 christos struct autofs_mount *amp = anp->an_mount;
288 1.1 christos struct autofs_request *ar;
289 1.1 christos char *key, *path;
290 1.1 christos int error = 0, request_error;
291 1.1 christos bool wildcards;
292 1.1 christos
293 1.1 christos KASSERT(mutex_owned(&autofs_softc->sc_lock));
294 1.1 christos
295 1.1 christos if (!anp->an_parent) {
296 1.1 christos key = autofs_strndup(component, componentlen, KM_SLEEP);
297 1.1 christos } else {
298 1.1 christos struct autofs_node *firstanp;
299 1.1 christos for (firstanp = anp; firstanp->an_parent->an_parent;
300 1.1 christos firstanp = firstanp->an_parent)
301 1.1 christos continue;
302 1.1 christos key = kmem_strdup(firstanp->an_name, KM_SLEEP);
303 1.1 christos }
304 1.1 christos
305 1.1 christos path = autofs_path(anp);
306 1.1 christos
307 1.1 christos TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
308 1.1 christos if (strcmp(ar->ar_path, path))
309 1.1 christos continue;
310 1.1 christos if (strcmp(ar->ar_key, key))
311 1.1 christos continue;
312 1.1 christos
313 1.1 christos KASSERT(!strcmp(ar->ar_from, amp->am_from));
314 1.1 christos KASSERT(!strcmp(ar->ar_prefix, amp->am_prefix));
315 1.1 christos KASSERT(!strcmp(ar->ar_options, amp->am_options));
316 1.1 christos break;
317 1.1 christos }
318 1.1 christos
319 1.1 christos if (ar) {
320 1.1 christos atomic_add_int(&ar->ar_refcount, 1);
321 1.1 christos } else {
322 1.1 christos ar = pool_get(&autofs_request_pool, PR_WAITOK);
323 1.1 christos ar->ar_mount = amp;
324 1.1 christos ar->ar_id = autofs_softc->sc_last_request_id++;
325 1.1 christos ar->ar_done = false;
326 1.1 christos ar->ar_error = 0;
327 1.1 christos ar->ar_wildcards = false;
328 1.1 christos ar->ar_in_progress = false;
329 1.1 christos strlcpy(ar->ar_from, amp->am_from, sizeof(ar->ar_from));
330 1.1 christos strlcpy(ar->ar_path, path, sizeof(ar->ar_path));
331 1.1 christos strlcpy(ar->ar_prefix, amp->am_prefix, sizeof(ar->ar_prefix));
332 1.1 christos strlcpy(ar->ar_key, key, sizeof(ar->ar_key));
333 1.1 christos strlcpy(ar->ar_options,
334 1.1 christos amp->am_options, sizeof(ar->ar_options));
335 1.1 christos
336 1.1 christos callout_init(&ar->ar_callout, 0);
337 1.1 christos callout_reset(&ar->ar_callout, autofs_timeout * hz,
338 1.1 christos autofs_timeout_callout, ar);
339 1.1 christos ar->ar_refcount = 1;
340 1.1 christos TAILQ_INSERT_TAIL(&autofs_softc->sc_requests, ar, ar_next);
341 1.1 christos }
342 1.1 christos
343 1.1 christos cv_broadcast(&autofs_softc->sc_cv);
344 1.1 christos while (ar->ar_done == false) {
345 1.1 christos if (autofs_interruptible) {
346 1.1 christos sigset_t oldset;
347 1.1 christos autofs_set_sigmask(&oldset);
348 1.1 christos error = cv_wait_sig(&autofs_softc->sc_cv,
349 1.1 christos &autofs_softc->sc_lock);
350 1.1 christos autofs_restore_sigmask(&oldset);
351 1.1 christos if (error) {
352 1.1 christos AUTOFS_WARN("cv_wait_sig for %s failed "
353 1.1 christos "with error %d", ar->ar_path, error);
354 1.1 christos break;
355 1.1 christos }
356 1.1 christos } else {
357 1.1 christos cv_wait(&autofs_softc->sc_cv, &autofs_softc->sc_lock);
358 1.1 christos }
359 1.1 christos }
360 1.1 christos
361 1.1 christos request_error = ar->ar_error;
362 1.1 christos if (request_error)
363 1.1 christos AUTOFS_WARN("request for %s completed with error %d",
364 1.1 christos ar->ar_path, request_error);
365 1.1 christos
366 1.1 christos wildcards = ar->ar_wildcards;
367 1.1 christos
368 1.1 christos /*
369 1.1 christos * Check if this is the last reference.
370 1.1 christos */
371 1.1 christos if (!atomic_add_int_nv(&ar->ar_refcount, -1)) {
372 1.1 christos TAILQ_REMOVE(&autofs_softc->sc_requests, ar, ar_next);
373 1.1 christos mutex_exit(&autofs_softc->sc_lock);
374 1.1 christos callout_halt(&ar->ar_callout, NULL);
375 1.1 christos pool_put(&autofs_request_pool, ar);
376 1.1 christos mutex_enter(&autofs_softc->sc_lock);
377 1.1 christos }
378 1.1 christos
379 1.1 christos /*
380 1.1 christos * Note that we do not do negative caching on purpose. This
381 1.1 christos * way the user can retry access at any time, e.g. after fixing
382 1.1 christos * the failure reason, without waiting for cache timer to expire.
383 1.1 christos */
384 1.1 christos if (!error && !request_error && autofs_cache > 0) {
385 1.1 christos autofs_node_cache(anp);
386 1.1 christos anp->an_wildcards = wildcards;
387 1.1 christos callout_reset(&anp->an_callout, autofs_cache * hz,
388 1.1 christos autofs_cache_callout, anp);
389 1.1 christos }
390 1.1 christos
391 1.1 christos kmem_strfree(key);
392 1.1 christos kmem_strfree(path);
393 1.1 christos
394 1.1 christos if (error)
395 1.1 christos return error;
396 1.1 christos return request_error;
397 1.1 christos }
398 1.1 christos
399 1.1 christos int
400 1.1 christos autofs_trigger(struct autofs_node *anp, const char *component,
401 1.1 christos int componentlen)
402 1.1 christos {
403 1.1 christos for (;;) {
404 1.1 christos int error, dummy;
405 1.1 christos
406 1.1 christos error = autofs_trigger_one(anp, component, componentlen);
407 1.1 christos if (!error) {
408 1.1 christos anp->an_retries = 0;
409 1.1 christos return 0;
410 1.1 christos }
411 1.1 christos if (error == EINTR || error == ERESTART) {
412 1.1 christos AUTOFS_DEBUG("trigger interrupted by signal, "
413 1.1 christos "not retrying");
414 1.1 christos anp->an_retries = 0;
415 1.1 christos return error;
416 1.1 christos }
417 1.1 christos anp->an_retries++;
418 1.1 christos if (anp->an_retries >= autofs_retry_attempts) {
419 1.1 christos AUTOFS_DEBUG("trigger failed %d times; returning "
420 1.1 christos "error %d", anp->an_retries, error);
421 1.1 christos anp->an_retries = 0;
422 1.1 christos return error;
423 1.1 christos
424 1.1 christos }
425 1.1 christos AUTOFS_DEBUG("trigger failed with error %d; will retry in "
426 1.1 christos "%d seconds, %d attempts left", error, autofs_retry_delay,
427 1.1 christos autofs_retry_attempts - anp->an_retries);
428 1.1 christos mutex_exit(&autofs_softc->sc_lock);
429 1.1 christos tsleep(&dummy, 0, "autofs_retry", autofs_retry_delay * hz);
430 1.1 christos mutex_enter(&autofs_softc->sc_lock);
431 1.1 christos }
432 1.1 christos }
433 1.1 christos
434 1.1 christos static int
435 1.1 christos autofs_ioctl_request(struct autofs_daemon_request *adr)
436 1.1 christos {
437 1.1 christos struct autofs_request *ar;
438 1.1 christos
439 1.1 christos mutex_enter(&autofs_softc->sc_lock);
440 1.1 christos for (;;) {
441 1.1 christos int error;
442 1.1 christos TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
443 1.1 christos if (ar->ar_done)
444 1.1 christos continue;
445 1.1 christos if (ar->ar_in_progress)
446 1.1 christos continue;
447 1.1 christos break;
448 1.1 christos }
449 1.1 christos
450 1.1 christos if (ar)
451 1.1 christos break;
452 1.1 christos
453 1.1 christos error = cv_wait_sig(&autofs_softc->sc_cv,
454 1.1 christos &autofs_softc->sc_lock);
455 1.1 christos if (error) {
456 1.1 christos mutex_exit(&autofs_softc->sc_lock);
457 1.1 christos return error;
458 1.1 christos }
459 1.1 christos }
460 1.1 christos
461 1.1 christos ar->ar_in_progress = true;
462 1.1 christos mutex_exit(&autofs_softc->sc_lock);
463 1.1 christos
464 1.1 christos adr->adr_id = ar->ar_id;
465 1.1 christos strlcpy(adr->adr_from, ar->ar_from, sizeof(adr->adr_from));
466 1.1 christos strlcpy(adr->adr_path, ar->ar_path, sizeof(adr->adr_path));
467 1.1 christos strlcpy(adr->adr_prefix, ar->ar_prefix, sizeof(adr->adr_prefix));
468 1.1 christos strlcpy(adr->adr_key, ar->ar_key, sizeof(adr->adr_key));
469 1.1 christos strlcpy(adr->adr_options, ar->ar_options, sizeof(adr->adr_options));
470 1.1 christos
471 1.1 christos mutex_enter(proc_lock);
472 1.1 christos autofs_softc->sc_dev_sid = curproc->p_pgrp->pg_id;
473 1.1 christos mutex_exit(proc_lock);
474 1.1 christos
475 1.1 christos return 0;
476 1.1 christos }
477 1.1 christos
478 1.1 christos static int
479 1.1 christos autofs_ioctl_done(struct autofs_daemon_done *add)
480 1.1 christos {
481 1.1 christos struct autofs_request *ar;
482 1.1 christos
483 1.1 christos mutex_enter(&autofs_softc->sc_lock);
484 1.1 christos TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) {
485 1.1 christos if (ar->ar_id == add->add_id)
486 1.1 christos break;
487 1.1 christos }
488 1.1 christos
489 1.1 christos if (!ar) {
490 1.1 christos mutex_exit(&autofs_softc->sc_lock);
491 1.1 christos AUTOFS_DEBUG("id %d not found", add->add_id);
492 1.1 christos return ESRCH;
493 1.1 christos }
494 1.1 christos
495 1.1 christos ar->ar_error = add->add_error;
496 1.1 christos ar->ar_wildcards = add->add_wildcards;
497 1.1 christos ar->ar_done = true;
498 1.1 christos ar->ar_in_progress = false;
499 1.1 christos cv_broadcast(&autofs_softc->sc_cv);
500 1.1 christos
501 1.1 christos mutex_exit(&autofs_softc->sc_lock);
502 1.1 christos
503 1.1 christos return 0;
504 1.1 christos }
505 1.1 christos
506 1.1 christos static int
507 1.1 christos autofs_open(dev_t dev, int flags, int mode, struct lwp *l)
508 1.1 christos {
509 1.1 christos
510 1.1 christos mutex_enter(&autofs_softc->sc_lock);
511 1.1 christos /*
512 1.1 christos * We must never block automountd(8) and its descendants, and we use
513 1.1 christos * session ID to determine that: we store session id of the process
514 1.1 christos * that opened the device, and then compare it with session ids
515 1.1 christos * of triggering processes. This means running a second automountd(8)
516 1.1 christos * instance would break the previous one. The check below prevents
517 1.1 christos * it from happening.
518 1.1 christos */
519 1.1 christos if (autofs_softc->sc_dev_opened) {
520 1.1 christos mutex_exit(&autofs_softc->sc_lock);
521 1.1 christos return EBUSY;
522 1.1 christos }
523 1.1 christos
524 1.1 christos autofs_softc->sc_dev_opened = true;
525 1.1 christos mutex_exit(&autofs_softc->sc_lock);
526 1.1 christos
527 1.1 christos return 0;
528 1.1 christos }
529 1.1 christos
530 1.1 christos static int
531 1.1 christos autofs_close(dev_t dev, int flags, int mode, struct lwp *l)
532 1.1 christos {
533 1.1 christos
534 1.1 christos mutex_enter(&autofs_softc->sc_lock);
535 1.1 christos KASSERT(autofs_softc->sc_dev_opened);
536 1.1 christos autofs_softc->sc_dev_opened = false;
537 1.1 christos mutex_exit(&autofs_softc->sc_lock);
538 1.1 christos
539 1.1 christos return 0;
540 1.1 christos }
541 1.1 christos
542 1.1 christos static int
543 1.1 christos autofs_ioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l)
544 1.1 christos {
545 1.1 christos
546 1.1 christos KASSERT(autofs_softc->sc_dev_opened);
547 1.1 christos
548 1.1 christos switch (cmd) {
549 1.1 christos case AUTOFSREQUEST:
550 1.1 christos return autofs_ioctl_request(
551 1.1 christos (struct autofs_daemon_request *)data);
552 1.1 christos case AUTOFSDONE:
553 1.1 christos return autofs_ioctl_done(
554 1.1 christos (struct autofs_daemon_done *)data);
555 1.1 christos default:
556 1.1 christos AUTOFS_DEBUG("invalid cmd %lx", cmd);
557 1.1 christos return EINVAL;
558 1.1 christos }
559 1.1 christos return EINVAL;
560 1.1 christos }
561