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
5004388ebScasper * Common Development and Distribution License (the "License").
6004388ebScasper * 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 */
217c478bd9Sstevel@tonic-gate /*
22*47856d53SCheng Sean Ye * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Reconfiguration Coordination Daemon
287c478bd9Sstevel@tonic-gate *
297c478bd9Sstevel@tonic-gate * Accept RCM messages in the form of RCM events and process them
307c478bd9Sstevel@tonic-gate * - to build and update the system resource map
317c478bd9Sstevel@tonic-gate * - to allow clients to register/unregister for resource
327c478bd9Sstevel@tonic-gate * - to allow dr initiators to offline a resource before removal
337c478bd9Sstevel@tonic-gate * - to call into clients to perform suspend/offline actions
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * The goal is to enable fully automated Dynamic Reconfiguration and better
367c478bd9Sstevel@tonic-gate * DR information tracking.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include <librcm_event.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include "rcm_impl.h"
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate /* will run in daemon mode if debug level < DEBUG_LEVEL_FORK */
447c478bd9Sstevel@tonic-gate #define DEBUG_LEVEL_FORK RCM_DEBUG
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #define DAEMON_LOCK_FILE "/var/run/rcm_daemon_lock"
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate static int hold_daemon_lock;
497c478bd9Sstevel@tonic-gate static int daemon_lock_fd;
507c478bd9Sstevel@tonic-gate static const char *daemon_lock_file = DAEMON_LOCK_FILE;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate int debug_level = 0;
537c478bd9Sstevel@tonic-gate static int idle_timeout;
547c478bd9Sstevel@tonic-gate static int logflag = 0;
557c478bd9Sstevel@tonic-gate static char *prog;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate static void usage(void);
587c478bd9Sstevel@tonic-gate static void catch_sighup(void);
597c478bd9Sstevel@tonic-gate static void catch_sigusr1(void);
607c478bd9Sstevel@tonic-gate static pid_t enter_daemon_lock(void);
617c478bd9Sstevel@tonic-gate static void exit_daemon_lock(void);
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate extern void init_poll_thread();
647c478bd9Sstevel@tonic-gate extern void cleanup_poll_thread();
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * Print command line syntax for starting rcm_daemon
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate static void
usage()707c478bd9Sstevel@tonic-gate usage() {
717c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
727c478bd9Sstevel@tonic-gate gettext("usage: %s [-d debug_level] [-t idle_timeout]\n"), prog);
737c478bd9Sstevel@tonic-gate rcmd_exit(EINVAL);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate /*
7703859504Sjg * common cleanup/exit functions to ensure releasing locks
787c478bd9Sstevel@tonic-gate */
7903859504Sjg static void
rcmd_cleanup(int status)8003859504Sjg rcmd_cleanup(int status)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate if (status == 0) {
837c478bd9Sstevel@tonic-gate rcm_log_message(RCM_INFO,
847c478bd9Sstevel@tonic-gate gettext("rcm_daemon normal exit\n"));
857c478bd9Sstevel@tonic-gate } else {
867c478bd9Sstevel@tonic-gate rcm_log_message(RCM_ERROR,
877c478bd9Sstevel@tonic-gate gettext("rcm_daemon exit: errno = %d\n"), status);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if (hold_daemon_lock) {
917c478bd9Sstevel@tonic-gate exit_daemon_lock();
927c478bd9Sstevel@tonic-gate }
9303859504Sjg }
947c478bd9Sstevel@tonic-gate
9503859504Sjg void
rcmd_exit(int status)9603859504Sjg rcmd_exit(int status)
9703859504Sjg {
9803859504Sjg rcmd_cleanup(status);
997c478bd9Sstevel@tonic-gate exit(status);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate * When SIGHUP is received, reload modules at the next safe moment (when
1047c478bd9Sstevel@tonic-gate * there is no DR activity.
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate void
catch_sighup(void)1077c478bd9Sstevel@tonic-gate catch_sighup(void)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate rcm_log_message(RCM_INFO,
1107c478bd9Sstevel@tonic-gate gettext("SIGHUP received, will exit when daemon is idle\n"));
1117c478bd9Sstevel@tonic-gate rcmd_thr_signal();
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate * When SIGUSR1 is received, exit the thread
1167c478bd9Sstevel@tonic-gate */
1177c478bd9Sstevel@tonic-gate void
catch_sigusr1(void)1187c478bd9Sstevel@tonic-gate catch_sigusr1(void)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate rcm_log_message(RCM_DEBUG, "SIGUSR1 received in thread %d\n",
1217c478bd9Sstevel@tonic-gate thr_self());
1227c478bd9Sstevel@tonic-gate cleanup_poll_thread();
1237c478bd9Sstevel@tonic-gate thr_exit(NULL);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate * Use an advisory lock to ensure that only one daemon process is
1287c478bd9Sstevel@tonic-gate * active at any point in time.
1297c478bd9Sstevel@tonic-gate */
1307c478bd9Sstevel@tonic-gate static pid_t
enter_daemon_lock(void)1317c478bd9Sstevel@tonic-gate enter_daemon_lock(void)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate struct flock lock;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate rcm_log_message(RCM_TRACE1,
1367c478bd9Sstevel@tonic-gate "enter_daemon_lock: lock file = %s\n", daemon_lock_file);
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate daemon_lock_fd = open(daemon_lock_file, O_CREAT|O_RDWR, 0644);
1397c478bd9Sstevel@tonic-gate if (daemon_lock_fd < 0) {
1407c478bd9Sstevel@tonic-gate rcm_log_message(RCM_ERROR, gettext("open(%s) - %s\n"),
1417c478bd9Sstevel@tonic-gate daemon_lock_file, strerror(errno));
1427c478bd9Sstevel@tonic-gate rcmd_exit(errno);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK;
1467c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET;
1477c478bd9Sstevel@tonic-gate lock.l_start = 0;
1487c478bd9Sstevel@tonic-gate lock.l_len = 0;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate if (fcntl(daemon_lock_fd, F_SETLK, &lock) == 0) {
1517c478bd9Sstevel@tonic-gate hold_daemon_lock = 1;
1527c478bd9Sstevel@tonic-gate return (getpid());
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate /* failed to get lock, attempt to find lock owner */
1567c478bd9Sstevel@tonic-gate if ((errno == EAGAIN || errno == EDEADLK) &&
1577c478bd9Sstevel@tonic-gate (fcntl(daemon_lock_fd, F_GETLK, &lock) == 0)) {
1587c478bd9Sstevel@tonic-gate return (lock.l_pid);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate /* die a horrible death */
1627c478bd9Sstevel@tonic-gate rcm_log_message(RCM_ERROR, gettext("lock(%s) - %s"), daemon_lock_file,
1637c478bd9Sstevel@tonic-gate strerror(errno));
1647c478bd9Sstevel@tonic-gate exit(errno);
1657c478bd9Sstevel@tonic-gate /*NOTREACHED*/
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate * Drop the advisory daemon lock, close lock file
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate static void
exit_daemon_lock(void)1727c478bd9Sstevel@tonic-gate exit_daemon_lock(void)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate struct flock lock;
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate lock.l_type = F_UNLCK;
1777c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET;
1787c478bd9Sstevel@tonic-gate lock.l_start = 0;
1797c478bd9Sstevel@tonic-gate lock.l_len = 0;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) {
1827c478bd9Sstevel@tonic-gate rcm_log_message(RCM_ERROR, gettext("unlock(%s) - %s"),
1837c478bd9Sstevel@tonic-gate daemon_lock_file, strerror(errno));
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate (void) close(daemon_lock_fd);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
1907c478bd9Sstevel@tonic-gate static void
rcm_log_msg_impl(int level,char * message,va_list ap)1917c478bd9Sstevel@tonic-gate rcm_log_msg_impl(int level, char *message, va_list ap)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate int log_level;
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate if (!logflag) {
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate * RCM_ERROR goes to stderr, others go to stdout
1987c478bd9Sstevel@tonic-gate */
1997c478bd9Sstevel@tonic-gate FILE *out = (level <= RCM_ERROR) ? stderr : stdout;
2007c478bd9Sstevel@tonic-gate (void) vfprintf(out, message, ap);
2017c478bd9Sstevel@tonic-gate return;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate * translate RCM_* to LOG_*
2067c478bd9Sstevel@tonic-gate */
2077c478bd9Sstevel@tonic-gate switch (level) {
2087c478bd9Sstevel@tonic-gate case RCM_ERROR:
2097c478bd9Sstevel@tonic-gate log_level = LOG_ERR;
2107c478bd9Sstevel@tonic-gate break;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate case RCM_WARNING:
2137c478bd9Sstevel@tonic-gate log_level = LOG_WARNING;
2147c478bd9Sstevel@tonic-gate break;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate case RCM_NOTICE:
2177c478bd9Sstevel@tonic-gate log_level = LOG_NOTICE;
2187c478bd9Sstevel@tonic-gate break;
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate case RCM_INFO:
2217c478bd9Sstevel@tonic-gate log_level = LOG_INFO;
2227c478bd9Sstevel@tonic-gate break;
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate case RCM_DEBUG:
2257c478bd9Sstevel@tonic-gate log_level = LOG_DEBUG;
2267c478bd9Sstevel@tonic-gate break;
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate default:
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate * Don't log RCM_TRACEn messages
2317c478bd9Sstevel@tonic-gate */
2327c478bd9Sstevel@tonic-gate return;
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate (void) vsyslog(log_level, message, ap);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate * print error messages to the terminal or to syslog
2407c478bd9Sstevel@tonic-gate */
2417c478bd9Sstevel@tonic-gate void
rcm_log_message(int level,char * message,...)2427c478bd9Sstevel@tonic-gate rcm_log_message(int level, char *message, ...)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate va_list ap;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate if (level > debug_level) {
2477c478bd9Sstevel@tonic-gate return;
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate va_start(ap, message);
2517c478bd9Sstevel@tonic-gate rcm_log_msg_impl(level, message, ap);
2527c478bd9Sstevel@tonic-gate va_end(ap);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate * Print error messages to the terminal or to syslog.
2577c478bd9Sstevel@tonic-gate * Same as rcm_log_message except that it does not check for
2587c478bd9Sstevel@tonic-gate * level > debug_level
2597c478bd9Sstevel@tonic-gate * allowing callers to override the global debug_level.
2607c478bd9Sstevel@tonic-gate */
2617c478bd9Sstevel@tonic-gate void
rcm_log_msg(int level,char * message,...)2627c478bd9Sstevel@tonic-gate rcm_log_msg(int level, char *message, ...)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate va_list ap;
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate va_start(ap, message);
2677c478bd9Sstevel@tonic-gate rcm_log_msg_impl(level, message, ap);
2687c478bd9Sstevel@tonic-gate va_end(ap);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate * grab daemon_lock and direct messages to syslog
2737c478bd9Sstevel@tonic-gate */
2747c478bd9Sstevel@tonic-gate static void
detachfromtty()2757c478bd9Sstevel@tonic-gate detachfromtty()
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate (void) chdir("/");
2787c478bd9Sstevel@tonic-gate (void) setsid();
2797c478bd9Sstevel@tonic-gate (void) close(0);
2807c478bd9Sstevel@tonic-gate (void) close(1);
2817c478bd9Sstevel@tonic-gate (void) close(2);
2827c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_RDWR, 0);
2837c478bd9Sstevel@tonic-gate (void) dup2(0, 1);
2847c478bd9Sstevel@tonic-gate (void) dup2(0, 2);
2857c478bd9Sstevel@tonic-gate openlog(prog, LOG_PID, LOG_DAEMON);
2867c478bd9Sstevel@tonic-gate logflag = 1;
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate
28903859504Sjg int
main(int argc,char ** argv)2907c478bd9Sstevel@tonic-gate main(int argc, char **argv)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate int c;
2937c478bd9Sstevel@tonic-gate pid_t pid;
2947c478bd9Sstevel@tonic-gate extern char *optarg;
2957c478bd9Sstevel@tonic-gate sigset_t mask;
2967c478bd9Sstevel@tonic-gate struct sigaction act;
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
2997c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
3007c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
3017c478bd9Sstevel@tonic-gate #endif
3027c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate if ((prog = strrchr(argv[0], '/')) == NULL) {
3057c478bd9Sstevel@tonic-gate prog = argv[0];
3067c478bd9Sstevel@tonic-gate } else {
3077c478bd9Sstevel@tonic-gate prog++;
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate
310004388ebScasper (void) enable_extended_FILE_stdio(-1, -1);
311004388ebScasper
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate * process arguments
3147c478bd9Sstevel@tonic-gate */
3157c478bd9Sstevel@tonic-gate if (argc > 3) {
3167c478bd9Sstevel@tonic-gate usage();
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "d:t:")) != EOF) {
3197c478bd9Sstevel@tonic-gate switch (c) {
3207c478bd9Sstevel@tonic-gate case 'd':
3217c478bd9Sstevel@tonic-gate debug_level = atoi(optarg);
3227c478bd9Sstevel@tonic-gate break;
3237c478bd9Sstevel@tonic-gate case 't':
3247c478bd9Sstevel@tonic-gate idle_timeout = atoi(optarg);
3257c478bd9Sstevel@tonic-gate break;
3267c478bd9Sstevel@tonic-gate case '?':
3277c478bd9Sstevel@tonic-gate default:
3287c478bd9Sstevel@tonic-gate usage();
3297c478bd9Sstevel@tonic-gate /*NOTREACHED*/
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate * Check permission
3357c478bd9Sstevel@tonic-gate */
3367c478bd9Sstevel@tonic-gate if (getuid() != 0) {
3377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Must be root to run %s\n"),
3387c478bd9Sstevel@tonic-gate prog);
3397c478bd9Sstevel@tonic-gate exit(EPERM);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate * When rcm_daemon is started by a call to librcm, it inherits file
3447c478bd9Sstevel@tonic-gate * descriptors from the DR initiator making a call. The file
3457c478bd9Sstevel@tonic-gate * descriptors may correspond to devices that can be removed by DR.
3467c478bd9Sstevel@tonic-gate * Since keeping them remain opened is problematic, close everything
3477c478bd9Sstevel@tonic-gate * but stdin/stdout/stderr.
3487c478bd9Sstevel@tonic-gate */
3497c478bd9Sstevel@tonic-gate closefrom(3);
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate /*
352*47856d53SCheng Sean Ye * When rcm_daemon is started by the caller, it will inherit the
353*47856d53SCheng Sean Ye * signal block mask. We unblock all signals to make sure the
354*47856d53SCheng Sean Ye * signal handling will work normally.
355*47856d53SCheng Sean Ye */
356*47856d53SCheng Sean Ye (void) sigfillset(&mask);
357*47856d53SCheng Sean Ye (void) thr_sigsetmask(SIG_UNBLOCK, &mask, NULL);
358*47856d53SCheng Sean Ye
359*47856d53SCheng Sean Ye /*
3607c478bd9Sstevel@tonic-gate * block SIGUSR1, use it for killing specific threads
3617c478bd9Sstevel@tonic-gate */
3627c478bd9Sstevel@tonic-gate (void) sigemptyset(&mask);
3637c478bd9Sstevel@tonic-gate (void) sigaddset(&mask, SIGUSR1);
3647c478bd9Sstevel@tonic-gate (void) thr_sigsetmask(SIG_BLOCK, &mask, NULL);
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate * Setup signal handlers for SIGHUP and SIGUSR1
3687c478bd9Sstevel@tonic-gate * SIGHUP - causes a "delayed" daemon exit, effectively the same
3697c478bd9Sstevel@tonic-gate * as a daemon restart.
3707c478bd9Sstevel@tonic-gate * SIGUSR1 - causes a thr_exit(). Unblocked in selected threads.
3717c478bd9Sstevel@tonic-gate */
3727c478bd9Sstevel@tonic-gate act.sa_flags = 0;
3737c478bd9Sstevel@tonic-gate act.sa_handler = catch_sighup;
3747c478bd9Sstevel@tonic-gate (void) sigaction(SIGHUP, &act, NULL);
3757c478bd9Sstevel@tonic-gate act.sa_handler = catch_sigusr1;
3767c478bd9Sstevel@tonic-gate (void) sigaction(SIGUSR1, &act, NULL);
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate * ignore SIGPIPE so that the rcm daemon does not exit when it
3807c478bd9Sstevel@tonic-gate * attempts to read or write from a pipe whose corresponding
3817c478bd9Sstevel@tonic-gate * rcm script process exited.
3827c478bd9Sstevel@tonic-gate */
3837c478bd9Sstevel@tonic-gate act.sa_handler = SIG_IGN;
3847c478bd9Sstevel@tonic-gate (void) sigaction(SIGPIPE, &act, NULL);
3857c478bd9Sstevel@tonic-gate
3867c478bd9Sstevel@tonic-gate /*
3877c478bd9Sstevel@tonic-gate * run in daemon mode
3887c478bd9Sstevel@tonic-gate */
3897c478bd9Sstevel@tonic-gate if (debug_level < DEBUG_LEVEL_FORK) {
3907c478bd9Sstevel@tonic-gate if (fork()) {
3917c478bd9Sstevel@tonic-gate exit(0);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate detachfromtty();
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate /* only one daemon can run at a time */
3977c478bd9Sstevel@tonic-gate if ((pid = enter_daemon_lock()) != getpid()) {
3987c478bd9Sstevel@tonic-gate rcm_log_message(RCM_DEBUG, "%s pid %d already running\n",
3997c478bd9Sstevel@tonic-gate prog, pid);
4007c478bd9Sstevel@tonic-gate exit(EDEADLK);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate
4037c478bd9Sstevel@tonic-gate rcm_log_message(RCM_TRACE1, "%s started, debug level = %d\n",
4047c478bd9Sstevel@tonic-gate prog, debug_level);
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate * Set daemon state to block RCM requests before rcm_daemon is
4087c478bd9Sstevel@tonic-gate * fully initialized. See rcmd_thr_incr().
4097c478bd9Sstevel@tonic-gate */
4107c478bd9Sstevel@tonic-gate rcmd_set_state(RCMD_INIT);
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate * create rcm_daemon door and set permission to 0400
4147c478bd9Sstevel@tonic-gate */
4157c478bd9Sstevel@tonic-gate if (create_event_service(RCM_SERVICE_DOOR, event_service) == -1) {
4167c478bd9Sstevel@tonic-gate rcm_log_message(RCM_ERROR,
4177c478bd9Sstevel@tonic-gate gettext("cannot create door service: %s\n"),
4187c478bd9Sstevel@tonic-gate strerror(errno));
4197c478bd9Sstevel@tonic-gate rcmd_exit(errno);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate (void) chmod(RCM_SERVICE_DOOR, S_IRUSR);
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate init_poll_thread(); /* initialize poll thread related data */
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate * Initialize database by asking modules to register.
4277c478bd9Sstevel@tonic-gate */
4287c478bd9Sstevel@tonic-gate rcmd_db_init();
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate * Initialize locking, including lock recovery in the event of
4327c478bd9Sstevel@tonic-gate * unexpected daemon failure.
4337c478bd9Sstevel@tonic-gate */
4347c478bd9Sstevel@tonic-gate rcmd_lock_init();
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate * Start accepting normal requests
4387c478bd9Sstevel@tonic-gate */
4397c478bd9Sstevel@tonic-gate rcmd_set_state(RCMD_NORMAL);
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate * Start cleanup thread
4437c478bd9Sstevel@tonic-gate */
4447c478bd9Sstevel@tonic-gate rcmd_db_clean();
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate /*
44703859504Sjg * Loop within daemon and return after a period of inactivity.
4487c478bd9Sstevel@tonic-gate */
4497c478bd9Sstevel@tonic-gate rcmd_start_timer(idle_timeout);
45003859504Sjg
45103859504Sjg rcmd_cleanup(0);
45203859504Sjg return (0);
4537c478bd9Sstevel@tonic-gate }
454