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>
37 #include <linux/types.h>
38 #include <linux/netlink.h>
50 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
54 pthread_mutex_t uevq_lock, *uevq_lockp = &uevq_lock;
55 pthread_cond_t uev_cond, *uev_condp = &uev_cond;
56 uev_trigger *my_uev_trigger;
57 void * my_trigger_data;
60 int is_uevent_busy(void)
64 pthread_mutex_lock(uevq_lockp);
65 empty = list_empty(&uevq);
66 pthread_mutex_unlock(uevq_lockp);
67 return (!empty || servicing_uev);
70 struct uevent * alloc_uevent (void)
72 struct uevent *uev = MALLOC(sizeof(struct uevent));
75 INIT_LIST_HEAD(&uev->node);
81 setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
83 if (pthread_attr_init(attr)) {
84 fprintf(stderr, "can't initialize thread attr: %s\n",
88 if (stacksize < PTHREAD_STACK_MIN)
89 stacksize = PTHREAD_STACK_MIN;
91 if (pthread_attr_setstacksize(attr, stacksize)) {
92 fprintf(stderr, "can't set thread stack size to %lu: %s\n",
93 (unsigned long)stacksize, strerror(errno));
96 if (detached && pthread_attr_setdetachstate(attr,
97 PTHREAD_CREATE_DETACHED)) {
98 fprintf(stderr, "can't set thread to detached: %s\n",
105 * Called with uevq_lockp held
108 service_uevq(struct list_head *tmpq)
110 struct uevent *uev, *tmp;
112 list_for_each_entry_safe(uev, tmp, tmpq, node) {
113 list_del_init(&uev->node);
115 if (my_uev_trigger && my_uev_trigger(uev, my_trigger_data))
116 condlog(0, "uevent trigger error");
122 static void uevq_stop(void *arg)
124 condlog(3, "Stopping uev queue");
125 pthread_mutex_lock(uevq_lockp);
126 my_uev_trigger = NULL;
127 pthread_cond_signal(uev_condp);
128 pthread_mutex_unlock(uevq_lockp);
132 * Service the uevent queue.
134 int uevent_dispatch(int (*uev_trigger)(struct uevent *, void * trigger_data),
137 my_uev_trigger = uev_trigger;
138 my_trigger_data = trigger_data;
140 mlockall(MCL_CURRENT | MCL_FUTURE);
145 pthread_mutex_lock(uevq_lockp);
148 * Condition signals are unreliable,
149 * so make sure we only wait if we have to.
151 if (list_empty(&uevq)) {
152 pthread_cond_wait(uev_condp, uevq_lockp);
155 list_splice_init(&uevq, &uevq_tmp);
156 pthread_mutex_unlock(uevq_lockp);
159 service_uevq(&uevq_tmp);
161 condlog(3, "Terminating uev service queue");
165 int failback_listen(void)
168 struct sockaddr_nl snl;
169 struct sockaddr_un sun;
172 int rcvbufsz = 128*1024;
174 int rcvszsz = sizeof(rcvsz);
175 unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
176 const int feature_on = 1;
178 * First check whether we have a udev socket
180 memset(&sun, 0x00, sizeof(struct sockaddr_un));
181 sun.sun_family = AF_LOCAL;
182 strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
183 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
185 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
188 condlog(3, "reading events from udev socket.");
190 /* the bind takes care of ensuring only one copy running */
191 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
193 condlog(0, "bind failed, exit");
197 /* enable receiving of the sender credentials */
198 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
199 &feature_on, sizeof(feature_on));
202 /* Fallback to read kernel netlink events */
203 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
204 snl.nl_family = AF_NETLINK;
205 snl.nl_pid = getpid();
206 snl.nl_groups = 0x01;
208 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
210 condlog(0, "error getting socket, exit");
214 condlog(3, "reading events from kernel.");
217 * try to avoid dropping uevents, even so, this is not a guarantee,
218 * but it does help to change the netlink uevent socket's
219 * receive buffer threshold from the default value of 106,496 to
220 * the maximum value of 262,142.
222 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
226 condlog(0, "error setting receive buffer size for socket, exit");
229 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
231 condlog(0, "error setting receive buffer size for socket, exit");
234 condlog(3, "receive buffer size for socket is %u.", rcvsz);
236 /* enable receiving of the sender credentials */
237 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
238 &feature_on, sizeof(feature_on));
240 retval = bind(sock, (struct sockaddr *) &snl,
241 sizeof(struct sockaddr_nl));
243 condlog(0, "bind failed, exit");
257 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
258 struct cmsghdr *cmsg;
260 static char buf[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
262 memset(buf, 0x00, sizeof(buf));
264 iov.iov_len = sizeof(buf);
265 memset (&smsg, 0x00, sizeof(struct msghdr));
268 smsg.msg_control = cred_msg;
269 smsg.msg_controllen = sizeof(cred_msg);
271 buflen = recvmsg(sock, &smsg, 0);
274 condlog(0, "error receiving message, errno %d", errno);
278 cmsg = CMSG_FIRSTHDR(&smsg);
279 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
280 condlog(3, "no sender credentials received, message ignored");
284 cred = (struct ucred *)CMSG_DATA(cmsg);
285 if (cred->uid != 0) {
286 condlog(3, "sender uid=%d, message ignored", cred->uid);
291 bufpos = strlen(buf) + 1;
292 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
293 condlog(3, "invalid message length");
297 /* check message header */
298 if (strstr(buf, "@/") == NULL) {
299 condlog(3, "unrecognized message header");
302 if ((size_t)buflen > sizeof(buf)-1) {
303 condlog(2, "buffer overflow for received uevent");
304 buflen = sizeof(buf)-1;
307 uev = alloc_uevent();
310 condlog(1, "lost uevent, oom");
314 if ((size_t)buflen > sizeof(buf)-1)
315 buflen = sizeof(buf)-1;
318 * Copy the shared receive buffer contents to buffer private
319 * to this uevent so we can immediately reuse the shared buffer.
321 memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
322 buffer = uev->buffer;
323 buffer[buflen] = '\0';
325 /* save start of payload */
326 bufpos = strlen(buffer) + 1;
329 uev->action = buffer;
330 pos = strchr(buffer, '@');
332 condlog(3, "bad action string '%s'", buffer);
338 uev->devpath = &pos[1];
340 /* hotplug events have the environment attached - reconstruct envp[] */
341 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
345 key = &buffer[bufpos];
346 keylen = strlen(key);
348 bufpos += keylen + 1;
352 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
353 uev->kernel = strrchr(uev->devpath, '/');
357 /* print payload environment */
358 for (i = 0; uev->envp[i] != NULL; i++)
359 condlog(5, "%s", uev->envp[i]);
362 * Queue uevent and poke service pthread.
364 pthread_mutex_lock(uevq_lockp);
365 list_add_tail(&uev->node, &uevq);
366 pthread_cond_signal(uev_condp);
367 pthread_mutex_unlock(uevq_lockp);
375 int uevent_listen(void)
378 struct udev *udev = NULL;
379 struct udev_monitor *monitor = NULL;
380 int fd, socket_flags;
381 int need_failback = 1;
383 * Queue uevents for service by dedicated thread so that the uevent
384 * listening thread does not block on multipathd locks (vecs->lock)
385 * thereby not getting to empty the socket's receive buffer queue
388 INIT_LIST_HEAD(&uevq);
390 pthread_mutex_init(uevq_lockp, NULL);
391 pthread_cond_init(uev_condp, NULL);
392 pthread_cleanup_push(uevq_stop, NULL);
396 condlog(2, "failed to create udev context");
399 monitor = udev_monitor_new_from_netlink(udev, "udev");
401 condlog(2, "failed to create udev monitor");
404 if (udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024))
405 condlog(2, "failed to increase buffer size");
406 fd = udev_monitor_get_fd(monitor);
408 condlog(2, "failed to get monitor fd");
411 socket_flags = fcntl(fd, F_GETFL);
412 if (socket_flags < 0) {
413 condlog(2, "failed to get monitor socket flags : %s",
417 if (fcntl(fd, F_SETFL, socket_flags & ~O_NONBLOCK) < 0) {
418 condlog(2, "failed to set monitor socket flags : %s",
422 err = udev_monitor_filter_add_match_subsystem_devtype(monitor, "block",
425 condlog(2, "failed to create filter : %s\n", strerror(-err));
426 err = udev_monitor_enable_receiving(monitor);
428 condlog(2, "failed to enable receiving : %s\n", strerror(-err));
435 struct udev_device *dev;
436 struct udev_list_entry *list_entry;
438 dev = udev_monitor_receive_device(monitor);
440 condlog(0, "failed getting udev device");
444 uev = alloc_uevent();
446 condlog(1, "lost uevent, oom");
450 end = pos + HOTPLUG_BUFFER_SIZE + OBJECT_SIZE - 1;
451 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev)) {
452 const char *name, *value;
455 name = udev_list_entry_get_name(list_entry);
458 value = udev_list_entry_get_value(list_entry);
461 bytes = snprintf(pos, end - pos, "%s=%s", name,
463 if (pos + bytes >= end) {
464 condlog(2, "buffer overflow for uevent");
471 if (strcmp(name, "DEVPATH") == 0)
472 uev->devpath = uev->envp[i] + 8;
473 if (strcmp(name, "ACTION") == 0)
474 uev->action = uev->envp[i] + 7;
476 if (i == HOTPLUG_NUM_ENVP - 1)
479 udev_device_unref(dev);
482 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
483 uev->kernel = strrchr(uev->devpath, '/');
487 /* print payload environment */
488 for (i = 0; uev->envp[i] != NULL; i++)
489 condlog(5, "%s", uev->envp[i]);
492 * Queue uevent and poke service pthread.
494 pthread_mutex_lock(uevq_lockp);
495 list_add_tail(&uev->node, &uevq);
496 pthread_cond_signal(uev_condp);
497 pthread_mutex_unlock(uevq_lockp);
502 udev_monitor_unref(monitor);
506 err = failback_listen();
507 pthread_cleanup_pop(1);
508 pthread_mutex_destroy(uevq_lockp);
509 pthread_cond_destroy(uev_condp);
514 uevent_get_major(struct uevent *uev)
519 for (i = 0; uev->envp[i] != NULL; i++) {
520 if (!strncmp(uev->envp[i], "MAJOR", 5) && strlen(uev->envp[i]) > 6) {
521 p = uev->envp[i] + 6;
522 major = strtoul(p, &q, 10);
524 condlog(2, "invalid major '%s'", p);
534 uevent_get_minor(struct uevent *uev)
539 for (i = 0; uev->envp[i] != NULL; i++) {
540 if (!strncmp(uev->envp[i], "MINOR", 5) && strlen(uev->envp[i]) > 6) {
541 p = uev->envp[i] + 6;
542 minor = strtoul(p, &q, 10);
544 condlog(2, "invalid minor '%s'", p);
554 uevent_get_disk_ro(struct uevent *uev)
559 for (i = 0; uev->envp[i] != NULL; i++) {
560 if (!strncmp(uev->envp[i], "DISK_RO", 6) && strlen(uev->envp[i]) > 7) {
561 p = uev->envp[i] + 8;
562 ro = strtoul(p, &q, 10);
564 condlog(2, "invalid read_only setting '%s'", p);
574 uevent_get_dm_name(struct uevent *uev)
579 for (i = 0; uev->envp[i] != NULL; i++) {
580 if (!strncmp(uev->envp[i], "DM_NAME", 6) &&
581 strlen(uev->envp[i]) > 7) {
582 p = MALLOC(strlen(uev->envp[i] + 8) + 1);
583 strcpy(p, uev->envp[i] + 8);