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>
33 #include <blacklist.h>
34 #include <structs_vec.h>
36 #include <devmapper.h>
39 #include <discovery.h>
43 #include <switchgroup.h>
45 #include <configure.h>
52 #include "cli_handlers.h"
56 #define FILE_NAME_SIZE 256
59 #define LOG_MSG(a,b) \
60 if (strlen(b)) condlog(a, "%s: %s", pp->dev, b);
62 pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
63 pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
66 * global copy of vecs for use in sig handlers
68 struct vectors * gvecs;
71 need_switch_pathgroup (struct multipath * mpp, int refresh)
73 struct pathgroup * pgp;
77 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
81 * Refresh path priority values
84 vector_foreach_slot (mpp->pg, pgp, i)
85 vector_foreach_slot (pgp->paths, pp, j)
86 pathinfo(pp, conf->hwtable, DI_PRIO);
88 mpp->bestpg = select_path_group(mpp);
90 if (mpp->bestpg != mpp->nextpg)
97 switch_pathgroup (struct multipath * mpp)
99 mpp->stat_switchgroup++;
100 dm_switchgroup(mpp->alias, mpp->bestpg);
101 condlog(2, "%s: switch to path group #%i",
102 mpp->alias, mpp->bestpg);
106 coalesce_maps(struct vectors *vecs, vector nmpv)
108 struct multipath * ompp;
109 vector ompv = vecs->mpvec;
113 vector_foreach_slot (ompv, ompp, i) {
114 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
116 * remove all current maps not allowed by the
117 * current configuration
119 if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
120 condlog(0, "%s: unable to flush devmap",
123 * may be just because the device is open
125 if (!vector_alloc_slot(nmpv))
128 vector_set_slot(nmpv, ompp);
129 setup_multipath(vecs, ompp);
131 if ((j = find_slot(ompv, (void *)ompp)) != -1)
132 vector_del_slot(ompv, j);
138 condlog(3, "%s devmap removed", ompp->alias);
146 sync_map_state(struct multipath *mpp)
148 struct pathgroup *pgp;
152 vector_foreach_slot (mpp->pg, pgp, i){
153 vector_foreach_slot (pgp->paths, pp, j){
154 if (pp->state <= PATH_UNCHECKED)
156 if ((pp->dmstate == PSTATE_FAILED ||
157 pp->dmstate == PSTATE_UNDEF) &&
158 (pp->state == PATH_UP || pp->state == PATH_GHOST))
159 dm_reinstate_path(mpp->alias, pp->dev_t);
160 else if ((pp->dmstate == PSTATE_ACTIVE ||
161 pp->dmstate == PSTATE_UNDEF) &&
162 (pp->state == PATH_DOWN ||
163 pp->state == PATH_SHAKY))
164 dm_fail_path(mpp->alias, pp->dev_t);
170 sync_maps_state(vector mpvec)
173 struct multipath *mpp;
175 vector_foreach_slot (mpvec, mpp, i)
180 flush_map(struct multipath * mpp, struct vectors * vecs)
183 * clear references to this map before flushing so we can ignore
184 * the spurious uevent we may generate with the dm_flush_map call below
186 if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
188 * May not really be an error -- if the map was already flushed
189 * from the device mapper by dmsetup(8) for instance.
191 condlog(0, "%s: can't flush", mpp->alias);
196 condlog(3, "%s: devmap removed", mpp->alias);
199 orphan_paths(vecs->pathvec, mpp);
200 remove_map(mpp, vecs, stop_waiter_thread, 1);
206 uev_add_map (struct sysfs_device * dev, struct vectors * vecs)
208 condlog(2, "%s: add map (uevent)", dev->kernel);
209 return ev_add_map(dev, vecs);
213 ev_add_map (struct sysfs_device * dev, struct vectors * vecs)
219 struct multipath * mpp;
223 dev_t = sysfs_attr_get_value(dev->devpath, "dev");
225 if (!dev_t || sscanf(dev_t, "%d:%d", &major, &minor) != 2)
228 alias = dm_mapname(major, minor);
233 map_present = dm_map_present(alias);
235 if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
236 condlog(4, "%s: not a multipath map", alias);
240 mpp = find_mp_by_alias(vecs->mpvec, alias);
244 * Not really an error -- we generate our own uevent
245 * if we create a multipath mapped device as a result
248 condlog(0, "%s: devmap already registered",
254 * now we can register the map
256 if (map_present && (mpp = add_map_without_path(vecs, minor, alias,
257 start_waiter_thread))) {
259 condlog(3, "%s: devmap %s added", alias, dev->kernel);
262 refwwid = get_refwwid(dev->kernel, DEV_DEVMAP, vecs->pathvec);
265 r = coalesce_paths(vecs, NULL, refwwid);
270 condlog(3, "%s: devmap %s added", alias, dev->kernel);
272 condlog(0, "%s: uev_add_map %s failed", alias, dev->kernel);
279 uev_remove_map (struct sysfs_device * dev, struct vectors * vecs)
281 condlog(2, "%s: remove map (uevent)", dev->kernel);
282 return ev_remove_map(dev->kernel, vecs);
286 ev_remove_map (char * devname, struct vectors * vecs)
288 struct multipath * mpp;
290 mpp = find_mp_by_str(vecs->mpvec, devname);
293 condlog(3, "%s: devmap not registered, can't remove",
297 flush_map(mpp, vecs);
303 uev_umount_map (struct sysfs_device * dev, struct vectors * vecs)
305 struct multipath * mpp;
307 condlog(2, "%s: umount map (uevent)", dev->kernel);
309 mpp = find_mp_by_str(vecs->mpvec, dev->kernel);
314 update_mpp_paths(mpp, vecs->pathvec);
315 verify_paths(mpp, vecs, NULL);
317 if (!VECTOR_SIZE(mpp->paths))
318 flush_map(mpp, vecs);
324 uev_add_path (struct sysfs_device * dev, struct vectors * vecs)
326 condlog(2, "%s: add path (uevent)", dev->kernel);
327 return (ev_add_path(dev->kernel, vecs) != 1)? 0 : 1;
338 ev_add_path (char * devname, struct vectors * vecs)
340 struct multipath * mpp;
342 char empty_buff[WWID_SIZE] = {0};
344 pp = find_path_by_dev(vecs->pathvec, devname);
347 condlog(0, "%s: spurious uevent, path already in pathvec",
354 * get path vital state
356 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
358 condlog(0, "%s: failed to store path info", devname);
361 pp->checkint = conf->checkint;
365 * need path UID to go any further
367 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
368 condlog(0, "%s: failed to get path uid", devname);
369 return 1; /* leave path added to pathvec */
371 if (filter_path(conf, pp)){
372 int i = find_slot(vecs->pathvec, (void *)pp);
374 vector_del_slot(vecs->pathvec, i);
378 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
381 if (adopt_paths(vecs->pathvec, mpp))
382 return 1; /* leave path added to pathvec */
384 verify_paths(mpp, vecs, NULL);
385 mpp->action = ACT_RELOAD;
388 if ((mpp = add_map_with_path(vecs, pp, 1)))
389 mpp->action = ACT_CREATE;
391 return 1; /* leave path added to pathvec */
395 * push the map to the device-mapper
397 if (setup_map(mpp)) {
398 condlog(0, "%s: failed to setup map for addition of new "
399 "path %s", mpp->alias, devname);
403 * reload the map for the multipath mapped device
405 if (domap(mpp) <= 0) {
406 condlog(0, "%s: failed in domap for addition of new "
407 "path %s", mpp->alias, devname);
409 * deal with asynchronous uevents :((
411 if (mpp->action == ACT_RELOAD) {
412 condlog(0, "%s: uev_add_path sleep", mpp->alias);
414 update_mpp_paths(mpp, vecs->pathvec);
423 * update our state from kernel regardless of create or reload
425 if (setup_multipath(vecs, mpp))
430 if (mpp->action == ACT_CREATE &&
431 start_waiter_thread(mpp, vecs))
434 condlog(3, "%s path added to devmap %s", devname, mpp->alias);
438 remove_map(mpp, vecs, NULL, 1);
443 uev_remove_path (struct sysfs_device * dev, struct vectors * vecs)
445 condlog(2, "%s: remove path (uevent)", dev->kernel);
446 return ev_remove_path(dev->kernel, vecs);
450 ev_remove_path (char * devname, struct vectors * vecs)
452 struct multipath * mpp;
457 pp = find_path_by_dev(vecs->pathvec, devname);
460 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
465 * avoid referring to the map of an orphanned path
467 if ((mpp = pp->mpp)) {
470 * remove the map IFF removing the last path
472 if (pathcount(mpp, PATH_WILD) > 1) {
473 vector rpvec = vector_alloc();
476 * transform the mp->pg vector of vectors of paths
477 * into a mp->params string to feed the device-mapper
479 update_mpp_paths(mpp, vecs->pathvec);
480 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
481 vector_del_slot(mpp->paths, i);
483 if (VECTOR_SIZE(mpp->paths) == 0) {
484 char alias[WWID_SIZE];
487 * flush_map will fail if the device is open
489 strncpy(alias, mpp->alias, WWID_SIZE);
490 if (flush_map(mpp, vecs))
493 condlog(3, "%s: removed map after removing"
494 " multiple paths", alias);
497 if (setup_map(mpp)) {
498 condlog(0, "%s: failed to setup map for"
499 " removal of path %s", mpp->alias, devname);
500 free_pathvec(rpvec, KEEP_PATHS);
506 mpp->action = ACT_RELOAD;
507 if (domap(mpp) <= 0) {
508 condlog(0, "%s: failed in domap for "
509 "removal of path %s",
510 mpp->alias, devname);
512 * Delete path from pathvec so that
513 * update_mpp_paths wont find it later
514 * when/if another path is removed.
516 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
517 vector_del_slot(vecs->pathvec, i);
522 * update our state from kernel
524 if (setup_multipath(vecs, mpp)) {
525 free_pathvec(rpvec, KEEP_PATHS);
530 condlog(3, "%s: path removed from map %s",
531 devname, mpp->alias);
533 free_pathvec(rpvec, KEEP_PATHS);
536 char alias[WWID_SIZE];
539 * flush_map will fail if the device is open
541 strncpy(alias, mpp->alias, WWID_SIZE);
542 if (flush_map(mpp, vecs))
545 condlog(3, "%s: removed map", alias);
550 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
551 vector_del_slot(vecs->pathvec, i);
558 remove_map(mpp, vecs, stop_waiter_thread, 1);
563 map_discovery (struct vectors * vecs)
565 struct multipath * mpp;
568 if (dm_get_maps(vecs->mpvec, "multipath"))
571 vector_foreach_slot (vecs->mpvec, mpp, i)
572 if (setup_multipath(vecs, mpp))
579 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
581 struct vectors * vecs;
586 vecs = (struct vectors *)trigger_data;
588 pthread_cleanup_push(cleanup_lock, vecs->lock);
591 r = parse_cmd(str, reply, len, vecs);
594 *reply = STRDUP("fail\n");
595 *len = strlen(*reply) + 1;
598 else if (!r && *len == 0) {
599 *reply = STRDUP("ok\n");
600 *len = strlen(*reply) + 1;
603 /* else if (r < 0) leave *reply alone */
605 lock_cleanup_pop(vecs->lock);
610 uev_discard(char * devpath)
615 * keep only block devices, discard partitions
617 if (sscanf(devpath, "/block/%10s", a) != 1 ||
618 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
619 condlog(4, "discard event on %s", devpath);
626 uev_trigger (struct uevent * uev, void * trigger_data)
629 struct sysfs_device *sysdev;
630 struct vectors * vecs;
632 vecs = (struct vectors *)trigger_data;
634 if (uev_discard(uev->devpath))
637 sysdev = sysfs_device_get(uev->devpath);
645 * Add events are ignored here as the tables
646 * are not fully initialised then.
648 if (!strncmp(sysdev->kernel, "dm-", 3)) {
649 if (!strncmp(uev->action, "change", 6)) {
650 r = uev_add_map(sysdev, vecs);
653 if (!strncmp(uev->action, "remove", 6)) {
654 r = uev_remove_map(sysdev, vecs);
657 if (!strncmp(uev->action, "umount", 6)) {
658 r = uev_umount_map(sysdev, vecs);
665 * path add/remove event
667 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
671 if (!strncmp(uev->action, "add", 3)) {
672 r = uev_add_path(sysdev, vecs);
675 if (!strncmp(uev->action, "remove", 6)) {
676 r = uev_remove_path(sysdev, vecs);
686 ueventloop (void * ap)
688 if (uevent_listen(&uev_trigger, ap))
689 fprintf(stderr, "error starting uevent listener");
695 uxlsnrloop (void * ap)
700 set_handler_callback(LIST+PATHS, cli_list_paths);
701 set_handler_callback(LIST+MAPS, cli_list_maps);
702 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
703 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
704 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
705 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
706 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
707 set_handler_callback(LIST+CONFIG, cli_list_config);
708 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
709 set_handler_callback(LIST+DEVICES, cli_list_devices);
710 set_handler_callback(ADD+PATH, cli_add_path);
711 set_handler_callback(DEL+PATH, cli_del_path);
712 set_handler_callback(ADD+MAP, cli_add_map);
713 set_handler_callback(DEL+MAP, cli_del_map);
714 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
715 set_handler_callback(RECONFIGURE, cli_reconfigure);
716 set_handler_callback(SUSPEND+MAP, cli_suspend);
717 set_handler_callback(RESUME+MAP, cli_resume);
718 set_handler_callback(REINSTATE+PATH, cli_reinstate);
719 set_handler_callback(FAIL+PATH, cli_fail);
721 uxsock_listen(&uxsock_trigger, ap);
727 exit_daemon (int status)
730 fprintf(stderr, "bad exit status. see daemon.log\n");
732 condlog(3, "unlink pidfile");
733 unlink(DEFAULT_PIDFILE);
736 pthread_cond_signal(&exit_cond);
743 fail_path (struct path * pp, int del_active)
748 condlog(2, "checker failed path %s in map %s",
749 pp->dev_t, pp->mpp->alias);
751 dm_fail_path(pp->mpp->alias, pp->dev_t);
753 update_queue_mode_del_path(pp->mpp);
757 * caller must have locked the path list before calling that function
760 reinstate_path (struct path * pp, int add_active)
765 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
766 condlog(0, "%s: reinstate failed", pp->dev_t);
768 condlog(2, "%s: reinstated", pp->dev_t);
770 update_queue_mode_add_path(pp->mpp);
775 enable_group(struct path * pp)
777 struct pathgroup * pgp;
780 * if path is added through uev_add_path, pgindex can be unset.
781 * next update_strings() will set it, upon map reload event.
783 * we can safely return here, because upon map reload, all
784 * PG will be enabled.
786 if (!pp->mpp->pg || !pp->pgindex)
789 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
791 if (pgp->status == PGSTATE_DISABLED) {
792 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
793 dm_enablegroup(pp->mpp->alias, pp->pgindex);
798 mpvec_garbage_collector (struct vectors * vecs)
800 struct multipath * mpp;
803 vector_foreach_slot (vecs->mpvec, mpp, i) {
804 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
805 condlog(2, "%s: remove dead map", mpp->alias);
806 remove_map(mpp, vecs, stop_waiter_thread, 1);
813 defered_failback_tick (vector mpvec)
815 struct multipath * mpp;
818 vector_foreach_slot (mpvec, mpp, i) {
820 * defered failback getting sooner
822 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
823 mpp->failback_tick--;
825 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
826 switch_pathgroup(mpp);
832 retry_count_tick(vector mpvec)
834 struct multipath *mpp;
837 vector_foreach_slot (mpvec, mpp, i) {
838 if (mpp->retry_tick) {
839 mpp->stat_total_queueing_time++;
840 condlog(4, "%s: Retrying.. No active path", mpp->alias);
841 if(--mpp->retry_tick == 0) {
842 dm_queue_if_no_path(mpp->alias, 0);
843 condlog(2, "%s: Disable queueing", mpp->alias);
850 checkerloop (void *ap)
852 struct vectors *vecs;
858 mlockall(MCL_CURRENT | MCL_FUTURE);
859 vecs = (struct vectors *)ap;
860 condlog(2, "path checkers start up");
863 * init the path check interval
865 vector_foreach_slot (vecs->pathvec, pp, i) {
866 pp->checkint = conf->checkint;
870 pthread_cleanup_push(cleanup_lock, vecs->lock);
874 vector_foreach_slot (vecs->pathvec, pp, i) {
878 if (pp->tick && --pp->tick)
879 continue; /* don't check this path yet */
882 * provision a next check soonest,
883 * in case we exit abnormaly from here
885 pp->tick = conf->checkint;
887 if (!checker_selected(&pp->checker)) {
888 pathinfo(pp, conf->hwtable, DI_SYSFS);
892 if (!checker_selected(&pp->checker)) {
893 condlog(0, "%s: checker is not set", pp->dev);
896 newstate = checker_check(&pp->checker);
899 condlog(2, "%s: unusable path", pp->dev);
900 pathinfo(pp, conf->hwtable, 0);
904 * Async IO in flight. Keep the previous path state
905 * and reschedule as soon as possible
907 if (newstate == PATH_PENDING) {
911 if (newstate != pp->state) {
912 int oldstate = pp->state;
913 pp->state = newstate;
914 LOG_MSG(1, checker_message(&pp->checker));
917 * upon state change, reset the checkint
918 * to the shortest delay
920 pp->checkint = conf->checkint;
922 if (newstate == PATH_DOWN ||
923 newstate == PATH_SHAKY ||
924 update_multipath_strings(pp->mpp,
927 * proactively fail path in the DM
929 if (oldstate == PATH_UP ||
930 oldstate == PATH_GHOST)
936 * cancel scheduled failback
938 pp->mpp->failback_tick = 0;
940 pp->mpp->stat_path_failures++;
945 * reinstate this path
947 if (oldstate != PATH_UP &&
948 oldstate != PATH_GHOST)
949 reinstate_path(pp, 1);
951 reinstate_path(pp, 0);
954 * schedule [defered] failback
956 if (pp->mpp->pgfailback > 0)
957 pp->mpp->failback_tick =
958 pp->mpp->pgfailback + 1;
959 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
960 need_switch_pathgroup(pp->mpp, 1))
961 switch_pathgroup(pp->mpp);
964 * if at least one path is up in a group, and
965 * the group is disabled, re-enable it
967 if (newstate == PATH_UP)
970 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
971 LOG_MSG(4, checker_message(&pp->checker));
973 * double the next check delay.
974 * max at conf->max_checkint
976 if (pp->checkint < (conf->max_checkint / 2))
977 pp->checkint = 2 * pp->checkint;
979 pp->checkint = conf->max_checkint;
981 pp->tick = pp->checkint;
982 condlog(4, "%s: delay next check %is",
983 pp->dev_t, pp->tick);
985 else if (newstate == PATH_DOWN)
986 LOG_MSG(2, checker_message(&pp->checker));
988 pp->state = newstate;
991 * path prio refreshing
993 condlog(4, "path prio refresh");
994 pathinfo(pp, conf->hwtable, DI_PRIO);
996 if (need_switch_pathgroup(pp->mpp, 0)) {
997 if (pp->mpp->pgfailback > 0 &&
998 pp->mpp->failback_tick <= 0)
999 pp->mpp->failback_tick =
1000 pp->mpp->pgfailback + 1;
1001 else if (pp->mpp->pgfailback ==
1002 -FAILBACK_IMMEDIATE)
1003 switch_pathgroup(pp->mpp);
1006 defered_failback_tick(vecs->mpvec);
1007 retry_count_tick(vecs->mpvec);
1012 condlog(4, "map garbage collection");
1013 mpvec_garbage_collector(vecs);
1017 lock_cleanup_pop(vecs->lock);
1024 configure (struct vectors * vecs, int start_waiters)
1026 struct multipath * mpp;
1031 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1034 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1037 if (!(mpvec = vector_alloc()))
1041 * probe for current path (from sysfs) and map (from dm) sets
1043 path_discovery(vecs->pathvec, conf, DI_ALL);
1045 vector_foreach_slot (vecs->pathvec, pp, i){
1046 if (filter_path(conf, pp)){
1047 vector_del_slot(vecs->pathvec, i);
1052 pp->checkint = conf->checkint;
1054 if (map_discovery(vecs))
1058 * create new set of maps & push changed ones into dm
1060 if (coalesce_paths(vecs, mpvec, NULL))
1064 * may need to remove some maps which are no longer relevant
1065 * e.g., due to blacklist changes in conf file
1067 if (coalesce_maps(vecs, mpvec))
1072 sync_maps_state(mpvec);
1075 * purge dm of old maps
1077 remove_maps(vecs, NULL);
1080 * save new set of maps formed by considering current path state
1082 vector_free(vecs->mpvec);
1083 vecs->mpvec = mpvec;
1086 * start dm event waiter threads for these new maps
1088 vector_foreach_slot(vecs->mpvec, mpp, i) {
1089 if (setup_multipath(vecs, mpp))
1092 if (start_waiter_thread(mpp, vecs))
1099 reconfigure (struct vectors * vecs)
1101 struct config * old = conf;
1104 * free old map and path vectors ... they use old conf state
1106 if (VECTOR_SIZE(vecs->mpvec))
1107 remove_maps(vecs, stop_waiter_thread);
1109 if (VECTOR_SIZE(vecs->pathvec))
1110 free_pathvec(vecs->pathvec, FREE_PATHS);
1112 vecs->pathvec = NULL;
1115 if (load_config(DEFAULT_CONFIGFILE))
1118 conf->verbosity = old->verbosity;
1120 if (!conf->checkint) {
1121 conf->checkint = DEFAULT_CHECKINT;
1122 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1129 static struct vectors *
1132 struct vectors * vecs;
1134 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1140 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1145 pthread_mutex_init(vecs->lock, NULL);
1151 condlog(0, "failed to init paths");
1156 signal_set(int signo, void (*func) (int))
1159 struct sigaction sig;
1160 struct sigaction osig;
1162 sig.sa_handler = func;
1163 sigemptyset(&sig.sa_mask);
1166 r = sigaction(signo, &sig, &osig);
1171 return (osig.sa_handler);
1177 condlog(2, "reconfigure (SIGHUP)");
1181 unlock(gvecs->lock);
1184 dbg_free_final(NULL);
1197 condlog(3, "SIGUSR1 received");
1203 signal_set(SIGHUP, sighup);
1204 signal_set(SIGUSR1, sigusr1);
1205 signal_set(SIGINT, sigend);
1206 signal_set(SIGTERM, sigend);
1207 signal(SIGPIPE, SIG_IGN);
1214 static struct sched_param sched_param = {
1215 .sched_priority = 99
1218 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1221 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1226 set_oom_adj (int val)
1230 fp = fopen("/proc/self/oom_adj", "w");
1235 fprintf(fp, "%i", val);
1240 child (void * param)
1242 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1243 pthread_attr_t attr;
1244 struct vectors * vecs;
1246 mlockall(MCL_CURRENT | MCL_FUTURE);
1251 condlog(2, "--------start up--------");
1252 condlog(2, "read " DEFAULT_CONFIGFILE);
1254 if (load_config(DEFAULT_CONFIGFILE))
1257 setlogmask(LOG_UPTO(conf->verbosity + 3));
1260 * fill the voids left in the config file
1262 if (!conf->checkint) {
1263 conf->checkint = DEFAULT_CHECKINT;
1264 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1267 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1276 vecs = gvecs = init_vecs();
1281 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1282 condlog(0, "can not find sysfs mount point");
1287 * fetch and configure both paths and multipaths
1289 if (configure(vecs, 1)) {
1290 condlog(0, "failure during configuration");
1297 pthread_attr_init(&attr);
1298 pthread_attr_setstacksize(&attr, 64 * 1024);
1299 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1301 pthread_create(&check_thr, &attr, checkerloop, vecs);
1302 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1303 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1305 pthread_cond_wait(&exit_cond, &exit_mutex);
1311 remove_maps(vecs, stop_waiter_thread);
1312 free_pathvec(vecs->pathvec, FREE_PATHS);
1314 pthread_cancel(check_thr);
1315 pthread_cancel(uevent_thr);
1316 pthread_cancel(uxlsnr_thr);
1322 free_handlers(handlers);
1327 pthread_mutex_destroy(vecs->lock);
1335 condlog(2, "--------shut down-------");
1344 dbg_free_final(NULL);
1356 if( (pid = fork()) < 0){
1357 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1365 if ( (pid = fork()) < 0)
1366 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1370 in_fd = open("/dev/null", O_RDONLY);
1372 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1376 out_fd = open("/dev/console", O_WRONLY);
1378 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1383 close(STDIN_FILENO);
1385 close(STDOUT_FILENO);
1387 close(STDERR_FILENO);
1398 main (int argc, char *argv[])
1400 extern char *optarg;
1408 if (getuid() != 0) {
1409 fprintf(stderr, "need to be root\n");
1413 /* make sure we don't lock any path */
1415 umask(umask(077) | 022);
1417 conf = alloc_config();
1422 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1426 //debug=1; /* ### comment me out ### */
1429 if (sizeof(optarg) > sizeof(char *) ||
1430 !isdigit(optarg[0]))
1433 conf->verbosity = atoi(optarg);
1456 return (child(NULL));