xref: /titanic_51/usr/src/cmd/syseventd/daemons/syseventconfd/syseventconfd.c (revision 80ab886d233f514d54c2a6bdeb9fdfd951bd6881)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*80ab886dSwesolows  * Common Development and Distribution License (the "License").
6*80ab886dSwesolows  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*80ab886dSwesolows 
227c478bd9Sstevel@tonic-gate /*
23*80ab886dSwesolows  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * syseventconfd - The sysevent conf daemon
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * This daemon is a companion to the sysevent_conf_mod module.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * The sysevent_conf_mod module receives events from syseventd,
357c478bd9Sstevel@tonic-gate  * and compares those events against event specs in the
367c478bd9Sstevel@tonic-gate  * sysevent.conf files.  For each matching event spec, the
377c478bd9Sstevel@tonic-gate  * specified command is invoked.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * This daemon manages the fork/exec's on behalf of sysevent_conf_mod.
407c478bd9Sstevel@tonic-gate  * The events and associated nvlist are delivered via a door upcall
417c478bd9Sstevel@tonic-gate  * from sysevent_conf_mod.  Arriving events are queued, and the
427c478bd9Sstevel@tonic-gate  * main thread of this daemon dequeues events one by one, and
437c478bd9Sstevel@tonic-gate  * builds the necessary arguments to fork/exec the command.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * Since sysevent_conf_mod is running in the context of syseventd,
467c478bd9Sstevel@tonic-gate  * invoking the fork/exec from that module blocks the door upcalls
477c478bd9Sstevel@tonic-gate  * from the kernel delivering events to syseventd.  We avoid a
487c478bd9Sstevel@tonic-gate  * major performance bottleneck in this fashion.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <stdio.h>
527c478bd9Sstevel@tonic-gate #include <stdarg.h>
537c478bd9Sstevel@tonic-gate #include <stddef.h>
547c478bd9Sstevel@tonic-gate #include <stdlib.h>
557c478bd9Sstevel@tonic-gate #include <errno.h>
567c478bd9Sstevel@tonic-gate #include <fcntl.h>
577c478bd9Sstevel@tonic-gate #include <signal.h>
587c478bd9Sstevel@tonic-gate #include <strings.h>
597c478bd9Sstevel@tonic-gate #include <unistd.h>
607c478bd9Sstevel@tonic-gate #include <synch.h>
617c478bd9Sstevel@tonic-gate #include <syslog.h>
627c478bd9Sstevel@tonic-gate #include <pthread.h>
637c478bd9Sstevel@tonic-gate #include <door.h>
647c478bd9Sstevel@tonic-gate #include <libsysevent.h>
657c478bd9Sstevel@tonic-gate #include <limits.h>
667c478bd9Sstevel@tonic-gate #include <locale.h>
677c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
687c478bd9Sstevel@tonic-gate #include <sys/stat.h>
697c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
707c478bd9Sstevel@tonic-gate #include <sys/wait.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #include "syseventconfd.h"
737c478bd9Sstevel@tonic-gate #include "syseventconfd_door.h"
747c478bd9Sstevel@tonic-gate #include "message_confd.h"
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static int	debug_level	= 0;
797c478bd9Sstevel@tonic-gate static char	*root_dir	= "";	/* Relative root for lock and door */
807c478bd9Sstevel@tonic-gate static char	*prog;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static struct cmd	*cmd_list;
837c478bd9Sstevel@tonic-gate static struct cmd	*cmd_tail;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static mutex_t		cmd_list_lock;
867c478bd9Sstevel@tonic-gate static cond_t		cmd_list_cv;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate extern char *optarg;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Support for door server thread handling
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate #define	MAX_SERVER_THREADS	1
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static mutex_t create_cnt_lock;
967c478bd9Sstevel@tonic-gate static int cnt_servers = 0;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static void
1007c478bd9Sstevel@tonic-gate usage() {
1017c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: syseventconfd [-d <debug_level>]\n");
1027c478bd9Sstevel@tonic-gate 	exit(2);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static void
1077c478bd9Sstevel@tonic-gate set_root_dir(char *dir)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	root_dir = malloc(strlen(dir) + 1);
1107c478bd9Sstevel@tonic-gate 	if (root_dir == NULL) {
1117c478bd9Sstevel@tonic-gate 		syserrmsg(INIT_ROOT_DIR_ERR, strerror(errno));
1127c478bd9Sstevel@tonic-gate 		exit(2);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	(void) strcpy(root_dir, dir);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
118*80ab886dSwesolows int
1197c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	int c;
1227c478bd9Sstevel@tonic-gate 	int fd;
1237c478bd9Sstevel@tonic-gate 	sigset_t set;
1247c478bd9Sstevel@tonic-gate 	struct cmd *cmd;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1277c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (getuid() != 0) {
1307c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Must be root to run syseventconfd\n");
1317c478bd9Sstevel@tonic-gate 		exit(1);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
1357c478bd9Sstevel@tonic-gate 		prog = argv[0];
1367c478bd9Sstevel@tonic-gate 	} else {
1377c478bd9Sstevel@tonic-gate 		prog++;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if ((c = getopt(argc, argv, "d:r:")) != EOF) {
1417c478bd9Sstevel@tonic-gate 		switch (c) {
1427c478bd9Sstevel@tonic-gate 		case 'd':
1437c478bd9Sstevel@tonic-gate 			debug_level = atoi(optarg);
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case 'r':
1467c478bd9Sstevel@tonic-gate 			/*
1477c478bd9Sstevel@tonic-gate 			 * Private flag for suninstall to run
1487c478bd9Sstevel@tonic-gate 			 * daemon during install.
1497c478bd9Sstevel@tonic-gate 			 */
1507c478bd9Sstevel@tonic-gate 			set_root_dir(optarg);
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		case '?':
1537c478bd9Sstevel@tonic-gate 		default:
1547c478bd9Sstevel@tonic-gate 			usage();
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (fork()) {
1607c478bd9Sstevel@tonic-gate 		exit(0);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	(void) chdir("/");
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	(void) setsid();
1667c478bd9Sstevel@tonic-gate 	if (debug_level <= 1) {
1677c478bd9Sstevel@tonic-gate 		closefrom(0);
1687c478bd9Sstevel@tonic-gate 		fd = open("/dev/null", 0);
1697c478bd9Sstevel@tonic-gate 		(void) dup2(fd, 1);
1707c478bd9Sstevel@tonic-gate 		(void) dup2(fd, 2);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	openlog("syseventconfd", LOG_PID, LOG_DAEMON);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	printmsg(1,
1767c478bd9Sstevel@tonic-gate 	    "syseventconfd started, debug level = %d\n", debug_level);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/*
1797c478bd9Sstevel@tonic-gate 	 * Block all signals to all threads include the main thread.
1807c478bd9Sstevel@tonic-gate 	 * The sigwait_thr thread will catch and process all signals.
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	(void) sigfillset(&set);
1837c478bd9Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_BLOCK, &set, NULL);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/* Create signal catching thread */
1867c478bd9Sstevel@tonic-gate 	if (thr_create(NULL, NULL, (void *(*)(void *))sigwait_thr,
1877c478bd9Sstevel@tonic-gate 		(void *)NULL, 0, NULL) < 0) {
1887c478bd9Sstevel@tonic-gate 		syserrmsg(INIT_THR_CREATE_ERR, strerror(errno));
1897c478bd9Sstevel@tonic-gate 		exit(2);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
1937c478bd9Sstevel@tonic-gate 	 * Init mutex and list of cmds to be fork/exec'ed
1947c478bd9Sstevel@tonic-gate 	 * This is multi-threaded so the fork/exec can be
1957c478bd9Sstevel@tonic-gate 	 * done without blocking the door upcall.
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	cmd_list = NULL;
1987c478bd9Sstevel@tonic-gate 	cmd_tail = NULL;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	(void) mutex_init(&create_cnt_lock, USYNC_THREAD, NULL);
2017c478bd9Sstevel@tonic-gate 	(void) mutex_init(&cmd_list_lock, USYNC_THREAD, NULL);
2027c478bd9Sstevel@tonic-gate 	(void) cond_init(&cmd_list_cv, USYNC_THREAD, NULL);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * Open communication channel from sysevent_conf_mod
2067c478bd9Sstevel@tonic-gate 	 */
2077c478bd9Sstevel@tonic-gate 	if (open_channel() == NULL) {
2087c478bd9Sstevel@tonic-gate 		exit(1);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/*
2127c478bd9Sstevel@tonic-gate 	 * main thread to wait for events to arrive and be placed
2137c478bd9Sstevel@tonic-gate 	 * on the queue.  As events are queued, dequeue them
2147c478bd9Sstevel@tonic-gate 	 * here and invoke the associated fork/exec.
2157c478bd9Sstevel@tonic-gate 	 */
2167c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cmd_list_lock);
2177c478bd9Sstevel@tonic-gate 	for (;;) {
2187c478bd9Sstevel@tonic-gate 		while (cmd_list == NULL)
2197c478bd9Sstevel@tonic-gate 			(void) cond_wait(&cmd_list_cv, &cmd_list_lock);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		cmd = cmd_list;
2227c478bd9Sstevel@tonic-gate 		cmd_list = cmd->cmd_next;
2237c478bd9Sstevel@tonic-gate 		if (cmd_list == NULL)
2247c478bd9Sstevel@tonic-gate 			cmd_tail = NULL;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cmd_list_lock);
2277c478bd9Sstevel@tonic-gate 		exec_cmd(cmd);
2287c478bd9Sstevel@tonic-gate 		free_cmd(cmd);
2297c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&cmd_list_lock);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
232*80ab886dSwesolows 	return (0);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate  * Events sent via the door call from sysevent_conf_mod arrive
2377c478bd9Sstevel@tonic-gate  * here.  Queue each event for the main thread to invoke, and
2387c478bd9Sstevel@tonic-gate  * return.  We want to avoid doing the fork/exec while in the
2397c478bd9Sstevel@tonic-gate  * context of the door call.
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2427c478bd9Sstevel@tonic-gate static void
2437c478bd9Sstevel@tonic-gate event_handler(sysevent_t *event)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlist;
2467c478bd9Sstevel@tonic-gate 	struct cmd	*cmd;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	nvlist = NULL;
2497c478bd9Sstevel@tonic-gate 	if (sysevent_get_attr_list(event, &nvlist) != 0) {
2507c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, NO_NVLIST_ERR);
2517c478bd9Sstevel@tonic-gate 		return;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if ((cmd = alloc_cmd(nvlist)) != NULL) {
2557c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&cmd_list_lock);
2567c478bd9Sstevel@tonic-gate 		if (cmd_list == NULL) {
2577c478bd9Sstevel@tonic-gate 			cmd_list = cmd;
2587c478bd9Sstevel@tonic-gate 			cmd_tail = cmd;
2597c478bd9Sstevel@tonic-gate 		} else {
2607c478bd9Sstevel@tonic-gate 			cmd_tail->cmd_next = cmd;
2617c478bd9Sstevel@tonic-gate 			cmd_tail = cmd;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		cmd->cmd_next = NULL;
2647c478bd9Sstevel@tonic-gate 		(void) cond_signal(&cmd_list_cv);
2657c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&cmd_list_lock);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	nvlist_free(nvlist);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * Decode the command, build the exec args and fork/exec the command
2747c478bd9Sstevel@tonic-gate  * All command attributes are packed into the nvlist bundled with
2757c478bd9Sstevel@tonic-gate  * the delivered event.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate static void
2787c478bd9Sstevel@tonic-gate exec_cmd(struct cmd *cmd)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	char		*path;
2817c478bd9Sstevel@tonic-gate 	char		*cmdline;
2827c478bd9Sstevel@tonic-gate 	uid_t		uid;
2837c478bd9Sstevel@tonic-gate 	gid_t		gid;
2847c478bd9Sstevel@tonic-gate 	char		*file;
2857c478bd9Sstevel@tonic-gate 	int		line;
2867c478bd9Sstevel@tonic-gate 	char		*user;
2877c478bd9Sstevel@tonic-gate 	arg_t		*args;
2887c478bd9Sstevel@tonic-gate 	pid_t		pid;
2897c478bd9Sstevel@tonic-gate 	char		*lp;
2907c478bd9Sstevel@tonic-gate 	char 		*p;
2917c478bd9Sstevel@tonic-gate 	int		i;
2927c478bd9Sstevel@tonic-gate 	sigset_t	set, prior_set;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(cmd->cmd_nvlist, "user", &user) != 0) {
2957c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, NVLIST_FORMAT_ERR, "user");
2967c478bd9Sstevel@tonic-gate 		return;
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(cmd->cmd_nvlist, "file", &file) != 0) {
2997c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, NVLIST_FORMAT_ERR, "file");
3007c478bd9Sstevel@tonic-gate 		return;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(cmd->cmd_nvlist, "path", &path) != 0) {
3047c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "path");
3057c478bd9Sstevel@tonic-gate 		return;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(cmd->cmd_nvlist, "cmd", &cmdline) != 0) {
3087c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "cmd");
3097c478bd9Sstevel@tonic-gate 		return;
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_int32(cmd->cmd_nvlist, "line", &line) != 0) {
3127c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "line");
3137c478bd9Sstevel@tonic-gate 		return;
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_int32(cmd->cmd_nvlist, "uid", (int *)&uid) == 0) {
3167c478bd9Sstevel@tonic-gate 		if (nvlist_lookup_int32(cmd->cmd_nvlist,
3177c478bd9Sstevel@tonic-gate 		    "gid", (int *)&gid) != 0) {
3187c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "gid");
3197c478bd9Sstevel@tonic-gate 			return;
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 	} else {
3227c478bd9Sstevel@tonic-gate 		uid = 0;
3237c478bd9Sstevel@tonic-gate 		gid = 0;
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	args = init_arglist(32);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	lp = cmdline;
3297c478bd9Sstevel@tonic-gate 	while ((p = next_arg(&lp)) != NULL) {
3307c478bd9Sstevel@tonic-gate 		if (add_arg(args, p)) {
3317c478bd9Sstevel@tonic-gate 			free_arglist(args);
3327c478bd9Sstevel@tonic-gate 			return;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (debug_level >= DBG_EXEC) {
3377c478bd9Sstevel@tonic-gate 		printmsg(DBG_EXEC, "path=%s\n", path);
3387c478bd9Sstevel@tonic-gate 		printmsg(DBG_EXEC, "cmd=%s\n", cmdline);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if (debug_level >= DBG_EXEC_ARGS) {
3427c478bd9Sstevel@tonic-gate 		for (i = 0; i < args->arg_nargs; i++) {
3437c478bd9Sstevel@tonic-gate 			printmsg(DBG_EXEC_ARGS,
3447c478bd9Sstevel@tonic-gate 				"arg[%d]: '%s'\n", i, args->arg_args[i]);
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, NULL, &set);
3497c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGCHLD);
3507c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &set, &prior_set);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate again:
3537c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) == (pid_t)-1) {
3547c478bd9Sstevel@tonic-gate 		if (errno == EINTR)
3557c478bd9Sstevel@tonic-gate 			goto again;
3567c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, CANNOT_FORK_ERR, strerror(errno));
3577c478bd9Sstevel@tonic-gate 		free_arglist(args);
3587c478bd9Sstevel@tonic-gate 		return;
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	if (pid != (pid_t)0) {
3617c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, &prior_set, NULL);
3627c478bd9Sstevel@tonic-gate 		free_arglist(args);
3637c478bd9Sstevel@tonic-gate 		return;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * The child
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	(void) close(0);
3707c478bd9Sstevel@tonic-gate 	(void) close(1);
3717c478bd9Sstevel@tonic-gate 	(void) close(2);
3727c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDONLY);
3737c478bd9Sstevel@tonic-gate 	(void) dup2(0, 1);
3747c478bd9Sstevel@tonic-gate 	(void) dup2(0, 2);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (uid != (uid_t)0) {
3777c478bd9Sstevel@tonic-gate 		i = setgid(gid);
3787c478bd9Sstevel@tonic-gate 		if (i == 0)
3797c478bd9Sstevel@tonic-gate 			i = setuid(uid);
3807c478bd9Sstevel@tonic-gate 		if (i != 0) {
3817c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, SETUID_ERR,
3827c478bd9Sstevel@tonic-gate 				file, line, user, strerror(errno));
3837c478bd9Sstevel@tonic-gate 			_exit(0);
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * Unblock all signals in the child
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &prior_set, NULL);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (execv(path, args->arg_args) == -1) {
3937c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, CANNOT_EXEC_ERR,
3947c478bd9Sstevel@tonic-gate 			path, strerror(errno));
3957c478bd9Sstevel@tonic-gate 		_exit(0);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * Thread to handle in-coming signals
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate static void
4047c478bd9Sstevel@tonic-gate sigwait_thr()
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	int	sig;
4077c478bd9Sstevel@tonic-gate 	sigset_t signal_set;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/*
4107c478bd9Sstevel@tonic-gate 	 * SIGCHLD is ignored by default, and we need to handle this
4117c478bd9Sstevel@tonic-gate 	 * signal to reap the status of all children spawned by
4127c478bd9Sstevel@tonic-gate 	 * this daemon.
4137c478bd9Sstevel@tonic-gate 	 */
4147c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCHLD, reapchild);
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	for (;;) {
4177c478bd9Sstevel@tonic-gate 		(void) sigfillset(&signal_set);
4187c478bd9Sstevel@tonic-gate 		if (sigwait(&signal_set, &sig) == 0) {
4197c478bd9Sstevel@tonic-gate 			/*
4207c478bd9Sstevel@tonic-gate 			 * Block all signals until the signal handler completes
4217c478bd9Sstevel@tonic-gate 			 */
4227c478bd9Sstevel@tonic-gate 			(void) sigfillset(&signal_set);
4237c478bd9Sstevel@tonic-gate 			(void) thr_sigsetmask(SIG_BLOCK, &signal_set, NULL);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 			if (sig == SIGCHLD) {
4267c478bd9Sstevel@tonic-gate 				reapchild(sig);
4277c478bd9Sstevel@tonic-gate 			} else {
4287c478bd9Sstevel@tonic-gate 				flt_handler(sig);
4297c478bd9Sstevel@tonic-gate 			}
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * reapchild - reap the status of each child as it exits
4397c478bd9Sstevel@tonic-gate  */
4407c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4417c478bd9Sstevel@tonic-gate static void
4427c478bd9Sstevel@tonic-gate reapchild(int sig)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	siginfo_t info;
4457c478bd9Sstevel@tonic-gate 	char *signam;
4467c478bd9Sstevel@tonic-gate 	int err;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	for (;;) {
4497c478bd9Sstevel@tonic-gate 		(void) memset(&info, 0, sizeof (info));
4507c478bd9Sstevel@tonic-gate 		err = waitid(P_ALL, 0, &info, WNOHANG|WEXITED);
4517c478bd9Sstevel@tonic-gate 		if (err == -1) {
4527c478bd9Sstevel@tonic-gate 			if (errno != EINTR && errno != EAGAIN)
4537c478bd9Sstevel@tonic-gate 				return;
4547c478bd9Sstevel@tonic-gate 		} else if (info.si_pid == 0) {
4557c478bd9Sstevel@tonic-gate 			return;
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		if (debug_level >= DBG_CHILD) {
4597c478bd9Sstevel@tonic-gate 			printmsg(DBG_CHILD, CHILD_EXIT_STATUS_ERR,
4607c478bd9Sstevel@tonic-gate 				info.si_pid, info.si_status);
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		if (info.si_status) {
4647c478bd9Sstevel@tonic-gate 			if (info.si_code == CLD_EXITED) {
4657c478bd9Sstevel@tonic-gate 				syserrmsg(CHILD_EXIT_STATUS_ERR,
4667c478bd9Sstevel@tonic-gate 					info.si_pid, info.si_status);
4677c478bd9Sstevel@tonic-gate 			} else {
4687c478bd9Sstevel@tonic-gate 				signam = strsignal(info.si_status);
4697c478bd9Sstevel@tonic-gate 				if (signam == NULL)
4707c478bd9Sstevel@tonic-gate 					signam = "";
4717c478bd9Sstevel@tonic-gate 				if (info.si_code == CLD_DUMPED) {
4727c478bd9Sstevel@tonic-gate 					syserrmsg(
4737c478bd9Sstevel@tonic-gate 					    CHILD_EXIT_CORE_ERR,
4747c478bd9Sstevel@tonic-gate 					    info.si_pid, signam);
4757c478bd9Sstevel@tonic-gate 				} else {
4767c478bd9Sstevel@tonic-gate 					syserrmsg(
4777c478bd9Sstevel@tonic-gate 					    CHILD_EXIT_SIGNAL_ERR,
4787c478bd9Sstevel@tonic-gate 					    info.si_pid, signam);
4797c478bd9Sstevel@tonic-gate 				}
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * Fault handler for other signals caught
4887c478bd9Sstevel@tonic-gate  */
4897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4907c478bd9Sstevel@tonic-gate static void
4917c478bd9Sstevel@tonic-gate flt_handler(int sig)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	struct sigaction act;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	(void) memset(&act, 0, sizeof (act));
4967c478bd9Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
4977c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_RESTART;
4987c478bd9Sstevel@tonic-gate 	(void) sigfillset(&act.sa_mask);
4997c478bd9Sstevel@tonic-gate 	(void) sigaction(sig, &act, NULL);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	switch (sig) {
5027c478bd9Sstevel@tonic-gate 		case SIGINT:
5037c478bd9Sstevel@tonic-gate 		case SIGSTOP:
5047c478bd9Sstevel@tonic-gate 		case SIGTERM:
5057c478bd9Sstevel@tonic-gate 		case SIGHUP:
5067c478bd9Sstevel@tonic-gate 			exit(1);
5077c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate static arg_t *
5137c478bd9Sstevel@tonic-gate init_arglist(int hint)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	arg_t	*arglist;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	if ((arglist = sc_malloc(sizeof (arg_t))) == NULL)
5187c478bd9Sstevel@tonic-gate 		return (NULL);
5197c478bd9Sstevel@tonic-gate 	arglist->arg_args = NULL;
5207c478bd9Sstevel@tonic-gate 	arglist->arg_nargs = 0;
5217c478bd9Sstevel@tonic-gate 	arglist->arg_alloc = 0;
5227c478bd9Sstevel@tonic-gate 	arglist->arg_hint = hint;
5237c478bd9Sstevel@tonic-gate 	return (arglist);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate static void
5287c478bd9Sstevel@tonic-gate free_arglist(arg_t *arglist)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	if (arglist->arg_args) {
5317c478bd9Sstevel@tonic-gate 		free(arglist->arg_args);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 	free(arglist);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static int
5387c478bd9Sstevel@tonic-gate add_arg(arg_t *arglist, char *arg)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	char	**new_args;
5417c478bd9Sstevel@tonic-gate 	int	len;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	len = arglist->arg_nargs + 2;
5447c478bd9Sstevel@tonic-gate 	if (arglist->arg_alloc < len) {
5457c478bd9Sstevel@tonic-gate 		arglist->arg_alloc = len + arglist->arg_hint;
5467c478bd9Sstevel@tonic-gate 		new_args = (arglist->arg_nargs == 0) ?
5477c478bd9Sstevel@tonic-gate 			sc_malloc(arglist->arg_alloc * sizeof (char **)) :
5487c478bd9Sstevel@tonic-gate 			sc_realloc(arglist->arg_args,
5497c478bd9Sstevel@tonic-gate 				arglist->arg_alloc * sizeof (char **));
5507c478bd9Sstevel@tonic-gate 		if (new_args == NULL)
5517c478bd9Sstevel@tonic-gate 			return (1);
5527c478bd9Sstevel@tonic-gate 		arglist->arg_args = new_args;
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	arglist->arg_args[arglist->arg_nargs++] = arg;
5567c478bd9Sstevel@tonic-gate 	arglist->arg_args[arglist->arg_nargs] = NULL;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	return (0);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  * next_arg() is used to break up a command line
5637c478bd9Sstevel@tonic-gate  * into the arguments for execv(2).  Break up
5647c478bd9Sstevel@tonic-gate  * arguments separated by spaces, but respecting
5657c478bd9Sstevel@tonic-gate  * single/double quotes.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate static char *
5687c478bd9Sstevel@tonic-gate next_arg(char **cpp)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	char	*cp = *cpp;
5717c478bd9Sstevel@tonic-gate 	char	*start;
5727c478bd9Sstevel@tonic-gate 	char	quote;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t')
5757c478bd9Sstevel@tonic-gate 		cp++;
5767c478bd9Sstevel@tonic-gate 	if (*cp == 0) {
5777c478bd9Sstevel@tonic-gate 		*cpp = 0;
5787c478bd9Sstevel@tonic-gate 		return (NULL);
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 	start = cp;
5817c478bd9Sstevel@tonic-gate 	while (*cp && *cp != ' ' && *cp != '\t') {
5827c478bd9Sstevel@tonic-gate 		if (*cp == '"' || *cp == '\'') {
5837c478bd9Sstevel@tonic-gate 			quote = *cp++;
5847c478bd9Sstevel@tonic-gate 			while (*cp && *cp != quote) {
5857c478bd9Sstevel@tonic-gate 				cp++;
5867c478bd9Sstevel@tonic-gate 			}
5877c478bd9Sstevel@tonic-gate 			if (*cp == 0) {
5887c478bd9Sstevel@tonic-gate 				*cpp = 0;
5897c478bd9Sstevel@tonic-gate 				return (NULL);
5907c478bd9Sstevel@tonic-gate 			} else {
5917c478bd9Sstevel@tonic-gate 				cp++;
5927c478bd9Sstevel@tonic-gate 			}
5937c478bd9Sstevel@tonic-gate 		} else {
5947c478bd9Sstevel@tonic-gate 			cp++;
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	if (*cp != 0)
5987c478bd9Sstevel@tonic-gate 		*cp++ = 0;
5997c478bd9Sstevel@tonic-gate 	*cpp = cp;
6007c478bd9Sstevel@tonic-gate 	return (start);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate static struct cmd *
6057c478bd9Sstevel@tonic-gate alloc_cmd(nvlist_t *nvlist)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	struct cmd *cmd;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	cmd = sc_malloc(sizeof (struct cmd));
6107c478bd9Sstevel@tonic-gate 	if (cmd) {
6117c478bd9Sstevel@tonic-gate 		if (nvlist_dup(nvlist, &cmd->cmd_nvlist, 0) != 0) {
6127c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, OUT_OF_MEMORY_ERR);
6137c478bd9Sstevel@tonic-gate 			free(cmd);
6147c478bd9Sstevel@tonic-gate 			return (NULL);
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 	return (cmd);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate static void
6217c478bd9Sstevel@tonic-gate free_cmd(struct cmd *cmd)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	nvlist_free(cmd->cmd_nvlist);
6247c478bd9Sstevel@tonic-gate 	free(cmd);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate static void *
6297c478bd9Sstevel@tonic-gate sc_malloc(size_t n)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	void *p;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	p = malloc(n);
6347c478bd9Sstevel@tonic-gate 	if (p == NULL) {
6357c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, OUT_OF_MEMORY_ERR);
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 	return (p);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate static void *
6417c478bd9Sstevel@tonic-gate sc_realloc(void *p, size_t n)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate 	p = realloc(p, n);
6447c478bd9Sstevel@tonic-gate 	if (p == NULL) {
6457c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, OUT_OF_MEMORY_ERR);
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate 	return (p);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate  * syserrsg - print error messages to the terminal if not
6547c478bd9Sstevel@tonic-gate  *			yet daemonized or to syslog.
6557c478bd9Sstevel@tonic-gate  */
6567c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
6577c478bd9Sstevel@tonic-gate static void
6587c478bd9Sstevel@tonic-gate syserrmsg(char *message, ...)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	va_list ap;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	va_start(ap, message);
6637c478bd9Sstevel@tonic-gate 	(void) vsyslog(LOG_ERR, message, ap);
6647c478bd9Sstevel@tonic-gate 	va_end(ap);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate  * printmsg -  print messages to the terminal or to syslog
6697c478bd9Sstevel@tonic-gate  *			the following levels are implemented:
6707c478bd9Sstevel@tonic-gate  */
6717c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
6727c478bd9Sstevel@tonic-gate static void
6737c478bd9Sstevel@tonic-gate printmsg(int level, char *message, ...)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	va_list ap;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (level > debug_level) {
6787c478bd9Sstevel@tonic-gate 		return;
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	va_start(ap, message);
6827c478bd9Sstevel@tonic-gate 	(void) syslog(LOG_DEBUG, "%s[%ld]: ", prog, getpid());
6837c478bd9Sstevel@tonic-gate 	(void) vsyslog(LOG_DEBUG, message, ap);
6847c478bd9Sstevel@tonic-gate 	va_end(ap);
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate /* ARGSUSED */
6887c478bd9Sstevel@tonic-gate static void *
6897c478bd9Sstevel@tonic-gate create_door_thr(void *arg)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
6927c478bd9Sstevel@tonic-gate 	(void) door_return(NULL, 0, NULL, 0);
6937c478bd9Sstevel@tonic-gate 	return (NULL);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate /*
6977c478bd9Sstevel@tonic-gate  * Control creation of door server threads
6987c478bd9Sstevel@tonic-gate  *
6997c478bd9Sstevel@tonic-gate  * If first creation of server thread fails there is nothing
7007c478bd9Sstevel@tonic-gate  * we can do about. Doors would not work.
7017c478bd9Sstevel@tonic-gate  */
7027c478bd9Sstevel@tonic-gate /* ARGSUSED */
7037c478bd9Sstevel@tonic-gate static void
7047c478bd9Sstevel@tonic-gate mk_thr_pool(door_info_t *dip)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&create_cnt_lock);
7077c478bd9Sstevel@tonic-gate 	if (++cnt_servers > MAX_SERVER_THREADS) {
7087c478bd9Sstevel@tonic-gate 		cnt_servers--;
7097c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&create_cnt_lock);
7107c478bd9Sstevel@tonic-gate 		return;
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&create_cnt_lock);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	(void) thr_create(NULL, 0, create_door_thr, NULL,
7157c478bd9Sstevel@tonic-gate 	    THR_BOUND|THR_DETACHED, NULL);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate static sysevent_handle_t *
7197c478bd9Sstevel@tonic-gate open_channel()
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate 	char	door_path[MAXPATHLEN];
7227c478bd9Sstevel@tonic-gate 	const char *subclass_list;
7237c478bd9Sstevel@tonic-gate 	sysevent_handle_t *handle;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	if (snprintf(door_path, sizeof (door_path), "%s/%s",
7267c478bd9Sstevel@tonic-gate 	    root_dir, SYSEVENTCONFD_SERVICE_DOOR) >= sizeof (door_path)) {
7277c478bd9Sstevel@tonic-gate 		syserrmsg(CHANNEL_OPEN_ERR);
7287c478bd9Sstevel@tonic-gate 		return (NULL);
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	/*
7327c478bd9Sstevel@tonic-gate 	 * Setup of door server create function to limit the
7337c478bd9Sstevel@tonic-gate 	 * amount of door servers
7347c478bd9Sstevel@tonic-gate 	 */
7357c478bd9Sstevel@tonic-gate 	(void) door_server_create(mk_thr_pool);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	handle = sysevent_open_channel_alt(door_path);
7387c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
7397c478bd9Sstevel@tonic-gate 		syserrmsg(CHANNEL_OPEN_ERR);
7407c478bd9Sstevel@tonic-gate 		return (NULL);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 	if (sysevent_bind_subscriber(handle, event_handler) != 0) {
7437c478bd9Sstevel@tonic-gate 		syserrmsg(CHANNEL_BIND_ERR);
7447c478bd9Sstevel@tonic-gate 		sysevent_close_channel(handle);
7457c478bd9Sstevel@tonic-gate 		return (NULL);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 	subclass_list = EC_SUB_ALL;
7487c478bd9Sstevel@tonic-gate 	if (sysevent_register_event(handle, EC_ALL, &subclass_list, 1)
7497c478bd9Sstevel@tonic-gate 	    != 0) {
7507c478bd9Sstevel@tonic-gate 		syserrmsg(CHANNEL_BIND_ERR);
7517c478bd9Sstevel@tonic-gate 		(void) sysevent_unbind_subscriber(handle);
7527c478bd9Sstevel@tonic-gate 		(void) sysevent_close_channel(handle);
7537c478bd9Sstevel@tonic-gate 		return (NULL);
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 	return (handle);
7567c478bd9Sstevel@tonic-gate }
757