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;
69 * global copy of vecs for use in sig handlers
71 struct vectors * gvecs;
74 need_switch_pathgroup (struct multipath * mpp, int refresh)
76 struct pathgroup * pgp;
80 if (!mpp || mpp->pgfailback == -FAILBACK_MANUAL)
84 * Refresh path priority values
87 vector_foreach_slot (mpp->pg, pgp, i)
88 vector_foreach_slot (pgp->paths, pp, j)
89 pathinfo(pp, conf->hwtable, DI_PRIO);
91 mpp->bestpg = select_path_group(mpp);
93 if (mpp->bestpg != mpp->nextpg)
100 switch_pathgroup (struct multipath * mpp)
102 mpp->stat_switchgroup++;
103 dm_switchgroup(mpp->alias, mpp->bestpg);
104 condlog(2, "%s: switch to path group #%i",
105 mpp->alias, mpp->bestpg);
109 coalesce_maps(struct vectors *vecs, vector nmpv)
111 struct multipath * ompp;
112 vector ompv = vecs->mpvec;
116 vector_foreach_slot (ompv, ompp, i) {
117 if (!find_mp_by_wwid(nmpv, ompp->wwid)) {
119 * remove all current maps not allowed by the
120 * current configuration
122 if (dm_flush_map(ompp->alias, DEFAULT_TARGET)) {
123 condlog(0, "%s: unable to flush devmap",
126 * may be just because the device is open
128 if (!vector_alloc_slot(nmpv))
131 vector_set_slot(nmpv, ompp);
132 setup_multipath(vecs, ompp);
134 if ((j = find_slot(ompv, (void *)ompp)) != -1)
135 vector_del_slot(ompv, j);
141 condlog(3, "%s devmap removed", ompp->alias);
149 sync_map_state(struct multipath *mpp)
151 struct pathgroup *pgp;
155 vector_foreach_slot (mpp->pg, pgp, i){
156 vector_foreach_slot (pgp->paths, pp, j){
157 if (pp->state <= PATH_UNCHECKED)
159 if ((pp->dmstate == PSTATE_FAILED ||
160 pp->dmstate == PSTATE_UNDEF) &&
161 (pp->state == PATH_UP || pp->state == PATH_GHOST))
162 dm_reinstate_path(mpp->alias, pp->dev_t);
163 else if ((pp->dmstate == PSTATE_ACTIVE ||
164 pp->dmstate == PSTATE_UNDEF) &&
165 (pp->state == PATH_DOWN ||
166 pp->state == PATH_SHAKY))
167 dm_fail_path(mpp->alias, pp->dev_t);
173 sync_maps_state(vector mpvec)
176 struct multipath *mpp;
178 vector_foreach_slot (mpvec, mpp, i)
183 flush_map(struct multipath * mpp, struct vectors * vecs)
186 * clear references to this map before flushing so we can ignore
187 * the spurious uevent we may generate with the dm_flush_map call below
189 if (dm_flush_map(mpp->alias, DEFAULT_TARGET)) {
191 * May not really be an error -- if the map was already flushed
192 * from the device mapper by dmsetup(8) for instance.
194 condlog(0, "%s: can't flush", mpp->alias);
199 condlog(3, "%s: devmap removed", mpp->alias);
202 orphan_paths(vecs->pathvec, mpp);
203 remove_map_and_stop_waiter(mpp, vecs, 1);
209 uev_add_map (struct sysfs_device * dev, struct vectors * vecs)
211 condlog(2, "%s: add map (uevent)", dev->kernel);
212 return ev_add_map(dev, vecs);
216 ev_add_map (struct sysfs_device * dev, struct vectors * vecs)
222 struct multipath * mpp;
226 dev_t = sysfs_attr_get_value(dev->devpath, "dev");
228 if (!dev_t || sscanf(dev_t, "%d:%d", &major, &minor) != 2)
231 alias = dm_mapname(major, minor);
236 map_present = dm_map_present(alias);
238 if (map_present && dm_type(alias, DEFAULT_TARGET) <= 0) {
239 condlog(4, "%s: not a multipath map", alias);
243 mpp = find_mp_by_alias(vecs->mpvec, alias);
247 * Not really an error -- we generate our own uevent
248 * if we create a multipath mapped device as a result
251 condlog(0, "%s: devmap already registered",
257 * now we can register the map
259 if (map_present && (mpp = add_map_without_path(vecs, minor, alias))) {
261 condlog(3, "%s: devmap %s added", alias, dev->kernel);
264 refwwid = get_refwwid(dev->kernel, DEV_DEVMAP, vecs->pathvec);
267 r = coalesce_paths(vecs, NULL, refwwid);
272 condlog(3, "%s: devmap %s added", alias, dev->kernel);
274 condlog(0, "%s: uev_add_map %s failed", alias, dev->kernel);
281 uev_remove_map (struct sysfs_device * dev, struct vectors * vecs)
283 condlog(2, "%s: remove map (uevent)", dev->kernel);
284 return ev_remove_map(dev->kernel, vecs);
288 ev_remove_map (char * devname, struct vectors * vecs)
290 struct multipath * mpp;
292 mpp = find_mp_by_str(vecs->mpvec, devname);
295 condlog(3, "%s: devmap not registered, can't remove",
299 flush_map(mpp, vecs);
305 uev_umount_map (struct sysfs_device * dev, struct vectors * vecs)
307 struct multipath * mpp;
309 condlog(2, "%s: umount map (uevent)", dev->kernel);
311 mpp = find_mp_by_str(vecs->mpvec, dev->kernel);
316 update_mpp_paths(mpp, vecs->pathvec);
317 verify_paths(mpp, vecs, NULL);
319 if (!VECTOR_SIZE(mpp->paths))
320 flush_map(mpp, vecs);
326 uev_add_path (struct sysfs_device * dev, struct vectors * vecs)
328 condlog(2, "%s: add path (uevent)", dev->kernel);
329 return (ev_add_path(dev->kernel, vecs) != 1)? 0 : 1;
340 ev_add_path (char * devname, struct vectors * vecs)
342 struct multipath * mpp;
344 char empty_buff[WWID_SIZE] = {0};
346 pp = find_path_by_dev(vecs->pathvec, devname);
349 condlog(0, "%s: spurious uevent, path already in pathvec",
356 * get path vital state
358 if (!(pp = store_pathinfo(vecs->pathvec, conf->hwtable,
360 condlog(0, "%s: failed to store path info", devname);
363 pp->checkint = conf->checkint;
367 * need path UID to go any further
369 if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0) {
370 condlog(0, "%s: failed to get path uid", devname);
371 return 1; /* leave path added to pathvec */
373 if (filter_path(conf, pp) > 0){
374 int i = find_slot(vecs->pathvec, (void *)pp);
376 vector_del_slot(vecs->pathvec, i);
380 mpp = pp->mpp = find_mp_by_wwid(vecs->mpvec, pp->wwid);
383 if (adopt_paths(vecs->pathvec, mpp))
384 return 1; /* leave path added to pathvec */
386 verify_paths(mpp, vecs, NULL);
387 mpp->action = ACT_RELOAD;
390 if ((mpp = add_map_with_path(vecs, pp, 1)))
391 mpp->action = ACT_CREATE;
393 return 1; /* leave path added to pathvec */
397 * push the map to the device-mapper
399 if (setup_map(mpp)) {
400 condlog(0, "%s: failed to setup map for addition of new "
401 "path %s", mpp->alias, devname);
405 * reload the map for the multipath mapped device
407 if (domap(mpp) <= 0) {
408 condlog(0, "%s: failed in domap for addition of new "
409 "path %s", mpp->alias, devname);
411 * deal with asynchronous uevents :((
413 if (mpp->action == ACT_RELOAD) {
414 condlog(0, "%s: uev_add_path sleep", mpp->alias);
416 update_mpp_paths(mpp, vecs->pathvec);
425 * update our state from kernel regardless of create or reload
427 if (setup_multipath(vecs, mpp))
432 if (mpp->action == ACT_CREATE &&
433 start_waiter_thread(mpp, vecs))
436 condlog(3, "%s path added to devmap %s", devname, mpp->alias);
440 remove_map(mpp, vecs, 1);
445 uev_remove_path (struct sysfs_device * dev, struct vectors * vecs)
449 condlog(2, "%s: remove path (uevent)", dev->kernel);
450 retval = ev_remove_path(dev->kernel, vecs);
452 sysfs_device_put(dev);
458 ev_remove_path (char * devname, struct vectors * vecs)
460 struct multipath * mpp;
465 pp = find_path_by_dev(vecs->pathvec, devname);
468 condlog(0, "%s: spurious uevent, path not in pathvec", devname);
473 * avoid referring to the map of an orphanned path
475 if ((mpp = pp->mpp)) {
478 * remove the map IFF removing the last path
480 if (pathcount(mpp, PATH_WILD) > 1) {
481 vector rpvec = vector_alloc();
484 * transform the mp->pg vector of vectors of paths
485 * into a mp->params string to feed the device-mapper
487 update_mpp_paths(mpp, vecs->pathvec);
488 if ((i = find_slot(mpp->paths, (void *)pp)) != -1)
489 vector_del_slot(mpp->paths, i);
491 if (VECTOR_SIZE(mpp->paths) == 0) {
492 char alias[WWID_SIZE];
495 * flush_map will fail if the device is open
497 strncpy(alias, mpp->alias, WWID_SIZE);
498 if (flush_map(mpp, vecs))
501 condlog(3, "%s: removed map after removing"
502 " multiple paths", alias);
505 if (setup_map(mpp)) {
506 condlog(0, "%s: failed to setup map for"
507 " removal of path %s", mpp->alias, devname);
508 free_pathvec(rpvec, KEEP_PATHS);
514 mpp->action = ACT_RELOAD;
515 if (domap(mpp) <= 0) {
516 condlog(0, "%s: failed in domap for "
517 "removal of path %s",
518 mpp->alias, devname);
520 * Delete path from pathvec so that
521 * update_mpp_paths wont find it later
522 * when/if another path is removed.
524 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
525 vector_del_slot(vecs->pathvec, i);
530 * update our state from kernel
532 if (setup_multipath(vecs, mpp)) {
533 free_pathvec(rpvec, KEEP_PATHS);
538 condlog(3, "%s: path removed from map %s",
539 devname, mpp->alias);
541 free_pathvec(rpvec, KEEP_PATHS);
544 char alias[WWID_SIZE];
547 * flush_map will fail if the device is open
549 strncpy(alias, mpp->alias, WWID_SIZE);
550 if (flush_map(mpp, vecs))
553 condlog(3, "%s: removed map", alias);
558 if ((i = find_slot(vecs->pathvec, (void *)pp)) != -1)
559 vector_del_slot(vecs->pathvec, i);
566 remove_map_and_stop_waiter(mpp, vecs, 1);
571 map_discovery (struct vectors * vecs)
573 struct multipath * mpp;
576 if (dm_get_maps(vecs->mpvec, "multipath"))
579 vector_foreach_slot (vecs->mpvec, mpp, i)
580 if (setup_multipath(vecs, mpp))
587 uxsock_trigger (char * str, char ** reply, int * len, void * trigger_data)
589 struct vectors * vecs;
594 vecs = (struct vectors *)trigger_data;
596 pthread_cleanup_push(cleanup_lock, vecs->lock);
599 r = parse_cmd(str, reply, len, vecs);
602 *reply = STRDUP("fail\n");
603 *len = strlen(*reply) + 1;
606 else if (!r && *len == 0) {
607 *reply = STRDUP("ok\n");
608 *len = strlen(*reply) + 1;
611 /* else if (r < 0) leave *reply alone */
613 lock_cleanup_pop(vecs->lock);
618 uev_discard(char * devpath)
623 * keep only block devices, discard partitions
625 if (sscanf(devpath, "/block/%10s", a) != 1 ||
626 sscanf(devpath, "/block/%10[^/]/%10s", a, b) == 2) {
627 condlog(4, "discard event on %s", devpath);
634 uev_trigger (struct uevent * uev, void * trigger_data)
637 struct sysfs_device *sysdev;
638 struct vectors * vecs;
640 vecs = (struct vectors *)trigger_data;
642 if (uev_discard(uev->devpath))
645 sysdev = sysfs_device_get(uev->devpath);
653 * Add events are ignored here as the tables
654 * are not fully initialised then.
656 if (!strncmp(sysdev->kernel, "dm-", 3)) {
657 if (!strncmp(uev->action, "change", 6)) {
658 r = uev_add_map(sysdev, vecs);
661 if (!strncmp(uev->action, "remove", 6)) {
662 r = uev_remove_map(sysdev, vecs);
665 if (!strncmp(uev->action, "umount", 6)) {
666 r = uev_umount_map(sysdev, vecs);
673 * path add/remove event
675 if (filter_devnode(conf->blist_devnode, conf->elist_devnode,
679 if (!strncmp(uev->action, "add", 3)) {
680 r = uev_add_path(sysdev, vecs);
683 if (!strncmp(uev->action, "remove", 6)) {
684 r = uev_remove_path(sysdev, vecs);
694 ueventloop (void * ap)
696 if (uevent_listen(&uev_trigger, ap))
697 fprintf(stderr, "error starting uevent listener");
703 uxlsnrloop (void * ap)
708 set_handler_callback(LIST+PATHS, cli_list_paths);
709 set_handler_callback(LIST+MAPS, cli_list_maps);
710 set_handler_callback(LIST+MAPS+STATUS, cli_list_maps_status);
711 set_handler_callback(LIST+MAPS+STATS, cli_list_maps_stats);
712 set_handler_callback(LIST+MAPS+TOPOLOGY, cli_list_maps_topology);
713 set_handler_callback(LIST+TOPOLOGY, cli_list_maps_topology);
714 set_handler_callback(LIST+MAP+TOPOLOGY, cli_list_map_topology);
715 set_handler_callback(LIST+CONFIG, cli_list_config);
716 set_handler_callback(LIST+BLACKLIST, cli_list_blacklist);
717 set_handler_callback(LIST+DEVICES, cli_list_devices);
718 set_handler_callback(ADD+PATH, cli_add_path);
719 set_handler_callback(DEL+PATH, cli_del_path);
720 set_handler_callback(ADD+MAP, cli_add_map);
721 set_handler_callback(DEL+MAP, cli_del_map);
722 set_handler_callback(SWITCH+MAP+GROUP, cli_switch_group);
723 set_handler_callback(RECONFIGURE, cli_reconfigure);
724 set_handler_callback(SUSPEND+MAP, cli_suspend);
725 set_handler_callback(RESUME+MAP, cli_resume);
726 set_handler_callback(REINSTATE+PATH, cli_reinstate);
727 set_handler_callback(FAIL+PATH, cli_fail);
729 uxsock_listen(&uxsock_trigger, ap);
735 exit_daemon (int status)
738 fprintf(stderr, "bad exit status. see daemon.log\n");
740 condlog(3, "unlink pidfile");
741 unlink(DEFAULT_PIDFILE);
744 pthread_cond_signal(&exit_cond);
751 fail_path (struct path * pp, int del_active)
756 condlog(2, "checker failed path %s in map %s",
757 pp->dev_t, pp->mpp->alias);
759 dm_fail_path(pp->mpp->alias, pp->dev_t);
761 update_queue_mode_del_path(pp->mpp);
765 * caller must have locked the path list before calling that function
768 reinstate_path (struct path * pp, int add_active)
773 if (dm_reinstate_path(pp->mpp->alias, pp->dev_t))
774 condlog(0, "%s: reinstate failed", pp->dev_t);
776 condlog(2, "%s: reinstated", pp->dev_t);
778 update_queue_mode_add_path(pp->mpp);
783 enable_group(struct path * pp)
785 struct pathgroup * pgp;
788 * if path is added through uev_add_path, pgindex can be unset.
789 * next update_strings() will set it, upon map reload event.
791 * we can safely return here, because upon map reload, all
792 * PG will be enabled.
794 if (!pp->mpp->pg || !pp->pgindex)
797 pgp = VECTOR_SLOT(pp->mpp->pg, pp->pgindex - 1);
799 if (pgp->status == PGSTATE_DISABLED) {
800 condlog(2, "%s: enable group #%i", pp->mpp->alias, pp->pgindex);
801 dm_enablegroup(pp->mpp->alias, pp->pgindex);
806 mpvec_garbage_collector (struct vectors * vecs)
808 struct multipath * mpp;
811 vector_foreach_slot (vecs->mpvec, mpp, i) {
812 if (mpp && mpp->alias && !dm_map_present(mpp->alias)) {
813 condlog(2, "%s: remove dead map", mpp->alias);
814 remove_map_and_stop_waiter(mpp, vecs, 1);
821 defered_failback_tick (vector mpvec)
823 struct multipath * mpp;
826 vector_foreach_slot (mpvec, mpp, i) {
828 * defered failback getting sooner
830 if (mpp->pgfailback > 0 && mpp->failback_tick > 0) {
831 mpp->failback_tick--;
833 if (!mpp->failback_tick && need_switch_pathgroup(mpp, 1))
834 switch_pathgroup(mpp);
840 retry_count_tick(vector mpvec)
842 struct multipath *mpp;
845 vector_foreach_slot (mpvec, mpp, i) {
846 if (mpp->retry_tick) {
847 mpp->stat_total_queueing_time++;
848 condlog(4, "%s: Retrying.. No active path", mpp->alias);
849 if(--mpp->retry_tick == 0) {
850 dm_queue_if_no_path(mpp->alias, 0);
851 condlog(2, "%s: Disable queueing", mpp->alias);
858 check_path (struct vectors * vecs, struct path * pp)
865 if (pp->tick && --pp->tick)
866 return; /* don't check this path yet */
869 * provision a next check soonest,
870 * in case we exit abnormaly from here
872 pp->tick = conf->checkint;
874 if (!checker_selected(&pp->checker)) {
875 pathinfo(pp, conf->hwtable, DI_SYSFS);
878 if (!checker_selected(&pp->checker)) {
879 condlog(0, "%s: checker is not set", pp->dev);
883 * Set checker in async mode.
884 * Honored only by checker implementing the said mode.
886 checker_set_async(&pp->checker);
888 newstate = checker_check(&pp->checker);
891 condlog(2, "%s: unusable path", pp->dev);
892 pathinfo(pp, conf->hwtable, 0);
896 * Async IO in flight. Keep the previous path state
897 * and reschedule as soon as possible
899 if (newstate == PATH_PENDING) {
903 if (newstate != pp->state) {
904 int oldstate = pp->state;
905 pp->state = newstate;
906 LOG_MSG(1, checker_message(&pp->checker));
909 * upon state change, reset the checkint
910 * to the shortest delay
912 pp->checkint = conf->checkint;
914 if (newstate == PATH_DOWN || newstate == PATH_SHAKY ||
915 update_multipath_strings(pp->mpp, vecs->pathvec)) {
917 * proactively fail path in the DM
919 if (oldstate == PATH_UP ||
920 oldstate == PATH_GHOST)
926 * cancel scheduled failback
928 pp->mpp->failback_tick = 0;
930 pp->mpp->stat_path_failures++;
935 * reinstate this path
937 if (oldstate != PATH_UP &&
938 oldstate != PATH_GHOST)
939 reinstate_path(pp, 1);
941 reinstate_path(pp, 0);
944 * schedule [defered] failback
946 if (pp->mpp->pgfailback > 0)
947 pp->mpp->failback_tick =
948 pp->mpp->pgfailback + 1;
949 else if (pp->mpp->pgfailback == -FAILBACK_IMMEDIATE &&
950 need_switch_pathgroup(pp->mpp, 1))
951 switch_pathgroup(pp->mpp);
954 * if at least one path is up in a group, and
955 * the group is disabled, re-enable it
957 if (newstate == PATH_UP)
960 else if (newstate == PATH_UP || newstate == PATH_GHOST) {
961 LOG_MSG(4, checker_message(&pp->checker));
963 * double the next check delay.
964 * max at conf->max_checkint
966 if (pp->checkint < (conf->max_checkint / 2))
967 pp->checkint = 2 * pp->checkint;
969 pp->checkint = conf->max_checkint;
971 pp->tick = pp->checkint;
972 condlog(4, "%s: delay next check %is",
973 pp->dev_t, pp->tick);
975 else if (newstate == PATH_DOWN)
976 LOG_MSG(2, checker_message(&pp->checker));
978 pp->state = newstate;
981 * path prio refreshing
983 condlog(4, "path prio refresh");
984 pathinfo(pp, conf->hwtable, DI_PRIO);
987 * pathgroup failback policy
989 if (need_switch_pathgroup(pp->mpp, 0)) {
990 if (pp->mpp->pgfailback > 0 &&
991 pp->mpp->failback_tick <= 0)
992 pp->mpp->failback_tick =
993 pp->mpp->pgfailback + 1;
994 else if (pp->mpp->pgfailback ==
996 switch_pathgroup(pp->mpp);
1001 checkerloop (void *ap)
1003 struct vectors *vecs;
1008 mlockall(MCL_CURRENT | MCL_FUTURE);
1009 vecs = (struct vectors *)ap;
1010 condlog(2, "path checkers start up");
1013 * init the path check interval
1015 vector_foreach_slot (vecs->pathvec, pp, i) {
1016 pp->checkint = conf->checkint;
1020 pthread_cleanup_push(cleanup_lock, vecs->lock);
1024 vector_foreach_slot (vecs->pathvec, pp, i) {
1025 check_path(vecs, pp);
1027 defered_failback_tick(vecs->mpvec);
1028 retry_count_tick(vecs->mpvec);
1033 condlog(4, "map garbage collection");
1034 mpvec_garbage_collector(vecs);
1038 lock_cleanup_pop(vecs->lock);
1045 configure (struct vectors * vecs, int start_waiters)
1047 struct multipath * mpp;
1052 if (!vecs->pathvec && !(vecs->pathvec = vector_alloc()))
1055 if (!vecs->mpvec && !(vecs->mpvec = vector_alloc()))
1058 if (!(mpvec = vector_alloc()))
1062 * probe for current path (from sysfs) and map (from dm) sets
1064 path_discovery(vecs->pathvec, conf, DI_ALL);
1066 vector_foreach_slot (vecs->pathvec, pp, i){
1067 if (filter_path(conf, pp) > 0){
1068 vector_del_slot(vecs->pathvec, i);
1073 pp->checkint = conf->checkint;
1075 if (map_discovery(vecs))
1079 * create new set of maps & push changed ones into dm
1081 if (coalesce_paths(vecs, mpvec, NULL))
1085 * may need to remove some maps which are no longer relevant
1086 * e.g., due to blacklist changes in conf file
1088 if (coalesce_maps(vecs, mpvec))
1093 sync_maps_state(mpvec);
1096 * purge dm of old maps
1101 * save new set of maps formed by considering current path state
1103 vector_free(vecs->mpvec);
1104 vecs->mpvec = mpvec;
1107 * start dm event waiter threads for these new maps
1109 vector_foreach_slot(vecs->mpvec, mpp, i) {
1110 if (setup_multipath(vecs, mpp))
1113 if (start_waiter_thread(mpp, vecs))
1120 reconfigure (struct vectors * vecs)
1122 struct config * old = conf;
1125 * free old map and path vectors ... they use old conf state
1127 if (VECTOR_SIZE(vecs->mpvec))
1128 remove_maps_and_stop_waiters(vecs);
1130 if (VECTOR_SIZE(vecs->pathvec))
1131 free_pathvec(vecs->pathvec, FREE_PATHS);
1133 vecs->pathvec = NULL;
1136 if (load_config(DEFAULT_CONFIGFILE))
1139 conf->verbosity = old->verbosity;
1141 if (!conf->checkint) {
1142 conf->checkint = DEFAULT_CHECKINT;
1143 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1150 static struct vectors *
1153 struct vectors * vecs;
1155 vecs = (struct vectors *)MALLOC(sizeof(struct vectors));
1161 (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
1166 pthread_mutex_init(vecs->lock, NULL);
1172 condlog(0, "failed to init paths");
1177 signal_set(int signo, void (*func) (int))
1180 struct sigaction sig;
1181 struct sigaction osig;
1183 sig.sa_handler = func;
1184 sigemptyset(&sig.sa_mask);
1187 r = sigaction(signo, &sig, &osig);
1192 return (osig.sa_handler);
1198 condlog(2, "reconfigure (SIGHUP)");
1202 unlock(gvecs->lock);
1205 dbg_free_final(NULL);
1218 condlog(3, "SIGUSR1 received");
1224 signal_set(SIGHUP, sighup);
1225 signal_set(SIGUSR1, sigusr1);
1226 signal_set(SIGINT, sigend);
1227 signal_set(SIGTERM, sigend);
1228 signal(SIGPIPE, SIG_IGN);
1235 static struct sched_param sched_param = {
1236 .sched_priority = 99
1239 res = sched_setscheduler (0, SCHED_RR, &sched_param);
1242 condlog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
1247 set_oom_adj (int val)
1251 fp = fopen("/proc/self/oom_adj", "w");
1256 fprintf(fp, "%i", val);
1261 child (void * param)
1263 pthread_t check_thr, uevent_thr, uxlsnr_thr;
1264 pthread_attr_t attr;
1265 struct vectors * vecs;
1267 mlockall(MCL_CURRENT | MCL_FUTURE);
1272 condlog(2, "--------start up--------");
1273 condlog(2, "read " DEFAULT_CONFIGFILE);
1276 condlog(0, "failed to initialize prioritizers");
1279 if (load_config(DEFAULT_CONFIGFILE))
1282 setlogmask(LOG_UPTO(conf->verbosity + 3));
1285 * fill the voids left in the config file
1287 if (!conf->checkint) {
1288 conf->checkint = DEFAULT_CHECKINT;
1289 conf->max_checkint = MAX_CHECKINT(conf->checkint);
1292 if (conf->max_fds) {
1293 struct rlimit fd_limit;
1294 if (conf->max_fds > 0) {
1295 fd_limit.rlim_cur = conf->max_fds;
1296 fd_limit.rlim_max = conf->max_fds;
1299 fd_limit.rlim_cur = RLIM_INFINITY;
1300 fd_limit.rlim_max = RLIM_INFINITY;
1302 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
1303 condlog(0, "can't set open fds limit to %d : %s\n",
1304 conf->max_fds, strerror(errno));
1307 if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
1316 vecs = gvecs = init_vecs();
1321 if (sysfs_init(conf->sysfs_dir, FILE_NAME_SIZE)) {
1322 condlog(0, "can not find sysfs mount point");
1327 * fetch and configure both paths and multipaths
1329 if (configure(vecs, 1)) {
1330 condlog(0, "failure during configuration");
1337 pthread_attr_init(&attr);
1338 pthread_attr_setstacksize(&attr, 64 * 1024);
1339 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1341 pthread_create(&check_thr, &attr, checkerloop, vecs);
1342 pthread_create(&uevent_thr, &attr, ueventloop, vecs);
1343 pthread_create(&uxlsnr_thr, &attr, uxlsnrloop, vecs);
1345 pthread_cond_wait(&exit_cond, &exit_mutex);
1351 remove_maps_and_stop_waiters(vecs);
1352 free_pathvec(vecs->pathvec, FREE_PATHS);
1354 pthread_cancel(check_thr);
1355 pthread_cancel(uevent_thr);
1356 pthread_cancel(uxlsnr_thr);
1362 free_handlers(handlers);
1367 pthread_mutex_destroy(vecs->lock);
1375 condlog(2, "--------shut down-------");
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));