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