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, DEFAULT_TARGET)) {
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)
164 if ((pp->dmstate == PSTATE_FAILED ||
165 pp->dmstate == PSTATE_UNDEF) &&
166 (pp->state == PATH_UP || pp->state == PATH_GHOST))
167 dm_reinstate_path(mpp->alias, pp->dev_t);
168 else if ((pp->dmstate == PSTATE_ACTIVE ||
169 pp->dmstate == PSTATE_UNDEF) &&
170 (pp->state == PATH_DOWN ||
171 pp->state == PATH_SHAKY))
172 dm_fail_path(mpp->alias, pp->dev_t);
178 sync_maps_state(vector mpvec)
181 struct multipath *mpp;
183 vector_foreach_slot (mpvec, mpp, i)
188 flush_map(struct multipath * mpp, struct vectors * vecs)
191 * clear references to this map before flushing so we can ignore
192 * the spurious uevent we may generate with the dm_flush_map call below
194 if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
196 * May not really be an error -- if the map was already flushed
197 * from the device mapper by dmsetup(8) for instance.
199 condlog(0, "%s: can't flush", mpp->alias);
204 condlog(2, "%s: devmap removed", mpp->alias);
207 orphan_paths(vecs->pathvec, mpp);
208 remove_map_and_stop_waiter(mpp, vecs, 1);
214 uev_add_map (struct sysfs_device * dev, struct vectors * vecs)
216 condlog(2, "%s: add map (uevent)", dev->kernel);
217 return ev_add_map(dev, vecs);
221 ev_add_map (struct sysfs_device * dev, struct vectors * vecs)
227 struct multipath * mpp;
231 dev_t = sysfs_attr_get_value(dev->devpath, "dev");
233 if (!dev_t || sscanf(dev_t, "%d:%d", &major, &minor) != 2)
236 alias = dm_mapname(major, minor);
241 map_present = dm_map_present(alias);
243 if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
244 condlog(4, "%s: not a multipath map", alias);
248 mpp = find_mp_by_alias(vecs->mpvec, alias);
252 * Not really an error -- we generate our own uevent
253 * if we create a multipath mapped device as a result
256 condlog(0, "%s: devmap already registered",
262 * now we can register the map
264 if (map_present && (mpp = add_map_without_path(vecs, minor, alias))) {
266 condlog(2, "%s: devmap %s added", alias, dev->kernel);
269 refwwid = get_refwwid(dev->kernel, DEV_DEVMAP, vecs->pathvec);
272 r = coalesce_paths(vecs, NULL, refwwid, 0);
277 condlog(2, "%s: devmap %s added", alias, dev->kernel);
279 condlog(0, "%s: uev_add_map %s failed", alias, dev->kernel);
286 uev_remove_map (struct sysfs_device * dev, struct vectors * vecs)
288 condlog(2, "%s: remove map (uevent)", dev->kernel);
289 return ev_remove_map(dev->kernel, vecs);
293 ev_remove_map (char * devname, struct vectors * vecs)
295 struct multipath * mpp;
297 mpp = find_mp_by_str(vecs->mpvec, devname);
300 condlog(2, "%s: devmap not registered, can't remove",
304 flush_map(mpp, vecs);
310 uev_umount_map (struct sysfs_device * dev, struct vectors * vecs)
312 struct multipath * mpp;
314 condlog(2, "%s: umount map (uevent)", dev->kernel);
316 mpp = find_mp_by_str(vecs->mpvec, dev->kernel);
321 update_mpp_paths(mpp, vecs->pathvec);
322 verify_paths(mpp, vecs, NULL);
324 if (!VECTOR_SIZE(mpp->paths))
325 flush_map(mpp, vecs);
331 uev_add_path (struct sysfs_device * dev, struct vectors * vecs)
333 condlog(2, "%s: add path (uevent)", dev->kernel);
334 return (ev_add_path(dev->kernel, vecs) != 1)? 0 : 1;
345 ev_add_path (char * devname, struct vectors * vecs)
347 struct multipath * mpp;
349 char empty_buff[WWID_SIZE] = {0};
351 pp = find_path_by_dev(vecs->pathvec, devname);
354 condlog(0, "%s: spurious uevent, path already in pathvec",
361 * get path vital state
363 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
365 condlog(0, "%s: failed to store path info", devname);
368 pp->checkint = conf->checkint;
372 * need path UID to go any further
374 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
375 condlog(0, "%s: failed to get path uid", devname);
376 return 1; /* leave path added to pathvec */
378 if (filter_path(conf, pp) > 0){
379 int i = find_slot(vecs->pathvec, (void *)pp);
381 vector_del_slot(vecs->pathvec, i);
385 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
388 if (adopt_paths(vecs->pathvec, mpp))
389 return 1; /* leave path added to pathvec */
391 verify_paths(mpp, vecs, NULL);
392 mpp->action = ACT_RELOAD;
395 if ((mpp = add_map_with_path(vecs, pp, 1)))
396 mpp->action = ACT_CREATE;
398 return 1; /* leave path added to pathvec */
402 * push the map to the device-mapper
404 if (setup_map(mpp)) {
405 condlog(0, "%s: failed to setup map for addition of new "
406 "path %s", mpp->alias, devname);
410 * reload the map for the multipath mapped device
412 if (domap(mpp) <= 0) {
413 condlog(0, "%s: failed in domap for addition of new "
414 "path %s", mpp->alias, devname);
416 * deal with asynchronous uevents :((
418 if (mpp->action == ACT_RELOAD) {
419 condlog(0, "%s: uev_add_path sleep", mpp->alias);
421 update_mpp_paths(mpp, vecs->pathvec);
430 * update our state from kernel regardless of create or reload
432 if (setup_multipath(vecs, mpp))
437 if (mpp->action == ACT_CREATE &&
438 start_waiter_thread(mpp, vecs))
441 condlog(2, "%s path added to devmap %s", devname, mpp->alias);
445 remove_map(mpp, vecs, 1);
450 uev_remove_path (struct sysfs_device * dev, struct vectors * vecs)
454 condlog(2, "%s: remove path (uevent)", dev->kernel);
455 retval = ev_remove_path(dev->kernel, vecs);
457 sysfs_device_put(dev);
463 ev_remove_path (char * devname, struct vectors * vecs)
465 struct multipath * mpp;
469 pp = find_path_by_dev(vecs->pathvec, devname);
472 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
477 * avoid referring to the map of an orphaned path
479 if ((mpp = pp->mpp)) {
481 * transform the mp->pg vector of vectors of paths
482 * into a mp->params string to feed the device-mapper
484 if (update_mpp_paths(mpp, vecs->pathvec)) {
485 condlog(0, "%s: failed to update paths",
489 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
490 vector_del_slot(mpp->paths, i);
493 * remove the map IFF removing the last path
495 if (VECTOR_SIZE(mpp->paths) == 0) {
496 char alias[WWID_SIZE];
499 * flush_map will fail if the device is open
501 strncpy(alias, mpp->alias, WWID_SIZE);
502 if (!flush_map(mpp, vecs)) {
503 condlog(2, "%s: removed map after"
504 " removing all paths",
510 * Not an error, continue
514 if (setup_map(mpp)) {
515 condlog(0, "%s: failed to setup map for"
516 " removal of path %s", mpp->alias,
523 mpp->action = ACT_RELOAD;
524 if (domap(mpp) <= 0) {
525 condlog(0, "%s: failed in domap for "
526 "removal of path %s",
527 mpp->alias, devname);
531 * update our state from kernel
533 if (setup_multipath(vecs, mpp)) {
538 condlog(2, "%s: path removed from map %s",
539 devname, mpp->alias);
543 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
544 vector_del_slot(vecs->pathvec, i);
551 remove_map_and_stop_waiter(mpp, vecs, 1);
556 map_discovery (struct vectors * vecs)
558 struct multipath * mpp;
561 if (dm_get_maps(vecs->mpvec, "multipath"))
564 vector_foreach_slot (vecs->mpvec, mpp, i)
565 if (setup_multipath(vecs, mpp))
572 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
574 struct vectors * vecs;
579 vecs = (struct vectors *)trigger_data;
581 pthread_cleanup_push(cleanup_lock, vecs->lock);
584 r = parse_cmd(str, reply, len, vecs);
587 *reply = STRDUP("fail\n");
588 *len = strlen(*reply) + 1;
591 else if (!r && *len == 0) {
592 *reply = STRDUP("ok\n");
593 *len = strlen(*reply) + 1;
596 /* else if (r < 0) leave *reply alone */
598 lock_cleanup_pop(vecs->lock);
603 uev_discard(char * devpath)
608 * keep only block devices, discard partitions
610 if (sscanf(devpath, "/block/%10s", a) != 1 ||
611 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
612 condlog(4, "discard event on %s", devpath);
619 uev_trigger (struct uevent * uev, void * trigger_data)
622 struct sysfs_device *sysdev;
623 struct vectors * vecs;
625 vecs = (struct vectors *)trigger_data;
627 if (uev_discard(uev->devpath))
630 sysdev = sysfs_device_get(uev->devpath);
638 * Add events are ignored here as the tables
639 * are not fully initialised then.
641 if (!strncmp(sysdev->kernel, "dm-", 3)) {
642 if (!strncmp(uev->action, "change", 6)) {
643 r = uev_add_map(sysdev, vecs);
646 if (!strncmp(uev->action, "remove", 6)) {
647 r = uev_remove_map(sysdev, vecs);
650 if (!strncmp(uev->action, "umount", 6)) {
651 r = uev_umount_map(sysdev, vecs);
658 * path add/remove event
660 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
664 if (!strncmp(uev->action, "add", 3)) {
665 r = uev_add_path(sysdev, vecs);
668 if (!strncmp(uev->action, "remove", 6)) {
669 r = uev_remove_path(sysdev, vecs);
679 ueventloop (void * ap)
681 if (uevent_listen(&uev_trigger, ap))
682 fprintf(stderr, "error starting uevent listener");
688 uxlsnrloop (void * ap)
693 set_handler_callback(LIST+PATHS, cli_list_paths);
694 set_handler_callback(LIST+PATHS+FMT, cli_list_paths_fmt);
695 set_handler_callback(LIST+MAPS, cli_list_maps);
696 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
697 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
698 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
699 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
700 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
701 set_handler_callback(LIST+CONFIG, cli_list_config);
702 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
703 set_handler_callback(LIST+DEVICES, cli_list_devices);
704 set_handler_callback(ADD+PATH, cli_add_path);
705 set_handler_callback(DEL+PATH, cli_del_path);
706 set_handler_callback(ADD+MAP, cli_add_map);
707 set_handler_callback(DEL+MAP, cli_del_map);
708 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
709 set_handler_callback(RECONFIGURE, cli_reconfigure);
710 set_handler_callback(SUSPEND+MAP, cli_suspend);
711 set_handler_callback(RESUME+MAP, cli_resume);
712 set_handler_callback(REINSTATE+PATH, cli_reinstate);
713 set_handler_callback(FAIL+PATH, cli_fail);
715 uxsock_listen(&uxsock_trigger, ap);
721 exit_daemon (int status)
724 fprintf(stderr, "bad exit status. see daemon.log\n");
726 condlog(3, "unlink pidfile");
727 unlink(DEFAULT_PIDFILE);
730 pthread_cond_signal(&exit_cond);
737 fail_path (struct path * pp, int del_active)
742 condlog(2, "checker failed path %s in map %s",
743 pp->dev_t, pp->mpp->alias);
745 dm_fail_path(pp->mpp->alias, pp->dev_t);
747 update_queue_mode_del_path(pp->mpp);
751 * caller must have locked the path list before calling that function
754 reinstate_path (struct path * pp, int add_active)
759 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
760 condlog(0, "%s: reinstate failed", pp->dev_t);
762 condlog(2, "%s: reinstated", pp->dev_t);
764 update_queue_mode_add_path(pp->mpp);
769 enable_group(struct path * pp)
771 struct pathgroup * pgp;
774 * if path is added through uev_add_path, pgindex can be unset.
775 * next update_strings() will set it, upon map reload event.
777 * we can safely return here, because upon map reload, all
778 * PG will be enabled.
780 if (!pp->mpp->pg || !pp->pgindex)
783 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
785 if (pgp->status == PGSTATE_DISABLED) {
786 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
787 dm_enablegroup(pp->mpp->alias, pp->pgindex);
792 mpvec_garbage_collector (struct vectors * vecs)
794 struct multipath * mpp;
797 vector_foreach_slot (vecs->mpvec, mpp, i) {
798 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
799 condlog(2, "%s: remove dead map", mpp->alias);
800 remove_map_and_stop_waiter(mpp, vecs, 1);
807 defered_failback_tick (vector mpvec)
809 struct multipath * mpp;
812 vector_foreach_slot (mpvec, mpp, i) {
814 * defered failback getting sooner
816 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
817 mpp->failback_tick--;
819 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
820 switch_pathgroup(mpp);
826 retry_count_tick(vector mpvec)
828 struct multipath *mpp;
831 vector_foreach_slot (mpvec, mpp, i) {
832 if (mpp->retry_tick) {
833 mpp->stat_total_queueing_time++;
834 condlog(4, "%s: Retrying.. No active path", mpp->alias);
835 if(--mpp->retry_tick == 0) {
836 dm_queue_if_no_path(mpp->alias, 0);
837 condlog(2, "%s: Disable queueing", mpp->alias);
844 check_path (struct vectors * vecs, struct path * pp)
851 if (pp->tick && --pp->tick)
852 return; /* don't check this path yet */
855 * provision a next check soonest,
856 * in case we exit abnormaly from here
858 pp->tick = conf->checkint;
860 if (!checker_selected(&pp->checker)) {
861 pathinfo(pp, conf->hwtable, DI_SYSFS);
864 if (!checker_selected(&pp->checker)) {
865 condlog(0, "%s: checker is not set", pp->dev);
869 * Set checker in async mode.
870 * Honored only by checker implementing the said mode.
872 checker_set_async(&pp->checker);
874 newstate = checker_check(&pp->checker);
877 condlog(2, "%s: unusable path", pp->dev);
878 pathinfo(pp, conf->hwtable, 0);
882 * Async IO in flight. Keep the previous path state
883 * and reschedule as soon as possible
885 if (newstate == PATH_PENDING) {
889 if (newstate != pp->state) {
890 int oldstate = pp->state;
891 pp->state = newstate;
892 LOG_MSG(1, checker_message(&pp->checker));
895 * upon state change, reset the checkint
896 * to the shortest delay
898 pp->checkint = conf->checkint;
900 if (newstate == PATH_DOWN || newstate == PATH_SHAKY ||
901 update_multipath_strings(pp->mpp, vecs->pathvec)) {
903 * proactively fail path in the DM
905 if (oldstate == PATH_UP ||
906 oldstate == PATH_GHOST)
912 * cancel scheduled failback
914 pp->mpp->failback_tick = 0;
916 pp->mpp->stat_path_failures++;
921 * reinstate this path
923 if (oldstate != PATH_UP &&
924 oldstate != PATH_GHOST)
925 reinstate_path(pp, 1);
927 reinstate_path(pp, 0);
930 * schedule [defered] failback
932 if (pp->mpp->pgfailback > 0)
933 pp->mpp->failback_tick =
934 pp->mpp->pgfailback + 1;
935 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
936 need_switch_pathgroup(pp->mpp, 1))
937 switch_pathgroup(pp->mpp);
940 * if at least one path is up in a group, and
941 * the group is disabled, re-enable it
943 if (newstate == PATH_UP)
946 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
947 LOG_MSG(4, checker_message(&pp->checker));
949 * double the next check delay.
950 * max at conf->max_checkint
952 if (pp->checkint < (conf->max_checkint / 2))
953 pp->checkint = 2 * pp->checkint;
955 pp->checkint = conf->max_checkint;
957 pp->tick = pp->checkint;
958 condlog(4, "%s: delay next check %is",
959 pp->dev_t, pp->tick);
961 else if (newstate == PATH_DOWN)
962 LOG_MSG(2, checker_message(&pp->checker));
964 pp->state = newstate;
967 * path prio refreshing
969 condlog(4, "path prio refresh");
970 pathinfo(pp, conf->hwtable, DI_PRIO);
973 * pathgroup failback policy
975 if (need_switch_pathgroup(pp->mpp, 0)) {
976 if (pp->mpp->pgfailback > 0 &&
977 pp->mpp->failback_tick <= 0)
978 pp->mpp->failback_tick =
979 pp->mpp->pgfailback + 1;
980 else if (pp->mpp->pgfailback ==
982 switch_pathgroup(pp->mpp);
987 checkerloop (void *ap)
989 struct vectors *vecs;
994 mlockall(MCL_CURRENT | MCL_FUTURE);
995 vecs = (struct vectors *)ap;
996 condlog(2, "path checkers start up");
999 * init the path check interval
1001 vector_foreach_slot (vecs->pathvec, pp, i) {
1002 pp->checkint = conf->checkint;
1006 pthread_cleanup_push(cleanup_lock, vecs->lock);
1010 if (vecs->pathvec) {
1011 vector_foreach_slot (vecs->pathvec, pp, i) {
1012 check_path(vecs, pp);
1016 defered_failback_tick(vecs->mpvec);
1017 retry_count_tick(vecs->mpvec);
1022 condlog(4, "map garbage collection");
1023 mpvec_garbage_collector(vecs);
1027 lock_cleanup_pop(vecs->lock);
1034 configure (struct vectors * vecs, int start_waiters)
1036 struct multipath * mpp;
1041 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1044 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1047 if (!(mpvec = vector_alloc()))
1051 * probe for current path (from sysfs) and map (from dm) sets
1053 path_discovery(vecs->pathvec, conf, DI_ALL);
1055 vector_foreach_slot (vecs->pathvec, pp, i){
1056 if (filter_path(conf, pp) > 0){
1057 vector_del_slot(vecs->pathvec, i);
1062 pp->checkint = conf->checkint;
1064 if (map_discovery(vecs))
1068 * create new set of maps & push changed ones into dm
1070 if (coalesce_paths(vecs, mpvec, NULL, 0))
1074 * may need to remove some maps which are no longer relevant
1075 * e.g., due to blacklist changes in conf file
1077 if (coalesce_maps(vecs, mpvec))
1082 sync_maps_state(mpvec);
1085 * purge dm of old maps
1090 * save new set of maps formed by considering current path state
1092 vector_free(vecs->mpvec);
1093 vecs->mpvec = mpvec;
1096 * start dm event waiter threads for these new maps
1098 vector_foreach_slot(vecs->mpvec, mpp, i) {
1099 if (setup_multipath(vecs, mpp))
1102 if (start_waiter_thread(mpp, vecs))
1109 reconfigure (struct vectors * vecs)
1111 struct config * old = conf;
1114 * free old map and path vectors ... they use old conf state
1116 if (VECTOR_SIZE(vecs->mpvec))
1117 remove_maps_and_stop_waiters(vecs);
1119 if (VECTOR_SIZE(vecs->pathvec))
1120 free_pathvec(vecs->pathvec, FREE_PATHS);
1122 vecs->pathvec = NULL;
1125 if (load_config(DEFAULT_CONFIGFILE))
1128 conf->verbosity = old->verbosity;
1130 if (!conf->checkint) {
1131 conf->checkint = DEFAULT_CHECKINT;
1132 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1139 static struct vectors *
1142 struct vectors * vecs;
1144 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1150 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1155 pthread_mutex_init(vecs->lock, NULL);
1161 condlog(0, "failed to init paths");
1166 signal_set(int signo, void (*func) (int))
1169 struct sigaction sig;
1170 struct sigaction osig;
1172 sig.sa_handler = func;
1173 sigemptyset(&sig.sa_mask);
1176 r = sigaction(signo, &sig, &osig);
1181 return (osig.sa_handler);
1187 condlog(2, "reconfigure (SIGHUP)");
1191 unlock(gvecs->lock);
1194 dbg_free_final(NULL);
1207 condlog(3, "SIGUSR1 received");
1213 signal_set(SIGHUP, sighup);
1214 signal_set(SIGUSR1, sigusr1);
1215 signal_set(SIGINT, sigend);
1216 signal_set(SIGTERM, sigend);
1217 signal(SIGPIPE, SIG_IGN);
1224 static struct sched_param sched_param = {
1225 .sched_priority = 99
1228 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1231 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1236 set_oom_adj (int val)
1240 fp = fopen("/proc/self/oom_adj", "w");
1245 fprintf(fp, "%i", val);
1250 child (void * param)
1252 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1253 pthread_attr_t attr;
1254 struct vectors * vecs;
1256 mlockall(MCL_CURRENT | MCL_FUTURE);
1261 condlog(2, "--------start up--------");
1262 condlog(2, "read " DEFAULT_CONFIGFILE);
1264 if (load_config(DEFAULT_CONFIGFILE))
1267 if (init_checkers()) {
1268 condlog(0, "failed to initialize checkers");
1272 condlog(0, "failed to initialize prioritizers");
1276 setlogmask(LOG_UPTO(conf->verbosity + 3));
1279 * fill the voids left in the config file
1281 if (!conf->checkint) {
1282 conf->checkint = DEFAULT_CHECKINT;
1283 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1286 if (conf->max_fds) {
1287 struct rlimit fd_limit;
1288 if (conf->max_fds > 0) {
1289 fd_limit.rlim_cur = conf->max_fds;
1290 fd_limit.rlim_max = conf->max_fds;
1293 fd_limit.rlim_cur = RLIM_INFINITY;
1294 fd_limit.rlim_max = RLIM_INFINITY;
1296 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
1297 condlog(0, "can't set open fds limit to %d : %s\n",
1298 conf->max_fds, strerror(errno));
1301 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1310 vecs = gvecs = init_vecs();
1315 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1316 condlog(0, "can not find sysfs mount point");
1321 * fetch and configure both paths and multipaths
1323 if (configure(vecs, 1)) {
1324 condlog(0, "failure during configuration");
1331 pthread_attr_init(&attr);
1332 pthread_attr_setstacksize(&attr, 64 * 1024);
1333 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1335 pthread_create(&check_thr, &attr, checkerloop, vecs);
1336 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1337 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1339 pthread_cond_wait(&exit_cond, &exit_mutex);
1345 remove_maps_and_stop_waiters(vecs);
1346 free_pathvec(vecs->pathvec, FREE_PATHS);
1348 pthread_cancel(check_thr);
1349 pthread_cancel(uevent_thr);
1350 pthread_cancel(uxlsnr_thr);
1356 free_handlers(handlers);
1361 pthread_mutex_destroy(vecs->lock);
1367 condlog(2, "--------shut down-------");
1376 * Freeing config must be done after condlog() and dm_lib_exit(),
1377 * because logging functions like dlog() and dm_write_log()
1378 * reference the config.
1384 dbg_free_final(NULL);
1396 if( (pid = fork()) < 0){
1397 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1405 if ( (pid = fork()) < 0)
1406 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1410 in_fd = open("/dev/null", O_RDONLY);
1412 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1416 out_fd = open("/dev/console", O_WRONLY);
1418 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1423 close(STDIN_FILENO);
1425 close(STDOUT_FILENO);
1427 close(STDERR_FILENO);
1438 main (int argc, char *argv[])
1440 extern char *optarg;
1448 if (getuid() != 0) {
1449 fprintf(stderr, "need to be root\n");
1453 /* make sure we don't lock any path */
1455 umask(umask(077) | 022);
1457 conf = alloc_config();
1462 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1466 //debug=1; /* ### comment me out ### */
1469 if (sizeof(optarg) > sizeof(char *) ||
1470 !isdigit(optarg[0]))
1473 conf->verbosity = atoi(optarg);
1496 return (child(NULL));