10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1717Swesolows * Common Development and Distribution License (the "License").
6*1717Swesolows * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*1717Swesolows
220Sstevel@tonic-gate /*
23*1717Swesolows * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * syseventconfd - The sysevent conf daemon
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * This daemon is a companion to the sysevent_conf_mod module.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * The sysevent_conf_mod module receives events from syseventd,
350Sstevel@tonic-gate * and compares those events against event specs in the
360Sstevel@tonic-gate * sysevent.conf files. For each matching event spec, the
370Sstevel@tonic-gate * specified command is invoked.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * This daemon manages the fork/exec's on behalf of sysevent_conf_mod.
400Sstevel@tonic-gate * The events and associated nvlist are delivered via a door upcall
410Sstevel@tonic-gate * from sysevent_conf_mod. Arriving events are queued, and the
420Sstevel@tonic-gate * main thread of this daemon dequeues events one by one, and
430Sstevel@tonic-gate * builds the necessary arguments to fork/exec the command.
440Sstevel@tonic-gate *
450Sstevel@tonic-gate * Since sysevent_conf_mod is running in the context of syseventd,
460Sstevel@tonic-gate * invoking the fork/exec from that module blocks the door upcalls
470Sstevel@tonic-gate * from the kernel delivering events to syseventd. We avoid a
480Sstevel@tonic-gate * major performance bottleneck in this fashion.
490Sstevel@tonic-gate */
500Sstevel@tonic-gate
510Sstevel@tonic-gate #include <stdio.h>
520Sstevel@tonic-gate #include <stdarg.h>
530Sstevel@tonic-gate #include <stddef.h>
540Sstevel@tonic-gate #include <stdlib.h>
550Sstevel@tonic-gate #include <errno.h>
560Sstevel@tonic-gate #include <fcntl.h>
570Sstevel@tonic-gate #include <signal.h>
580Sstevel@tonic-gate #include <strings.h>
590Sstevel@tonic-gate #include <unistd.h>
600Sstevel@tonic-gate #include <synch.h>
610Sstevel@tonic-gate #include <syslog.h>
620Sstevel@tonic-gate #include <pthread.h>
630Sstevel@tonic-gate #include <door.h>
640Sstevel@tonic-gate #include <libsysevent.h>
650Sstevel@tonic-gate #include <limits.h>
660Sstevel@tonic-gate #include <locale.h>
670Sstevel@tonic-gate #include <sys/modctl.h>
680Sstevel@tonic-gate #include <sys/stat.h>
690Sstevel@tonic-gate #include <sys/systeminfo.h>
700Sstevel@tonic-gate #include <sys/wait.h>
710Sstevel@tonic-gate
720Sstevel@tonic-gate #include "syseventconfd.h"
730Sstevel@tonic-gate #include "syseventconfd_door.h"
740Sstevel@tonic-gate #include "message_confd.h"
750Sstevel@tonic-gate
760Sstevel@tonic-gate
770Sstevel@tonic-gate
780Sstevel@tonic-gate static int debug_level = 0;
790Sstevel@tonic-gate static char *root_dir = ""; /* Relative root for lock and door */
800Sstevel@tonic-gate static char *prog;
810Sstevel@tonic-gate
820Sstevel@tonic-gate static struct cmd *cmd_list;
830Sstevel@tonic-gate static struct cmd *cmd_tail;
840Sstevel@tonic-gate
850Sstevel@tonic-gate static mutex_t cmd_list_lock;
860Sstevel@tonic-gate static cond_t cmd_list_cv;
870Sstevel@tonic-gate
880Sstevel@tonic-gate extern char *optarg;
890Sstevel@tonic-gate
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate * Support for door server thread handling
920Sstevel@tonic-gate */
930Sstevel@tonic-gate #define MAX_SERVER_THREADS 1
940Sstevel@tonic-gate
950Sstevel@tonic-gate static mutex_t create_cnt_lock;
960Sstevel@tonic-gate static int cnt_servers = 0;
970Sstevel@tonic-gate
980Sstevel@tonic-gate
990Sstevel@tonic-gate static void
usage()1000Sstevel@tonic-gate usage() {
1010Sstevel@tonic-gate (void) fprintf(stderr, "usage: syseventconfd [-d <debug_level>]\n");
1020Sstevel@tonic-gate exit(2);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate static void
set_root_dir(char * dir)1070Sstevel@tonic-gate set_root_dir(char *dir)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate root_dir = malloc(strlen(dir) + 1);
1100Sstevel@tonic-gate if (root_dir == NULL) {
1110Sstevel@tonic-gate syserrmsg(INIT_ROOT_DIR_ERR, strerror(errno));
1120Sstevel@tonic-gate exit(2);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate (void) strcpy(root_dir, dir);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate
118*1717Swesolows int
main(int argc,char ** argv)1190Sstevel@tonic-gate main(int argc, char **argv)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate int c;
1220Sstevel@tonic-gate int fd;
1230Sstevel@tonic-gate sigset_t set;
1240Sstevel@tonic-gate struct cmd *cmd;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1270Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate if (getuid() != 0) {
1300Sstevel@tonic-gate (void) fprintf(stderr, "Must be root to run syseventconfd\n");
1310Sstevel@tonic-gate exit(1);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate if ((prog = strrchr(argv[0], '/')) == NULL) {
1350Sstevel@tonic-gate prog = argv[0];
1360Sstevel@tonic-gate } else {
1370Sstevel@tonic-gate prog++;
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate if ((c = getopt(argc, argv, "d:r:")) != EOF) {
1410Sstevel@tonic-gate switch (c) {
1420Sstevel@tonic-gate case 'd':
1430Sstevel@tonic-gate debug_level = atoi(optarg);
1440Sstevel@tonic-gate break;
1450Sstevel@tonic-gate case 'r':
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate * Private flag for suninstall to run
1480Sstevel@tonic-gate * daemon during install.
1490Sstevel@tonic-gate */
1500Sstevel@tonic-gate set_root_dir(optarg);
1510Sstevel@tonic-gate break;
1520Sstevel@tonic-gate case '?':
1530Sstevel@tonic-gate default:
1540Sstevel@tonic-gate usage();
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if (fork()) {
1600Sstevel@tonic-gate exit(0);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate (void) chdir("/");
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate (void) setsid();
1660Sstevel@tonic-gate if (debug_level <= 1) {
1670Sstevel@tonic-gate closefrom(0);
1680Sstevel@tonic-gate fd = open("/dev/null", 0);
1690Sstevel@tonic-gate (void) dup2(fd, 1);
1700Sstevel@tonic-gate (void) dup2(fd, 2);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate openlog("syseventconfd", LOG_PID, LOG_DAEMON);
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate printmsg(1,
1760Sstevel@tonic-gate "syseventconfd started, debug level = %d\n", debug_level);
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate * Block all signals to all threads include the main thread.
1800Sstevel@tonic-gate * The sigwait_thr thread will catch and process all signals.
1810Sstevel@tonic-gate */
1820Sstevel@tonic-gate (void) sigfillset(&set);
1830Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &set, NULL);
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /* Create signal catching thread */
1860Sstevel@tonic-gate if (thr_create(NULL, NULL, (void *(*)(void *))sigwait_thr,
1870Sstevel@tonic-gate (void *)NULL, 0, NULL) < 0) {
1880Sstevel@tonic-gate syserrmsg(INIT_THR_CREATE_ERR, strerror(errno));
1890Sstevel@tonic-gate exit(2);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate * Init mutex and list of cmds to be fork/exec'ed
1940Sstevel@tonic-gate * This is multi-threaded so the fork/exec can be
1950Sstevel@tonic-gate * done without blocking the door upcall.
1960Sstevel@tonic-gate */
1970Sstevel@tonic-gate cmd_list = NULL;
1980Sstevel@tonic-gate cmd_tail = NULL;
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate (void) mutex_init(&create_cnt_lock, USYNC_THREAD, NULL);
2010Sstevel@tonic-gate (void) mutex_init(&cmd_list_lock, USYNC_THREAD, NULL);
2020Sstevel@tonic-gate (void) cond_init(&cmd_list_cv, USYNC_THREAD, NULL);
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate /*
2050Sstevel@tonic-gate * Open communication channel from sysevent_conf_mod
2060Sstevel@tonic-gate */
2070Sstevel@tonic-gate if (open_channel() == NULL) {
2080Sstevel@tonic-gate exit(1);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate * main thread to wait for events to arrive and be placed
2130Sstevel@tonic-gate * on the queue. As events are queued, dequeue them
2140Sstevel@tonic-gate * here and invoke the associated fork/exec.
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate (void) mutex_lock(&cmd_list_lock);
2170Sstevel@tonic-gate for (;;) {
2180Sstevel@tonic-gate while (cmd_list == NULL)
2190Sstevel@tonic-gate (void) cond_wait(&cmd_list_cv, &cmd_list_lock);
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate cmd = cmd_list;
2220Sstevel@tonic-gate cmd_list = cmd->cmd_next;
2230Sstevel@tonic-gate if (cmd_list == NULL)
2240Sstevel@tonic-gate cmd_tail = NULL;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate (void) mutex_unlock(&cmd_list_lock);
2270Sstevel@tonic-gate exec_cmd(cmd);
2280Sstevel@tonic-gate free_cmd(cmd);
2290Sstevel@tonic-gate (void) mutex_lock(&cmd_list_lock);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate /* NOTREACHED */
232*1717Swesolows return (0);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate * Events sent via the door call from sysevent_conf_mod arrive
2370Sstevel@tonic-gate * here. Queue each event for the main thread to invoke, and
2380Sstevel@tonic-gate * return. We want to avoid doing the fork/exec while in the
2390Sstevel@tonic-gate * context of the door call.
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate /*ARGSUSED*/
2420Sstevel@tonic-gate static void
event_handler(sysevent_t * event)2430Sstevel@tonic-gate event_handler(sysevent_t *event)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate nvlist_t *nvlist;
2460Sstevel@tonic-gate struct cmd *cmd;
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate nvlist = NULL;
2490Sstevel@tonic-gate if (sysevent_get_attr_list(event, &nvlist) != 0) {
2500Sstevel@tonic-gate syslog(LOG_ERR, NO_NVLIST_ERR);
2510Sstevel@tonic-gate return;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if ((cmd = alloc_cmd(nvlist)) != NULL) {
2550Sstevel@tonic-gate (void) mutex_lock(&cmd_list_lock);
2560Sstevel@tonic-gate if (cmd_list == NULL) {
2570Sstevel@tonic-gate cmd_list = cmd;
2580Sstevel@tonic-gate cmd_tail = cmd;
2590Sstevel@tonic-gate } else {
2600Sstevel@tonic-gate cmd_tail->cmd_next = cmd;
2610Sstevel@tonic-gate cmd_tail = cmd;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate cmd->cmd_next = NULL;
2640Sstevel@tonic-gate (void) cond_signal(&cmd_list_cv);
2650Sstevel@tonic-gate (void) mutex_unlock(&cmd_list_lock);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate nvlist_free(nvlist);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate * Decode the command, build the exec args and fork/exec the command
2740Sstevel@tonic-gate * All command attributes are packed into the nvlist bundled with
2750Sstevel@tonic-gate * the delivered event.
2760Sstevel@tonic-gate */
2770Sstevel@tonic-gate static void
exec_cmd(struct cmd * cmd)2780Sstevel@tonic-gate exec_cmd(struct cmd *cmd)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate char *path;
2810Sstevel@tonic-gate char *cmdline;
2820Sstevel@tonic-gate uid_t uid;
2830Sstevel@tonic-gate gid_t gid;
2840Sstevel@tonic-gate char *file;
2850Sstevel@tonic-gate int line;
2860Sstevel@tonic-gate char *user;
2870Sstevel@tonic-gate arg_t *args;
2880Sstevel@tonic-gate pid_t pid;
2890Sstevel@tonic-gate char *lp;
2900Sstevel@tonic-gate char *p;
2910Sstevel@tonic-gate int i;
2920Sstevel@tonic-gate sigset_t set, prior_set;
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate if (nvlist_lookup_string(cmd->cmd_nvlist, "user", &user) != 0) {
2950Sstevel@tonic-gate syslog(LOG_ERR, NVLIST_FORMAT_ERR, "user");
2960Sstevel@tonic-gate return;
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate if (nvlist_lookup_string(cmd->cmd_nvlist, "file", &file) != 0) {
2990Sstevel@tonic-gate syslog(LOG_ERR, NVLIST_FORMAT_ERR, "file");
3000Sstevel@tonic-gate return;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate if (nvlist_lookup_string(cmd->cmd_nvlist, "path", &path) != 0) {
3040Sstevel@tonic-gate syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "path");
3050Sstevel@tonic-gate return;
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate if (nvlist_lookup_string(cmd->cmd_nvlist, "cmd", &cmdline) != 0) {
3080Sstevel@tonic-gate syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "cmd");
3090Sstevel@tonic-gate return;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate if (nvlist_lookup_int32(cmd->cmd_nvlist, "line", &line) != 0) {
3120Sstevel@tonic-gate syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "line");
3130Sstevel@tonic-gate return;
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate if (nvlist_lookup_int32(cmd->cmd_nvlist, "uid", (int *)&uid) == 0) {
3160Sstevel@tonic-gate if (nvlist_lookup_int32(cmd->cmd_nvlist,
3170Sstevel@tonic-gate "gid", (int *)&gid) != 0) {
3180Sstevel@tonic-gate syslog(LOG_ERR, NVLIST_FILE_LINE_FORMAT_ERR, "gid");
3190Sstevel@tonic-gate return;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate } else {
3220Sstevel@tonic-gate uid = 0;
3230Sstevel@tonic-gate gid = 0;
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate args = init_arglist(32);
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate lp = cmdline;
3290Sstevel@tonic-gate while ((p = next_arg(&lp)) != NULL) {
3300Sstevel@tonic-gate if (add_arg(args, p)) {
3310Sstevel@tonic-gate free_arglist(args);
3320Sstevel@tonic-gate return;
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate if (debug_level >= DBG_EXEC) {
3370Sstevel@tonic-gate printmsg(DBG_EXEC, "path=%s\n", path);
3380Sstevel@tonic-gate printmsg(DBG_EXEC, "cmd=%s\n", cmdline);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate if (debug_level >= DBG_EXEC_ARGS) {
3420Sstevel@tonic-gate for (i = 0; i < args->arg_nargs; i++) {
3430Sstevel@tonic-gate printmsg(DBG_EXEC_ARGS,
3440Sstevel@tonic-gate "arg[%d]: '%s'\n", i, args->arg_args[i]);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, NULL, &set);
3490Sstevel@tonic-gate (void) sigaddset(&set, SIGCHLD);
3500Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &set, &prior_set);
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate again:
3530Sstevel@tonic-gate if ((pid = fork1()) == (pid_t)-1) {
3540Sstevel@tonic-gate if (errno == EINTR)
3550Sstevel@tonic-gate goto again;
3560Sstevel@tonic-gate syslog(LOG_ERR, CANNOT_FORK_ERR, strerror(errno));
3570Sstevel@tonic-gate free_arglist(args);
3580Sstevel@tonic-gate return;
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate if (pid != (pid_t)0) {
3610Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &prior_set, NULL);
3620Sstevel@tonic-gate free_arglist(args);
3630Sstevel@tonic-gate return;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate * The child
3680Sstevel@tonic-gate */
3690Sstevel@tonic-gate (void) close(0);
3700Sstevel@tonic-gate (void) close(1);
3710Sstevel@tonic-gate (void) close(2);
3720Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY);
3730Sstevel@tonic-gate (void) dup2(0, 1);
3740Sstevel@tonic-gate (void) dup2(0, 2);
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate if (uid != (uid_t)0) {
3770Sstevel@tonic-gate i = setgid(gid);
3780Sstevel@tonic-gate if (i == 0)
3790Sstevel@tonic-gate i = setuid(uid);
3800Sstevel@tonic-gate if (i != 0) {
3810Sstevel@tonic-gate syslog(LOG_ERR, SETUID_ERR,
3820Sstevel@tonic-gate file, line, user, strerror(errno));
3830Sstevel@tonic-gate _exit(0);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate * Unblock all signals in the child
3890Sstevel@tonic-gate */
3900Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &prior_set, NULL);
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate if (execv(path, args->arg_args) == -1) {
3930Sstevel@tonic-gate syslog(LOG_ERR, CANNOT_EXEC_ERR,
3940Sstevel@tonic-gate path, strerror(errno));
3950Sstevel@tonic-gate _exit(0);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate * Thread to handle in-coming signals
4020Sstevel@tonic-gate */
4030Sstevel@tonic-gate static void
sigwait_thr()4040Sstevel@tonic-gate sigwait_thr()
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate int sig;
4070Sstevel@tonic-gate sigset_t signal_set;
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate * SIGCHLD is ignored by default, and we need to handle this
4110Sstevel@tonic-gate * signal to reap the status of all children spawned by
4120Sstevel@tonic-gate * this daemon.
4130Sstevel@tonic-gate */
4140Sstevel@tonic-gate (void) sigset(SIGCHLD, reapchild);
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate for (;;) {
4170Sstevel@tonic-gate (void) sigfillset(&signal_set);
4180Sstevel@tonic-gate if (sigwait(&signal_set, &sig) == 0) {
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * Block all signals until the signal handler completes
4210Sstevel@tonic-gate */
4220Sstevel@tonic-gate (void) sigfillset(&signal_set);
4230Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &signal_set, NULL);
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate if (sig == SIGCHLD) {
4260Sstevel@tonic-gate reapchild(sig);
4270Sstevel@tonic-gate } else {
4280Sstevel@tonic-gate flt_handler(sig);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate /* NOTREACHED */
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate * reapchild - reap the status of each child as it exits
4390Sstevel@tonic-gate */
4400Sstevel@tonic-gate /*ARGSUSED*/
4410Sstevel@tonic-gate static void
reapchild(int sig)4420Sstevel@tonic-gate reapchild(int sig)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate siginfo_t info;
4450Sstevel@tonic-gate char *signam;
4460Sstevel@tonic-gate int err;
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate for (;;) {
4490Sstevel@tonic-gate (void) memset(&info, 0, sizeof (info));
4500Sstevel@tonic-gate err = waitid(P_ALL, 0, &info, WNOHANG|WEXITED);
4510Sstevel@tonic-gate if (err == -1) {
4520Sstevel@tonic-gate if (errno != EINTR && errno != EAGAIN)
4530Sstevel@tonic-gate return;
4540Sstevel@tonic-gate } else if (info.si_pid == 0) {
4550Sstevel@tonic-gate return;
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate if (debug_level >= DBG_CHILD) {
4590Sstevel@tonic-gate printmsg(DBG_CHILD, CHILD_EXIT_STATUS_ERR,
4600Sstevel@tonic-gate info.si_pid, info.si_status);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate if (info.si_status) {
4640Sstevel@tonic-gate if (info.si_code == CLD_EXITED) {
4650Sstevel@tonic-gate syserrmsg(CHILD_EXIT_STATUS_ERR,
4660Sstevel@tonic-gate info.si_pid, info.si_status);
4670Sstevel@tonic-gate } else {
4680Sstevel@tonic-gate signam = strsignal(info.si_status);
4690Sstevel@tonic-gate if (signam == NULL)
4700Sstevel@tonic-gate signam = "";
4710Sstevel@tonic-gate if (info.si_code == CLD_DUMPED) {
4720Sstevel@tonic-gate syserrmsg(
4730Sstevel@tonic-gate CHILD_EXIT_CORE_ERR,
4740Sstevel@tonic-gate info.si_pid, signam);
4750Sstevel@tonic-gate } else {
4760Sstevel@tonic-gate syserrmsg(
4770Sstevel@tonic-gate CHILD_EXIT_SIGNAL_ERR,
4780Sstevel@tonic-gate info.si_pid, signam);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate /*
4870Sstevel@tonic-gate * Fault handler for other signals caught
4880Sstevel@tonic-gate */
4890Sstevel@tonic-gate /*ARGSUSED*/
4900Sstevel@tonic-gate static void
flt_handler(int sig)4910Sstevel@tonic-gate flt_handler(int sig)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate struct sigaction act;
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate (void) memset(&act, 0, sizeof (act));
4960Sstevel@tonic-gate act.sa_handler = SIG_DFL;
4970Sstevel@tonic-gate act.sa_flags = SA_RESTART;
4980Sstevel@tonic-gate (void) sigfillset(&act.sa_mask);
4990Sstevel@tonic-gate (void) sigaction(sig, &act, NULL);
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate switch (sig) {
5020Sstevel@tonic-gate case SIGINT:
5030Sstevel@tonic-gate case SIGSTOP:
5040Sstevel@tonic-gate case SIGTERM:
5050Sstevel@tonic-gate case SIGHUP:
5060Sstevel@tonic-gate exit(1);
5070Sstevel@tonic-gate /*NOTREACHED*/
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate static arg_t *
init_arglist(int hint)5130Sstevel@tonic-gate init_arglist(int hint)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate arg_t *arglist;
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate if ((arglist = sc_malloc(sizeof (arg_t))) == NULL)
5180Sstevel@tonic-gate return (NULL);
5190Sstevel@tonic-gate arglist->arg_args = NULL;
5200Sstevel@tonic-gate arglist->arg_nargs = 0;
5210Sstevel@tonic-gate arglist->arg_alloc = 0;
5220Sstevel@tonic-gate arglist->arg_hint = hint;
5230Sstevel@tonic-gate return (arglist);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate static void
free_arglist(arg_t * arglist)5280Sstevel@tonic-gate free_arglist(arg_t *arglist)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate if (arglist->arg_args) {
5310Sstevel@tonic-gate free(arglist->arg_args);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate free(arglist);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate static int
add_arg(arg_t * arglist,char * arg)5380Sstevel@tonic-gate add_arg(arg_t *arglist, char *arg)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate char **new_args;
5410Sstevel@tonic-gate int len;
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate len = arglist->arg_nargs + 2;
5440Sstevel@tonic-gate if (arglist->arg_alloc < len) {
5450Sstevel@tonic-gate arglist->arg_alloc = len + arglist->arg_hint;
5460Sstevel@tonic-gate new_args = (arglist->arg_nargs == 0) ?
5470Sstevel@tonic-gate sc_malloc(arglist->arg_alloc * sizeof (char **)) :
5480Sstevel@tonic-gate sc_realloc(arglist->arg_args,
5490Sstevel@tonic-gate arglist->arg_alloc * sizeof (char **));
5500Sstevel@tonic-gate if (new_args == NULL)
5510Sstevel@tonic-gate return (1);
5520Sstevel@tonic-gate arglist->arg_args = new_args;
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate arglist->arg_args[arglist->arg_nargs++] = arg;
5560Sstevel@tonic-gate arglist->arg_args[arglist->arg_nargs] = NULL;
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate return (0);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate /*
5620Sstevel@tonic-gate * next_arg() is used to break up a command line
5630Sstevel@tonic-gate * into the arguments for execv(2). Break up
5640Sstevel@tonic-gate * arguments separated by spaces, but respecting
5650Sstevel@tonic-gate * single/double quotes.
5660Sstevel@tonic-gate */
5670Sstevel@tonic-gate static char *
next_arg(char ** cpp)5680Sstevel@tonic-gate next_arg(char **cpp)
5690Sstevel@tonic-gate {
5700Sstevel@tonic-gate char *cp = *cpp;
5710Sstevel@tonic-gate char *start;
5720Sstevel@tonic-gate char quote;
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t')
5750Sstevel@tonic-gate cp++;
5760Sstevel@tonic-gate if (*cp == 0) {
5770Sstevel@tonic-gate *cpp = 0;
5780Sstevel@tonic-gate return (NULL);
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate start = cp;
5810Sstevel@tonic-gate while (*cp && *cp != ' ' && *cp != '\t') {
5820Sstevel@tonic-gate if (*cp == '"' || *cp == '\'') {
5830Sstevel@tonic-gate quote = *cp++;
5840Sstevel@tonic-gate while (*cp && *cp != quote) {
5850Sstevel@tonic-gate cp++;
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate if (*cp == 0) {
5880Sstevel@tonic-gate *cpp = 0;
5890Sstevel@tonic-gate return (NULL);
5900Sstevel@tonic-gate } else {
5910Sstevel@tonic-gate cp++;
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate } else {
5940Sstevel@tonic-gate cp++;
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate if (*cp != 0)
5980Sstevel@tonic-gate *cp++ = 0;
5990Sstevel@tonic-gate *cpp = cp;
6000Sstevel@tonic-gate return (start);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate static struct cmd *
alloc_cmd(nvlist_t * nvlist)6050Sstevel@tonic-gate alloc_cmd(nvlist_t *nvlist)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate struct cmd *cmd;
6080Sstevel@tonic-gate
6090Sstevel@tonic-gate cmd = sc_malloc(sizeof (struct cmd));
6100Sstevel@tonic-gate if (cmd) {
6110Sstevel@tonic-gate if (nvlist_dup(nvlist, &cmd->cmd_nvlist, 0) != 0) {
6120Sstevel@tonic-gate syslog(LOG_ERR, OUT_OF_MEMORY_ERR);
6130Sstevel@tonic-gate free(cmd);
6140Sstevel@tonic-gate return (NULL);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate return (cmd);
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate static void
free_cmd(struct cmd * cmd)6210Sstevel@tonic-gate free_cmd(struct cmd *cmd)
6220Sstevel@tonic-gate {
6230Sstevel@tonic-gate nvlist_free(cmd->cmd_nvlist);
6240Sstevel@tonic-gate free(cmd);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate static void *
sc_malloc(size_t n)6290Sstevel@tonic-gate sc_malloc(size_t n)
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate void *p;
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate p = malloc(n);
6340Sstevel@tonic-gate if (p == NULL) {
6350Sstevel@tonic-gate syslog(LOG_ERR, OUT_OF_MEMORY_ERR);
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate return (p);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate static void *
sc_realloc(void * p,size_t n)6410Sstevel@tonic-gate sc_realloc(void *p, size_t n)
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate p = realloc(p, n);
6440Sstevel@tonic-gate if (p == NULL) {
6450Sstevel@tonic-gate syslog(LOG_ERR, OUT_OF_MEMORY_ERR);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate return (p);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate /*
6530Sstevel@tonic-gate * syserrsg - print error messages to the terminal if not
6540Sstevel@tonic-gate * yet daemonized or to syslog.
6550Sstevel@tonic-gate */
6560Sstevel@tonic-gate /*PRINTFLIKE1*/
6570Sstevel@tonic-gate static void
syserrmsg(char * message,...)6580Sstevel@tonic-gate syserrmsg(char *message, ...)
6590Sstevel@tonic-gate {
6600Sstevel@tonic-gate va_list ap;
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate va_start(ap, message);
6630Sstevel@tonic-gate (void) vsyslog(LOG_ERR, message, ap);
6640Sstevel@tonic-gate va_end(ap);
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate * printmsg - print messages to the terminal or to syslog
6690Sstevel@tonic-gate * the following levels are implemented:
6700Sstevel@tonic-gate */
6710Sstevel@tonic-gate /*PRINTFLIKE2*/
6720Sstevel@tonic-gate static void
printmsg(int level,char * message,...)6730Sstevel@tonic-gate printmsg(int level, char *message, ...)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate va_list ap;
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate if (level > debug_level) {
6780Sstevel@tonic-gate return;
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate va_start(ap, message);
6820Sstevel@tonic-gate (void) syslog(LOG_DEBUG, "%s[%ld]: ", prog, getpid());
6830Sstevel@tonic-gate (void) vsyslog(LOG_DEBUG, message, ap);
6840Sstevel@tonic-gate va_end(ap);
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate /* ARGSUSED */
6880Sstevel@tonic-gate static void *
create_door_thr(void * arg)6890Sstevel@tonic-gate create_door_thr(void *arg)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
6920Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0);
6930Sstevel@tonic-gate return (NULL);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate * Control creation of door server threads
6980Sstevel@tonic-gate *
6990Sstevel@tonic-gate * If first creation of server thread fails there is nothing
7000Sstevel@tonic-gate * we can do about. Doors would not work.
7010Sstevel@tonic-gate */
7020Sstevel@tonic-gate /* ARGSUSED */
7030Sstevel@tonic-gate static void
mk_thr_pool(door_info_t * dip)7040Sstevel@tonic-gate mk_thr_pool(door_info_t *dip)
7050Sstevel@tonic-gate {
7060Sstevel@tonic-gate (void) mutex_lock(&create_cnt_lock);
7070Sstevel@tonic-gate if (++cnt_servers > MAX_SERVER_THREADS) {
7080Sstevel@tonic-gate cnt_servers--;
7090Sstevel@tonic-gate (void) mutex_unlock(&create_cnt_lock);
7100Sstevel@tonic-gate return;
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate (void) mutex_unlock(&create_cnt_lock);
7130Sstevel@tonic-gate
7140Sstevel@tonic-gate (void) thr_create(NULL, 0, create_door_thr, NULL,
7150Sstevel@tonic-gate THR_BOUND|THR_DETACHED, NULL);
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate static sysevent_handle_t *
open_channel()7190Sstevel@tonic-gate open_channel()
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate char door_path[MAXPATHLEN];
7220Sstevel@tonic-gate const char *subclass_list;
7230Sstevel@tonic-gate sysevent_handle_t *handle;
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate if (snprintf(door_path, sizeof (door_path), "%s/%s",
7260Sstevel@tonic-gate root_dir, SYSEVENTCONFD_SERVICE_DOOR) >= sizeof (door_path)) {
7270Sstevel@tonic-gate syserrmsg(CHANNEL_OPEN_ERR);
7280Sstevel@tonic-gate return (NULL);
7290Sstevel@tonic-gate }
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate /*
7320Sstevel@tonic-gate * Setup of door server create function to limit the
7330Sstevel@tonic-gate * amount of door servers
7340Sstevel@tonic-gate */
7350Sstevel@tonic-gate (void) door_server_create(mk_thr_pool);
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate handle = sysevent_open_channel_alt(door_path);
7380Sstevel@tonic-gate if (handle == NULL) {
7390Sstevel@tonic-gate syserrmsg(CHANNEL_OPEN_ERR);
7400Sstevel@tonic-gate return (NULL);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate if (sysevent_bind_subscriber(handle, event_handler) != 0) {
7430Sstevel@tonic-gate syserrmsg(CHANNEL_BIND_ERR);
7440Sstevel@tonic-gate sysevent_close_channel(handle);
7450Sstevel@tonic-gate return (NULL);
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate subclass_list = EC_SUB_ALL;
7480Sstevel@tonic-gate if (sysevent_register_event(handle, EC_ALL, &subclass_list, 1)
7490Sstevel@tonic-gate != 0) {
7500Sstevel@tonic-gate syserrmsg(CHANNEL_BIND_ERR);
7510Sstevel@tonic-gate (void) sysevent_unbind_subscriber(handle);
7520Sstevel@tonic-gate (void) sysevent_close_channel(handle);
7530Sstevel@tonic-gate return (NULL);
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate return (handle);
7560Sstevel@tonic-gate }
757