|
root / base / usr / src / cmd / syseventd / daemons / syseventconfd
syseventconfd Plain Text 1027 lines 21.8 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
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"%Z%%M%	%I%	%E% SMI"
#

PROG =	syseventconfd
SRCS =	$(PROG:%=%.c)

include ../Makefile.com

LDLIBS += -lnvpair

.KEEP_STATE:

all: $(PROG)

install: all				\
	$(ROOTLIBSYSEVENTDIR)		\
	$(ROOTLIBSYSEVENTSYSEVENTCONFD)

include ../Makefile.targ
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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) 2000 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#ifndef	_MESSAGE_CONFD_H
#define	_MESSAGE_CONFD_H

#ifdef	__cplusplus
extern "C" {
#endif


#define	OUT_OF_MEMORY_ERR	\
	gettext("Out of memory.")

#define	INIT_ROOT_DIR_ERR \
	gettext("Initialization error: could not allocate space for the local \
		root directory - %s\n")

#define	INIT_THR_CREATE_ERR \
	gettext("Initialization error:could not create signal thread - '%s' \
		\n")

#define	CANNOT_FORK_ERR	\
	gettext("cannot fork - %s\n")

#define	SETUID_ERR	\
	gettext("%s, line %d: " "cannot setuid to user '%s' - %s\n")

#define	CANNOT_EXEC_ERR	\
	gettext("cannot exec %s - %s\n")

#define	CHILD_EXIT_STATUS_ERR \
	gettext("process %d exited with status %d\n")

#define	CHILD_EXIT_CORE_ERR \
	gettext("process %d dumped core - %s\n")

#define	CHILD_EXIT_SIGNAL_ERR \
	gettext("process %d - %s\n")

#define	CHANNEL_OPEN_ERR \
	gettext("unable to open channel to syseventd\n")

#define	CHANNEL_BIND_ERR \
	gettext("unable to bind channel to syseventd\n")

#define	NO_NVLIST_ERR \
	gettext("missing nvlist\n")

#define	NVLIST_FORMAT_ERR \
	gettext("nvlist missing '%s'\n")

#define	NVLIST_FILE_LINE_FORMAT_ERR \
	gettext("%s, line %d: nvlist missing '%s'\n")


#ifdef	__cplusplus
}
#endif

#endif	/* _MESSAGE_CONFD_H */
/*
 * 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 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * syseventconfd - The sysevent conf daemon
 *
 * This daemon is a companion to the sysevent_conf_mod module.
 *
 * The sysevent_conf_mod module receives events from syseventd,
 * and compares those events against event specs in the
 * sysevent.conf files.  For each matching event spec, the
 * specified command is invoked.
 *
 * This daemon manages the fork/exec's on behalf of sysevent_conf_mod.
 * The events and associated nvlist are delivered via a door upcall
 * from sysevent_conf_mod.  Arriving events are queued, and the
 * main thread of this daemon dequeues events one by one, and
 * builds the necessary arguments to fork/exec the command.
 *
 * Since sysevent_conf_mod is running in the context of syseventd,
 * invoking the fork/exec from that module blocks the door upcalls
 * from the kernel delivering events to syseventd.  We avoid a
 * major performance bottleneck in this fashion.
 */

#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <strings.h>
#include <unistd.h>
#include <synch.h>
#include <syslog.h>
#include <pthread.h>
#include <door.h>
#include <libsysevent.h>
#include <limits.h>
#include <locale.h>
#include <sys/modctl.h>
#include <sys/stat.h>
#include <sys/systeminfo.h>
#include <sys/wait.h>

#include "syseventconfd.h"
#include "syseventconfd_door.h"
#include "message_confd.h"



static int	debug_level	= 0;
static char	*root_dir	= "";	/* Relative root for lock and door */
static char	*prog;

static struct cmd	*cmd_list;
static struct cmd	*cmd_tail;

static mutex_t		cmd_list_lock;
static cond_t		cmd_list_cv;

extern char *optarg;

/*
 * Support for door server thread handling
 */
#define	MAX_SERVER_THREADS	1

static mutex_t create_cnt_lock;
static int cnt_servers = 0;


static void
usage() {
	(void) fprintf(stderr, "usage: syseventconfd [-d <debug_level>]\n");
	exit(2);
}


static void
set_root_dir(char *dir)
{
	root_dir = malloc(strlen(dir) + 1);
	if (root_dir == NULL) {
		syserrmsg(INIT_ROOT_DIR_ERR, strerror(errno));
		exit(2);
	}
	(void) strcpy(root_dir, dir);
}


int
main(int argc, char **argv)
{
	int c;
	int fd;
	sigset_t set;
	struct cmd *cmd;

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	if (getuid() != 0) {
		(void) fprintf(stderr, "Must be root to run syseventconfd\n");
		exit(1);
	}

	if ((prog = strrchr(argv[0], '/')) == NULL) {
		prog = argv[0];
	} else {
		prog++;
	}

	if ((c = getopt(argc, argv, "d:r:")) != EOF) {
		switch (c) {
		case 'd':
			debug_level = atoi(optarg);
			break;
		case 'r':
			/*
			 * Private flag for suninstall to run
			 * daemon during install.
			 */
			set_root_dir(optarg);
			break;
		case '?':
		default:
			usage();
		}
	}


	if (fork()) {
		exit(0);
	}

	(void) chdir("/");

	(void) setsid();
	if (debug_level <= 1) {
		closefrom(0);
		fd = open("/dev/null", 0);
		(void) dup2(fd, 1);
		(void) dup2(fd, 2);
	}

	openlog("syseventconfd", LOG_PID, LOG_DAEMON);

	printmsg(1,
	    "syseventconfd started, debug level = %d\n", debug_level);

	/*
	 * Block all signals to all threads include the main thread.
	 * The sigwait_thr thread will catch and process all signals.
	 */
	(void) sigfillset(&set);
	(void) thr_sigsetmask(SIG_BLOCK, &set, NULL);

	/* Create signal catching thread */
	if (thr_create(NULL, 0, (void *(*)(void *))sigwait_thr,
		NULL, 0, NULL) < 0) {
		syserrmsg(INIT_THR_CREATE_ERR, strerror(errno));
		exit(2);
	}

	/*
	 * Init mutex and list of cmds to be fork/exec'ed
	 * This is multi-threaded so the fork/exec can be
	 * done without blocking the door upcall.
	 */
	cmd_list = NULL;
	cmd_tail = NULL;

	(void) mutex_init(&create_cnt_lock, USYNC_THREAD, NULL);
	(void) mutex_init(&cmd_list_lock, USYNC_THREAD, NULL);
	(void) cond_init(&cmd_list_cv, USYNC_THREAD, NULL);

	/*
	 * Open communication channel from sysevent_conf_mod
	 */
	if (open_channel() == NULL) {
		exit(1);
	}

	/*
	 * main thread to wait for events to arrive and be placed
	 * on the queue.  As events are queued, dequeue them
	 * here and invoke the associated fork/exec.
	 */
	(void) mutex_lock(&cmd_list_lock);
	for (;;) {
		while (cmd_list == NULL)
			(void) cond_wait(&cmd_list_cv, &cmd_list_lock);

		cmd = cmd_list;
		cmd_list = cmd->cmd_next;
		if (cmd_list == NULL)
			cmd_tail = NULL;

		(void) mutex_unlock(&cmd_list_lock);
		exec_cmd(cmd);
		free_cmd(cmd);
		(void) mutex_lock(&cmd_list_lock);
	}
	/* NOTREACHED */
	return (0);
}

/*
 * Events sent via the door call from sysevent_conf_mod arrive
 * here.  Queue each event for the main thread to invoke, and
 * return.  We want to avoid doing the fork/exec while in the
 * context of the door call.
 */
/*ARGSUSED*/
static void
event_handler(sysevent_t *event)
{
	nvlist_t	*nvlist;
	struct cmd	*cmd;

	nvlist = NULL;
	if (sysevent_get_attr_list(event, &nvlist) != 0) {
		syslog(LOG_ERR, NO_NVLIST_ERR);
		return;
	}

	if ((cmd = alloc_cmd(nvlist)) != NULL) {
		(void) mutex_lock(&cmd_list_lock);
		if (cmd_list == NULL) {
			cmd_list = cmd;
			cmd_tail = cmd;
		} else {
			cmd_tail->cmd_next = cmd;
			cmd_tail = cmd;
		}
		cmd->cmd_next = NULL;
		(void) cond_signal(&cmd_list_cv);
		(void) mutex_unlock(&cmd_list_lock);
	}

	nvlist_free(nvlist);
}


/*
 * Decode the command, build the exec args and fork/exec the command
 * All command attributes are packed into the nvlist bundled with
 * the delivered event.
 */
static void
exec_cmd(struct cmd *cmd)
{
	char		*path;
	char		*cmdline;
	uid_t		uid;
	gid_t		gid;
	char		*file;
	int		line;
	char		*user;
	arg_t		*args;
	pid_t		pid;
	char		*lp;
	char		*p;
	int		i;
	sigset_t	set, prior_set;

	if (nvlist_lookup_string(cmd->cmd_nvlist, "user", &user) != 0) {
		syslog(LOG_ERR, NVLIST_FORMAT_ERR, "user");
		return;
	}
	if (nvlist_lookup_string(cmd->cmd_nvlist, "file", &file) != 0) {
		syslog(LOG_ERR, NVLIST_FORMAT_ERR, "file");
		return;
	}

	if (nvlist_lookup_string(cmd->cmd_nvlist, "path", &path) != 0) {
		syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "path");
		return;
	}
	if (nvlist_lookup_string(cmd->cmd_nvlist, "cmd", &cmdline) != 0) {
		syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "cmd");
		return;
	}
	if (nvlist_lookup_int32(cmd->cmd_nvlist, "line", &line) != 0) {
		syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "line");
		return;
	}
	if (nvlist_lookup_int32(cmd->cmd_nvlist, "uid", (int *)&uid) == 0) {
		if (nvlist_lookup_int32(cmd->cmd_nvlist,
		    "gid", (int *)&gid) != 0) {
			syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "gid");
			return;
		}
	} else {
		uid = 0;
		gid = 0;
	}

	args = init_arglist(32);

	lp = cmdline;
	while ((p = next_arg(&lp)) != NULL) {
		if (add_arg(args, p)) {
			free_arglist(args);
			return;
		}
	}

	if (debug_level >= DBG_EXEC) {
		printmsg(DBG_EXEC, "path=%s\n", path);
		printmsg(DBG_EXEC, "cmd=%s\n", cmdline);
	}

	if (debug_level >= DBG_EXEC_ARGS) {
		for (i = 0; i < args->arg_nargs; i++) {
			printmsg(DBG_EXEC_ARGS,
				"arg[%d]: '%s'\n", i, args->arg_args[i]);
		}
	}

	(void) sigprocmask(SIG_SETMASK, NULL, &set);
	(void) sigaddset(&set, SIGCHLD);
	(void) sigprocmask(SIG_SETMASK, &set, &prior_set);

again:
	if ((pid = fork1()) == (pid_t)-1) {
		if (errno == EINTR)
			goto again;
		syslog(LOG_ERR, CANNOT_FORK_ERR, strerror(errno));
		free_arglist(args);
		return;
	}
	if (pid != (pid_t)0) {
		(void) sigprocmask(SIG_SETMASK, &prior_set, NULL);
		free_arglist(args);
		return;
	}

	/*
	 * The child
	 */
	(void) close(0);
	(void) close(1);
	(void) close(2);
	(void) open("/dev/null", O_RDONLY);
	(void) dup2(0, 1);
	(void) dup2(0, 2);

	if (uid != (uid_t)0) {
		i = setgid(gid);
		if (i == 0)
			i = setuid(uid);
		if (i != 0) {
			syslog(LOG_ERR, SETUID_ERR,
				file, line, user, strerror(errno));
			_exit(0);
		}
	}

	/*
	 * Unblock all signals in the child
	 */
	(void) sigprocmask(SIG_UNBLOCK, &prior_set, NULL);

	if (execv(path, args->arg_args) == -1) {
		syslog(LOG_ERR, CANNOT_EXEC_ERR,
			path, strerror(errno));
		_exit(0);
	}
}


/*
 * Thread to handle in-coming signals
 */
static void
sigwait_thr()
{
	int	sig;
	sigset_t signal_set;

	/*
	 * SIGCHLD is ignored by default, and we need to handle this
	 * signal to reap the status of all children spawned by
	 * this daemon.
	 */
	(void) sigset(SIGCHLD, reapchild);

	for (;;) {
		(void) sigfillset(&signal_set);
		if (sigwait(&signal_set, &sig) == 0) {
			/*
			 * Block all signals until the signal handler completes
			 */
			(void) sigfillset(&signal_set);
			(void) thr_sigsetmask(SIG_BLOCK, &signal_set, NULL);

			if (sig == SIGCHLD) {
				reapchild(sig);
			} else {
				flt_handler(sig);
			}
		}
	}
	/* NOTREACHED */
}



/*
 * reapchild - reap the status of each child as it exits
 */
/*ARGSUSED*/
static void
reapchild(int sig)
{
	siginfo_t info;
	char *signam;
	int err;

	for (;;) {
		(void) memset(&info, 0, sizeof (info));
		err = waitid(P_ALL, 0, &info, WNOHANG|WEXITED);
		if (err == -1) {
			if (errno != EINTR && errno != EAGAIN)
				return;
		} else if (info.si_pid == 0) {
			return;
		}

		if (debug_level >= DBG_CHILD) {
			printmsg(DBG_CHILD, CHILD_EXIT_STATUS_ERR,
				info.si_pid, info.si_status);
		}

		if (info.si_status) {
			if (info.si_code == CLD_EXITED) {
				syserrmsg(CHILD_EXIT_STATUS_ERR,
					info.si_pid, info.si_status);
			} else {
				signam = strsignal(info.si_status);
				if (signam == NULL)
					signam = "";
				if (info.si_code == CLD_DUMPED) {
					syserrmsg(
					    CHILD_EXIT_CORE_ERR,
					    info.si_pid, signam);
				} else {
					syserrmsg(
					    CHILD_EXIT_SIGNAL_ERR,
					    info.si_pid, signam);
				}
			}
		}
	}
}


/*
 * Fault handler for other signals caught
 */
/*ARGSUSED*/
static void
flt_handler(int sig)
{
	struct sigaction act;

	(void) memset(&act, 0, sizeof (act));
	act.sa_handler = SIG_DFL;
	act.sa_flags = SA_RESTART;
	(void) sigfillset(&act.sa_mask);
	(void) sigaction(sig, &act, NULL);

	switch (sig) {
		case SIGINT:
		case SIGSTOP:
		case SIGTERM:
		case SIGHUP:
			exit(1);
			/*NOTREACHED*/
	}
}


static arg_t *
init_arglist(int hint)
{
	arg_t	*arglist;

	if ((arglist = sc_malloc(sizeof (arg_t))) == NULL)
		return (NULL);
	arglist->arg_args = NULL;
	arglist->arg_nargs = 0;
	arglist->arg_alloc = 0;
	arglist->arg_hint = hint;
	return (arglist);
}


static void
free_arglist(arg_t *arglist)
{
	if (arglist->arg_args) {
		free(arglist->arg_args);
	}
	free(arglist);
}


static int
add_arg(arg_t *arglist, char *arg)
{
	char	**new_args;
	int	len;

	len = arglist->arg_nargs + 2;
	if (arglist->arg_alloc < len) {
		arglist->arg_alloc = len + arglist->arg_hint;
		new_args = (arglist->arg_nargs == 0) ?
			sc_malloc(arglist->arg_alloc * sizeof (char **)) :
			sc_realloc(arglist->arg_args,
				arglist->arg_alloc * sizeof (char **));
		if (new_args == NULL)
			return (1);
		arglist->arg_args = new_args;
	}

	arglist->arg_args[arglist->arg_nargs++] = arg;
	arglist->arg_args[arglist->arg_nargs] = NULL;

	return (0);
}

/*
 * next_arg() is used to break up a command line
 * into the arguments for execv(2).  Break up
 * arguments separated by spaces, but respecting
 * single/double quotes.
 */
static char *
next_arg(char **cpp)
{
	char	*cp = *cpp;
	char	*start;
	char	quote;

	while (*cp == ' ' || *cp == '\t')
		cp++;
	if (*cp == 0) {
		*cpp = 0;
		return (NULL);
	}
	start = cp;
	while (*cp && *cp != ' ' && *cp != '\t') {
		if (*cp == '"' || *cp == '\'') {
			quote = *cp++;
			while (*cp && *cp != quote) {
				cp++;
			}
			if (*cp == 0) {
				*cpp = 0;
				return (NULL);
			} else {
				cp++;
			}
		} else {
			cp++;
		}
	}
	if (*cp != 0)
		*cp++ = 0;
	*cpp = cp;
	return (start);
}


static struct cmd *
alloc_cmd(nvlist_t *nvlist)
{
	struct cmd *cmd;

	cmd = sc_malloc(sizeof (struct cmd));
	if (cmd) {
		if (nvlist_dup(nvlist, &cmd->cmd_nvlist, 0) != 0) {
			syslog(LOG_ERR, OUT_OF_MEMORY_ERR);
			free(cmd);
			return (NULL);
		}
	}
	return (cmd);
}

static void
free_cmd(struct cmd *cmd)
{
	nvlist_free(cmd->cmd_nvlist);
	free(cmd);
}


static void *
sc_malloc(size_t n)
{
	void *p;

	p = malloc(n);
	if (p == NULL) {
		syslog(LOG_ERR, OUT_OF_MEMORY_ERR);
	}
	return (p);
}

static void *
sc_realloc(void *p, size_t n)
{
	p = realloc(p, n);
	if (p == NULL) {
		syslog(LOG_ERR, OUT_OF_MEMORY_ERR);
	}
	return (p);
}



/*
 * syserrsg - print error messages to the terminal if not
 *			yet daemonized or to syslog.
 */
/*PRINTFLIKE1*/
static void
syserrmsg(char *message, ...)
{
	va_list ap;

	va_start(ap, message);
	(void) vsyslog(LOG_ERR, message, ap);
	va_end(ap);
}

/*
 * printmsg -  print messages to the terminal or to syslog
 *			the following levels are implemented:
 */
/*PRINTFLIKE2*/
static void
printmsg(int level, char *message, ...)
{
	va_list ap;

	if (level > debug_level) {
		return;
	}

	va_start(ap, message);
	(void) syslog(LOG_DEBUG, "%s[%ld]: ", prog, getpid());
	(void) vsyslog(LOG_DEBUG, message, ap);
	va_end(ap);
}

/* ARGSUSED */
static void *
create_door_thr(void *arg)
{
	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
	(void) door_return(NULL, 0, NULL, 0);
	return (NULL);
}

/*
 * Control creation of door server threads
 *
 * If first creation of server thread fails there is nothing
 * we can do about. Doors would not work.
 */
/* ARGSUSED */
static void
mk_thr_pool(door_info_t *dip)
{
	(void) mutex_lock(&create_cnt_lock);
	if (++cnt_servers > MAX_SERVER_THREADS) {
		cnt_servers--;
		(void) mutex_unlock(&create_cnt_lock);
		return;
	}
	(void) mutex_unlock(&create_cnt_lock);

	(void) thr_create(NULL, 0, create_door_thr, NULL,
	    THR_BOUND|THR_DETACHED, NULL);
}

static sysevent_handle_t *
open_channel()
{
	char	door_path[MAXPATHLEN];
	const char *subclass_list;
	sysevent_handle_t *handle;

	if (snprintf(door_path, sizeof (door_path), "%s/%s",
	    root_dir, SYSEVENTCONFD_SERVICE_DOOR) >= sizeof (door_path)) {
		syserrmsg(CHANNEL_OPEN_ERR);
		return (NULL);
	}

	/*
	 * Setup of door server create function to limit the
	 * amount of door servers
	 */
	(void) door_server_create(mk_thr_pool);

	handle = sysevent_open_channel_alt(door_path);
	if (handle == NULL) {
		syserrmsg(CHANNEL_OPEN_ERR);
		return (NULL);
	}
	if (sysevent_bind_subscriber(handle, event_handler) != 0) {
		syserrmsg(CHANNEL_BIND_ERR);
		sysevent_close_channel(handle);
		return (NULL);
	}
	subclass_list = EC_SUB_ALL;
	if (sysevent_register_event(handle, EC_ALL, &subclass_list, 1)
	    != 0) {
		syserrmsg(CHANNEL_BIND_ERR);
		(void) sysevent_unbind_subscriber(handle);
		(void) sysevent_close_channel(handle);
		return (NULL);
	}
	return (handle);
}
/*
 * 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 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_SYSEVENTCONFD_H
#define	_SYSEVENTCONFD_H

#ifdef	__cplusplus
extern "C" {
#endif


/*
 * debug levels
 */
#define	DBG_EXEC	1	/* path and args for each fork/exec */
#define	DBG_EVENTS	2	/* received events */
#define	DBG_CHILD	3	/* child exit status */
#define	DBG_EXEC_ARGS	4	/* more detail on exec args */


/*
 * list of cmds received from syseventd/sysevent_conf_mod
 */
struct cmd {
	nvlist_t	*cmd_nvlist;
	struct cmd	*cmd_next;
	struct cmd	*cmd_taiL;
};


/*
 * Structures for building arbitarily long strings and argument lists
 */
typedef struct arg {
	char	**arg_args;
	int	arg_nargs;
	int	arg_alloc;
	int	arg_hint;
} arg_t;

typedef struct str {
	char	*s_str;
	int	s_len;
	int	s_alloc;
	int	s_hint;
} str_t;


/*
 * Prototypes
 */
static void event_handler(sysevent_t *event);
static void exec_cmd(struct cmd *cmd);
static void sigwait_thr(void);
static void reapchild(int sig);
static void flt_handler(int sig);
static void syserrmsg(char *message, ...);
static void printmsg(int level, char *message, ...);
static void set_root_dir(char *dir);
static void usage(void);
static arg_t *init_arglist(int hint);
static void free_arglist(arg_t *arglist);
static int add_arg(arg_t *arglist, char *arg);
static char *next_arg(char **cpp);
static struct cmd *alloc_cmd(nvlist_t *nvlist);
static void free_cmd(struct cmd *cmd);
static void *sc_malloc(size_t n);
static void *sc_realloc(void *p, size_t n);
static sysevent_handle_t *open_channel(void);


#ifdef	__cplusplus
}
#endif

#endif	/* _SYSEVENTCONFD_H */
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_SYSEVENTCONFD_DOOR_H
#define	_SYSEVENTCONFD_DOOR_H

#ifdef	__cplusplus
extern "C" {
#endif

/*
 * Door for channel sysevent_conf_mod -> syseventconfd
 */
#define	SYSEVENTCONFD_SERVICE_DOOR \
	"/var/run/syseventconfd_door"



#ifdef	__cplusplus
}
#endif

#endif	/* _SYSEVENTCONFD_DOOR_H */