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.
32 #include <sys/socket.h>
34 #include <asm/types.h>
35 #include <linux/netlink.h>
43 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
46 struct uevent *uevqhp, *uevqtp;
47 pthread_mutex_t uevq_lock, *uevq_lockp = &uevq_lock;
48 pthread_mutex_t uevc_lock, *uevc_lockp = &uevc_lock;
49 pthread_cond_t uev_cond, *uev_condp = &uev_cond;
50 uev_trigger *my_uev_trigger;
51 void * my_trigger_data;
53 struct uevent * alloc_uevent (void)
55 return (struct uevent *)MALLOC(sizeof(struct uevent));
65 pthread_mutex_lock(uevq_lockp);
66 empty = (uevqhp == NULL);
72 pthread_mutex_unlock(uevq_lockp);
74 if (my_uev_trigger && my_uev_trigger(uev,
76 condlog(0, "uevent trigger error");
81 pthread_mutex_unlock(uevq_lockp);
87 * Service the uevent queue.
90 uevq_thread(void * et)
92 mlockall(MCL_CURRENT | MCL_FUTURE);
95 pthread_mutex_lock(uevc_lockp);
96 pthread_cond_wait(uev_condp, uevc_lockp);
97 pthread_mutex_unlock(uevc_lockp);
103 int uevent_listen(int (*uev_trigger)(struct uevent *, void * trigger_data),
107 struct sockaddr_nl snl;
109 int rcvbufsz = 128*1024;
111 int rcvszsz = sizeof(rcvsz);
112 unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
115 my_uev_trigger = uev_trigger;
116 my_trigger_data = trigger_data;
119 * Queue uevents for service by dedicated thread so that the uevent
120 * listening thread does not block on multipathd locks (vecs->lock)
121 * thereby not getting to empty the socket's receive buffer queue
124 uevqhp = uevqtp = NULL;
126 pthread_mutex_init(uevq_lockp, NULL);
127 pthread_mutex_init(uevc_lockp, NULL);
128 pthread_cond_init(uev_condp, NULL);
130 pthread_attr_init(&attr);
131 pthread_attr_setstacksize(&attr, 64 * 1024);
132 pthread_create(&uevq_thr, &attr, uevq_thread, NULL);
134 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
135 snl.nl_family = AF_NETLINK;
136 snl.nl_pid = getpid();
137 snl.nl_groups = 0xffffffff;
139 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
141 condlog(0, "error getting socket, exit");
146 * try to avoid dropping uevents, even so, this is not a guarantee,
147 * but it does help to change the netlink uevent socket's
148 * receive buffer threshold from the default value of 106,496 to
149 * the maximum value of 262,142.
151 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
155 condlog(0, "error setting receive buffer size for socket, exit");
158 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
161 condlog(0, "error setting receive buffer size for socket, exit");
164 condlog(3, "receive buffer size for socket is %u.", rcvsz);
166 retval = bind(sock, (struct sockaddr *) &snl,
167 sizeof(struct sockaddr_nl));
169 condlog(0, "bind failed, exit");
174 static char buff[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
182 buflen = recv(sock, &buff, sizeof(buff), 0);
184 condlog(0, "error receiving message");
188 if ((size_t)buflen > sizeof(buff)-1)
189 buflen = sizeof(buff)-1;
191 uev = alloc_uevent();
194 condlog(1, "lost uevent, oom");
199 * Copy the shared receive buffer contents to buffer private
200 * to this uevent so we can immediately reuse the shared buffer.
202 memcpy(uev->buffer, buff, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
203 buffer = uev->buffer;
204 buffer[buflen] = '\0';
206 /* save start of payload */
207 bufpos = strlen(buffer) + 1;
210 uev->action = buffer;
211 pos = strchr(buffer, '@');
217 uev->devpath = &pos[1];
219 /* hotplug events have the environment attached - reconstruct envp[] */
220 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
224 key = &buffer[bufpos];
225 keylen = strlen(key);
227 bufpos += keylen + 1;
231 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
233 /* print payload environment */
234 for (i = 0; uev->envp[i] != NULL; i++)
235 condlog(3, "%s", uev->envp[i]);
238 * Queue uevent and poke service pthread.
240 pthread_mutex_lock(uevq_lockp);
247 pthread_mutex_unlock(uevq_lockp);
249 pthread_mutex_lock(uevc_lockp);
250 pthread_cond_signal(uev_condp);
251 pthread_mutex_unlock(uevc_lockp);
257 pthread_mutex_lock(uevq_lockp);
258 pthread_cancel(uevq_thr);
259 pthread_mutex_unlock(uevq_lockp);
261 pthread_mutex_destroy(uevq_lockp);
262 pthread_mutex_destroy(uevc_lockp);
263 pthread_cond_destroy(uev_condp);