xref: /openbsd-src/usr.sbin/hotplugd/hotplugd.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: hotplugd.c,v 1.15 2019/04/30 17:05:15 mestre 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 
39 #define _PATH_DEV_HOTPLUG		"/dev/hotplug"
40 #define _PATH_ETC_HOTPLUG		"/etc/hotplug"
41 #define _PATH_ETC_HOTPLUG_ATTACH	_PATH_ETC_HOTPLUG "/attach"
42 #define _PATH_ETC_HOTPLUG_DETACH	_PATH_ETC_HOTPLUG "/detach"
43 #define _LOG_TAG			"hotplugd"
44 #define _LOG_FACILITY			LOG_DAEMON
45 #define _LOG_OPT			(LOG_NDELAY | LOG_PID)
46 
47 volatile sig_atomic_t quit = 0;
48 char *device = _PATH_DEV_HOTPLUG;
49 int devfd = -1;
50 
51 void exec_script(const char *, int, char *);
52 
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 (unveil(device, "r") == -1)
81 		err(1, "unveil");
82 	if (unveil(_PATH_ETC_HOTPLUG_ATTACH, "rx") == -1)
83 		err(1, "unveil");
84 	if (unveil(_PATH_ETC_HOTPLUG_DETACH, "rx") == -1)
85 		err(1, "unveil");
86 	if (pledge("stdio rpath proc exec", NULL) == -1)
87 		err(1, "pledge");
88 
89 	if ((devfd = open(device, O_RDONLY | O_CLOEXEC)) == -1)
90 		err(1, "%s", device);
91 
92 	bzero(&sact, sizeof(sact));
93 	sigemptyset(&sact.sa_mask);
94 	sact.sa_flags = 0;
95 	sact.sa_handler = sigquit;
96 	sigaction(SIGINT, &sact, NULL);
97 	sigaction(SIGQUIT, &sact, NULL);
98 	sigaction(SIGTERM, &sact, NULL);
99 	sact.sa_handler = SIG_IGN;
100 	sigaction(SIGHUP, &sact, NULL);
101 	sact.sa_handler = sigchild;
102 	sact.sa_flags = SA_NOCLDSTOP;
103 	sigaction(SIGCHLD, &sact, NULL);
104 
105 	openlog(_LOG_TAG, _LOG_OPT, _LOG_FACILITY);
106 	if (daemon(0, 0) == -1)
107 		err(1, "daemon");
108 
109 	syslog(LOG_INFO, "started");
110 
111 	while (!quit) {
112 		if (read(devfd, &he, sizeof(he)) == -1) {
113 			if (errno == EINTR)
114 				/* ignore */
115 				continue;
116 			syslog(LOG_ERR, "read: %m");
117 			exit(1);
118 		}
119 
120 		switch (he.he_type) {
121 		case HOTPLUG_DEVAT:
122 			syslog(LOG_INFO, "%s attached, class %d",
123 			    he.he_devname, he.he_devclass);
124 			exec_script(_PATH_ETC_HOTPLUG_ATTACH, he.he_devclass,
125 			    he.he_devname);
126 			break;
127 		case HOTPLUG_DEVDT:
128 			syslog(LOG_INFO, "%s detached, class %d",
129 			    he.he_devname, he.he_devclass);
130 			exec_script(_PATH_ETC_HOTPLUG_DETACH, he.he_devclass,
131 			    he.he_devname);
132 			break;
133 		default:
134 			syslog(LOG_NOTICE, "unknown event (0x%x)", he.he_type);
135 		}
136 	}
137 
138 	syslog(LOG_INFO, "terminated");
139 
140 	closelog();
141 	close(devfd);
142 
143 	return (0);
144 }
145 
146 void
147 exec_script(const char *file, int class, char *name)
148 {
149 	char strclass[8];
150 	pid_t pid;
151 
152 	snprintf(strclass, sizeof(strclass), "%d", class);
153 
154 	if (access(file, X_OK | R_OK) == -1) {
155 		if (errno != ENOENT)
156 			syslog(LOG_ERR, "%s: %m", file);
157 		return;
158 	}
159 
160 	if ((pid = fork()) == -1) {
161 		syslog(LOG_ERR, "fork: %m");
162 		return;
163 	}
164 	if (pid == 0) {
165 		/* child process */
166 		execl(file, basename(file), strclass, name, (char *)NULL);
167 		syslog(LOG_ERR, "execl %s: %m", file);
168 		_exit(1);
169 		/* NOTREACHED */
170 	}
171 }
172 
173 /* ARGSUSED */
174 void
175 sigchild(int signum)
176 {
177 	struct syslog_data sdata = SYSLOG_DATA_INIT;
178 	int saved_errno, status;
179 	pid_t pid;
180 
181 	saved_errno = errno;
182 
183 	sdata.log_tag = _LOG_TAG;
184 	sdata.log_fac = _LOG_FACILITY;
185 	sdata.log_stat = _LOG_OPT;
186 
187 	while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
188 		if (pid == -1) {
189 			if (errno == EINTR)
190 				continue;
191 			if (errno != ECHILD)
192 				syslog_r(LOG_ERR, &sdata, "waitpid: %m");
193 			break;
194 		}
195 
196 		if (WIFEXITED(status)) {
197 			if (WEXITSTATUS(status) != 0) {
198 				syslog_r(LOG_NOTICE, &sdata,
199 				    "child exit status: %d",
200 				    WEXITSTATUS(status));
201 			}
202 		} else {
203 			syslog_r(LOG_NOTICE, &sdata,
204 			    "child is terminated abnormally");
205 		}
206 	}
207 
208 	errno = saved_errno;
209 }
210 
211 /* ARGSUSED */
212 void
213 sigquit(int signum)
214 {
215 	quit = 1;
216 }
217 
218 __dead void
219 usage(void)
220 {
221 	extern char *__progname;
222 
223 	fprintf(stderr, "usage: %s [-d device]\n", __progname);
224 	exit(1);
225 }
226