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