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