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.
33 #include <sys/socket.h>
36 #include <linux/types.h>
37 #include <linux/netlink.h>
45 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
48 struct uevent *uevqhp, *uevqtp;
49 pthread_mutex_t uevq_lock, *uevq_lockp = &uevq_lock;
50 pthread_mutex_t uevc_lock, *uevc_lockp = &uevc_lock;
51 pthread_cond_t uev_cond, *uev_condp = &uev_cond;
52 uev_trigger *my_uev_trigger;
53 void * my_trigger_data;
55 struct uevent * alloc_uevent (void)
57 return (struct uevent *)MALLOC(sizeof(struct uevent));
67 pthread_mutex_lock(uevq_lockp);
68 empty = (uevqhp == NULL);
74 pthread_mutex_unlock(uevq_lockp);
76 if (my_uev_trigger && my_uev_trigger(uev,
78 condlog(0, "uevent trigger error");
83 pthread_mutex_unlock(uevq_lockp);
89 * Service the uevent queue.
92 uevq_thread(void * et)
94 mlockall(MCL_CURRENT | MCL_FUTURE);
97 pthread_mutex_lock(uevc_lockp);
98 pthread_cond_wait(uev_condp, uevc_lockp);
99 pthread_mutex_unlock(uevc_lockp);
105 int uevent_listen(int (*uev_trigger)(struct uevent *, void * trigger_data),
109 struct sockaddr_nl snl;
110 struct sockaddr_un sun;
113 int rcvbufsz = 128*1024;
115 int rcvszsz = sizeof(rcvsz);
116 unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
119 my_uev_trigger = uev_trigger;
120 my_trigger_data = trigger_data;
123 * Queue uevents for service by dedicated thread so that the uevent
124 * listening thread does not block on multipathd locks (vecs->lock)
125 * thereby not getting to empty the socket's receive buffer queue
128 uevqhp = uevqtp = NULL;
130 pthread_mutex_init(uevq_lockp, NULL);
131 pthread_mutex_init(uevc_lockp, NULL);
132 pthread_cond_init(uev_condp, NULL);
134 pthread_attr_init(&attr);
135 pthread_attr_setstacksize(&attr, 64 * 1024);
136 pthread_create(&uevq_thr, &attr, uevq_thread, NULL);
139 * First check whether we have a udev socket
141 memset(&sun, 0x00, sizeof(struct sockaddr_un));
142 sun.sun_family = AF_LOCAL;
143 strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
144 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
146 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
148 const int feature_on = 1;
150 condlog(3, "reading events from udev socket.");
152 /* the bind takes care of ensuring only one copy running */
153 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
155 condlog(0, "bind failed, exit");
159 /* enable receiving of the sender credentials */
160 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
161 &feature_on, sizeof(feature_on));
164 /* Fallback to read kernel netlink events */
165 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
166 snl.nl_family = AF_NETLINK;
167 snl.nl_pid = getpid();
168 snl.nl_groups = 0x01;
170 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
172 condlog(0, "error getting socket, exit");
176 condlog(3, "reading events from kernel.");
179 * try to avoid dropping uevents, even so, this is not a guarantee,
180 * but it does help to change the netlink uevent socket's
181 * receive buffer threshold from the default value of 106,496 to
182 * the maximum value of 262,142.
184 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
188 condlog(0, "error setting receive buffer size for socket, exit");
191 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
193 condlog(0, "error setting receive buffer size for socket, exit");
196 condlog(3, "receive buffer size for socket is %u.", rcvsz);
198 retval = bind(sock, (struct sockaddr *) &snl,
199 sizeof(struct sockaddr_nl));
201 condlog(0, "bind failed, exit");
207 static char buff[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
215 buflen = recv(sock, &buff, sizeof(buff), 0);
217 condlog(0, "error receiving message");
221 if ((size_t)buflen > sizeof(buff)-1)
222 buflen = sizeof(buff)-1;
224 uev = alloc_uevent();
227 condlog(1, "lost uevent, oom");
232 * Copy the shared receive buffer contents to buffer private
233 * to this uevent so we can immediately reuse the shared buffer.
235 memcpy(uev->buffer, buff, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
236 buffer = uev->buffer;
237 buffer[buflen] = '\0';
239 /* save start of payload */
240 bufpos = strlen(buffer) + 1;
243 uev->action = buffer;
244 pos = strchr(buffer, '@');
250 uev->devpath = &pos[1];
252 /* hotplug events have the environment attached - reconstruct envp[] */
253 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
257 key = &buffer[bufpos];
258 keylen = strlen(key);
260 bufpos += keylen + 1;
264 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
266 /* print payload environment */
267 for (i = 0; uev->envp[i] != NULL; i++)
268 condlog(3, "%s", uev->envp[i]);
271 * Queue uevent and poke service pthread.
273 pthread_mutex_lock(uevq_lockp);
280 pthread_mutex_unlock(uevq_lockp);
282 pthread_mutex_lock(uevc_lockp);
283 pthread_cond_signal(uev_condp);
284 pthread_mutex_unlock(uevc_lockp);
290 pthread_mutex_lock(uevq_lockp);
291 pthread_cancel(uevq_thr);
292 pthread_mutex_unlock(uevq_lockp);
294 pthread_mutex_destroy(uevq_lockp);
295 pthread_mutex_destroy(uevc_lockp);
296 pthread_cond_destroy(uev_condp);