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
52380Snakanon * Common Development and Distribution License (the "License").
62380Snakanon * 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 */
210Sstevel@tonic-gate /*
22*13063SJerry.Gilliam@Sun.COM * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <unistd.h>
260Sstevel@tonic-gate #include <stdlib.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate #include <strings.h>
290Sstevel@tonic-gate #include <limits.h>
300Sstevel@tonic-gate #include <thread.h>
310Sstevel@tonic-gate #include <wait.h>
320Sstevel@tonic-gate #include <synch.h>
330Sstevel@tonic-gate #include <syslog.h>
340Sstevel@tonic-gate #include <libintl.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <libsysevent.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate #include "sysevent_signal.h"
410Sstevel@tonic-gate #include "../devfsadm/devfsadm.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate * SLM for devfsadmd device configuration daemon
450Sstevel@tonic-gate */
460Sstevel@tonic-gate
470Sstevel@tonic-gate extern char *root_dir;
480Sstevel@tonic-gate extern void syseventd_print();
490Sstevel@tonic-gate
500Sstevel@tonic-gate sysevent_handle_t *sysevent_hp;
510Sstevel@tonic-gate
520Sstevel@tonic-gate /* Alternate root declarations during install */
530Sstevel@tonic-gate static int use_alt_root = 0;
540Sstevel@tonic-gate
550Sstevel@tonic-gate static int devfsadmdeliver_event(sysevent_t *ev, int flag);
560Sstevel@tonic-gate
570Sstevel@tonic-gate static struct slm_mod_ops devfsadm_mod_ops = {
580Sstevel@tonic-gate SE_MAJOR_VERSION, SE_MINOR_VERSION, 10, devfsadmdeliver_event};
590Sstevel@tonic-gate
600Sstevel@tonic-gate typedef struct ev_queue {
610Sstevel@tonic-gate struct ev_queue *evq_next;
620Sstevel@tonic-gate sysevent_t *evq_ev;
630Sstevel@tonic-gate } ev_queue_t;
640Sstevel@tonic-gate
650Sstevel@tonic-gate static mutex_t evq_lock;
660Sstevel@tonic-gate static cond_t evq_cv;
670Sstevel@tonic-gate static ev_queue_t *eventq_head;
680Sstevel@tonic-gate static ev_queue_t *eventq_tail;
690Sstevel@tonic-gate
70*13063SJerry.Gilliam@Sun.COM #define DELIVERY_FAILED \
71*13063SJerry.Gilliam@Sun.COM gettext("devfsadmd not responding, /dev may not be current")
72*13063SJerry.Gilliam@Sun.COM
73*13063SJerry.Gilliam@Sun.COM #define DELIVERY_RESUMED \
74*13063SJerry.Gilliam@Sun.COM gettext("devfsadmd now responding again")
75*13063SJerry.Gilliam@Sun.COM
76*13063SJerry.Gilliam@Sun.COM /*
77*13063SJerry.Gilliam@Sun.COM * Retry error recovery when attempting to send an event to devfsadmd
78*13063SJerry.Gilliam@Sun.COM */
79*13063SJerry.Gilliam@Sun.COM #define RETRY_DAEMON_RESTART 0
80*13063SJerry.Gilliam@Sun.COM #define RETRY_MSG_THRESHOLD 60
81*13063SJerry.Gilliam@Sun.COM #define RETRY_DAEMON_INTERVAL 60
820Sstevel@tonic-gate
830Sstevel@tonic-gate static int
system1(const char * s_path,const char * s)840Sstevel@tonic-gate system1(const char *s_path, const char *s)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate struct sigaction cbuf, ibuf, qbuf, ignore, dfl;
870Sstevel@tonic-gate sigset_t mask, savemask;
880Sstevel@tonic-gate struct stat st;
890Sstevel@tonic-gate pid_t pid;
900Sstevel@tonic-gate int status, w;
910Sstevel@tonic-gate
920Sstevel@tonic-gate /* Check the requested command */
930Sstevel@tonic-gate if (s == NULL) {
940Sstevel@tonic-gate errno = EINVAL;
950Sstevel@tonic-gate return (-1);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate /* Check the ability to execute devfsadmd from this process */
990Sstevel@tonic-gate if (stat(s_path, &st) < 0) {
1000Sstevel@tonic-gate return (-1);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate if (((geteuid() == st.st_uid) && ((st.st_mode & S_IXUSR) == 0)) ||
103*13063SJerry.Gilliam@Sun.COM ((getegid() == st.st_gid) && ((st.st_mode & S_IXGRP) == 0)) ||
104*13063SJerry.Gilliam@Sun.COM ((st.st_mode & S_IXOTH) == 0)) {
1050Sstevel@tonic-gate errno = EPERM;
1060Sstevel@tonic-gate return (-1);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate * Block SIGCHLD and set up a default handler for the duration of the
1110Sstevel@tonic-gate * system1 call.
1120Sstevel@tonic-gate */
1130Sstevel@tonic-gate (void) sigemptyset(&mask);
1140Sstevel@tonic-gate (void) sigaddset(&mask, SIGCHLD);
1150Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &mask, &savemask);
1160Sstevel@tonic-gate (void) memset(&dfl, 0, sizeof (dfl));
1170Sstevel@tonic-gate dfl.sa_handler = SIG_DFL;
1180Sstevel@tonic-gate (void) sigaction(SIGCHLD, &dfl, &cbuf);
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate /* Fork off the child process (using fork1(), because it's MT-safe) */
1210Sstevel@tonic-gate switch (pid = fork1()) {
1220Sstevel@tonic-gate case -1:
1230Sstevel@tonic-gate /* Error */
1240Sstevel@tonic-gate (void) sigaction(SIGCHLD, &cbuf, NULL);
1250Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &savemask, NULL);
1260Sstevel@tonic-gate return (-1);
1270Sstevel@tonic-gate case 0:
1280Sstevel@tonic-gate /* Set-up an initial signal mask for the child */
1290Sstevel@tonic-gate (void) sigemptyset(&mask);
1300Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &mask, NULL);
1312380Snakanon closefrom(3);
1320Sstevel@tonic-gate (void) execl(s_path, s, (char *)0);
1330Sstevel@tonic-gate _exit(-1);
1340Sstevel@tonic-gate break;
1350Sstevel@tonic-gate default:
1360Sstevel@tonic-gate /* Parent */
1370Sstevel@tonic-gate break;
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate (void) memset(&ignore, 0, sizeof (ignore));
1410Sstevel@tonic-gate ignore.sa_handler = SIG_IGN;
1420Sstevel@tonic-gate (void) sigaction(SIGINT, &ignore, &ibuf);
1430Sstevel@tonic-gate (void) sigaction(SIGQUIT, &ignore, &qbuf);
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate do {
1460Sstevel@tonic-gate w = waitpid(pid, &status, 0);
1470Sstevel@tonic-gate } while (w == -1 && errno == EINTR);
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate (void) sigaction(SIGINT, &ibuf, NULL);
1500Sstevel@tonic-gate (void) sigaction(SIGQUIT, &qbuf, NULL);
1510Sstevel@tonic-gate (void) sigaction(SIGCHLD, &cbuf, NULL);
1520Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &savemask, NULL);
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate return ((w == -1)? w: status);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate * devfsadmdeliver_event - called by syseventd to deliver an event buffer.
1590Sstevel@tonic-gate * The event buffer is subsequently delivered to
1600Sstevel@tonic-gate * devfsadmd. If devfsadmd, is not responding to the
1610Sstevel@tonic-gate * delivery attempt, we will try to startup the
1620Sstevel@tonic-gate * daemon. MT protection is provided by syseventd
1630Sstevel@tonic-gate * and the client lock. This insures sequential
1640Sstevel@tonic-gate * event delivery and protection from re-entrance.
1650Sstevel@tonic-gate */
1660Sstevel@tonic-gate /*ARGSUSED*/
1670Sstevel@tonic-gate static int
devfsadmdeliver_event(sysevent_t * ev,int flag)1680Sstevel@tonic-gate devfsadmdeliver_event(sysevent_t *ev, int flag)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate int ev_size;
1710Sstevel@tonic-gate ev_queue_t *new_evq;
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /* Not initialized */
1740Sstevel@tonic-gate if (sysevent_hp == NULL) {
1750Sstevel@tonic-gate return (0);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate /* Quick return for uninteresting events */
1790Sstevel@tonic-gate if (strcmp(sysevent_get_class_name(ev), EC_DEVFS) != 0) {
1800Sstevel@tonic-gate return (0);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /* Queue event for delivery to devfsadmd */
1840Sstevel@tonic-gate new_evq = (ev_queue_t *)calloc(1, sizeof (ev_queue_t));
1850Sstevel@tonic-gate if (new_evq == NULL) {
1860Sstevel@tonic-gate return (EAGAIN);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate ev_size = sysevent_get_size(ev);
1900Sstevel@tonic-gate new_evq->evq_ev = (sysevent_t *)malloc(ev_size);
1910Sstevel@tonic-gate if (new_evq->evq_ev == NULL) {
1920Sstevel@tonic-gate free(new_evq);
1930Sstevel@tonic-gate return (EAGAIN);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate bcopy(ev, new_evq->evq_ev, ev_size);
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate (void) mutex_lock(&evq_lock);
1980Sstevel@tonic-gate if (eventq_head == NULL) {
1990Sstevel@tonic-gate eventq_head = new_evq;
2000Sstevel@tonic-gate } else {
2010Sstevel@tonic-gate eventq_tail->evq_next = new_evq;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate eventq_tail = new_evq;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate (void) cond_signal(&evq_cv);
2060Sstevel@tonic-gate (void) mutex_unlock(&evq_lock);
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate return (0);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate static int cleanup;
2120Sstevel@tonic-gate thread_t deliver_thr_id;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate void
devfsadmd_deliver_thr()2150Sstevel@tonic-gate devfsadmd_deliver_thr()
2160Sstevel@tonic-gate {
217*13063SJerry.Gilliam@Sun.COM int retry = 0;
218*13063SJerry.Gilliam@Sun.COM int msg_emitted = 0;
2190Sstevel@tonic-gate ev_queue_t *evqp;
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate (void) mutex_lock(&evq_lock);
2220Sstevel@tonic-gate for (;;) {
2230Sstevel@tonic-gate while (eventq_head == NULL) {
2240Sstevel@tonic-gate (void) cond_wait(&evq_cv, &evq_lock);
2250Sstevel@tonic-gate if (cleanup && eventq_head == NULL) {
2260Sstevel@tonic-gate (void) cond_signal(&evq_cv);
2270Sstevel@tonic-gate (void) mutex_unlock(&evq_lock);
2280Sstevel@tonic-gate return;
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /* Send events on to devfsadmd */
2330Sstevel@tonic-gate evqp = eventq_head;
2340Sstevel@tonic-gate while (evqp) {
2350Sstevel@tonic-gate (void) mutex_unlock(&evq_lock);
2360Sstevel@tonic-gate retry = 0;
2370Sstevel@tonic-gate while (sysevent_send_event(sysevent_hp,
2380Sstevel@tonic-gate evqp->evq_ev) != 0) {
2390Sstevel@tonic-gate /*
240*13063SJerry.Gilliam@Sun.COM * Invoke devfsadm to handle node creation
241*13063SJerry.Gilliam@Sun.COM * but not for an alternate root.
242*13063SJerry.Gilliam@Sun.COM */
243*13063SJerry.Gilliam@Sun.COM if (use_alt_root != 0)
244*13063SJerry.Gilliam@Sun.COM break;
245*13063SJerry.Gilliam@Sun.COM /*
246*13063SJerry.Gilliam@Sun.COM * daemon unresponsive -
247*13063SJerry.Gilliam@Sun.COM * restart daemon and retry once more
2480Sstevel@tonic-gate */
249*13063SJerry.Gilliam@Sun.COM if ((errno == EBADF || errno == ENOENT) &&
250*13063SJerry.Gilliam@Sun.COM (retry == RETRY_DAEMON_RESTART) ||
251*13063SJerry.Gilliam@Sun.COM ((retry % RETRY_DAEMON_INTERVAL) == 0)) {
252*13063SJerry.Gilliam@Sun.COM (void) system1(
253*13063SJerry.Gilliam@Sun.COM DEVFSADMD_START_PATH,
254*13063SJerry.Gilliam@Sun.COM DEVFSADMD_START);
255*13063SJerry.Gilliam@Sun.COM }
256*13063SJerry.Gilliam@Sun.COM if (retry == RETRY_MSG_THRESHOLD) {
257*13063SJerry.Gilliam@Sun.COM syslog(LOG_ERR, DELIVERY_FAILED);
258*13063SJerry.Gilliam@Sun.COM msg_emitted = 1;
2590Sstevel@tonic-gate }
260*13063SJerry.Gilliam@Sun.COM (void) sleep(1);
261*13063SJerry.Gilliam@Sun.COM ++retry;
262*13063SJerry.Gilliam@Sun.COM continue;
2630Sstevel@tonic-gate }
264*13063SJerry.Gilliam@Sun.COM
265*13063SJerry.Gilliam@Sun.COM /*
266*13063SJerry.Gilliam@Sun.COM * Event delivered: remove from queue
267*13063SJerry.Gilliam@Sun.COM * and reset delivery retry state.
268*13063SJerry.Gilliam@Sun.COM */
269*13063SJerry.Gilliam@Sun.COM if (msg_emitted) {
270*13063SJerry.Gilliam@Sun.COM syslog(LOG_ERR, DELIVERY_RESUMED);
271*13063SJerry.Gilliam@Sun.COM msg_emitted = 0;
272*13063SJerry.Gilliam@Sun.COM }
273*13063SJerry.Gilliam@Sun.COM retry = 0;
2740Sstevel@tonic-gate (void) mutex_lock(&evq_lock);
2750Sstevel@tonic-gate if (eventq_head != NULL) {
2760Sstevel@tonic-gate eventq_head = eventq_head->evq_next;
2770Sstevel@tonic-gate if (eventq_head == NULL)
2780Sstevel@tonic-gate eventq_tail = NULL;
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate free(evqp->evq_ev);
2810Sstevel@tonic-gate free(evqp);
2820Sstevel@tonic-gate evqp = eventq_head;
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate if (cleanup) {
2850Sstevel@tonic-gate (void) cond_signal(&evq_cv);
2860Sstevel@tonic-gate (void) mutex_unlock(&evq_lock);
2870Sstevel@tonic-gate return;
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate /* NOTREACHED */
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate struct slm_mod_ops *
slm_init()2950Sstevel@tonic-gate slm_init()
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate char alt_door[MAXPATHLEN];
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate if (strcmp(root_dir, "") == 0) {
3000Sstevel@tonic-gate /* Initialize the private sysevent handle */
3010Sstevel@tonic-gate sysevent_hp = sysevent_open_channel_alt(DEVFSADM_SERVICE_DOOR);
3020Sstevel@tonic-gate } else {
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate /* Try alternate door during install time */
3050Sstevel@tonic-gate if (snprintf(alt_door, MAXPATHLEN, "%s%s", "/tmp",
3060Sstevel@tonic-gate DEVFSADM_SERVICE_DOOR) >= MAXPATHLEN)
3070Sstevel@tonic-gate return (NULL);
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate sysevent_hp = sysevent_open_channel_alt(alt_door);
3100Sstevel@tonic-gate use_alt_root = 1;
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate if (sysevent_hp == NULL) {
3130Sstevel@tonic-gate syseventd_print(0, "Unable to allocate sysevent handle"
3140Sstevel@tonic-gate " for devfsadm module\n");
3150Sstevel@tonic-gate return (NULL);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate if (sysevent_bind_publisher(sysevent_hp) != 0) {
3190Sstevel@tonic-gate if (errno == EBUSY) {
3200Sstevel@tonic-gate sysevent_cleanup_publishers(sysevent_hp);
3210Sstevel@tonic-gate if (sysevent_bind_publisher(sysevent_hp) != 0) {
3220Sstevel@tonic-gate (void) sysevent_close_channel(sysevent_hp);
3230Sstevel@tonic-gate return (NULL);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate sysevent_cleanup_subscribers(sysevent_hp);
3290Sstevel@tonic-gate cleanup = 0;
3300Sstevel@tonic-gate eventq_head = NULL;
3310Sstevel@tonic-gate eventq_tail = NULL;
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate (void) mutex_init(&evq_lock, USYNC_THREAD, NULL);
3340Sstevel@tonic-gate (void) cond_init(&evq_cv, USYNC_THREAD, NULL);
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate if (thr_create(NULL, NULL, (void *(*)(void *))devfsadmd_deliver_thr,
3370Sstevel@tonic-gate NULL, THR_BOUND, &deliver_thr_id) != 0) {
3380Sstevel@tonic-gate (void) mutex_destroy(&evq_lock);
3390Sstevel@tonic-gate (void) cond_destroy(&evq_cv);
3400Sstevel@tonic-gate sysevent_close_channel(sysevent_hp);
3410Sstevel@tonic-gate return (NULL);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate return (&devfsadm_mod_ops);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate void
slm_fini()3480Sstevel@tonic-gate slm_fini()
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate /* Wait for all events to be flushed out to devfsadmd */
3510Sstevel@tonic-gate (void) mutex_lock(&evq_lock);
3520Sstevel@tonic-gate cleanup = 1;
3530Sstevel@tonic-gate (void) cond_signal(&evq_cv);
3540Sstevel@tonic-gate (void) cond_wait(&evq_cv, &evq_lock);
3550Sstevel@tonic-gate (void) mutex_unlock(&evq_lock);
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate /* Wait for delivery thread to exit */
3580Sstevel@tonic-gate (void) thr_join(deliver_thr_id, NULL, NULL);
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate (void) mutex_destroy(&evq_lock);
3610Sstevel@tonic-gate (void) cond_destroy(&evq_cv);
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate sysevent_close_channel(sysevent_hp);
3640Sstevel@tonic-gate sysevent_hp = NULL;
3650Sstevel@tonic-gate }
366