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>
51 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
55 pthread_mutex_t uevq_lock = PTHREAD_MUTEX_INITIALIZER;
56 pthread_mutex_t *uevq_lockp = &uevq_lock;
57 pthread_cond_t uev_cond = PTHREAD_COND_INITIALIZER;
58 pthread_cond_t *uev_condp = &uev_cond;
59 uev_trigger *my_uev_trigger;
60 void * my_trigger_data;
63 int is_uevent_busy(void)
67 pthread_mutex_lock(uevq_lockp);
68 empty = list_empty(&uevq);
69 pthread_mutex_unlock(uevq_lockp);
70 return (!empty || servicing_uev);
73 struct uevent * alloc_uevent (void)
75 struct uevent *uev = MALLOC(sizeof(struct uevent));
78 INIT_LIST_HEAD(&uev->node);
84 setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
86 if (pthread_attr_init(attr)) {
87 fprintf(stderr, "can't initialize thread attr: %s\n",
91 if (stacksize < PTHREAD_STACK_MIN)
92 stacksize = PTHREAD_STACK_MIN;
94 if (pthread_attr_setstacksize(attr, stacksize)) {
95 fprintf(stderr, "can't set thread stack size to %lu: %s\n",
96 (unsigned long)stacksize, strerror(errno));
99 if (detached && pthread_attr_setdetachstate(attr,
100 PTHREAD_CREATE_DETACHED)) {
101 fprintf(stderr, "can't set thread to detached: %s\n",
108 * Called with uevq_lockp held
111 service_uevq(struct list_head *tmpq)
113 struct uevent *uev, *tmp;
115 list_for_each_entry_safe(uev, tmp, tmpq, node) {
116 list_del_init(&uev->node);
118 if (my_uev_trigger && my_uev_trigger(uev, my_trigger_data))
119 condlog(0, "uevent trigger error");
122 udev_device_unref(uev->udev);
127 static void uevq_stop(void *arg)
129 struct udev *udev = arg;
131 condlog(3, "Stopping uev queue");
132 pthread_mutex_lock(uevq_lockp);
133 my_uev_trigger = NULL;
134 pthread_cond_signal(uev_condp);
135 pthread_mutex_unlock(uevq_lockp);
140 uevq_cleanup(struct list_head *tmpq)
142 struct uevent *uev, *tmp;
144 list_for_each_entry_safe(uev, tmp, tmpq, node) {
145 list_del_init(&uev->node);
151 * Service the uevent queue.
153 int uevent_dispatch(int (*uev_trigger)(struct uevent *, void * trigger_data),
156 my_uev_trigger = uev_trigger;
157 my_trigger_data = trigger_data;
159 mlockall(MCL_CURRENT | MCL_FUTURE);
164 pthread_mutex_lock(uevq_lockp);
167 * Condition signals are unreliable,
168 * so make sure we only wait if we have to.
170 if (list_empty(&uevq)) {
171 pthread_cond_wait(uev_condp, uevq_lockp);
174 list_splice_init(&uevq, &uevq_tmp);
175 pthread_mutex_unlock(uevq_lockp);
178 service_uevq(&uevq_tmp);
180 condlog(3, "Terminating uev service queue");
185 struct uevent *uevent_from_buffer(char *buf, ssize_t buflen)
193 uev = alloc_uevent();
195 condlog(1, "lost uevent, oom");
199 if ((size_t)buflen > sizeof(buf)-1)
200 buflen = sizeof(buf)-1;
203 * Copy the shared receive buffer contents to buffer private
204 * to this uevent so we can immediately reuse the shared buffer.
206 memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
207 buffer = uev->buffer;
208 buffer[buflen] = '\0';
210 /* save start of payload */
211 bufpos = strlen(buffer) + 1;
214 uev->action = buffer;
215 pos = strchr(buffer, '@');
217 condlog(3, "bad action string '%s'", buffer);
224 uev->devpath = &pos[1];
226 /* hotplug events have the environment attached - reconstruct envp[] */
227 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
231 key = &buffer[bufpos];
232 keylen = strlen(key);
234 /* Filter out sequence number */
235 if (strncmp(key, "SEQNUM=", 7) == 0) {
238 uev->seqnum = strtoul(key + 7, &eptr, 10);
242 bufpos += keylen + 1;
246 condlog(3, "uevent %ld '%s' from '%s'", uev->seqnum,
247 uev->action, uev->devpath);
248 uev->kernel = strrchr(uev->devpath, '/');
252 /* print payload environment */
253 for (i = 0; uev->envp[i] != NULL; i++)
254 condlog(5, "%s", uev->envp[i]);
259 int failback_listen(void)
262 struct sockaddr_nl snl;
263 struct sockaddr_un sun;
266 int rcvbufsz = 128*1024;
268 int rcvszsz = sizeof(rcvsz);
269 unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
270 const int feature_on = 1;
272 * First check whether we have a udev socket
274 memset(&sun, 0x00, sizeof(struct sockaddr_un));
275 sun.sun_family = AF_LOCAL;
276 strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
277 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
279 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
282 condlog(3, "reading events from udev socket.");
284 /* the bind takes care of ensuring only one copy running */
285 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
287 condlog(0, "bind failed, exit");
291 /* enable receiving of the sender credentials */
292 retval = setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
293 &feature_on, sizeof(feature_on));
295 condlog(0, "failed to enable credential passing, exit");
300 /* Fallback to read kernel netlink events */
301 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
302 snl.nl_family = AF_NETLINK;
303 snl.nl_pid = getpid();
304 snl.nl_groups = 0x01;
306 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
308 condlog(0, "error getting socket, exit");
312 condlog(3, "reading events from kernel.");
315 * try to avoid dropping uevents, even so, this is not a guarantee,
316 * but it does help to change the netlink uevent socket's
317 * receive buffer threshold from the default value of 106,496 to
318 * the maximum value of 262,142.
320 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
324 condlog(0, "error setting receive buffer size for socket, exit");
327 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
329 condlog(0, "error setting receive buffer size for socket, exit");
332 condlog(3, "receive buffer size for socket is %u.", rcvsz);
334 /* enable receiving of the sender credentials */
335 if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
336 &feature_on, sizeof(feature_on)) < 0) {
337 condlog(0, "error on enabling credential passing for socket");
341 retval = bind(sock, (struct sockaddr *) &snl,
342 sizeof(struct sockaddr_nl));
344 condlog(0, "bind failed, exit");
355 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
356 struct cmsghdr *cmsg;
358 static char buf[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
360 memset(buf, 0x00, sizeof(buf));
362 iov.iov_len = sizeof(buf);
363 memset (&smsg, 0x00, sizeof(struct msghdr));
366 smsg.msg_control = cred_msg;
367 smsg.msg_controllen = sizeof(cred_msg);
369 buflen = recvmsg(sock, &smsg, 0);
372 condlog(0, "error receiving message, errno %d", errno);
376 cmsg = CMSG_FIRSTHDR(&smsg);
377 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
378 condlog(3, "no sender credentials received, message ignored");
382 cred = (struct ucred *)CMSG_DATA(cmsg);
383 if (cred->uid != 0) {
384 condlog(3, "sender uid=%d, message ignored", cred->uid);
389 bufpos = strlen(buf) + 1;
390 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
391 condlog(3, "invalid message length");
395 /* check message header */
396 if (strstr(buf, "@/") == NULL) {
397 condlog(3, "unrecognized message header");
400 if ((size_t)buflen > sizeof(buf)-1) {
401 condlog(2, "buffer overflow for received uevent");
402 buflen = sizeof(buf)-1;
405 uev = uevent_from_buffer(buf, buflen);
409 * Queue uevent and poke service pthread.
411 pthread_mutex_lock(uevq_lockp);
412 list_add_tail(&uev->node, &uevq);
413 pthread_cond_signal(uev_condp);
414 pthread_mutex_unlock(uevq_lockp);
422 struct uevent *uevent_from_udev_device(struct udev_device *dev)
427 struct udev_list_entry *list_entry;
429 uev = alloc_uevent();
431 udev_device_unref(dev);
432 condlog(1, "lost uevent, oom");
436 end = pos + HOTPLUG_BUFFER_SIZE + OBJECT_SIZE - 1;
437 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev)) {
438 const char *name, *value;
441 name = udev_list_entry_get_name(list_entry);
444 value = udev_list_entry_get_value(list_entry);
447 bytes = snprintf(pos, end - pos, "%s=%s", name, value);
448 if (pos + bytes >= end) {
449 condlog(2, "buffer overflow for uevent");
456 if (strcmp(name, "DEVPATH") == 0)
457 uev->devpath = uev->envp[i] + 8;
458 if (strcmp(name, "ACTION") == 0)
459 uev->action = uev->envp[i] + 7;
461 if (i == HOTPLUG_NUM_ENVP - 1)
467 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
468 uev->kernel = strrchr(uev->devpath, '/');
472 /* print payload environment */
473 for (i = 0; uev->envp[i] != NULL; i++)
474 condlog(5, "%s", uev->envp[i]);
478 int uevent_listen(struct udev *udev)
481 struct udev_monitor *monitor = NULL;
482 int fd, socket_flags, events;
483 int need_failback = 1;
485 LIST_HEAD(uevlisten_tmp);
488 * Queue uevents for service by dedicated thread so that the uevent
489 * listening thread does not block on multipathd locks (vecs->lock)
490 * thereby not getting to empty the socket's receive buffer queue
494 condlog(1, "no udev context");
498 pthread_cleanup_push(uevq_stop, udev);
500 monitor = udev_monitor_new_from_netlink(udev, "udev");
502 condlog(2, "failed to create udev monitor");
505 #ifdef LIBUDEV_API_RECVBUF
506 if (udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024))
507 condlog(2, "failed to increase buffer size");
509 fd = udev_monitor_get_fd(monitor);
511 condlog(2, "failed to get monitor fd");
514 socket_flags = fcntl(fd, F_GETFL);
515 if (socket_flags < 0) {
516 condlog(2, "failed to get monitor socket flags : %s",
520 if (fcntl(fd, F_SETFL, socket_flags & ~O_NONBLOCK) < 0) {
521 condlog(2, "failed to set monitor socket flags : %s",
525 err = udev_monitor_filter_add_match_subsystem_devtype(monitor, "block",
528 condlog(2, "failed to create filter : %s", strerror(-err));
529 err = udev_monitor_enable_receiving(monitor);
531 condlog(2, "failed to enable receiving : %s", strerror(-err));
538 struct udev_device *dev;
539 struct pollfd ev_poll;
543 memset(&ev_poll, 0, sizeof(struct pollfd));
545 ev_poll.events = POLLIN;
546 poll_timeout = timeout * 1000;
548 fdcount = poll(&ev_poll, 1, poll_timeout);
549 if (fdcount && ev_poll.revents & POLLIN) {
551 dev = udev_monitor_receive_device(monitor);
553 condlog(0, "failed getting udev device");
556 uev = uevent_from_udev_device(dev);
559 list_add_tail(&uev->node, &uevlisten_tmp);
567 condlog(0, "error receiving "
568 "uevent message: %m");
572 if (!list_empty(&uevlisten_tmp)) {
574 * Queue uevents and poke service pthread.
576 condlog(3, "Forwarding %d uevents", events);
577 pthread_mutex_lock(uevq_lockp);
578 list_splice_tail_init(&uevlisten_tmp, &uevq);
579 pthread_cond_signal(uev_condp);
580 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);