multipath-tools: Fix uevent handling code
[platform/upstream/multipath-tools.git] / libmultipath / uevent.c
1 /*
2  * uevent.c - trigger upon netlink uevents from the kernel
3  *
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:
6  *
7  *        gcc -I /lib/modules/`uname -r`/build/include -o uevent_listen uevent_listen.c
8  *
9  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
10  *
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.
14  *
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.
19  *
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.
23  *
24  */
25
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <time.h>
34 #include <sys/socket.h>
35 #include <sys/user.h>
36 #include <sys/un.h>
37 #include <linux/types.h>
38 #include <linux/netlink.h>
39 #include <pthread.h>
40 #include <sys/mman.h>
41
42 #include "memory.h"
43 #include "debug.h"
44 #include "uevent.h"
45
46 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
47
48 pthread_t uevq_thr;
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;
55
56 static struct uevent * alloc_uevent (void)
57 {
58         return (struct uevent *)MALLOC(sizeof(struct uevent));
59 }
60
61 void
62 service_uevq(void)
63 {
64         int empty;
65         struct uevent *uev;
66
67         do {
68                 pthread_mutex_lock(uevq_lockp);
69                 empty = (uevqhp == NULL);
70                 if (!empty) {
71                         uev = uevqhp;
72                         uevqhp = uev->next;
73                         if (uevqtp == uev)
74                                 uevqtp = uev->next;
75                         pthread_mutex_unlock(uevq_lockp);
76
77                         if (my_uev_trigger && my_uev_trigger(uev,
78                                                         my_trigger_data))
79                                 condlog(0, "uevent trigger error");
80
81                         FREE(uev);
82                 }
83                 else {
84                         pthread_mutex_unlock(uevq_lockp);
85                 }
86         } while (empty == 0);
87 }
88
89 /*
90  * Service the uevent queue.
91  */
92 static void *
93 uevq_thread(void * et)
94 {
95         mlockall(MCL_CURRENT | MCL_FUTURE);
96
97         while (1) {
98                 pthread_mutex_lock(uevc_lockp);
99                 pthread_cond_wait(uev_condp, uevc_lockp);
100                 pthread_mutex_unlock(uevc_lockp);
101
102                 service_uevq();
103         }
104 }
105
106 int uevent_listen(int (*uev_trigger)(struct uevent *, void * trigger_data),
107                   void * trigger_data)
108 {
109         int sock;
110         struct sockaddr_nl snl;
111         struct sockaddr_un sun;
112         socklen_t addrlen;
113         int retval;
114         int rcvbufsz = 128*1024;
115         int rcvsz = 0;
116         int rcvszsz = sizeof(rcvsz);
117         unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
118         pthread_attr_t attr;
119         const int feature_on = 1;
120
121         my_uev_trigger = uev_trigger;
122         my_trigger_data = trigger_data;
123
124         /*
125          * Queue uevents for service by dedicated thread so that the uevent
126          * listening thread does not block on multipathd locks (vecs->lock)
127          * thereby not getting to empty the socket's receive buffer queue
128          * often enough.
129          */
130         uevqhp = uevqtp = NULL;
131
132         pthread_mutex_init(uevq_lockp, NULL);
133         pthread_mutex_init(uevc_lockp, NULL);
134         pthread_cond_init(uev_condp, NULL);
135
136         pthread_attr_init(&attr);
137         pthread_attr_setstacksize(&attr, 64 * 1024);
138         pthread_create(&uevq_thr, &attr, uevq_thread, NULL);
139
140         /*
141          * First check whether we have a udev socket
142          */
143         memset(&sun, 0x00, sizeof(struct sockaddr_un));
144         sun.sun_family = AF_LOCAL;
145         strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
146         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
147
148         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
149         if (sock >= 0) {
150
151                 condlog(3, "reading events from udev socket.");
152
153                 /* the bind takes care of ensuring only one copy running */
154                 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
155                 if (retval < 0) {
156                         condlog(0, "bind failed, exit");
157                         goto exit;
158                 }
159
160                 /* enable receiving of the sender credentials */
161                 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
162                            &feature_on, sizeof(feature_on));
163
164         } else {
165                 /* Fallback to read kernel netlink events */
166                 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
167                 snl.nl_family = AF_NETLINK;
168                 snl.nl_pid = getpid();
169                 snl.nl_groups = 0x01;
170
171                 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
172                 if (sock == -1) {
173                         condlog(0, "error getting socket, exit");
174                         return 1;
175                 }
176
177                 condlog(3, "reading events from kernel.");
178
179                 /*
180                  * try to avoid dropping uevents, even so, this is not a guarantee,
181                  * but it does help to change the netlink uevent socket's
182                  * receive buffer threshold from the default value of 106,496 to
183                  * the maximum value of 262,142.
184                  */
185                 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
186                                     sizeof(rcvbufsz));
187
188                 if (retval < 0) {
189                         condlog(0, "error setting receive buffer size for socket, exit");
190                         exit(1);
191                 }
192                 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
193                 if (retval < 0) {
194                         condlog(0, "error setting receive buffer size for socket, exit");
195                         exit(1);
196                 }
197                 condlog(3, "receive buffer size for socket is %u.", rcvsz);
198
199                 /* enable receiving of the sender credentials */
200                 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
201                            &feature_on, sizeof(feature_on));
202
203                 retval = bind(sock, (struct sockaddr *) &snl,
204                               sizeof(struct sockaddr_nl));
205                 if (retval < 0) {
206                         condlog(0, "bind failed, exit");
207                         goto exit;
208                 }
209         }
210
211         while (1) {
212                 int i;
213                 char *pos;
214                 size_t bufpos;
215                 ssize_t buflen;
216                 struct uevent *uev;
217                 char *buffer;
218                 struct msghdr smsg;
219                 struct iovec iov;
220                 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
221                 struct cmsghdr *cmsg;
222                 struct ucred *cred;
223                 static char buf[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
224
225                 memset(buf, 0x00, sizeof(buf));
226                 iov.iov_base = &buf;
227                 iov.iov_len = sizeof(buf);
228                 memset (&smsg, 0x00, sizeof(struct msghdr));
229                 smsg.msg_iov = &iov;
230                 smsg.msg_iovlen = 1;
231                 smsg.msg_control = cred_msg;
232                 smsg.msg_controllen = sizeof(cred_msg);
233
234                 buflen = recvmsg(sock, &smsg, 0);
235                 if (buflen < 0) {
236                         if (errno != EINTR)
237                                 condlog(0, "error receiving message");
238                         continue;
239                 }
240
241                 cmsg = CMSG_FIRSTHDR(&smsg);
242                 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
243                         condlog(3, "no sender credentials received, message ignored");
244                         continue;
245                 }
246
247                 cred = (struct ucred *)CMSG_DATA(cmsg);
248                 if (cred->uid != 0) {
249                         condlog(3, "sender uid=%d, message ignored", cred->uid);
250                         continue;
251                 }
252
253                 /* skip header */
254                 bufpos = strlen(buf) + 1;
255                 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
256                         condlog(3, "invalid message length");
257                         continue;
258                 }
259
260                 /* check message header */
261                 if (strstr(buf, "@/") == NULL) {
262                         condlog(3, "unrecognized message header");
263                         continue;
264                 }
265
266                 uev = alloc_uevent();
267
268                 if (!uev) {
269                         condlog(1, "lost uevent, oom");
270                         continue;
271                 }
272
273                 if ((size_t)buflen > sizeof(buf)-1)
274                         buflen = sizeof(buf)-1;
275
276                 /*
277                  * Copy the shared receive buffer contents to buffer private
278                  * to this uevent so we can immediately reuse the shared buffer.
279                  */
280                 memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
281                 buffer = uev->buffer;
282                 buffer[buflen] = '\0';
283
284                 /* save start of payload */
285                 bufpos = strlen(buffer) + 1;
286
287                 /* action string */
288                 uev->action = buffer;
289                 pos = strchr(buffer, '@');
290                 if (!pos) {
291                         condlog(3, "bad action string '%s'", buffer);
292                         continue;
293                 }
294                 pos[0] = '\0';
295
296                 /* sysfs path */
297                 uev->devpath = &pos[1];
298
299                 /* hotplug events have the environment attached - reconstruct envp[] */
300                 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
301                         int keylen;
302                         char *key;
303
304                         key = &buffer[bufpos];
305                         keylen = strlen(key);
306                         uev->envp[i] = key;
307                         bufpos += keylen + 1;
308                 }
309                 uev->envp[i] = NULL;
310
311                 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
312
313                 /* print payload environment */
314                 for (i = 0; uev->envp[i] != NULL; i++)
315                         condlog(3, "%s", uev->envp[i]);
316
317                 /*
318                  * Queue uevent and poke service pthread.
319                  */
320                 pthread_mutex_lock(uevq_lockp);
321                 if (uevqtp)
322                         uevqtp->next = uev;
323                 else
324                         uevqhp = uev;
325                 uevqtp = uev;
326                 uev->next = NULL;
327                 pthread_mutex_unlock(uevq_lockp);
328
329                 pthread_mutex_lock(uevc_lockp);
330                 pthread_cond_signal(uev_condp);
331                 pthread_mutex_unlock(uevc_lockp);
332         }
333
334 exit:
335         close(sock);
336
337         pthread_mutex_lock(uevq_lockp);
338         pthread_cancel(uevq_thr);
339         pthread_mutex_unlock(uevq_lockp);
340
341         pthread_mutex_destroy(uevq_lockp);
342         pthread_mutex_destroy(uevc_lockp);
343         pthread_cond_destroy(uev_condp);
344
345         return 1;
346 }