aboutsummaryrefslogtreecommitdiffstats
path: root/daemon/automount.c
blob: 2fb2c900189e83313c014541e61fb0ff22d7d1ee (plain)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *
 *  automount.c - Linux automounter daemon
 *   
 *   Copyright 1997 Transmeta Corporation - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version.
 *   
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 * ----------------------------------------------------------------------- */

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <linux/auto_fs.h>

#include "automount.h"

const char *program;		        /* Initialized with argv[0] */
const char *version = VERSION_STRING;	/* Program version */

static pid_t my_pgrp;		        /* The "magic" process group */
static pid_t my_pid;		        /* The pid of this process */
static char *pid_file = NULL;	        /* File in which to keep pid */

int kproto_version;		        /* Kernel protocol version used */

static int mounted = 0;

struct pending_mount {
  pid_t pid;		/* Which process is mounting for us */
  unsigned long wait_queue_token;	/* Associated kernel wait token */
  volatile struct pending_mount *next;
};

static struct autofs_point {
  char *path;		/* Mount point name */
  int pipefd;		/* File descriptor for pipe */
  int ioctlfd;		/* File descriptor for ioctls */
  dev_t dev;		/* "Device" number assigned by kernel */
  unsigned exp_timeout;	/* Timeout for expiring mounts */
  unsigned exp_runfreq;	/* Frequency for polling for timeouts */
  volatile pid_t exp_process; /* Process that is currently expiring */
  volatile struct pending_mount *mounts; /* Pending mount queue */
  struct lookup_mod *lookup; /* Lookup module */
} ap;

volatile struct pending_mount *junk_mounts = NULL;

#define DEFAULT_TIMEOUT (5*60)	/* 5 minutes */
#define CHECK_RATIO     4	/* exp_runfreq = exp_timeout/CHECK_RATIO */

static int umount_ent(char *root, char *name)
{
  char path_buf[PATH_MAX];
  struct stat st;
  int rv = 0;
  
  sprintf(path_buf, "%s/%s", root, name);
  if ( !lstat(path_buf,&st) ) {
    if ( S_ISDIR(st.st_mode) ) {
      if ( st.st_dev != ap.dev ) {
	rv = spawnl(LOG_NOTICE, _PATH_UMOUNT,
		    _PATH_UMOUNT, path_buf, NULL);
	if ( !rv )
	  rmdir(path_buf);
      } else
	rmdir(path_buf);
    } else 
      unlink(path_buf);
  }
  return rv;
}	

static int umount_all(int force)
{
  int left;
  struct dirent *de;
  DIR *dp;
  
  chdir("/");
  
  if ( ap.exp_process ) {
    kill(ap.exp_process, SIGTERM);
    usleep(100000);
  }
  
  dp = opendir(ap.path);
  if ( !dp ) {
    syslog(LOG_ERR, "umount_all: opendir: %m");
    return -1;
  }
  
  left = 0;
  
  while ( (de = readdir(dp)) != NULL ) {
    if ( strcmp(de->d_name,".") && strcmp(de->d_name,"..") ) {
      if ( umount_ent(ap.path,de->d_name) ) {
	if ( force )
	  syslog(LOG_WARNING,
		 "could not unmount %s/%s",
		 ap.path,de->d_name);
	left++;
      }
    }
  }
  closedir(dp);
  
  return left;
}	

static int umount_autofs(int force)
{
  int rv;
  
  if ( !mounted )
    return -1;
  
  if ( umount_all(force) && !force )
    return -1;
  
  if (ap.ioctlfd >= 0) {
    ioctl(ap.ioctlfd, AUTOFS_IOC_CATATONIC, 0);
    close(ap.ioctlfd);
  }
  if (ap.pipefd >= 0)
    close(ap.pipefd);
  rv = spawnl(LOG_ERR, _PATH_UMOUNT, _PATH_UMOUNT, ap.path, NULL);
  free(ap.path);
  mounted = 0;
  return rv;
}

static int mount_autofs(char *path)
{
  int pipefd[2];
  char options[128];
  char our_name[128];
  struct stat st;
  
  if ( mounted ) {
    /* This can't happen */
    syslog(LOG_ERR, "mount_autofs: already mounted");
    return -1;
  }
  
  if ( path[0] != '/' ) {
    errno = EINVAL;	/* Must be an absolute pathname */
    return -1;
  }
  
  ap.path = strdup(path);
  if ( !ap.path ) {
    errno = ENOMEM;
    return -1;
  }
  ap.pipefd = ap.ioctlfd = -1;
  
  /* In case the directory doesn't exist, try to mkdir it */
  if ( mkdir(path, 0555) < 0 && errno != EEXIST )
    return -1;
  
  if ( pipe(pipefd) < 0 )
    return -1;
  
  sprintf(options, "fd=%d,pgrp=%u,minproto=2,maxproto=%d", pipefd[1],
	  (unsigned)my_pgrp, AUTOFS_PROTO_VERSION);
  sprintf(our_name, "automount(pid%u)", (unsigned)my_pid);
  
  if ( spawnl(LOG_CRIT, _PATH_MOUNT, _PATH_MOUNT, "-t", "autofs", "-o",
	      options, our_name, path, NULL) ) {
    close(pipefd[0]);
    close(pipefd[1]);
    return -1;
  }
  
  close(pipefd[1]);	/* Close kernel pipe end */
  ap.pipefd = pipefd[0];
  
  chdir(path);
  ap.ioctlfd = open(".", O_RDONLY); /* Root directory for ioctl()'s */
  chdir("/");
  if ( ap.ioctlfd < 0 ) {
    umount_autofs(1);
    return -1;
  }
  
  stat(path,&st);
  ap.dev = st.st_dev;	/* Device number for mount point checks */
  
  ap.mounts = NULL;	/* No pending mounts */
  mounted = 1;
  
  return 0;
}

static void run_expire(void)
{
  pid_t f;
  struct autofs_packet_expire pkt;
  sigset_t s, olds;
  
  if ( ap.exp_process )	/* Another process is already expiring */
    return;		/* Try again next interval */
  
  if ( ioctl(ap.ioctlfd, AUTOFS_IOC_EXPIRE, &pkt) )
    return;		/* Don't fork if there is nothing do expire */
  
  /* Temporarily block SIGCHLD and SIGALRM between forking and setting
     ap.exp_process */
  
  sigemptyset(&s);
  sigaddset(&s, SIGCHLD);
  sigaddset(&s, SIGALRM);
  sigprocmask(SIG_BLOCK, &s, &olds);
  
  f = fork();
  if ( f < 0 )
    return;
  if ( f > 0 ) {
    ap.exp_process = f;
    sigprocmask(SIG_SETMASK, &olds, NULL);
    return;
  }
  
  /* This is the actual expire run, run as a subprocess */
  
  reset_signals();
  close(ap.pipefd);
  
  syslog(LOG_DEBUG, "running expiration on path %s", ap.path);
  
  do {
    if ( pkt.hdr.type == autofs_ptype_expire ) {
      if ( !umount_ent(ap.path,pkt.name) )
	syslog(LOG_NOTICE, "expired %s/%s", ap.path,
	       pkt.name);
    }
  } while ( ioctl(ap.ioctlfd, AUTOFS_IOC_EXPIRE, &pkt) == 0 );
  _exit(0);
}

static sig_atomic_t shutdown = 0;
static sig_atomic_t prune = 0;
static sig_atomic_t reinit = 0;
static sig_atomic_t expire_now = 0;

static void sig_shutdown(int sig)
{
  shutdown = sig;
}

static void sig_prune(int sig)
{
  prune = 1;
}

static void sig_expire(int sig)
{
  expire_now = 1;
}

static void sig_reinit(int sig)
{
  prune = 1;
  reinit = 1;
}

static int get_pkt(int fd, struct autofs_packet_missing *pkt)
{
  char *buf = (char *) pkt;
  int bytes = sizeof(*pkt);

  do {
    int i = read(fd, buf, bytes);
    if (!i)
      break;
    if (i < 0) {
      if (errno == EINTR) {
	if ( prune ) {
	  umount_all(0);
	  prune = 0;
	}
	if ( expire_now ) {
	  run_expire();
	  expire_now = 0;
	  alarm(ap.exp_runfreq);
	}
	if ( !shutdown && !reinit )
	  continue;
      }
      break;
    }
    buf += i;
    bytes -= i;
  } while (bytes);
  return bytes;
}

static int send_ready(unsigned int wait_queue_token)
{
  if ( ioctl(ap.ioctlfd, AUTOFS_IOC_READY, wait_queue_token) < 0 ) {
    syslog(LOG_ERR, "AUTOFS_IOC_READY: %m");
    return 1;
  } else
    return 0;
}

static int send_fail(unsigned int wait_queue_token)
{
  if ( ioctl(ap.ioctlfd, AUTOFS_IOC_FAIL, wait_queue_token) < 0 ) {
    syslog(LOG_ERR, "AUTOFS_IOC_READY: %m");
    return 1;
  } else
    return 0;
}

static int handle_packet(void)
{
  struct autofs_packet_missing pkt;
  struct stat st;
  sigset_t allsignals, oldsig, chldsig;
  pid_t f;

  if (get_pkt(ap.pipefd, &pkt))
    return -1;
  
  sigemptyset(&chldsig);
  sigaddset(&chldsig, SIGCHLD);
  sigfillset(&allsignals);

  chdir(ap.path);
  if ( lstat(pkt.name,&st) || (S_ISDIR(st.st_mode) && st.st_dev == ap.dev) ) {
    /* Need to mount or symlink */
    struct pending_mount *mt;
    
    /* Block SIGCHLD while mucking with linked lists */
    sigprocmask(SIG_BLOCK, &chldsig, NULL);
    if ( (mt = (struct pending_mount *) junk_mounts) ) {
      junk_mounts = junk_mounts->next;
    } else if ( !(mt = malloc(sizeof(struct pending_mount))) ) {
      syslog(LOG_ERR, "handle_packet: malloc: %m");
      send_fail(pkt.wait_queue_token);
      return 1;
    }
    sigprocmask(SIG_UNBLOCK, &chldsig, NULL);

    syslog(LOG_INFO, "attempting to mount entry %s/%s",
	   ap.path, pkt.name);

    sigprocmask(SIG_BLOCK, &allsignals, &oldsig);

    f = fork();
    if ( f == -1 ) {
      sigprocmask(SIG_SETMASK, &oldsig, NULL);
      syslog(LOG_ERR, "handle_packet: fork: %m");
      send_fail(pkt.wait_queue_token);
      return 1;
    } else if ( !f ) {
      int i;
      
      reset_signals();		/* Set up a standard signal environment */
      close(ap.pipefd);
      close(ap.ioctlfd);
      
      i = ap.lookup->lookup_mount(ap.path,pkt.name,pkt.len,ap.lookup->context);
      _exit(i ? 1 : 0);
    } else {
      /* Important: set up data structures while signals still blocked */
      mt->pid = f;
      mt->wait_queue_token = pkt.wait_queue_token;
      mt->next = ap.mounts;
      ap.mounts = mt;

      sigprocmask(SIG_SETMASK, &oldsig, NULL);
    }
  } else {
    /* Already there (can happen if a process connects to a
       directory while we're still working on it) */
    chdir("/");
    send_ready(pkt.wait_queue_token);
  }
  return 0;
}

static void sig_child(int sig)
{
  pid_t pid;
  int status;
  volatile struct pending_mount *mt, * volatile *mtp;
  
  while ( (pid = waitpid(-1, &status, WNOHANG)) > 0 ) {
    if ( pid == ap.exp_process ) {
      ap.exp_process = 0;
    } else {
      for ( mtp = &ap.mounts ; (mt = *mtp) ; mtp = &mt->next ) {
	if ( mt->pid == pid ) {
	  if ( !WIFEXITED(status) && !WIFSIGNALED(status) )
	    break;
	  else if ( WIFSIGNALED(status) ||
		    WEXITSTATUS(status) != 0 )
	    send_fail(mt->wait_queue_token);
	  else
	    send_ready(mt->wait_queue_token);
	  *mtp = mt->next; /* Remove from linked list */
	  /* We can't use free() from a signal handler, so put this on
	     a "junk list" which we can reclaim later */
	  mt->next = junk_mounts;
	  junk_mounts = mt;
	  break;
	}
      }
    }
  }
}

static void become_daemon(void)
{
  FILE *pidfp;
  pid_t pid;
  int nullfd;
  
  /* Don't BUSY any directories unneccessarily */

  chdir("/");
  
  /* Detach from foreground process */

  pid = fork();
  if ( pid > 0 )
    exit(0);
  else if ( pid < 0 ) {
    fprintf(stderr, "%s: Could not detach process\n", program);
    exit(1);
  }
  
  /* Open syslog */ 

  openlog("automount", LOG_PID, LOG_DAEMON);

  /* Initialize global data */

  my_pid = getpid();

  /* Make our own process group for "magic" reason */

  if ( setpgrp() ) {
    syslog(LOG_CRIT, "setpgrp: %m");
    exit(1);
  }
  my_pgrp = getpgrp();
  
  /* Redirect all our file descriptors to /dev/null */

  if ( (nullfd = open("/dev/null", O_RDWR)) < 0 ) {
    syslog(LOG_CRIT, "cannot open /dev/null: %m");
    exit(1);
  }
  
  if ( dup2(nullfd, STDIN_FILENO) < 0 ||
       dup2(nullfd, STDOUT_FILENO) < 0 ||
       dup2(nullfd, STDERR_FILENO) < 0 ) {
    syslog(LOG_CRIT, "redirecting file descriptors failed: %m");
    exit(1);
  }
  close(nullfd);

  /* Write pid file if requested */

  if ( pid_file ) {
    if ( (pidfp = fopen(pid_file, "wt")) ) {
      fprintf(pidfp, "%lu\n", (unsigned long) my_pid);
      fclose(pidfp);
    } else {
      syslog(LOG_WARNING, "failed to write pid file %s: %m", pid_file);
      pid_file = NULL;
    }
  }
}

/*
 * cleanup_exit() is valid to call once we have daemonized
 */

static void cleanup_exit(int exit_code)
{
  if ( ap.lookup )
    close_lookup(ap.lookup);

  if ( pid_file )
    unlink(pid_file);

  closelog();

  exit(exit_code);
}

static unsigned long getnumopt(char *str, char option)
{
  unsigned long val;
  char *end;

  val = strtoul(str, &end, 0);
  if ( ! *str || *end ) {
    fprintf(stderr, "%s: option -%c requires a numeric argument, got %s\n",
	    program, option, str);
    exit(1);
  }
  return val;
}

static void usage(void)
{
  fprintf(stderr, "Usage: %s [options] path map_type [args...]\n", program);
}

int main(int argc, char *argv[])
{
  char *path, *map, *mapfmt;
  const char **mapargv;
  struct sigaction sa;
  int mapargc, opt;
  static const struct option long_options[] = {
    {"help",     0, 0, 'h'},
    {"pid-file", 1, 0, 'p'},
    {"timeout",  1, 0, 't'},
    {"version",  0, 0, 'v'},
    {0,0,0,0}
  };
  
  program = argv[0];
  
  memset(&ap, 0, sizeof ap);	/* Initialize ap so we can test for null */
  ap.exp_timeout = DEFAULT_TIMEOUT;
  
  opterr = 0;
  while ( (opt = getopt_long(argc, argv, "+hp:t:v", long_options,
			     NULL)) != EOF ) {
    switch( opt ) {
    case 'h':
      usage();
      exit(0);
    case 'p':
      pid_file = optarg;
      break;
    case 't':
      ap.exp_timeout = getnumopt(optarg, opt);
      break;
    case 'v':
      printf("Linux automount version %s\n", version);
      exit(0);
    case '?':
    case ':':
      printf("%s: Ambiguous or unknown options\n", program);
      exit(1);
    }
  }
  
  if ( geteuid() != 0 ) {
    fprintf(stderr, "%s: This program must be run by root.\n", program);
    exit(1);
  }
  
  /* Remove the options */
  argv += optind;
  argc -= optind;
  
  if ( argc < 2 ) {
    usage();
    exit(1);
  }
  
  become_daemon();
  
  path    = argv[0];
  map     = argv[1];
  mapargv = (const char **) &argv[2];
  mapargc = argc-2;         
  
  syslog(LOG_INFO, "starting automounter version %s, path = %s, "
	 "maptype = %s, mapname = %s", version, path, map,
	 (mapargc < 1) ? "none" : mapargv[0]);
  
  if ( mapargc ) {
    int i;
    syslog(LOG_DEBUG, "Map argc = %d", mapargc);
    for ( i = 0 ; i < mapargc ; i++ )
      syslog(LOG_DEBUG, "Map argv[%d] = %s", i, mapargv[i]);
  }
  
  if ( (mapfmt = strchr(map,',')) )
    *(mapfmt++) = '\0';
  
  if ( !(ap.lookup = open_lookup(map, "", mapfmt, mapargc, mapargv)) )
    cleanup_exit(1);
  
  if (mount_autofs(path) < 0) {
    syslog(LOG_CRIT, "%s: mount failed!", path);
    cleanup_exit(1);
  }	
  
  /* The following signals cause a shutdown event to occur */
  sa.sa_handler = sig_shutdown;
  sigemptyset(&sa.sa_mask);
  sigaddset(&sa.sa_mask, SIGCHLD);
  sigaddset(&sa.sa_mask, SIGALRM);
  sa.sa_flags = 0;
  sigaction(SIGIO, &sa, NULL);
  sigaction(SIGTERM, &sa, NULL);
  /* USR2 is special, but is handled by the same handler */
  sigaction(SIGUSR2, &sa, NULL);

  /* The following signals cause a shutdown event to occur, but if we get
     more than one, permit the signal to proceed so we don't loop.  This
     is basically the complete list of "this shouldn't happen" signals. */
  sa.sa_flags = SA_ONESHOT;
  sigaction(SIGXCPU, &sa, NULL);
  sigaction(SIGXFSZ, &sa, NULL);
  sigaction(SIGINT, &sa, NULL);
#ifdef SIGPWR
  sigaction(SIGPWR, &sa, NULL);
#endif
#ifndef DEBUG
  /* When debugging, these signals should be in the default state; when
     in production, we want to at least attempt to catch them and shut down. */
  sigaction(SIGILL, &sa, NULL);
  sigaction(SIGQUIT, &sa, NULL);
  sigaction(SIGABRT, &sa, NULL);
  sigaction(SIGTRAP, &sa, NULL);
  sigaction(SIGFPE, &sa, NULL);
  sigaction(SIGSEGV, &sa, NULL);
  sigaction(SIGBUS, &sa, NULL);
  sigaction(SIGPROF, &sa, NULL);
  sigaction(SIGPIPE, &sa, NULL);
#ifdef SIGSYS
  sigaction(SIGSYS, &sa, NULL);
#endif
#ifdef SIGSTKFLT
  sigaction(SIGSTKFLT, &sa, NULL);
#endif
#ifdef SIGLOST
  sigaction(SIGLOST, &sa, NULL);
#endif
#ifdef SIGEMT
  sigaction(SIGEMT, &sa, NULL);
#endif
#endif /* DEBUG */
  
  /* The SIGCHLD handler is the bottom handler of the mount request */
  sa.sa_handler = sig_child;
  sigemptyset(&sa.sa_mask);
  sigaddset(&sa.sa_mask, SIGALRM);
  sa.sa_flags = SA_NOCLDSTOP;	/* Don't need info about stopped children */
  sigaction(SIGCHLD, &sa, NULL);
  
  /* The SIGALRM handler controls expiration of entries. */
  sa.sa_handler = sig_expire;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sigaction(SIGALRM, &sa, NULL);

  /* SIGUSR1 causes a prune event */
  sa.sa_handler = sig_prune;
  sigaction(SIGUSR1, &sa, NULL);

  /* SIGHUP causes complete reinitialization */
  sa.sa_handler = sig_reinit;
  sigaction(SIGHUP, &sa, NULL);
  
  /* The following signals shouldn't occur, and are ignored */
  sa.sa_handler = SIG_IGN;
  sigaction(SIGVTALRM, &sa, NULL);
  sigaction(SIGURG, &sa, NULL);
  sigaction(SIGWINCH, &sa, NULL);
#ifdef SIGUNUSED
  sigaction(SIGUNUSED, &sa, NULL);
#endif
  
  /* If this ioctl() doesn't work, it is kernel version 2 */
  if ( ioctl(ap.ioctlfd, AUTOFS_IOC_PROTOVER, &kproto_version) ) {
    syslog(LOG_DEBUG, "kproto: %m");
    kproto_version = 2;
  }
  
  syslog(LOG_INFO, "using kernel protocol version %d", kproto_version);
  
  if ( kproto_version < 3 ) {
    ap.exp_timeout = ap.exp_runfreq = 0;
    syslog(LOG_INFO, "kernel does not support timeouts");
  } else {
    unsigned long timeout;
    
    ap.exp_runfreq = (ap.exp_timeout+CHECK_RATIO-1) / CHECK_RATIO;
    
    timeout = ap.exp_timeout;
    ioctl(ap.ioctlfd, AUTOFS_IOC_SETTIMEOUT, &timeout);
    
    /* We often start several automounters at the same time.  Add some
       randomness so we don't all expire at the same time. */
    if ( ap.exp_timeout )
      alarm(ap.exp_timeout + my_pid % ap.exp_runfreq);
  }
  
  while ( !shutdown ) {
    if (handle_packet() && errno != EINTR)
      break;
    if (shutdown == SIGUSR2 && umount_autofs(0))
      shutdown = 0;
    if (reinit) {
      reinit = 0;
      close_lookup(ap.lookup);
      if ( !(ap.lookup = open_lookup(map, "", mapfmt, mapargc, mapargv)) ) {
	syslog(LOG_CRIT, "reinitialization failed, shutting down");
	umount_autofs(1);
	cleanup_exit(1);
      }
    }
  }
  
  syslog(LOG_INFO, "shutting down, path = %s", path);

  umount_autofs(1);
  cleanup_exit(0);
}