[multipathd] change last CLI command syntax
[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 static int
864 map_discovery (struct vectors * vecs)
865 {
866         int i;
867         struct multipath * mpp;
868
869         if (dm_get_maps(vecs->mpvec, "multipath"))
870                 return 1;
871
872         vector_foreach_slot (vecs->mpvec, mpp, i)
873                 if (setup_multipath(vecs, mpp))
874                         return 1;
875
876         return 0;
877 }
878
879 int
880 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
881 {
882         struct vectors * vecs;
883         int r;
884         
885         *reply = NULL;
886         *len = 0;
887         vecs = (struct vectors *)trigger_data;
888
889         pthread_cleanup_push(cleanup_lock, vecs->lock);
890         lock(vecs->lock);
891
892         r = parse_cmd(str, reply, len, vecs);
893
894         if (r > 0) {
895                 *reply = STRDUP("fail\n");
896                 *len = strlen(*reply) + 1;
897                 r = 1;
898         }
899         else if (!r && *len == 0) {
900                 *reply = STRDUP("ok\n");
901                 *len = strlen(*reply) + 1;
902                 r = 0;
903         }
904         /* else if (r < 0) leave *reply alone */
905
906         lock_cleanup_pop(vecs->lock);
907         return r;
908 }
909
910 static int
911 uev_discard(char * devpath)
912 {
913         char a[10], b[10];
914
915         /*
916          * keep only block devices, discard partitions
917          */
918         if (sscanf(devpath, "/block/%10s", a) != 1 ||
919             sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
920                 condlog(4, "discard event on %s", devpath);
921                 return 1;
922         }
923         return 0;
924 }
925
926 int 
927 uev_trigger (struct uevent * uev, void * trigger_data)
928 {
929         int r = 0;
930         char devname[32];
931         struct vectors * vecs;
932
933         vecs = (struct vectors *)trigger_data;
934
935         if (uev_discard(uev->devpath))
936                 return 0;
937
938         basename(uev->devpath, devname);
939         lock(vecs->lock);
940
941         /*
942          * device map add/remove event
943          */
944         if (!strncmp(devname, "dm-", 3)) {
945                 if (!strncmp(uev->action, "add", 3)) {
946                         r = uev_add_map(devname, vecs);
947                         goto out;
948                 }
949                 if (!strncmp(uev->action, "remove", 6)) {
950                         r = uev_remove_map(devname, vecs);
951                         goto out;
952                 }
953                 if (!strncmp(uev->action, "umount", 6)) {
954                         r = uev_umount_map(devname, vecs);
955                         goto out;
956                 }
957                 goto out;
958         }
959         
960         /*
961          * path add/remove event
962          */
963         if (blacklist(conf->blist, devname))
964                 goto out;
965
966         if (!strncmp(uev->action, "add", 3)) {
967                 r = uev_add_path(devname, vecs);
968                 goto out;
969         }
970         if (!strncmp(uev->action, "remove", 6)) {
971                 r = uev_remove_path(devname, vecs);
972                 goto out;
973         }
974
975 out:
976         unlock(vecs->lock);
977         return r;
978 }
979
980 static void *
981 ueventloop (void * ap)
982 {
983         if (uevent_listen(&uev_trigger, ap))
984                 fprintf(stderr, "error starting uevent listener");
985                 
986         return NULL;
987 }
988
989 static void *
990 uxlsnrloop (void * ap)
991 {
992         if (load_keys())
993                 return NULL;
994         
995         if (alloc_handlers())
996                 return NULL;
997
998         add_handler(LIST+PATHS, cli_list_paths);
999         add_handler(LIST+MAPS, cli_list_maps);
1000         add_handler(LIST+MAPS+STATS, cli_list_maps_stats);
1001         add_handler(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
1002         add_handler(ADD+PATH, cli_add_path);
1003         add_handler(DEL+PATH, cli_del_path);
1004         add_handler(ADD+MAP, cli_add_map);
1005         add_handler(DEL+MAP, cli_del_map);
1006         add_handler(SWITCH+MAP+GROUP, cli_switch_group);
1007         add_handler(RECONFIGURE, cli_reconfigure);
1008         add_handler(SUSPEND+MAP, cli_suspend);
1009         add_handler(RESUME+MAP, cli_resume);
1010         add_handler(REINSTATE+PATH, cli_reinstate);
1011         add_handler(FAIL+PATH, cli_fail);
1012
1013         uxsock_listen(&uxsock_trigger, ap);
1014
1015         return NULL;
1016 }
1017
1018 static int
1019 exit_daemon (int status)
1020 {
1021         if (status != 0)
1022                 fprintf(stderr, "bad exit status. see daemon.log\n");
1023
1024         condlog(3, "unlink pidfile");
1025         unlink(DEFAULT_PIDFILE);
1026
1027         lock(&exit_mutex);
1028         pthread_cond_signal(&exit_cond);
1029         unlock(&exit_mutex);
1030
1031         return status;
1032 }
1033
1034 static void
1035 fail_path (struct path * pp)
1036 {
1037         if (!pp->mpp)
1038                 return;
1039
1040         condlog(2, "checker failed path %s in map %s",
1041                  pp->dev_t, pp->mpp->alias);
1042
1043         dm_fail_path(pp->mpp->alias, pp->dev_t);
1044         update_queue_mode_del_path(pp->mpp);
1045 }
1046
1047 /*
1048  * caller must have locked the path list before calling that function
1049  */
1050 static void
1051 reinstate_path (struct path * pp)
1052 {
1053         if (!pp->mpp)
1054                 return;
1055
1056         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1057                 condlog(0, "%s: reinstate failed", pp->dev_t);
1058         else {
1059                 condlog(2, "%s: reinstated", pp->dev_t);
1060                 update_queue_mode_add_path(pp->mpp);
1061         }
1062 }
1063
1064 static void
1065 enable_group(struct path * pp)
1066 {
1067         struct pathgroup * pgp;
1068
1069         /*
1070          * if path is added through uev_add_path, pgindex can be unset.
1071          * next update_strings() will set it, upon map reload event.
1072          *
1073          * we can safely return here, because upon map reload, all
1074          * PG will be enabled.
1075          */
1076         if (!pp->mpp->pg || !pp->pgindex)
1077                 return;
1078
1079         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1080         
1081         if (pgp->status == PGSTATE_DISABLED) {
1082                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1083                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1084         }
1085 }
1086
1087 static void
1088 mpvec_garbage_collector (struct vectors * vecs)
1089 {
1090         struct multipath * mpp;
1091         int i;
1092
1093         vector_foreach_slot (vecs->mpvec, mpp, i) {
1094                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1095                         condlog(2, "%s: remove dead map", mpp->alias);
1096                         remove_map(mpp, vecs, stop_waiter_thread, 1);
1097                         i--;
1098                 }
1099         }
1100 }
1101
1102 static void
1103 defered_failback_tick (vector mpvec)
1104 {
1105         struct multipath * mpp;
1106         int i;
1107
1108         vector_foreach_slot (mpvec, mpp, i) {
1109                 /*
1110                  * defered failback getting sooner
1111                  */
1112                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1113                         mpp->failback_tick--;
1114
1115                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1116                                 switch_pathgroup(mpp);
1117                 }
1118         }
1119 }
1120
1121 static void
1122 retry_count_tick(vector mpvec)
1123 {
1124         struct multipath *mpp;
1125         int i;
1126
1127         vector_foreach_slot (mpvec, mpp, i) {
1128                 if (mpp->retry_tick) {
1129                         mpp->stat_total_queueing_time++;
1130                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
1131                         if(--mpp->retry_tick == 0) {
1132                                 dm_queue_if_no_path(mpp->alias, 0);
1133                                 condlog(2, "%s: Disable queueing", mpp->alias);
1134                         }
1135                 }
1136         }
1137 }
1138
1139 static void *
1140 checkerloop (void *ap)
1141 {
1142         struct vectors *vecs;
1143         struct path *pp;
1144         int i, count = 0;
1145         int newstate;
1146         char checker_msg[MAX_CHECKER_MSG_SIZE];
1147
1148         mlockall(MCL_CURRENT | MCL_FUTURE);
1149
1150         memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
1151         vecs = (struct vectors *)ap;
1152
1153         condlog(2, "path checkers start up");
1154
1155         /*
1156          * init the path check interval
1157          */
1158         vector_foreach_slot (vecs->pathvec, pp, i) {
1159                 pp->checkint = conf->checkint;
1160         }
1161
1162         while (1) {
1163                 pthread_cleanup_push(cleanup_lock, vecs->lock);
1164                 lock(vecs->lock);
1165                 condlog(4, "tick");
1166
1167                 vector_foreach_slot (vecs->pathvec, pp, i) {
1168                         if (!pp->mpp)
1169                                 continue;
1170
1171                         if (pp->tick && --pp->tick)
1172                                 continue; /* don't check this path yet */
1173
1174                         /*
1175                          * provision a next check soonest,
1176                          * in case we exit abnormaly from here
1177                          */
1178                         pp->tick = conf->checkint;
1179                         
1180                         if (!pp->checkfn) {
1181                                 pathinfo(pp, conf->hwtable, DI_SYSFS);
1182                                 select_checkfn(pp);
1183                         }
1184
1185                         if (!pp->checkfn) {
1186                                 condlog(0, "%s: checkfn is void", pp->dev);
1187                                 continue;
1188                         }
1189                         newstate = pp->checkfn(pp->fd, checker_msg,
1190                                                &pp->checker_context);
1191                         
1192                         if (newstate < 0) {
1193                                 condlog(2, "%s: unusable path", pp->dev);
1194                                 pathinfo(pp, conf->hwtable, 0);
1195                                 continue;
1196                         }
1197
1198                         if (newstate != pp->state) {
1199                                 pp->state = newstate;
1200                                 LOG_MSG(1, checker_msg);
1201
1202                                 /*
1203                                  * upon state change, reset the checkint
1204                                  * to the shortest delay
1205                                  */
1206                                 pp->checkint = conf->checkint;
1207
1208                                 if (newstate == PATH_DOWN ||
1209                                     newstate == PATH_SHAKY ||
1210                                     update_multipath_strings(pp->mpp,
1211                                                              vecs->pathvec)) {
1212                                         /*
1213                                          * proactively fail path in the DM
1214                                          */
1215                                         fail_path(pp);
1216
1217                                         /*
1218                                          * cancel scheduled failback
1219                                          */
1220                                         pp->mpp->failback_tick = 0;
1221
1222                                         pp->mpp->stat_path_failures++;
1223                                         continue;
1224                                 }
1225
1226                                 /*
1227                                  * reinstate this path
1228                                  */
1229                                 reinstate_path(pp);
1230
1231                                 /*
1232                                  * schedule [defered] failback
1233                                  */
1234                                 if (pp->mpp->pgfailback > 0)
1235                                         pp->mpp->failback_tick =
1236                                                 pp->mpp->pgfailback + 1;
1237                                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1238                                     need_switch_pathgroup(pp->mpp, 1))
1239                                         switch_pathgroup(pp->mpp);
1240
1241                                 /*
1242                                  * if at least one path is up in a group, and
1243                                  * the group is disabled, re-enable it
1244                                  */
1245                                 if (newstate == PATH_UP)
1246                                         enable_group(pp);
1247                         }
1248                         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1249                                 LOG_MSG(4, checker_msg);
1250                                 /*
1251                                  * double the next check delay.
1252                                  * max at conf->max_checkint
1253                                  */
1254                                 if (pp->checkint < (conf->max_checkint / 2))
1255                                         pp->checkint = 2 * pp->checkint;
1256                                 else
1257                                         pp->checkint = conf->max_checkint;
1258
1259                                 pp->tick = pp->checkint;
1260                                 condlog(4, "%s: delay next check %is",
1261                                                 pp->dev_t, pp->tick);
1262
1263                         }
1264                         pp->state = newstate;
1265
1266                         /*
1267                          * path prio refreshing
1268                          */
1269                         condlog(4, "path prio refresh");
1270                         pathinfo(pp, conf->hwtable, DI_PRIO);
1271
1272                         if (need_switch_pathgroup(pp->mpp, 0)) {
1273                                 if (pp->mpp->pgfailback > 0)
1274                                         pp->mpp->failback_tick =
1275                                                 pp->mpp->pgfailback + 1;
1276                                 else if (pp->mpp->pgfailback ==
1277                                                 -FAILBACK_IMMEDIATE)
1278                                         switch_pathgroup(pp->mpp);
1279                         }
1280                 }
1281                 defered_failback_tick(vecs->mpvec);
1282                 retry_count_tick(vecs->mpvec);
1283
1284                 if (count)
1285                         count--;
1286                 else {
1287                         condlog(4, "map garbage collection");
1288                         mpvec_garbage_collector(vecs);
1289                         count = MAPGCINT;
1290                 }
1291                 
1292                 lock_cleanup_pop(vecs->lock);
1293                 sleep(1);
1294         }
1295         return NULL;
1296 }
1297
1298 int
1299 configure (struct vectors * vecs, int start_waiters)
1300 {
1301         struct multipath * mpp;
1302         struct path * pp;
1303         vector mpvec;
1304         int i;
1305
1306         if (!(vecs->pathvec = vector_alloc()))
1307                 return 1;
1308         
1309         if (!(vecs->mpvec = vector_alloc()))
1310                 return 1;
1311         
1312         if (!(mpvec = vector_alloc()))
1313                 return 1;
1314
1315         /*
1316          * probe for current path (from sysfs) and map (from dm) sets
1317          */
1318         path_discovery(vecs->pathvec, conf, DI_ALL);
1319
1320         vector_foreach_slot (vecs->pathvec, pp, i)
1321                 pp->checkint = conf->checkint;
1322
1323         if (map_discovery(vecs))
1324                 return 1;
1325
1326         /*
1327          * create new set of maps & push changed ones into dm
1328          */
1329         if (coalesce_paths(vecs, mpvec, NULL))
1330                 return 1;
1331
1332         /*
1333          * may need to remove some maps which are no longer relevant
1334          * e.g., due to blacklist changes in conf file
1335          */
1336         if (coalesce_maps(vecs, mpvec))
1337                 return 1;
1338
1339         dm_lib_release();
1340
1341         if (conf->verbosity > 2)
1342                 vector_foreach_slot(mpvec, mpp, i)
1343                         print_map(mpp);
1344
1345         /*
1346          * purge dm of old maps
1347          */
1348         remove_maps(vecs, NULL);
1349
1350         /*
1351          * save new set of maps formed by considering current path state
1352          */
1353         vecs->mpvec = mpvec;
1354
1355         /*
1356          * start dm event waiter threads for these new maps
1357          */
1358         vector_foreach_slot(vecs->mpvec, mpp, i) {
1359                 if (setup_multipath(vecs, mpp))
1360                         return 1;
1361                 if (start_waiters)
1362                         if (start_waiter_thread(mpp, vecs))
1363                                 return 1;
1364         }
1365         return 0;
1366 }
1367
1368 int
1369 reconfigure (struct vectors * vecs)
1370 {
1371         struct config * old = conf;
1372
1373         /*
1374          * free old map and path vectors ... they use old conf state
1375          */
1376         if (VECTOR_SIZE(vecs->mpvec))
1377                 remove_maps(vecs, stop_waiter_thread);
1378
1379         if (VECTOR_SIZE(vecs->pathvec))
1380                 free_pathvec(vecs->pathvec, FREE_PATHS);
1381
1382         conf = NULL;
1383
1384         if (load_config(DEFAULT_CONFIGFILE))
1385                 return 1;
1386
1387         conf->verbosity = old->verbosity;
1388
1389         if (!conf->checkint) {
1390                 conf->checkint = CHECKINT;
1391                 conf->max_checkint = MAX_CHECKINT;
1392         }
1393         configure(vecs, 1);
1394         free_config(old);
1395         return 0;
1396 }
1397
1398 static struct vectors *
1399 init_vecs (void)
1400 {
1401         struct vectors * vecs;
1402
1403         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1404
1405         if (!vecs)
1406                 return NULL;
1407
1408         vecs->lock = 
1409                 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1410
1411         if (!vecs->lock)
1412                 goto out;
1413
1414         vecs->pathvec = vector_alloc();
1415
1416         if (!vecs->pathvec)
1417                 goto out1;
1418                 
1419         vecs->mpvec = vector_alloc();
1420
1421         if (!vecs->mpvec)
1422                 goto out2;
1423         
1424         pthread_mutex_init(vecs->lock, NULL);
1425
1426         return vecs;
1427
1428 out2:
1429         vector_free(vecs->pathvec);
1430 out1:
1431         FREE(vecs->lock);
1432 out:
1433         FREE(vecs);
1434         condlog(0, "failed to init paths");
1435         return NULL;
1436 }
1437
1438 static void *
1439 signal_set(int signo, void (*func) (int))
1440 {
1441         int r;
1442         struct sigaction sig;
1443         struct sigaction osig;
1444
1445         sig.sa_handler = func;
1446         sigemptyset(&sig.sa_mask);
1447         sig.sa_flags = 0;
1448
1449         r = sigaction(signo, &sig, &osig);
1450
1451         if (r < 0)
1452                 return (SIG_ERR);
1453         else
1454                 return (osig.sa_handler);
1455 }
1456
1457 static void
1458 sighup (int sig)
1459 {
1460         condlog(2, "reconfigure (SIGHUP)");
1461
1462         lock(gvecs->lock);
1463         reconfigure(gvecs);
1464         unlock(gvecs->lock);
1465
1466 #ifdef _DEBUG_
1467         dbg_free_final(NULL);
1468 #endif
1469 }
1470
1471 static void
1472 sigend (int sig)
1473 {
1474         exit_daemon(0);
1475 }
1476
1477 static void
1478 sigusr1 (int sig)
1479 {
1480         condlog(3, "SIGUSR1 received");
1481 }
1482
1483 static void
1484 signal_init(void)
1485 {
1486         signal_set(SIGHUP, sighup);
1487         signal_set(SIGUSR1, sigusr1);
1488         signal_set(SIGINT, sigend);
1489         signal_set(SIGTERM, sigend);
1490         signal_set(SIGKILL, sigend);
1491 }
1492
1493 static void
1494 setscheduler (void)
1495 {
1496         int res;
1497         static struct sched_param sched_param = {
1498                 sched_priority: 99
1499         };
1500
1501         res = sched_setscheduler (0, SCHED_RR, &sched_param);
1502
1503         if (res == -1)
1504                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1505         return;
1506 }
1507
1508 static void
1509 set_oom_adj (int val)
1510 {
1511         FILE *fp;
1512
1513         fp = fopen("/proc/self/oom_adj", "w");
1514
1515         if (!fp)
1516                 return;
1517
1518         fprintf(fp, "%i", val);
1519         fclose(fp);
1520 }
1521         
1522 static int
1523 child (void * param)
1524 {
1525         pthread_t check_thr, uevent_thr, uxlsnr_thr;
1526         pthread_attr_t attr;
1527         struct vectors * vecs;
1528
1529         mlockall(MCL_CURRENT | MCL_FUTURE);
1530
1531         if (logsink)
1532                 log_thread_start();
1533
1534         condlog(2, "--------start up--------");
1535         condlog(2, "read " DEFAULT_CONFIGFILE);
1536
1537         if (load_config(DEFAULT_CONFIGFILE))
1538                 exit(1);
1539
1540         setlogmask(LOG_UPTO(conf->verbosity + 3));
1541
1542         /*
1543          * fill the voids left in the config file
1544          */
1545         if (!conf->checkint) {
1546                 conf->checkint = CHECKINT;
1547                 conf->max_checkint = MAX_CHECKINT;
1548         }
1549
1550         if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1551                 if (logsink)
1552                         log_thread_stop();
1553
1554                 exit(1);
1555         }
1556         signal_init();
1557         setscheduler();
1558         set_oom_adj(-16);
1559         vecs = gvecs = init_vecs();
1560
1561         if (!vecs)
1562                 exit(1);
1563
1564         if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1565                 condlog(0, "can not find sysfs mount point");
1566                 exit(1);
1567         }
1568
1569         /*
1570          * fetch and configure both paths and multipaths
1571          */
1572         if (configure(vecs, 1)) {
1573                 condlog(0, "failure during configuration");
1574                 exit(1);
1575         }
1576
1577         /*
1578          * start threads
1579          */
1580         pthread_attr_init(&attr);
1581         pthread_attr_setstacksize(&attr, 64 * 1024);
1582         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1583         
1584         pthread_create(&check_thr, &attr, checkerloop, vecs);
1585         pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1586         pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1587
1588         pthread_cond_wait(&exit_cond, &exit_mutex);
1589
1590         /*
1591          * exit path
1592          */
1593         lock(vecs->lock);
1594         remove_maps(vecs, stop_waiter_thread);
1595         free_pathvec(vecs->pathvec, FREE_PATHS);
1596
1597         pthread_cancel(check_thr);
1598         pthread_cancel(uevent_thr);
1599         pthread_cancel(uxlsnr_thr);
1600
1601         free_keys(keys);
1602         keys = NULL;
1603         free_handlers(handlers);
1604         handlers = NULL;
1605         free_polls();
1606
1607         unlock(vecs->lock);
1608         pthread_mutex_destroy(vecs->lock);
1609         FREE(vecs->lock);
1610         vecs->lock = NULL;
1611         FREE(vecs);
1612         vecs = NULL;
1613         free_config(conf);
1614         conf = NULL;
1615
1616         condlog(2, "--------shut down-------");
1617         
1618         if (logsink)
1619                 log_thread_stop();
1620
1621         dm_lib_release();
1622         dm_lib_exit();
1623
1624 #ifdef _DEBUG_
1625         dbg_free_final(NULL);
1626 #endif
1627
1628         exit(0);
1629 }
1630
1631 static int
1632 daemonize(void)
1633 {
1634         int pid;
1635         int in_fd, out_fd;
1636
1637         if( (pid = fork()) < 0){
1638                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1639                 return -1;
1640         }
1641         else if (pid != 0)
1642                 return pid;
1643
1644         setsid();
1645
1646         if ( (pid = fork()) < 0)
1647                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1648         else if (pid != 0)
1649                 _exit(0);
1650
1651         in_fd = open("/dev/null", O_RDONLY);
1652         if (in_fd < 0){
1653                 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1654                         strerror(errno));
1655                 _exit(0);
1656         }
1657         out_fd = open("/dev/console", O_WRONLY);
1658         if (out_fd < 0){
1659                 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1660                         strerror(errno));
1661                 _exit(0);
1662         }
1663
1664         close(STDIN_FILENO);
1665         dup(in_fd);
1666         close(STDOUT_FILENO);
1667         dup(out_fd);
1668         close(STDERR_FILENO);
1669         dup(out_fd);
1670
1671         close(in_fd);
1672         close(out_fd);
1673         chdir("/");
1674         umask(0);
1675         return 0;
1676 }
1677
1678 int
1679 main (int argc, char *argv[])
1680 {
1681         extern char *optarg;
1682         extern int optind;
1683         int arg;
1684         int err;
1685         
1686         logsink = 1;
1687
1688         if (getuid() != 0) {
1689                 fprintf(stderr, "need to be root\n");
1690                 exit(1);
1691         }
1692
1693         /* make sure we don't lock any path */
1694         chdir("/");
1695         umask(umask(077) | 022);
1696
1697         conf = alloc_config();
1698
1699         if (!conf)
1700                 exit(1);
1701
1702         while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1703         switch(arg) {
1704                 case 'd':
1705                         logsink = 0;
1706                         //debug=1; /* ### comment me out ### */
1707                         break;
1708                 case 'v':
1709                         if (sizeof(optarg) > sizeof(char *) ||
1710                             !isdigit(optarg[0]))
1711                                 exit(1);
1712
1713                         conf->verbosity = atoi(optarg);
1714                         break;
1715                 case 'k':
1716                         uxclnt(optarg);
1717                         exit(0);
1718                 default:
1719                         ;
1720                 }
1721         }
1722
1723         if (!logsink)
1724                 err = 0;
1725         else
1726                 err = daemonize();
1727         
1728         if (err < 0)
1729                 /* error */
1730                 exit(1);
1731         else if (err > 0)
1732                 /* parent dies */
1733                 exit(0);
1734         else
1735                 /* child lives */
1736                 return (child(NULL));
1737 }
1738