2 * uevent.c - trigger upon netlink uevents from the kernel
4 * Only kernels from version 2.6.10* on provide the uevent netlink socket.
5 * Until the libc-kernel-headers are updated, you need to compile with:
7 * gcc -I /lib/modules/`uname -r`/build/include -o uevent_listen uevent_listen.c
9 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation version 2 of the License.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include <sys/socket.h>
37 #include <linux/types.h>
38 #include <linux/netlink.h>
50 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
53 pthread_mutex_t uevq_lock = PTHREAD_MUTEX_INITIALIZER;
54 pthread_mutex_t *uevq_lockp = &uevq_lock;
55 pthread_cond_t uev_cond = PTHREAD_COND_INITIALIZER;
56 pthread_cond_t *uev_condp = &uev_cond;
57 uev_trigger *my_uev_trigger;
58 void * my_trigger_data;
61 int is_uevent_busy(void)
65 pthread_mutex_lock(uevq_lockp);
66 empty = list_empty(&uevq);
67 pthread_mutex_unlock(uevq_lockp);
68 return (!empty || servicing_uev);
71 struct uevent * alloc_uevent (void)
73 struct uevent *uev = MALLOC(sizeof(struct uevent));
76 INIT_LIST_HEAD(&uev->node);
82 service_uevq(struct list_head *tmpq)
84 struct uevent *uev, *tmp;
86 list_for_each_entry_safe(uev, tmp, tmpq, node) {
87 list_del_init(&uev->node);
89 if (my_uev_trigger && my_uev_trigger(uev, my_trigger_data))
90 condlog(0, "uevent trigger error");
93 udev_device_unref(uev->udev);
98 static void uevent_cleanup(void *arg)
100 struct udev *udev = arg;
102 condlog(3, "Releasing uevent_listen() resources");
107 uevq_cleanup(struct list_head *tmpq)
109 struct uevent *uev, *tmp;
111 list_for_each_entry_safe(uev, tmp, tmpq, node) {
112 list_del_init(&uev->node);
118 * Service the uevent queue.
120 int uevent_dispatch(int (*uev_trigger)(struct uevent *, void * trigger_data),
123 my_uev_trigger = uev_trigger;
124 my_trigger_data = trigger_data;
126 mlockall(MCL_CURRENT | MCL_FUTURE);
131 pthread_mutex_lock(uevq_lockp);
134 * Condition signals are unreliable,
135 * so make sure we only wait if we have to.
137 if (list_empty(&uevq)) {
138 pthread_cond_wait(uev_condp, uevq_lockp);
141 list_splice_init(&uevq, &uevq_tmp);
142 pthread_mutex_unlock(uevq_lockp);
145 service_uevq(&uevq_tmp);
147 condlog(3, "Terminating uev service queue");
152 struct uevent *uevent_from_buffer(char *buf, ssize_t buflen)
160 uev = alloc_uevent();
162 condlog(1, "lost uevent, oom");
166 if ((size_t)buflen > sizeof(buf)-1)
167 buflen = sizeof(buf)-1;
170 * Copy the shared receive buffer contents to buffer private
171 * to this uevent so we can immediately reuse the shared buffer.
173 memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
174 buffer = uev->buffer;
175 buffer[buflen] = '\0';
177 /* save start of payload */
178 bufpos = strlen(buffer) + 1;
181 uev->action = buffer;
182 pos = strchr(buffer, '@');
184 condlog(3, "bad action string '%s'", buffer);
191 uev->devpath = &pos[1];
193 /* hotplug events have the environment attached - reconstruct envp[] */
194 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
198 key = &buffer[bufpos];
199 keylen = strlen(key);
201 /* Filter out sequence number */
202 if (strncmp(key, "SEQNUM=", 7) == 0) {
205 uev->seqnum = strtoul(key + 7, &eptr, 10);
209 bufpos += keylen + 1;
213 condlog(3, "uevent %ld '%s' from '%s'", uev->seqnum,
214 uev->action, uev->devpath);
215 uev->kernel = strrchr(uev->devpath, '/');
219 /* print payload environment */
220 for (i = 0; uev->envp[i] != NULL; i++)
221 condlog(5, "%s", uev->envp[i]);
226 int failback_listen(void)
229 struct sockaddr_nl snl;
230 struct sockaddr_un sun;
233 int rcvbufsz = 128*1024;
235 int rcvszsz = sizeof(rcvsz);
236 unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
237 const int feature_on = 1;
239 * First check whether we have a udev socket
241 memset(&sun, 0x00, sizeof(struct sockaddr_un));
242 sun.sun_family = AF_LOCAL;
243 strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
244 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
246 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
249 condlog(3, "reading events from udev socket.");
251 /* the bind takes care of ensuring only one copy running */
252 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
254 condlog(0, "bind failed, exit");
258 /* enable receiving of the sender credentials */
259 retval = setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
260 &feature_on, sizeof(feature_on));
262 condlog(0, "failed to enable credential passing, exit");
267 /* Fallback to read kernel netlink events */
268 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
269 snl.nl_family = AF_NETLINK;
270 snl.nl_pid = getpid();
271 snl.nl_groups = 0x01;
273 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
275 condlog(0, "error getting socket, exit");
279 condlog(3, "reading events from kernel.");
282 * try to avoid dropping uevents, even so, this is not a guarantee,
283 * but it does help to change the netlink uevent socket's
284 * receive buffer threshold from the default value of 106,496 to
285 * the maximum value of 262,142.
287 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
291 condlog(0, "error setting receive buffer size for socket, exit");
294 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
296 condlog(0, "error setting receive buffer size for socket, exit");
299 condlog(3, "receive buffer size for socket is %u.", rcvsz);
301 /* enable receiving of the sender credentials */
302 if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
303 &feature_on, sizeof(feature_on)) < 0) {
304 condlog(0, "error on enabling credential passing for socket");
308 retval = bind(sock, (struct sockaddr *) &snl,
309 sizeof(struct sockaddr_nl));
311 condlog(0, "bind failed, exit");
322 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
323 struct cmsghdr *cmsg;
325 static char buf[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
327 memset(buf, 0x00, sizeof(buf));
329 iov.iov_len = sizeof(buf);
330 memset (&smsg, 0x00, sizeof(struct msghdr));
333 smsg.msg_control = cred_msg;
334 smsg.msg_controllen = sizeof(cred_msg);
336 buflen = recvmsg(sock, &smsg, 0);
339 condlog(0, "error receiving message, errno %d", errno);
343 cmsg = CMSG_FIRSTHDR(&smsg);
344 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
345 condlog(3, "no sender credentials received, message ignored");
349 cred = (struct ucred *)CMSG_DATA(cmsg);
350 if (cred->uid != 0) {
351 condlog(3, "sender uid=%d, message ignored", cred->uid);
356 bufpos = strlen(buf) + 1;
357 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
358 condlog(3, "invalid message length");
362 /* check message header */
363 if (strstr(buf, "@/") == NULL) {
364 condlog(3, "unrecognized message header");
367 if ((size_t)buflen > sizeof(buf)-1) {
368 condlog(2, "buffer overflow for received uevent");
369 buflen = sizeof(buf)-1;
372 uev = uevent_from_buffer(buf, buflen);
376 * Queue uevent and poke service pthread.
378 pthread_mutex_lock(uevq_lockp);
379 list_add_tail(&uev->node, &uevq);
380 pthread_cond_signal(uev_condp);
381 pthread_mutex_unlock(uevq_lockp);
389 struct uevent *uevent_from_udev_device(struct udev_device *dev)
394 struct udev_list_entry *list_entry;
396 uev = alloc_uevent();
398 udev_device_unref(dev);
399 condlog(1, "lost uevent, oom");
403 end = pos + HOTPLUG_BUFFER_SIZE + OBJECT_SIZE - 1;
404 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev)) {
405 const char *name, *value;
408 name = udev_list_entry_get_name(list_entry);
411 value = udev_list_entry_get_value(list_entry);
414 bytes = snprintf(pos, end - pos, "%s=%s", name, value);
415 if (pos + bytes >= end) {
416 condlog(2, "buffer overflow for uevent");
423 if (strcmp(name, "DEVPATH") == 0)
424 uev->devpath = uev->envp[i] + 8;
425 if (strcmp(name, "ACTION") == 0)
426 uev->action = uev->envp[i] + 7;
428 if (i == HOTPLUG_NUM_ENVP - 1)
434 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
435 uev->kernel = strrchr(uev->devpath, '/');
439 /* print payload environment */
440 for (i = 0; uev->envp[i] != NULL; i++)
441 condlog(5, "%s", uev->envp[i]);
445 int uevent_listen(struct udev *udev)
448 struct udev_monitor *monitor = NULL;
449 int fd, socket_flags, events;
450 int need_failback = 1;
452 LIST_HEAD(uevlisten_tmp);
455 * Queue uevents for service by dedicated thread so that the uevent
456 * listening thread does not block on multipathd locks (vecs->lock)
457 * thereby not getting to empty the socket's receive buffer queue
461 condlog(1, "no udev context");
465 pthread_cleanup_push(uevent_cleanup, udev);
467 monitor = udev_monitor_new_from_netlink(udev, "udev");
469 condlog(2, "failed to create udev monitor");
472 #ifdef LIBUDEV_API_RECVBUF
473 if (udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024))
474 condlog(2, "failed to increase buffer size");
476 fd = udev_monitor_get_fd(monitor);
478 condlog(2, "failed to get monitor fd");
481 socket_flags = fcntl(fd, F_GETFL);
482 if (socket_flags < 0) {
483 condlog(2, "failed to get monitor socket flags : %s",
487 if (fcntl(fd, F_SETFL, socket_flags & ~O_NONBLOCK) < 0) {
488 condlog(2, "failed to set monitor socket flags : %s",
492 err = udev_monitor_filter_add_match_subsystem_devtype(monitor, "block",
495 condlog(2, "failed to create filter : %s", strerror(-err));
496 err = udev_monitor_enable_receiving(monitor);
498 condlog(2, "failed to enable receiving : %s", strerror(-err));
505 struct udev_device *dev;
506 struct pollfd ev_poll;
510 memset(&ev_poll, 0, sizeof(struct pollfd));
512 ev_poll.events = POLLIN;
513 poll_timeout = timeout * 1000;
515 fdcount = poll(&ev_poll, 1, poll_timeout);
516 if (fdcount && ev_poll.revents & POLLIN) {
518 dev = udev_monitor_receive_device(monitor);
520 condlog(0, "failed getting udev device");
523 uev = uevent_from_udev_device(dev);
526 list_add_tail(&uev->node, &uevlisten_tmp);
534 condlog(0, "error receiving "
535 "uevent message: %m");
539 if (!list_empty(&uevlisten_tmp)) {
541 * Queue uevents and poke service pthread.
543 condlog(3, "Forwarding %d uevents", events);
544 pthread_mutex_lock(uevq_lockp);
545 list_splice_tail_init(&uevlisten_tmp, &uevq);
546 pthread_cond_signal(uev_condp);
547 pthread_mutex_unlock(uevq_lockp);
555 udev_monitor_unref(monitor);
557 err = failback_listen();
558 pthread_cleanup_pop(1);
563 uevent_get_major(struct uevent *uev)
568 for (i = 0; uev->envp[i] != NULL; i++) {
569 if (!strncmp(uev->envp[i], "MAJOR", 5) && strlen(uev->envp[i]) > 6) {
570 p = uev->envp[i] + 6;
571 major = strtoul(p, &q, 10);
573 condlog(2, "invalid major '%s'", p);
583 uevent_get_minor(struct uevent *uev)
588 for (i = 0; uev->envp[i] != NULL; i++) {
589 if (!strncmp(uev->envp[i], "MINOR", 5) && strlen(uev->envp[i]) > 6) {
590 p = uev->envp[i] + 6;
591 minor = strtoul(p, &q, 10);
593 condlog(2, "invalid minor '%s'", p);
603 uevent_get_disk_ro(struct uevent *uev)
608 for (i = 0; uev->envp[i] != NULL; i++) {
609 if (!strncmp(uev->envp[i], "DISK_RO", 6) && strlen(uev->envp[i]) > 7) {
610 p = uev->envp[i] + 8;
611 ro = strtoul(p, &q, 10);
613 condlog(2, "invalid read_only setting '%s'", p);
623 uevent_get_dm_name(struct uevent *uev)
628 for (i = 0; uev->envp[i] != NULL; i++) {
629 if (!strncmp(uev->envp[i], "DM_NAME", 6) &&
630 strlen(uev->envp[i]) > 7) {
631 p = MALLOC(strlen(uev->envp[i] + 8) + 1);
632 strcpy(p, uev->envp[i] + 8);