multipath-tools: miscellaneous code cleanups
[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         return NULL;
105 }
106
107 int uevent_listen(int (*uev_trigger)(struct uevent *, void * trigger_data),
108                   void * trigger_data)
109 {
110         int sock;
111         struct sockaddr_nl snl;
112         struct sockaddr_un sun;
113         socklen_t addrlen;
114         int retval;
115         int rcvbufsz = 128*1024;
116         int rcvsz = 0;
117         int rcvszsz = sizeof(rcvsz);
118         unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
119         pthread_attr_t attr;
120         const int feature_on = 1;
121
122         my_uev_trigger = uev_trigger;
123         my_trigger_data = trigger_data;
124
125         /*
126          * Queue uevents for service by dedicated thread so that the uevent
127          * listening thread does not block on multipathd locks (vecs->lock)
128          * thereby not getting to empty the socket's receive buffer queue
129          * often enough.
130          */
131         uevqhp = uevqtp = NULL;
132
133         pthread_mutex_init(uevq_lockp, NULL);
134         pthread_mutex_init(uevc_lockp, NULL);
135         pthread_cond_init(uev_condp, NULL);
136
137         pthread_attr_init(&attr);
138         pthread_attr_setstacksize(&attr, 64 * 1024);
139         pthread_create(&uevq_thr, &attr, uevq_thread, NULL);
140
141         /*
142          * First check whether we have a udev socket
143          */
144         memset(&sun, 0x00, sizeof(struct sockaddr_un));
145         sun.sun_family = AF_LOCAL;
146         strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
147         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
148
149         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
150         if (sock >= 0) {
151
152                 condlog(3, "reading events from udev socket.");
153
154                 /* the bind takes care of ensuring only one copy running */
155                 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
156                 if (retval < 0) {
157                         condlog(0, "bind failed, exit");
158                         goto exit;
159                 }
160
161                 /* enable receiving of the sender credentials */
162                 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
163                            &feature_on, sizeof(feature_on));
164
165         } else {
166                 /* Fallback to read kernel netlink events */
167                 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
168                 snl.nl_family = AF_NETLINK;
169                 snl.nl_pid = getpid();
170                 snl.nl_groups = 0x01;
171
172                 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
173                 if (sock == -1) {
174                         condlog(0, "error getting socket, exit");
175                         return 1;
176                 }
177
178                 condlog(3, "reading events from kernel.");
179
180                 /*
181                  * try to avoid dropping uevents, even so, this is not a guarantee,
182                  * but it does help to change the netlink uevent socket's
183                  * receive buffer threshold from the default value of 106,496 to
184                  * the maximum value of 262,142.
185                  */
186                 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
187                                     sizeof(rcvbufsz));
188
189                 if (retval < 0) {
190                         condlog(0, "error setting receive buffer size for socket, exit");
191                         exit(1);
192                 }
193                 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
194                 if (retval < 0) {
195                         condlog(0, "error setting receive buffer size for socket, exit");
196                         exit(1);
197                 }
198                 condlog(3, "receive buffer size for socket is %u.", rcvsz);
199
200                 /* enable receiving of the sender credentials */
201                 setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
202                            &feature_on, sizeof(feature_on));
203
204                 retval = bind(sock, (struct sockaddr *) &snl,
205                               sizeof(struct sockaddr_nl));
206                 if (retval < 0) {
207                         condlog(0, "bind failed, exit");
208                         goto exit;
209                 }
210         }
211
212         while (1) {
213                 int i;
214                 char *pos;
215                 size_t bufpos;
216                 ssize_t buflen;
217                 struct uevent *uev;
218                 char *buffer;
219                 struct msghdr smsg;
220                 struct iovec iov;
221                 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
222                 struct cmsghdr *cmsg;
223                 struct ucred *cred;
224                 static char buf[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
225
226                 memset(buf, 0x00, sizeof(buf));
227                 iov.iov_base = &buf;
228                 iov.iov_len = sizeof(buf);
229                 memset (&smsg, 0x00, sizeof(struct msghdr));
230                 smsg.msg_iov = &iov;
231                 smsg.msg_iovlen = 1;
232                 smsg.msg_control = cred_msg;
233                 smsg.msg_controllen = sizeof(cred_msg);
234
235                 buflen = recvmsg(sock, &smsg, 0);
236                 if (buflen < 0) {
237                         if (errno != EINTR)
238                                 condlog(0, "error receiving message");
239                         continue;
240                 }
241
242                 cmsg = CMSG_FIRSTHDR(&smsg);
243                 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
244                         condlog(3, "no sender credentials received, message ignored");
245                         continue;
246                 }
247
248                 cred = (struct ucred *)CMSG_DATA(cmsg);
249                 if (cred->uid != 0) {
250                         condlog(3, "sender uid=%d, message ignored", cred->uid);
251                         continue;
252                 }
253
254                 /* skip header */
255                 bufpos = strlen(buf) + 1;
256                 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
257                         condlog(3, "invalid message length");
258                         continue;
259                 }
260
261                 /* check message header */
262                 if (strstr(buf, "@/") == NULL) {
263                         condlog(3, "unrecognized message header");
264                         continue;
265                 }
266
267                 uev = alloc_uevent();
268
269                 if (!uev) {
270                         condlog(1, "lost uevent, oom");
271                         continue;
272                 }
273
274                 if ((size_t)buflen > sizeof(buf)-1)
275                         buflen = sizeof(buf)-1;
276
277                 /*
278                  * Copy the shared receive buffer contents to buffer private
279                  * to this uevent so we can immediately reuse the shared buffer.
280                  */
281                 memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
282                 buffer = uev->buffer;
283                 buffer[buflen] = '\0';
284
285                 /* save start of payload */
286                 bufpos = strlen(buffer) + 1;
287
288                 /* action string */
289                 uev->action = buffer;
290                 pos = strchr(buffer, '@');
291                 if (!pos) {
292                         condlog(3, "bad action string '%s'", buffer);
293                         continue;
294                 }
295                 pos[0] = '\0';
296
297                 /* sysfs path */
298                 uev->devpath = &pos[1];
299
300                 /* hotplug events have the environment attached - reconstruct envp[] */
301                 for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
302                         int keylen;
303                         char *key;
304
305                         key = &buffer[bufpos];
306                         keylen = strlen(key);
307                         uev->envp[i] = key;
308                         bufpos += keylen + 1;
309                 }
310                 uev->envp[i] = NULL;
311
312                 condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
313
314                 /* print payload environment */
315                 for (i = 0; uev->envp[i] != NULL; i++)
316                         condlog(3, "%s", uev->envp[i]);
317
318                 /*
319                  * Queue uevent and poke service pthread.
320                  */
321                 pthread_mutex_lock(uevq_lockp);
322                 if (uevqtp)
323                         uevqtp->next = uev;
324                 else
325                         uevqhp = uev;
326                 uevqtp = uev;
327                 uev->next = NULL;
328                 pthread_mutex_unlock(uevq_lockp);
329
330                 pthread_mutex_lock(uevc_lockp);
331                 pthread_cond_signal(uev_condp);
332                 pthread_mutex_unlock(uevc_lockp);
333         }
334
335 exit:
336         close(sock);
337
338         pthread_mutex_lock(uevq_lockp);
339         pthread_cancel(uevq_thr);
340         pthread_mutex_unlock(uevq_lockp);
341
342         pthread_mutex_destroy(uevq_lockp);
343         pthread_mutex_destroy(uevc_lockp);
344         pthread_cond_destroy(uev_condp);
345
346         return 1;
347 }