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