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/>.
34 #include <sys/socket.h>
38 #include <linux/types.h>
39 #include <linux/netlink.h>
54 #include "blacklist.h"
56 #define MAX_ACCUMULATION_COUNT 2048
57 #define MAX_ACCUMULATION_TIME 30*1000
58 #define MIN_BURST_SPEED 10
60 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
63 pthread_mutex_t uevq_lock = PTHREAD_MUTEX_INITIALIZER;
64 pthread_mutex_t *uevq_lockp = &uevq_lock;
65 pthread_cond_t uev_cond = PTHREAD_COND_INITIALIZER;
66 pthread_cond_t *uev_condp = &uev_cond;
67 uev_trigger *my_uev_trigger;
68 void * my_trigger_data;
71 int is_uevent_busy(void)
75 pthread_mutex_lock(uevq_lockp);
76 empty = list_empty(&uevq);
77 pthread_mutex_unlock(uevq_lockp);
78 return (!empty || servicing_uev);
81 struct uevent * alloc_uevent (void)
83 struct uevent *uev = MALLOC(sizeof(struct uevent));
86 INIT_LIST_HEAD(&uev->node);
87 INIT_LIST_HEAD(&uev->merge_node);
94 uevq_cleanup(struct list_head *tmpq)
96 struct uevent *uev, *tmp;
98 list_for_each_entry_safe(uev, tmp, tmpq, node) {
99 list_del_init(&uev->node);
102 udev_device_unref(uev->udev);
108 uevent_get_wwid(struct uevent *uev)
112 struct config * conf;
114 conf = get_multipath_config();
115 uid_attribute = parse_uid_attribute_by_attrs(conf->uid_attrs, uev->kernel);
116 put_multipath_config(conf);
121 for (i = 0; uev->envp[i] != NULL; i++) {
122 if (!strncmp(uev->envp[i], uid_attribute, strlen(uid_attribute)) &&
123 strlen(uev->envp[i]) > strlen(uid_attribute) &&
124 uev->envp[i][strlen(uid_attribute)] == '=') {
125 uev->wwid = uev->envp[i] + strlen(uid_attribute) + 1;
133 uevent_need_merge(void)
135 struct config * conf;
136 bool need_merge = false;
138 conf = get_multipath_config();
141 put_multipath_config(conf);
147 uevent_can_discard_by_devpath(const char *devpath)
149 static const char BLOCK[] = "/block/";
150 const char *tmp = strstr(devpath, BLOCK);
153 condlog(4, "no /block/ in '%s'", devpath);
156 tmp += sizeof(BLOCK) - 1;
158 /* just ".../block/" - discard */
161 * If there are more path elements after ".../block/xyz",
162 * it's a partition - discard it; but don't discard ".../block/sda/".
164 tmp = strchr(tmp, '/');
165 return tmp != NULL && *(tmp + 1) != '\0';
169 uevent_can_discard(struct uevent *uev)
171 struct config * conf;
173 if (uevent_can_discard_by_devpath(uev->devpath))
177 * do not filter dm devices by devnode
179 if (!strncmp(uev->kernel, "dm-", 3))
182 * filter paths devices by devnode
184 conf = get_multipath_config();
185 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
187 put_multipath_config(conf);
190 put_multipath_config(conf);
196 uevent_can_filter(struct uevent *earlier, struct uevent *later)
200 * filter earlier uvents if path has removed later. Eg:
201 * "add path1 |chang path1 |add path2 |remove path1"
203 * "add path2 |remove path1"
204 * uevents "add path1" and "chang path1" are filtered out
206 if (!strcmp(earlier->kernel, later->kernel) &&
207 !strcmp(later->action, "remove") &&
208 strncmp(later->kernel, "dm-", 3)) {
213 * filter change uvents if add uevents exist. Eg:
214 * "change path1| add path1 |add path2"
216 * "add path1 |add path2"
217 * uevent "chang path1" is filtered out
219 if (!strcmp(earlier->kernel, later->kernel) &&
220 !strcmp(earlier->action, "change") &&
221 !strcmp(later->action, "add") &&
222 strncmp(later->kernel, "dm-", 3)) {
230 merge_need_stop(struct uevent *earlier, struct uevent *later)
233 * dm uevent do not try to merge with left uevents
235 if (!strncmp(later->kernel, "dm-", 3))
239 * we can not make a jugement without wwid,
240 * so it is sensible to stop merging
242 if (!earlier->wwid || !later->wwid)
245 * uevents merging stoped
246 * when we meet an opposite action uevent from the same LUN to AVOID
247 * "add path1 |remove path1 |add path2 |remove path2 |add path3"
248 * to merge as "remove path1, path2" and "add path1, path2, path3"
250 * "remove path1 |add path1 |remove path2 |add path2 |remove path3"
251 * to merge as "add path1, path2" and "remove path1, path2, path3"
253 * when we meet a non-change uevent from the same LUN
254 * with the same wwid and different action
255 * it would be better to stop merging.
257 if (!strcmp(earlier->wwid, later->wwid) &&
258 strcmp(earlier->action, later->action) &&
259 strcmp(earlier->action, "change") &&
260 strcmp(later->action, "change"))
267 uevent_can_merge(struct uevent *earlier, struct uevent *later)
269 /* merge paths uevents
270 * whose wwids exsit and are same
271 * and actions are same,
272 * and actions are addition or deletion
274 if (earlier->wwid && later->wwid &&
275 !strcmp(earlier->wwid, later->wwid) &&
276 !strcmp(earlier->action, later->action) &&
277 strncmp(earlier->action, "change", 6) &&
278 strncmp(earlier->kernel, "dm-", 3)) {
286 uevent_prepare(struct list_head *tmpq)
288 struct uevent *uev, *tmp;
290 list_for_each_entry_reverse_safe(uev, tmp, tmpq, node) {
291 if (uevent_can_discard(uev)) {
292 list_del_init(&uev->node);
294 udev_device_unref(uev->udev);
299 if (strncmp(uev->kernel, "dm-", 3) &&
301 uevent_get_wwid(uev);
306 uevent_filter(struct uevent *later, struct list_head *tmpq)
308 struct uevent *earlier, *tmp;
310 list_for_some_entry_reverse_safe(earlier, tmp, &later->node, tmpq, node) {
312 * filter unnessary earlier uevents
313 * by the later uevent
315 if (uevent_can_filter(earlier, later)) {
316 condlog(2, "uevent: %s-%s has filtered by uevent: %s-%s",
317 earlier->kernel, earlier->action,
318 later->kernel, later->action);
320 list_del_init(&earlier->node);
322 udev_device_unref(earlier->udev);
329 uevent_merge(struct uevent *later, struct list_head *tmpq)
331 struct uevent *earlier, *tmp;
333 list_for_some_entry_reverse_safe(earlier, tmp, &later->node, tmpq, node) {
334 if (merge_need_stop(earlier, later))
337 * merge earlier uevents to the later uevent
339 if (uevent_can_merge(earlier, later)) {
340 condlog(2, "merged uevent: %s-%s-%s with uevent: %s-%s-%s",
341 earlier->action, earlier->kernel, earlier->wwid,
342 later->action, later->kernel, later->wwid);
344 list_move(&earlier->node, &later->merge_node);
350 merge_uevq(struct list_head *tmpq)
352 struct uevent *later;
354 uevent_prepare(tmpq);
355 list_for_each_entry_reverse(later, tmpq, node) {
356 uevent_filter(later, tmpq);
357 if(uevent_need_merge())
358 uevent_merge(later, tmpq);
363 service_uevq(struct list_head *tmpq)
365 struct uevent *uev, *tmp;
367 list_for_each_entry_safe(uev, tmp, tmpq, node) {
368 list_del_init(&uev->node);
370 if (my_uev_trigger && my_uev_trigger(uev, my_trigger_data))
371 condlog(0, "uevent trigger error");
373 uevq_cleanup(&uev->merge_node);
376 udev_device_unref(uev->udev);
381 static void uevent_cleanup(void *arg)
383 struct udev *udev = arg;
385 condlog(3, "Releasing uevent_listen() resources");
390 * Service the uevent queue.
392 int uevent_dispatch(int (*uev_trigger)(struct uevent *, void * trigger_data),
395 my_uev_trigger = uev_trigger;
396 my_trigger_data = trigger_data;
398 mlockall(MCL_CURRENT | MCL_FUTURE);
403 pthread_mutex_lock(uevq_lockp);
406 * Condition signals are unreliable,
407 * so make sure we only wait if we have to.
409 if (list_empty(&uevq)) {
410 pthread_cond_wait(uev_condp, uevq_lockp);
413 list_splice_init(&uevq, &uevq_tmp);
414 pthread_mutex_unlock(uevq_lockp);
417 merge_uevq(&uevq_tmp);
418 service_uevq(&uevq_tmp);
420 condlog(3, "Terminating uev service queue");
425 struct uevent *uevent_from_buffer(char *buf, ssize_t buflen)
433 uev = alloc_uevent();
435 condlog(1, "lost uevent, oom");
439 if ((size_t)buflen > sizeof(buf)-1)
440 buflen = sizeof(buf)-1;
443 * Copy the shared receive buffer contents to buffer private
444 * to this uevent so we can immediately reuse the shared buffer.
446 memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
447 buffer = uev->buffer;
448 buffer[buflen] = '\0';
450 /* save start of payload */
451 bufpos = strlen(buffer) + 1;
454 uev->action = buffer;
455 pos = strchr(buffer, '@');
457 condlog(3, "bad action string '%s'", buffer);
464 uev->devpath = &pos[1];
466 /* hotplug events have the environment attached - reconstruct envp[] */
467 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
471 key = &buffer[bufpos];
472 keylen = strlen(key);
474 /* Filter out sequence number */
475 if (strncmp(key, "SEQNUM=", 7) == 0) {
478 uev->seqnum = strtoul(key + 7, &eptr, 10);
482 bufpos += keylen + 1;
486 condlog(3, "uevent %ld '%s' from '%s'", uev->seqnum,
487 uev->action, uev->devpath);
488 uev->kernel = strrchr(uev->devpath, '/');
492 /* print payload environment */
493 for (i = 0; uev->envp[i] != NULL; i++)
494 condlog(5, "%s", uev->envp[i]);
499 int failback_listen(void)
502 struct sockaddr_nl snl;
503 struct sockaddr_un sun;
506 int rcvbufsz = 128*1024;
508 int rcvszsz = sizeof(rcvsz);
509 unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
510 const int feature_on = 1;
512 * First check whether we have a udev socket
514 memset(&sun, 0x00, sizeof(struct sockaddr_un));
515 sun.sun_family = AF_LOCAL;
516 strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
517 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
519 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
522 condlog(3, "reading events from udev socket.");
524 /* the bind takes care of ensuring only one copy running */
525 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
527 condlog(0, "bind failed, exit");
531 /* enable receiving of the sender credentials */
532 retval = setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
533 &feature_on, sizeof(feature_on));
535 condlog(0, "failed to enable credential passing, exit");
540 /* Fallback to read kernel netlink events */
541 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
542 snl.nl_family = AF_NETLINK;
543 snl.nl_pid = getpid();
544 snl.nl_groups = 0x01;
546 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
548 condlog(0, "error getting socket, exit");
552 condlog(3, "reading events from kernel.");
555 * try to avoid dropping uevents, even so, this is not a guarantee,
556 * but it does help to change the netlink uevent socket's
557 * receive buffer threshold from the default value of 106,496 to
558 * the maximum value of 262,142.
560 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
564 condlog(0, "error setting receive buffer size for socket, exit");
567 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
569 condlog(0, "error setting receive buffer size for socket, exit");
572 condlog(3, "receive buffer size for socket is %u.", rcvsz);
574 /* enable receiving of the sender credentials */
575 if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
576 &feature_on, sizeof(feature_on)) < 0) {
577 condlog(0, "error on enabling credential passing for socket");
581 retval = bind(sock, (struct sockaddr *) &snl,
582 sizeof(struct sockaddr_nl));
584 condlog(0, "bind failed, exit");
595 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
596 struct cmsghdr *cmsg;
598 static char buf[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
600 memset(buf, 0x00, sizeof(buf));
602 iov.iov_len = sizeof(buf);
603 memset (&smsg, 0x00, sizeof(struct msghdr));
606 smsg.msg_control = cred_msg;
607 smsg.msg_controllen = sizeof(cred_msg);
609 buflen = recvmsg(sock, &smsg, 0);
612 condlog(0, "error receiving message, errno %d", errno);
616 cmsg = CMSG_FIRSTHDR(&smsg);
617 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
618 condlog(3, "no sender credentials received, message ignored");
622 cred = (struct ucred *)CMSG_DATA(cmsg);
623 if (cred->uid != 0) {
624 condlog(3, "sender uid=%d, message ignored", cred->uid);
629 bufpos = strlen(buf) + 1;
630 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
631 condlog(3, "invalid message length");
635 /* check message header */
636 if (strstr(buf, "@/") == NULL) {
637 condlog(3, "unrecognized message header");
640 if ((size_t)buflen > sizeof(buf)-1) {
641 condlog(2, "buffer overflow for received uevent");
642 buflen = sizeof(buf)-1;
645 uev = uevent_from_buffer(buf, buflen);
649 * Queue uevent and poke service pthread.
651 pthread_mutex_lock(uevq_lockp);
652 list_add_tail(&uev->node, &uevq);
653 pthread_cond_signal(uev_condp);
654 pthread_mutex_unlock(uevq_lockp);
662 struct uevent *uevent_from_udev_device(struct udev_device *dev)
667 struct udev_list_entry *list_entry;
669 uev = alloc_uevent();
671 udev_device_unref(dev);
672 condlog(1, "lost uevent, oom");
676 end = pos + HOTPLUG_BUFFER_SIZE + OBJECT_SIZE - 1;
677 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev)) {
678 const char *name, *value;
681 name = udev_list_entry_get_name(list_entry);
684 value = udev_list_entry_get_value(list_entry);
687 bytes = snprintf(pos, end - pos, "%s=%s", name, value);
688 if (pos + bytes >= end) {
689 condlog(2, "buffer overflow for uevent");
696 if (strcmp(name, "DEVPATH") == 0)
697 uev->devpath = uev->envp[i] + 8;
698 if (strcmp(name, "ACTION") == 0)
699 uev->action = uev->envp[i] + 7;
701 if (i == HOTPLUG_NUM_ENVP - 1)
707 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
708 uev->kernel = strrchr(uev->devpath, '/');
712 /* print payload environment */
713 for (i = 0; uev->envp[i] != NULL; i++)
714 condlog(5, "%s", uev->envp[i]);
718 bool uevent_burst(struct timeval *start_time, int events)
720 struct timeval diff_time, end_time;
722 unsigned long eclipse_ms;
724 if(events > MAX_ACCUMULATION_COUNT) {
725 condlog(2, "burst got %u uevents, too much uevents, stopped", events);
729 gettimeofday(&end_time, NULL);
730 timersub(&end_time, start_time, &diff_time);
732 eclipse_ms = diff_time.tv_sec * 1000 + diff_time.tv_usec / 1000;
737 if (eclipse_ms > MAX_ACCUMULATION_TIME) {
738 condlog(2, "burst continued %lu ms, too long time, stopped", eclipse_ms);
742 speed = (events * 1000) / eclipse_ms;
743 if (speed > MIN_BURST_SPEED)
749 int uevent_listen(struct udev *udev)
752 struct udev_monitor *monitor = NULL;
753 int fd, socket_flags, events;
754 struct timeval start_time;
755 int need_failback = 1;
757 LIST_HEAD(uevlisten_tmp);
760 * Queue uevents for service by dedicated thread so that the uevent
761 * listening thread does not block on multipathd locks (vecs->lock)
762 * thereby not getting to empty the socket's receive buffer queue
766 condlog(1, "no udev context");
770 pthread_cleanup_push(uevent_cleanup, udev);
772 monitor = udev_monitor_new_from_netlink(udev, "udev");
774 condlog(2, "failed to create udev monitor");
777 #ifdef LIBUDEV_API_RECVBUF
778 if (udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024))
779 condlog(2, "failed to increase buffer size");
781 fd = udev_monitor_get_fd(monitor);
783 condlog(2, "failed to get monitor fd");
786 socket_flags = fcntl(fd, F_GETFL);
787 if (socket_flags < 0) {
788 condlog(2, "failed to get monitor socket flags : %s",
792 if (fcntl(fd, F_SETFL, socket_flags & ~O_NONBLOCK) < 0) {
793 condlog(2, "failed to set monitor socket flags : %s",
797 err = udev_monitor_filter_add_match_subsystem_devtype(monitor, "block",
800 condlog(2, "failed to create filter : %s", strerror(-err));
801 err = udev_monitor_enable_receiving(monitor);
803 condlog(2, "failed to enable receiving : %s", strerror(-err));
808 gettimeofday(&start_time, NULL);
811 struct udev_device *dev;
812 struct pollfd ev_poll;
816 memset(&ev_poll, 0, sizeof(struct pollfd));
818 ev_poll.events = POLLIN;
819 poll_timeout = timeout * 1000;
821 fdcount = poll(&ev_poll, 1, poll_timeout);
822 if (fdcount && ev_poll.revents & POLLIN) {
823 timeout = uevent_burst(&start_time, events + 1) ? 1 : 0;
824 dev = udev_monitor_receive_device(monitor);
826 condlog(0, "failed getting udev device");
829 uev = uevent_from_udev_device(dev);
832 list_add_tail(&uev->node, &uevlisten_tmp);
840 condlog(0, "error receiving "
841 "uevent message: %m");
845 if (!list_empty(&uevlisten_tmp)) {
847 * Queue uevents and poke service pthread.
849 condlog(3, "Forwarding %d uevents", events);
850 pthread_mutex_lock(uevq_lockp);
851 list_splice_tail_init(&uevlisten_tmp, &uevq);
852 pthread_cond_signal(uev_condp);
853 pthread_mutex_unlock(uevq_lockp);
856 gettimeofday(&start_time, NULL);
862 udev_monitor_unref(monitor);
864 err = failback_listen();
865 pthread_cleanup_pop(1);
869 int uevent_get_major(struct uevent *uev)
874 for (i = 0; uev->envp[i] != NULL; i++) {
875 if (!strncmp(uev->envp[i], "MAJOR", 5) && strlen(uev->envp[i]) > 6) {
876 p = uev->envp[i] + 6;
877 major = strtoul(p, &q, 10);
879 condlog(2, "invalid major '%s'", p);
888 int uevent_get_minor(struct uevent *uev)
893 for (i = 0; uev->envp[i] != NULL; i++) {
894 if (!strncmp(uev->envp[i], "MINOR", 5) && strlen(uev->envp[i]) > 6) {
895 p = uev->envp[i] + 6;
896 minor = strtoul(p, &q, 10);
898 condlog(2, "invalid minor '%s'", p);
907 int uevent_get_disk_ro(struct uevent *uev)
912 for (i = 0; uev->envp[i] != NULL; i++) {
913 if (!strncmp(uev->envp[i], "DISK_RO", 6) && strlen(uev->envp[i]) > 7) {
914 p = uev->envp[i] + 8;
915 ro = strtoul(p, &q, 10);
917 condlog(2, "invalid read_only setting '%s'", p);
926 char *uevent_get_dm_name(struct uevent *uev)
931 for (i = 0; uev->envp[i] != NULL; i++) {
932 if (!strncmp(uev->envp[i], "DM_NAME", 6) &&
933 strlen(uev->envp[i]) > 7) {
934 p = MALLOC(strlen(uev->envp[i] + 8) + 1);
935 strcpy(p, uev->envp[i] + 8);