[libmultipath] more path: and multipath: output prefixing
[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 <hwtable.h>
38 #include <defaults.h>
39 #include <structs.h>
40 #include <blacklist.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         vector_foreach_slot (ompv, ompp, i) {
230                 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
231                         /*
232                          * remove all current maps not allowed by the
233                          * current configuration
234                          */
235                         if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
236                                 condlog(0, "%s: unable to flush devmap",
237                                         ompp->alias);
238                                 /*
239                                  * may be just because the device is open
240                                  */
241                                 if (!vector_alloc_slot(nmpv))
242                                         return 1;
243
244                                 vector_set_slot(nmpv, ompp);
245                                 setup_multipath(vecs, ompp);
246
247                                 if ((j = find_slot(ompv, (void *)ompp)) != -1)
248                                         vector_del_slot(ompv, j);
249
250                                 continue;
251                         }
252                         else {
253                                 dm_lib_release();
254                                 condlog(3, "%s devmap removed", ompp->alias);
255                         }
256                 }
257         }
258         return 0;
259 }
260
261 static int
262 update_multipath (struct vectors *vecs, char *mapname)
263 {
264         struct multipath *mpp;
265         struct pathgroup  *pgp;
266         struct path *pp;
267         int i, j;
268         int r = 1;
269
270         mpp = find_mp_by_alias(vecs->mpvec, mapname);
271
272         if (!mpp)
273                 goto out;
274
275         free_pgvec(mpp->pg, KEEP_PATHS);
276         mpp->pg = NULL;
277
278         if (setup_multipath(vecs, mpp))
279                 goto out; /* mpp freed in setup_multipath */
280
281         /*
282          * compare checkers states with DM states
283          */
284         vector_foreach_slot (mpp->pg, pgp, i) {
285                 vector_foreach_slot (pgp->paths, pp, j) {
286                         if (pp->dmstate != PSTATE_FAILED)
287                                 continue;
288
289                         if (pp->state != PATH_DOWN) {
290                                 condlog(2, "%s: mark as failed", pp->dev_t);
291                                 mpp->stat_path_failures++;
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 static int
507 uev_add_map (char * devname, struct vectors * vecs)
508 {
509         condlog(2, "%s: add map (uevent)", devname);
510         return ev_add_map(devname, vecs);
511 }
512
513 int
514 ev_add_map (char * devname, struct vectors * vecs)
515 {
516         int major, minor;
517         char dev_t[BLK_DEV_SIZE];
518         char * alias;
519         char * refwwid;
520         struct multipath * mpp;
521         int map_present;
522         int r = 1;
523
524         if (sscanf(devname, "dm-%d", &minor) == 1 &&
525             !sysfs_get_dev(sysfs_path, devname, dev_t, BLK_DEV_SIZE) &&
526             sscanf(dev_t, "%d:%d", &major, &minor) == 2)
527                 alias = dm_mapname(major, minor);
528         else
529                 alias = STRDUP(devname);
530                 
531         if (!alias)
532                 return 1;
533         
534         map_present = dm_map_present(alias);
535
536         if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
537                 condlog(4, "%s: not a multipath map", alias);
538                 FREE(alias);
539                 return 0;
540         }
541
542         mpp = find_mp_by_alias(vecs->mpvec, alias);
543
544         if (mpp) {
545                 /*
546                  * Not really an error -- we generate our own uevent
547                  * if we create a multipath mapped device as a result
548                  * of uev_add_path
549                  */
550                 condlog(0, "%s: devmap already registered",
551                         devname);
552                 FREE(alias);
553                 return 0;
554         }
555
556         /*
557          * now we can register the map
558          */
559         if (map_present && (mpp = add_map_without_path(vecs, minor, alias,
560                                         start_waiter_thread))) {
561                 condlog(3, "%s: devmap %s added", alias, devname);
562                 return 0;
563         }
564         refwwid = get_refwwid(devname, DEV_DEVMAP, vecs->pathvec);
565
566         if (refwwid) {
567                 r = coalesce_paths(vecs, NULL, refwwid);
568                 dm_lib_release();
569         }
570         
571         if (!r)
572                 condlog(3, "%s: devmap %s added", alias, devname);
573         else
574                 condlog(0, "%s: uev_add_map %s failed", alias, devname);
575
576         FREE(refwwid);
577         FREE(alias);
578         return r;
579 }
580
581 static int
582 uev_remove_map (char * devname, struct vectors * vecs)
583 {
584         condlog(2, "%s: remove map (uevent)", devname);
585         return ev_remove_map(devname, vecs);
586 }
587
588 int
589 ev_remove_map (char * devname, struct vectors * vecs)
590 {
591         struct multipath * mpp;
592
593         mpp = find_mp_by_str(vecs->mpvec, devname);
594
595         if (!mpp) {
596                 condlog(3, "%s: devmap not registered, can't remove",
597                         devname);
598                 return 0;
599         }
600         flush_map(mpp, vecs);
601
602         return 0;
603 }
604
605 static int
606 uev_umount_map (char * devname, struct vectors * vecs)
607 {
608         struct multipath * mpp;
609
610         condlog(2, "%s: umount map (uevent)", devname);
611
612         mpp = find_mp_by_str(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 static int
627 uev_add_path (char * devname, struct vectors * vecs)
628 {
629         condlog(2, "%s: add path (uevent)", devname);
630         return ev_add_path(devname, vecs);
631 }
632
633 int
634 ev_add_path (char * devname, struct vectors * vecs)
635 {
636         struct multipath * mpp;
637         struct path * pp;
638         char empty_buff[WWID_SIZE] = {0};
639
640         pp = find_path_by_dev(vecs->pathvec, devname);
641
642         if (pp) {
643                 condlog(0, "%s: spurious uevent, path already in pathvec, %p",
644                         devname, pp->mpp);
645                 /*
646                  * allow reconfig of orphaned path here
647                  */
648                 if (pp->mpp) return 1;
649         }
650         else {
651                 /*
652                  * get path vital state
653                  */
654                 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
655                       devname, DI_ALL))) {
656                         condlog(0, "%s: failed to store path info", devname);
657                         return 1;
658                 }
659                 pp->checkint = conf->checkint;
660         }
661
662         /*
663          * need path UID to go any further
664          */
665         if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
666                 condlog(0, "%s: failed to get path uid", devname);
667                 return 1; /* leave path added to pathvec */
668         }
669
670         mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
671 rescan:
672         if (mpp) {
673                 if (adopt_paths(vecs->pathvec, mpp))
674                         return 1; /* leave path added to pathvec */
675
676                 verify_paths(mpp, vecs, NULL);
677                 mpp->action = ACT_RELOAD;
678         }
679         else {
680                 if ((mpp = add_map_with_path(vecs, pp, 1)))
681                         mpp->action = ACT_CREATE;
682                 else
683                         return 1; /* leave path added to pathvec */
684         }
685
686         /*
687          * push the map to the device-mapper
688          */
689         if (setup_map(mpp)) {
690                 condlog(0, "%s: failed to setup map for addition of new "
691                         "path %s", mpp->alias, devname);
692                 goto out;
693         }
694         /*
695          * reload the map for the multipath mapped device
696          */
697         if (domap(mpp) <= 0) {
698                 condlog(0, "%s: failed in domap for addition of new "
699                         "path %s", mpp->alias, devname);
700                 /*
701                  * deal with asynchronous uevents :((
702                  */
703                 if (mpp->action == ACT_RELOAD) {
704                         condlog(0, "%s: uev_add_path sleep", mpp->alias);
705                         sleep(1);
706                         update_mpp_paths(mpp, vecs->pathvec);
707                         goto rescan;
708                 }
709                 else
710                         goto out;
711         }
712         dm_lib_release();
713
714         /*
715          * update our state from kernel regardless of create or reload
716          */
717         if (setup_multipath(vecs, mpp))
718                 goto out;
719
720         if (mpp->action == ACT_CREATE &&
721             start_waiter_thread(mpp, vecs))
722                         goto out;
723
724         condlog(3, "%s path added to devmap %s", devname, mpp->alias);
725         return 0;
726
727 out:
728         remove_map(mpp, vecs, NULL, 1);
729         return 1;
730 }
731
732 static int
733 uev_remove_path (char * devname, struct vectors * vecs)
734 {
735         condlog(2, "%s: remove path (uevent)", devname);
736         return ev_remove_path(devname, vecs);
737 }
738
739 int
740 ev_remove_path (char * devname, struct vectors * vecs)
741 {
742         struct multipath * mpp;
743         struct path * pp;
744         int i;
745         int rm_path = 1;
746
747         pp = find_path_by_dev(vecs->pathvec, devname);
748
749         if (!pp) {
750                 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
751                 return 1;
752         }
753
754         /*
755          * avoid referring to the map of an orphanned path
756          */
757         if ((mpp = pp->mpp)) {
758
759                 /*
760                  * remove the map IFF removing the last path
761                  */
762                 if (pathcount(mpp, PATH_WILD) > 1) {
763                         vector rpvec = vector_alloc();
764
765                         /*
766                          * transform the mp->pg vector of vectors of paths
767                          * into a mp->params string to feed the device-mapper
768                          */
769                         update_mpp_paths(mpp, vecs->pathvec);
770                         if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
771                                 vector_del_slot(mpp->paths, i);
772
773                         if (VECTOR_SIZE(mpp->paths) == 0) {
774                                 char alias[WWID_SIZE];
775
776                                 /*
777                                  * flush_map will fail if the device is open
778                                  */
779                                 strncpy(alias, mpp->alias, WWID_SIZE);
780                                 if (flush_map(mpp, vecs))
781                                         rm_path = 0;
782                                 else
783                                         condlog(3, "%s: removed map after removing"
784                                                 " multiple paths", alias);
785                         }
786                         else {
787                                 if (setup_map(mpp)) {
788                                         condlog(0, "%s: failed to setup map for"
789                                                 " removal of path %s", mpp->alias, devname);
790                                         free_pathvec(rpvec, KEEP_PATHS);
791                                         goto out;
792                                 }
793                                 /*
794                                  * reload the map
795                                  */
796                                 mpp->action = ACT_RELOAD;
797                                 if (domap(mpp) <= 0) {
798                                         condlog(0, "%s: failed in domap for "
799                                                 "removal of path %s",
800                                                 mpp->alias, devname);
801                                         /*
802                                          * Delete path from pathvec so that
803                                          * update_mpp_paths wont find it later
804                                          * when/if another path is removed.
805                                          */
806                                         if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
807                                                 vector_del_slot(vecs->pathvec, i);
808                                         free_path(pp);
809                                         return 1;
810                                 }
811                                 /*
812                                  * update our state from kernel
813                                  */
814                                 if (setup_multipath(vecs, mpp)) {
815                                         free_pathvec(rpvec, KEEP_PATHS);
816                                         goto out;
817                                 }
818
819                                 condlog(3, "%s path removed from devmap %s",
820                                         devname, mpp->alias);
821                         }
822                         free_pathvec(rpvec, KEEP_PATHS);
823                 }
824                 else {
825                         char alias[WWID_SIZE];
826
827                         /*
828                          * flush_map will fail if the device is open
829                          */
830                         strncpy(alias, mpp->alias, WWID_SIZE);
831                         if (flush_map(mpp, vecs))
832                                 rm_path = 0;
833                         else
834                                 condlog(3, "%s: removed map", alias);
835                 }
836         }
837
838         if (rm_path) {
839                 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
840                         vector_del_slot(vecs->pathvec, i);
841                 free_path(pp);
842         }
843
844         return 0;
845
846 out:
847         remove_map(mpp, vecs, stop_waiter_thread, 1);
848         return 1;
849 }
850
851 static int
852 map_discovery (struct vectors * vecs)
853 {
854         int i;
855         struct multipath * mpp;
856
857         if (dm_get_maps(vecs->mpvec, "multipath"))
858                 return 1;
859
860         vector_foreach_slot (vecs->mpvec, mpp, i)
861                 if (setup_multipath(vecs, mpp))
862                         return 1;
863
864         return 0;
865 }
866
867 int
868 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
869 {
870         struct vectors * vecs;
871         int r;
872         
873         *reply = NULL;
874         *len = 0;
875         vecs = (struct vectors *)trigger_data;
876
877         pthread_cleanup_push(cleanup_lock, vecs->lock);
878         lock(vecs->lock);
879
880         r = parse_cmd(str, reply, len, vecs);
881
882         if (r > 0) {
883                 *reply = STRDUP("fail\n");
884                 *len = strlen(*reply) + 1;
885                 r = 1;
886         }
887         else if (!r && *len == 0) {
888                 *reply = STRDUP("ok\n");
889                 *len = strlen(*reply) + 1;
890                 r = 0;
891         }
892         /* else if (r < 0) leave *reply alone */
893
894         lock_cleanup_pop(vecs->lock);
895         return r;
896 }
897
898 static int
899 uev_discard(char * devpath)
900 {
901         char a[10], b[10];
902
903         /*
904          * keep only block devices, discard partitions
905          */
906         if (sscanf(devpath, "/block/%10s", a) != 1 ||
907             sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
908                 condlog(4, "discard event on %s", devpath);
909                 return 1;
910         }
911         return 0;
912 }
913
914 int 
915 uev_trigger (struct uevent * uev, void * trigger_data)
916 {
917         int r = 0;
918         char devname[32];
919         struct vectors * vecs;
920
921         vecs = (struct vectors *)trigger_data;
922
923         if (uev_discard(uev->devpath))
924                 return 0;
925
926         basename(uev->devpath, devname);
927         lock(vecs->lock);
928
929         /*
930          * device map add/remove event
931          */
932         if (!strncmp(devname, "dm-", 3)) {
933                 if (!strncmp(uev->action, "add", 3)) {
934                         r = uev_add_map(devname, vecs);
935                         goto out;
936                 }
937                 if (!strncmp(uev->action, "remove", 6)) {
938                         r = uev_remove_map(devname, vecs);
939                         goto out;
940                 }
941                 if (!strncmp(uev->action, "umount", 6)) {
942                         r = uev_umount_map(devname, vecs);
943                         goto out;
944                 }
945                 goto out;
946         }
947         
948         /*
949          * path add/remove event
950          */
951         if (blacklist(conf->blist_devnode, devname))
952                 goto out;
953
954         if (!strncmp(uev->action, "add", 3)) {
955                 r = uev_add_path(devname, vecs);
956                 goto out;
957         }
958         if (!strncmp(uev->action, "remove", 6)) {
959                 r = uev_remove_path(devname, vecs);
960                 goto out;
961         }
962
963 out:
964         unlock(vecs->lock);
965         return r;
966 }
967
968 static void *
969 ueventloop (void * ap)
970 {
971         if (uevent_listen(&uev_trigger, ap))
972                 fprintf(stderr, "error starting uevent listener");
973                 
974         return NULL;
975 }
976
977 static void *
978 uxlsnrloop (void * ap)
979 {
980         if (load_keys())
981                 return NULL;
982         
983         if (alloc_handlers())
984                 return NULL;
985
986         add_handler(LIST+PATHS, cli_list_paths);
987         add_handler(LIST+MAPS, cli_list_maps);
988         add_handler(LIST+MAPS+STATUS, cli_list_maps_status);
989         add_handler(LIST+MAPS+STATS, cli_list_maps_stats);
990         add_handler(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
991         add_handler(LIST+TOPOLOGY, cli_list_maps_topology);
992         add_handler(LIST+MAP+TOPOLOGY, cli_list_map_topology);
993         add_handler(LIST+CONFIG, cli_list_config);
994         add_handler(ADD+PATH, cli_add_path);
995         add_handler(DEL+PATH, cli_del_path);
996         add_handler(ADD+MAP, cli_add_map);
997         add_handler(DEL+MAP, cli_del_map);
998         add_handler(SWITCH+MAP+GROUP, cli_switch_group);
999         add_handler(RECONFIGURE, cli_reconfigure);
1000         add_handler(SUSPEND+MAP, cli_suspend);
1001         add_handler(RESUME+MAP, cli_resume);
1002         add_handler(REINSTATE+PATH, cli_reinstate);
1003         add_handler(FAIL+PATH, cli_fail);
1004
1005         uxsock_listen(&uxsock_trigger, ap);
1006
1007         return NULL;
1008 }
1009
1010 static int
1011 exit_daemon (int status)
1012 {
1013         if (status != 0)
1014                 fprintf(stderr, "bad exit status. see daemon.log\n");
1015
1016         condlog(3, "unlink pidfile");
1017         unlink(DEFAULT_PIDFILE);
1018
1019         lock(&exit_mutex);
1020         pthread_cond_signal(&exit_cond);
1021         unlock(&exit_mutex);
1022
1023         return status;
1024 }
1025
1026 static void
1027 fail_path (struct path * pp)
1028 {
1029         if (!pp->mpp)
1030                 return;
1031
1032         condlog(2, "checker failed path %s in map %s",
1033                  pp->dev_t, pp->mpp->alias);
1034
1035         dm_fail_path(pp->mpp->alias, pp->dev_t);
1036         update_queue_mode_del_path(pp->mpp);
1037 }
1038
1039 /*
1040  * caller must have locked the path list before calling that function
1041  */
1042 static void
1043 reinstate_path (struct path * pp)
1044 {
1045         if (!pp->mpp)
1046                 return;
1047
1048         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
1049                 condlog(0, "%s: reinstate failed", pp->dev_t);
1050         else {
1051                 condlog(2, "%s: reinstated", pp->dev_t);
1052                 update_queue_mode_add_path(pp->mpp);
1053         }
1054 }
1055
1056 static void
1057 enable_group(struct path * pp)
1058 {
1059         struct pathgroup * pgp;
1060
1061         /*
1062          * if path is added through uev_add_path, pgindex can be unset.
1063          * next update_strings() will set it, upon map reload event.
1064          *
1065          * we can safely return here, because upon map reload, all
1066          * PG will be enabled.
1067          */
1068         if (!pp->mpp->pg || !pp->pgindex)
1069                 return;
1070
1071         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
1072         
1073         if (pgp->status == PGSTATE_DISABLED) {
1074                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
1075                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
1076         }
1077 }
1078
1079 static void
1080 mpvec_garbage_collector (struct vectors * vecs)
1081 {
1082         struct multipath * mpp;
1083         int i;
1084
1085         vector_foreach_slot (vecs->mpvec, mpp, i) {
1086                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
1087                         condlog(2, "%s: remove dead map", mpp->alias);
1088                         remove_map(mpp, vecs, stop_waiter_thread, 1);
1089                         i--;
1090                 }
1091         }
1092 }
1093
1094 static void
1095 defered_failback_tick (vector mpvec)
1096 {
1097         struct multipath * mpp;
1098         int i;
1099
1100         vector_foreach_slot (mpvec, mpp, i) {
1101                 /*
1102                  * defered failback getting sooner
1103                  */
1104                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
1105                         mpp->failback_tick--;
1106
1107                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
1108                                 switch_pathgroup(mpp);
1109                 }
1110         }
1111 }
1112
1113 static void
1114 retry_count_tick(vector mpvec)
1115 {
1116         struct multipath *mpp;
1117         int i;
1118
1119         vector_foreach_slot (mpvec, mpp, i) {
1120                 if (mpp->retry_tick) {
1121                         mpp->stat_total_queueing_time++;
1122                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
1123                         if(--mpp->retry_tick == 0) {
1124                                 dm_queue_if_no_path(mpp->alias, 0);
1125                                 condlog(2, "%s: Disable queueing", mpp->alias);
1126                         }
1127                 }
1128         }
1129 }
1130
1131 static void *
1132 checkerloop (void *ap)
1133 {
1134         struct vectors *vecs;
1135         struct path *pp;
1136         int i, count = 0;
1137         int newstate;
1138         char checker_msg[MAX_CHECKER_MSG_SIZE];
1139
1140         mlockall(MCL_CURRENT | MCL_FUTURE);
1141
1142         memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
1143         vecs = (struct vectors *)ap;
1144
1145         condlog(2, "path checkers start up");
1146
1147         /*
1148          * init the path check interval
1149          */
1150         vector_foreach_slot (vecs->pathvec, pp, i) {
1151                 pp->checkint = conf->checkint;
1152         }
1153
1154         while (1) {
1155                 pthread_cleanup_push(cleanup_lock, vecs->lock);
1156                 lock(vecs->lock);
1157                 condlog(4, "tick");
1158
1159                 vector_foreach_slot (vecs->pathvec, pp, i) {
1160                         if (!pp->mpp)
1161                                 continue;
1162
1163                         if (pp->tick && --pp->tick)
1164                                 continue; /* don't check this path yet */
1165
1166                         /*
1167                          * provision a next check soonest,
1168                          * in case we exit abnormaly from here
1169                          */
1170                         pp->tick = conf->checkint;
1171                         
1172                         if (!pp->checkfn) {
1173                                 pathinfo(pp, conf->hwtable, DI_SYSFS);
1174                                 select_checkfn(pp);
1175                         }
1176
1177                         if (!pp->checkfn) {
1178                                 condlog(0, "%s: checkfn is void", pp->dev);
1179                                 continue;
1180                         }
1181                         newstate = pp->checkfn(pp->fd, checker_msg,
1182                                                &pp->checker_context);
1183                         
1184                         if (newstate < 0) {
1185                                 condlog(2, "%s: unusable path", pp->dev);
1186                                 pathinfo(pp, conf->hwtable, 0);
1187                                 continue;
1188                         }
1189
1190                         if (newstate != pp->state) {
1191                                 pp->state = newstate;
1192                                 LOG_MSG(1, checker_msg);
1193
1194                                 /*
1195                                  * upon state change, reset the checkint
1196                                  * to the shortest delay
1197                                  */
1198                                 pp->checkint = conf->checkint;
1199
1200                                 if (newstate == PATH_DOWN ||
1201                                     newstate == PATH_SHAKY ||
1202                                     update_multipath_strings(pp->mpp,
1203                                                              vecs->pathvec)) {
1204                                         /*
1205                                          * proactively fail path in the DM
1206                                          */
1207                                         fail_path(pp);
1208
1209                                         /*
1210                                          * cancel scheduled failback
1211                                          */
1212                                         pp->mpp->failback_tick = 0;
1213
1214                                         pp->mpp->stat_path_failures++;
1215                                         continue;
1216                                 }
1217
1218                                 /*
1219                                  * reinstate this path
1220                                  */
1221                                 reinstate_path(pp);
1222
1223                                 /*
1224                                  * schedule [defered] failback
1225                                  */
1226                                 if (pp->mpp->pgfailback > 0)
1227                                         pp->mpp->failback_tick =
1228                                                 pp->mpp->pgfailback + 1;
1229                                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1230                                     need_switch_pathgroup(pp->mpp, 1))
1231                                         switch_pathgroup(pp->mpp);
1232
1233                                 /*
1234                                  * if at least one path is up in a group, and
1235                                  * the group is disabled, re-enable it
1236                                  */
1237                                 if (newstate == PATH_UP)
1238                                         enable_group(pp);
1239                         }
1240                         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1241                                 LOG_MSG(4, checker_msg);
1242                                 /*
1243                                  * double the next check delay.
1244                                  * max at conf->max_checkint
1245                                  */
1246                                 if (pp->checkint < (conf->max_checkint / 2))
1247                                         pp->checkint = 2 * pp->checkint;
1248                                 else
1249                                         pp->checkint = conf->max_checkint;
1250
1251                                 pp->tick = pp->checkint;
1252                                 condlog(4, "%s: delay next check %is",
1253                                                 pp->dev_t, pp->tick);
1254
1255                         }
1256                         pp->state = newstate;
1257
1258                         /*
1259                          * path prio refreshing
1260                          */
1261                         condlog(4, "path prio refresh");
1262                         pathinfo(pp, conf->hwtable, DI_PRIO);
1263
1264                         if (need_switch_pathgroup(pp->mpp, 0)) {
1265                                 if (pp->mpp->pgfailback > 0)
1266                                         pp->mpp->failback_tick =
1267                                                 pp->mpp->pgfailback + 1;
1268                                 else if (pp->mpp->pgfailback ==
1269                                                 -FAILBACK_IMMEDIATE)
1270                                         switch_pathgroup(pp->mpp);
1271                         }
1272                 }
1273                 defered_failback_tick(vecs->mpvec);
1274                 retry_count_tick(vecs->mpvec);
1275
1276                 if (count)
1277                         count--;
1278                 else {
1279                         condlog(4, "map garbage collection");
1280                         mpvec_garbage_collector(vecs);
1281                         count = MAPGCINT;
1282                 }
1283                 
1284                 lock_cleanup_pop(vecs->lock);
1285                 sleep(1);
1286         }
1287         return NULL;
1288 }
1289
1290 int
1291 configure (struct vectors * vecs, int start_waiters)
1292 {
1293         struct multipath * mpp;
1294         struct path * pp;
1295         vector mpvec;
1296         int i;
1297
1298         if (!(vecs->pathvec = vector_alloc()))
1299                 return 1;
1300         
1301         if (!(vecs->mpvec = vector_alloc()))
1302                 return 1;
1303         
1304         if (!(mpvec = vector_alloc()))
1305                 return 1;
1306
1307         /*
1308          * probe for current path (from sysfs) and map (from dm) sets
1309          */
1310         path_discovery(vecs->pathvec, conf, DI_ALL);
1311
1312         vector_foreach_slot (vecs->pathvec, pp, i)
1313                 pp->checkint = conf->checkint;
1314
1315         if (map_discovery(vecs))
1316                 return 1;
1317
1318         /*
1319          * create new set of maps & push changed ones into dm
1320          */
1321         if (coalesce_paths(vecs, mpvec, NULL))
1322                 return 1;
1323
1324         /*
1325          * may need to remove some maps which are no longer relevant
1326          * e.g., due to blacklist changes in conf file
1327          */
1328         if (coalesce_maps(vecs, mpvec))
1329                 return 1;
1330
1331         dm_lib_release();
1332
1333         if (conf->verbosity > 2)
1334                 vector_foreach_slot(mpvec, mpp, i)
1335                         print_map(mpp);
1336
1337         /*
1338          * purge dm of old maps
1339          */
1340         remove_maps(vecs, NULL);
1341
1342         /*
1343          * save new set of maps formed by considering current path state
1344          */
1345         vecs->mpvec = mpvec;
1346
1347         /*
1348          * start dm event waiter threads for these new maps
1349          */
1350         vector_foreach_slot(vecs->mpvec, mpp, i) {
1351                 if (setup_multipath(vecs, mpp))
1352                         return 1;
1353                 if (start_waiters)
1354                         if (start_waiter_thread(mpp, vecs))
1355                                 return 1;
1356         }
1357         return 0;
1358 }
1359
1360 int
1361 reconfigure (struct vectors * vecs)
1362 {
1363         struct config * old = conf;
1364
1365         /*
1366          * free old map and path vectors ... they use old conf state
1367          */
1368         if (VECTOR_SIZE(vecs->mpvec))
1369                 remove_maps(vecs, stop_waiter_thread);
1370
1371         if (VECTOR_SIZE(vecs->pathvec))
1372                 free_pathvec(vecs->pathvec, FREE_PATHS);
1373
1374         conf = NULL;
1375
1376         if (load_config(DEFAULT_CONFIGFILE))
1377                 return 1;
1378
1379         conf->verbosity = old->verbosity;
1380
1381         if (!conf->checkint) {
1382                 conf->checkint = DEFAULT_CHECKINT;
1383                 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1384         }
1385         configure(vecs, 1);
1386         free_config(old);
1387         return 0;
1388 }
1389
1390 static struct vectors *
1391 init_vecs (void)
1392 {
1393         struct vectors * vecs;
1394
1395         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1396
1397         if (!vecs)
1398                 return NULL;
1399
1400         vecs->lock = 
1401                 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1402
1403         if (!vecs->lock)
1404                 goto out;
1405
1406         vecs->pathvec = vector_alloc();
1407
1408         if (!vecs->pathvec)
1409                 goto out1;
1410                 
1411         vecs->mpvec = vector_alloc();
1412
1413         if (!vecs->mpvec)
1414                 goto out2;
1415         
1416         pthread_mutex_init(vecs->lock, NULL);
1417
1418         return vecs;
1419
1420 out2:
1421         vector_free(vecs->pathvec);
1422 out1:
1423         FREE(vecs->lock);
1424 out:
1425         FREE(vecs);
1426         condlog(0, "failed to init paths");
1427         return NULL;
1428 }
1429
1430 static void *
1431 signal_set(int signo, void (*func) (int))
1432 {
1433         int r;
1434         struct sigaction sig;
1435         struct sigaction osig;
1436
1437         sig.sa_handler = func;
1438         sigemptyset(&sig.sa_mask);
1439         sig.sa_flags = 0;
1440
1441         r = sigaction(signo, &sig, &osig);
1442
1443         if (r < 0)
1444                 return (SIG_ERR);
1445         else
1446                 return (osig.sa_handler);
1447 }
1448
1449 static void
1450 sighup (int sig)
1451 {
1452         condlog(2, "reconfigure (SIGHUP)");
1453
1454         lock(gvecs->lock);
1455         reconfigure(gvecs);
1456         unlock(gvecs->lock);
1457
1458 #ifdef _DEBUG_
1459         dbg_free_final(NULL);
1460 #endif
1461 }
1462
1463 static void
1464 sigend (int sig)
1465 {
1466         exit_daemon(0);
1467 }
1468
1469 static void
1470 sigusr1 (int sig)
1471 {
1472         condlog(3, "SIGUSR1 received");
1473 }
1474
1475 static void
1476 signal_init(void)
1477 {
1478         signal_set(SIGHUP, sighup);
1479         signal_set(SIGUSR1, sigusr1);
1480         signal_set(SIGINT, sigend);
1481         signal_set(SIGTERM, sigend);
1482         signal_set(SIGKILL, sigend);
1483 }
1484
1485 static void
1486 setscheduler (void)
1487 {
1488         int res;
1489         static struct sched_param sched_param = {
1490                 sched_priority: 99
1491         };
1492
1493         res = sched_setscheduler (0, SCHED_RR, &sched_param);
1494
1495         if (res == -1)
1496                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1497         return;
1498 }
1499
1500 static void
1501 set_oom_adj (int val)
1502 {
1503         FILE *fp;
1504
1505         fp = fopen("/proc/self/oom_adj", "w");
1506
1507         if (!fp)
1508                 return;
1509
1510         fprintf(fp, "%i", val);
1511         fclose(fp);
1512 }
1513         
1514 static int
1515 child (void * param)
1516 {
1517         pthread_t check_thr, uevent_thr, uxlsnr_thr;
1518         pthread_attr_t attr;
1519         struct vectors * vecs;
1520
1521         mlockall(MCL_CURRENT | MCL_FUTURE);
1522
1523         if (logsink)
1524                 log_thread_start();
1525
1526         condlog(2, "--------start up--------");
1527         condlog(2, "read " DEFAULT_CONFIGFILE);
1528
1529         if (load_config(DEFAULT_CONFIGFILE))
1530                 exit(1);
1531
1532         setlogmask(LOG_UPTO(conf->verbosity + 3));
1533
1534         /*
1535          * fill the voids left in the config file
1536          */
1537         if (!conf->checkint) {
1538                 conf->checkint = DEFAULT_CHECKINT;
1539                 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1540         }
1541
1542         if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1543                 if (logsink)
1544                         log_thread_stop();
1545
1546                 exit(1);
1547         }
1548         signal_init();
1549         setscheduler();
1550         set_oom_adj(-16);
1551         vecs = gvecs = init_vecs();
1552
1553         if (!vecs)
1554                 exit(1);
1555
1556         if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
1557                 condlog(0, "can not find sysfs mount point");
1558                 exit(1);
1559         }
1560
1561         /*
1562          * fetch and configure both paths and multipaths
1563          */
1564         if (configure(vecs, 1)) {
1565                 condlog(0, "failure during configuration");
1566                 exit(1);
1567         }
1568
1569         /*
1570          * start threads
1571          */
1572         pthread_attr_init(&attr);
1573         pthread_attr_setstacksize(&attr, 64 * 1024);
1574         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1575         
1576         pthread_create(&check_thr, &attr, checkerloop, vecs);
1577         pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1578         pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1579
1580         pthread_cond_wait(&exit_cond, &exit_mutex);
1581
1582         /*
1583          * exit path
1584          */
1585         lock(vecs->lock);
1586         remove_maps(vecs, stop_waiter_thread);
1587         free_pathvec(vecs->pathvec, FREE_PATHS);
1588
1589         pthread_cancel(check_thr);
1590         pthread_cancel(uevent_thr);
1591         pthread_cancel(uxlsnr_thr);
1592
1593         free_keys(keys);
1594         keys = NULL;
1595         free_handlers(handlers);
1596         handlers = NULL;
1597         free_polls();
1598
1599         unlock(vecs->lock);
1600         pthread_mutex_destroy(vecs->lock);
1601         FREE(vecs->lock);
1602         vecs->lock = NULL;
1603         FREE(vecs);
1604         vecs = NULL;
1605         free_config(conf);
1606         conf = NULL;
1607
1608         condlog(2, "--------shut down-------");
1609         
1610         if (logsink)
1611                 log_thread_stop();
1612
1613         dm_lib_release();
1614         dm_lib_exit();
1615
1616 #ifdef _DEBUG_
1617         dbg_free_final(NULL);
1618 #endif
1619
1620         exit(0);
1621 }
1622
1623 static int
1624 daemonize(void)
1625 {
1626         int pid;
1627         int in_fd, out_fd;
1628
1629         if( (pid = fork()) < 0){
1630                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1631                 return -1;
1632         }
1633         else if (pid != 0)
1634                 return pid;
1635
1636         setsid();
1637
1638         if ( (pid = fork()) < 0)
1639                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1640         else if (pid != 0)
1641                 _exit(0);
1642
1643         in_fd = open("/dev/null", O_RDONLY);
1644         if (in_fd < 0){
1645                 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1646                         strerror(errno));
1647                 _exit(0);
1648         }
1649         out_fd = open("/dev/console", O_WRONLY);
1650         if (out_fd < 0){
1651                 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1652                         strerror(errno));
1653                 _exit(0);
1654         }
1655
1656         close(STDIN_FILENO);
1657         dup(in_fd);
1658         close(STDOUT_FILENO);
1659         dup(out_fd);
1660         close(STDERR_FILENO);
1661         dup(out_fd);
1662
1663         close(in_fd);
1664         close(out_fd);
1665         chdir("/");
1666         umask(0);
1667         return 0;
1668 }
1669
1670 int
1671 main (int argc, char *argv[])
1672 {
1673         extern char *optarg;
1674         extern int optind;
1675         int arg;
1676         int err;
1677         
1678         logsink = 1;
1679
1680         if (getuid() != 0) {
1681                 fprintf(stderr, "need to be root\n");
1682                 exit(1);
1683         }
1684
1685         /* make sure we don't lock any path */
1686         chdir("/");
1687         umask(umask(077) | 022);
1688
1689         conf = alloc_config();
1690
1691         if (!conf)
1692                 exit(1);
1693
1694         while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1695         switch(arg) {
1696                 case 'd':
1697                         logsink = 0;
1698                         //debug=1; /* ### comment me out ### */
1699                         break;
1700                 case 'v':
1701                         if (sizeof(optarg) > sizeof(char *) ||
1702                             !isdigit(optarg[0]))
1703                                 exit(1);
1704
1705                         conf->verbosity = atoi(optarg);
1706                         break;
1707                 case 'k':
1708                         uxclnt(optarg);
1709                         exit(0);
1710                 default:
1711                         ;
1712                 }
1713         }
1714
1715         if (!logsink)
1716                 err = 0;
1717         else
1718                 err = daemonize();
1719         
1720         if (err < 0)
1721                 /* error */
1722                 exit(1);
1723         else if (err > 0)
1724                 /* parent dies */
1725                 exit(0);
1726         else
1727                 /* child lives */
1728                 return (child(NULL));
1729 }
1730