Imported Upstream version 0.7.7
[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, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdbool.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 <poll.h>
38 #include <linux/types.h>
39 #include <linux/netlink.h>
40 #include <pthread.h>
41 #include <sys/mman.h>
42 #include <sys/time.h>
43 #include <libudev.h>
44 #include <errno.h>
45
46 #include "memory.h"
47 #include "debug.h"
48 #include "list.h"
49 #include "uevent.h"
50 #include "vector.h"
51 #include "structs.h"
52 #include "util.h"
53 #include "config.h"
54 #include "blacklist.h"
55 #include "devmapper.h"
56
57 #define MAX_ACCUMULATION_COUNT 2048
58 #define MAX_ACCUMULATION_TIME 30*1000
59 #define MIN_BURST_SPEED 10
60
61 typedef int (uev_trigger)(struct uevent *, void * trigger_data);
62
63 LIST_HEAD(uevq);
64 pthread_mutex_t uevq_lock = PTHREAD_MUTEX_INITIALIZER;
65 pthread_mutex_t *uevq_lockp = &uevq_lock;
66 pthread_cond_t uev_cond = PTHREAD_COND_INITIALIZER;
67 pthread_cond_t *uev_condp = &uev_cond;
68 uev_trigger *my_uev_trigger;
69 void * my_trigger_data;
70 int servicing_uev;
71
72 int is_uevent_busy(void)
73 {
74         int empty;
75
76         pthread_mutex_lock(uevq_lockp);
77         empty = list_empty(&uevq);
78         pthread_mutex_unlock(uevq_lockp);
79         return (!empty || servicing_uev);
80 }
81
82 struct uevent * alloc_uevent (void)
83 {
84         struct uevent *uev = MALLOC(sizeof(struct uevent));
85
86         if (uev) {
87                 INIT_LIST_HEAD(&uev->node);
88                 INIT_LIST_HEAD(&uev->merge_node);
89         }
90
91         return uev;
92 }
93
94 void
95 uevq_cleanup(struct list_head *tmpq)
96 {
97         struct uevent *uev, *tmp;
98
99         list_for_each_entry_safe(uev, tmp, tmpq, node) {
100                 list_del_init(&uev->node);
101
102                 if (uev->udev)
103                         udev_device_unref(uev->udev);
104                 FREE(uev);
105         }
106 }
107
108 static const char* uevent_get_env_var(const struct uevent *uev,
109                                       const char *attr)
110 {
111         int i, len;
112         const char *p = NULL;
113
114         if (attr == NULL)
115                 goto invalid;
116
117         len = strlen(attr);
118         if (len == 0)
119                 goto invalid;
120
121         for (i = 0; uev->envp[i] != NULL; i++) {
122                 const char *var = uev->envp[i];
123
124                 if (strlen(var) > len &&
125                     !memcmp(var, attr, len) && var[len] == '=') {
126                         p = var + len + 1;
127                         break;
128                 }
129         }
130
131         condlog(4, "%s: %s -> '%s'", __func__, attr, p);
132         return p;
133
134 invalid:
135         condlog(2, "%s: empty variable name", __func__);
136         return NULL;
137 }
138
139 static int uevent_get_env_positive_int(const struct uevent *uev,
140                                        const char *attr)
141 {
142         const char *p = uevent_get_env_var(uev, attr);
143         char *q;
144         int ret;
145
146         if (p == NULL || *p == '\0')
147                 return -1;
148
149         ret = strtoul(p, &q, 10);
150         if (*q != '\0' || ret < 0) {
151                 condlog(2, "%s: invalid %s: '%s'", __func__, attr, p);
152                 return -1;
153         }
154         return ret;
155 }
156
157 void
158 uevent_get_wwid(struct uevent *uev)
159 {
160         char *uid_attribute;
161         const char *val;
162         struct config * conf;
163
164         conf = get_multipath_config();
165         pthread_cleanup_push(put_multipath_config, conf);
166         uid_attribute = parse_uid_attribute_by_attrs(conf->uid_attrs, uev->kernel);
167         pthread_cleanup_pop(1);
168
169         val = uevent_get_env_var(uev, uid_attribute);
170         if (val)
171                 uev->wwid = val;
172         FREE(uid_attribute);
173 }
174
175 bool
176 uevent_need_merge(void)
177 {
178         struct config * conf;
179         bool need_merge = false;
180
181         conf = get_multipath_config();
182         if (conf->uid_attrs)
183                 need_merge = true;
184         put_multipath_config(conf);
185
186         return need_merge;
187 }
188
189 bool
190 uevent_can_discard(struct uevent *uev)
191 {
192         int invalid = 0;
193         struct config * conf;
194
195         /*
196          * do not filter dm devices by devnode
197          */
198         if (!strncmp(uev->kernel, "dm-", 3))
199                 return false;
200         /*
201          * filter paths devices by devnode
202          */
203         conf = get_multipath_config();
204         pthread_cleanup_push(put_multipath_config, conf);
205         if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
206                            uev->kernel) > 0)
207                 invalid = 1;
208         pthread_cleanup_pop(1);
209
210         if (invalid)
211                 return true;
212         return false;
213 }
214
215 bool
216 uevent_can_filter(struct uevent *earlier, struct uevent *later)
217 {
218
219         /*
220          * filter earlier uvents if path has removed later. Eg:
221          * "add path1 |chang path1 |add path2 |remove path1"
222          * can filter as:
223          * "add path2 |remove path1"
224          * uevents "add path1" and "chang path1" are filtered out
225          */
226         if (!strcmp(earlier->kernel, later->kernel) &&
227                 !strcmp(later->action, "remove") &&
228                 strncmp(later->kernel, "dm-", 3)) {
229                 return true;
230         }
231
232         /*
233          * filter change uvents if add uevents exist. Eg:
234          * "change path1| add path1 |add path2"
235          * can filter as:
236          * "add path1 |add path2"
237          * uevent "chang path1" is filtered out
238          */
239         if (!strcmp(earlier->kernel, later->kernel) &&
240                 !strcmp(earlier->action, "change") &&
241                 !strcmp(later->action, "add") &&
242                 strncmp(later->kernel, "dm-", 3)) {
243                 return true;
244         }
245
246         return false;
247 }
248
249 bool
250 merge_need_stop(struct uevent *earlier, struct uevent *later)
251 {
252         /*
253          * dm uevent do not try to merge with left uevents
254          */
255         if (!strncmp(later->kernel, "dm-", 3))
256                 return true;
257
258         /*
259          * we can not make a jugement without wwid,
260          * so it is sensible to stop merging
261          */
262         if (!earlier->wwid || !later->wwid)
263                 return true;
264         /*
265          * uevents merging stopped
266          * when we meet an opposite action uevent from the same LUN to AVOID
267          * "add path1 |remove path1 |add path2 |remove path2 |add path3"
268          * to merge as "remove path1, path2" and "add path1, path2, path3"
269          * OR
270          * "remove path1 |add path1 |remove path2 |add path2 |remove path3"
271          * to merge as "add path1, path2" and "remove path1, path2, path3"
272          * SO
273          * when we meet a non-change uevent from the same LUN
274          * with the same wwid and different action
275          * it would be better to stop merging.
276          */
277         if (!strcmp(earlier->wwid, later->wwid) &&
278             strcmp(earlier->action, later->action) &&
279             strcmp(earlier->action, "change") &&
280             strcmp(later->action, "change"))
281                 return true;
282
283         return false;
284 }
285
286 bool
287 uevent_can_merge(struct uevent *earlier, struct uevent *later)
288 {
289         /* merge paths uevents
290          * whose wwids exsit and are same
291          * and actions are same,
292          * and actions are addition or deletion
293          */
294         if (earlier->wwid && later->wwid &&
295             !strcmp(earlier->wwid, later->wwid) &&
296             !strcmp(earlier->action, later->action) &&
297             strncmp(earlier->action, "change", 6) &&
298             strncmp(earlier->kernel, "dm-", 3)) {
299                 return true;
300         }
301
302         return false;
303 }
304
305 void
306 uevent_prepare(struct list_head *tmpq)
307 {
308         struct uevent *uev, *tmp;
309
310         list_for_each_entry_reverse_safe(uev, tmp, tmpq, node) {
311                 if (uevent_can_discard(uev)) {
312                         list_del_init(&uev->node);
313                         if (uev->udev)
314                                 udev_device_unref(uev->udev);
315                         FREE(uev);
316                         continue;
317                 }
318
319                 if (strncmp(uev->kernel, "dm-", 3) &&
320                     uevent_need_merge())
321                         uevent_get_wwid(uev);
322         }
323 }
324
325 void
326 uevent_filter(struct uevent *later, struct list_head *tmpq)
327 {
328         struct uevent *earlier, *tmp;
329
330         list_for_some_entry_reverse_safe(earlier, tmp, &later->node, tmpq, node) {
331                 /*
332                  * filter unnessary earlier uevents
333                  * by the later uevent
334                  */
335                 if (uevent_can_filter(earlier, later)) {
336                         condlog(3, "uevent: %s-%s has filtered by uevent: %s-%s",
337                                 earlier->kernel, earlier->action,
338                                 later->kernel, later->action);
339
340                         list_del_init(&earlier->node);
341                         if (earlier->udev)
342                                 udev_device_unref(earlier->udev);
343                         FREE(earlier);
344                 }
345         }
346 }
347
348 void
349 uevent_merge(struct uevent *later, struct list_head *tmpq)
350 {
351         struct uevent *earlier, *tmp;
352
353         list_for_some_entry_reverse_safe(earlier, tmp, &later->node, tmpq, node) {
354                 if (merge_need_stop(earlier, later))
355                         break;
356                 /*
357                  * merge earlier uevents to the later uevent
358                  */
359                 if (uevent_can_merge(earlier, later)) {
360                         condlog(3, "merged uevent: %s-%s-%s with uevent: %s-%s-%s",
361                                 earlier->action, earlier->kernel, earlier->wwid,
362                                 later->action, later->kernel, later->wwid);
363
364                         list_move(&earlier->node, &later->merge_node);
365                 }
366         }
367 }
368
369 void
370 merge_uevq(struct list_head *tmpq)
371 {
372         struct uevent *later;
373
374         uevent_prepare(tmpq);
375         list_for_each_entry_reverse(later, tmpq, node) {
376                 uevent_filter(later, tmpq);
377                 if(uevent_need_merge())
378                         uevent_merge(later, tmpq);
379         }
380 }
381
382 void
383 service_uevq(struct list_head *tmpq)
384 {
385         struct uevent *uev, *tmp;
386
387         list_for_each_entry_safe(uev, tmp, tmpq, node) {
388                 list_del_init(&uev->node);
389
390                 if (my_uev_trigger && my_uev_trigger(uev, my_trigger_data))
391                         condlog(0, "uevent trigger error");
392
393                 uevq_cleanup(&uev->merge_node);
394
395                 if (uev->udev)
396                         udev_device_unref(uev->udev);
397                 FREE(uev);
398         }
399 }
400
401 static void uevent_cleanup(void *arg)
402 {
403         struct udev *udev = arg;
404
405         condlog(3, "Releasing uevent_listen() resources");
406         udev_unref(udev);
407 }
408
409 static void monitor_cleanup(void *arg)
410 {
411         struct udev_monitor *monitor = arg;
412
413         condlog(3, "Releasing uevent_monitor() resources");
414         udev_monitor_unref(monitor);
415 }
416
417 /*
418  * Service the uevent queue.
419  */
420 int uevent_dispatch(int (*uev_trigger)(struct uevent *, void * trigger_data),
421                     void * trigger_data)
422 {
423         my_uev_trigger = uev_trigger;
424         my_trigger_data = trigger_data;
425
426         mlockall(MCL_CURRENT | MCL_FUTURE);
427
428         while (1) {
429                 LIST_HEAD(uevq_tmp);
430
431                 pthread_mutex_lock(uevq_lockp);
432                 servicing_uev = 0;
433                 /*
434                  * Condition signals are unreliable,
435                  * so make sure we only wait if we have to.
436                  */
437                 if (list_empty(&uevq)) {
438                         pthread_cond_wait(uev_condp, uevq_lockp);
439                 }
440                 servicing_uev = 1;
441                 list_splice_init(&uevq, &uevq_tmp);
442                 pthread_mutex_unlock(uevq_lockp);
443                 if (!my_uev_trigger)
444                         break;
445                 merge_uevq(&uevq_tmp);
446                 service_uevq(&uevq_tmp);
447         }
448         condlog(3, "Terminating uev service queue");
449         uevq_cleanup(&uevq);
450         return 0;
451 }
452
453 struct uevent *uevent_from_buffer(char *buf, ssize_t buflen)
454 {
455         struct uevent *uev;
456         char *buffer;
457         size_t bufpos;
458         int i;
459         char *pos;
460
461         uev = alloc_uevent();
462         if (!uev) {
463                 condlog(1, "lost uevent, oom");
464                 return NULL;
465         }
466
467         if ((size_t)buflen > sizeof(buf)-1)
468                 buflen = sizeof(buf)-1;
469
470         /*
471          * Copy the shared receive buffer contents to buffer private
472          * to this uevent so we can immediately reuse the shared buffer.
473          */
474         memcpy(uev->buffer, buf, HOTPLUG_BUFFER_SIZE + OBJECT_SIZE);
475         buffer = uev->buffer;
476         buffer[buflen] = '\0';
477
478         /* save start of payload */
479         bufpos = strlen(buffer) + 1;
480
481         /* action string */
482         uev->action = buffer;
483         pos = strchr(buffer, '@');
484         if (!pos) {
485                 condlog(3, "bad action string '%s'", buffer);
486                 FREE(uev);
487                 return NULL;
488         }
489         pos[0] = '\0';
490
491         /* sysfs path */
492         uev->devpath = &pos[1];
493
494         /* hotplug events have the environment attached - reconstruct envp[] */
495         for (i = 0; (bufpos < (size_t)buflen) && (i < HOTPLUG_NUM_ENVP-1); i++) {
496                 int keylen;
497                 char *key;
498
499                 key = &buffer[bufpos];
500                 keylen = strlen(key);
501                 uev->envp[i] = key;
502                 /* Filter out sequence number */
503                 if (strncmp(key, "SEQNUM=", 7) == 0) {
504                         char *eptr;
505
506                         uev->seqnum = strtoul(key + 7, &eptr, 10);
507                         if (eptr == key + 7)
508                                 uev->seqnum = -1;
509                 }
510                 bufpos += keylen + 1;
511         }
512         uev->envp[i] = NULL;
513
514         condlog(3, "uevent %ld '%s' from '%s'", uev->seqnum,
515                 uev->action, uev->devpath);
516         uev->kernel = strrchr(uev->devpath, '/');
517         if (uev->kernel)
518                 uev->kernel++;
519
520         /* print payload environment */
521         for (i = 0; uev->envp[i] != NULL; i++)
522                 condlog(5, "%s", uev->envp[i]);
523
524         return uev;
525 }
526
527 int failback_listen(void)
528 {
529         int sock;
530         struct sockaddr_nl snl;
531         struct sockaddr_un sun;
532         socklen_t addrlen;
533         int retval;
534         int rcvbufsz = 128*1024;
535         int rcvsz = 0;
536         int rcvszsz = sizeof(rcvsz);
537         unsigned int *prcvszsz = (unsigned int *)&rcvszsz;
538         const int feature_on = 1;
539         /*
540          * First check whether we have a udev socket
541          */
542         memset(&sun, 0x00, sizeof(struct sockaddr_un));
543         sun.sun_family = AF_LOCAL;
544         strcpy(&sun.sun_path[1], "/org/kernel/dm/multipath_event");
545         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(sun.sun_path+1) + 1;
546
547         sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
548         if (sock >= 0) {
549
550                 condlog(3, "reading events from udev socket.");
551
552                 /* the bind takes care of ensuring only one copy running */
553                 retval = bind(sock, (struct sockaddr *) &sun, addrlen);
554                 if (retval < 0) {
555                         condlog(0, "bind failed, exit");
556                         goto exit;
557                 }
558
559                 /* enable receiving of the sender credentials */
560                 retval = setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
561                                     &feature_on, sizeof(feature_on));
562                 if (retval < 0) {
563                         condlog(0, "failed to enable credential passing, exit");
564                         goto exit;
565                 }
566
567         } else {
568                 /* Fallback to read kernel netlink events */
569                 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
570                 snl.nl_family = AF_NETLINK;
571                 snl.nl_pid = getpid();
572                 snl.nl_groups = 0x01;
573
574                 sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
575                 if (sock == -1) {
576                         condlog(0, "error getting socket, exit");
577                         return 1;
578                 }
579
580                 condlog(3, "reading events from kernel.");
581
582                 /*
583                  * try to avoid dropping uevents, even so, this is not a guarantee,
584                  * but it does help to change the netlink uevent socket's
585                  * receive buffer threshold from the default value of 106,496 to
586                  * the maximum value of 262,142.
587                  */
588                 retval = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbufsz,
589                                     sizeof(rcvbufsz));
590
591                 if (retval < 0) {
592                         condlog(0, "error setting receive buffer size for socket, exit");
593                         exit(1);
594                 }
595                 retval = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvsz, prcvszsz);
596                 if (retval < 0) {
597                         condlog(0, "error setting receive buffer size for socket, exit");
598                         exit(1);
599                 }
600                 condlog(3, "receive buffer size for socket is %u.", rcvsz);
601
602                 /* enable receiving of the sender credentials */
603                 if (setsockopt(sock, SOL_SOCKET, SO_PASSCRED,
604                                &feature_on, sizeof(feature_on)) < 0) {
605                         condlog(0, "error on enabling credential passing for socket");
606                         exit(1);
607                 }
608
609                 retval = bind(sock, (struct sockaddr *) &snl,
610                               sizeof(struct sockaddr_nl));
611                 if (retval < 0) {
612                         condlog(0, "bind failed, exit");
613                         goto exit;
614                 }
615         }
616
617         while (1) {
618                 size_t bufpos;
619                 ssize_t buflen;
620                 struct uevent *uev;
621                 struct msghdr smsg;
622                 struct iovec iov;
623                 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
624                 struct cmsghdr *cmsg;
625                 struct ucred *cred;
626                 static char buf[HOTPLUG_BUFFER_SIZE + OBJECT_SIZE];
627
628                 memset(buf, 0x00, sizeof(buf));
629                 iov.iov_base = &buf;
630                 iov.iov_len = sizeof(buf);
631                 memset (&smsg, 0x00, sizeof(struct msghdr));
632                 smsg.msg_iov = &iov;
633                 smsg.msg_iovlen = 1;
634                 smsg.msg_control = cred_msg;
635                 smsg.msg_controllen = sizeof(cred_msg);
636
637                 buflen = recvmsg(sock, &smsg, 0);
638                 if (buflen < 0) {
639                         if (errno != EINTR)
640                                 condlog(0, "error receiving message, errno %d", errno);
641                         continue;
642                 }
643
644                 cmsg = CMSG_FIRSTHDR(&smsg);
645                 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
646                         condlog(3, "no sender credentials received, message ignored");
647                         continue;
648                 }
649
650                 cred = (struct ucred *)CMSG_DATA(cmsg);
651                 if (cred->uid != 0) {
652                         condlog(3, "sender uid=%d, message ignored", cred->uid);
653                         continue;
654                 }
655
656                 /* skip header */
657                 bufpos = strlen(buf) + 1;
658                 if (bufpos < sizeof("a@/d") || bufpos >= sizeof(buf)) {
659                         condlog(3, "invalid message length");
660                         continue;
661                 }
662
663                 /* check message header */
664                 if (strstr(buf, "@/") == NULL) {
665                         condlog(3, "unrecognized message header");
666                         continue;
667                 }
668                 if ((size_t)buflen > sizeof(buf)-1) {
669                         condlog(2, "buffer overflow for received uevent");
670                         buflen = sizeof(buf)-1;
671                 }
672
673                 uev = uevent_from_buffer(buf, buflen);
674                 if (!uev)
675                         continue;
676                 /*
677                  * Queue uevent and poke service pthread.
678                  */
679                 pthread_mutex_lock(uevq_lockp);
680                 list_add_tail(&uev->node, &uevq);
681                 pthread_cond_signal(uev_condp);
682                 pthread_mutex_unlock(uevq_lockp);
683         }
684
685 exit:
686         close(sock);
687         return 1;
688 }
689
690 struct uevent *uevent_from_udev_device(struct udev_device *dev)
691 {
692         struct uevent *uev;
693         int i = 0;
694         char *pos, *end;
695         struct udev_list_entry *list_entry;
696
697         uev = alloc_uevent();
698         if (!uev) {
699                 udev_device_unref(dev);
700                 condlog(1, "lost uevent, oom");
701                 return NULL;
702         }
703         pos = uev->buffer;
704         end = pos + HOTPLUG_BUFFER_SIZE + OBJECT_SIZE - 1;
705         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev)) {
706                 const char *name, *value;
707                 int bytes;
708
709                 name = udev_list_entry_get_name(list_entry);
710                 if (!name)
711                         name = "(null)";
712                 value = udev_list_entry_get_value(list_entry);
713                 if (!value)
714                         value = "(null)";
715                 bytes = snprintf(pos, end - pos, "%s=%s", name, value);
716                 if (pos + bytes >= end) {
717                         condlog(2, "buffer overflow for uevent");
718                         break;
719                 }
720                 uev->envp[i] = pos;
721                 pos += bytes;
722                 *pos = '\0';
723                 pos++;
724                 if (strcmp(name, "DEVPATH") == 0)
725                         uev->devpath = uev->envp[i] + 8;
726                 if (strcmp(name, "ACTION") == 0)
727                         uev->action = uev->envp[i] + 7;
728                 i++;
729                 if (i == HOTPLUG_NUM_ENVP - 1)
730                         break;
731         }
732         uev->udev = dev;
733         uev->envp[i] = NULL;
734
735         condlog(3, "uevent '%s' from '%s'", uev->action, uev->devpath);
736         uev->kernel = strrchr(uev->devpath, '/');
737         if (uev->kernel)
738                 uev->kernel++;
739
740         /* print payload environment */
741         for (i = 0; uev->envp[i] != NULL; i++)
742                 condlog(5, "%s", uev->envp[i]);
743         return uev;
744 }
745
746 bool uevent_burst(struct timeval *start_time, int events)
747 {
748         struct timeval diff_time, end_time;
749         unsigned long speed;
750         unsigned long eclipse_ms;
751
752         if(events > MAX_ACCUMULATION_COUNT) {
753                 condlog(2, "burst got %u uevents, too much uevents, stopped", events);
754                 return false;
755         }
756
757         gettimeofday(&end_time, NULL);
758         timersub(&end_time, start_time, &diff_time);
759
760         eclipse_ms = diff_time.tv_sec * 1000 + diff_time.tv_usec / 1000;
761
762         if (eclipse_ms == 0)
763                 return true;
764
765         if (eclipse_ms > MAX_ACCUMULATION_TIME) {
766                 condlog(2, "burst continued %lu ms, too long time, stopped", eclipse_ms);
767                 return false;
768         }
769
770         speed = (events * 1000) / eclipse_ms;
771         if (speed > MIN_BURST_SPEED)
772                 return true;
773
774         return false;
775 }
776
777 int uevent_listen(struct udev *udev)
778 {
779         int err = 2;
780         struct udev_monitor *monitor = NULL;
781         int fd, socket_flags, events;
782         struct timeval start_time;
783         int need_failback = 1;
784         int timeout = 30;
785         LIST_HEAD(uevlisten_tmp);
786
787         /*
788          * Queue uevents for service by dedicated thread so that the uevent
789          * listening thread does not block on multipathd locks (vecs->lock)
790          * thereby not getting to empty the socket's receive buffer queue
791          * often enough.
792          */
793         if (!udev) {
794                 condlog(1, "no udev context");
795                 return 1;
796         }
797         udev_ref(udev);
798         pthread_cleanup_push(uevent_cleanup, udev);
799
800         monitor = udev_monitor_new_from_netlink(udev, "udev");
801         if (!monitor) {
802                 condlog(2, "failed to create udev monitor");
803                 goto out;
804         }
805         pthread_cleanup_push(monitor_cleanup, monitor);
806 #ifdef LIBUDEV_API_RECVBUF
807         if (udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024))
808                 condlog(2, "failed to increase buffer size");
809 #endif
810         fd = udev_monitor_get_fd(monitor);
811         if (fd < 0) {
812                 condlog(2, "failed to get monitor fd");
813                 goto out;
814         }
815         socket_flags = fcntl(fd, F_GETFL);
816         if (socket_flags < 0) {
817                 condlog(2, "failed to get monitor socket flags : %s",
818                         strerror(errno));
819                 goto out;
820         }
821         if (fcntl(fd, F_SETFL, socket_flags & ~O_NONBLOCK) < 0) {
822                 condlog(2, "failed to set monitor socket flags : %s",
823                         strerror(errno));
824                 goto out;
825         }
826         err = udev_monitor_filter_add_match_subsystem_devtype(monitor, "block",
827                                                               "disk");
828         if (err)
829                 condlog(2, "failed to create filter : %s", strerror(-err));
830         err = udev_monitor_enable_receiving(monitor);
831         if (err) {
832                 condlog(2, "failed to enable receiving : %s", strerror(-err));
833                 goto out;
834         }
835
836         events = 0;
837         gettimeofday(&start_time, NULL);
838         while (1) {
839                 struct uevent *uev;
840                 struct udev_device *dev;
841                 struct pollfd ev_poll;
842                 int poll_timeout;
843                 int fdcount;
844
845                 memset(&ev_poll, 0, sizeof(struct pollfd));
846                 ev_poll.fd = fd;
847                 ev_poll.events = POLLIN;
848                 poll_timeout = timeout * 1000;
849                 errno = 0;
850                 fdcount = poll(&ev_poll, 1, poll_timeout);
851                 if (fdcount && ev_poll.revents & POLLIN) {
852                         timeout = uevent_burst(&start_time, events + 1) ? 1 : 0;
853                         dev = udev_monitor_receive_device(monitor);
854                         if (!dev) {
855                                 condlog(0, "failed getting udev device");
856                                 continue;
857                         }
858                         uev = uevent_from_udev_device(dev);
859                         if (!uev)
860                                 continue;
861                         list_add_tail(&uev->node, &uevlisten_tmp);
862                         events++;
863                         continue;
864                 }
865                 if (fdcount < 0) {
866                         if (errno == EINTR)
867                                 continue;
868
869                         condlog(0, "error receiving "
870                                 "uevent message: %m");
871                         err = -errno;
872                         break;
873                 }
874                 if (!list_empty(&uevlisten_tmp)) {
875                         /*
876                          * Queue uevents and poke service pthread.
877                          */
878                         condlog(3, "Forwarding %d uevents", events);
879                         pthread_mutex_lock(uevq_lockp);
880                         list_splice_tail_init(&uevlisten_tmp, &uevq);
881                         pthread_cond_signal(uev_condp);
882                         pthread_mutex_unlock(uevq_lockp);
883                         events = 0;
884                 }
885                 gettimeofday(&start_time, NULL);
886                 timeout = 30;
887         }
888         need_failback = 0;
889 out:
890         if (monitor)
891                 pthread_cleanup_pop(1);
892         if (need_failback)
893                 err = failback_listen();
894         pthread_cleanup_pop(1);
895         return err;
896 }
897
898 int uevent_get_major(const struct uevent *uev)
899 {
900         return uevent_get_env_positive_int(uev, "MAJOR");
901 }
902
903 int uevent_get_minor(const struct uevent *uev)
904 {
905         return uevent_get_env_positive_int(uev, "MINOR");
906 }
907
908 int uevent_get_disk_ro(const struct uevent *uev)
909 {
910         return uevent_get_env_positive_int(uev, "DISK_RO");
911 }
912
913 static char *uevent_get_dm_str(const struct uevent *uev, char *attr)
914 {
915         const char *tmp = uevent_get_env_var(uev, attr);
916
917         if (tmp == NULL)
918                 return NULL;
919         return strdup(tmp);
920 }
921
922 char *uevent_get_dm_name(const struct uevent *uev)
923 {
924         return uevent_get_dm_str(uev, "DM_NAME");
925 }
926
927 char *uevent_get_dm_path(const struct uevent *uev)
928 {
929         return uevent_get_dm_str(uev, "DM_PATH");
930 }
931
932 char *uevent_get_dm_action(const struct uevent *uev)
933 {
934         return uevent_get_dm_str(uev, "DM_ACTION");
935 }
936
937 bool uevent_is_mpath(const struct uevent *uev)
938 {
939         const char *uuid = uevent_get_env_var(uev, "DM_UUID");
940
941         if (uuid == NULL)
942                 return false;
943         if (strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN))
944                 return false;
945         return uuid[UUID_PREFIX_LEN] != '\0';
946 }