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, write to the Free Software Foundation, Inc.,
22 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <sys/socket.h>
38 #include <linux/types.h>
39 #include <linux/netlink.h>
53 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
57 pthread_mutex_t uevq_lock = PTHREAD_MUTEX_INITIALIZER;
58 pthread_mutex_t *uevq_lockp = &uevq_lock;
59 pthread_cond_t uev_cond = PTHREAD_COND_INITIALIZER;
60 pthread_cond_t *uev_condp = &uev_cond;
61 uev_trigger *my_uev_trigger;
62 void * my_trigger_data;
65 int is_uevent_busy(void)
69 pthread_mutex_lock(uevq_lockp);
70 empty = list_empty(&uevq);
71 pthread_mutex_unlock(uevq_lockp);
72 return (!empty || servicing_uev);
75 struct uevent * alloc_uevent (void)
77 struct uevent *uev = MALLOC(sizeof(struct uevent));
80 INIT_LIST_HEAD(&uev->node);
86 setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
88 if (pthread_attr_init(attr)) {
89 fprintf(stderr, "can't initialize thread attr: %s\n",
93 if (stacksize < PTHREAD_STACK_MIN)
94 stacksize = PTHREAD_STACK_MIN;
96 if (pthread_attr_setstacksize(attr, stacksize)) {
97 fprintf(stderr, "can't set thread stack size to %lu: %s\n",
98 (unsigned long)stacksize, strerror(errno));
101 if (detached && pthread_attr_setdetachstate(attr,
102 PTHREAD_CREATE_DETACHED)) {
103 fprintf(stderr, "can't set thread to detached: %s\n",
110 * Called with uevq_lockp held
113 service_uevq(struct list_head *tmpq)
115 struct uevent *uev, *tmp;
117 list_for_each_entry_safe(uev, tmp, tmpq, node) {
118 list_del_init(&uev->node);
120 if (my_uev_trigger && my_uev_trigger(uev, my_trigger_data))
121 condlog(0, "uevent trigger error");
124 udev_device_unref(uev->udev);
129 static void uevq_stop(void *arg)
131 struct udev *udev = arg;
133 condlog(3, "Stopping uev queue");
134 pthread_mutex_lock(uevq_lockp);
135 my_uev_trigger = NULL;
136 pthread_cond_signal(uev_condp);
137 pthread_mutex_unlock(uevq_lockp);
142 uevq_cleanup(struct list_head *tmpq)
144 struct uevent *uev, *tmp;
146 list_for_each_entry_safe(uev, tmp, tmpq, node) {
147 list_del_init(&uev->node);
153 * Service the uevent queue.
155 int uevent_dispatch(int (*uev_trigger)(struct uevent *, void * trigger_data),
158 my_uev_trigger = uev_trigger;
159 my_trigger_data = trigger_data;
161 mlockall(MCL_CURRENT | MCL_FUTURE);
166 pthread_mutex_lock(uevq_lockp);
169 * Condition signals are unreliable,
170 * so make sure we only wait if we have to.
172 if (list_empty(&uevq)) {
173 pthread_cond_wait(uev_condp, uevq_lockp);
176 list_splice_init(&uevq, &uevq_tmp);
177 pthread_mutex_unlock(uevq_lockp);
180 service_uevq(&uevq_tmp);
182 condlog(3, "Terminating uev service queue");
187 struct uevent *uevent_from_buffer(char *buf, ssize_t buflen)
195 uev = alloc_uevent();
197 condlog(1, "lost uevent, oom");
201 if ((size_t)buflen > sizeof(buf)-1)
202 buflen = sizeof(buf)-1;
205 * Copy the shared receive buffer contents to buffer private
206 * to this uevent so we can immediately reuse the shared buffer.
208 memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
209 buffer = uev->buffer;
210 buffer[buflen] = '\0';
212 /* save start of payload */
213 bufpos = strlen(buffer) + 1;
216 uev->action = buffer;
217 pos = strchr(buffer, '@');
219 condlog(3, "bad action string '%s'", buffer);
226 uev->devpath = &pos[1];
228 /* hotplug events have the environment attached - reconstruct envp[] */
229 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
233 key = &buffer[bufpos];
234 keylen = strlen(key);
236 /* Filter out sequence number */
237 if (strncmp(key, "SEQNUM=", 7) == 0) {
240 uev->seqnum = strtoul(key + 7, &eptr, 10);
244 bufpos += keylen + 1;
248 condlog(3, "uevent %ld '%s' from '%s'", uev->seqnum,
249 uev->action, uev->devpath);
250 uev->kernel = strrchr(uev->devpath, '/');
254 /* print payload environment */
255 for (i = 0; uev->envp[i] != NULL; i++)
256 condlog(5, "%s", uev->envp[i]);
261 int failback_listen(void)
264 struct sockaddr_nl snl;
265 struct sockaddr_un sun;
268 int rcvbufsz = 128*1024;
270 int rcvszsz = sizeof(rcvsz);
271 unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
272 const int feature_on = 1;
274 * First check whether we have a udev socket
276 memset(&sun, 0x00, sizeof(struct sockaddr_un));
277 sun.sun_family = AF_LOCAL;
278 strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
279 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
281 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
284 condlog(3, "reading events from udev socket.");
286 /* the bind takes care of ensuring only one copy running */
287 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
289 condlog(0, "bind failed, exit");
293 /* enable receiving of the sender credentials */
294 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
295 &feature_on, sizeof(feature_on));
298 /* Fallback to read kernel netlink events */
299 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
300 snl.nl_family = AF_NETLINK;
301 snl.nl_pid = getpid();
302 snl.nl_groups = 0x01;
304 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
306 condlog(0, "error getting socket, exit");
310 condlog(3, "reading events from kernel.");
313 * try to avoid dropping uevents, even so, this is not a guarantee,
314 * but it does help to change the netlink uevent socket's
315 * receive buffer threshold from the default value of 106,496 to
316 * the maximum value of 262,142.
318 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
322 condlog(0, "error setting receive buffer size for socket, exit");
325 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
327 condlog(0, "error setting receive buffer size for socket, exit");
330 condlog(3, "receive buffer size for socket is %u.", rcvsz);
332 /* enable receiving of the sender credentials */
333 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
334 &feature_on, sizeof(feature_on));
336 retval = bind(sock, (struct sockaddr *) &snl,
337 sizeof(struct sockaddr_nl));
339 condlog(0, "bind failed, exit");
350 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
351 struct cmsghdr *cmsg;
353 static char buf[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
355 memset(buf, 0x00, sizeof(buf));
357 iov.iov_len = sizeof(buf);
358 memset (&smsg, 0x00, sizeof(struct msghdr));
361 smsg.msg_control = cred_msg;
362 smsg.msg_controllen = sizeof(cred_msg);
364 buflen = recvmsg(sock, &smsg, 0);
367 condlog(0, "error receiving message, errno %d", errno);
371 cmsg = CMSG_FIRSTHDR(&smsg);
372 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
373 condlog(3, "no sender credentials received, message ignored");
377 cred = (struct ucred *)CMSG_DATA(cmsg);
378 if (cred->uid != 0) {
379 condlog(3, "sender uid=%d, message ignored", cred->uid);
384 bufpos = strlen(buf) + 1;
385 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
386 condlog(3, "invalid message length");
390 /* check message header */
391 if (strstr(buf, "@/") == NULL) {
392 condlog(3, "unrecognized message header");
395 if ((size_t)buflen > sizeof(buf)-1) {
396 condlog(2, "buffer overflow for received uevent");
397 buflen = sizeof(buf)-1;
400 uev = uevent_from_buffer(buf, buflen);
404 * Queue uevent and poke service pthread.
406 pthread_mutex_lock(uevq_lockp);
407 list_add_tail(&uev->node, &uevq);
408 pthread_cond_signal(uev_condp);
409 pthread_mutex_unlock(uevq_lockp);
417 struct uevent *uevent_from_udev_device(struct udev_device *dev)
422 struct udev_list_entry *list_entry;
424 uev = alloc_uevent();
426 udev_device_unref(dev);
427 condlog(1, "lost uevent, oom");
431 end = pos + HOTPLUG_BUFFER_SIZE + OBJECT_SIZE - 1;
432 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev)) {
433 const char *name, *value;
436 name = udev_list_entry_get_name(list_entry);
439 value = udev_list_entry_get_value(list_entry);
442 bytes = snprintf(pos, end - pos, "%s=%s", name, value);
443 if (pos + bytes >= end) {
444 condlog(2, "buffer overflow for uevent");
451 if (strcmp(name, "DEVPATH") == 0)
452 uev->devpath = uev->envp[i] + 8;
453 if (strcmp(name, "ACTION") == 0)
454 uev->action = uev->envp[i] + 7;
456 if (i == HOTPLUG_NUM_ENVP - 1)
462 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
463 uev->kernel = strrchr(uev->devpath, '/');
467 /* print payload environment */
468 for (i = 0; uev->envp[i] != NULL; i++)
469 condlog(5, "%s", uev->envp[i]);
473 int uevent_listen(struct udev *udev)
476 struct udev_monitor *monitor = NULL;
477 int fd, fd_ep = -1, socket_flags, events;
478 int need_failback = 1;
481 LIST_HEAD(uevlisten_tmp);
484 * Queue uevents for service by dedicated thread so that the uevent
485 * listening thread does not block on multipathd locks (vecs->lock)
486 * thereby not getting to empty the socket's receive buffer queue
490 condlog(1, "no udev context");
494 pthread_cleanup_push(uevq_stop, udev);
496 monitor = udev_monitor_new_from_netlink(udev, "udev");
498 condlog(2, "failed to create udev monitor");
501 #ifdef LIBUDEV_API_RECVBUF
502 if (udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024))
503 condlog(2, "failed to increase buffer size");
505 fd = udev_monitor_get_fd(monitor);
507 condlog(2, "failed to get monitor fd");
510 socket_flags = fcntl(fd, F_GETFL);
511 if (socket_flags < 0) {
512 condlog(2, "failed to get monitor socket flags : %s",
516 if (fcntl(fd, F_SETFL, socket_flags & ~O_NONBLOCK) < 0) {
517 condlog(2, "failed to set monitor socket flags : %s",
521 err = udev_monitor_filter_add_match_subsystem_devtype(monitor, "block",
524 condlog(2, "failed to create filter : %s", strerror(-err));
525 err = udev_monitor_enable_receiving(monitor);
527 condlog(2, "failed to enable receiving : %s", strerror(-err));
531 pthread_sigmask(SIG_SETMASK, NULL, &mask);
535 struct udev_device *dev;
536 struct pollfd ev_poll;
537 struct timespec poll_timeout;
540 memset(&ev_poll, 0, sizeof(struct pollfd));
542 ev_poll.events = POLLIN;
543 memset(&poll_timeout, 0, sizeof(struct timespec));
544 poll_timeout.tv_sec = timeout;
546 fdcount = ppoll(&ev_poll, 1, &poll_timeout, &mask);
547 if (fdcount && ev_poll.revents & POLLIN) {
549 dev = udev_monitor_receive_device(monitor);
551 condlog(0, "failed getting udev device");
554 uev = uevent_from_udev_device(dev);
557 list_add_tail(&uev->node, &uevlisten_tmp);
565 condlog(0, "error receiving "
566 "uevent message: %m");
570 if (!list_empty(&uevlisten_tmp)) {
572 * Queue uevents and poke service pthread.
574 condlog(3, "Forwarding %d uevents", events);
575 pthread_mutex_lock(uevq_lockp);
576 list_splice_tail_init(&uevlisten_tmp, &uevq);
577 pthread_cond_signal(uev_condp);
578 pthread_mutex_unlock(uevq_lockp);
588 udev_monitor_unref(monitor);
590 err = failback_listen();
591 pthread_cleanup_pop(1);
596 uevent_get_major(struct uevent *uev)
601 for (i = 0; uev->envp[i] != NULL; i++) {
602 if (!strncmp(uev->envp[i], "MAJOR", 5) && strlen(uev->envp[i]) > 6) {
603 p = uev->envp[i] + 6;
604 major = strtoul(p, &q, 10);
606 condlog(2, "invalid major '%s'", p);
616 uevent_get_minor(struct uevent *uev)
621 for (i = 0; uev->envp[i] != NULL; i++) {
622 if (!strncmp(uev->envp[i], "MINOR", 5) && strlen(uev->envp[i]) > 6) {
623 p = uev->envp[i] + 6;
624 minor = strtoul(p, &q, 10);
626 condlog(2, "invalid minor '%s'", p);
636 uevent_get_disk_ro(struct uevent *uev)
641 for (i = 0; uev->envp[i] != NULL; i++) {
642 if (!strncmp(uev->envp[i], "DISK_RO", 6) && strlen(uev->envp[i]) > 7) {
643 p = uev->envp[i] + 8;
644 ro = strtoul(p, &q, 10);
646 condlog(2, "invalid read_only setting '%s'", p);
656 uevent_get_dm_name(struct uevent *uev)
661 for (i = 0; uev->envp[i] != NULL; i++) {
662 if (!strncmp(uev->envp[i], "DM_NAME", 6) &&
663 strlen(uev->envp[i]) > 7) {
664 p = MALLOC(strlen(uev->envp[i] + 8) + 1);
665 strcpy(p, uev->envp[i] + 8);