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>
49 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
53 pthread_mutex_t uevq_lock, *uevq_lockp = &uevq_lock;
54 pthread_cond_t uev_cond, *uev_condp = &uev_cond;
55 uev_trigger *my_uev_trigger;
56 void * my_trigger_data;
59 int is_uevent_busy(void)
63 pthread_mutex_lock(uevq_lockp);
64 empty = list_empty(&uevq);
65 pthread_mutex_unlock(uevq_lockp);
66 return (!empty || servicing_uev);
69 struct uevent * alloc_uevent (void)
71 struct uevent *uev = MALLOC(sizeof(struct uevent));
74 INIT_LIST_HEAD(&uev->node);
80 setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
82 if (pthread_attr_init(attr)) {
83 fprintf(stderr, "can't initialize thread attr: %s\n",
87 if (stacksize < PTHREAD_STACK_MIN)
88 stacksize = PTHREAD_STACK_MIN;
90 if (pthread_attr_setstacksize(attr, stacksize)) {
91 fprintf(stderr, "can't set thread stack size to %lu: %s\n",
92 (unsigned long)stacksize, strerror(errno));
95 if (detached && pthread_attr_setdetachstate(attr,
96 PTHREAD_CREATE_DETACHED)) {
97 fprintf(stderr, "can't set thread to detached: %s\n",
104 * Called with uevq_lockp held
107 service_uevq(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);
114 if (my_uev_trigger && my_uev_trigger(uev, my_trigger_data))
115 condlog(0, "uevent trigger error");
121 static void uevq_stop(void *arg)
123 condlog(3, "Stopping uev queue");
124 pthread_mutex_lock(uevq_lockp);
125 my_uev_trigger = NULL;
126 pthread_cond_signal(uev_condp);
127 pthread_mutex_unlock(uevq_lockp);
131 * Service the uevent queue.
133 int uevent_dispatch(int (*uev_trigger)(struct uevent *, void * trigger_data),
136 my_uev_trigger = uev_trigger;
137 my_trigger_data = trigger_data;
139 mlockall(MCL_CURRENT | MCL_FUTURE);
144 pthread_mutex_lock(uevq_lockp);
147 * Condition signals are unreliable,
148 * so make sure we only wait if we have to.
150 if (list_empty(&uevq)) {
151 pthread_cond_wait(uev_condp, uevq_lockp);
154 list_splice_init(&uevq, &uevq_tmp);
155 pthread_mutex_unlock(uevq_lockp);
158 service_uevq(&uevq_tmp);
160 condlog(3, "Terminating uev service queue");
164 int uevent_listen(void)
167 struct sockaddr_nl snl;
168 struct sockaddr_un sun;
171 int rcvbufsz = 128*1024;
173 int rcvszsz = sizeof(rcvsz);
174 unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
175 const int feature_on = 1;
178 * Queue uevents for service by dedicated thread so that the uevent
179 * listening thread does not block on multipathd locks (vecs->lock)
180 * thereby not getting to empty the socket's receive buffer queue
183 INIT_LIST_HEAD(&uevq);
185 pthread_mutex_init(uevq_lockp, NULL);
186 pthread_cond_init(uev_condp, NULL);
188 pthread_cleanup_push(uevq_stop, NULL);
191 * First check whether we have a udev socket
193 memset(&sun, 0x00, sizeof(struct sockaddr_un));
194 sun.sun_family = AF_LOCAL;
195 strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
196 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
198 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
201 condlog(3, "reading events from udev socket.");
203 /* the bind takes care of ensuring only one copy running */
204 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
206 condlog(0, "bind failed, exit");
210 /* enable receiving of the sender credentials */
211 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
212 &feature_on, sizeof(feature_on));
215 /* Fallback to read kernel netlink events */
216 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
217 snl.nl_family = AF_NETLINK;
218 snl.nl_pid = getpid();
219 snl.nl_groups = 0x01;
221 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
223 condlog(0, "error getting socket, exit");
227 condlog(3, "reading events from kernel.");
230 * try to avoid dropping uevents, even so, this is not a guarantee,
231 * but it does help to change the netlink uevent socket's
232 * receive buffer threshold from the default value of 106,496 to
233 * the maximum value of 262,142.
235 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
239 condlog(0, "error setting receive buffer size for socket, exit");
242 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
244 condlog(0, "error setting receive buffer size for socket, exit");
247 condlog(3, "receive buffer size for socket is %u.", rcvsz);
249 /* enable receiving of the sender credentials */
250 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
251 &feature_on, sizeof(feature_on));
253 retval = bind(sock, (struct sockaddr *) &snl,
254 sizeof(struct sockaddr_nl));
256 condlog(0, "bind failed, exit");
270 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
271 struct cmsghdr *cmsg;
273 static char buf[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
275 memset(buf, 0x00, sizeof(buf));
277 iov.iov_len = sizeof(buf);
278 memset (&smsg, 0x00, sizeof(struct msghdr));
281 smsg.msg_control = cred_msg;
282 smsg.msg_controllen = sizeof(cred_msg);
284 buflen = recvmsg(sock, &smsg, 0);
287 condlog(0, "error receiving message, errno %d", errno);
291 cmsg = CMSG_FIRSTHDR(&smsg);
292 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
293 condlog(3, "no sender credentials received, message ignored");
297 cred = (struct ucred *)CMSG_DATA(cmsg);
298 if (cred->uid != 0) {
299 condlog(3, "sender uid=%d, message ignored", cred->uid);
304 bufpos = strlen(buf) + 1;
305 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
306 condlog(3, "invalid message length");
310 /* check message header */
311 if (strstr(buf, "@/") == NULL) {
312 condlog(3, "unrecognized message header");
315 if ((size_t)buflen > sizeof(buf)-1) {
316 condlog(2, "buffer overflow for received uevent");
317 buflen = sizeof(buf)-1;
320 uev = alloc_uevent();
323 condlog(1, "lost uevent, oom");
327 if ((size_t)buflen > sizeof(buf)-1)
328 buflen = sizeof(buf)-1;
331 * Copy the shared receive buffer contents to buffer private
332 * to this uevent so we can immediately reuse the shared buffer.
334 memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
335 buffer = uev->buffer;
336 buffer[buflen] = '\0';
338 /* save start of payload */
339 bufpos = strlen(buffer) + 1;
342 uev->action = buffer;
343 pos = strchr(buffer, '@');
345 condlog(3, "bad action string '%s'", buffer);
351 uev->devpath = &pos[1];
353 /* hotplug events have the environment attached - reconstruct envp[] */
354 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
358 key = &buffer[bufpos];
359 keylen = strlen(key);
361 bufpos += keylen + 1;
365 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
367 /* print payload environment */
368 for (i = 0; uev->envp[i] != NULL; i++)
369 condlog(3, "%s", uev->envp[i]);
372 * Queue uevent and poke service pthread.
374 pthread_mutex_lock(uevq_lockp);
375 list_add_tail(&uev->node, &uevq);
376 pthread_cond_signal(uev_condp);
377 pthread_mutex_unlock(uevq_lockp);
383 pthread_cleanup_pop(1);
385 pthread_mutex_destroy(uevq_lockp);
386 pthread_cond_destroy(uev_condp);