Merge branch 'master' of git://git.kernel.org/pub/scm/linux/storage/multipath-tools/
[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 <stdlib.h>
29 #include <stddef.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <time.h>
33 #include <sys/socket.h>
34 #include <sys/user.h>
35 #include <sys/un.h>
36 #include <linux/types.h>
37 #include <linux/netlink.h>
38 #include <pthread.h>
39 #include <sys/mman.h>
40
41 #include "memory.h"
42 #include "debug.h"
43 #include "uevent.h"
44
45 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
46
47 pthread_t uevq_thr;
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;
54
55 struct uevent * alloc_uevent (void)
56 {
57         return (struct uevent *)MALLOC(sizeof(struct uevent));
58 }
59
60 void
61 service_uevq(void)
62 {
63         int empty;
64         struct uevent *uev;
65
66         do {
67                 pthread_mutex_lock(uevq_lockp);
68                 empty = (uevqhp == NULL);
69                 if (!empty) {
70                         uev = uevqhp;
71                         uevqhp = uev->next;
72                         if (uevqtp == uev)
73                                 uevqtp = uev->next;
74                         pthread_mutex_unlock(uevq_lockp);
75
76                         if (my_uev_trigger && my_uev_trigger(uev,
77                                                         my_trigger_data))
78                                 condlog(0, "uevent trigger error");
79
80                         FREE(uev);
81                 }
82                 else {
83                         pthread_mutex_unlock(uevq_lockp);
84                 }
85         } while (empty == 0);
86 }
87
88 /*
89  * Service the uevent queue.
90  */
91 static void *
92 uevq_thread(void * et)
93 {
94         mlockall(MCL_CURRENT | MCL_FUTURE);
95
96         while (1) {
97                 pthread_mutex_lock(uevc_lockp);
98                 pthread_cond_wait(uev_condp, uevc_lockp);
99                 pthread_mutex_unlock(uevc_lockp);
100
101                 service_uevq();
102         }
103 }
104
105 int uevent_listen(int (*uev_trigger)(struct uevent *, void * trigger_data),
106                   void * trigger_data)
107 {
108         int sock;
109         struct sockaddr_nl snl;
110         struct sockaddr_un sun;
111         socklen_t addrlen;
112         int retval;
113         int rcvbufsz = 128*1024;
114         int rcvsz = 0;
115         int rcvszsz = sizeof(rcvsz);
116         unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
117         pthread_attr_t attr;
118
119         my_uev_trigger = uev_trigger;
120         my_trigger_data = trigger_data;
121
122         /*
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
126          * often enough.
127          */
128         uevqhp = uevqtp = NULL;
129
130         pthread_mutex_init(uevq_lockp, NULL);
131         pthread_mutex_init(uevc_lockp, NULL);
132         pthread_cond_init(uev_condp, NULL);
133
134         pthread_attr_init(&attr);
135         pthread_attr_setstacksize(&attr, 64 * 1024);
136         pthread_create(&uevq_thr, &attr, uevq_thread, NULL);
137
138         /*
139          * First check whether we have a udev socket
140          */
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;
145
146         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
147         if (sock >= 0) {
148                 const int feature_on = 1;
149
150                 condlog(3, "reading events from udev socket.");
151
152                 /* the bind takes care of ensuring only one copy running */
153                 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
154                 if (retval < 0) {
155                         condlog(0, "bind failed, exit");
156                         goto exit;
157                 }
158
159                 /* enable receiving of the sender credentials */
160                 setsockopt(sock, SOL_SOCKET, SO_PASSCRED, 
161                            &feature_on, sizeof(feature_on));
162
163         } else {
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 = 0xffffffff;
169
170                 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
171                 if (sock == -1) {
172                         condlog(0, "error getting socket, exit");
173                         return 1;
174                 }
175
176                 condlog(3, "reading events from kernel.");
177
178                 /*
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.
183                  */
184                 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
185                                     sizeof(rcvbufsz));
186
187                 if (retval < 0) {
188                         condlog(0, "error setting receive buffer size for socket, exit");
189                         exit(1);
190                 }
191                 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
192                 if (retval < 0) {
193                         condlog(0, "error setting receive buffer size for socket, exit");
194                         exit(1);
195                 }
196                 condlog(3, "receive buffer size for socket is %u.", rcvsz);
197
198                 retval = bind(sock, (struct sockaddr *) &snl,
199                               sizeof(struct sockaddr_nl));
200                 if (retval < 0) {
201                         condlog(0, "bind failed, exit");
202                         goto exit;
203                 }
204         }
205
206         while (1) {
207                 static char buff[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
208                 int i;
209                 char *pos;
210                 size_t bufpos;
211                 ssize_t buflen;
212                 struct uevent *uev;
213                 char *buffer;
214
215                 buflen = recv(sock, &buff, sizeof(buff), 0);
216                 if (buflen <  0) {
217                         condlog(0, "error receiving message");
218                         continue;
219                 }
220
221                 if ((size_t)buflen > sizeof(buff)-1)
222                         buflen = sizeof(buff)-1;
223
224                 uev = alloc_uevent();
225
226                 if (!uev) {
227                         condlog(1, "lost uevent, oom");
228                         continue;
229                 }
230
231                 /*
232                  * Copy the shared receive buffer contents to buffer private
233                  * to this uevent so we can immediately reuse the shared buffer.
234                  */
235                 memcpy(uev->buffer, buff, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
236                 buffer = uev->buffer;
237                 buffer[buflen] = '\0';
238
239                 /* save start of payload */
240                 bufpos = strlen(buffer) + 1;
241
242                 /* action string */
243                 uev->action = buffer;
244                 pos = strchr(buffer, '@');
245                 if (!pos)
246                         continue;
247                 pos[0] = '\0';
248
249                 /* sysfs path */
250                 uev->devpath = &pos[1];
251
252                 /* hotplug events have the environment attached - reconstruct envp[] */
253                 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
254                         int keylen;
255                         char *key;
256
257                         key = &buffer[bufpos];
258                         keylen = strlen(key);
259                         uev->envp[i] = key;
260                         bufpos += keylen + 1;
261                 }
262                 uev->envp[i] = NULL;
263
264                 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
265
266                 /* print payload environment */
267                 for (i = 0; uev->envp[i] != NULL; i++)
268                         condlog(3, "%s", uev->envp[i]);
269
270                 /*
271                  * Queue uevent and poke service pthread.
272                  */
273                 pthread_mutex_lock(uevq_lockp);
274                 if (uevqtp)
275                         uevqtp->next = uev;
276                 else
277                         uevqhp = uev;
278                 uevqtp = uev;
279                 uev->next = NULL;
280                 pthread_mutex_unlock(uevq_lockp);
281
282                 pthread_mutex_lock(uevc_lockp);
283                 pthread_cond_signal(uev_condp);
284                 pthread_mutex_unlock(uevc_lockp);
285         }
286
287 exit:
288         close(sock);
289
290         pthread_mutex_lock(uevq_lockp);
291         pthread_cancel(uevq_thr);
292         pthread_mutex_unlock(uevq_lockp);
293
294         pthread_mutex_destroy(uevq_lockp);
295         pthread_mutex_destroy(uevc_lockp);
296         pthread_cond_destroy(uev_condp);
297
298         return 1;
299 }