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