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
9 #include <libdevmapper.h>
12 #include <sys/types.h>
16 #include <sys/resource.h>
35 #include <blacklist.h>
36 #include <structs_vec.h>
38 #include <devmapper.h>
41 #include <discovery.h>
45 #include <switchgroup.h>
47 #include <configure.h>
55 #include "cli_handlers.h"
59 #define FILE_NAME_SIZE 256
62 #define LOG_MSG(a,b) \
63 if (strlen(b)) condlog(a, "%s: %s", pp->dev, b);
65 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
66 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
71 * global copy of vecs for use in sig handlers
73 struct vectors * gvecs;
76 need_switch_pathgroup (struct multipath * mpp, int refresh)
78 struct pathgroup * pgp;
82 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
86 * Refresh path priority values
89 vector_foreach_slot (mpp->pg, pgp, i)
90 vector_foreach_slot (pgp->paths, pp, j)
91 pathinfo(pp, conf->hwtable, DI_PRIO);
93 mpp->bestpg = select_path_group(mpp);
95 if (mpp->bestpg != mpp->nextpg)
102 switch_pathgroup (struct multipath * mpp)
104 mpp->stat_switchgroup++;
105 dm_switchgroup(mpp->alias, mpp->bestpg);
106 condlog(2, "%s: switch to path group #%i",
107 mpp->alias, mpp->bestpg);
111 coalesce_maps(struct vectors *vecs, vector nmpv)
113 struct multipath * ompp;
114 vector ompv = vecs->mpvec;
118 vector_foreach_slot (ompv, ompp, i) {
119 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
121 * remove all current maps not allowed by the
122 * current configuration
124 if (dm_flush_map(ompp->alias)) {
125 condlog(0, "%s: unable to flush devmap",
128 * may be just because the device is open
130 if (!vector_alloc_slot(nmpv))
133 vector_set_slot(nmpv, ompp);
134 setup_multipath(vecs, ompp);
136 if ((j = find_slot(ompv, (void *)ompp)) != -1)
137 vector_del_slot(ompv, j);
143 condlog(2, "%s devmap removed", ompp->alias);
151 sync_map_state(struct multipath *mpp)
153 struct pathgroup *pgp;
160 vector_foreach_slot (mpp->pg, pgp, i){
161 vector_foreach_slot (pgp->paths, pp, j){
162 if (pp->state == PATH_UNCHECKED ||
163 pp->state == PATH_WILD)
165 if ((pp->dmstate == PSTATE_FAILED ||
166 pp->dmstate == PSTATE_UNDEF) &&
167 (pp->state == PATH_UP || pp->state == PATH_GHOST))
168 dm_reinstate_path(mpp->alias, pp->dev_t);
169 else if ((pp->dmstate == PSTATE_ACTIVE ||
170 pp->dmstate == PSTATE_UNDEF) &&
171 (pp->state == PATH_DOWN ||
172 pp->state == PATH_SHAKY))
173 dm_fail_path(mpp->alias, pp->dev_t);
179 sync_maps_state(vector mpvec)
182 struct multipath *mpp;
184 vector_foreach_slot (mpvec, mpp, i)
189 flush_map(struct multipath * mpp, struct vectors * vecs)
192 * clear references to this map before flushing so we can ignore
193 * the spurious uevent we may generate with the dm_flush_map call below
195 if (dm_flush_map(mpp->alias)) {
197 * May not really be an error -- if the map was already flushed
198 * from the device mapper by dmsetup(8) for instance.
200 condlog(0, "%s: can't flush", mpp->alias);
205 condlog(2, "%s: devmap removed", mpp->alias);
208 orphan_paths(vecs->pathvec, mpp);
209 remove_map_and_stop_waiter(mpp, vecs, 1);
215 uev_add_map (struct sysfs_device * dev, struct vectors * vecs)
217 condlog(2, "%s: add map (uevent)", dev->kernel);
218 return ev_add_map(dev, vecs);
222 ev_add_map (struct sysfs_device * dev, struct vectors * vecs)
228 struct multipath * mpp;
232 dev_t = sysfs_attr_get_value(dev->devpath, "dev");
234 if (!dev_t || sscanf(dev_t, "%d:%d", &major, &minor) != 2)
237 alias = dm_mapname(major, minor);
242 map_present = dm_map_present(alias);
244 if (map_present && dm_type(alias, TGT_MPATH) <= 0) {
245 condlog(4, "%s: not a multipath map", alias);
249 mpp = find_mp_by_alias(vecs->mpvec, alias);
253 * Not really an error -- we generate our own uevent
254 * if we create a multipath mapped device as a result
257 condlog(0, "%s: devmap already registered",
263 * now we can register the map
265 if (map_present && (mpp = add_map_without_path(vecs, minor, alias))) {
267 condlog(2, "%s: devmap %s added", alias, dev->kernel);
270 refwwid = get_refwwid(dev->kernel, DEV_DEVMAP, vecs->pathvec);
273 r = coalesce_paths(vecs, NULL, refwwid, 0);
278 condlog(2, "%s: devmap %s added", alias, dev->kernel);
280 condlog(0, "%s: uev_add_map %s failed", alias, dev->kernel);
287 uev_remove_map (struct sysfs_device * dev, struct vectors * vecs)
289 condlog(2, "%s: remove map (uevent)", dev->kernel);
290 return ev_remove_map(dev->kernel, vecs);
294 ev_remove_map (char * devname, struct vectors * vecs)
296 struct multipath * mpp;
298 mpp = find_mp_by_str(vecs->mpvec, devname);
301 condlog(2, "%s: devmap not registered, can't remove",
305 flush_map(mpp, vecs);
311 uev_umount_map (struct sysfs_device * dev, struct vectors * vecs)
313 struct multipath * mpp;
315 condlog(2, "%s: umount map (uevent)", dev->kernel);
317 mpp = find_mp_by_str(vecs->mpvec, dev->kernel);
322 update_mpp_paths(mpp, vecs->pathvec);
323 verify_paths(mpp, vecs, NULL);
325 if (!VECTOR_SIZE(mpp->paths))
326 flush_map(mpp, vecs);
332 uev_add_path (struct sysfs_device * dev, struct vectors * vecs)
334 condlog(2, "%s: add path (uevent)", dev->kernel);
335 return (ev_add_path(dev->kernel, vecs) != 1)? 0 : 1;
346 ev_add_path (char * devname, struct vectors * vecs)
348 struct multipath * mpp;
350 char empty_buff[WWID_SIZE] = {0};
352 pp = find_path_by_dev(vecs->pathvec, devname);
355 condlog(0, "%s: spurious uevent, path already in pathvec",
362 * get path vital state
364 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
366 condlog(0, "%s: failed to store path info", devname);
369 pp->checkint = conf->checkint;
373 * need path UID to go any further
375 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
376 condlog(0, "%s: failed to get path uid", devname);
377 return 1; /* leave path added to pathvec */
379 if (filter_path(conf, pp) > 0){
380 int i = find_slot(vecs->pathvec, (void *)pp);
382 vector_del_slot(vecs->pathvec, i);
386 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
389 if (adopt_paths(vecs->pathvec, mpp))
390 return 1; /* leave path added to pathvec */
392 verify_paths(mpp, vecs, NULL);
393 mpp->action = ACT_RELOAD;
396 if ((mpp = add_map_with_path(vecs, pp, 1)))
397 mpp->action = ACT_CREATE;
399 return 1; /* leave path added to pathvec */
403 * push the map to the device-mapper
405 if (setup_map(mpp)) {
406 condlog(0, "%s: failed to setup map for addition of new "
407 "path %s", mpp->alias, devname);
411 * reload the map for the multipath mapped device
413 if (domap(mpp) <= 0) {
414 condlog(0, "%s: failed in domap for addition of new "
415 "path %s", mpp->alias, devname);
417 * deal with asynchronous uevents :((
419 if (mpp->action == ACT_RELOAD) {
420 condlog(0, "%s: uev_add_path sleep", mpp->alias);
422 update_mpp_paths(mpp, vecs->pathvec);
431 * update our state from kernel regardless of create or reload
433 if (setup_multipath(vecs, mpp))
438 if (mpp->action == ACT_CREATE &&
439 start_waiter_thread(mpp, vecs))
442 condlog(2, "%s path added to devmap %s", devname, mpp->alias);
446 remove_map(mpp, vecs, 1);
451 uev_remove_path (struct sysfs_device * dev, struct vectors * vecs)
455 condlog(2, "%s: remove path (uevent)", dev->kernel);
456 retval = ev_remove_path(dev->kernel, vecs);
458 sysfs_device_put(dev);
464 ev_remove_path (char * devname, struct vectors * vecs)
466 struct multipath * mpp;
470 pp = find_path_by_dev(vecs->pathvec, devname);
473 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
478 * avoid referring to the map of an orphaned path
480 if ((mpp = pp->mpp)) {
482 * transform the mp->pg vector of vectors of paths
483 * into a mp->params string to feed the device-mapper
485 if (update_mpp_paths(mpp, vecs->pathvec)) {
486 condlog(0, "%s: failed to update paths",
490 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
491 vector_del_slot(mpp->paths, i);
494 * remove the map IFF removing the last path
496 if (VECTOR_SIZE(mpp->paths) == 0) {
497 char alias[WWID_SIZE];
500 * flush_map will fail if the device is open
502 strncpy(alias, mpp->alias, WWID_SIZE);
503 if (!flush_map(mpp, vecs)) {
504 condlog(2, "%s: removed map after"
505 " removing all paths",
511 * Not an error, continue
515 if (setup_map(mpp)) {
516 condlog(0, "%s: failed to setup map for"
517 " removal of path %s", mpp->alias,
524 mpp->action = ACT_RELOAD;
525 if (domap(mpp) <= 0) {
526 condlog(0, "%s: failed in domap for "
527 "removal of path %s",
528 mpp->alias, devname);
532 * update our state from kernel
534 if (setup_multipath(vecs, mpp)) {
539 condlog(2, "%s: path removed from map %s",
540 devname, mpp->alias);
544 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
545 vector_del_slot(vecs->pathvec, i);
552 remove_map_and_stop_waiter(mpp, vecs, 1);
557 map_discovery (struct vectors * vecs)
559 struct multipath * mpp;
562 if (dm_get_maps(vecs->mpvec))
565 vector_foreach_slot (vecs->mpvec, mpp, i)
566 if (setup_multipath(vecs, mpp))
573 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
575 struct vectors * vecs;
580 vecs = (struct vectors *)trigger_data;
582 pthread_cleanup_push(cleanup_lock, vecs->lock);
585 r = parse_cmd(str, reply, len, vecs);
588 *reply = STRDUP("fail\n");
589 *len = strlen(*reply) + 1;
592 else if (!r && *len == 0) {
593 *reply = STRDUP("ok\n");
594 *len = strlen(*reply) + 1;
597 /* else if (r < 0) leave *reply alone */
599 lock_cleanup_pop(vecs->lock);
604 uev_discard(char * devpath)
609 * keep only block devices, discard partitions
611 if (sscanf(devpath, "/block/%10s", a) != 1 ||
612 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
613 condlog(4, "discard event on %s", devpath);
620 uev_trigger (struct uevent * uev, void * trigger_data)
623 struct sysfs_device *sysdev;
624 struct vectors * vecs;
626 vecs = (struct vectors *)trigger_data;
628 if (uev_discard(uev->devpath))
631 sysdev = sysfs_device_get(uev->devpath);
639 * Add events are ignored here as the tables
640 * are not fully initialised then.
642 if (!strncmp(sysdev->kernel, "dm-", 3)) {
643 if (!strncmp(uev->action, "change", 6)) {
644 r = uev_add_map(sysdev, vecs);
647 if (!strncmp(uev->action, "remove", 6)) {
648 r = uev_remove_map(sysdev, vecs);
651 if (!strncmp(uev->action, "umount", 6)) {
652 r = uev_umount_map(sysdev, vecs);
659 * path add/remove event
661 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
665 if (!strncmp(uev->action, "add", 3)) {
666 r = uev_add_path(sysdev, vecs);
669 if (!strncmp(uev->action, "remove", 6)) {
670 r = uev_remove_path(sysdev, vecs);
680 ueventloop (void * ap)
682 if (uevent_listen(&uev_trigger, ap))
683 fprintf(stderr, "error starting uevent listener");
689 uxlsnrloop (void * ap)
694 set_handler_callback(LIST+PATHS, cli_list_paths);
695 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
696 set_handler_callback(LIST+MAPS, cli_list_maps);
697 set_handler_callback(LIST+STATUS, cli_list_status);
698 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
699 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
700 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
701 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
702 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
703 set_handler_callback(LIST+CONFIG, cli_list_config);
704 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
705 set_handler_callback(LIST+DEVICES, cli_list_devices);
706 set_handler_callback(LIST+WILDCARDS, cli_list_wildcards);
707 set_handler_callback(ADD+PATH, cli_add_path);
708 set_handler_callback(DEL+PATH, cli_del_path);
709 set_handler_callback(ADD+MAP, cli_add_map);
710 set_handler_callback(DEL+MAP, cli_del_map);
711 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
712 set_handler_callback(RECONFIGURE, cli_reconfigure);
713 set_handler_callback(SUSPEND+MAP, cli_suspend);
714 set_handler_callback(RESUME+MAP, cli_resume);
715 set_handler_callback(REINSTATE+PATH, cli_reinstate);
716 set_handler_callback(FAIL+PATH, cli_fail);
718 uxsock_listen(&uxsock_trigger, ap);
724 exit_daemon (int status)
727 fprintf(stderr, "bad exit status. see daemon.log\n");
729 condlog(3, "unlink pidfile");
730 unlink(DEFAULT_PIDFILE);
733 pthread_cond_signal(&exit_cond);
740 fail_path (struct path * pp, int del_active)
745 condlog(2, "checker failed path %s in map %s",
746 pp->dev_t, pp->mpp->alias);
748 dm_fail_path(pp->mpp->alias, pp->dev_t);
750 update_queue_mode_del_path(pp->mpp);
754 * caller must have locked the path list before calling that function
757 reinstate_path (struct path * pp, int add_active)
762 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
763 condlog(0, "%s: reinstate failed", pp->dev_t);
765 condlog(2, "%s: reinstated", pp->dev_t);
767 update_queue_mode_add_path(pp->mpp);
772 enable_group(struct path * pp)
774 struct pathgroup * pgp;
777 * if path is added through uev_add_path, pgindex can be unset.
778 * next update_strings() will set it, upon map reload event.
780 * we can safely return here, because upon map reload, all
781 * PG will be enabled.
783 if (!pp->mpp->pg || !pp->pgindex)
786 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
788 if (pgp->status == PGSTATE_DISABLED) {
789 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
790 dm_enablegroup(pp->mpp->alias, pp->pgindex);
795 mpvec_garbage_collector (struct vectors * vecs)
797 struct multipath * mpp;
800 vector_foreach_slot (vecs->mpvec, mpp, i) {
801 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
802 condlog(2, "%s: remove dead map", mpp->alias);
803 remove_map_and_stop_waiter(mpp, vecs, 1);
810 defered_failback_tick (vector mpvec)
812 struct multipath * mpp;
815 vector_foreach_slot (mpvec, mpp, i) {
817 * defered failback getting sooner
819 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
820 mpp->failback_tick--;
822 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
823 switch_pathgroup(mpp);
829 retry_count_tick(vector mpvec)
831 struct multipath *mpp;
834 vector_foreach_slot (mpvec, mpp, i) {
835 if (mpp->retry_tick) {
836 mpp->stat_total_queueing_time++;
837 condlog(4, "%s: Retrying.. No active path", mpp->alias);
838 if(--mpp->retry_tick == 0) {
839 dm_queue_if_no_path(mpp->alias, 0);
840 condlog(2, "%s: Disable queueing", mpp->alias);
847 check_path (struct vectors * vecs, struct path * pp)
854 if (pp->tick && --pp->tick)
855 return; /* don't check this path yet */
858 * provision a next check soonest,
859 * in case we exit abnormaly from here
861 pp->tick = conf->checkint;
863 if (!checker_selected(&pp->checker)) {
864 pathinfo(pp, conf->hwtable, DI_SYSFS);
867 if (!checker_selected(&pp->checker)) {
868 condlog(0, "%s: checker is not set", pp->dev);
872 * Set checker in async mode.
873 * Honored only by checker implementing the said mode.
875 checker_set_async(&pp->checker);
877 newstate = checker_check(&pp->checker);
880 condlog(2, "%s: unusable path", pp->dev);
881 pathinfo(pp, conf->hwtable, 0);
885 * Async IO in flight. Keep the previous path state
886 * and reschedule as soon as possible
888 if (newstate == PATH_PENDING) {
892 if (newstate != pp->state) {
893 int oldstate = pp->state;
894 pp->state = newstate;
895 LOG_MSG(1, checker_message(&pp->checker));
898 * upon state change, reset the checkint
899 * to the shortest delay
901 pp->checkint = conf->checkint;
903 if (newstate == PATH_DOWN || newstate == PATH_SHAKY ||
904 update_multipath_strings(pp->mpp, vecs->pathvec)) {
906 * proactively fail path in the DM
908 if (oldstate == PATH_UP ||
909 oldstate == PATH_GHOST)
915 * cancel scheduled failback
917 pp->mpp->failback_tick = 0;
919 pp->mpp->stat_path_failures++;
924 * reinstate this path
926 if (oldstate != PATH_UP &&
927 oldstate != PATH_GHOST)
928 reinstate_path(pp, 1);
930 reinstate_path(pp, 0);
933 * schedule [defered] failback
935 if (pp->mpp->pgfailback > 0)
936 pp->mpp->failback_tick =
937 pp->mpp->pgfailback + 1;
938 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
939 need_switch_pathgroup(pp->mpp, 1))
940 switch_pathgroup(pp->mpp);
943 * if at least one path is up in a group, and
944 * the group is disabled, re-enable it
946 if (newstate == PATH_UP)
949 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
950 LOG_MSG(4, checker_message(&pp->checker));
952 * double the next check delay.
953 * max at conf->max_checkint
955 if (pp->checkint < (conf->max_checkint / 2))
956 pp->checkint = 2 * pp->checkint;
958 pp->checkint = conf->max_checkint;
960 pp->tick = pp->checkint;
961 condlog(4, "%s: delay next check %is",
962 pp->dev_t, pp->tick);
964 else if (newstate == PATH_DOWN)
965 LOG_MSG(2, checker_message(&pp->checker));
967 pp->state = newstate;
970 * path prio refreshing
972 condlog(4, "path prio refresh");
973 pathinfo(pp, conf->hwtable, DI_PRIO);
976 * pathgroup failback policy
978 if (need_switch_pathgroup(pp->mpp, 0)) {
979 if (pp->mpp->pgfailback > 0 &&
980 pp->mpp->failback_tick <= 0)
981 pp->mpp->failback_tick =
982 pp->mpp->pgfailback + 1;
983 else if (pp->mpp->pgfailback ==
985 switch_pathgroup(pp->mpp);
990 checkerloop (void *ap)
992 struct vectors *vecs;
997 mlockall(MCL_CURRENT | MCL_FUTURE);
998 vecs = (struct vectors *)ap;
999 condlog(2, "path checkers start up");
1002 * init the path check interval
1004 vector_foreach_slot (vecs->pathvec, pp, i) {
1005 pp->checkint = conf->checkint;
1009 pthread_cleanup_push(cleanup_lock, vecs->lock);
1013 if (vecs->pathvec) {
1014 vector_foreach_slot (vecs->pathvec, pp, i) {
1015 check_path(vecs, pp);
1019 defered_failback_tick(vecs->mpvec);
1020 retry_count_tick(vecs->mpvec);
1025 condlog(4, "map garbage collection");
1026 mpvec_garbage_collector(vecs);
1030 lock_cleanup_pop(vecs->lock);
1037 configure (struct vectors * vecs, int start_waiters)
1039 struct multipath * mpp;
1044 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1047 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1050 if (!(mpvec = vector_alloc()))
1054 * probe for current path (from sysfs) and map (from dm) sets
1056 path_discovery(vecs->pathvec, conf, DI_ALL);
1058 vector_foreach_slot (vecs->pathvec, pp, i){
1059 if (filter_path(conf, pp) > 0){
1060 vector_del_slot(vecs->pathvec, i);
1065 pp->checkint = conf->checkint;
1067 if (map_discovery(vecs))
1071 * create new set of maps & push changed ones into dm
1073 if (coalesce_paths(vecs, mpvec, NULL, 0))
1077 * may need to remove some maps which are no longer relevant
1078 * e.g., due to blacklist changes in conf file
1080 if (coalesce_maps(vecs, mpvec))
1085 sync_maps_state(mpvec);
1088 * purge dm of old maps
1093 * save new set of maps formed by considering current path state
1095 vector_free(vecs->mpvec);
1096 vecs->mpvec = mpvec;
1099 * start dm event waiter threads for these new maps
1101 vector_foreach_slot(vecs->mpvec, mpp, i) {
1102 if (setup_multipath(vecs, mpp))
1105 if (start_waiter_thread(mpp, vecs))
1112 reconfigure (struct vectors * vecs)
1114 struct config * old = conf;
1117 * free old map and path vectors ... they use old conf state
1119 if (VECTOR_SIZE(vecs->mpvec))
1120 remove_maps_and_stop_waiters(vecs);
1122 if (VECTOR_SIZE(vecs->pathvec))
1123 free_pathvec(vecs->pathvec, FREE_PATHS);
1125 vecs->pathvec = NULL;
1128 if (load_config(DEFAULT_CONFIGFILE))
1131 conf->verbosity = old->verbosity;
1133 if (!conf->checkint) {
1134 conf->checkint = DEFAULT_CHECKINT;
1135 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1142 static struct vectors *
1145 struct vectors * vecs;
1147 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1153 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1158 pthread_mutex_init(vecs->lock, NULL);
1164 condlog(0, "failed to init paths");
1169 signal_set(int signo, void (*func) (int))
1172 struct sigaction sig;
1173 struct sigaction osig;
1175 sig.sa_handler = func;
1176 sigemptyset(&sig.sa_mask);
1179 r = sigaction(signo, &sig, &osig);
1184 return (osig.sa_handler);
1190 condlog(2, "reconfigure (SIGHUP)");
1194 unlock(gvecs->lock);
1197 dbg_free_final(NULL);
1210 condlog(3, "SIGUSR1 received");
1216 signal_set(SIGHUP, sighup);
1217 signal_set(SIGUSR1, sigusr1);
1218 signal_set(SIGINT, sigend);
1219 signal_set(SIGTERM, sigend);
1220 signal(SIGPIPE, SIG_IGN);
1227 static struct sched_param sched_param = {
1228 .sched_priority = 99
1231 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1234 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1239 set_oom_adj (int val)
1243 fp = fopen("/proc/self/oom_adj", "w");
1248 fprintf(fp, "%i", val);
1253 child (void * param)
1255 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1256 pthread_attr_t attr;
1257 struct vectors * vecs;
1259 mlockall(MCL_CURRENT | MCL_FUTURE);
1264 condlog(2, "--------start up--------");
1265 condlog(2, "read " DEFAULT_CONFIGFILE);
1267 if (load_config(DEFAULT_CONFIGFILE))
1270 if (init_checkers()) {
1271 condlog(0, "failed to initialize checkers");
1275 condlog(0, "failed to initialize prioritizers");
1279 setlogmask(LOG_UPTO(conf->verbosity + 3));
1282 * fill the voids left in the config file
1284 if (!conf->checkint) {
1285 conf->checkint = DEFAULT_CHECKINT;
1286 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1289 if (conf->max_fds) {
1290 struct rlimit fd_limit;
1291 if (conf->max_fds > 0) {
1292 fd_limit.rlim_cur = conf->max_fds;
1293 fd_limit.rlim_max = conf->max_fds;
1296 fd_limit.rlim_cur = RLIM_INFINITY;
1297 fd_limit.rlim_max = RLIM_INFINITY;
1299 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
1300 condlog(0, "can't set open fds limit to %d : %s\n",
1301 conf->max_fds, strerror(errno));
1304 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1313 vecs = gvecs = init_vecs();
1318 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1319 condlog(0, "can not find sysfs mount point");
1324 * fetch and configure both paths and multipaths
1326 if (configure(vecs, 1)) {
1327 condlog(0, "failure during configuration");
1334 pthread_attr_init(&attr);
1335 pthread_attr_setstacksize(&attr, 64 * 1024);
1336 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1338 pthread_create(&check_thr, &attr, checkerloop, vecs);
1339 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1340 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1342 pthread_cond_wait(&exit_cond, &exit_mutex);
1348 remove_maps_and_stop_waiters(vecs);
1349 free_pathvec(vecs->pathvec, FREE_PATHS);
1351 pthread_cancel(check_thr);
1352 pthread_cancel(uevent_thr);
1353 pthread_cancel(uxlsnr_thr);
1359 free_handlers(handlers);
1364 pthread_mutex_destroy(vecs->lock);
1370 condlog(2, "--------shut down-------");
1379 * Freeing config must be done after condlog() and dm_lib_exit(),
1380 * because logging functions like dlog() and dm_write_log()
1381 * reference the config.
1387 dbg_free_final(NULL);
1399 if( (pid = fork()) < 0){
1400 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1408 if ( (pid = fork()) < 0)
1409 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1413 in_fd = open("/dev/null", O_RDONLY);
1415 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1419 out_fd = open("/dev/console", O_WRONLY);
1421 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1426 close(STDIN_FILENO);
1428 close(STDOUT_FILENO);
1430 close(STDERR_FILENO);
1441 main (int argc, char *argv[])
1443 extern char *optarg;
1451 if (getuid() != 0) {
1452 fprintf(stderr, "need to be root\n");
1456 /* make sure we don't lock any path */
1458 umask(umask(077) | 022);
1460 conf = alloc_config();
1465 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1469 //debug=1; /* ### comment me out ### */
1472 if (sizeof(optarg) > sizeof(char *) ||
1473 !isdigit(optarg[0]))
1476 conf->verbosity = atoi(optarg);
1499 return (child(NULL));