[multipathd] syncing path states
[platform/upstream/multipath-tools.git] / multipathd / main.c
1 /*
2  * Copyright (c) 2004, 2005 Christophe Varoqui
3  * Copyright (c) 2005 Kiyoshi Ueda, NEC
4  * Copyright (c) 2005 Benjamin Marzinski, Redhat
5  * Copyright (c) 2005 Edward Goggin, EMC
6  */
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <libdevmapper.h>
10 #include <wait.h>
11 #include <sys/mman.h>
12 #include <sys/types.h>
13 #include <fcntl.h>
14 #include <errno.h>
15
16 /*
17  * libsysfs
18  */
19 #include <sysfs/libsysfs.h>
20 #include <sysfs/dlist.h>
21
22 /*
23  * libcheckers
24  */
25 #include <checkers.h>
26
27 /*
28  * libmultipath
29  */
30 #include <parser.h>
31 #include <vector.h>
32 #include <memory.h>
33 #include <config.h>
34 #include <util.h>
35 #include <hwtable.h>
36 #include <defaults.h>
37 #include <structs.h>
38 #include <callout.h>
39 #include <blacklist.h>
40 #include <structs_vec.h>
41 #include <dmparser.h>
42 #include <devmapper.h>
43 #include <dict.h>
44 #include <discovery.h>
45 #include <debug.h>
46 #include <propsel.h>
47 #include <uevent.h>
48 #include <switchgroup.h>
49 #include <print.h>
50 #include <configure.h>
51
52 #include "main.h"
53 #include "pidfile.h"
54 #include "uxlsnr.h"
55 #include "uxclnt.h"
56 #include "cli.h"
57 #include "cli_handlers.h"
58
59 #define FILE_NAME_SIZE 256
60 #define CMDSIZE 160
61
62 #define LOG_MSG(a,b) \
63         if (strlen(b)) condlog(a, "%s: %s", pp->dev_t, b);
64
65 #ifdef LCKDBG
66 #define lock(a) \
67         fprintf(stderr, "%s:%s(%i) lock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
68         pthread_mutex_lock(a)
69 #define unlock(a) \
70         fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
71         pthread_mutex_unlock(a)
72 #define lock_cleanup_pop(a) \
73         fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
74         pthread_cleanup_pop(1);
75 #else
76 #define lock(a) pthread_mutex_lock(a)
77 #define unlock(a) pthread_mutex_unlock(a)
78 #define lock_cleanup_pop(a) pthread_cleanup_pop(1);
79 #endif
80
81 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
82 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
83
84 /*
85  * structs
86  */
87 struct vectors * gvecs; /* global copy of vecs for use in sig handlers */
88
89 static struct event_thread *
90 alloc_waiter (void)
91 {
92
93         struct event_thread * wp;
94
95         wp = (struct event_thread *)MALLOC(sizeof(struct event_thread));
96
97         return wp;
98 }
99
100 static void
101 free_waiter (void * data)
102 {
103         struct event_thread * wp = (struct event_thread *)data;
104
105         /*
106          * indicate in mpp that the wp is already freed storage
107          */
108         lock(wp->vecs->lock);
109
110         if (wp->mpp)
111                 /*
112                  * be careful, mpp may already be freed -- null if so
113                  */
114                 wp->mpp->waiter = NULL;
115         else
116                 condlog(3, "free_waiter, mpp freed before wp=%p,", wp);
117
118         unlock(wp->vecs->lock);
119
120         if (wp->dmt)
121                 dm_task_destroy(wp->dmt);
122
123         FREE(wp);
124 }
125
126 static void
127 stop_waiter_thread (struct multipath * mpp, struct vectors * vecs)
128 {
129         struct event_thread * wp = (struct event_thread *)mpp->waiter;
130         
131         if (!wp) {
132                 condlog(3, "%s: no waiter thread", mpp->alias);
133                 return;
134         }
135         condlog(2, "%s: stop event checker thread", wp->mapname);
136         pthread_kill((pthread_t)wp->thread, SIGUSR1);
137 }
138
139 static void
140 cleanup_lock (void * data)
141 {
142         unlock((pthread_mutex_t *)data);
143 }
144
145 /*
146  * mpp->no_path_retry:
147  *   -2 (QUEUE) : queue_if_no_path enabled, never turned off
148  *   -1 (FAIL)  : fail_if_no_path
149  *    0 (UNDEF) : nothing
150  *   >0         : queue_if_no_path enabled, turned off after polling n times
151  */
152 static void
153 update_queue_mode_del_path(struct multipath *mpp)
154 {
155         if (--mpp->nr_active == 0 && mpp->no_path_retry > 0) {
156                 /*
157                  * Enter retry mode.
158                  * meaning of +1: retry_tick may be decremented in
159                  *                checkerloop before starting retry.
160                  */
161                 mpp->stat_queueing_timeouts++;
162                 mpp->retry_tick = mpp->no_path_retry * conf->checkint + 1;
163                 condlog(1, "%s: Entering recovery mode: max_retries=%d",
164                         mpp->alias, mpp->no_path_retry);
165         }
166         condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
167 }
168
169 static void
170 update_queue_mode_add_path(struct multipath *mpp)
171 {
172         if (mpp->nr_active++ == 0 && mpp->no_path_retry > 0) {
173                 /* come back to normal mode from retry mode */
174                 mpp->retry_tick = 0;
175                 dm_queue_if_no_path(mpp->alias, 1);
176                 condlog(2, "%s: queue_if_no_path enabled", mpp->alias);
177                 condlog(1, "%s: Recovered to normal mode", mpp->alias);
178         }
179         condlog(2, "%s: remaining active paths: %d", mpp->alias, mpp->nr_active);
180 }
181
182 static int
183 need_switch_pathgroup (struct multipath * mpp, int refresh)
184 {
185         struct pathgroup * pgp;
186         struct path * pp;
187         int i, j;
188
189         if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
190                 return 0;
191
192         /*
193          * Refresh path priority values
194          */
195         if (refresh)
196                 vector_foreach_slot (mpp->pg, pgp, i)
197                         vector_foreach_slot (pgp->paths, pp, j)
198                                 pathinfo(pp, conf->hwtable, DI_PRIO);
199
200         mpp->bestpg = select_path_group(mpp);
201
202         if (mpp->bestpg != mpp->nextpg)
203                 return 1;
204
205         return 0;
206 }
207
208 static void
209 switch_pathgroup (struct multipath * mpp)
210 {
211         mpp->stat_switchgroup++;
212         dm_switchgroup(mpp->alias, mpp->bestpg);
213         condlog(2, "%s: switch to path group #%i",
214                  mpp->alias, mpp->bestpg);
215 }
216
217 static int
218 coalesce_maps(struct vectors *vecs, vector nmpv)
219 {
220         struct multipath * ompp;
221         vector ompv = vecs->mpvec;
222         int i, j;
223
224         vector_foreach_slot (ompv, ompp, i) {
225                 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
226                         /*
227                          * remove all current maps not allowed by the
228                          * current configuration
229                          */
230                         if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
231                                 condlog(0, "%s: unable to flush devmap",
232                                         ompp->alias);
233                                 /*
234                                  * may be just because the device is open
235                                  */
236                                 if (!vector_alloc_slot(nmpv))
237                                         return 1;
238
239                                 vector_set_slot(nmpv, ompp);
240                                 setup_multipath(vecs, ompp);
241
242                                 if ((j = find_slot(ompv, (void *)ompp)) != -1)
243                                         vector_del_slot(ompv, j);
244
245                                 continue;
246                         }
247                         else {
248                                 dm_lib_release();
249                                 condlog(3, "%s devmap removed", ompp->alias);
250                         }
251                 }
252         }
253         return 0;
254 }
255
256 static int
257 update_multipath (struct vectors *vecs, char *mapname)
258 {
259         struct multipath *mpp;
260         struct pathgroup  *pgp;
261         struct path *pp;
262         int i, j;
263         int r = 1;
264
265         mpp = find_mp_by_alias(vecs->mpvec, mapname);
266
267         if (!mpp)
268                 goto out;
269
270         free_pgvec(mpp->pg, KEEP_PATHS);
271         mpp->pg = NULL;
272
273         if (setup_multipath(vecs, mpp))
274                 goto out; /* mpp freed in setup_multipath */
275
276         /*
277          * compare checkers states with DM states
278          */
279         vector_foreach_slot (mpp->pg, pgp, i) {
280                 vector_foreach_slot (pgp->paths, pp, j) {
281                         if (pp->dmstate != PSTATE_FAILED)
282                                 continue;
283
284                         if (pp->state != PATH_DOWN) {
285                                 int oldstate = pp->state;
286                                 condlog(2, "%s: mark as failed", pp->dev_t);
287                                 mpp->stat_path_failures++;
288                                 pp->state = PATH_DOWN;
289                                 if (oldstate == PATH_UP ||
290                                     oldstate == PATH_GHOST)
291                                         update_queue_mode_del_path(mpp);
292
293                                 /*
294                                  * if opportune,
295                                  * schedule the next check earlier
296                                  */
297                                 if (pp->tick > conf->checkint)
298                                         pp->tick = conf->checkint;
299                         }
300                 }
301         }
302         r = 0;
303 out:
304         if (r)
305                 condlog(0, "failed to update multipath");
306
307         return r;
308 }
309
310 static sigset_t unblock_signals(void)
311 {
312         sigset_t set, old;
313
314         sigemptyset(&set);
315         sigaddset(&set, SIGHUP);
316         sigaddset(&set, SIGUSR1);
317         pthread_sigmask(SIG_UNBLOCK, &set, &old);
318         return old;
319 }
320
321 /*
322  * returns the reschedule delay
323  * negative means *stop*
324  */
325 static int
326 waiteventloop (struct event_thread * waiter)
327 {
328         sigset_t set;
329         int event_nr;
330         int r;
331
332         if (!waiter->event_nr)
333                 waiter->event_nr = dm_geteventnr(waiter->mapname);
334
335         if (!(waiter->dmt = dm_task_create(DM_DEVICE_WAITEVENT))) {
336                 condlog(0, "%s: devmap event #%i dm_task_create error",
337                                 waiter->mapname, waiter->event_nr);
338                 return 1;
339         }
340
341         if (!dm_task_set_name(waiter->dmt, waiter->mapname)) {
342                 condlog(0, "%s: devmap event #%i dm_task_set_name error",
343                                 waiter->mapname, waiter->event_nr);
344                 dm_task_destroy(waiter->dmt);
345                 return 1;
346         }
347
348         if (waiter->event_nr && !dm_task_set_event_nr(waiter->dmt,
349                                                       waiter->event_nr)) {
350                 condlog(0, "%s: devmap event #%i dm_task_set_event_nr error",
351                                 waiter->mapname, waiter->event_nr);
352                 dm_task_destroy(waiter->dmt);
353                 return 1;
354         }
355
356         dm_task_no_open_count(waiter->dmt);
357         
358         /* accept wait interruption */
359         set = unblock_signals();
360
361         /* interruption spits messages */
362         dm_shut_log();
363
364         /* wait */
365         r = dm_task_run(waiter->dmt);
366
367         /* wait is over : event or interrupt */
368         pthread_sigmask(SIG_SETMASK, &set, NULL);
369         //dm_restore_log();
370
371         if (!r) /* wait interrupted by signal */
372                 return -1;
373
374         dm_task_destroy(waiter->dmt);
375         waiter->dmt = NULL;
376         waiter->event_nr++;
377
378         /*
379          * upon event ...
380          */
381         while (1) {
382                 condlog(3, "%s: devmap event #%i",
383                                 waiter->mapname, waiter->event_nr);
384
385                 /*
386                  * event might be :
387                  *
388                  * 1) a table reload, which means our mpp structure is
389                  *    obsolete : refresh it through update_multipath()
390                  * 2) a path failed by DM : mark as such through
391                  *    update_multipath()
392                  * 3) map has gone away : stop the thread.
393                  * 4) a path reinstate : nothing to do
394                  * 5) a switch group : nothing to do
395                  */
396                 pthread_cleanup_push(cleanup_lock, waiter->vecs->lock);
397                 lock(waiter->vecs->lock);
398                 r = update_multipath(waiter->vecs, waiter->mapname);
399                 lock_cleanup_pop(waiter->vecs->lock);
400
401                 if (r)
402                         return -1; /* stop the thread */
403
404                 event_nr = dm_geteventnr(waiter->mapname);
405
406                 if (waiter->event_nr == event_nr)
407                         return 1; /* upon problem reschedule 1s later */
408
409                 waiter->event_nr = event_nr;
410         }
411         return -1; /* never reach there */
412 }
413
414 static void *
415 waitevent (void * et)
416 {
417         int r;
418         struct event_thread *waiter;
419
420         mlockall(MCL_CURRENT | MCL_FUTURE);
421
422         waiter = (struct event_thread *)et;
423         pthread_cleanup_push(free_waiter, et);
424
425         while (1) {
426                 r = waiteventloop(waiter);
427
428                 if (r < 0)
429                         break;
430
431                 sleep(r);
432         }
433
434         pthread_cleanup_pop(1);
435         return NULL;
436 }
437
438 static int
439 start_waiter_thread (struct multipath * mpp, struct vectors * vecs)
440 {
441         pthread_attr_t attr;
442         struct event_thread * wp;
443
444         if (!mpp)
445                 return 0;
446
447         if (pthread_attr_init(&attr))
448                 goto out;
449
450         pthread_attr_setstacksize(&attr, 32 * 1024);
451         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
452
453         wp = alloc_waiter();
454
455         if (!wp)
456                 goto out;
457
458         mpp->waiter = (void *)wp;
459         strncpy(wp->mapname, mpp->alias, WWID_SIZE);
460         wp->vecs = vecs;
461         wp->mpp = mpp;
462
463         if (pthread_create(&wp->thread, &attr, waitevent, wp)) {
464                 condlog(0, "%s: cannot create event checker", wp->mapname);
465                 goto out1;
466         }
467         condlog(2, "%s: event checker started", wp->mapname);
468
469         return 0;
470 out1:
471         free_waiter(wp);
472         mpp->waiter = NULL;
473 out:
474         condlog(0, "failed to start waiter thread");
475         return 1;
476 }
477
478 static void
479 sync_map_state(struct multipath *mpp)
480 {
481         int i, j;
482         struct pathgroup *pgp;
483         struct path *pp;
484
485         vector_foreach_slot (mpp->pg, pgp, i){
486                 vector_foreach_slot (pgp->paths, pp, j){
487                         if (pp->state <= PATH_UNCHECKED)
488                                 continue;
489                         if ((pp->dmstate == PSTATE_FAILED ||
490                              pp->dmstate == PSTATE_UNDEF) &&
491                             (pp->state == PATH_UP || pp->state == PATH_GHOST))
492                                 dm_reinstate_path(mpp->alias, pp->dev_t);
493                         else if ((pp->dmstate == PSTATE_ACTIVE ||
494                                   pp->dmstate == PSTATE_UNDEF) &&
495                                  (pp->state == PATH_DOWN ||
496                                   pp->state == PATH_SHAKY))
497                                 dm_fail_path(mpp->alias, pp->dev_t);
498                 }
499         }
500 }
501
502 static void
503 sync_maps_state(vector mpvec)
504 {
505         int i;
506         struct multipath *mpp;
507
508         vector_foreach_slot (mpvec, mpp, i) 
509                 sync_map_state(mpp);
510 }
511
512 static int
513 flush_map(struct multipath * mpp, struct vectors * vecs)
514 {
515         /*
516          * clear references to this map before flushing so we can ignore
517          * the spurious uevent we may generate with the dm_flush_map call below
518          */
519         if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
520                 /*
521                  * May not really be an error -- if the map was already flushed
522                  * from the device mapper by dmsetup(8) for instance.
523                  */
524                 condlog(0, "%s: can't flush", mpp->alias);
525                 return 1;
526         }
527         else {
528                 dm_lib_release();
529                 condlog(3, "%s: devmap removed", mpp->alias);
530         }
531
532         orphan_paths(vecs->pathvec, mpp);
533         remove_map(mpp, vecs, stop_waiter_thread, 1);
534
535         return 0;
536 }
537
538 static int
539 uev_add_map (char * devname, struct vectors * vecs)
540 {
541         condlog(2, "%s: add map (uevent)", devname);
542         return ev_add_map(devname, vecs);
543 }
544
545 int
546 ev_add_map (char * devname, struct vectors * vecs)
547 {
548         int major, minor;
549         char dev_t[BLK_DEV_SIZE];
550         char * alias;
551         char * refwwid;
552         struct multipath * mpp;
553         int map_present;
554         int r = 1;
555
556         if (sscanf(devname, "dm-%d", &minor) == 1 &&
557             !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
558             sscanf(dev_t, "%d:%d", &major, &minor) == 2)
559                 alias = dm_mapname(major, minor);
560         else
561                 alias = STRDUP(devname);
562                 
563         if (!alias)
564                 return 1;
565         
566         map_present = dm_map_present(alias);
567
568         if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
569                 condlog(4, "%s: not a multipath map", alias);
570                 FREE(alias);
571                 return 0;
572         }
573
574         mpp = find_mp_by_alias(vecs->mpvec, alias);
575
576         if (mpp) {
577                 /*
578                  * Not really an error -- we generate our own uevent
579                  * if we create a multipath mapped device as a result
580                  * of uev_add_path
581                  */
582                 condlog(0, "%s: devmap already registered",
583                         devname);
584                 FREE(alias);
585                 return 0;
586         }
587
588         /*
589          * now we can register the map
590          */
591         if (map_present && (mpp = add_map_without_path(vecs, minor, alias,
592                                         start_waiter_thread))) {
593                 sync_map_state(mpp);
594                 condlog(3, "%s: devmap %s added", alias, devname);
595                 return 0;
596         }
597         refwwid = get_refwwid(devname, DEV_DEVMAP, vecs->pathvec);
598
599         if (refwwid) {
600                 r = coalesce_paths(vecs, NULL, refwwid);
601                 dm_lib_release();
602         }
603         
604         if (!r)
605                 condlog(3, "%s: devmap %s added", alias, devname);
606         else
607                 condlog(0, "%s: uev_add_map %s failed", alias, devname);
608
609         FREE(refwwid);
610         FREE(alias);
611         return r;
612 }
613
614 static int
615 uev_remove_map (char * devname, struct vectors * vecs)
616 {
617         condlog(2, "%s: remove map (uevent)", devname);
618         return ev_remove_map(devname, vecs);
619 }
620
621 int
622 ev_remove_map (char * devname, struct vectors * vecs)
623 {
624         struct multipath * mpp;
625
626         mpp = find_mp_by_str(vecs->mpvec, devname);
627
628         if (!mpp) {
629                 condlog(3, "%s: devmap not registered, can't remove",
630                         devname);
631                 return 0;
632         }
633         flush_map(mpp, vecs);
634
635         return 0;
636 }
637
638 static int
639 uev_umount_map (char * devname, struct vectors * vecs)
640 {
641         struct multipath * mpp;
642
643         condlog(2, "%s: umount map (uevent)", devname);
644
645         mpp = find_mp_by_str(vecs->mpvec, devname);
646
647         if (!mpp)
648                 return 0;
649
650         update_mpp_paths(mpp, vecs->pathvec);
651         verify_paths(mpp, vecs, NULL);
652
653         if (!VECTOR_SIZE(mpp->paths))
654                 flush_map(mpp, vecs);
655
656         return 0;
657 }
658         
659 static int
660 uev_add_path (char * devname, struct vectors * vecs)
661 {
662         condlog(2, "%s: add path (uevent)", devname);
663         return (ev_add_path(devname, vecs) != 1)? 0 : 1;
664 }
665
666
667 /*
668  * returns:
669  * 0: added
670  * 1: error
671  * 2: blacklisted
672  */
673 int
674 ev_add_path (char * devname, struct vectors * vecs)
675 {
676         struct multipath * mpp;
677         struct path * pp;
678         char empty_buff[WWID_SIZE] = {0};
679
680         pp = find_path_by_dev(vecs->pathvec, devname);
681
682         if (pp) {
683                 condlog(0, "%s: spurious uevent, path already in pathvec, %p",
684                         devname, pp->mpp);
685                 /*
686                  * allow reconfig of orphaned path here
687                  */
688                 if (pp->mpp) return 1;
689         }
690         else {
691                 /*
692                  * get path vital state
693                  */
694                 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
695                       devname, DI_ALL))) {
696                         condlog(0, "%s: failed to store path info", devname);
697                         return 1;
698                 }
699                 pp->checkint = conf->checkint;
700         }
701
702         /*
703          * need path UID to go any further
704          */
705         if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
706                 condlog(0, "%s: failed to get path uid", devname);
707                 return 1; /* leave path added to pathvec */
708         }
709         if (blacklist_path(conf, pp)){
710                 int i = find_slot(vecs->pathvec, (void *)pp);
711                 if (i != -1)
712                         vector_del_slot(vecs->pathvec, i);
713                 free_path(pp);
714                 return 2;
715         }       
716         mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
717 rescan:
718         if (mpp) {
719                 if (adopt_paths(vecs->pathvec, mpp))
720                         return 1; /* leave path added to pathvec */
721
722                 verify_paths(mpp, vecs, NULL);
723                 mpp->action = ACT_RELOAD;
724         }
725         else {
726                 if ((mpp = add_map_with_path(vecs, pp, 1)))
727                         mpp->action = ACT_CREATE;
728                 else
729                         return 1; /* leave path added to pathvec */
730         }
731
732         /*
733          * push the map to the device-mapper
734          */
735         if (setup_map(mpp)) {
736                 condlog(0, "%s: failed to setup map for addition of new "
737                         "path %s", mpp->alias, devname);
738                 goto out;
739         }
740         /*
741          * reload the map for the multipath mapped device
742          */
743         if (domap(mpp) <= 0) {
744                 condlog(0, "%s: failed in domap for addition of new "
745                         "path %s", mpp->alias, devname);
746                 /*
747                  * deal with asynchronous uevents :((
748                  */
749                 if (mpp->action == ACT_RELOAD) {
750                         condlog(0, "%s: uev_add_path sleep", mpp->alias);
751                         sleep(1);
752                         update_mpp_paths(mpp, vecs->pathvec);
753                         goto rescan;
754                 }
755                 else
756                         goto out;
757         }
758         dm_lib_release();
759
760         /*
761          * update our state from kernel regardless of create or reload
762          */
763         if (setup_multipath(vecs, mpp))
764                 goto out;
765
766         sync_map_state(mpp);
767
768         if (mpp->action == ACT_CREATE &&
769             start_waiter_thread(mpp, vecs))
770                         goto out;
771
772         condlog(3, "%s path added to devmap %s", devname, mpp->alias);
773         return 0;
774
775 out:
776         remove_map(mpp, vecs, NULL, 1);
777         return 1;
778 }
779
780 static int
781 uev_remove_path (char * devname, struct vectors * vecs)
782 {
783         condlog(2, "%s: remove path (uevent)", devname);
784         return ev_remove_path(devname, vecs);
785 }
786
787 int
788 ev_remove_path (char * devname, struct vectors * vecs)
789 {
790         struct multipath * mpp;
791         struct path * pp;
792         int i;
793         int rm_path = 1;
794
795         pp = find_path_by_dev(vecs->pathvec, devname);
796
797         if (!pp) {
798                 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
799                 return 1;
800         }
801
802         /*
803          * avoid referring to the map of an orphanned path
804          */
805         if ((mpp = pp->mpp)) {
806
807                 /*
808                  * remove the map IFF removing the last path
809                  */
810                 if (pathcount(mpp, PATH_WILD) > 1) {
811                         vector rpvec = vector_alloc();
812
813                         /*
814                          * transform the mp->pg vector of vectors of paths
815                          * into a mp->params string to feed the device-mapper
816                          */
817                         update_mpp_paths(mpp, vecs->pathvec);
818                         if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
819                                 vector_del_slot(mpp->paths, i);
820
821                         if (VECTOR_SIZE(mpp->paths) == 0) {
822                                 char alias[WWID_SIZE];
823
824                                 /*
825                                  * flush_map will fail if the device is open
826                                  */
827                                 strncpy(alias, mpp->alias, WWID_SIZE);
828                                 if (flush_map(mpp, vecs))
829                                         rm_path = 0;
830                                 else
831                                         condlog(3, "%s: removed map after removing"
832                                                 " multiple paths", alias);
833                         }
834                         else {
835                                 if (setup_map(mpp)) {
836                                         condlog(0, "%s: failed to setup map for"
837                                                 " removal of path %s", mpp->alias, devname);
838                                         free_pathvec(rpvec, KEEP_PATHS);
839                                         goto out;
840                                 }
841                                 /*
842                                  * reload the map
843                                  */
844                                 mpp->action = ACT_RELOAD;
845                                 if (domap(mpp) <= 0) {
846                                         condlog(0, "%s: failed in domap for "
847                                                 "removal of path %s",
848                                                 mpp->alias, devname);
849                                         /*
850                                          * Delete path from pathvec so that
851                                          * update_mpp_paths wont find it later
852                                          * when/if another path is removed.
853                                          */
854                                         if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
855                                                 vector_del_slot(vecs->pathvec, i);
856                                         free_path(pp);
857                                         return 1;
858                                 }
859                                 /*
860                                  * update our state from kernel
861                                  */
862                                 if (setup_multipath(vecs, mpp)) {
863                                         free_pathvec(rpvec, KEEP_PATHS);
864                                         goto out;
865                                 }
866                                 sync_map_state(mpp);
867
868                                 condlog(3, "%s path removed from devmap %s",
869                                         devname, mpp->alias);
870                         }
871                         free_pathvec(rpvec, KEEP_PATHS);
872                 }
873                 else {
874                         char alias[WWID_SIZE];
875
876                         /*
877                          * flush_map will fail if the device is open
878                          */
879                         strncpy(alias, mpp->alias, WWID_SIZE);
880                         if (flush_map(mpp, vecs))
881                                 rm_path = 0;
882                         else
883                                 condlog(3, "%s: removed map", alias);
884                 }
885         }
886
887         if (rm_path) {
888                 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
889                         vector_del_slot(vecs->pathvec, i);
890                 free_path(pp);
891         }
892
893         return 0;
894
895 out:
896         remove_map(mpp, vecs, stop_waiter_thread, 1);
897         return 1;
898 }
899
900 static int
901 map_discovery (struct vectors * vecs)
902 {
903         int i;
904         struct multipath * mpp;
905
906         if (dm_get_maps(vecs->mpvec, "multipath"))
907                 return 1;
908
909         vector_foreach_slot (vecs->mpvec, mpp, i)
910                 if (setup_multipath(vecs, mpp))
911                         return 1;
912
913         return 0;
914 }
915
916 int
917 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
918 {
919         struct vectors * vecs;
920         int r;
921         
922         *reply = NULL;
923         *len = 0;
924         vecs = (struct vectors *)trigger_data;
925
926         pthread_cleanup_push(cleanup_lock, vecs->lock);
927         lock(vecs->lock);
928
929         r = parse_cmd(str, reply, len, vecs);
930
931         if (r > 0) {
932                 *reply = STRDUP("fail\n");
933                 *len = strlen(*reply) + 1;
934                 r = 1;
935         }
936         else if (!r && *len == 0) {
937                 *reply = STRDUP("ok\n");
938                 *len = strlen(*reply) + 1;
939                 r = 0;
940         }
941         /* else if (r < 0) leave *reply alone */
942
943         lock_cleanup_pop(vecs->lock);
944         return r;
945 }
946
947 static int
948 uev_discard(char * devpath)
949 {
950         char a[10], b[10];
951
952         /*
953          * keep only block devices, discard partitions
954          */
955         if (sscanf(devpath, "/block/%10s", a) != 1 ||
956             sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
957                 condlog(4, "discard event on %s", devpath);
958                 return 1;
959         }
960         return 0;
961 }
962
963 int 
964 uev_trigger (struct uevent * uev, void * trigger_data)
965 {
966         int r = 0;
967         char devname[32];
968         struct vectors * vecs;
969
970         vecs = (struct vectors *)trigger_data;
971
972         if (uev_discard(uev->devpath))
973                 return 0;
974
975         basename(uev->devpath, devname);
976         lock(vecs->lock);
977
978         /*
979          * device map add/remove event
980          */
981         if (!strncmp(devname, "dm-", 3)) {
982                 if (!strncmp(uev->action, "add", 3)) {
983                         r = uev_add_map(devname, vecs);
984                         goto out;
985                 }
986                 if (!strncmp(uev->action, "remove", 6)) {
987                         r = uev_remove_map(devname, vecs);
988                         goto out;
989                 }
990                 if (!strncmp(uev->action, "umount", 6)) {
991                         r = uev_umount_map(devname, vecs);
992                         goto out;
993                 }
994                 goto out;
995         }
996         
997         /*
998          * path add/remove event
999          */
1000         if (blacklist(conf->blist_devnode, devname))
1001                 goto out;
1002
1003         if (!strncmp(uev->action, "add", 3)) {
1004                 r = uev_add_path(devname, vecs);
1005                 goto out;
1006         }
1007         if (!strncmp(uev->action, "remove", 6)) {
1008                 r = uev_remove_path(devname, vecs);
1009                 goto out;
1010         }
1011
1012 out:
1013         unlock(vecs->lock);
1014         return r;
1015 }
1016
1017 static void *
1018 ueventloop (void * ap)
1019 {
1020         if (uevent_listen(&uev_trigger, ap))
1021                 fprintf(stderr, "error starting uevent listener");
1022                 
1023         return NULL;
1024 }
1025
1026 static void *
1027 uxlsnrloop (void * ap)
1028 {
1029         if (load_keys())
1030                 return NULL;
1031         
1032         if (alloc_handlers())
1033                 return NULL;
1034
1035         add_handler(LIST+PATHS, cli_list_paths);
1036         add_handler(LIST+MAPS, cli_list_maps);
1037         add_handler(LIST+MAPS+STATUS, cli_list_maps_status);
1038         add_handler(LIST+MAPS+STATS, cli_list_maps_stats);
1039         add_handler(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
1040         add_handler(LIST+TOPOLOGY, cli_list_maps_topology);
1041         add_handler(LIST+MAP+TOPOLOGY, cli_list_map_topology);
1042         add_handler(LIST+CONFIG, cli_list_config);
1043         add_handler(ADD+PATH, cli_add_path);
1044         add_handler(DEL+PATH, cli_del_path);
1045         add_handler(ADD+MAP, cli_add_map);
1046         add_handler(DEL+MAP, cli_del_map);
1047         add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1048         add_handler(RECONFIGURE, cli_reconfigure);
1049         add_handler(SUSPEND+MAP, cli_suspend);
1050         add_handler(RESUME+MAP, cli_resume);
1051         add_handler(REINSTATE+PATH, cli_reinstate);
1052         add_handler(FAIL+PATH, cli_fail);
1053
1054         uxsock_listen(&uxsock_trigger, ap);
1055
1056         return NULL;
1057 }
1058
1059 static int
1060 exit_daemon (int status)
1061 {
1062         if (status != 0)
1063                 fprintf(stderr, "bad exit status. see daemon.log\n");
1064
1065         condlog(3, "unlink pidfile");
1066         unlink(DEFAULT_PIDFILE);
1067
1068         lock(&exit_mutex);
1069         pthread_cond_signal(&exit_cond);
1070         unlock(&exit_mutex);
1071
1072         return status;
1073 }
1074
1075 static void
1076 fail_path (struct path * pp, int del_active)
1077 {
1078         if (!pp->mpp)
1079                 return;
1080
1081         condlog(2, "checker failed path %s in map %s",
1082                  pp->dev_t, pp->mpp->alias);
1083
1084         dm_fail_path(pp->mpp->alias, pp->dev_t);
1085         if (del_active)
1086                 update_queue_mode_del_path(pp->mpp);
1087 }
1088
1089 /*
1090  * caller must have locked the path list before calling that function
1091  */
1092 static void
1093 reinstate_path (struct path * pp, int add_active)
1094 {
1095         if (!pp->mpp)
1096                 return;
1097
1098         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1099                 condlog(0, "%s: reinstate failed", pp->dev_t);
1100         else {
1101                 condlog(2, "%s: reinstated", pp->dev_t);
1102                 if (add_active)
1103                         update_queue_mode_add_path(pp->mpp);
1104         }
1105 }
1106
1107 static void
1108 enable_group(struct path * pp)
1109 {
1110         struct pathgroup * pgp;
1111
1112         /*
1113          * if path is added through uev_add_path, pgindex can be unset.
1114          * next update_strings() will set it, upon map reload event.
1115          *
1116          * we can safely return here, because upon map reload, all
1117          * PG will be enabled.
1118          */
1119         if (!pp->mpp->pg || !pp->pgindex)
1120                 return;
1121
1122         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1123         
1124         if (pgp->status == PGSTATE_DISABLED) {
1125                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1126                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1127         }
1128 }
1129
1130 static void
1131 mpvec_garbage_collector (struct vectors * vecs)
1132 {
1133         struct multipath * mpp;
1134         int i;
1135
1136         vector_foreach_slot (vecs->mpvec, mpp, i) {
1137                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1138                         condlog(2, "%s: remove dead map", mpp->alias);
1139                         remove_map(mpp, vecs, stop_waiter_thread, 1);
1140                         i--;
1141                 }
1142         }
1143 }
1144
1145 static void
1146 defered_failback_tick (vector mpvec)
1147 {
1148         struct multipath * mpp;
1149         int i;
1150
1151         vector_foreach_slot (mpvec, mpp, i) {
1152                 /*
1153                  * defered failback getting sooner
1154                  */
1155                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1156                         mpp->failback_tick--;
1157
1158                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1159                                 switch_pathgroup(mpp);
1160                 }
1161         }
1162 }
1163
1164 static void
1165 retry_count_tick(vector mpvec)
1166 {
1167         struct multipath *mpp;
1168         int i;
1169
1170         vector_foreach_slot (mpvec, mpp, i) {
1171                 if (mpp->retry_tick) {
1172                         mpp->stat_total_queueing_time++;
1173                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
1174                         if(--mpp->retry_tick == 0) {
1175                                 dm_queue_if_no_path(mpp->alias, 0);
1176                                 condlog(2, "%s: Disable queueing", mpp->alias);
1177                         }
1178                 }
1179         }
1180 }
1181
1182 static void *
1183 checkerloop (void *ap)
1184 {
1185         struct vectors *vecs;
1186         struct path *pp;
1187         int i, count = 0;
1188         int newstate;
1189
1190         mlockall(MCL_CURRENT | MCL_FUTURE);
1191         vecs = (struct vectors *)ap;
1192         condlog(2, "path checkers start up");
1193
1194         /*
1195          * init the path check interval
1196          */
1197         vector_foreach_slot (vecs->pathvec, pp, i) {
1198                 pp->checkint = conf->checkint;
1199         }
1200
1201         while (1) {
1202                 pthread_cleanup_push(cleanup_lock, vecs->lock);
1203                 lock(vecs->lock);
1204                 condlog(4, "tick");
1205
1206                 vector_foreach_slot (vecs->pathvec, pp, i) {
1207                         if (!pp->mpp)
1208                                 continue;
1209
1210                         if (pp->tick && --pp->tick)
1211                                 continue; /* don't check this path yet */
1212
1213                         /*
1214                          * provision a next check soonest,
1215                          * in case we exit abnormaly from here
1216                          */
1217                         pp->tick = conf->checkint;
1218                         
1219                         if (!checker_selected(&pp->checker)) {
1220                                 pathinfo(pp, conf->hwtable, DI_SYSFS);
1221                                 select_checker(pp);
1222                         }
1223
1224                         if (!checker_selected(&pp->checker)) {
1225                                 condlog(0, "%s: checker is not set", pp->dev);
1226                                 continue;
1227                         }
1228                         newstate = checker_check(&pp->checker);
1229                         
1230                         if (newstate < 0) {
1231                                 condlog(2, "%s: unusable path", pp->dev);
1232                                 pathinfo(pp, conf->hwtable, 0);
1233                                 continue;
1234                         }
1235
1236                         if (newstate != pp->state) {
1237                                 int oldstate = pp->state;
1238                                 pp->state = newstate;
1239                                 LOG_MSG(1, checker_message(&pp->checker));
1240
1241                                 /*
1242                                  * upon state change, reset the checkint
1243                                  * to the shortest delay
1244                                  */
1245                                 pp->checkint = conf->checkint;
1246
1247                                 if (newstate == PATH_DOWN ||
1248                                     newstate == PATH_SHAKY ||
1249                                     update_multipath_strings(pp->mpp,
1250                                                              vecs->pathvec)) {
1251                                         /*
1252                                          * proactively fail path in the DM
1253                                          */
1254                                         if (oldstate == PATH_UP ||
1255                                             oldstate == PATH_GHOST)
1256                                                 fail_path(pp, 1);
1257                                         else
1258                                                 fail_path(pp, 0);
1259
1260                                         /*
1261                                          * cancel scheduled failback
1262                                          */
1263                                         pp->mpp->failback_tick = 0;
1264
1265                                         pp->mpp->stat_path_failures++;
1266                                         continue;
1267                                 }
1268
1269                                 /*
1270                                  * reinstate this path
1271                                  */
1272                                 if (oldstate != PATH_UP &&
1273                                     oldstate != PATH_GHOST)
1274                                         reinstate_path(pp, 1);
1275                                 else
1276                                         reinstate_path(pp, 0);
1277
1278                                 /*
1279                                  * schedule [defered] failback
1280                                  */
1281                                 if (pp->mpp->pgfailback > 0)
1282                                         pp->mpp->failback_tick =
1283                                                 pp->mpp->pgfailback + 1;
1284                                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1285                                     need_switch_pathgroup(pp->mpp, 1))
1286                                         switch_pathgroup(pp->mpp);
1287
1288                                 /*
1289                                  * if at least one path is up in a group, and
1290                                  * the group is disabled, re-enable it
1291                                  */
1292                                 if (newstate == PATH_UP)
1293                                         enable_group(pp);
1294                         }
1295                         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1296                                 LOG_MSG(4, checker_message(&pp->checker));
1297                                 /*
1298                                  * double the next check delay.
1299                                  * max at conf->max_checkint
1300                                  */
1301                                 if (pp->checkint < (conf->max_checkint / 2))
1302                                         pp->checkint = 2 * pp->checkint;
1303                                 else
1304                                         pp->checkint = conf->max_checkint;
1305
1306                                 pp->tick = pp->checkint;
1307                                 condlog(4, "%s: delay next check %is",
1308                                                 pp->dev_t, pp->tick);
1309
1310                         }
1311                         pp->state = newstate;
1312
1313                         /*
1314                          * path prio refreshing
1315                          */
1316                         condlog(4, "path prio refresh");
1317                         pathinfo(pp, conf->hwtable, DI_PRIO);
1318
1319                         if (need_switch_pathgroup(pp->mpp, 0)) {
1320                                 if (pp->mpp->pgfailback > 0 &&
1321                                     pp->mpp->failback_tick <= 0)
1322                                         pp->mpp->failback_tick =
1323                                                 pp->mpp->pgfailback + 1;
1324                                 else if (pp->mpp->pgfailback ==
1325                                                 -FAILBACK_IMMEDIATE)
1326                                         switch_pathgroup(pp->mpp);
1327                         }
1328                 }
1329                 defered_failback_tick(vecs->mpvec);
1330                 retry_count_tick(vecs->mpvec);
1331
1332                 if (count)
1333                         count--;
1334                 else {
1335                         condlog(4, "map garbage collection");
1336                         mpvec_garbage_collector(vecs);
1337                         count = MAPGCINT;
1338                 }
1339                 
1340                 lock_cleanup_pop(vecs->lock);
1341                 sleep(1);
1342         }
1343         return NULL;
1344 }
1345
1346 int
1347 configure (struct vectors * vecs, int start_waiters)
1348 {
1349         struct multipath * mpp;
1350         struct path * pp;
1351         vector mpvec;
1352         int i;
1353
1354         if (!(vecs->pathvec = vector_alloc()))
1355                 return 1;
1356         
1357         if (!(vecs->mpvec = vector_alloc()))
1358                 return 1;
1359         
1360         if (!(mpvec = vector_alloc()))
1361                 return 1;
1362
1363         /*
1364          * probe for current path (from sysfs) and map (from dm) sets
1365          */
1366         path_discovery(vecs->pathvec, conf, DI_ALL);
1367
1368         vector_foreach_slot (vecs->pathvec, pp, i){
1369                 if (blacklist_path(conf, pp)){
1370                         vector_del_slot(vecs->pathvec, i);
1371                         free_path(pp);
1372                         i--;
1373                 }       
1374                 else
1375                         pp->checkint = conf->checkint;
1376         }
1377         if (map_discovery(vecs))
1378                 return 1;
1379
1380         /*
1381          * create new set of maps & push changed ones into dm
1382          */
1383         if (coalesce_paths(vecs, mpvec, NULL))
1384                 return 1;
1385
1386         /*
1387          * may need to remove some maps which are no longer relevant
1388          * e.g., due to blacklist changes in conf file
1389          */
1390         if (coalesce_maps(vecs, mpvec))
1391                 return 1;
1392
1393         dm_lib_release();
1394
1395         sync_maps_state(mpvec);
1396
1397         if (conf->verbosity > 2)
1398                 vector_foreach_slot(mpvec, mpp, i)
1399                         print_map(mpp);
1400
1401         /*
1402          * purge dm of old maps
1403          */
1404         remove_maps(vecs, NULL);
1405
1406         /*
1407          * save new set of maps formed by considering current path state
1408          */
1409         vecs->mpvec = mpvec;
1410
1411         /*
1412          * start dm event waiter threads for these new maps
1413          */
1414         vector_foreach_slot(vecs->mpvec, mpp, i) {
1415                 if (setup_multipath(vecs, mpp))
1416                         return 1;
1417                 if (start_waiters)
1418                         if (start_waiter_thread(mpp, vecs))
1419                                 return 1;
1420         }
1421         return 0;
1422 }
1423
1424 int
1425 reconfigure (struct vectors * vecs)
1426 {
1427         struct config * old = conf;
1428
1429         /*
1430          * free old map and path vectors ... they use old conf state
1431          */
1432         if (VECTOR_SIZE(vecs->mpvec))
1433                 remove_maps(vecs, stop_waiter_thread);
1434
1435         if (VECTOR_SIZE(vecs->pathvec))
1436                 free_pathvec(vecs->pathvec, FREE_PATHS);
1437
1438         conf = NULL;
1439
1440         if (load_config(DEFAULT_CONFIGFILE))
1441                 return 1;
1442
1443         conf->verbosity = old->verbosity;
1444
1445         if (!conf->checkint) {
1446                 conf->checkint = DEFAULT_CHECKINT;
1447                 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1448         }
1449         configure(vecs, 1);
1450         free_config(old);
1451         return 0;
1452 }
1453
1454 static struct vectors *
1455 init_vecs (void)
1456 {
1457         struct vectors * vecs;
1458
1459         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1460
1461         if (!vecs)
1462                 return NULL;
1463
1464         vecs->lock = 
1465                 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1466
1467         if (!vecs->lock)
1468                 goto out;
1469
1470         vecs->pathvec = vector_alloc();
1471
1472         if (!vecs->pathvec)
1473                 goto out1;
1474                 
1475         vecs->mpvec = vector_alloc();
1476
1477         if (!vecs->mpvec)
1478                 goto out2;
1479         
1480         pthread_mutex_init(vecs->lock, NULL);
1481
1482         return vecs;
1483
1484 out2:
1485         vector_free(vecs->pathvec);
1486 out1:
1487         FREE(vecs->lock);
1488 out:
1489         FREE(vecs);
1490         condlog(0, "failed to init paths");
1491         return NULL;
1492 }
1493
1494 static void *
1495 signal_set(int signo, void (*func) (int))
1496 {
1497         int r;
1498         struct sigaction sig;
1499         struct sigaction osig;
1500
1501         sig.sa_handler = func;
1502         sigemptyset(&sig.sa_mask);
1503         sig.sa_flags = 0;
1504
1505         r = sigaction(signo, &sig, &osig);
1506
1507         if (r < 0)
1508                 return (SIG_ERR);
1509         else
1510                 return (osig.sa_handler);
1511 }
1512
1513 static void
1514 sighup (int sig)
1515 {
1516         condlog(2, "reconfigure (SIGHUP)");
1517
1518         lock(gvecs->lock);
1519         reconfigure(gvecs);
1520         unlock(gvecs->lock);
1521
1522 #ifdef _DEBUG_
1523         dbg_free_final(NULL);
1524 #endif
1525 }
1526
1527 static void
1528 sigend (int sig)
1529 {
1530         exit_daemon(0);
1531 }
1532
1533 static void
1534 sigusr1 (int sig)
1535 {
1536         condlog(3, "SIGUSR1 received");
1537 }
1538
1539 static void
1540 signal_init(void)
1541 {
1542         signal_set(SIGHUP, sighup);
1543         signal_set(SIGUSR1, sigusr1);
1544         signal_set(SIGINT, sigend);
1545         signal_set(SIGTERM, sigend);
1546         signal_set(SIGKILL, sigend);
1547 }
1548
1549 static void
1550 setscheduler (void)
1551 {
1552         int res;
1553         static struct sched_param sched_param = {
1554                 sched_priority: 99
1555         };
1556
1557         res = sched_setscheduler (0, SCHED_RR, &sched_param);
1558
1559         if (res == -1)
1560                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1561         return;
1562 }
1563
1564 static void
1565 set_oom_adj (int val)
1566 {
1567         FILE *fp;
1568
1569         fp = fopen("/proc/self/oom_adj", "w");
1570
1571         if (!fp)
1572                 return;
1573
1574         fprintf(fp, "%i", val);
1575         fclose(fp);
1576 }
1577         
1578 static int
1579 child (void * param)
1580 {
1581         pthread_t check_thr, uevent_thr, uxlsnr_thr;
1582         pthread_attr_t attr;
1583         struct vectors * vecs;
1584
1585         mlockall(MCL_CURRENT | MCL_FUTURE);
1586
1587         if (logsink)
1588                 log_thread_start();
1589
1590         condlog(2, "--------start up--------");
1591         condlog(2, "read " DEFAULT_CONFIGFILE);
1592
1593         if (load_config(DEFAULT_CONFIGFILE))
1594                 exit(1);
1595
1596         setlogmask(LOG_UPTO(conf->verbosity + 3));
1597
1598         /*
1599          * fill the voids left in the config file
1600          */
1601         if (!conf->checkint) {
1602                 conf->checkint = DEFAULT_CHECKINT;
1603                 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1604         }
1605
1606         if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1607                 if (logsink)
1608                         log_thread_stop();
1609
1610                 exit(1);
1611         }
1612         signal_init();
1613         setscheduler();
1614         set_oom_adj(-16);
1615         vecs = gvecs = init_vecs();
1616
1617         if (!vecs)
1618                 exit(1);
1619
1620         if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1621                 condlog(0, "can not find sysfs mount point");
1622                 exit(1);
1623         }
1624
1625         /*
1626          * fetch and configure both paths and multipaths
1627          */
1628         if (configure(vecs, 1)) {
1629                 condlog(0, "failure during configuration");
1630                 exit(1);
1631         }
1632
1633         /*
1634          * start threads
1635          */
1636         pthread_attr_init(&attr);
1637         pthread_attr_setstacksize(&attr, 64 * 1024);
1638         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1639         
1640         pthread_create(&check_thr, &attr, checkerloop, vecs);
1641         pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1642         pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1643
1644         pthread_cond_wait(&exit_cond, &exit_mutex);
1645
1646         /*
1647          * exit path
1648          */
1649         lock(vecs->lock);
1650         remove_maps(vecs, stop_waiter_thread);
1651         free_pathvec(vecs->pathvec, FREE_PATHS);
1652
1653         pthread_cancel(check_thr);
1654         pthread_cancel(uevent_thr);
1655         pthread_cancel(uxlsnr_thr);
1656
1657         free_keys(keys);
1658         keys = NULL;
1659         free_handlers(handlers);
1660         handlers = NULL;
1661         free_polls();
1662
1663         unlock(vecs->lock);
1664         pthread_mutex_destroy(vecs->lock);
1665         FREE(vecs->lock);
1666         vecs->lock = NULL;
1667         FREE(vecs);
1668         vecs = NULL;
1669         free_config(conf);
1670         conf = NULL;
1671
1672         condlog(2, "--------shut down-------");
1673         
1674         if (logsink)
1675                 log_thread_stop();
1676
1677         dm_lib_release();
1678         dm_lib_exit();
1679
1680 #ifdef _DEBUG_
1681         dbg_free_final(NULL);
1682 #endif
1683
1684         exit(0);
1685 }
1686
1687 static int
1688 daemonize(void)
1689 {
1690         int pid;
1691         int in_fd, out_fd;
1692
1693         if( (pid = fork()) < 0){
1694                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1695                 return -1;
1696         }
1697         else if (pid != 0)
1698                 return pid;
1699
1700         setsid();
1701
1702         if ( (pid = fork()) < 0)
1703                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1704         else if (pid != 0)
1705                 _exit(0);
1706
1707         in_fd = open("/dev/null", O_RDONLY);
1708         if (in_fd < 0){
1709                 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1710                         strerror(errno));
1711                 _exit(0);
1712         }
1713         out_fd = open("/dev/console", O_WRONLY);
1714         if (out_fd < 0){
1715                 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1716                         strerror(errno));
1717                 _exit(0);
1718         }
1719
1720         close(STDIN_FILENO);
1721         dup(in_fd);
1722         close(STDOUT_FILENO);
1723         dup(out_fd);
1724         close(STDERR_FILENO);
1725         dup(out_fd);
1726
1727         close(in_fd);
1728         close(out_fd);
1729         chdir("/");
1730         umask(0);
1731         return 0;
1732 }
1733
1734 int
1735 main (int argc, char *argv[])
1736 {
1737         extern char *optarg;
1738         extern int optind;
1739         int arg;
1740         int err;
1741         
1742         logsink = 1;
1743
1744         if (getuid() != 0) {
1745                 fprintf(stderr, "need to be root\n");
1746                 exit(1);
1747         }
1748
1749         /* make sure we don't lock any path */
1750         chdir("/");
1751         umask(umask(077) | 022);
1752
1753         conf = alloc_config();
1754
1755         if (!conf)
1756                 exit(1);
1757
1758         while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1759         switch(arg) {
1760                 case 'd':
1761                         logsink = 0;
1762                         //debug=1; /* ### comment me out ### */
1763                         break;
1764                 case 'v':
1765                         if (sizeof(optarg) > sizeof(char *) ||
1766                             !isdigit(optarg[0]))
1767                                 exit(1);
1768
1769                         conf->verbosity = atoi(optarg);
1770                         break;
1771                 case 'k':
1772                         uxclnt(optarg);
1773                         exit(0);
1774                 default:
1775                         ;
1776                 }
1777         }
1778
1779         if (!logsink)
1780                 err = 0;
1781         else
1782                 err = daemonize();
1783         
1784         if (err < 0)
1785                 /* error */
1786                 exit(1);
1787         else if (err > 0)
1788                 /* parent dies */
1789                 exit(0);
1790         else
1791                 /* child lives */
1792                 return (child(NULL));
1793 }
1794