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>
46 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
49 struct uevent *uevqhp, *uevqtp;
50 pthread_mutex_t uevq_lock, *uevq_lockp = &uevq_lock;
51 pthread_mutex_t uevc_lock, *uevc_lockp = &uevc_lock;
52 pthread_cond_t uev_cond, *uev_condp = &uev_cond;
53 uev_trigger *my_uev_trigger;
54 void * my_trigger_data;
57 int is_uevent_busy(void)
59 return (uevqhp != NULL || servicing_uev);
62 static struct uevent * alloc_uevent (void)
64 return (struct uevent *)MALLOC(sizeof(struct uevent));
74 pthread_mutex_lock(uevq_lockp);
75 empty = (uevqhp == NULL);
81 pthread_mutex_unlock(uevq_lockp);
83 if (my_uev_trigger && my_uev_trigger(uev,
85 condlog(0, "uevent trigger error");
90 pthread_mutex_unlock(uevq_lockp);
96 * Service the uevent queue.
99 uevq_thread(void * et)
101 mlockall(MCL_CURRENT | MCL_FUTURE);
104 pthread_mutex_lock(uevc_lockp);
106 pthread_cond_wait(uev_condp, uevc_lockp);
108 pthread_mutex_unlock(uevc_lockp);
115 int uevent_listen(int (*uev_trigger)(struct uevent *, void * trigger_data),
119 struct sockaddr_nl snl;
120 struct sockaddr_un sun;
123 int rcvbufsz = 128*1024;
125 int rcvszsz = sizeof(rcvsz);
126 unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
128 const int feature_on = 1;
130 my_uev_trigger = uev_trigger;
131 my_trigger_data = trigger_data;
134 * Queue uevents for service by dedicated thread so that the uevent
135 * listening thread does not block on multipathd locks (vecs->lock)
136 * thereby not getting to empty the socket's receive buffer queue
139 uevqhp = uevqtp = NULL;
141 pthread_mutex_init(uevq_lockp, NULL);
142 pthread_mutex_init(uevc_lockp, NULL);
143 pthread_cond_init(uev_condp, NULL);
145 pthread_attr_init(&attr);
146 pthread_attr_setstacksize(&attr, 64 * 1024);
147 pthread_create(&uevq_thr, &attr, uevq_thread, NULL);
150 * First check whether we have a udev socket
152 memset(&sun, 0x00, sizeof(struct sockaddr_un));
153 sun.sun_family = AF_LOCAL;
154 strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
155 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
157 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
160 condlog(3, "reading events from udev socket.");
162 /* the bind takes care of ensuring only one copy running */
163 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
165 condlog(0, "bind failed, exit");
169 /* enable receiving of the sender credentials */
170 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
171 &feature_on, sizeof(feature_on));
174 /* Fallback to read kernel netlink events */
175 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
176 snl.nl_family = AF_NETLINK;
177 snl.nl_pid = getpid();
178 snl.nl_groups = 0x01;
180 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
182 condlog(0, "error getting socket, exit");
186 condlog(3, "reading events from kernel.");
189 * try to avoid dropping uevents, even so, this is not a guarantee,
190 * but it does help to change the netlink uevent socket's
191 * receive buffer threshold from the default value of 106,496 to
192 * the maximum value of 262,142.
194 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
198 condlog(0, "error setting receive buffer size for socket, exit");
201 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
203 condlog(0, "error setting receive buffer size for socket, exit");
206 condlog(3, "receive buffer size for socket is %u.", rcvsz);
208 /* enable receiving of the sender credentials */
209 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
210 &feature_on, sizeof(feature_on));
212 retval = bind(sock, (struct sockaddr *) &snl,
213 sizeof(struct sockaddr_nl));
215 condlog(0, "bind failed, exit");
229 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
230 struct cmsghdr *cmsg;
232 static char buf[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
234 memset(buf, 0x00, sizeof(buf));
236 iov.iov_len = sizeof(buf);
237 memset (&smsg, 0x00, sizeof(struct msghdr));
240 smsg.msg_control = cred_msg;
241 smsg.msg_controllen = sizeof(cred_msg);
243 buflen = recvmsg(sock, &smsg, 0);
246 condlog(0, "error receiving message");
250 cmsg = CMSG_FIRSTHDR(&smsg);
251 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
252 condlog(3, "no sender credentials received, message ignored");
256 cred = (struct ucred *)CMSG_DATA(cmsg);
257 if (cred->uid != 0) {
258 condlog(3, "sender uid=%d, message ignored", cred->uid);
263 bufpos = strlen(buf) + 1;
264 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
265 condlog(3, "invalid message length");
269 /* check message header */
270 if (strstr(buf, "@/") == NULL) {
271 condlog(3, "unrecognized message header");
275 uev = alloc_uevent();
278 condlog(1, "lost uevent, oom");
282 if ((size_t)buflen > sizeof(buf)-1)
283 buflen = sizeof(buf)-1;
286 * Copy the shared receive buffer contents to buffer private
287 * to this uevent so we can immediately reuse the shared buffer.
289 memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
290 buffer = uev->buffer;
291 buffer[buflen] = '\0';
293 /* save start of payload */
294 bufpos = strlen(buffer) + 1;
297 uev->action = buffer;
298 pos = strchr(buffer, '@');
300 condlog(3, "bad action string '%s'", buffer);
306 uev->devpath = &pos[1];
308 /* hotplug events have the environment attached - reconstruct envp[] */
309 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
313 key = &buffer[bufpos];
314 keylen = strlen(key);
316 bufpos += keylen + 1;
320 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
322 /* print payload environment */
323 for (i = 0; uev->envp[i] != NULL; i++)
324 condlog(3, "%s", uev->envp[i]);
327 * Queue uevent and poke service pthread.
329 pthread_mutex_lock(uevq_lockp);
336 pthread_mutex_unlock(uevq_lockp);
338 pthread_mutex_lock(uevc_lockp);
339 pthread_cond_signal(uev_condp);
340 pthread_mutex_unlock(uevc_lockp);
346 pthread_mutex_lock(uevq_lockp);
347 pthread_cancel(uevq_thr);
348 pthread_mutex_unlock(uevq_lockp);
350 pthread_mutex_destroy(uevq_lockp);
351 pthread_mutex_destroy(uevc_lockp);
352 pthread_cond_destroy(uev_condp);