|
root / base / usr / src / cmd / fs.d / autofs / autod_main.c
autod_main.c C 1058 lines 23.9 KB
   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
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */


#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <memory.h>
#include <stropts.h>
#include <netconfig.h>
#include <stdarg.h>
#include <sys/resource.h>
#include <sys/systeminfo.h>
#include <syslog.h>
#include <errno.h>
#include <sys/sockio.h>
#include <rpc/xdr.h>
#include <net/if.h>
#include <netdir.h>
#include <string.h>
#include <thread.h>
#include <locale.h>
#include <door.h>
#include <limits.h>
#include "automount.h"
#include <sys/vfs.h>
#include <sys/mnttab.h>
#include <arpa/inet.h>
#include <rpcsvc/daemon_utils.h>
#include <deflt.h>
#include <strings.h>
#include <priv.h>
#include <tsol/label.h>
#include <sys/utsname.h>
#include <sys/thread.h>
#include <nfs/rnode.h>
#include <nfs/nfs.h>
#include <wait.h>
#include <libshare.h>
#include <libscf.h>
#include "smfcfg.h"

static void autofs_doorfunc(void *, char *, size_t, door_desc_t *, uint_t);
static void autofs_setdoor(int);
static void autofs_mntinfo_1_r(autofs_lookupargs *, autofs_mountres *);
static void autofs_mount_1_free_r(struct autofs_mountres *);
static void autofs_lookup_1_r(autofs_lookupargs *, autofs_lookupres *);
static void autofs_lookup_1_free_args(autofs_lookupargs *);
static void autofs_unmount_1_r(umntrequest *, umntres *);
static void autofs_unmount_1_free_args(umntrequest *);
static void autofs_readdir_1_r(autofs_rddirargs *, autofs_rddirres *);
static void autofs_readdir_1_free_r(struct autofs_rddirres *);
static int decode_args(xdrproc_t, autofs_door_args_t *, caddr_t *, int);
static bool_t encode_res(xdrproc_t, autofs_door_res_t **, caddr_t, int *);
static void usage();
static void warn_hup(int);
static void free_action_list();
static int start_autofs_svcs();
static void automountd_wait_for_cleanup(pid_t);

/*
 * Private autofs system call
 */
extern int _autofssys(int, void *);

#define	CTIME_BUF_LEN 26

#define	RESOURCE_FACTOR 8
#ifdef DEBUG
#define	AUTOFS_DOOR	"/var/run/autofs_door"
#endif /* DEBUG */

static thread_key_t	s_thr_key;

struct autodir *dir_head;
struct autodir *dir_tail;
char self[64];

time_t timenow;
int verbose = 0;
int trace = 0;
int automountd_nobrowse = 0;
int did_fork_exec;

int
main(argc, argv)
	int argc;
	char *argv[];

{
	pid_t pid;
	int c, error;
	struct rlimit rlset;
	char defval[6];
	int ret = 0, bufsz;

	if (geteuid() != 0) {
		(void) fprintf(stderr, "%s must be run as root\n", argv[0]);
		exit(1);
	}

	/*
	 * Read in the values from SMF first before we check
	 * commandline options so the options override the file.
	 */
	bufsz = 6;
	ret = autofs_smf_get_prop("automountd_verbose", defval,
	    DEFAULT_INSTANCE, SCF_TYPE_BOOLEAN, AUTOMOUNTD, &bufsz);
	if (ret == SA_OK) {
		if (strncasecmp("true", defval, 4) == 0)
			verbose = TRUE;
		else
			verbose = FALSE;
	}
	bufsz = 6;
	ret = autofs_smf_get_prop("nobrowse", defval, DEFAULT_INSTANCE,
	    SCF_TYPE_BOOLEAN, AUTOMOUNTD, &bufsz);
	if (ret == SA_OK) {
		if (strncasecmp("true", defval, 4) == 0)
			automountd_nobrowse = TRUE;
		else
			automountd_nobrowse = FALSE;
	}
	bufsz = 6;
	ret = autofs_smf_get_prop("trace", defval, DEFAULT_INSTANCE,
	    SCF_TYPE_INTEGER, AUTOMOUNTD, &bufsz);
	if (ret == SA_OK) {
		errno = 0;
		trace = strtol(defval, (char **)NULL, 10);
		if (errno != 0)
			trace = 0;
	}
	put_automountd_env();

	while ((c = getopt(argc, argv, "vnTD:")) != EOF) {
		switch (c) {
		case 'v':
			verbose++;
			break;
		case 'n':
			automountd_nobrowse++;
			break;
		case 'T':
			trace++;
			break;
		case 'D':
			(void) putenv(optarg);
			break;
		default:
			usage();
		}
	}

	if (sysinfo(SI_HOSTNAME, self, sizeof (self)) == -1) {
		error = errno;
		(void) fprintf(stderr,
			"automountd: can't determine hostname, error: %d\n",
			error);
		exit(1);
	}

#ifndef DEBUG
	pid = fork();
	if (pid < 0) {
		perror("cannot fork");
		exit(1);
	}
	if (pid)
		exit(0);
#endif

	(void) setsid();
	openlog("automountd", LOG_PID, LOG_DAEMON);
	(void) setlocale(LC_ALL, "");

	/*
	 * Create the door_servers to manage fork/exec requests for
	 * mounts and executable automount maps
	 */
	if ((did_fork_exec = door_create(automountd_do_fork_exec,
	    NULL, 0)) == -1) {
		syslog(LOG_ERR, "door_create failed: %m, Exiting.");
		exit(errno);
	}
	if ((did_exec_map = door_create(automountd_do_exec_map,
	    NULL, 0)) == -1) {
		syslog(LOG_ERR, "door_create failed: %m, Exiting.");
		if (door_revoke(did_fork_exec) == -1) {
			syslog(LOG_ERR, "failed to door_revoke(%d) %m",
			    did_fork_exec);
		}
		exit(errno);
	}
	/*
	 * Before we become multithreaded we fork allowing the parent
	 * to become a door server to handle all mount and unmount
	 * requests. This works around a potential hang in using
	 * fork1() within a multithreaded environment
	 */

	pid = fork1();
	if (pid < 0) {
		syslog(LOG_ERR,
			"can't fork the automountd mount process %m");
		if (door_revoke(did_fork_exec) == -1) {
			syslog(LOG_ERR, "failed to door_revoke(%d) %m",
				did_fork_exec);
		}
		if (door_revoke(did_exec_map) == -1) {
			syslog(LOG_ERR, "failed to door_revoke(%d) %m",
				did_exec_map);
		}
		exit(1);
	} else if (pid > 0) {
		/* this is the door server process */
		automountd_wait_for_cleanup(pid);
	}


	(void) rwlock_init(&cache_lock, USYNC_THREAD, NULL);
	(void) rwlock_init(&autofs_rddir_cache_lock, USYNC_THREAD, NULL);

	/*
	 * initialize the name services, use NULL arguments to ensure
	 * we don't initialize the stack of files used in file service
	 */
	(void) ns_setup(NULL, NULL);

	/*
	 * we're using doors and its thread management now so we need to
	 * make sure we have more than the default of 256 file descriptors
	 * available.
	 */
	rlset.rlim_cur = RLIM_INFINITY;
	rlset.rlim_max = RLIM_INFINITY;
	if (setrlimit(RLIMIT_NOFILE, &rlset) == -1)
		syslog(LOG_ERR, "setrlimit failed for %s: %s", AUTOMOUNTD,
		    strerror(errno));

	(void) enable_extended_FILE_stdio(-1, -1);

	/*
	 * establish our lock on the lock file and write our pid to it.
	 * exit if some other process holds the lock, or if there's any
	 * error in writing/locking the file.
	 */
	pid = _enter_daemon_lock(AUTOMOUNTD);
	switch (pid) {
	case 0:
		break;
	case -1:
		syslog(LOG_ERR, "error locking for %s: %m", AUTOMOUNTD);
		exit(2);
	default:
		/* daemon was already running */
		exit(0);
	}

	/*
	 * If we coredump it'll be /core.
	 */
	if (chdir("/") < 0)
		syslog(LOG_ERR, "chdir /: %m");

	/*
	 * Create cache_cleanup thread
	 */
	if (thr_create(NULL, 0, (void *(*)(void *))cache_cleanup, NULL,
			THR_DETACHED | THR_DAEMON | THR_NEW_LWP, NULL)) {
		syslog(LOG_ERR, "unable to create cache_cleanup thread");
		exit(1);
	}

	/* other initializations */
	(void) rwlock_init(&portmap_cache_lock, USYNC_THREAD, NULL);

	/*
	 * On a labeled system, allow read-down nfs mounts if privileged
	 * (PRIV_NET_MAC_AWARE) to do so.  Otherwise, ignore the error
	 * and "mount equal label only" behavior will result.
	 */
	if (is_system_labeled()) {
		(void) setpflags(NET_MAC_AWARE, 1);
		(void) setpflags(NET_MAC_AWARE_INHERIT, 1);
	}

	(void) signal(SIGHUP, warn_hup);

	/* start services */
	return (start_autofs_svcs());

}

/*
 * The old automounter supported a SIGHUP
 * to allow it to resynchronize internal
 * state with the /etc/mnttab.
 * This is no longer relevant, but we
 * need to catch the signal and warn
 * the user.
 */
/* ARGSUSED */
static void
warn_hup(i)
	int i;
{
	syslog(LOG_ERR, "SIGHUP received: ignored");
	(void) signal(SIGHUP, warn_hup);
}

static void
usage()
{
	(void) fprintf(stderr, "Usage: automountd\n"
	    "\t[-T]\t\t(trace requests)\n"
	    "\t[-v]\t\t(verbose error msgs)\n"
	    "\t[-D n=s]\t(define env variable)\n");
	exit(1);
	/* NOTREACHED */
}

static void
autofs_readdir_1_r(
	autofs_rddirargs *req,
	autofs_rddirres *res)
{
	if (trace > 0)
		trace_prt(1, "READDIR REQUEST	: %s @ %ld\n",
		    req->rda_map, req->rda_offset);

	do_readdir(req, res);
	if (trace > 0)
		trace_prt(1, "READDIR REPLY	: status=%d\n", res->rd_status);
}

static void
autofs_readdir_1_free_r(struct autofs_rddirres *res)
{
	if (res->rd_status == AUTOFS_OK) {
		if (res->rd_rddir.rddir_entries)
			free(res->rd_rddir.rddir_entries);
	}
}


/* ARGSUSED */
static void
autofs_unmount_1_r(
	umntrequest *m,
	umntres *res)
{
	struct umntrequest *ul;

	if (trace > 0) {
		char ctime_buf[CTIME_BUF_LEN];
		if (ctime_r(&timenow, ctime_buf, CTIME_BUF_LEN) == NULL)
			ctime_buf[0] = '\0';

		trace_prt(1, "UNMOUNT REQUEST: %s", ctime_buf);
		for (ul = m; ul; ul = ul->next)
			trace_prt(1, " resource=%s fstype=%s mntpnt=%s"
			    " mntopts=%s %s\n",
			    ul->mntresource,
			    ul->fstype,
			    ul->mntpnt,
			    ul->mntopts,
			    ul->isdirect ? "direct" : "indirect");
	}


	res->status = do_unmount1(m);

	if (trace > 0)
		trace_prt(1, "UNMOUNT REPLY: status=%d\n", res->status);
}

static void
autofs_lookup_1_r(
	autofs_lookupargs *m,
	autofs_lookupres *res)
{
	autofs_action_t action;
	struct	linka link;
	int status;

	if (trace > 0) {
		char ctime_buf[CTIME_BUF_LEN];
		if (ctime_r(&timenow, ctime_buf, CTIME_BUF_LEN) == NULL)
			ctime_buf[0] = '\0';

		trace_prt(1, "LOOKUP REQUEST: %s", ctime_buf);
		trace_prt(1, "  name=%s[%s] map=%s opts=%s path=%s direct=%d\n",
		    m->name, m->subdir, m->map, m->opts, m->path, m->isdirect);
	}

	bzero(&link, sizeof (struct linka));

	status = do_lookup1(m->map, m->name, m->subdir, m->opts, m->path,
	    (uint_t)m->isdirect, m->uid, &action, &link);
	if (status == 0) {
		/*
		 * Return action list to kernel.
		 */
		res->lu_res = AUTOFS_OK;
		if ((res->lu_type.action = action) == AUTOFS_LINK_RQ) {
			res->lu_type.lookup_result_type_u.lt_linka = link;
		}
	} else {
		/*
		 * Entry not found
		 */
		res->lu_res = AUTOFS_NOENT;
	}
	res->lu_verbose = verbose;

	if (trace > 0)
		trace_prt(1, "LOOKUP REPLY    : status=%d\n", res->lu_res);
}

static void
autofs_mntinfo_1_r(
	autofs_lookupargs *m,
	autofs_mountres *res)
{
	int status;
	action_list		*alp = NULL;

	if (trace > 0) {
		char ctime_buf[CTIME_BUF_LEN];
		if (ctime_r(&timenow, ctime_buf, CTIME_BUF_LEN) == NULL)
			ctime_buf[0] = '\0';

		trace_prt(1, "MOUNT REQUEST:   %s", ctime_buf);
		trace_prt(1, "  name=%s[%s] map=%s opts=%s path=%s direct=%d\n",
		    m->name, m->subdir, m->map, m->opts, m->path, m->isdirect);
	}

	status = do_mount1(m->map, m->name, m->subdir, m->opts, m->path,
	    (uint_t)m->isdirect, m->uid, &alp, DOMOUNT_USER);
	if (status != 0) {
		/*
		 * An error occurred, free action list if allocated.
		 */
		if (alp != NULL) {
			free_action_list(alp);
			alp = NULL;
		}
	}
	if (alp != NULL) {
		/*
		 * Return action list to kernel.
		 */
		res->mr_type.status = AUTOFS_ACTION;
		res->mr_type.mount_result_type_u.list = alp;
	} else {
		/*
		 * No work to do left for the kernel
		 */
		res->mr_type.status = AUTOFS_DONE;
		res->mr_type.mount_result_type_u.error = status;
	}

	if (trace > 0) {
		switch (res->mr_type.status) {
		case AUTOFS_ACTION:
			trace_prt(1,
			    "MOUNT REPLY    : status=%d, AUTOFS_ACTION\n",
			    status);
			break;
		case AUTOFS_DONE:
			trace_prt(1,
			    "MOUNT REPLY    : status=%d, AUTOFS_DONE\n",
			    status);
			break;
		default:
			trace_prt(1, "MOUNT REPLY    : status=%d, UNKNOWN\n",
			    status);
		}
	}

	if (status && verbose) {
		if (m->isdirect) {
			/* direct mount */
			syslog(LOG_ERR, "mount of %s failed", m->path);
		} else {
			/* indirect mount */
			syslog(LOG_ERR,
			    "mount of %s/%s failed", m->path, m->name);
		}
	}
}

static void
autofs_mount_1_free_r(struct autofs_mountres *res)
{
	if (res->mr_type.status == AUTOFS_ACTION) {
		if (trace > 2)
			trace_prt(1, "freeing action list\n");
		free_action_list(res->mr_type.mount_result_type_u.list);
	}
}

/*
 * Used for reporting messages from code shared with automount command.
 * Formats message into a buffer and calls syslog.
 *
 * Print an error.  Works like printf (fmt string and variable args)
 * except that it will subsititute an error message for a "%m" string
 * (like syslog).
 */
void
pr_msg(const char *fmt, ...)
{
	va_list ap;
	char fmtbuff[BUFSIZ], buff[BUFSIZ];
	const char *p1;
	char *p2;

	p2 = fmtbuff;
	fmt = gettext(fmt);

	for (p1 = fmt; *p1; p1++) {
		if (*p1 == '%' && *(p1 + 1) == 'm') {
			(void) strcpy(p2, strerror(errno));
			p2 += strlen(p2);
			p1++;
		} else {
			*p2++ = *p1;
		}
	}
	if (p2 > fmtbuff && *(p2-1) != '\n')
		*p2++ = '\n';
	*p2 = '\0';

	va_start(ap, fmt);
	(void) vsprintf(buff, fmtbuff, ap);
	va_end(ap);
	syslog(LOG_ERR, buff);
}

static void
free_action_list(action_list *alp)
{
	action_list *p, *next = NULL;
	struct mounta *mp;

	for (p = alp; p != NULL; p = next) {
		switch (p->action.action) {
		case AUTOFS_MOUNT_RQ:
			mp = &(p->action.action_list_entry_u.mounta);
			/* LINTED pointer alignment */
			if (mp->fstype) {
				if (strcmp(mp->fstype, "autofs") == 0) {
					free_autofs_args((autofs_args *)
					    mp->dataptr);
				} else if (strncmp(mp->fstype, "nfs", 3) == 0) {
					free_nfs_args((struct nfs_args *)
					    mp->dataptr);
				}
			}
			mp->dataptr = NULL;
			mp->datalen = 0;
			free_mounta(mp);
			break;
		case AUTOFS_LINK_RQ:
			syslog(LOG_ERR,
			    "non AUTOFS_MOUNT_RQ requests not implemented\n");
			break;
		default:
			syslog(LOG_ERR,
			    "non AUTOFS_MOUNT_RQ requests not implemented\n");
			break;
		}
		next = p->next;
		free(p);
	}
}

static void
autofs_lookup_1_free_args(autofs_lookupargs *args)
{
	if (args->map)
		free(args->map);
	if (args->path)
		free(args->path);
	if (args->name)
		free(args->name);
	if (args->subdir)
		free(args->subdir);
	if (args->opts)
		free(args->opts);
}

static void
autofs_unmount_1_free_args(umntrequest *args)
{
	if (args->mntresource)
		free(args->mntresource);
	if (args->mntpnt)
		free(args->mntpnt);
	if (args->fstype)
		free(args->fstype);
	if (args->mntopts)
		free(args->mntopts);
	if (args->next)
		autofs_unmount_1_free_args(args->next);
}

static void
autofs_setdoor(int did)
{

	if (did < 0) {
		did = 0;
	}

	(void) _autofssys(AUTOFS_SETDOOR, &did);
}

void *
autofs_get_buffer(size_t size)
{
	autofs_tsd_t *tsd = NULL;

	/*
	 * Make sure the buffer size is aligned
	 */
	(void) thr_getspecific(s_thr_key, (void **)&tsd);
	if (tsd == NULL) {
		tsd = (autofs_tsd_t *)malloc(sizeof (autofs_tsd_t));
		if (tsd == NULL) {
			return (NULL);
		}
		tsd->atsd_buf = malloc(size);
		if (tsd->atsd_buf != NULL)
			tsd->atsd_len = size;
		else
			tsd->atsd_len = 0;
		(void) thr_setspecific(s_thr_key, tsd);
	} else {
		if (tsd->atsd_buf && (tsd->atsd_len < size)) {
			free(tsd->atsd_buf);
			tsd->atsd_buf = malloc(size);
			if (tsd->atsd_buf != NULL)
				tsd->atsd_len = size;
			else {
				tsd->atsd_len = 0;
			}
		}
	}
	if (tsd->atsd_buf) {
		bzero(tsd->atsd_buf, size);
		return (tsd->atsd_buf);
	} else {
		syslog(LOG_ERR,
		    gettext("Can't Allocate tsd buffer, size %d"), size);
		return (NULL);
	}
}

/*
 * Each request will automatically spawn a new thread with this
 * as its entry point.
 */
/* ARGUSED */
static void
autofs_doorfunc(
	void *cookie,
	char *argp,
	size_t arg_size,
	door_desc_t *dp,
	uint_t n_desc)
{
	char			*res;
	int			 res_size;
	int			 which;
	int			 error = 0;
	int			 srsz = 0;
	autofs_lookupargs	*xdrargs;
	autofs_lookupres	 lookup_res;
	autofs_rddirargs	*rddir_args;
	autofs_rddirres		 rddir_res;
	autofs_mountres		 mount_res;
	umntrequest		*umnt_args;
	umntres			 umount_res;
	autofs_door_res_t	*door_res;
	autofs_door_res_t	 failed_res;

	if (arg_size < sizeof (autofs_door_args_t)) {
		failed_res.res_status = EINVAL;
		error = door_return((char *)&failed_res,
		    sizeof (autofs_door_res_t), NULL, 0);
		/*
		 * If we got here the door_return() failed.
		 */
		syslog(LOG_ERR, "Bad argument, door_return failure %d", error);
		return;
	}

	timenow = time((time_t *)NULL);

	which = ((autofs_door_args_t *)argp)->cmd;
	switch (which) {
	case AUTOFS_LOOKUP:
		if (error = decode_args(xdr_autofs_lookupargs,
		    (autofs_door_args_t *)argp, (caddr_t *)&xdrargs,
		    sizeof (autofs_lookupargs))) {
			syslog(LOG_ERR,
			    "error allocating lookup arguments buffer");
			failed_res.res_status = error;
			failed_res.xdr_len = 0;
			res = (caddr_t)&failed_res;
			res_size = 0;
			break;
		}
		bzero(&lookup_res, sizeof (autofs_lookupres));

		autofs_lookup_1_r(xdrargs, &lookup_res);

		autofs_lookup_1_free_args(xdrargs);
		free(xdrargs);

		if (!encode_res(xdr_autofs_lookupres, &door_res,
		    (caddr_t)&lookup_res, &res_size)) {
			syslog(LOG_ERR,
			    "error allocating lookup results buffer");
			failed_res.res_status = EINVAL;
			failed_res.xdr_len = 0;
			res = (caddr_t)&failed_res;
		} else {
			door_res->res_status = 0;
			res = (caddr_t)door_res;
		}
		break;

	case AUTOFS_MNTINFO:
		if (error = decode_args(xdr_autofs_lookupargs,
		    (autofs_door_args_t *)argp, (caddr_t *)&xdrargs,
		    sizeof (autofs_lookupargs))) {
			syslog(LOG_ERR,
			    "error allocating lookup arguments buffer");
			failed_res.res_status = error;
			failed_res.xdr_len = 0;
			res = (caddr_t)&failed_res;
			res_size = 0;
			break;
		}

		autofs_mntinfo_1_r((autofs_lookupargs *)xdrargs, &mount_res);

		autofs_lookup_1_free_args(xdrargs);
		free(xdrargs);

		/*
		 * Only reason we would get a NULL res is because
		 * we could not allocate a results buffer.  Use
		 * a local one to return the error EAGAIN as has
		 * always been done when memory allocations fail.
		 */
		if (!encode_res(xdr_autofs_mountres, &door_res,
		    (caddr_t)&mount_res, &res_size)) {
			syslog(LOG_ERR,
			    "error allocating mount results buffer");
			failed_res.res_status = EAGAIN;
			failed_res.xdr_len = 0;
			res = (caddr_t)&failed_res;
		} else {
			door_res->res_status = 0;
			res = (caddr_t)door_res;
		}
		autofs_mount_1_free_r(&mount_res);
		break;

	case AUTOFS_UNMOUNT:
		if (error = decode_args(xdr_umntrequest,
		    (autofs_door_args_t *)argp,
		    (caddr_t *)&umnt_args, sizeof (umntrequest))) {
			syslog(LOG_ERR,
			    "error allocating unmount argument buffer");
			failed_res.res_status = error;
			failed_res.xdr_len = 0;
			res = (caddr_t)&failed_res;
			res_size = sizeof (autofs_door_res_t);
			break;
		}

		autofs_unmount_1_r(umnt_args, &umount_res);

		error = umount_res.status;

		autofs_unmount_1_free_args(umnt_args);
		free(umnt_args);

		if (!encode_res(xdr_umntres, &door_res, (caddr_t)&umount_res,
		    &res_size)) {
			syslog(LOG_ERR,
			    "error allocating unmount results buffer");
			failed_res.res_status = EINVAL;
			failed_res.xdr_len = 0;
			res = (caddr_t)&failed_res;
			res_size = sizeof (autofs_door_res_t);
		} else {
			door_res->res_status = 0;
			res = (caddr_t)door_res;
		}
		break;

	case AUTOFS_READDIR:
		if (error = decode_args(xdr_autofs_rddirargs,
		    (autofs_door_args_t *)argp,
		    (caddr_t *)&rddir_args,
		    sizeof (autofs_rddirargs))) {
			syslog(LOG_ERR,
			    "error allocating readdir argument buffer");
			failed_res.res_status = error;
			failed_res.xdr_len = 0;
			res = (caddr_t)&failed_res;
			res_size = sizeof (autofs_door_res_t);
			break;
		}

		autofs_readdir_1_r(rddir_args, &rddir_res);

		free(rddir_args->rda_map);
		free(rddir_args);

		if (!encode_res(xdr_autofs_rddirres, &door_res,
		    (caddr_t)&rddir_res, &res_size)) {
			syslog(LOG_ERR,
			    "error allocating readdir results buffer");
			failed_res.res_status = ENOMEM;
			failed_res.xdr_len = 0;
			res = (caddr_t)&failed_res;
			res_size = sizeof (autofs_door_res_t);
		} else {
			door_res->res_status = 0;
			res = (caddr_t)door_res;
		}
		autofs_readdir_1_free_r(&rddir_res);
		break;
#ifdef MALLOC_DEBUG
	case AUTOFS_DUMP_DEBUG:
			check_leaks("/var/tmp/automountd.leak");
			error = door_return(NULL, 0, NULL, 0);
			/*
			 * If we got here, door_return() failed
			 */
			syslog(LOG_ERR, "dump debug door_return failure %d",
			    error);
			return;
#endif
	case NULLPROC:
			res = NULL;
			res_size = 0;
			break;
	default:
			failed_res.res_status = EINVAL;
			res = (char *)&failed_res;
			res_size = sizeof (autofs_door_res_t);
			break;
	}

	srsz = res_size;
	errno = 0;
	error = door_return(res, res_size, NULL, 0);

	if (errno == E2BIG) {
		/*
		 * Failed due to encoded results being bigger than the
		 * kernel expected bufsize. Passing actual results size
		 * back down to kernel.
		 */
		failed_res.res_status = EOVERFLOW;
		failed_res.xdr_len = srsz;
		res = (caddr_t)&failed_res;
		res_size = sizeof (autofs_door_res_t);
	} else {
		syslog(LOG_ERR, "door_return failed %d, buffer %p, "
		    "buffer size %d", error, (void *)res, res_size);
		res = NULL;
		res_size = 0;
	}
	(void) door_return(res, res_size, NULL, 0);
	/* NOTREACHED */
}

static int
start_autofs_svcs(void)
{
	int doorfd;
#ifdef DEBUG
	int dfd;
#endif

	if ((doorfd = door_create(autofs_doorfunc, NULL,
	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
		syslog(LOG_ERR, gettext("Unable to create door\n"));
		return (1);
	}

#ifdef DEBUG
	/*
	 * Create a file system path for the door
	 */
	if ((dfd = open(AUTOFS_DOOR, O_RDWR|O_CREAT|O_TRUNC,
	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) {
		syslog(LOG_ERR, "Unable to open %s: %m\n", AUTOFS_DOOR);
		(void) close(doorfd);
		return (1);
	}

	/*
	 * stale associations clean up
	 */
	(void) fdetach(AUTOFS_DOOR);

	/*
	 * Register in the namespace to the kernel to door_ki_open.
	 */
	if (fattach(doorfd, AUTOFS_DOOR) == -1) {
		syslog(LOG_ERR, "Unable to fattach door %m\n", AUTOFS_DOOR);
		(void) close(dfd);
		(void) close(doorfd);
		return (1);
	}
#endif /* DEBUG */

	/*
	 * Pass door name to kernel for door_ki_open
	 */
	autofs_setdoor(doorfd);

	(void) thr_keycreate(&s_thr_key, NULL);

	/*
	 * Wait for incoming calls
	 */
	/*CONSTCOND*/
	while (1)
		(void) pause();

	/* NOTREACHED */
	syslog(LOG_ERR, gettext("Door server exited"));
	return (10);
}

static int
decode_args(
	xdrproc_t xdrfunc,
	autofs_door_args_t *argp,
	caddr_t *xdrargs,
	int size)
{
	XDR xdrs;

	caddr_t tmpargs = (caddr_t)&((autofs_door_args_t *)argp)->xdr_arg;
	size_t arg_size = ((autofs_door_args_t *)argp)->xdr_len;

	xdrmem_create(&xdrs, tmpargs, arg_size, XDR_DECODE);

	*xdrargs = malloc(size);
	if (*xdrargs == NULL) {
		syslog(LOG_ERR, "error allocating arguments buffer");
		return (ENOMEM);
	}

	bzero(*xdrargs, size);

	if (!(*xdrfunc)(&xdrs, *xdrargs)) {
		free(*xdrargs);
		*xdrargs = NULL;
		syslog(LOG_ERR, "error decoding arguments");
		return (EINVAL);
	}

	return (0);
}


static bool_t
encode_res(
	xdrproc_t xdrfunc,
	autofs_door_res_t **results,
	caddr_t resp,
	int *size)
{
	XDR xdrs;

	*size = xdr_sizeof((*xdrfunc), resp);
	*results = autofs_get_buffer(
	    sizeof (autofs_door_res_t) + *size);
	if (*results == NULL) {
		(*results)->res_status = ENOMEM;
		return (FALSE);
	}
	(*results)->xdr_len = *size;
	*size = sizeof (autofs_door_res_t) + (*results)->xdr_len;
	xdrmem_create(&xdrs, (caddr_t)((*results)->xdr_res),
	    (*results)->xdr_len, XDR_ENCODE);
	if (!(*xdrfunc)(&xdrs, resp)) {
		(*results)->res_status = EINVAL;
		syslog(LOG_ERR, "error encoding results");
		return (FALSE);
	}
	(*results)->res_status = 0;
	return (TRUE);
}

static void
automountd_wait_for_cleanup(pid_t pid)
{
	int status;
	int child_exitval;

	/*
	 * Wait for the main automountd process to exit so we cleanup
	 */
	(void) waitpid(pid, &status, 0);

	child_exitval = WEXITSTATUS(status);

	/*
	 * Shutdown the door server for mounting and unmounting
	 * filesystems
	 */
	if (door_revoke(did_fork_exec) == -1) {
		syslog(LOG_ERR, "failed to door_revoke(%d) %m", did_fork_exec);
	}
	if (door_revoke(did_exec_map) == -1) {
		syslog(LOG_ERR, "failed to door_revoke(%d) %m", did_exec_map);
	}
	exit(child_exitval);
}