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)
447 condlog(2, "%s: remove path (uevent)", dev->kernel);
448 retval = ev_remove_path(dev->kernel, vecs);
450 sysfs_device_put(dev);
456 ev_remove_path (char * devname, struct vectors * vecs)
458 struct multipath * mpp;
463 pp = find_path_by_dev(vecs->pathvec, devname);
466 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
471 * avoid referring to the map of an orphanned path
473 if ((mpp = pp->mpp)) {
476 * remove the map IFF removing the last path
478 if (pathcount(mpp, PATH_WILD) > 1) {
479 vector rpvec = vector_alloc();
482 * transform the mp->pg vector of vectors of paths
483 * into a mp->params string to feed the device-mapper
485 update_mpp_paths(mpp, vecs->pathvec);
486 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
487 vector_del_slot(mpp->paths, i);
489 if (VECTOR_SIZE(mpp->paths) == 0) {
490 char alias[WWID_SIZE];
493 * flush_map will fail if the device is open
495 strncpy(alias, mpp->alias, WWID_SIZE);
496 if (flush_map(mpp, vecs))
499 condlog(3, "%s: removed map after removing"
500 " multiple paths", alias);
503 if (setup_map(mpp)) {
504 condlog(0, "%s: failed to setup map for"
505 " removal of path %s", mpp->alias, devname);
506 free_pathvec(rpvec, KEEP_PATHS);
512 mpp->action = ACT_RELOAD;
513 if (domap(mpp) <= 0) {
514 condlog(0, "%s: failed in domap for "
515 "removal of path %s",
516 mpp->alias, devname);
518 * Delete path from pathvec so that
519 * update_mpp_paths wont find it later
520 * when/if another path is removed.
522 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
523 vector_del_slot(vecs->pathvec, i);
528 * update our state from kernel
530 if (setup_multipath(vecs, mpp)) {
531 free_pathvec(rpvec, KEEP_PATHS);
536 condlog(3, "%s: path removed from map %s",
537 devname, mpp->alias);
539 free_pathvec(rpvec, KEEP_PATHS);
542 char alias[WWID_SIZE];
545 * flush_map will fail if the device is open
547 strncpy(alias, mpp->alias, WWID_SIZE);
548 if (flush_map(mpp, vecs))
551 condlog(3, "%s: removed map", alias);
556 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
557 vector_del_slot(vecs->pathvec, i);
564 remove_map(mpp, vecs, stop_waiter_thread, 1);
569 map_discovery (struct vectors * vecs)
571 struct multipath * mpp;
574 if (dm_get_maps(vecs->mpvec, "multipath"))
577 vector_foreach_slot (vecs->mpvec, mpp, i)
578 if (setup_multipath(vecs, mpp))
585 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
587 struct vectors * vecs;
592 vecs = (struct vectors *)trigger_data;
594 pthread_cleanup_push(cleanup_lock, vecs->lock);
597 r = parse_cmd(str, reply, len, vecs);
600 *reply = STRDUP("fail\n");
601 *len = strlen(*reply) + 1;
604 else if (!r && *len == 0) {
605 *reply = STRDUP("ok\n");
606 *len = strlen(*reply) + 1;
609 /* else if (r < 0) leave *reply alone */
611 lock_cleanup_pop(vecs->lock);
616 uev_discard(char * devpath)
621 * keep only block devices, discard partitions
623 if (sscanf(devpath, "/block/%10s", a) != 1 ||
624 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
625 condlog(4, "discard event on %s", devpath);
632 uev_trigger (struct uevent * uev, void * trigger_data)
635 struct sysfs_device *sysdev;
636 struct vectors * vecs;
638 vecs = (struct vectors *)trigger_data;
640 if (uev_discard(uev->devpath))
643 sysdev = sysfs_device_get(uev->devpath);
651 * Add events are ignored here as the tables
652 * are not fully initialised then.
654 if (!strncmp(sysdev->kernel, "dm-", 3)) {
655 if (!strncmp(uev->action, "change", 6)) {
656 r = uev_add_map(sysdev, vecs);
659 if (!strncmp(uev->action, "remove", 6)) {
660 r = uev_remove_map(sysdev, vecs);
663 if (!strncmp(uev->action, "umount", 6)) {
664 r = uev_umount_map(sysdev, vecs);
671 * path add/remove event
673 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
677 if (!strncmp(uev->action, "add", 3)) {
678 r = uev_add_path(sysdev, vecs);
681 if (!strncmp(uev->action, "remove", 6)) {
682 r = uev_remove_path(sysdev, vecs);
692 ueventloop (void * ap)
694 if (uevent_listen(&uev_trigger, ap))
695 fprintf(stderr, "error starting uevent listener");
701 uxlsnrloop (void * ap)
706 set_handler_callback(LIST+PATHS, cli_list_paths);
707 set_handler_callback(LIST+MAPS, cli_list_maps);
708 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
709 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
710 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
711 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
712 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
713 set_handler_callback(LIST+CONFIG, cli_list_config);
714 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
715 set_handler_callback(LIST+DEVICES, cli_list_devices);
716 set_handler_callback(ADD+PATH, cli_add_path);
717 set_handler_callback(DEL+PATH, cli_del_path);
718 set_handler_callback(ADD+MAP, cli_add_map);
719 set_handler_callback(DEL+MAP, cli_del_map);
720 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
721 set_handler_callback(RECONFIGURE, cli_reconfigure);
722 set_handler_callback(SUSPEND+MAP, cli_suspend);
723 set_handler_callback(RESUME+MAP, cli_resume);
724 set_handler_callback(REINSTATE+PATH, cli_reinstate);
725 set_handler_callback(FAIL+PATH, cli_fail);
727 uxsock_listen(&uxsock_trigger, ap);
733 exit_daemon (int status)
736 fprintf(stderr, "bad exit status. see daemon.log\n");
738 condlog(3, "unlink pidfile");
739 unlink(DEFAULT_PIDFILE);
742 pthread_cond_signal(&exit_cond);
749 fail_path (struct path * pp, int del_active)
754 condlog(2, "checker failed path %s in map %s",
755 pp->dev_t, pp->mpp->alias);
757 dm_fail_path(pp->mpp->alias, pp->dev_t);
759 update_queue_mode_del_path(pp->mpp);
763 * caller must have locked the path list before calling that function
766 reinstate_path (struct path * pp, int add_active)
771 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
772 condlog(0, "%s: reinstate failed", pp->dev_t);
774 condlog(2, "%s: reinstated", pp->dev_t);
776 update_queue_mode_add_path(pp->mpp);
781 enable_group(struct path * pp)
783 struct pathgroup * pgp;
786 * if path is added through uev_add_path, pgindex can be unset.
787 * next update_strings() will set it, upon map reload event.
789 * we can safely return here, because upon map reload, all
790 * PG will be enabled.
792 if (!pp->mpp->pg || !pp->pgindex)
795 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
797 if (pgp->status == PGSTATE_DISABLED) {
798 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
799 dm_enablegroup(pp->mpp->alias, pp->pgindex);
804 mpvec_garbage_collector (struct vectors * vecs)
806 struct multipath * mpp;
809 vector_foreach_slot (vecs->mpvec, mpp, i) {
810 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
811 condlog(2, "%s: remove dead map", mpp->alias);
812 remove_map(mpp, vecs, stop_waiter_thread, 1);
819 defered_failback_tick (vector mpvec)
821 struct multipath * mpp;
824 vector_foreach_slot (mpvec, mpp, i) {
826 * defered failback getting sooner
828 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
829 mpp->failback_tick--;
831 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
832 switch_pathgroup(mpp);
838 retry_count_tick(vector mpvec)
840 struct multipath *mpp;
843 vector_foreach_slot (mpvec, mpp, i) {
844 if (mpp->retry_tick) {
845 mpp->stat_total_queueing_time++;
846 condlog(4, "%s: Retrying.. No active path", mpp->alias);
847 if(--mpp->retry_tick == 0) {
848 dm_queue_if_no_path(mpp->alias, 0);
849 condlog(2, "%s: Disable queueing", mpp->alias);
856 checkerloop (void *ap)
858 struct vectors *vecs;
864 mlockall(MCL_CURRENT | MCL_FUTURE);
865 vecs = (struct vectors *)ap;
866 condlog(2, "path checkers start up");
869 * init the path check interval
871 vector_foreach_slot (vecs->pathvec, pp, i) {
872 pp->checkint = conf->checkint;
876 pthread_cleanup_push(cleanup_lock, vecs->lock);
880 vector_foreach_slot (vecs->pathvec, pp, i) {
884 if (pp->tick && --pp->tick)
885 continue; /* don't check this path yet */
888 * provision a next check soonest,
889 * in case we exit abnormaly from here
891 pp->tick = conf->checkint;
893 if (!checker_selected(&pp->checker)) {
894 pathinfo(pp, conf->hwtable, DI_SYSFS);
897 if (!checker_selected(&pp->checker)) {
898 condlog(0, "%s: checker is not set", pp->dev);
902 * Set checker in async mode.
903 * Honored only by checker implementing the said mode.
905 checker_set_async(&pp->checker);
907 newstate = checker_check(&pp->checker);
910 condlog(2, "%s: unusable path", pp->dev);
911 pathinfo(pp, conf->hwtable, 0);
915 * Async IO in flight. Keep the previous path state
916 * and reschedule as soon as possible
918 if (newstate == PATH_PENDING) {
922 if (newstate != pp->state) {
923 int oldstate = pp->state;
924 pp->state = newstate;
925 LOG_MSG(1, checker_message(&pp->checker));
928 * upon state change, reset the checkint
929 * to the shortest delay
931 pp->checkint = conf->checkint;
933 if (newstate == PATH_DOWN ||
934 newstate == PATH_SHAKY ||
935 update_multipath_strings(pp->mpp,
938 * proactively fail path in the DM
940 if (oldstate == PATH_UP ||
941 oldstate == PATH_GHOST)
947 * cancel scheduled failback
949 pp->mpp->failback_tick = 0;
951 pp->mpp->stat_path_failures++;
956 * reinstate this path
958 if (oldstate != PATH_UP &&
959 oldstate != PATH_GHOST)
960 reinstate_path(pp, 1);
962 reinstate_path(pp, 0);
965 * schedule [defered] failback
967 if (pp->mpp->pgfailback > 0)
968 pp->mpp->failback_tick =
969 pp->mpp->pgfailback + 1;
970 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
971 need_switch_pathgroup(pp->mpp, 1))
972 switch_pathgroup(pp->mpp);
975 * if at least one path is up in a group, and
976 * the group is disabled, re-enable it
978 if (newstate == PATH_UP)
981 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
982 LOG_MSG(4, checker_message(&pp->checker));
984 * double the next check delay.
985 * max at conf->max_checkint
987 if (pp->checkint < (conf->max_checkint / 2))
988 pp->checkint = 2 * pp->checkint;
990 pp->checkint = conf->max_checkint;
992 pp->tick = pp->checkint;
993 condlog(4, "%s: delay next check %is",
994 pp->dev_t, pp->tick);
996 else if (newstate == PATH_DOWN)
997 LOG_MSG(2, checker_message(&pp->checker));
999 pp->state = newstate;
1002 * path prio refreshing
1004 condlog(4, "path prio refresh");
1005 pathinfo(pp, conf->hwtable, DI_PRIO);
1007 if (need_switch_pathgroup(pp->mpp, 0)) {
1008 if (pp->mpp->pgfailback > 0 &&
1009 pp->mpp->failback_tick <= 0)
1010 pp->mpp->failback_tick =
1011 pp->mpp->pgfailback + 1;
1012 else if (pp->mpp->pgfailback ==
1013 -FAILBACK_IMMEDIATE)
1014 switch_pathgroup(pp->mpp);
1017 defered_failback_tick(vecs->mpvec);
1018 retry_count_tick(vecs->mpvec);
1023 condlog(4, "map garbage collection");
1024 mpvec_garbage_collector(vecs);
1028 lock_cleanup_pop(vecs->lock);
1035 configure (struct vectors * vecs, int start_waiters)
1037 struct multipath * mpp;
1042 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1045 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1048 if (!(mpvec = vector_alloc()))
1052 * probe for current path (from sysfs) and map (from dm) sets
1054 path_discovery(vecs->pathvec, conf, DI_ALL);
1056 vector_foreach_slot (vecs->pathvec, pp, i){
1057 if (filter_path(conf, pp)){
1058 vector_del_slot(vecs->pathvec, i);
1063 pp->checkint = conf->checkint;
1065 if (map_discovery(vecs))
1069 * create new set of maps & push changed ones into dm
1071 if (coalesce_paths(vecs, mpvec, NULL))
1075 * may need to remove some maps which are no longer relevant
1076 * e.g., due to blacklist changes in conf file
1078 if (coalesce_maps(vecs, mpvec))
1083 sync_maps_state(mpvec);
1086 * purge dm of old maps
1088 remove_maps(vecs, NULL);
1091 * save new set of maps formed by considering current path state
1093 vector_free(vecs->mpvec);
1094 vecs->mpvec = mpvec;
1097 * start dm event waiter threads for these new maps
1099 vector_foreach_slot(vecs->mpvec, mpp, i) {
1100 if (setup_multipath(vecs, mpp))
1103 if (start_waiter_thread(mpp, vecs))
1110 reconfigure (struct vectors * vecs)
1112 struct config * old = conf;
1115 * free old map and path vectors ... they use old conf state
1117 if (VECTOR_SIZE(vecs->mpvec))
1118 remove_maps(vecs, stop_waiter_thread);
1120 if (VECTOR_SIZE(vecs->pathvec))
1121 free_pathvec(vecs->pathvec, FREE_PATHS);
1123 vecs->pathvec = NULL;
1126 if (load_config(DEFAULT_CONFIGFILE))
1129 conf->verbosity = old->verbosity;
1131 if (!conf->checkint) {
1132 conf->checkint = DEFAULT_CHECKINT;
1133 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1140 static struct vectors *
1143 struct vectors * vecs;
1145 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1151 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1156 pthread_mutex_init(vecs->lock, NULL);
1162 condlog(0, "failed to init paths");
1167 signal_set(int signo, void (*func) (int))
1170 struct sigaction sig;
1171 struct sigaction osig;
1173 sig.sa_handler = func;
1174 sigemptyset(&sig.sa_mask);
1177 r = sigaction(signo, &sig, &osig);
1182 return (osig.sa_handler);
1188 condlog(2, "reconfigure (SIGHUP)");
1192 unlock(gvecs->lock);
1195 dbg_free_final(NULL);
1208 condlog(3, "SIGUSR1 received");
1214 signal_set(SIGHUP, sighup);
1215 signal_set(SIGUSR1, sigusr1);
1216 signal_set(SIGINT, sigend);
1217 signal_set(SIGTERM, sigend);
1218 signal(SIGPIPE, SIG_IGN);
1225 static struct sched_param sched_param = {
1226 .sched_priority = 99
1229 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1232 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1237 set_oom_adj (int val)
1241 fp = fopen("/proc/self/oom_adj", "w");
1246 fprintf(fp, "%i", val);
1251 child (void * param)
1253 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1254 pthread_attr_t attr;
1255 struct vectors * vecs;
1257 mlockall(MCL_CURRENT | MCL_FUTURE);
1262 condlog(2, "--------start up--------");
1263 condlog(2, "read " DEFAULT_CONFIGFILE);
1265 if (load_config(DEFAULT_CONFIGFILE))
1268 setlogmask(LOG_UPTO(conf->verbosity + 3));
1271 * fill the voids left in the config file
1273 if (!conf->checkint) {
1274 conf->checkint = DEFAULT_CHECKINT;
1275 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1278 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1287 vecs = gvecs = init_vecs();
1292 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1293 condlog(0, "can not find sysfs mount point");
1298 * fetch and configure both paths and multipaths
1300 if (configure(vecs, 1)) {
1301 condlog(0, "failure during configuration");
1308 pthread_attr_init(&attr);
1309 pthread_attr_setstacksize(&attr, 64 * 1024);
1310 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1312 pthread_create(&check_thr, &attr, checkerloop, vecs);
1313 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1314 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1316 pthread_cond_wait(&exit_cond, &exit_mutex);
1322 remove_maps(vecs, stop_waiter_thread);
1323 free_pathvec(vecs->pathvec, FREE_PATHS);
1325 pthread_cancel(check_thr);
1326 pthread_cancel(uevent_thr);
1327 pthread_cancel(uxlsnr_thr);
1333 free_handlers(handlers);
1338 pthread_mutex_destroy(vecs->lock);
1346 condlog(2, "--------shut down-------");
1355 dbg_free_final(NULL);
1367 if( (pid = fork()) < 0){
1368 fprintf(stderr, "Failed first fork : %s\n", strerror(errno));
1376 if ( (pid = fork()) < 0)
1377 fprintf(stderr, "Failed second fork : %s\n", strerror(errno));
1381 in_fd = open("/dev/null", O_RDONLY);
1383 fprintf(stderr, "cannot open /dev/null for input : %s\n",
1387 out_fd = open("/dev/console", O_WRONLY);
1389 fprintf(stderr, "cannot open /dev/console for output : %s\n",
1394 close(STDIN_FILENO);
1396 close(STDOUT_FILENO);
1398 close(STDERR_FILENO);
1409 main (int argc, char *argv[])
1411 extern char *optarg;
1419 if (getuid() != 0) {
1420 fprintf(stderr, "need to be root\n");
1424 /* make sure we don't lock any path */
1426 umask(umask(077) | 022);
1428 conf = alloc_config();
1433 while ((arg = getopt(argc, argv, ":dv:k::")) != EOF ) {
1437 //debug=1; /* ### comment me out ### */
1440 if (sizeof(optarg) > sizeof(char *) ||
1441 !isdigit(optarg[0]))
1444 conf->verbosity = atoi(optarg);
1467 return (child(NULL));