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