Latest git -master is not compilable
[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 #include <sys/time.h>
16 #include <sys/resource.h>
17 #include <limits.h>
18
19 /*
20  * libcheckers
21  */
22 #include <checkers.h>
23
24 /*
25  * libmultipath
26  */
27 #include <parser.h>
28 #include <vector.h>
29 #include <memory.h>
30 #include <config.h>
31 #include <util.h>
32 #include <hwtable.h>
33 #include <defaults.h>
34 #include <structs.h>
35 #include <callout.h>
36 #include <blacklist.h>
37 #include <structs_vec.h>
38 #include <dmparser.h>
39 #include <devmapper.h>
40 #include <sysfs.h>
41 #include <dict.h>
42 #include <discovery.h>
43 #include <debug.h>
44 #include <propsel.h>
45 #include <uevent.h>
46 #include <switchgroup.h>
47 #include <print.h>
48 #include <configure.h>
49 #include <prio.h>
50
51 #include "main.h"
52 #include "pidfile.h"
53 #include "uxlsnr.h"
54 #include "uxclnt.h"
55 #include "cli.h"
56 #include "cli_handlers.h"
57 #include "lock.h"
58 #include "waiter.h"
59
60 #define FILE_NAME_SIZE 256
61 #define CMDSIZE 160
62
63 #define LOG_MSG(a,b) \
64         if (strlen(b)) condlog(a, "%s: %s", pp->dev, b);
65
66 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
67 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
68
69 int logsink;
70
71 /*
72  * global copy of vecs for use in sig handlers
73  */
74 struct vectors * gvecs;
75
76 static int
77 need_switch_pathgroup (struct multipath * mpp, int refresh)
78 {
79         struct pathgroup * pgp;
80         struct path * pp;
81         unsigned int i, j;
82
83         if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
84                 return 0;
85
86         /*
87          * Refresh path priority values
88          */
89         if (refresh)
90                 vector_foreach_slot (mpp->pg, pgp, i)
91                         vector_foreach_slot (pgp->paths, pp, j)
92                                 pathinfo(pp, conf->hwtable, DI_PRIO);
93
94         mpp->bestpg = select_path_group(mpp);
95
96         if (mpp->bestpg != mpp->nextpg)
97                 return 1;
98
99         return 0;
100 }
101
102 static void
103 switch_pathgroup (struct multipath * mpp)
104 {
105         mpp->stat_switchgroup++;
106         dm_switchgroup(mpp->alias, mpp->bestpg);
107         condlog(2, "%s: switch to path group #%i",
108                  mpp->alias, mpp->bestpg);
109 }
110
111 static int
112 coalesce_maps(struct vectors *vecs, vector nmpv)
113 {
114         struct multipath * ompp;
115         vector ompv = vecs->mpvec;
116         unsigned int i;
117         int j;
118
119         vector_foreach_slot (ompv, ompp, i) {
120                 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
121                         /*
122                          * remove all current maps not allowed by the
123                          * current configuration
124                          */
125                         if (dm_flush_map(ompp->alias)) {
126                                 condlog(0, "%s: unable to flush devmap",
127                                         ompp->alias);
128                                 /*
129                                  * may be just because the device is open
130                                  */
131                                 if (!vector_alloc_slot(nmpv))
132                                         return 1;
133
134                                 vector_set_slot(nmpv, ompp);
135                                 setup_multipath(vecs, ompp);
136
137                                 if ((j = find_slot(ompv, (void *)ompp)) != -1)
138                                         vector_del_slot(ompv, j);
139
140                                 continue;
141                         }
142                         else {
143                                 dm_lib_release();
144                                 condlog(2, "%s devmap removed", ompp->alias);
145                         }
146                 }
147         }
148         return 0;
149 }
150
151 void
152 sync_map_state(struct multipath *mpp)
153 {
154         struct pathgroup *pgp;
155         struct path *pp;
156         unsigned int i, j;
157
158         if (!mpp->pg)
159                 return;
160
161         vector_foreach_slot (mpp->pg, pgp, i){
162                 vector_foreach_slot (pgp->paths, pp, j){
163                         if (pp->state == PATH_UNCHECKED || 
164                             pp->state == PATH_WILD)
165                                 continue;
166                         if ((pp->dmstate == PSTATE_FAILED ||
167                              pp->dmstate == PSTATE_UNDEF) &&
168                             (pp->state == PATH_UP || pp->state == PATH_GHOST))
169                                 dm_reinstate_path(mpp->alias, pp->dev_t);
170                         else if ((pp->dmstate == PSTATE_ACTIVE ||
171                                   pp->dmstate == PSTATE_UNDEF) &&
172                                  (pp->state == PATH_DOWN ||
173                                   pp->state == PATH_SHAKY))
174                                 dm_fail_path(mpp->alias, pp->dev_t);
175                 }
176         }
177 }
178
179 static void
180 sync_maps_state(vector mpvec)
181 {
182         unsigned int i;
183         struct multipath *mpp;
184
185         vector_foreach_slot (mpvec, mpp, i)
186                 sync_map_state(mpp);
187 }
188
189 static int
190 flush_map(struct multipath * mpp, struct vectors * vecs)
191 {
192         /*
193          * clear references to this map before flushing so we can ignore
194          * the spurious uevent we may generate with the dm_flush_map call below
195          */
196         if (dm_flush_map(mpp->alias)) {
197                 /*
198                  * May not really be an error -- if the map was already flushed
199                  * from the device mapper by dmsetup(8) for instance.
200                  */
201                 condlog(0, "%s: can't flush", mpp->alias);
202                 return 1;
203         }
204         else {
205                 dm_lib_release();
206                 condlog(2, "%s: devmap removed", mpp->alias);
207         }
208
209         orphan_paths(vecs->pathvec, mpp);
210         remove_map_and_stop_waiter(mpp, vecs, 1);
211
212         return 0;
213 }
214
215 static int
216 uev_add_map (struct sysfs_device * dev, struct vectors * vecs)
217 {
218         condlog(2, "%s: add map (uevent)", dev->kernel);
219         return ev_add_map(dev, vecs);
220 }
221
222 int
223 ev_add_map (struct sysfs_device * dev, struct vectors * vecs)
224 {
225         char * alias;
226         char *dev_t;
227         int major, minor;
228         char * refwwid;
229         struct multipath * mpp;
230         int map_present;
231         int r = 1;
232
233         dev_t = sysfs_attr_get_value(dev->devpath, "dev");
234
235         if (!dev_t || sscanf(dev_t, "%d:%d", &major, &minor) != 2)
236                 return 1;
237
238         alias = dm_mapname(major, minor);
239
240         if (!alias)
241                 return 1;
242
243         map_present = dm_map_present(alias);
244
245         if (map_present && dm_type(alias, TGT_MPATH) <= 0) {
246                 condlog(4, "%s: not a multipath map", alias);
247                 FREE(alias);
248                 return 0;
249         }
250
251         mpp = find_mp_by_alias(vecs->mpvec, alias);
252
253         if (mpp) {
254                 /*
255                  * Not really an error -- we generate our own uevent
256                  * if we create a multipath mapped device as a result
257                  * of uev_add_path
258                  */
259                 condlog(0, "%s: devmap already registered",
260                         dev->kernel);
261                 FREE(alias);
262                 return 0;
263         }
264
265         /*
266          * now we can register the map
267          */
268         if (map_present && (mpp = add_map_without_path(vecs, minor, alias))) {
269                 sync_map_state(mpp);
270                 condlog(2, "%s: devmap %s added", alias, dev->kernel);
271                 return 0;
272         }
273         refwwid = get_refwwid(dev->kernel, DEV_DEVMAP, vecs->pathvec);
274
275         if (refwwid) {
276                 r = coalesce_paths(vecs, NULL, refwwid, 0);
277                 dm_lib_release();
278         }
279
280         if (!r)
281                 condlog(2, "%s: devmap %s added", alias, dev->kernel);
282         else
283                 condlog(0, "%s: uev_add_map %s failed", alias, dev->kernel);
284
285         FREE(refwwid);
286         FREE(alias);
287         return r;
288 }
289
290 static int
291 uev_remove_map (struct sysfs_device * dev, struct vectors * vecs)
292 {
293         condlog(2, "%s: remove map (uevent)", dev->kernel);
294         return ev_remove_map(dev->kernel, vecs);
295 }
296
297 int
298 ev_remove_map (char * devname, struct vectors * vecs)
299 {
300         struct multipath * mpp;
301
302         mpp = find_mp_by_str(vecs->mpvec, devname);
303
304         if (!mpp) {
305                 condlog(2, "%s: devmap not registered, can't remove",
306                         devname);
307                 return 0;
308         }
309         flush_map(mpp, vecs);
310
311         return 0;
312 }
313
314 static int
315 uev_umount_map (struct sysfs_device * dev, struct vectors * vecs)
316 {
317         struct multipath * mpp;
318
319         condlog(2, "%s: umount map (uevent)", dev->kernel);
320
321         mpp = find_mp_by_str(vecs->mpvec, dev->kernel);
322
323         if (!mpp)
324                 return 0;
325
326         update_mpp_paths(mpp, vecs->pathvec);
327         verify_paths(mpp, vecs, NULL);
328
329         if (!VECTOR_SIZE(mpp->paths))
330                 flush_map(mpp, vecs);
331
332         return 0;
333 }
334
335 static int
336 uev_add_path (struct sysfs_device * dev, struct vectors * vecs)
337 {
338         condlog(2, "%s: add path (uevent)", dev->kernel);
339         return (ev_add_path(dev->kernel, vecs) != 1)? 0 : 1;
340 }
341
342
343 /*
344  * returns:
345  * 0: added
346  * 1: error
347  * 2: blacklisted
348  */
349 int
350 ev_add_path (char * devname, struct vectors * vecs)
351 {
352         struct multipath * mpp;
353         struct path * pp;
354         char empty_buff[WWID_SIZE] = {0};
355
356         if (strstr(devname, "..") != NULL) {
357                 /*
358                  * Don't allow relative device names in the pathvec
359                  */
360                 condlog(0, "%s: path name is invalid", devname);
361                 return 1;
362         }
363
364         pp = find_path_by_dev(vecs->pathvec, devname);
365
366         if (pp) {
367                 condlog(0, "%s: spurious uevent, path already in pathvec",
368                         devname);
369                 if (pp->mpp)
370                         return 0;
371         }
372         else {
373                 /*
374                  * get path vital state
375                  */
376                 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
377                       devname, DI_ALL))) {
378                         condlog(0, "%s: failed to store path info", devname);
379                         return 1;
380                 }
381                 pp->checkint = conf->checkint;
382         }
383
384         /*
385          * need path UID to go any further
386          */
387         if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
388                 condlog(0, "%s: failed to get path uid", devname);
389                 return 1; /* leave path added to pathvec */
390         }
391         if (filter_path(conf, pp) > 0){
392                 int i = find_slot(vecs->pathvec, (void *)pp);
393                 if (i != -1)
394                         vector_del_slot(vecs->pathvec, i);
395                 free_path(pp);
396                 return 2;
397         }
398         mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
399 rescan:
400         if (mpp) {
401                 if ((!pp->size) || (mpp->size != pp->size)) {
402                         if (!pp->size)
403                                 condlog(0, "%s: failed to add new path %s, "
404                                         "device size is 0",
405                                         devname, pp->dev);
406                         else
407                                 condlog(0, "%s: failed to add new path %s, "
408                                         "device size mismatch",
409                                         devname, pp->dev);
410                         int i = find_slot(vecs->pathvec, (void *)pp);
411                         if (i != -1)
412                                 vector_del_slot(vecs->pathvec, i);
413                         free_path(pp);
414                         return 1;
415                 }
416
417                 condlog(4,"%s: adopting all paths for path %s",
418                         mpp->alias, pp->dev);
419                 if (adopt_paths(vecs->pathvec, mpp))
420                         return 1; /* leave path added to pathvec */
421
422                 verify_paths(mpp, vecs, NULL);
423                 mpp->flush_on_last_del = FLUSH_UNDEF;
424                 mpp->action = ACT_RELOAD;
425         }
426         else {
427                 if (!pp->size) {
428                         condlog(0, "%s: failed to create new map,"
429                                 " %s device size is 0 ", devname, pp->dev);
430                         int i = find_slot(vecs->pathvec, (void *)pp);
431                         if (i != -1)
432                                 vector_del_slot(vecs->pathvec, i);
433                         free_path(pp);
434                         return 1;
435                 }
436
437                 condlog(4,"%s: creating new map", pp->dev);
438                 if ((mpp = add_map_with_path(vecs, pp, 1)))
439                         mpp->action = ACT_CREATE;
440                 else
441                         return 1; /* leave path added to pathvec */
442         }
443
444         /*
445          * push the map to the device-mapper
446          */
447         if (setup_map(mpp)) {
448                 condlog(0, "%s: failed to setup map for addition of new "
449                         "path %s", mpp->alias, devname);
450                 goto out;
451         }
452         /*
453          * reload the map for the multipath mapped device
454          */
455         if (domap(mpp) <= 0) {
456                 condlog(0, "%s: failed in domap for addition of new "
457                         "path %s", mpp->alias, devname);
458                 /*
459                  * deal with asynchronous uevents :((
460                  */
461                 if (mpp->action == ACT_RELOAD) {
462                         condlog(0, "%s: uev_add_path sleep", mpp->alias);
463                         sleep(1);
464                         update_mpp_paths(mpp, vecs->pathvec);
465                         goto rescan;
466                 }
467                 else
468                         goto out;
469         }
470         dm_lib_release();
471
472         /*
473          * update our state from kernel regardless of create or reload
474          */
475         if (setup_multipath(vecs, mpp))
476                 goto out;
477
478         sync_map_state(mpp);
479
480         if (mpp->action == ACT_CREATE &&
481             start_waiter_thread(mpp, vecs))
482                         goto out;
483
484         condlog(2, "%s path added to devmap %s", devname, mpp->alias);
485         return 0;
486
487 out:
488         remove_map(mpp, vecs, 1);
489         return 1;
490 }
491
492 static int
493 uev_remove_path (struct sysfs_device * dev, struct vectors * vecs)
494 {
495         int retval;
496
497         condlog(2, "%s: remove path (uevent)", dev->kernel);
498         retval = ev_remove_path(dev->kernel, vecs);
499         if (!retval)
500                 sysfs_device_put(dev);
501
502         return retval;
503 }
504
505 int
506 ev_remove_path (char * devname, struct vectors * vecs)
507 {
508         struct multipath * mpp;
509         struct path * pp;
510         int i, retval = 0;
511
512         pp = find_path_by_dev(vecs->pathvec, devname);
513
514         if (!pp) {
515                 /* Not an error; path might have been purged earlier */
516                 condlog(0, "%s: path already removed", devname);
517                 return 0;
518         }
519
520         /*
521          * avoid referring to the map of an orphaned path
522          */
523         if ((mpp = pp->mpp)) {
524                 /*
525                  * transform the mp->pg vector of vectors of paths
526                  * into a mp->params string to feed the device-mapper
527                  */
528                 if (update_mpp_paths(mpp, vecs->pathvec)) {
529                         condlog(0, "%s: failed to update paths",
530                                 mpp->alias);
531                         goto fail;
532                 }
533                 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
534                         vector_del_slot(mpp->paths, i);
535
536                 /*
537                  * remove the map IFF removing the last path
538                  */
539                 if (VECTOR_SIZE(mpp->paths) == 0) {
540                         char alias[WWID_SIZE];
541
542                         /*
543                          * flush_map will fail if the device is open
544                          */
545                         strncpy(alias, mpp->alias, WWID_SIZE);
546                         if (mpp->flush_on_last_del == FLUSH_ENABLED) {
547                                 condlog(2, "%s Last path deleted, disabling queueing", mpp->alias);
548                                 mpp->retry_tick = 0;
549                                 mpp->no_path_retry = NO_PATH_RETRY_FAIL;
550                                 mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
551                                 dm_queue_if_no_path(mpp->alias, 0);
552                         }
553                         if (!flush_map(mpp, vecs)) {
554                                 condlog(2, "%s: removed map after"
555                                         " removing all paths",
556                                         alias);
557                                 retval = 0;
558                                 goto out;
559                         }
560                         /*
561                          * Not an error, continue
562                          */
563                 }
564
565                 if (setup_map(mpp)) {
566                         condlog(0, "%s: failed to setup map for"
567                                 " removal of path %s", mpp->alias,
568                                 devname);
569                         goto fail;
570                 }
571                 /*
572                  * reload the map
573                  */
574                 mpp->action = ACT_RELOAD;
575                 if (domap(mpp) <= 0) {
576                         condlog(0, "%s: failed in domap for "
577                                 "removal of path %s",
578                                 mpp->alias, devname);
579                         retval = 1;
580                 } else {
581                         /*
582                          * update our state from kernel
583                          */
584                         if (setup_multipath(vecs, mpp)) {
585                                 goto fail;
586                         }
587                         sync_map_state(mpp);
588
589                         condlog(2, "%s: path removed from map %s",
590                                 devname, mpp->alias);
591                 }
592         }
593
594 out:
595         if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
596                 vector_del_slot(vecs->pathvec, i);
597
598         free_path(pp);
599
600         return retval;
601
602 fail:
603         remove_map_and_stop_waiter(mpp, vecs, 1);
604         return 1;
605 }
606
607 static int
608 map_discovery (struct vectors * vecs)
609 {
610         struct multipath * mpp;
611         unsigned int i;
612
613         if (dm_get_maps(vecs->mpvec))
614                 return 1;
615
616         vector_foreach_slot (vecs->mpvec, mpp, i)
617                 if (setup_multipath(vecs, mpp))
618                         return 1;
619
620         return 0;
621 }
622
623 int
624 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
625 {
626         struct vectors * vecs;
627         int r;
628
629         *reply = NULL;
630         *len = 0;
631         vecs = (struct vectors *)trigger_data;
632
633         pthread_cleanup_push(cleanup_lock, &vecs->lock);
634         lock(vecs->lock);
635
636         r = parse_cmd(str, reply, len, vecs);
637
638         if (r > 0) {
639                 *reply = STRDUP("fail\n");
640                 *len = strlen(*reply) + 1;
641                 r = 1;
642         }
643         else if (!r && *len == 0) {
644                 *reply = STRDUP("ok\n");
645                 *len = strlen(*reply) + 1;
646                 r = 0;
647         }
648         /* else if (r < 0) leave *reply alone */
649
650         lock_cleanup_pop(vecs->lock);
651         return r;
652 }
653
654 static int
655 uev_discard(char * devpath)
656 {
657         char *tmp;
658         char a[11], b[11];
659
660         /*
661          * keep only block devices, discard partitions
662          */
663         tmp = strstr(devpath, "/block/");
664         if (tmp == NULL){
665                 condlog(4, "no /block/ in '%s'", devpath);
666                 return 1;
667         }
668         if (sscanf(tmp, "/block/%10s", a) != 1 ||
669             sscanf(tmp, "/block/%10[^/]/%10s", a, b) == 2) {
670                 condlog(4, "discard event on %s", devpath);
671                 return 1;
672         }
673         return 0;
674 }
675
676 int
677 uev_trigger (struct uevent * uev, void * trigger_data)
678 {
679         int r = 0;
680         struct sysfs_device *sysdev;
681         struct vectors * vecs;
682
683         vecs = (struct vectors *)trigger_data;
684
685         if (uev_discard(uev->devpath))
686                 return 0;
687
688         sysdev = sysfs_device_get(uev->devpath);
689         if(!sysdev)
690                 return 0;
691
692         lock(vecs->lock);
693
694         /*
695          * device map event
696          * Add events are ignored here as the tables
697          * are not fully initialised then.
698          */
699         if (!strncmp(sysdev->kernel, "dm-", 3)) {
700                 if (!strncmp(uev->action, "change", 6)) {
701                         r = uev_add_map(sysdev, vecs);
702                         goto out;
703                 }
704                 if (!strncmp(uev->action, "remove", 6)) {
705                         r = uev_remove_map(sysdev, vecs);
706                         goto out;
707                 }
708                 if (!strncmp(uev->action, "umount", 6)) {
709                         r = uev_umount_map(sysdev, vecs);
710                         goto out;
711                 }
712                 goto out;
713         }
714
715         /*
716          * path add/remove event
717          */
718         if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
719                            sysdev->kernel) > 0)
720                 goto out;
721
722         if (!strncmp(uev->action, "add", 3)) {
723                 r = uev_add_path(sysdev, vecs);
724                 goto out;
725         }
726         if (!strncmp(uev->action, "remove", 6)) {
727                 r = uev_remove_path(sysdev, vecs);
728                 goto out;
729         }
730
731 out:
732         unlock(vecs->lock);
733         return r;
734 }
735
736 static void *
737 ueventloop (void * ap)
738 {
739         block_signal(SIGUSR1, NULL);
740         block_signal(SIGHUP, NULL);
741
742         if (uevent_listen(&uev_trigger, ap))
743                 fprintf(stderr, "error starting uevent listener");
744
745         return NULL;
746 }
747
748 static void *
749 uxlsnrloop (void * ap)
750 {
751         block_signal(SIGUSR1, NULL);
752         block_signal(SIGHUP, NULL);
753
754         if (cli_init())
755                 return NULL;
756
757         set_handler_callback(LIST+PATHS, cli_list_paths);
758         set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
759         set_handler_callback(LIST+MAPS, cli_list_maps);
760         set_handler_callback(LIST+STATUS, cli_list_status);
761         set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
762         set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
763         set_handler_callback(LIST+MAPS+FMT, cli_list_maps_fmt);
764         set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
765         set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
766         set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
767         set_handler_callback(LIST+CONFIG, cli_list_config);
768         set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
769         set_handler_callback(LIST+DEVICES, cli_list_devices);
770         set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
771         set_handler_callback(ADD+PATH, cli_add_path);
772         set_handler_callback(DEL+PATH, cli_del_path);
773         set_handler_callback(ADD+MAP, cli_add_map);
774         set_handler_callback(DEL+MAP, cli_del_map);
775         set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
776         set_handler_callback(RECONFIGURE, cli_reconfigure);
777         set_handler_callback(SUSPEND+MAP, cli_suspend);
778         set_handler_callback(RESUME+MAP, cli_resume);
779         set_handler_callback(RESIZE+MAP, cli_resize);
780         set_handler_callback(REINSTATE+PATH, cli_reinstate);
781         set_handler_callback(FAIL+PATH, cli_fail);
782         set_handler_callback(DISABLEQ+MAP, cli_disable_queueing);
783         set_handler_callback(RESTOREQ+MAP, cli_restore_queueing);
784         set_handler_callback(DISABLEQ+MAPS, cli_disable_all_queueing);
785         set_handler_callback(RESTOREQ+MAPS, cli_restore_all_queueing);
786         set_handler_callback(QUIT, cli_quit);
787
788         umask(077);
789         uxsock_listen(&uxsock_trigger, ap);
790
791         return NULL;
792 }
793
794 static int
795 exit_daemon (int status)
796 {
797         if (status != 0)
798                 fprintf(stderr, "bad exit status. see daemon.log\n");
799
800         condlog(3, "unlink pidfile");
801         unlink(DEFAULT_PIDFILE);
802
803         pthread_mutex_lock(&exit_mutex);
804         pthread_cond_signal(&exit_cond);
805         pthread_mutex_unlock(&exit_mutex);
806
807         return status;
808 }
809
810 static void
811 fail_path (struct path * pp, int del_active)
812 {
813         if (!pp->mpp)
814                 return;
815
816         condlog(2, "checker failed path %s in map %s",
817                  pp->dev_t, pp->mpp->alias);
818
819         dm_fail_path(pp->mpp->alias, pp->dev_t);
820         if (del_active)
821                 update_queue_mode_del_path(pp->mpp);
822 }
823
824 /*
825  * caller must have locked the path list before calling that function
826  */
827 static void
828 reinstate_path (struct path * pp, int add_active)
829 {
830         if (!pp->mpp)
831                 return;
832
833         if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
834                 condlog(0, "%s: reinstate failed", pp->dev_t);
835         else {
836                 condlog(2, "%s: reinstated", pp->dev_t);
837                 if (add_active)
838                         update_queue_mode_add_path(pp->mpp);
839         }
840 }
841
842 static void
843 enable_group(struct path * pp)
844 {
845         struct pathgroup * pgp;
846
847         /*
848          * if path is added through uev_add_path, pgindex can be unset.
849          * next update_strings() will set it, upon map reload event.
850          *
851          * we can safely return here, because upon map reload, all
852          * PG will be enabled.
853          */
854         if (!pp->mpp->pg || !pp->pgindex)
855                 return;
856
857         pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
858
859         if (pgp->status == PGSTATE_DISABLED) {
860                 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
861                 dm_enablegroup(pp->mpp->alias, pp->pgindex);
862         }
863 }
864
865 static void
866 mpvec_garbage_collector (struct vectors * vecs)
867 {
868         struct multipath * mpp;
869         unsigned int i;
870
871         if (!vecs->mpvec)
872                 return;
873
874         vector_foreach_slot (vecs->mpvec, mpp, i) {
875                 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
876                         condlog(2, "%s: remove dead map", mpp->alias);
877                         remove_map_and_stop_waiter(mpp, vecs, 1);
878                         i--;
879                 }
880         }
881 }
882
883 static void
884 defered_failback_tick (vector mpvec)
885 {
886         struct multipath * mpp;
887         unsigned int i;
888
889         vector_foreach_slot (mpvec, mpp, i) {
890                 /*
891                  * defered failback getting sooner
892                  */
893                 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
894                         mpp->failback_tick--;
895
896                         if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
897                                 switch_pathgroup(mpp);
898                 }
899         }
900 }
901
902 static void
903 retry_count_tick(vector mpvec)
904 {
905         struct multipath *mpp;
906         unsigned int i;
907
908         vector_foreach_slot (mpvec, mpp, i) {
909                 if (mpp->retry_tick) {
910                         mpp->stat_total_queueing_time++;
911                         condlog(4, "%s: Retrying.. No active path", mpp->alias);
912                         if(--mpp->retry_tick == 0) {
913                                 dm_queue_if_no_path(mpp->alias, 0);
914                                 condlog(2, "%s: Disable queueing", mpp->alias);
915                         }
916                 }
917         }
918 }
919
920 void
921 check_path (struct vectors * vecs, struct path * pp)
922 {
923         int newstate;
924
925         if (!pp->mpp)
926                 return;
927
928         if (pp->tick && --pp->tick)
929                 return; /* don't check this path yet */
930
931         /*
932          * provision a next check soonest,
933          * in case we exit abnormaly from here
934          */
935         pp->tick = conf->checkint;
936
937         if (!checker_selected(&pp->checker)) {
938                 pathinfo(pp, conf->hwtable, DI_SYSFS);
939                 select_checker(pp);
940         }
941         if (!checker_selected(&pp->checker)) {
942                 condlog(0, "%s: checker is not set", pp->dev);
943                 return;
944         }
945         /*
946          * Set checker in async mode.
947          * Honored only by checker implementing the said mode.
948          */
949         checker_set_async(&pp->checker);
950
951         newstate = path_offline(pp);
952         if (newstate == PATH_UP)
953                 newstate = checker_check(&pp->checker);
954
955         if (newstate < 0) {
956                 condlog(2, "%s: unusable path", pp->dev);
957                 pathinfo(pp, conf->hwtable, 0);
958                 return;
959         }
960         /*
961          * Async IO in flight. Keep the previous path state
962          * and reschedule as soon as possible
963          */
964         if (newstate == PATH_PENDING) {
965                 pp->tick = 1;
966                 return;
967         }
968         if (newstate != pp->state) {
969                 int oldstate = pp->state;
970                 pp->state = newstate;
971                 LOG_MSG(1, checker_message(&pp->checker));
972
973                 /*
974                  * upon state change, reset the checkint
975                  * to the shortest delay
976                  */
977                 pp->checkint = conf->checkint;
978
979                 if (newstate == PATH_DOWN || newstate == PATH_SHAKY ||
980                     update_multipath_strings(pp->mpp, vecs->pathvec)) {
981                         /*
982                          * proactively fail path in the DM
983                          */
984                         if (oldstate == PATH_UP ||
985                             oldstate == PATH_GHOST)
986                                 fail_path(pp, 1);
987                         else
988                                 fail_path(pp, 0);
989
990                         /*
991                          * cancel scheduled failback
992                          */
993                         pp->mpp->failback_tick = 0;
994
995                         pp->mpp->stat_path_failures++;
996                         return;
997                 }
998
999                 /*
1000                  * reinstate this path
1001                  */
1002                 if (oldstate != PATH_UP &&
1003                     oldstate != PATH_GHOST)
1004                         reinstate_path(pp, 1);
1005                 else
1006                         reinstate_path(pp, 0);
1007
1008                 /*
1009                  * schedule [defered] failback
1010                  */
1011                 if (pp->mpp->pgfailback > 0)
1012                         pp->mpp->failback_tick =
1013                                 pp->mpp->pgfailback + 1;
1014                 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
1015                     need_switch_pathgroup(pp->mpp, 1))
1016                         switch_pathgroup(pp->mpp);
1017
1018                 /*
1019                  * if at least one path is up in a group, and
1020                  * the group is disabled, re-enable it
1021                  */
1022                 if (newstate == PATH_UP)
1023                         enable_group(pp);
1024         }
1025         else if (newstate == PATH_UP || newstate == PATH_GHOST) {
1026                 LOG_MSG(4, checker_message(&pp->checker));
1027                 /*
1028                  * double the next check delay.
1029                  * max at conf->max_checkint
1030                  */
1031                 if (pp->checkint < (conf->max_checkint / 2))
1032                         pp->checkint = 2 * pp->checkint;
1033                 else
1034                         pp->checkint = conf->max_checkint;
1035
1036                 pp->tick = pp->checkint;
1037                 condlog(4, "%s: delay next check %is",
1038                                 pp->dev_t, pp->tick);
1039         }
1040         else if (newstate == PATH_DOWN)
1041                 LOG_MSG(2, checker_message(&pp->checker));
1042
1043         pp->state = newstate;
1044
1045         /*
1046          * path prio refreshing
1047          */
1048         condlog(4, "path prio refresh");
1049         pathinfo(pp, conf->hwtable, DI_PRIO);
1050
1051         /*
1052          * pathgroup failback policy
1053          */
1054         if (need_switch_pathgroup(pp->mpp, 0)) {
1055                 if (pp->mpp->pgfailback > 0 &&
1056                     pp->mpp->failback_tick <= 0)
1057                         pp->mpp->failback_tick =
1058                                 pp->mpp->pgfailback + 1;
1059                 else if (pp->mpp->pgfailback ==
1060                                 -FAILBACK_IMMEDIATE)
1061                         switch_pathgroup(pp->mpp);
1062         }
1063 }
1064
1065 static void *
1066 checkerloop (void *ap)
1067 {
1068         struct vectors *vecs;
1069         struct path *pp;
1070         int count = 0;
1071         unsigned int i;
1072         sigset_t old;
1073
1074         mlockall(MCL_CURRENT | MCL_FUTURE);
1075         vecs = (struct vectors *)ap;
1076         condlog(2, "path checkers start up");
1077
1078         /*
1079          * init the path check interval
1080          */
1081         vector_foreach_slot (vecs->pathvec, pp, i) {
1082                 pp->checkint = conf->checkint;
1083         }
1084
1085         while (1) {
1086                 block_signal(SIGHUP, &old);
1087                 pthread_cleanup_push(cleanup_lock, &vecs->lock);
1088                 lock(vecs->lock);
1089                 condlog(4, "tick");
1090
1091                 if (vecs->pathvec) {
1092                         vector_foreach_slot (vecs->pathvec, pp, i) {
1093                                 check_path(vecs, pp);
1094                         }
1095                 }
1096                 if (vecs->mpvec) {
1097                         defered_failback_tick(vecs->mpvec);
1098                         retry_count_tick(vecs->mpvec);
1099                 }
1100                 if (count)
1101                         count--;
1102                 else {
1103                         condlog(4, "map garbage collection");
1104                         mpvec_garbage_collector(vecs);
1105                         count = MAPGCINT;
1106                 }
1107
1108                 lock_cleanup_pop(vecs->lock);
1109                 pthread_sigmask(SIG_SETMASK, &old, NULL);
1110                 sleep(1);
1111         }
1112         return NULL;
1113 }
1114
1115 int
1116 configure (struct vectors * vecs, int start_waiters)
1117 {
1118         struct multipath * mpp;
1119         struct path * pp;
1120         vector mpvec;
1121         int i;
1122
1123         if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1124                 return 1;
1125
1126         if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1127                 return 1;
1128
1129         if (!(mpvec = vector_alloc()))
1130                 return 1;
1131
1132         /*
1133          * probe for current path (from sysfs) and map (from dm) sets
1134          */
1135         path_discovery(vecs->pathvec, conf, DI_ALL);
1136
1137         vector_foreach_slot (vecs->pathvec, pp, i){
1138                 if (filter_path(conf, pp) > 0){
1139                         vector_del_slot(vecs->pathvec, i);
1140                         free_path(pp);
1141                         i--;
1142                 }
1143                 else
1144                         pp->checkint = conf->checkint;
1145         }
1146         if (map_discovery(vecs))
1147                 return 1;
1148
1149         /*
1150          * create new set of maps & push changed ones into dm
1151          */
1152         if (coalesce_paths(vecs, mpvec, NULL, 0))
1153                 return 1;
1154
1155         /*
1156          * may need to remove some maps which are no longer relevant
1157          * e.g., due to blacklist changes in conf file
1158          */
1159         if (coalesce_maps(vecs, mpvec))
1160                 return 1;
1161
1162         dm_lib_release();
1163
1164         sync_maps_state(mpvec);
1165
1166         /*
1167          * purge dm of old maps
1168          */
1169         remove_maps(vecs);
1170
1171         /*
1172          * save new set of maps formed by considering current path state
1173          */
1174         vector_free(vecs->mpvec);
1175         vecs->mpvec = mpvec;
1176
1177         /*
1178          * start dm event waiter threads for these new maps
1179          */
1180         vector_foreach_slot(vecs->mpvec, mpp, i) {
1181                 if (setup_multipath(vecs, mpp))
1182                         return 1;
1183                 if (start_waiters)
1184                         if (start_waiter_thread(mpp, vecs))
1185                                 return 1;
1186         }
1187         return 0;
1188 }
1189
1190 int
1191 reconfigure (struct vectors * vecs)
1192 {
1193         struct config * old = conf;
1194
1195         /*
1196          * free old map and path vectors ... they use old conf state
1197          */
1198         if (VECTOR_SIZE(vecs->mpvec))
1199                 remove_maps_and_stop_waiters(vecs);
1200
1201         if (VECTOR_SIZE(vecs->pathvec))
1202                 free_pathvec(vecs->pathvec, FREE_PATHS);
1203
1204         vecs->pathvec = NULL;
1205         conf = NULL;
1206
1207         if (load_config(DEFAULT_CONFIGFILE))
1208                 return 1;
1209
1210         conf->verbosity = old->verbosity;
1211
1212         if (!conf->checkint) {
1213                 conf->checkint = DEFAULT_CHECKINT;
1214                 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1215         }
1216         configure(vecs, 1);
1217         free_config(old);
1218         return 0;
1219 }
1220
1221 static struct vectors *
1222 init_vecs (void)
1223 {
1224         struct vectors * vecs;
1225
1226         vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1227
1228         if (!vecs)
1229                 return NULL;
1230
1231         vecs->lock.mutex =
1232                 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1233
1234         if (!vecs->lock.mutex)
1235                 goto out;
1236
1237         pthread_mutex_init(vecs->lock.mutex, NULL);
1238         vecs->lock.depth = 0;
1239
1240         return vecs;
1241
1242 out:
1243         FREE(vecs);
1244         condlog(0, "failed to init paths");
1245         return NULL;
1246 }
1247
1248 static void *
1249 signal_set(int signo, void (*func) (int))
1250 {
1251         int r;
1252         struct sigaction sig;
1253         struct sigaction osig;
1254
1255         sig.sa_handler = func;
1256         sigemptyset(&sig.sa_mask);
1257         sig.sa_flags = 0;
1258
1259         r = sigaction(signo, &sig, &osig);
1260
1261         if (r < 0)
1262                 return (SIG_ERR);
1263         else
1264                 return (osig.sa_handler);
1265 }
1266
1267 static void
1268 sighup (int sig)
1269 {
1270         condlog(2, "reconfigure (SIGHUP)");
1271
1272         lock(gvecs->lock);
1273         reconfigure(gvecs);
1274         unlock(gvecs->lock);
1275
1276 #ifdef _DEBUG_
1277         dbg_free_final(NULL);
1278 #endif
1279 }
1280
1281 static void
1282 sigend (int sig)
1283 {
1284         exit_daemon(0);
1285 }
1286
1287 static void
1288 sigusr1 (int sig)
1289 {
1290         condlog(3, "SIGUSR1 received");
1291 }
1292
1293 static void
1294 signal_init(void)
1295 {
1296         signal_set(SIGHUP, sighup);
1297         signal_set(SIGUSR1, sigusr1);
1298         signal_set(SIGINT, sigend);
1299         signal_set(SIGTERM, sigend);
1300         signal(SIGPIPE, SIG_IGN);
1301 }
1302
1303 static void
1304 setscheduler (void)
1305 {
1306         int res;
1307         static struct sched_param sched_param = {
1308                 .sched_priority = 99
1309         };
1310
1311         res = sched_setscheduler (0, SCHED_RR, &sched_param);
1312
1313         if (res == -1)
1314                 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1315         return;
1316 }
1317
1318 static void
1319 set_oom_adj (int val)
1320 {
1321         FILE *fp;
1322
1323         fp = fopen("/proc/self/oom_adj", "w");
1324
1325         if (!fp)
1326                 return;
1327
1328         fprintf(fp, "%i", val);
1329         fclose(fp);
1330 }
1331
1332 void
1333 setup_thread_attr(pthread_attr_t *attr, size_t stacksize, int detached)
1334 {
1335         if (pthread_attr_init(attr)) {
1336                 fprintf(stderr, "can't initialize thread attr: %s\n",
1337                         strerror(errno));
1338                 exit(1);
1339         }
1340         if (stacksize < PTHREAD_STACK_MIN)
1341                 stacksize = PTHREAD_STACK_MIN;
1342
1343         if (pthread_attr_setstacksize(attr, stacksize)) {
1344                 fprintf(stderr, "can't set thread stack size to %lu: %s\n",
1345                         (unsigned long)stacksize, strerror(errno));
1346                 exit(1);
1347         }
1348         if (detached && pthread_attr_setdetachstate(attr,
1349                                                     PTHREAD_CREATE_DETACHED)) {
1350                 fprintf(stderr, "can't set thread to detached: %s\n",
1351                         strerror(errno));
1352                 exit(1);
1353         }
1354 }
1355
1356 static int
1357 child (void * param)
1358 {
1359         pthread_t check_thr, uevent_thr, uxlsnr_thr;
1360         pthread_attr_t log_attr, misc_attr;
1361         struct vectors * vecs;
1362
1363         mlockall(MCL_CURRENT | MCL_FUTURE);
1364
1365         setup_thread_attr(&misc_attr, 64 * 1024, 1);
1366         setup_thread_attr(&waiter_attr, 32 * 1024, 1);
1367
1368         if (logsink) {
1369                 setup_thread_attr(&log_attr, 64 * 1024, 0);
1370                 log_thread_start(&log_attr);
1371                 pthread_attr_destroy(&log_attr);
1372         }
1373
1374         condlog(2, "--------start up--------");
1375         condlog(2, "read " DEFAULT_CONFIGFILE);
1376
1377         if (load_config(DEFAULT_CONFIGFILE))
1378                 exit(1);
1379
1380         if (init_checkers()) {
1381                 condlog(0, "failed to initialize checkers");
1382                 exit(1);
1383         }
1384         if (init_prio()) {
1385                 condlog(0, "failed to initialize prioritizers");
1386                 exit(1);
1387         }
1388
1389         setlogmask(LOG_UPTO(conf->verbosity + 3));
1390
1391         /*
1392          * fill the voids left in the config file
1393          */
1394         if (!conf->checkint) {
1395                 conf->checkint = DEFAULT_CHECKINT;
1396                 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1397         }
1398
1399         if (conf->max_fds) {
1400                 struct rlimit fd_limit;
1401
1402                 fd_limit.rlim_cur = conf->max_fds;
1403                 fd_limit.rlim_max = conf->max_fds;
1404                 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
1405                         condlog(0, "can't set open fds limit to %d : %s\n",
1406                                 conf->max_fds, strerror(errno));
1407         }
1408
1409         if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1410                 if (logsink)
1411                         log_thread_stop();
1412
1413                 exit(1);
1414         }
1415         signal_init();
1416         setscheduler();
1417         set_oom_adj(-16);
1418         vecs = gvecs = init_vecs();
1419
1420         if (!vecs)
1421                 exit(1);
1422
1423         if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1424                 condlog(0, "can not find sysfs mount point");
1425                 exit(1);
1426         }
1427         conf->daemon = 1;
1428         /*
1429          * fetch and configure both paths and multipaths
1430          */
1431         if (configure(vecs, 1)) {
1432                 condlog(0, "failure during configuration");
1433                 exit(1);
1434         }
1435         /*
1436          * start threads
1437          */
1438         pthread_create(&check_thr, &misc_attr, checkerloop, vecs);
1439         pthread_create(&uevent_thr, &misc_attr, ueventloop, vecs);
1440         pthread_create(&uxlsnr_thr, &misc_attr, uxlsnrloop, vecs);
1441         pthread_attr_destroy(&misc_attr);
1442
1443         pthread_mutex_lock(&exit_mutex);
1444         pthread_cond_wait(&exit_cond, &exit_mutex);
1445
1446         /*
1447          * exit path
1448          */
1449         block_signal(SIGHUP, NULL);
1450         lock(vecs->lock);
1451         remove_maps_and_stop_waiters(vecs);
1452         free_pathvec(vecs->pathvec, FREE_PATHS);
1453
1454         pthread_cancel(check_thr);
1455         pthread_cancel(uevent_thr);
1456         pthread_cancel(uxlsnr_thr);
1457
1458         sysfs_cleanup();
1459
1460         free_keys(keys);
1461         keys = NULL;
1462         free_handlers(handlers);
1463         handlers = NULL;
1464         free_polls();
1465
1466         unlock(vecs->lock);
1467         /* Now all the waitevent threads will start rushing in. */
1468         while (vecs->lock.depth > 0) {
1469                 sleep (1); /* This is weak. */
1470                 condlog(3,"Have %d wait event checkers threads to de-alloc, waiting..\n", vecs->lock.depth);
1471         }
1472         pthread_mutex_destroy(vecs->lock.mutex);
1473         FREE(vecs->lock.mutex);
1474         vecs->lock.depth = 0;
1475         vecs->lock.mutex = NULL;
1476         FREE(vecs);
1477         vecs = NULL;
1478
1479         condlog(2, "--------shut down-------");
1480
1481         if (logsink)
1482                 log_thread_stop();
1483
1484         dm_lib_release();
1485         dm_lib_exit();
1486
1487         cleanup_prio();
1488         cleanup_checkers();
1489         /*
1490          * Freeing config must be done after condlog() and dm_lib_exit(),
1491          * because logging functions like dlog() and dm_write_log()
1492          * reference the config.
1493          */
1494         free_config(conf);
1495         conf = NULL;
1496
1497 #ifdef _DEBUG_
1498         dbg_free_final(NULL);
1499 #endif
1500
1501         exit(0);
1502 }
1503
1504 static int
1505 daemonize(void)
1506 {
1507         int pid;
1508         int in_fd, out_fd;
1509
1510         if( (pid = fork()) < 0){
1511                 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1512                 return -1;
1513         }
1514         else if (pid != 0)
1515                 return pid;
1516
1517         setsid();
1518
1519         if ( (pid = fork()) < 0)
1520                 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1521         else if (pid != 0)
1522                 _exit(0);
1523
1524         in_fd = open("/dev/null", O_RDONLY);
1525         if (in_fd < 0){
1526                 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1527                         strerror(errno));
1528                 _exit(0);
1529         }
1530         out_fd = open("/dev/console", O_WRONLY);
1531         if (out_fd < 0){
1532                 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1533                         strerror(errno));
1534                 _exit(0);
1535         }
1536
1537         close(STDIN_FILENO);
1538         dup(in_fd);
1539         close(STDOUT_FILENO);
1540         dup(out_fd);
1541         close(STDERR_FILENO);
1542         dup(out_fd);
1543
1544         close(in_fd);
1545         close(out_fd);
1546         if (chdir("/") < 0)
1547                 fprintf(stderr, "cannot chdir to '/', continuing\n");
1548
1549         return 0;
1550 }
1551
1552 int
1553 main (int argc, char *argv[])
1554 {
1555         extern char *optarg;
1556         extern int optind;
1557         int arg;
1558         int err;
1559
1560         logsink = 1;
1561         dm_init();
1562
1563         if (getuid() != 0) {
1564                 fprintf(stderr, "need to be root\n");
1565                 exit(1);
1566         }
1567
1568         /* make sure we don't lock any path */
1569         chdir("/");
1570         umask(umask(077) | 022);
1571
1572         conf = alloc_config();
1573
1574         if (!conf)
1575                 exit(1);
1576
1577         while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1578         switch(arg) {
1579                 case 'd':
1580                         logsink = 0;
1581                         //debug=1; /* ### comment me out ### */
1582                         break;
1583                 case 'v':
1584                         if (sizeof(optarg) > sizeof(char *) ||
1585                             !isdigit(optarg[0]))
1586                                 exit(1);
1587
1588                         conf->verbosity = atoi(optarg);
1589                         break;
1590                 case 'k':
1591                         uxclnt(optarg);
1592                         exit(0);
1593                 default:
1594                         ;
1595                 }
1596         }
1597         if (optind < argc) {
1598                 char cmd[CMDSIZE];
1599                 char * s = cmd;
1600                 char * c = s;
1601
1602                 while (optind < argc) {
1603                         if (strchr(argv[optind], ' '))
1604                                 c += snprintf(c, s + CMDSIZE - c, "\"%s\" ", argv[optind]);
1605                         else
1606                                 c += snprintf(c, s + CMDSIZE - c, "%s ", argv[optind]);
1607                         optind++;
1608                 }
1609                 c += snprintf(c, s + CMDSIZE - c, "\n");
1610                 uxclnt(s);
1611                 exit(0);
1612         }
1613
1614         if (!logsink)
1615                 err = 0;
1616         else
1617                 err = daemonize();
1618
1619         if (err < 0)
1620                 /* error */
1621                 exit(1);
1622         else if (err > 0)
1623                 /* parent dies */
1624                 exit(0);
1625         else
1626                 /* child lives */
1627                 return (child(NULL));
1628 }
1629