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