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