xref: /dragonfly/usr.sbin/hotplugd/hotplugd.c (revision 768af85b)
1 /*	$OpenBSD: hotplugd.c,v 1.11 2009/06/26 01:06:04 kurt Exp $	*/
2 /*
3  * Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /*
19  * Devices hot plugging daemon.
20  */
21 
22 #include <sys/types.h>
23 #include <sys/device.h>
24 #include <sys/hotplug.h>
25 #include <sys/wait.h>
26 
27 #include <err.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <libgen.h>
31 #include <signal.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <syslog.h>
37 #include <unistd.h>
38 #include "compat.h"
39 
40 #define _PATH_DEV_HOTPLUG		"/dev/hotplug"
41 #define _PATH_ETC_HOTPLUG		"/etc/hotplug"
42 #define _PATH_ETC_HOTPLUG_ATTACH	_PATH_ETC_HOTPLUG "/attach"
43 #define _PATH_ETC_HOTPLUG_DETACH	_PATH_ETC_HOTPLUG "/detach"
44 #define _LOG_TAG			"hotplugd"
45 #define _LOG_FACILITY			LOG_DAEMON
46 #define _LOG_OPT			(LOG_NDELAY | LOG_PID)
47 
48 volatile sig_atomic_t quit = 0;
49 char *device = _PATH_DEV_HOTPLUG;
50 int devfd = -1;
51 
52 void exec_script(const char *, int, char *);
53 void sigchild(int);
54 void sigquit(int);
55 __dead void usage(void);
56 
57 int
58 main(int argc, char *argv[])
59 {
60 	int ch;
61 	struct sigaction sact;
62 	struct hotplug_event he;
63 
64 	while ((ch = getopt(argc, argv, "d:")) != -1)
65 		switch (ch) {
66 		case 'd':
67 			device = optarg;
68 			break;
69 		case '?':
70 		default:
71 			usage();
72 			/* NOTREACHED */
73 		}
74 
75 	argc -= optind;
76 	argv += optind;
77 	if (argc > 0)
78 		usage();
79 
80 	if ((devfd = open(device, O_RDONLY)) == -1)
81 		err(1, "%s", device);
82 
83 	bzero(&sact, sizeof(sact));
84 	sigemptyset(&sact.sa_mask);
85 	sact.sa_flags = 0;
86 	sact.sa_handler = sigquit;
87 	sigaction(SIGINT, &sact, NULL);
88 	sigaction(SIGQUIT, &sact, NULL);
89 	sigaction(SIGTERM, &sact, NULL);
90 	sact.sa_handler = SIG_IGN;
91 	sigaction(SIGHUP, &sact, NULL);
92 	sact.sa_handler = sigchild;
93 	sact.sa_flags = SA_NOCLDSTOP;
94 	sigaction(SIGCHLD, &sact, NULL);
95 
96 	openlog(_LOG_TAG, _LOG_OPT, _LOG_FACILITY);
97 	if (daemon(0, 0) == -1)
98 		err(1, "daemon");
99 
100 	syslog(LOG_INFO, "started");
101 
102 	while (!quit) {
103 		if (read(devfd, &he, sizeof(he)) == -1) {
104 			if (errno == EINTR)
105 				/* ignore */
106 				continue;
107 			syslog(LOG_ERR, "read: %m");
108 			exit(1);
109 		}
110 
111 		switch (he.he_type) {
112 		case HOTPLUG_DEVAT:
113 			syslog(LOG_INFO, "%s attached, class %d",
114 			    he.he_devname, he.he_devclass);
115 			exec_script(_PATH_ETC_HOTPLUG_ATTACH, he.he_devclass,
116 			    he.he_devname);
117 			break;
118 		case HOTPLUG_DEVDT:
119 			syslog(LOG_INFO, "%s detached, class %d",
120 			    he.he_devname, he.he_devclass);
121 			exec_script(_PATH_ETC_HOTPLUG_DETACH, he.he_devclass,
122 			    he.he_devname);
123 			break;
124 		default:
125 			syslog(LOG_NOTICE, "unknown event (0x%x)", he.he_type);
126 		}
127 	}
128 
129 	syslog(LOG_INFO, "terminated");
130 
131 	closelog();
132 	close(devfd);
133 
134 	return (0);
135 }
136 
137 void
138 exec_script(const char *file, int class, char *name)
139 {
140 	char strclass[8];
141 	pid_t pid;
142 
143 	snprintf(strclass, sizeof(strclass), "%d", class);
144 
145 	if (access(file, X_OK | R_OK)) {
146 		syslog(LOG_ERR, "could not access %s", file);
147 		return;
148 	}
149 
150 	if ((pid = fork()) == -1) {
151 		syslog(LOG_ERR, "fork: %m");
152 		return;
153 	}
154 	if (pid == 0) {
155 		/* child process */
156 		execl(file, basename(file), strclass, name, (char *)NULL);
157 		syslog(LOG_ERR, "execl %s: %m", file);
158 		_exit(1);
159 		/* NOTREACHED */
160 	}
161 }
162 
163 /* ARGSUSED */
164 void
165 sigchild(int signum)
166 {
167 	struct syslog_data sdata = SYSLOG_DATA_INIT;
168 	int saved_errno, status;
169 	pid_t pid;
170 
171 	saved_errno = errno;
172 
173 	sdata.log_tag = _LOG_TAG;
174 	sdata.log_fac = _LOG_FACILITY;
175 	sdata.log_stat = _LOG_OPT;
176 
177 	while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
178 		if (pid == -1) {
179 			if (errno == EINTR)
180 				continue;
181 			if (errno != ECHILD)
182 				syslog_r(LOG_ERR, &sdata, "waitpid: %m");
183 			break;
184 		}
185 
186 		if (WIFEXITED(status)) {
187 			if (WEXITSTATUS(status) != 0) {
188 				syslog_r(LOG_NOTICE, &sdata,
189 				    "child exit status: %d",
190 				    WEXITSTATUS(status));
191 			}
192 		} else {
193 			syslog_r(LOG_NOTICE, &sdata,
194 			    "child is terminated abnormally");
195 		}
196 	}
197 
198 	errno = saved_errno;
199 }
200 
201 /* ARGSUSED */
202 void
203 sigquit(int signum)
204 {
205 	quit = 1;
206 }
207 
208 __dead void
209 usage(void)
210 {
211 	extern char *__progname;
212 
213 	fprintf(stderr, "usage: %s [-d device]\n", __progname);
214 	exit(1);
215 }
216