1 1.6 ad /* $NetBSD: autofs.c,v 1.6 2020/05/23 23:42:43 ad 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.6 ad __KERNEL_RCSID(0, "$NetBSD: autofs.c,v 1.6 2020/05/23 23:42:43 ad Exp $"); 72 1.1 christos 73 1.1 christos #include "autofs.h" 74 1.1 christos 75 1.3 christos #include "ioconf.h" 76 1.3 christos 77 1.2 martin #include <sys/atomic.h> 78 1.1 christos #include <sys/queue.h> 79 1.1 christos #include <sys/signalvar.h> 80 1.1 christos 81 1.4 tkusumi static dev_type_open(autofs_open); 82 1.4 tkusumi static dev_type_close(autofs_close); 83 1.4 tkusumi static dev_type_ioctl(autofs_ioctl); 84 1.3 christos 85 1.3 christos const struct cdevsw autofs_cdevsw = { 86 1.3 christos .d_open = autofs_open, 87 1.3 christos .d_close = autofs_close, 88 1.3 christos .d_read = noread, 89 1.3 christos .d_write = nowrite, 90 1.3 christos .d_ioctl = autofs_ioctl, 91 1.3 christos .d_stop = nostop, 92 1.3 christos .d_tty = notty, 93 1.3 christos .d_poll = nopoll, 94 1.3 christos .d_mmap = nommap, 95 1.3 christos .d_kqfilter = nokqfilter, 96 1.3 christos .d_discard = nodiscard, 97 1.3 christos .d_flag = D_OTHER, 98 1.1 christos }; 99 1.1 christos 100 1.1 christos /* 101 1.1 christos * List of signals that can interrupt an autofs trigger. 102 1.1 christos */ 103 1.1 christos static int autofs_sig_set[] = { 104 1.1 christos SIGINT, 105 1.1 christos SIGTERM, 106 1.1 christos SIGHUP, 107 1.1 christos SIGKILL, 108 1.1 christos SIGQUIT, 109 1.1 christos }; 110 1.1 christos 111 1.1 christos struct pool autofs_request_pool; 112 1.1 christos struct pool autofs_node_pool; 113 1.1 christos struct autofs_softc *autofs_softc = NULL; 114 1.1 christos struct workqueue *autofs_tmo_wq = NULL; 115 1.1 christos 116 1.1 christos int autofs_debug = 1; 117 1.1 christos int autofs_mount_on_stat = 0; 118 1.1 christos int autofs_timeout = 30; 119 1.1 christos int autofs_cache = 600; 120 1.1 christos int autofs_retry_attempts = 3; 121 1.1 christos int autofs_retry_delay = 1; 122 1.1 christos int autofs_interruptible = 1; 123 1.1 christos 124 1.3 christos void 125 1.3 christos autofsattach(int n) 126 1.3 christos { 127 1.3 christos } 128 1.3 christos 129 1.1 christos static int 130 1.1 christos autofs_node_cmp(const struct autofs_node *a, const struct autofs_node *b) 131 1.1 christos { 132 1.1 christos 133 1.1 christos return strcmp(a->an_name, b->an_name); 134 1.1 christos } 135 1.1 christos 136 1.1 christos RB_GENERATE(autofs_node_tree, autofs_node, an_entry, autofs_node_cmp); 137 1.1 christos 138 1.1 christos bool 139 1.1 christos autofs_ignore_thread(void) 140 1.1 christos { 141 1.1 christos 142 1.1 christos if (autofs_softc->sc_dev_opened == false) 143 1.1 christos return false; 144 1.1 christos 145 1.6 ad mutex_enter(&proc_lock); 146 1.1 christos if (autofs_softc->sc_dev_sid == curproc->p_pgrp->pg_id) { 147 1.6 ad mutex_exit(&proc_lock); 148 1.1 christos return true; 149 1.1 christos } 150 1.6 ad mutex_exit(&proc_lock); 151 1.1 christos 152 1.1 christos return false; 153 1.1 christos } 154 1.1 christos 155 1.1 christos static char * 156 1.1 christos autofs_path(struct autofs_node *anp) 157 1.1 christos { 158 1.1 christos struct autofs_mount *amp = anp->an_mount; 159 1.1 christos size_t len; 160 1.1 christos char *path, *tmp; 161 1.1 christos 162 1.1 christos path = kmem_strdup("", KM_SLEEP); 163 1.1 christos for (; anp->an_parent; anp = anp->an_parent) { 164 1.1 christos len = strlen(anp->an_name) + strlen(path) + 2; 165 1.1 christos tmp = kmem_alloc(len, KM_SLEEP); 166 1.1 christos snprintf(tmp, len, "%s/%s", anp->an_name, path); 167 1.1 christos kmem_strfree(path); 168 1.1 christos path = tmp; 169 1.1 christos } 170 1.1 christos 171 1.1 christos len = strlen(amp->am_on) + strlen(path) + 2; 172 1.1 christos tmp = kmem_alloc(len, KM_SLEEP); 173 1.1 christos snprintf(tmp, len, "%s/%s", amp->am_on, path); 174 1.1 christos kmem_strfree(path); 175 1.1 christos 176 1.1 christos return tmp; 177 1.1 christos } 178 1.1 christos 179 1.1 christos void 180 1.1 christos autofs_timeout_wq(struct work *wk, void *arg) 181 1.1 christos { 182 1.1 christos struct autofs_request *ar = (void *)wk; 183 1.1 christos 184 1.1 christos mutex_enter(&autofs_softc->sc_lock); 185 1.1 christos AUTOFS_WARN("request %d for %s timed out after %d seconds", 186 1.1 christos ar->ar_id, ar->ar_path, autofs_timeout); 187 1.1 christos 188 1.1 christos ar->ar_error = ETIMEDOUT; 189 1.1 christos ar->ar_wildcards = true; 190 1.1 christos ar->ar_done = true; 191 1.1 christos ar->ar_in_progress = false; 192 1.1 christos cv_broadcast(&autofs_softc->sc_cv); 193 1.1 christos mutex_exit(&autofs_softc->sc_lock); 194 1.1 christos } 195 1.1 christos 196 1.1 christos static void 197 1.1 christos autofs_timeout_callout(void *context) 198 1.1 christos { 199 1.1 christos struct autofs_request *ar = context; 200 1.1 christos 201 1.1 christos workqueue_enqueue(autofs_tmo_wq, &ar->ar_wk, NULL); 202 1.1 christos } 203 1.1 christos 204 1.1 christos bool 205 1.1 christos autofs_cached(struct autofs_node *anp, const char *component, int componentlen) 206 1.1 christos { 207 1.1 christos struct autofs_mount *amp = anp->an_mount; 208 1.1 christos 209 1.1 christos KASSERT(!mutex_owned(&->am_lock)); 210 1.1 christos 211 1.1 christos /* 212 1.1 christos * For root node we need to request automountd(8) assistance even 213 1.1 christos * if the node is marked as cached, but the requested top-level 214 1.1 christos * directory does not exist. This is necessary for wildcard indirect 215 1.1 christos * map keys to work. We don't do this if we know that there are 216 1.1 christos * no wildcards. 217 1.1 christos */ 218 1.1 christos if (!anp->an_parent && componentlen && anp->an_wildcards) { 219 1.1 christos int error; 220 1.1 christos KASSERT(amp->am_root == anp); 221 1.1 christos mutex_enter(&->am_lock); 222 1.1 christos error = autofs_node_find(anp, component, componentlen, NULL); 223 1.1 christos mutex_exit(&->am_lock); 224 1.1 christos if (error) 225 1.1 christos return false; 226 1.1 christos } 227 1.1 christos 228 1.1 christos return anp->an_cached; 229 1.1 christos } 230 1.1 christos 231 1.1 christos static void 232 1.1 christos autofs_cache_callout(void *context) 233 1.1 christos { 234 1.1 christos struct autofs_node *anp = context; 235 1.1 christos 236 1.1 christos autofs_node_uncache(anp); 237 1.1 christos } 238 1.1 christos 239 1.1 christos void 240 1.1 christos autofs_flush(struct autofs_mount *amp) 241 1.1 christos { 242 1.1 christos struct autofs_node *anp = amp->am_root; 243 1.1 christos struct autofs_node *child; 244 1.1 christos 245 1.1 christos mutex_enter(&->am_lock); 246 1.1 christos RB_FOREACH(child, autofs_node_tree, &anp->an_children) { 247 1.1 christos autofs_node_uncache(child); 248 1.1 christos } 249 1.1 christos autofs_node_uncache(amp->am_root); 250 1.1 christos mutex_exit(&->am_lock); 251 1.1 christos 252 1.1 christos AUTOFS_DEBUG("%s flushed", amp->am_on); 253 1.1 christos } 254 1.1 christos 255 1.1 christos /* 256 1.1 christos * The set/restore sigmask functions are used to (temporarily) overwrite 257 1.1 christos * the thread sigmask during triggering. 258 1.1 christos */ 259 1.1 christos static void 260 1.1 christos autofs_set_sigmask(sigset_t *oldset) 261 1.1 christos { 262 1.1 christos sigset_t newset; 263 1.1 christos int i; 264 1.1 christos 265 1.1 christos sigfillset(&newset); 266 1.1 christos /* Remove the autofs set of signals from newset */ 267 1.6 ad mutex_enter(&proc_lock); 268 1.1 christos mutex_enter(curproc->p_lock); 269 1.1 christos 270 1.1 christos for (i = 0; i < __arraycount(autofs_sig_set); i++) { 271 1.1 christos /* 272 1.1 christos * But make sure we leave the ones already masked 273 1.1 christos * by the process, i.e. remove the signal from the 274 1.1 christos * temporary signalmask only if it wasn't already 275 1.1 christos * in sigmask. 276 1.1 christos */ 277 1.1 christos if (!sigismasked(curlwp, autofs_sig_set[i])) 278 1.1 christos sigdelset(&newset, autofs_sig_set[i]); 279 1.1 christos } 280 1.1 christos sigprocmask1(curlwp, SIG_SETMASK, &newset, oldset); 281 1.1 christos 282 1.1 christos mutex_exit(curproc->p_lock); 283 1.6 ad mutex_exit(&proc_lock); 284 1.1 christos } 285 1.1 christos 286 1.1 christos static void 287 1.1 christos autofs_restore_sigmask(sigset_t *set) 288 1.1 christos { 289 1.1 christos 290 1.6 ad mutex_enter(&proc_lock); 291 1.1 christos mutex_enter(curproc->p_lock); 292 1.1 christos 293 1.1 christos sigprocmask1(curlwp, SIG_SETMASK, set, NULL); 294 1.1 christos 295 1.1 christos mutex_exit(curproc->p_lock); 296 1.6 ad mutex_exit(&proc_lock); 297 1.1 christos } 298 1.1 christos 299 1.1 christos static int 300 1.1 christos autofs_trigger_one(struct autofs_node *anp, const char *component, 301 1.1 christos int componentlen) 302 1.1 christos { 303 1.1 christos struct autofs_mount *amp = anp->an_mount; 304 1.1 christos struct autofs_request *ar; 305 1.1 christos char *key, *path; 306 1.1 christos int error = 0, request_error; 307 1.1 christos bool wildcards; 308 1.1 christos 309 1.1 christos KASSERT(mutex_owned(&autofs_softc->sc_lock)); 310 1.1 christos 311 1.1 christos if (!anp->an_parent) { 312 1.1 christos key = autofs_strndup(component, componentlen, KM_SLEEP); 313 1.1 christos } else { 314 1.1 christos struct autofs_node *firstanp; 315 1.1 christos for (firstanp = anp; firstanp->an_parent->an_parent; 316 1.1 christos firstanp = firstanp->an_parent) 317 1.1 christos continue; 318 1.1 christos key = kmem_strdup(firstanp->an_name, KM_SLEEP); 319 1.1 christos } 320 1.1 christos 321 1.1 christos path = autofs_path(anp); 322 1.1 christos 323 1.1 christos TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) { 324 1.1 christos if (strcmp(ar->ar_path, path)) 325 1.1 christos continue; 326 1.1 christos if (strcmp(ar->ar_key, key)) 327 1.1 christos continue; 328 1.1 christos 329 1.1 christos KASSERT(!strcmp(ar->ar_from, amp->am_from)); 330 1.1 christos KASSERT(!strcmp(ar->ar_prefix, amp->am_prefix)); 331 1.1 christos KASSERT(!strcmp(ar->ar_options, amp->am_options)); 332 1.1 christos break; 333 1.1 christos } 334 1.1 christos 335 1.1 christos if (ar) { 336 1.1 christos atomic_add_int(&ar->ar_refcount, 1); 337 1.1 christos } else { 338 1.1 christos ar = pool_get(&autofs_request_pool, PR_WAITOK); 339 1.1 christos ar->ar_mount = amp; 340 1.1 christos ar->ar_id = autofs_softc->sc_last_request_id++; 341 1.1 christos ar->ar_done = false; 342 1.1 christos ar->ar_error = 0; 343 1.1 christos ar->ar_wildcards = false; 344 1.1 christos ar->ar_in_progress = false; 345 1.1 christos strlcpy(ar->ar_from, amp->am_from, sizeof(ar->ar_from)); 346 1.1 christos strlcpy(ar->ar_path, path, sizeof(ar->ar_path)); 347 1.1 christos strlcpy(ar->ar_prefix, amp->am_prefix, sizeof(ar->ar_prefix)); 348 1.1 christos strlcpy(ar->ar_key, key, sizeof(ar->ar_key)); 349 1.1 christos strlcpy(ar->ar_options, 350 1.1 christos amp->am_options, sizeof(ar->ar_options)); 351 1.1 christos 352 1.1 christos callout_init(&ar->ar_callout, 0); 353 1.1 christos callout_reset(&ar->ar_callout, autofs_timeout * hz, 354 1.1 christos autofs_timeout_callout, ar); 355 1.1 christos ar->ar_refcount = 1; 356 1.1 christos TAILQ_INSERT_TAIL(&autofs_softc->sc_requests, ar, ar_next); 357 1.1 christos } 358 1.1 christos 359 1.1 christos cv_broadcast(&autofs_softc->sc_cv); 360 1.1 christos while (ar->ar_done == false) { 361 1.1 christos if (autofs_interruptible) { 362 1.1 christos sigset_t oldset; 363 1.1 christos autofs_set_sigmask(&oldset); 364 1.1 christos error = cv_wait_sig(&autofs_softc->sc_cv, 365 1.1 christos &autofs_softc->sc_lock); 366 1.1 christos autofs_restore_sigmask(&oldset); 367 1.1 christos if (error) { 368 1.1 christos AUTOFS_WARN("cv_wait_sig for %s failed " 369 1.1 christos "with error %d", ar->ar_path, error); 370 1.1 christos break; 371 1.1 christos } 372 1.1 christos } else { 373 1.1 christos cv_wait(&autofs_softc->sc_cv, &autofs_softc->sc_lock); 374 1.1 christos } 375 1.1 christos } 376 1.1 christos 377 1.1 christos request_error = ar->ar_error; 378 1.1 christos if (request_error) 379 1.5 tkusumi AUTOFS_WARN("request for %s completed with error %d, " 380 1.5 tkusumi "pid %d (%s)", ar->ar_path, request_error, 381 1.5 tkusumi curproc->p_pid, curproc->p_comm); 382 1.1 christos 383 1.1 christos wildcards = ar->ar_wildcards; 384 1.1 christos 385 1.1 christos /* 386 1.1 christos * Check if this is the last reference. 387 1.1 christos */ 388 1.1 christos if (!atomic_add_int_nv(&ar->ar_refcount, -1)) { 389 1.1 christos TAILQ_REMOVE(&autofs_softc->sc_requests, ar, ar_next); 390 1.1 christos mutex_exit(&autofs_softc->sc_lock); 391 1.1 christos callout_halt(&ar->ar_callout, NULL); 392 1.1 christos pool_put(&autofs_request_pool, ar); 393 1.1 christos mutex_enter(&autofs_softc->sc_lock); 394 1.1 christos } 395 1.1 christos 396 1.1 christos /* 397 1.1 christos * Note that we do not do negative caching on purpose. This 398 1.1 christos * way the user can retry access at any time, e.g. after fixing 399 1.1 christos * the failure reason, without waiting for cache timer to expire. 400 1.1 christos */ 401 1.1 christos if (!error && !request_error && autofs_cache > 0) { 402 1.1 christos autofs_node_cache(anp); 403 1.1 christos anp->an_wildcards = wildcards; 404 1.1 christos callout_reset(&anp->an_callout, autofs_cache * hz, 405 1.1 christos autofs_cache_callout, anp); 406 1.1 christos } 407 1.1 christos 408 1.1 christos kmem_strfree(key); 409 1.1 christos kmem_strfree(path); 410 1.1 christos 411 1.1 christos if (error) 412 1.1 christos return error; 413 1.1 christos return request_error; 414 1.1 christos } 415 1.1 christos 416 1.1 christos int 417 1.1 christos autofs_trigger(struct autofs_node *anp, const char *component, 418 1.1 christos int componentlen) 419 1.1 christos { 420 1.1 christos for (;;) { 421 1.1 christos int error, dummy; 422 1.1 christos 423 1.1 christos error = autofs_trigger_one(anp, component, componentlen); 424 1.1 christos if (!error) { 425 1.1 christos anp->an_retries = 0; 426 1.1 christos return 0; 427 1.1 christos } 428 1.1 christos if (error == EINTR || error == ERESTART) { 429 1.1 christos AUTOFS_DEBUG("trigger interrupted by signal, " 430 1.1 christos "not retrying"); 431 1.1 christos anp->an_retries = 0; 432 1.1 christos return error; 433 1.1 christos } 434 1.1 christos anp->an_retries++; 435 1.1 christos if (anp->an_retries >= autofs_retry_attempts) { 436 1.1 christos AUTOFS_DEBUG("trigger failed %d times; returning " 437 1.1 christos "error %d", anp->an_retries, error); 438 1.1 christos anp->an_retries = 0; 439 1.1 christos return error; 440 1.1 christos 441 1.1 christos } 442 1.1 christos AUTOFS_DEBUG("trigger failed with error %d; will retry in " 443 1.1 christos "%d seconds, %d attempts left", error, autofs_retry_delay, 444 1.1 christos autofs_retry_attempts - anp->an_retries); 445 1.1 christos mutex_exit(&autofs_softc->sc_lock); 446 1.1 christos tsleep(&dummy, 0, "autofs_retry", autofs_retry_delay * hz); 447 1.1 christos mutex_enter(&autofs_softc->sc_lock); 448 1.1 christos } 449 1.1 christos } 450 1.1 christos 451 1.1 christos static int 452 1.1 christos autofs_ioctl_request(struct autofs_daemon_request *adr) 453 1.1 christos { 454 1.1 christos struct autofs_request *ar; 455 1.1 christos 456 1.1 christos mutex_enter(&autofs_softc->sc_lock); 457 1.1 christos for (;;) { 458 1.1 christos int error; 459 1.1 christos TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) { 460 1.1 christos if (ar->ar_done) 461 1.1 christos continue; 462 1.1 christos if (ar->ar_in_progress) 463 1.1 christos continue; 464 1.1 christos break; 465 1.1 christos } 466 1.1 christos 467 1.1 christos if (ar) 468 1.1 christos break; 469 1.1 christos 470 1.1 christos error = cv_wait_sig(&autofs_softc->sc_cv, 471 1.1 christos &autofs_softc->sc_lock); 472 1.1 christos if (error) { 473 1.1 christos mutex_exit(&autofs_softc->sc_lock); 474 1.1 christos return error; 475 1.1 christos } 476 1.1 christos } 477 1.1 christos 478 1.1 christos ar->ar_in_progress = true; 479 1.1 christos mutex_exit(&autofs_softc->sc_lock); 480 1.1 christos 481 1.1 christos adr->adr_id = ar->ar_id; 482 1.1 christos strlcpy(adr->adr_from, ar->ar_from, sizeof(adr->adr_from)); 483 1.1 christos strlcpy(adr->adr_path, ar->ar_path, sizeof(adr->adr_path)); 484 1.1 christos strlcpy(adr->adr_prefix, ar->ar_prefix, sizeof(adr->adr_prefix)); 485 1.1 christos strlcpy(adr->adr_key, ar->ar_key, sizeof(adr->adr_key)); 486 1.1 christos strlcpy(adr->adr_options, ar->ar_options, sizeof(adr->adr_options)); 487 1.1 christos 488 1.6 ad mutex_enter(&proc_lock); 489 1.1 christos autofs_softc->sc_dev_sid = curproc->p_pgrp->pg_id; 490 1.6 ad mutex_exit(&proc_lock); 491 1.1 christos 492 1.1 christos return 0; 493 1.1 christos } 494 1.1 christos 495 1.1 christos static int 496 1.1 christos autofs_ioctl_done(struct autofs_daemon_done *add) 497 1.1 christos { 498 1.1 christos struct autofs_request *ar; 499 1.1 christos 500 1.1 christos mutex_enter(&autofs_softc->sc_lock); 501 1.1 christos TAILQ_FOREACH(ar, &autofs_softc->sc_requests, ar_next) { 502 1.1 christos if (ar->ar_id == add->add_id) 503 1.1 christos break; 504 1.1 christos } 505 1.1 christos 506 1.1 christos if (!ar) { 507 1.1 christos mutex_exit(&autofs_softc->sc_lock); 508 1.1 christos AUTOFS_DEBUG("id %d not found", add->add_id); 509 1.1 christos return ESRCH; 510 1.1 christos } 511 1.1 christos 512 1.1 christos ar->ar_error = add->add_error; 513 1.1 christos ar->ar_wildcards = add->add_wildcards; 514 1.1 christos ar->ar_done = true; 515 1.1 christos ar->ar_in_progress = false; 516 1.1 christos cv_broadcast(&autofs_softc->sc_cv); 517 1.1 christos 518 1.1 christos mutex_exit(&autofs_softc->sc_lock); 519 1.1 christos 520 1.1 christos return 0; 521 1.1 christos } 522 1.1 christos 523 1.4 tkusumi static int 524 1.1 christos autofs_open(dev_t dev, int flags, int mode, struct lwp *l) 525 1.1 christos { 526 1.1 christos 527 1.1 christos mutex_enter(&autofs_softc->sc_lock); 528 1.1 christos /* 529 1.1 christos * We must never block automountd(8) and its descendants, and we use 530 1.1 christos * session ID to determine that: we store session id of the process 531 1.1 christos * that opened the device, and then compare it with session ids 532 1.1 christos * of triggering processes. This means running a second automountd(8) 533 1.1 christos * instance would break the previous one. The check below prevents 534 1.1 christos * it from happening. 535 1.1 christos */ 536 1.1 christos if (autofs_softc->sc_dev_opened) { 537 1.1 christos mutex_exit(&autofs_softc->sc_lock); 538 1.1 christos return EBUSY; 539 1.1 christos } 540 1.1 christos 541 1.1 christos autofs_softc->sc_dev_opened = true; 542 1.1 christos mutex_exit(&autofs_softc->sc_lock); 543 1.1 christos 544 1.1 christos return 0; 545 1.1 christos } 546 1.1 christos 547 1.4 tkusumi static int 548 1.1 christos autofs_close(dev_t dev, int flags, int mode, struct lwp *l) 549 1.1 christos { 550 1.1 christos 551 1.1 christos mutex_enter(&autofs_softc->sc_lock); 552 1.1 christos KASSERT(autofs_softc->sc_dev_opened); 553 1.1 christos autofs_softc->sc_dev_opened = false; 554 1.1 christos mutex_exit(&autofs_softc->sc_lock); 555 1.1 christos 556 1.1 christos return 0; 557 1.1 christos } 558 1.1 christos 559 1.4 tkusumi static int 560 1.1 christos autofs_ioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l) 561 1.1 christos { 562 1.1 christos 563 1.1 christos KASSERT(autofs_softc->sc_dev_opened); 564 1.1 christos 565 1.1 christos switch (cmd) { 566 1.1 christos case AUTOFSREQUEST: 567 1.1 christos return autofs_ioctl_request( 568 1.1 christos (struct autofs_daemon_request *)data); 569 1.1 christos case AUTOFSDONE: 570 1.1 christos return autofs_ioctl_done( 571 1.1 christos (struct autofs_daemon_done *)data); 572 1.1 christos default: 573 1.1 christos AUTOFS_DEBUG("invalid cmd %lx", cmd); 574 1.1 christos return EINVAL; 575 1.1 christos } 576 1.1 christos return EINVAL; 577 1.1 christos } 578