r144 - in multipath-tools/trunk: . devmap_name dmadm klibc-0.190 kpartx libcheckers libdevmapper libmultipath libsysfs multipath multipathd path_priority path_priority/pp_alua path_priority/pp_emc

Bastian Blank waldi at costa.debian.org
Sat Aug 13 15:23:12 UTC 2005


Author: waldi
Date: Sat Aug 13 15:23:02 2005
New Revision: 144

Added:
   multipath-tools/trunk/kpartx/devmapper.c
   multipath-tools/trunk/kpartx/devmapper.h
   multipath-tools/trunk/kpartx/gpt.c.orig
   multipath-tools/trunk/kpartx/kpartx.dev   (contents, props changed)
   multipath-tools/trunk/libmultipath/cache.c
   multipath-tools/trunk/libmultipath/cache.h
   multipath-tools/trunk/libmultipath/config.c
   multipath-tools/trunk/libmultipath/config.h
   multipath-tools/trunk/libmultipath/debug.c
   multipath-tools/trunk/libmultipath/debug.h
   multipath-tools/trunk/libmultipath/defaults.c
   multipath-tools/trunk/libmultipath/dict.c
   multipath-tools/trunk/libmultipath/dict.h
   multipath-tools/trunk/libmultipath/discovery.c
   multipath-tools/trunk/libmultipath/discovery.h
   multipath-tools/trunk/libmultipath/dmparser.c
   multipath-tools/trunk/libmultipath/dmparser.h
   multipath-tools/trunk/libmultipath/pgpolicies.c
   multipath-tools/trunk/libmultipath/pgpolicies.h
   multipath-tools/trunk/libmultipath/propsel.c
   multipath-tools/trunk/libmultipath/propsel.h
   multipath-tools/trunk/libmultipath/regex.c
   multipath-tools/trunk/libmultipath/regex.h
   multipath-tools/trunk/libmultipath/sg_include.h
   multipath-tools/trunk/libmultipath/structs.c
   multipath-tools/trunk/multipathd/log.c
   multipath-tools/trunk/multipathd/log.h
   multipath-tools/trunk/multipathd/log_pthread.c
   multipath-tools/trunk/multipathd/log_pthread.h
   multipath-tools/trunk/multipathd/multipathd.init.debian
   multipath-tools/trunk/multipathd/multipathd.init.redhat
   multipath-tools/trunk/multipathd/pidfile.c
   multipath-tools/trunk/multipathd/pidfile.h
   multipath-tools/trunk/path_priority/pp_alua/
   multipath-tools/trunk/path_priority/pp_alua/LICENSE
   multipath-tools/trunk/path_priority/pp_alua/Makefile
   multipath-tools/trunk/path_priority/pp_alua/main.c
   multipath-tools/trunk/path_priority/pp_alua/rtpg.c
   multipath-tools/trunk/path_priority/pp_alua/rtpg.h
   multipath-tools/trunk/path_priority/pp_alua/spc3.h
   multipath-tools/trunk/path_priority/pp_emc/
   multipath-tools/trunk/path_priority/pp_emc/Makefile
   multipath-tools/trunk/path_priority/pp_emc/pp_emc.c
Removed:
   multipath-tools/trunk/dmadm/
   multipath-tools/trunk/klibc
   multipath-tools/trunk/klibc-0.190/
   multipath-tools/trunk/kpartx/loop.h
   multipath-tools/trunk/libcheckers/checkers.c
   multipath-tools/trunk/libcheckers/sg_include.h
   multipath-tools/trunk/libdevmapper/
   multipath-tools/trunk/libmultipath/safe_printf.h
   multipath-tools/trunk/libmultipath/sysfs_devinfo.c
   multipath-tools/trunk/libmultipath/sysfs_devinfo.h
   multipath-tools/trunk/libsysfs/
   multipath-tools/trunk/multipath/.main.c.swo
   multipath-tools/trunk/multipath/.main.c.swp
   multipath-tools/trunk/multipath/config.h
   multipath-tools/trunk/multipath/debug.h
   multipath-tools/trunk/multipath/devinfo.c
   multipath-tools/trunk/multipath/devinfo.h
   multipath-tools/trunk/multipath/dict.c
   multipath-tools/trunk/multipath/dict.h
   multipath-tools/trunk/multipath/dmparser.c
   multipath-tools/trunk/multipath/dmparser.h
   multipath-tools/trunk/multipath/pgpolicies.c
   multipath-tools/trunk/multipath/pgpolicies.h
   multipath-tools/trunk/multipath/propsel.c
   multipath-tools/trunk/multipath/propsel.h
   multipath-tools/trunk/multipath/sg_include.h
   multipath-tools/trunk/multipathd/devinfo.c
   multipath-tools/trunk/multipathd/devinfo.h
   multipath-tools/trunk/multipathd/dict.c
   multipath-tools/trunk/multipathd/dict.h
   multipath-tools/trunk/multipathd/multipathd.init
Modified:
   multipath-tools/trunk/   (props changed)
   multipath-tools/trunk/ChangeLog
   multipath-tools/trunk/Makefile
   multipath-tools/trunk/Makefile.inc
   multipath-tools/trunk/devmap_name/Makefile
   multipath-tools/trunk/devmap_name/devmap_name.c
   multipath-tools/trunk/kpartx/Makefile
   multipath-tools/trunk/kpartx/gpt.c
   multipath-tools/trunk/kpartx/kpartx.c
   multipath-tools/trunk/kpartx/lopart.c
   multipath-tools/trunk/libcheckers/Makefile
   multipath-tools/trunk/libcheckers/checkers.h
   multipath-tools/trunk/libcheckers/emc_clariion.c
   multipath-tools/trunk/libcheckers/path_state.h
   multipath-tools/trunk/libcheckers/readsector0.c
   multipath-tools/trunk/libcheckers/selector.c
   multipath-tools/trunk/libcheckers/tur.c
   multipath-tools/trunk/libmultipath/Makefile
   multipath-tools/trunk/libmultipath/blacklist.c
   multipath-tools/trunk/libmultipath/blacklist.h
   multipath-tools/trunk/libmultipath/callout.c
   multipath-tools/trunk/libmultipath/defaults.h
   multipath-tools/trunk/libmultipath/devmapper.c
   multipath-tools/trunk/libmultipath/devmapper.h
   multipath-tools/trunk/libmultipath/hwtable.c
   multipath-tools/trunk/libmultipath/hwtable.h
   multipath-tools/trunk/libmultipath/memory.c
   multipath-tools/trunk/libmultipath/memory.h
   multipath-tools/trunk/libmultipath/parser.c
   multipath-tools/trunk/libmultipath/parser.h
   multipath-tools/trunk/libmultipath/structs.h
   multipath-tools/trunk/libmultipath/util.c
   multipath-tools/trunk/libmultipath/util.h
   multipath-tools/trunk/libmultipath/vector.c
   multipath-tools/trunk/libmultipath/vector.h
   multipath-tools/trunk/multipath.conf.annotated
   multipath-tools/trunk/multipath.conf.synthetic
   multipath-tools/trunk/multipath/Makefile
   multipath-tools/trunk/multipath/main.c
   multipath-tools/trunk/multipath/main.h
   multipath-tools/trunk/multipath/multipath.dev
   multipath-tools/trunk/multipathd/Makefile
   multipath-tools/trunk/multipathd/copy.c
   multipath-tools/trunk/multipathd/main.c
   multipath-tools/trunk/multipathd/main.h
   multipath-tools/trunk/path_priority/Makefile
Log:
Merge /multipath-tools/upstream/current (0.4.4).


Modified: multipath-tools/trunk/ChangeLog
==============================================================================
--- multipath-tools/trunk/ChangeLog	(original)
+++ multipath-tools/trunk/ChangeLog	Sat Aug 13 15:23:02 2005
@@ -1,3 +1,194 @@
+2005-03-19 multipath-tools-0.4.4
+
+	* [path_prio] clarify pp_alua licensing. Stefan Bader, IBM.
+	* [devmap_name] add a target_type filter (suggested by Hannes)
+	  and set DM task device by major:minor rather than parsing
+	  the full map list.
+	* [libmultipath] propagate an error on getprio callout
+	  failures, so that multipath can mark the map as immutable.
+	  Reported by Lars Marowsky-Brée, Suse.
+	* [libmultipath] move push_callout() from dict.c to config.c
+	  Use it in store_hwe() to get in multipathd's ramfs the
+	  callout defined in hwtable.c when no config file is used.
+	  Reported by Lars Marowsky-Brée, Suse.
+	* [checkers] zero sense buffers before use in EMC checker.
+	  Lars Marowsky-Brée, Suse.
+	* [all] pre-release bugfixing effort from Alasdair, Hannes, 
+	  Lars, Benjamin Marzinski
+	* [multipathd] set oom_adj to -17 when kernel permits.
+	  Immune to OOM killer ? agk says : watch out for mem
+	  leaks :/
+	* [multipathd] safety nets from udevd : exit early if
+	  not root, chdir to / to avoid pining a mount.
+	* [multipathd] multipathd could loose events coming from
+	  sighup or DM waitev. Add a pending_event counter to
+	  track that.
+	* [path_prio] add pp_emc from Lars M Bree, Suse.
+	* [path_prio] add pp_alua from Stefan Bader, IBM.
+	* [libmultipath] add config.c:load_config(), which sucks
+	  a big chunk of code out of multipath/main.c.
+	* [libmultipath] don't allocate memory in :
+		* devmapper.c:dm_get_map()
+		* devmapper.c:dm_get_status()
+	* [libmultipath] devinfo() a la carte fetching
+	* [libmultipath] merge keepalived memory audit framework
+	  (Thanks again, M. Cassen). Already found and fixed a
+	  couple of leaks.
+	* [libmultipath] flatten/optimize dm_map_present() and
+	  dm_mapname(). Inspired by Alasdair Kergon, RedHat.
+	* [kpartx] dm_map_name() free before use bugfix. Kiyoshi
+	  Ueda, NEC
+	* [kpartx] add hotplug mode. To enable name the binary
+	  "kpartx.dev". Kiyoshi Ueda, NEC
+	* [multipathd] don't loose events in event waiter thread.
+	  Suggested and prototyped by Edward Goggin, EMC
+	* [libmultipath] add return values to vector manipulation
+	  fonctions. Mem alloc return code audit.
+	* [libmultipath] Use "config->udev_dir + path->dev" as
+	  a path devnode to open instead of mknod'ing for each
+	  one. Fix some DoS issues regarding usage of /tmp in
+	  libmultipath/discovery.c:opennode(). Kill unlinknode()
+	* [multipathd] merged the redhat init script and stop
+	  installing one on "make install"
+	* [libmultipath] fold safe_sprintf.h into util.h
+	* [libmultipath] move blacklist to a real regex matcher
+	  Example config files updated : check yours !!
+	* [multipath] fix path group compare logic to not stop
+	  comparing at first path in each PG.
+	* [multipathd] check if pidfile is a dead remnent of a
+	  crashed daemon. If so, overwrite it. Suggested by
+	  Alasdair Kergon, RedHat. Code heavily based on work
+	  by Andrew Tridgell, Samba Fame.
+	* [build] dropped libdevmapper/ and libsysfs/ from the
+	  package. klibc build is now broken until distributors
+	  provide klibc compiled static libraries in their
+	  respective packages.
+	* [libmultipath] dm_task_no_open_count() before each DM
+	  ioctl. Not doing that is reported to cause deadlocks
+	  in kernel-space. Reported by Edward Goggin, EMC, fix
+	  suggested by Alasdair Kergon, RedHat
+	  Note minimal libdevmapper version bumped up to 1.01.
+	* [multipath] switched to condlog(). "make DEBUG=N" is
+	  deprecated. Debug is spat with "-v3" or more.
+	* [multipathd] "multipathd -vN" cmdline arg to control
+	  daemon verbosity. 0 < N < 4. "make LOGLEVEL=N" is
+	  deprecated.
+	* [libmultipath] provide a common condlog() primitive to
+	  switch lib messages to syslog or stdout depending on
+	  who uses the lib (daemon or tool).
+	* [kpartx] give kpartx a private, slim copy of devmap.[ch]
+	* [multipath] allow wwid in blacklist{} config section.
+	  Kiyoshi Ueda, NEC.
+	* [multipathd] set mode value before use (S_IRWXU). Fixes
+	  RedHat Bugzilla Bug 150665.
+	* [all] add ->fd to "struct path *". remove fd from all
+	  checker context declaration. remove lots of duplicate
+	  logic. Now a fd is opened only once for path. It should
+	  also bring a bit safety in contended memory scenarii
+	* [libcheckers] remove redundant sg_include.h
+	* [libmultipath] merge multipath:dict.[ch] with
+	  multipathd:dict.[ch] into libmultipath/. move config.h
+	  there too, add some helper functions to alloc/free
+	  "struct config *" in a new config.c. Start using a
+	  config in the daemon.
+	* [libmultipath] move dm_geteventnr(), dm_get_maps() and
+	  dm_switchgroup() in devmapper.[ch]
+	* [libmultipath] move path discovery logic in
+	  libmultipath. merge devinfo.[ch] and sysfs_devinfo.[ch]
+	  into discovery.[ch]
+	* [libmultipath] move config.h in libmultipath. Move
+	  find_[mp|hw]e in a new config.c. Move "struct hwtable"
+	  declaration in config.h. Move propsel.[ch] in the
+	  lib too.
+	* [multipathd] use libmultipath:dm_type() instead of
+	  duplacate and bogus devmap discovery code.
+	* [multipathd] asynchronous & non-blocking logger
+	  thread. Implementation split into a generic log
+	  lib and a pthread wrapper for locking and events.
+	  An ipc wrapper could be easily created by
+	  interested parties.
+	* [multipath] add "multipath -l -v2 [devname|devt]"
+	  support in complement to [mapname|wwid]
+	* [kpartx] suppress loop.h private copy. Should fix
+	  the reported build problems
+	* [multipath] do sysfs_get_mnt_path() only one time
+	  and store in a global var.
+	* [multipath] further path discovery optimzation
+	* [multipath] purge superfluous includes in main.c
+	* [libmultipath] introduce a cache file. expiry set
+	  to 5 secondes to covert the hotplug event storms.
+	* [multipath] split get_pathvec_sysfs(). Introduce
+	  get_refwwid() and filter_pathvec()
+
+2005-03-19 multipath-tools-0.4.3
+
+	* [libmultipath] rename find_[mp|hw] to find_[mp|hw]e and
+	  introduce a real find_mp().
+	* [priority] provison for recursive compilation of prio
+	  subdirs, in preparation of merging more signicant
+	  prioritizers. Stephan Bader, IBM
+	* [libmultipath] add a netapp controler to the hwtable
+	* [libmultipath] blacklist() not to discard sda[0-9]*
+	  when sda is blacklisted
+	* [multipath] add a rr_min_io keyword in config file.
+	  Suggested by Igor Feoktistov, NetApp
+	* [multipath] stop trying to avoid running in parallel
+	* [multipath] bump up params size to 1024
+	* [multipathd] put prio callouts in to ramfs. Stephan
+	  Bader, IBM
+	* [multipath] simplify multibus pgpolicy : no need to
+	  copy mp->paths into mp->pg->paths then free source :
+	  just copy the ptr and set source to NULL.
+	* [multipath] sort PG by descending prio value in
+	  group_by_prio. Stephan Bader, IBM
+	* [multipath] fix a bug in group_by_prio that lead to
+	  creation of multiple PG for a single prio value
+	* [multipath] don't store multipaths in a vector anymore :
+	  free the "struct multipath" after usage.
+	* [multipath] multiple optimizations in the exec plan
+	* [multipath] allow "multipath -l -v2 [mapname|wwid]"
+	* [build] rip off klibc and move to klcc, at last.
+	  Good job hpa. multipath-tools now depend on klibc
+	  > 1.0 to build with BUILD=klibc flag.
+	* [multipath] never reload a map if no path is up in the
+	  computed new map
+	* [multipath] don't flush maps with open count != 0
+	* [libmultipath] add "int *dm_get_opencount(char *map)"
+	  to devmapper.c
+	* [multipath] plug leaks and optimize devinfo.c. From
+	  Edward Goggin, EMC
+	* [multipath] fix the multipath.dev hotplug script to not
+	  do kpartx stuff in the same run as multipath stuff.
+	  Igor Feoktistov, NetApp, noted the devmap symlink was
+	  not yet present for kpartx to use.
+	* [devmap_name] accept major:minor synthax
+	* [libmultipath] add "char *dm_mapname(int maj, int min)",
+	  needed to fail paths from checker thread
+	* [libmultipath] move dm_reinstate() in the lib, and add
+	  dm_fail_path()
+	* [multipathd] mark failed path as failed upon DM
+	  event. This should fix the design bug noticed by
+	  Ramesh Caushik, Intel, where the daemon didn't run
+	  multipath when a path went down and up in between 2
+	  checks.
+	* [libmultipath] allow NULL as a pathvec in disassemble_map
+	  as is passed only for memory optimization
+	* [libmultipath] add structs.c to store alloc_*() and
+	  free_*()
+	* [libmultipath] move dmparser.[ch] to the lib.
+	  remove devinfo.[ch] dependency.
+	* [build] fix compilation glitch with BUILD=klibc,
+	  flags to force use of local libs, remove the link
+	  dependency in klibc, try to guess kernel sources
+	  and build dirs. Stefan Bader, IBM
+	* [libmultipath] find_hw matching logic to take str
+	  lengths in account. Noticed by Ramesh Caushik, Intel
+	* [multipath] select_action matching logic to take str 
+	  length in account.
+	* [multipath] lookup mp alias name earlier (in coalesce)
+	  Edward Goggin, EMC, noticed we tried to use it before
+	  it was set up.
+
 2005-01-23 multipath-tools-0.4.2
 
 	* [libmultipath] add symmetrix controler family to the

Modified: multipath-tools/trunk/Makefile
==============================================================================
--- multipath-tools/trunk/Makefile	(original)
+++ multipath-tools/trunk/Makefile	Sat Aug 13 15:23:02 2005
@@ -4,16 +4,25 @@
 
 BUILD = glibc
 
-KERNEL_BUILD = /lib/modules/$(shell uname -r)/build
-
-ifeq ($(strip $(BUILD)),klibc)
-	BUILDDIRS = libsysfs libdevmapper libcheckers \
-		    libmultipath path_priority \
-		    devmap_name multipath multipathd kpartx
+#
+# Try to supply the linux kernel headers.
+#
+ifeq    ($(KRNLSRC),)
+KRNLLIB = /lib/modules/$(shell uname -r)
+ifeq    ($(shell test -r $(KRNLLIB)/source && echo 1),1)
+KRNLSRC = $(KRNLLIB)/source
+KRNLOBJ = $(KRNLLIB)/build
 else
-	BUILDDIRS = libmultipath libcheckers path_priority \
-		    devmap_name multipath multipathd kpartx
+KRNLSRC = $(KRNLLIB)/build
+KRNLOBJ = $(KRNLLIB)/build
+endif
 endif
+export KRNLSRC
+export KRNLOBJ
+
+BUILDDIRS = libmultipath libcheckers path_priority \
+	    devmap_name multipath multipathd kpartx
+ALLDIRS	= $(shell find . -type d -maxdepth 1 -mindepth 1)
 
 VERSION = $(shell basename ${PWD} | cut -d'-' -f3)
 INSTALLDIRS = devmap_name multipath multipathd kpartx path_priority
@@ -21,26 +30,16 @@
 all: recurse
 
 recurse:
-	@if [ "$(BUILD)" = "klibc" ]; then\
-	ln -s ${KERNEL_BUILD} klibc/linux ; \
-	$(MAKE) -C klibc || exit $?; \
-	fi
-	@for dir in $(BUILDDIRS); do\
-	$(MAKE) -C $$dir BUILD=$(BUILD) VERSION=$(VERSION) || \
-	exit $?; \
+	@for dir in $(BUILDDIRS); do \
+	$(MAKE) -C $$dir BUILD=$(BUILD) VERSION=$(VERSION) \
+		KRNLSRC=$(KRNLSRC) KRNLOBJ=$(KRNLOBJ) || exit $?; \
 	done
 
 recurse_clean:
-	@for dir in $(BUILDDIRS); do\
+	@for dir in $(ALLDIRS); do\
 	$(MAKE) -C $$dir clean || exit $?; \
 	done
 
-recurse_clean_klibc:
-	@for dir in $(BUILDDIRS); do\
-	$(MAKE) -C $$dir BUILD=klibc clean || exit $?; \
-	done
-#	$(MAKE) -C klibc spotless
-
 recurse_install:
 	@for dir in $(INSTALLDIRS); do\
 	$(MAKE) -C $$dir install || exit $?; \
@@ -51,9 +50,8 @@
 	$(MAKE) -C $$dir uninstall || exit $?; \
 	done
 
-clean:	recurse_clean recurse_clean_klibc
+clean:	recurse_clean
 	rm -f multipath-tools.spec
-	rm -f klibc/linux
 	rm -rf rpms
 
 install:	recurse_install

Modified: multipath-tools/trunk/Makefile.inc
==============================================================================
--- multipath-tools/trunk/Makefile.inc	(original)
+++ multipath-tools/trunk/Makefile.inc	Sat Aug 13 15:23:02 2005
@@ -2,27 +2,33 @@
 #
 # Copyright (C) 2004 Christophe Varoqui, <christophe.varoqui at free.fr>
 
+#
+# Allow to force some libraries to be used statically. (Uncomment one of the
+# following lines or define the values when calling make.)
+#
+# WITH_LOCAL_LIBDM	= 1
+# WITH_LOCAL_LIBSYSFS	= 1
+
+ifeq ($(TOPDIR),)
+	TOPDIR	= ..
+endif
+
 ifeq ($(strip $(BUILD)),klibc)
-	SRCROOT=../klibc
-	include ${SRCROOT}/MCONFIG
+	CC = klcc
+	klibcdir = /usr/lib/klibc
+	libdm    = $(klibcdir)/lib/libdevmapper.a
+	libsysfs = $(klibcdir)/lib/libsysfs.a
 endif
 
 prefix      = 
 exec_prefix = $(prefix)
 bindir      = $(exec_prefix)/sbin
-klibcdir    = $(SRCROOT)
-libdmdir    = ../libdevmapper
-sysfsdir    = ../libsysfs
-checkersdir = ../libcheckers
-multipathdir = ../libmultipath
+checkersdir = $(TOPDIR)/libcheckers
+multipathdir = $(TOPDIR)/libmultipath
 mandir      = /usr/share/man/man8
 
 GZIP        = /bin/gzip -9 -c
 STRIP       = strip --strip-all -R .comment -R .note
 
-DMLIB = $(libdmdir)/libdevmapper
-SYSFSLIB = $(sysfsdir)/libsysfs
 CHECKERSLIB = $(checkersdir)/libcheckers
 MULTIPATHLIB = $(multipathdir)/libmultipath
-
-KERNEL_BUILD = /lib/modules/$(shell uname -r)/build

Modified: multipath-tools/trunk/devmap_name/Makefile
==============================================================================
--- multipath-tools/trunk/devmap_name/Makefile	(original)
+++ multipath-tools/trunk/devmap_name/Makefile	Sat Aug 13 15:23:02 2005
@@ -5,14 +5,13 @@
 
 include ../Makefile.inc
 
+OBJS = devmap_name.o
+CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes
+
 ifeq ($(strip $(BUILD)),klibc)
-	CFLAGS = ${REQFLAGS} ${OPTFLAGS} -I$(libdmdir)
-	OBJS = devmap_name.o $(DMLIB)-$(BUILD).a
+	OBJS += $(libdm)
 else
-	CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes \
-		 -DDEBUG=$(DEBUG)
 	LDFLAGS = -ldevmapper
-	OBJS = devmap_name.o
 endif
 
 EXEC = devmap_name
@@ -27,19 +26,11 @@
 	$(STRIP) $(EXEC)
 	$(GZIP) $(EXEC).8 > $(EXEC).8.gz
 	
-klibc: prepare $(KLIBC) $(OBJS)
-	$(LD) -o $(EXEC) $(CRT0) $(OBJS) $(KLIBC) $(LIBGCC)
+klibc: prepare $(OBJS)
+	$(CC) -static -o $(EXEC) $(OBJS)
 	$(STRIP) $(EXEC)
 	$(GZIP) $(EXEC).8 > $(EXEC).8.gz
 
-$(DMLIB)-$(BUILD).a:
-	make -C $(libdmdir) BUILD=$(BUILD) $(BUILD)
-
-$(KLIBC):
-	rm -f $(klibcdir)/linux
-	ln -s $(KERNEL_BUILD) $(klibcdir)/linux
-	make -C $(klibcdir)
-
 install:
 	install -d $(DESTDIR)$(bindir)
 	install -m 755 $(EXEC) $(DESTDIR)$(bindir)/

Modified: multipath-tools/trunk/devmap_name/devmap_name.c
==============================================================================
--- multipath-tools/trunk/devmap_name/devmap_name.c	(original)
+++ multipath-tools/trunk/devmap_name/devmap_name.c	Sat Aug 13 15:23:02 2005
@@ -1,58 +1,85 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <ctype.h>
+#include <unistd.h>
 #include <linux/kdev_t.h>
 #include <libdevmapper.h>
-                                                                                
+
 static void usage(char * progname) {
-	fprintf(stderr, "usage : %s major minor\n", progname);
+	fprintf(stderr, "usage : %s [-t target type] dev_t\n", progname);
+	fprintf(stderr, "where dev_t is either 'major minor' or 'major:minor'\n");
 	exit(1);
 }
 
+int dm_target_type(int major, int minor, char *type)
+{
+	struct dm_task *dmt;
+	void *next = NULL;
+	uint64_t start, length;
+	char *target_type = NULL;
+	char *params;
+	int r = 1;
+
+	if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
+		return 1;
+
+	if (!dm_task_set_major(dmt, major) ||
+	    !dm_task_set_minor(dmt, minor))
+		goto bad;
+
+	dm_task_no_open_count(dmt);
+
+	if (!dm_task_run(dmt))
+		goto bad;
+
+	if (!type)
+		goto good;
+
+	do {
+		next = dm_get_next_target(dmt, next, &start, &length,
+					  &target_type, &params);
+		if (target_type && strcmp(target_type, type))
+			goto bad;
+	} while (next);
+
+good:
+	printf("%s\n", dm_task_get_name(dmt));
+	r = 0;
+bad:
+	dm_task_destroy(dmt);
+	return r;
+}
+
 int main(int argc, char **argv)
 {
-        int r = 0;
-        struct dm_names *names;
-        unsigned next = 0;
+	int c;
 	int major, minor;
-        struct dm_task *dmt;
+	char *target_type = NULL;
+
+	while ((c = getopt(argc, argv, "t:")) != -1) {
+		switch (c) {
+		case 't':
+			target_type = optarg;
+			break;
+		default:
+			usage(argv[0]);
+			return 1;
+			break;
+		}
+	}
 
 	/* sanity check */
-	if (argc != 3)
+	if (optind == argc - 2) {
+		major = atoi(argv[argc - 2]);
+		minor = atoi(argv[argc - 1]);
+	} else if (optind != argc - 1 ||
+		   2 != sscanf(argv[argc - 1], "%i:%i", &major, &minor))
 		usage(argv[0]);
 
-	major = atoi(argv[1]);
-	minor = atoi(argv[2]);
-
-        if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
-                return 0;
-                                                                                
-        if (!dm_task_run(dmt))
-                goto out;
-                                                                                
-        if (!(names = dm_task_get_names(dmt)))
-                goto out;
-                                                                                
-        if (!names->dev) {
-                printf("No devices found\n");
-                goto out;
-        }
+	if (dm_target_type(major, minor, target_type))
+		return 1;
                                                                                 
-        do {
-                names = (void *) names + next;
-		if ((int) MAJOR(names->dev) == major &&
-		    (int) MINOR(names->dev) == minor) {
-	                printf("%s", names->name);
-			goto out;
-		}
-                next = names->next;
-        } while (next);
-                                                                                
-      /* No correspondance found */
-      r = 1;
-
-      out:
-        dm_task_destroy(dmt);
-        return r;
+	return 0;
 }
 

Modified: multipath-tools/trunk/kpartx/Makefile
==============================================================================
--- multipath-tools/trunk/kpartx/Makefile	(original)
+++ multipath-tools/trunk/kpartx/Makefile	Sat Aug 13 15:23:02 2005
@@ -6,17 +6,16 @@
 
 include ../Makefile.inc
 
+CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes -I.
+
 ifeq ($(strip $(BUILD)),klibc)
-	CFLAGS = ${REQFLAGS} ${OPTFLAGS} -I$(libdmdir) -I. -I$(multipathdir)
 	OBJS = bsd.o dos.o kpartx.o solaris.o unixware.o gpt.o crc32.o \
-	       lopart.o xstrncpy.o \
-	       $(MULTIPATHLIB)-$(BUILD).a $(DMLIB)-$(BUILD).a
+	       lopart.o xstrncpy.o devmapper.o \
+	       $(MULTIPATHLIB)-$(BUILD).a $(libdm)
 else
-	CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes -I. \
-		 -I$(multipathdir)
 	LDFLAGS = -ldevmapper
 	OBJS = bsd.o dos.o kpartx.o solaris.o unixware.o \
-	       gpt.o crc32.o lopart.o xstrncpy.o $(MULTIPATHLIB)-$(BUILD).a
+	       gpt.o crc32.o lopart.o xstrncpy.o devmapper.o
 endif
 
 EXEC = kpartx
@@ -31,22 +30,14 @@
 	$(STRIP) $(EXEC)
 	$(GZIP) $(EXEC).8 > $(EXEC).8.gz
 	
-klibc: prepare $(KLIBC) $(OBJS)
-	$(LD) -o $(EXEC) $(CRT0) $(OBJS) $(KLIBC) $(LIBGCC)
+klibc: prepare $(OBJS)
+	$(CC) -static -o $(EXEC) $(CRT0) $(OBJS) $(KLIBC) $(LIBGCC)
 	$(STRIP) $(EXEC)
 	$(GZIP) $(EXEC).8 > $(EXEC).8.gz
 
-$(DMLIB)-$(BUILD).a:
-	make -C $(libdmdir) BUILD=$(BUILD)
-
 $(MULTIPATHLIB)-$(BUILD).a:
 	make -C $(multipathdir) BUILD=$(BUILD)
 
-$(KLIBC):
-	rm -f $(klibcdir)/linux
-	ln -s $(KERNEL_BUILD) $(klibcdir)/linux
-	make -C $(klibcdir)
-
 install:
 	install -d $(DESTDIR)$(bindir)
 	install -m 755 $(EXEC) $(DESTDIR)$(bindir)

Added: multipath-tools/trunk/kpartx/devmapper.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/kpartx/devmapper.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,140 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libdevmapper.h>
+#include <ctype.h>
+#include <linux/kdev_t.h>
+
+extern int
+dm_prereq (char * str, int x, int y, int z)
+{
+	int r = 1;
+	struct dm_task *dmt;
+	struct dm_versions *target;
+	struct dm_versions *last_target;
+
+	if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
+		return 1;
+
+	dm_task_no_open_count(dmt);
+
+	if (!dm_task_run(dmt))
+		goto out;
+
+	target = dm_task_get_versions(dmt);
+
+	/* Fetch targets and print 'em */
+	do {
+		last_target = target;
+
+		if (!strncmp(str, target->name, strlen(str)) &&
+		    /* dummy prereq on multipath version */
+		    target->version[0] >= x &&
+		    target->version[1] >= y &&
+		    target->version[2] >= z
+		   )
+			r = 0;
+
+		target = (void *) target + target->next;
+	} while (last_target != target);
+
+	out:
+	dm_task_destroy(dmt);
+	return r;
+}
+
+extern int
+dm_simplecmd (int task, const char *name) {
+	int r = 0;
+	struct dm_task *dmt;
+
+	if (!(dmt = dm_task_create(task)))
+		return 0;
+
+	if (!dm_task_set_name(dmt, name))
+		goto out;
+
+	dm_task_no_open_count(dmt);
+
+	r = dm_task_run(dmt);
+
+	out:
+	dm_task_destroy(dmt);
+	return r;
+}
+
+extern int
+dm_addmap (int task, const char *name, const char *target,
+	   const char *params, unsigned long size) {
+	int r = 0;
+	struct dm_task *dmt;
+
+	if (!(dmt = dm_task_create (task)))
+		return 0;
+
+	if (!dm_task_set_name (dmt, name))
+		goto addout;
+
+	if (!dm_task_add_target (dmt, 0, size, target, params))
+		goto addout;
+
+	dm_task_no_open_count(dmt);
+
+	r = dm_task_run (dmt);
+
+	addout:
+	dm_task_destroy (dmt);
+	return r;
+}
+
+extern int
+dm_map_present (char * str)
+{
+	int r = 0;
+	struct dm_task *dmt;
+	struct dm_info info;
+
+	if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
+		return 0;
+
+	if (!dm_task_set_name(dmt, str))
+		goto out;
+
+	dm_task_no_open_count(dmt);
+
+	if (!dm_task_run(dmt))
+		goto out;
+
+	if (!dm_task_get_info(dmt, &info))
+		goto out;
+
+	if (info.exists)
+		r = 1;
+out:
+	dm_task_destroy(dmt);
+	return r;
+}
+
+
+const char *
+dm_mapname(int major, int minor)
+{
+	struct dm_task *dmt;
+	const char *mapname;
+
+	if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
+		return NULL;
+
+	dm_task_no_open_count(dmt);
+	dm_task_set_major(dmt, major);
+	dm_task_set_minor(dmt, minor);
+
+	if (!dm_task_run(dmt))
+		goto out;
+
+	mapname = strdup(dm_task_get_name(dmt));
+out:
+	dm_task_destroy(dmt);
+	return mapname;
+}
+

Added: multipath-tools/trunk/kpartx/devmapper.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/kpartx/devmapper.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,5 @@
+int dm_prereq (char *, int, int, int);
+int dm_simplecmd (int, const char *);
+int dm_addmap (int, const char *, const char *, const char *, unsigned long);
+int dm_map_present (char *);
+const char * dm_mapname(int major, int minor);

Modified: multipath-tools/trunk/kpartx/gpt.c
==============================================================================
--- multipath-tools/trunk/kpartx/gpt.c	(original)
+++ multipath-tools/trunk/kpartx/gpt.c	Sat Aug 13 15:23:02 2005
@@ -34,9 +34,22 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
-#include <asm/byteorder.h>
+#include <endian.h>
+#include <byteswap.h>
 #include "crc32.h"
 
+#if BYTE_ORDER == LITTLE_ENDIAN
+#  define __le16_to_cpu(x) (x)
+#  define __le32_to_cpu(x) (x)
+#  define __le64_to_cpu(x) (x)
+#  define __cpu_to_le32(x) (x)
+#elif BYTE_ORDER == BIG_ENDIAN
+#  define __le16_to_cpu(x) bswap_16(x)
+#  define __le32_to_cpu(x) bswap_32(x)
+#  define __le64_to_cpu(x) bswap_64(x)
+#  define __cpu_to_le32(x) bswap_32(x)
+#endif
+
 #define BLKGETLASTSECT  _IO(0x12,108)   /* get last sector of block device */
 #define BLKGETSIZE _IO(0x12,96)	        /* return device size */
 #define BLKSSZGET  _IO(0x12,104)	/* get block device sector size */

Added: multipath-tools/trunk/kpartx/gpt.c.orig
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/kpartx/gpt.c.orig	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,625 @@
+/*
+    gpt.[ch]
+
+    Copyright (C) 2000-2001 Dell Computer Corporation <Matt_Domsch at dell.com> 
+
+    EFI GUID Partition Table handling
+    Per Intel EFI Specification v1.02
+    http://developer.intel.com/technology/efi/efi.htm
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#define _FILE_OFFSET_BITS 64
+
+#include "gpt.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <asm/byteorder.h>
+#include "crc32.h"
+
+#define BLKGETLASTSECT  _IO(0x12,108)   /* get last sector of block device */
+#define BLKGETSIZE _IO(0x12,96)	        /* return device size */
+#define BLKSSZGET  _IO(0x12,104)	/* get block device sector size */
+#define BLKGETSIZE64 _IOR(0x12,114,sizeof(uint64_t))	/* return device size in bytes (u64 *arg) */
+
+struct blkdev_ioctl_param {
+        unsigned int block;
+        size_t content_length;
+        char * block_contents;
+};
+
+/**
+ * efi_crc32() - EFI version of crc32 function
+ * @buf: buffer to calculate crc32 of
+ * @len - length of buf
+ *
+ * Description: Returns EFI-style CRC32 value for @buf
+ * 
+ * This function uses the little endian Ethernet polynomial
+ * but seeds the function with ~0, and xor's with ~0 at the end.
+ * Note, the EFI Specification, v1.02, has a reference to
+ * Dr. Dobbs Journal, May 1994 (actually it's in May 1992).
+ */
+static inline uint32_t
+efi_crc32(const void *buf, unsigned long len)
+{
+	return (crc32(~0L, buf, len) ^ ~0L);
+}
+
+/**
+ * is_pmbr_valid(): test Protective MBR for validity
+ * @mbr: pointer to a legacy mbr structure
+ *
+ * Description: Returns 1 if PMBR is valid, 0 otherwise.
+ * Validity depends on two things:
+ *  1) MSDOS signature is in the last two bytes of the MBR
+ *  2) One partition of type 0xEE is found
+ */
+static int
+is_pmbr_valid(legacy_mbr *mbr)
+{
+	int i, found = 0, signature = 0;
+	if (!mbr)
+		return 0;
+	signature = (__le16_to_cpu(mbr->signature) == MSDOS_MBR_SIGNATURE);
+	for (i = 0; signature && i < 4; i++) {
+		if (mbr->partition[i].sys_type ==
+                    EFI_PMBR_OSTYPE_EFI_GPT) {
+			found = 1;
+			break;
+		}
+	}
+	return (signature && found);
+}
+
+
+/************************************************************
+ * get_sector_size
+ * Requires:
+ *  - filedes is an open file descriptor, suitable for reading
+ * Modifies: nothing
+ * Returns:
+ *  sector size, or 512.
+ ************************************************************/
+static int
+get_sector_size(int filedes)
+{
+	int rc, sector_size = 512;
+
+	rc = ioctl(filedes, BLKSSZGET, &sector_size);
+	if (rc)
+		sector_size = 512;
+	return sector_size;
+}
+
+/************************************************************
+ * _get_num_sectors
+ * Requires:
+ *  - filedes is an open file descriptor, suitable for reading
+ * Modifies: nothing
+ * Returns:
+ *  Last LBA value on success 
+ *  0 on error
+ *
+ * Try getting BLKGETSIZE64 and BLKSSZGET first,
+ * then BLKGETSIZE if necessary.
+ *  Kernels 2.4.15-2.4.18 and 2.5.0-2.5.3 have a broken BLKGETSIZE64
+ *  which returns the number of 512-byte sectors, not the size of
+ *  the disk in bytes. Fixed in kernels 2.4.18-pre8 and 2.5.4-pre3.
+ ************************************************************/
+static uint64_t
+_get_num_sectors(int filedes)
+{
+	unsigned long sectors=0;
+	int rc;
+#if 0
+        uint64_t bytes=0;
+
+ 	rc = ioctl(filedes, BLKGETSIZE64, &bytes);
+	if (!rc)
+		return bytes / get_sector_size(filedes);
+#endif
+        rc = ioctl(filedes, BLKGETSIZE, &sectors);
+        if (rc)
+                return 0;
+        
+	return sectors;
+}
+
+/************************************************************
+ * last_lba(): return number of last logical block of device
+ * 
+ * @fd
+ * 
+ * Description: returns Last LBA value on success, 0 on error.
+ * Notes: The value st_blocks gives the size of the file
+ *        in 512-byte blocks, which is OK if
+ *        EFI_BLOCK_SIZE_SHIFT == 9.
+ ************************************************************/
+
+static uint64_t
+last_lba(int filedes)
+{
+	int rc;
+	uint64_t sectors = 0;
+	struct stat s;
+	memset(&s, 0, sizeof (s));
+	rc = fstat(filedes, &s);
+	if (rc == -1) {
+		fprintf(stderr, "last_lba() could not stat: %s\n",
+			strerror(errno));
+		return 0;
+	}
+
+	if (S_ISBLK(s.st_mode)) {
+		sectors = _get_num_sectors(filedes);
+	} else {
+		fprintf(stderr,
+			"last_lba(): I don't know how to handle files with mode %x\n",
+			s.st_mode);
+		sectors = 1;
+	}
+
+	return sectors - 1;
+}
+
+
+static ssize_t
+read_lastoddsector(int fd, uint64_t lba, void *buffer, size_t count)
+{
+        int rc;
+        struct blkdev_ioctl_param ioctl_param;
+
+        if (!buffer) return 0; 
+
+        ioctl_param.block = 0; /* read the last sector */
+        ioctl_param.content_length = count;
+        ioctl_param.block_contents = buffer;
+
+        rc = ioctl(fd, BLKGETLASTSECT, &ioctl_param);
+        if (rc == -1) perror("read failed");
+
+        return !rc;
+}
+
+static ssize_t
+read_lba(int fd, uint64_t lba, void *buffer, size_t bytes)
+{
+	int sector_size = get_sector_size(fd);
+	off_t offset = lba * sector_size;
+        ssize_t bytesread;
+
+	lseek(fd, offset, SEEK_SET);
+	bytesread = read(fd, buffer, bytes);
+
+        /* Kludge.  This is necessary to read/write the last
+           block of an odd-sized disk, until Linux 2.5.x kernel fixes.
+           This is only used by gpt.c, and only to read
+           one sector, so we don't have to be fancy.
+        */
+        if (!bytesread && !(last_lba(fd) & 1) && lba == last_lba(fd)) {
+                bytesread = read_lastoddsector(fd, lba, buffer, bytes);
+        }
+        return bytesread;
+}
+
+/**
+ * alloc_read_gpt_entries(): reads partition entries from disk
+ * @fd  is an open file descriptor to the whole disk
+ * @gpt is a buffer into which the GPT will be put  
+ * Description: Returns ptes on success,  NULL on error.
+ * Allocates space for PTEs based on information found in @gpt.
+ * Notes: remember to free pte when you're done!
+ */
+static gpt_entry *
+alloc_read_gpt_entries(int fd, gpt_header * gpt)
+{
+	gpt_entry *pte;
+        size_t count = __le32_to_cpu(gpt->num_partition_entries) *
+                __le32_to_cpu(gpt->sizeof_partition_entry);
+
+        if (!count) return NULL;
+
+	pte = (gpt_entry *)malloc(count);
+	if (!pte)
+		return NULL;
+	memset(pte, 0, count);
+
+	if (!read_lba(fd, __le64_to_cpu(gpt->partition_entry_lba), pte,
+                      count)) {
+		free(pte);
+		return NULL;
+	}
+	return pte;
+}
+
+/**
+ * alloc_read_gpt_header(): Allocates GPT header, reads into it from disk
+ * @fd  is an open file descriptor to the whole disk
+ * @lba is the Logical Block Address of the partition table
+ * 
+ * Description: returns GPT header on success, NULL on error.   Allocates
+ * and fills a GPT header starting at @ from @bdev.
+ * Note: remember to free gpt when finished with it.
+ */
+static gpt_header *
+alloc_read_gpt_header(int fd, uint64_t lba)
+{
+	gpt_header *gpt;
+	gpt = (gpt_header *)
+	    malloc(sizeof (gpt_header));
+	if (!gpt)
+		return NULL;
+	memset(gpt, 0, sizeof (*gpt));
+	if (!read_lba(fd, lba, gpt, sizeof (gpt_header))) {
+		free(gpt);
+		return NULL;
+	}
+
+	return gpt;
+}
+
+/**
+ * is_gpt_valid() - tests one GPT header and PTEs for validity
+ * @fd  is an open file descriptor to the whole disk
+ * @lba is the logical block address of the GPT header to test
+ * @gpt is a GPT header ptr, filled on return.
+ * @ptes is a PTEs ptr, filled on return.
+ *
+ * Description: returns 1 if valid,  0 on error.
+ * If valid, returns pointers to newly allocated GPT header and PTEs.
+ */
+static int
+is_gpt_valid(int fd, uint64_t lba,
+             gpt_header ** gpt, gpt_entry ** ptes)
+{
+	int rc = 0;		/* default to not valid */
+	uint32_t crc, origcrc;
+
+	if (!gpt || !ptes)
+                return 0;
+	if (!(*gpt = alloc_read_gpt_header(fd, lba)))
+		return 0;
+
+	/* Check the GUID Partition Table signature */
+	if (__le64_to_cpu((*gpt)->signature) != GPT_HEADER_SIGNATURE) {
+		/* 
+		   printf("GUID Partition Table Header signature is wrong: %" PRIx64" != %" PRIx64 "\n",
+		   __le64_to_cpu((*gpt)->signature), GUID_PT_HEADER_SIGNATURE);
+		 */
+		free(*gpt);
+		*gpt = NULL;
+		return rc;
+	}
+
+	/* Check the GUID Partition Table Header CRC */
+	origcrc = __le32_to_cpu((*gpt)->header_crc32);
+	(*gpt)->header_crc32 = 0;
+	crc = efi_crc32(*gpt, __le32_to_cpu((*gpt)->header_size));
+	if (crc != origcrc) {
+		// printf( "GPTH CRC check failed, %x != %x.\n", origcrc, crc);
+		(*gpt)->header_crc32 = __cpu_to_le32(origcrc);
+		free(*gpt);
+		*gpt = NULL;
+		return 0;
+	}
+	(*gpt)->header_crc32 = __cpu_to_le32(origcrc);
+
+	/* Check that the my_lba entry points to the LBA
+	 * that contains the GPT we read */
+	if (__le64_to_cpu((*gpt)->my_lba) != lba) {
+		/*
+		printf( "my_lba % PRIx64 "x != lba %"PRIx64 "x.\n",
+		 		__le64_to_cpu((*gpt)->my_lba), lba);
+		 */
+		free(*gpt);
+		*gpt = NULL;
+		return 0;
+	}
+
+	if (!(*ptes = alloc_read_gpt_entries(fd, *gpt))) {
+		free(*gpt);
+		*gpt = NULL;
+		return 0;
+	}
+
+	/* Check the GUID Partition Entry Array CRC */
+	crc = efi_crc32(*ptes,
+                        __le32_to_cpu((*gpt)->num_partition_entries) *
+			__le32_to_cpu((*gpt)->sizeof_partition_entry));
+	if (crc != __le32_to_cpu((*gpt)->partition_entry_array_crc32)) {
+		// printf("GUID Partitition Entry Array CRC check failed.\n");
+		free(*gpt);
+		*gpt = NULL;
+		free(*ptes);
+		*ptes = NULL;
+		return 0;
+	}
+
+	/* We're done, all's well */
+	return 1;
+}
+/**
+ * compare_gpts() - Search disk for valid GPT headers and PTEs
+ * @pgpt is the primary GPT header
+ * @agpt is the alternate GPT header
+ * @lastlba is the last LBA number
+ * Description: Returns nothing.  Sanity checks pgpt and agpt fields
+ * and prints warnings on discrepancies.
+ * 
+ */
+static void
+compare_gpts(gpt_header *pgpt, gpt_header *agpt, uint64_t lastlba)
+{
+	int error_found = 0;
+	if (!pgpt || !agpt)
+		return;
+	if (__le64_to_cpu(pgpt->my_lba) != __le64_to_cpu(agpt->alternate_lba)) {
+		error_found++;
+		fprintf(stderr, 
+		       "GPT:Primary header LBA != Alt. header alternate_lba\n");
+#ifdef DEBUG
+		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
+		       __le64_to_cpu(pgpt->my_lba),
+                       __le64_to_cpu(agpt->alternate_lba));
+#endif
+	}
+	if (__le64_to_cpu(pgpt->alternate_lba) != __le64_to_cpu(agpt->my_lba)) {
+		error_found++;
+		fprintf(stderr, 
+		       "GPT:Primary header alternate_lba != Alt. header my_lba\n");
+#ifdef DEBUG
+		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
+		       __le64_to_cpu(pgpt->alternate_lba),
+                       __le64_to_cpu(agpt->my_lba));
+#endif
+	}
+	if (__le64_to_cpu(pgpt->first_usable_lba) !=
+            __le64_to_cpu(agpt->first_usable_lba)) {
+		error_found++;
+		fprintf(stderr,  "GPT:first_usable_lbas don't match.\n");
+#ifdef DEBUG
+		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
+		       __le64_to_cpu(pgpt->first_usable_lba),
+                       __le64_to_cpu(agpt->first_usable_lba));
+#endif
+	}
+	if (__le64_to_cpu(pgpt->last_usable_lba) !=
+            __le64_to_cpu(agpt->last_usable_lba)) {
+		error_found++;
+		fprintf(stderr,  "GPT:last_usable_lbas don't match.\n");
+#ifdef DEBUG
+		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
+		       __le64_to_cpu(pgpt->last_usable_lba),
+                       __le64_to_cpu(agpt->last_usable_lba));
+#endif
+	}
+	if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) {
+		error_found++;
+		fprintf(stderr,  "GPT:disk_guids don't match.\n");
+	}
+	if (__le32_to_cpu(pgpt->num_partition_entries) !=
+            __le32_to_cpu(agpt->num_partition_entries)) {
+		error_found++;
+		fprintf(stderr,  "GPT:num_partition_entries don't match: "
+		       "0x%x != 0x%x\n",
+		       __le32_to_cpu(pgpt->num_partition_entries),
+		       __le32_to_cpu(agpt->num_partition_entries));
+	}
+	if (__le32_to_cpu(pgpt->sizeof_partition_entry) !=
+            __le32_to_cpu(agpt->sizeof_partition_entry)) {
+		error_found++;
+		fprintf(stderr, 
+		       "GPT:sizeof_partition_entry values don't match: "
+		       "0x%x != 0x%x\n",
+                       __le32_to_cpu(pgpt->sizeof_partition_entry),
+		       __le32_to_cpu(agpt->sizeof_partition_entry));
+	}
+	if (__le32_to_cpu(pgpt->partition_entry_array_crc32) !=
+            __le32_to_cpu(agpt->partition_entry_array_crc32)) {
+		error_found++;
+		fprintf(stderr, 
+		       "GPT:partition_entry_array_crc32 values don't match: "
+		       "0x%x != 0x%x\n",
+                       __le32_to_cpu(pgpt->partition_entry_array_crc32),
+		       __le32_to_cpu(agpt->partition_entry_array_crc32));
+	}
+	if (__le64_to_cpu(pgpt->alternate_lba) != lastlba) {
+		error_found++;
+		fprintf(stderr, 
+		       "GPT:Primary header thinks Alt. header is not at the end of the disk.\n");
+#ifdef DEBUG
+		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
+		       __le64_to_cpu(pgpt->alternate_lba), lastlba);
+#endif
+	}
+
+	if (__le64_to_cpu(agpt->my_lba) != lastlba) {
+		error_found++;
+		fprintf(stderr, 
+		       "GPT:Alternate GPT header not at the end of the disk.\n");
+#ifdef DEBUG
+		fprintf(stderr,  "GPT:%" PRIx64 " != %" PRIx64 "\n",
+		       __le64_to_cpu(agpt->my_lba), lastlba);
+#endif
+	}
+
+	if (error_found)
+		fprintf(stderr, 
+		       "GPT: Use GNU Parted to correct GPT errors.\n");
+	return;
+}
+
+/**
+ * find_valid_gpt() - Search disk for valid GPT headers and PTEs
+ * @fd  is an open file descriptor to the whole disk
+ * @gpt is a GPT header ptr, filled on return.
+ * @ptes is a PTEs ptr, filled on return.
+ * Description: Returns 1 if valid, 0 on error.
+ * If valid, returns pointers to newly allocated GPT header and PTEs.
+ * Validity depends on finding either the Primary GPT header and PTEs valid,
+ * or the Alternate GPT header and PTEs valid, and the PMBR valid.
+ */
+static int
+find_valid_gpt(int fd, gpt_header ** gpt, gpt_entry ** ptes)
+{
+        extern int force_gpt;
+	int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
+	gpt_header *pgpt = NULL, *agpt = NULL;
+	gpt_entry *pptes = NULL, *aptes = NULL;
+	legacy_mbr *legacymbr = NULL;
+	uint64_t lastlba;
+	if (!gpt || !ptes)
+		return 0;
+
+	lastlba = last_lba(fd);
+	good_pgpt = is_gpt_valid(fd, GPT_PRIMARY_PARTITION_TABLE_LBA,
+				 &pgpt, &pptes);
+        if (good_pgpt) {
+		good_agpt = is_gpt_valid(fd,
+                                         __le64_to_cpu(pgpt->alternate_lba),
+					 &agpt, &aptes);
+                if (!good_agpt) {
+                        good_agpt = is_gpt_valid(fd, lastlba,
+                                                 &agpt, &aptes);
+                }
+        }
+        else {
+                good_agpt = is_gpt_valid(fd, lastlba,
+                                         &agpt, &aptes);
+        }
+
+        /* The obviously unsuccessful case */
+        if (!good_pgpt && !good_agpt) {
+                goto fail;
+        }
+
+	/* This will be added to the EFI Spec. per Intel after v1.02. */
+        legacymbr = malloc(sizeof (*legacymbr));
+        if (legacymbr) {
+                memset(legacymbr, 0, sizeof (*legacymbr));
+                read_lba(fd, 0, (uint8_t *) legacymbr,
+                         sizeof (*legacymbr));
+                good_pmbr = is_pmbr_valid(legacymbr);
+                free(legacymbr);
+                legacymbr=NULL;
+        }
+
+        /* Failure due to bad PMBR */
+        if ((good_pgpt || good_agpt) && !good_pmbr && !force_gpt) {
+                fprintf(stderr,
+                       "  Warning: Disk has a valid GPT signature "
+                       "but invalid PMBR.\n"
+                       "  Assuming this disk is *not* a GPT disk anymore.\n"
+                       "  Use gpt kernel option to override.  "
+                       "Use GNU Parted to correct disk.\n");
+                goto fail;
+        }
+
+        /* Would fail due to bad PMBR, but force GPT anyhow */
+        if ((good_pgpt || good_agpt) && !good_pmbr && force_gpt) {
+                fprintf(stderr, 
+                       "  Warning: Disk has a valid GPT signature but "
+                       "invalid PMBR.\n"
+                       "  Use GNU Parted to correct disk.\n"
+                       "  gpt option taken, disk treated as GPT.\n");
+        }
+
+        compare_gpts(pgpt, agpt, lastlba);
+
+        /* The good cases */
+        if (good_pgpt && (good_pmbr || force_gpt)) {
+                *gpt  = pgpt;
+                *ptes = pptes;
+                if (agpt)  { free(agpt);   agpt = NULL; }
+                if (aptes) { free(aptes); aptes = NULL; }
+                if (!good_agpt) {
+                        fprintf(stderr, 
+			       "Alternate GPT is invalid, "
+                               "using primary GPT.\n");
+                }
+                return 1;
+        }
+        else if (good_agpt && (good_pmbr || force_gpt)) {
+                *gpt  = agpt;
+                *ptes = aptes;
+                if (pgpt)  { free(pgpt);   pgpt = NULL; }
+                if (pptes) { free(pptes); pptes = NULL; }
+                fprintf(stderr, 
+                       "Primary GPT is invalid, using alternate GPT.\n");
+                return 1;
+        }
+
+ fail:
+        if (pgpt)  { free(pgpt);   pgpt=NULL; }
+        if (agpt)  { free(agpt);   agpt=NULL; }
+        if (pptes) { free(pptes); pptes=NULL; }
+        if (aptes) { free(aptes); aptes=NULL; }
+        *gpt = NULL;
+        *ptes = NULL;
+        return 0;
+}
+
+/**
+ * read_gpt_pt() 
+ * @fd
+ * @all - slice with start/size of whole disk
+ *
+ *  0 if this isn't our partition table
+ *  number of partitions if successful
+ *
+ */
+int
+read_gpt_pt (int fd, struct slice all, struct slice *sp, int ns)
+{
+	gpt_header *gpt = NULL;
+	gpt_entry *ptes = NULL;
+	uint32_t i;
+	int n = 0;
+        int last_used_index=-1;
+
+	if (!find_valid_gpt (fd, &gpt, &ptes) || !gpt || !ptes) {
+		if (gpt)
+			free (gpt);
+		if (ptes)
+			free (ptes);
+		return 0;
+	}
+
+	for (i = 0; i < __le32_to_cpu(gpt->num_partition_entries) && i < ns; i++) {
+		if (!efi_guidcmp (NULL_GUID, ptes[i].partition_type_guid)) {
+			sp[n].start = 0;
+			sp[n].size = 0;
+			n++;
+		} else {
+			sp[n].start = __le64_to_cpu(ptes[i].starting_lba);
+			sp[n].size  = __le64_to_cpu(ptes[i].ending_lba) -
+				__le64_to_cpu(ptes[i].starting_lba) + 1;
+                        last_used_index=n;
+			n++;
+		}
+	}
+	free (ptes);
+	free (gpt);
+	return last_used_index+1;
+}

Modified: multipath-tools/trunk/kpartx/kpartx.c
==============================================================================
--- multipath-tools/trunk/kpartx/kpartx.c	(original)
+++ multipath-tools/trunk/kpartx/kpartx.c	Sat Aug 13 15:23:02 2005
@@ -10,7 +10,6 @@
  * cva, 2002-10-26
  */
 
-#include "kpartx.h"
 #include <stdio.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -21,15 +20,16 @@
 #include <sys/types.h>
 #include <ctype.h>
 #include <libdevmapper.h>
-#include <devmapper.h>
+#include <linux/kdev_t.h>
 
+#include "devmapper.h"
 #include "crc32.h"
-
-/* loop devices */
 #include "lopart.h"
+#include "kpartx.h"
 
 #define SIZE(a) (sizeof(a)/sizeof((a)[0]))
 
+#define READ_SIZE	1024
 #define MAXTYPES	64
 #define MAXSLICES	256
 #define DM_TARGET	"linear"
@@ -123,20 +123,66 @@
 
 	return (int)(q - device) + 1;
 }
-			
+
+static char *
+get_hotplug_device(void)
+{
+	unsigned int major, minor, off, len;
+	const char *mapname;
+	char *devname = NULL;
+	char *device = NULL;
+	char *var = NULL;
+	struct stat buf;
+
+	var = getenv("ACTION");
+
+	if (!var || strcmp(var, "add"))
+		return NULL;
+
+	/* Get dm mapname for hotpluged device. */
+	if (!(devname = getenv("DEVNAME")))
+		return NULL;
+
+	if (stat(devname, &buf))
+		return NULL;
+
+	major = (unsigned int)MAJOR(buf.st_rdev);
+	minor = (unsigned int)MINOR(buf.st_rdev);
+
+	if (!(mapname = dm_mapname(major, minor))) /* Not dm device. */
+		return NULL;
+
+	off = find_devname_offset(devname);
+	len = strlen(mapname);
+
+	/* Dirname + mapname + \0 */
+	if (!(device = (char *)malloc(sizeof(char) * (off + len + 1))))
+		return NULL;
+
+	/* Create new device name. */
+	snprintf(device, off + 1, "%s", devname);
+	snprintf(device + off, len + 1, "%s", mapname);
+
+	if (strlen(device) != (off + len))
+		return NULL;
+
+	return device;
+}
+
 int
 main(int argc, char **argv){
         int fd, i, j, k, n, op, off, arg;
 	struct slice all;
 	struct pt *ptp;
 	enum action what = LIST;
-	char *p, *type, *diskdevice, *device;
+	char *p, *type, *diskdevice, *device, *progname;
 	int lower, upper;
 	int verbose = 0;
 	char partname[PARTNAME_SIZE], params[PARTNAME_SIZE + 16];
 	char * loopdev = NULL;
 	char * delim = NULL;
 	int loopro = 0;
+	int hotplug = 0;
 	struct stat buf;
 
 	initpts();
@@ -147,7 +193,24 @@
 	memset(&all, 0, sizeof(all));
 	memset(&partname, 0, sizeof(partname));
 	
-	if (argc < 2) {
+	/* Check whether hotplug mode. */
+	progname = strrchr(argv[0], '/');
+
+	if (!progname)
+		progname = argv[0];
+	else
+		progname++;
+
+	if (!strcmp(progname, "kpartx.dev")) { /* Hotplug mode */
+		hotplug = 1;
+
+		/* Setup for original kpartx variables */
+		if (!(device = get_hotplug_device()))
+			exit(1);
+
+		diskdevice = device;
+		what = ADD;
+	} else if (argc < 2) {
 		usage();
 		exit(1);
 	}
@@ -192,7 +255,9 @@
 		exit(1);
 	}
 
-	if (optind == argc-2) {
+	if (hotplug) {
+		/* already got [disk]device */
+	} else if (optind == argc-2) {
 		device = argv[optind];
 		diskdevice = argv[optind+1];
 	} else if (optind == argc-1) {
@@ -202,8 +267,8 @@
 		exit(1);
 	}
 
-	if (stat (device, &buf)) {
-		printf("failed to stat() device\n");
+	if (stat(device, &buf)) {
+		printf("failed to stat() %s\n", device);
 		exit (1);
 	}
 
@@ -438,9 +503,9 @@
 	bp->secnr = secnr;
 	bp->next = blockhead;
 	blockhead = bp;
-	bp->block = (char *) xmalloc(1024);
+	bp->block = (char *) xmalloc(READ_SIZE);
 	
-	if (read(fd, bp->block, 1024) != 1024) {
+	if (read(fd, bp->block, READ_SIZE) != READ_SIZE) {
 		fprintf(stderr, "read error, sector %d\n", secnr);
 		bp->block = NULL;
 	}

Added: multipath-tools/trunk/kpartx/kpartx.dev
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/kpartx/kpartx.dev	Sat Aug 13 15:23:02 2005
@@ -0,0 +1 @@
+link kpartx
\ No newline at end of file

Modified: multipath-tools/trunk/kpartx/lopart.c
==============================================================================
--- multipath-tools/trunk/kpartx/lopart.c	(original)
+++ multipath-tools/trunk/kpartx/lopart.c	Sat Aug 13 15:23:02 2005
@@ -26,8 +26,8 @@
 #include <sys/stat.h>
 #include <sys/mman.h>
 #include <sysmacros.h>
+#include <linux/loop.h>
 
-#include "loop.h"
 #include "lopart.h"
 #include "xstrncpy.h"
 

Modified: multipath-tools/trunk/libcheckers/Makefile
==============================================================================
--- multipath-tools/trunk/libcheckers/Makefile	(original)
+++ multipath-tools/trunk/libcheckers/Makefile	Sat Aug 13 15:23:02 2005
@@ -6,30 +6,19 @@
 
 include ../Makefile.inc
 
-OBJS = readsector0.o tur.o selector.o checkers.o emc_clariion.o
-
-ifeq ($(strip $(BUILD)),klibc)
-	CFLAGS = ${REQFLAGS} ${OPTFLAGS}
-else
-	CFLAGS =
-endif
+OBJS = readsector0.o tur.o selector.o emc_clariion.o
 
 all: $(BUILD)
 
 prepare:
 	rm -f core *.o *.gz
 
-klibc: prepare $(KLIBC) $(OBJS)
+klibc: prepare $(OBJS)
 	ar rs libcheckers-klibc.a *.o
 
 glibc: prepare $(OBJS)
 	ar rs libcheckers-glibc.a *.o
 
-$(KLIBC):
-	rm -f $(klibcdir)/linux
-	ln -s $(KERNEL_BUILD) $(klibcdir)/linux
-	make -C $(klibcdir)
-
 install:
 
 uninstall:

Modified: multipath-tools/trunk/libcheckers/checkers.h
==============================================================================
--- multipath-tools/trunk/libcheckers/checkers.h	(original)
+++ multipath-tools/trunk/libcheckers/checkers.h	Sat Aug 13 15:23:02 2005
@@ -5,12 +5,6 @@
 #define DEVNODE_SIZE 256
 #define MAX_CHECKER_MSG_SIZE 256
 
-enum devnode_actions {
-	CREATE_NODE,
-	UNLINK_NODE,
-	OPEN_NODE
-};
-
 enum checkers {
 	CHECKER_RESERVED,
 	TUR,
@@ -21,14 +15,12 @@
 #define MSG(a) if (msg != NULL) \
 			snprintf(msg, MAX_CHECKER_MSG_SIZE, "%s\n", a);
 
-int devnode (int, char *);
-
 int get_checker_id (char *);
 void *get_checker_addr (int);
 int get_checker_name (char *, int);
 
-int emc_clariion (char *, char *, void **);
-int readsector0 (char *, char *, void **);
-int tur (char *, char *, void **);
+int emc_clariion (int fd, char * msg, void ** ctxt);
+int readsector0 (int fd, char * msg, void ** ctxt);
+int tur (int fd, char * msg, void ** ctxt);
 
 #endif /* _CHECKERS_H */

Modified: multipath-tools/trunk/libcheckers/emc_clariion.c
==============================================================================
--- multipath-tools/trunk/libcheckers/emc_clariion.c	(original)
+++ multipath-tools/trunk/libcheckers/emc_clariion.c	Sat Aug 13 15:23:02 2005
@@ -8,25 +8,25 @@
 #include <sys/ioctl.h>
 #include <errno.h>
 
-#include "sg_include.h"
 #include "path_state.h"
 #include "checkers.h"
 
+#include "../libmultipath/sg_include.h"
+
 #define INQUIRY_CMD     0x12
 #define INQUIRY_CMDLEN  6
 #define HEAVY_CHECK_COUNT       10
 
 struct emc_clariion_checker_context {
-	int fd;
 	int run_count;
 	char wwn[16];
 	unsigned wwn_set;
 };
 
-int emc_clariion(char *devt, char *msg, void **context)
+int emc_clariion(int fd, char *msg, void **context)
 {
-	unsigned char sense_buffer[256];
-	unsigned char sb[128];
+	unsigned char sense_buffer[256] = { 0, };
+	unsigned char sb[128] = { 0, };
 	unsigned char inqCmdBlk[INQUIRY_CMDLEN] = {INQUIRY_CMD, 1, 0xC0, 0,
 						sizeof(sb), 0};
 	struct sg_io_hdr io_hdr;
@@ -61,13 +61,10 @@
 		/* do stuff */
 	}
 
-	if (!ctxt->fd) {
-		if (devnode(CREATE_NODE, devt)) {
-			ret = -1;
-			goto out;
-		}
-		ctxt->fd = devnode(OPEN_NODE, devt);
-		devnode(UNLINK_NODE, devt);
+	if (fd <= 0) {
+		MSG("no usable fd");
+		ret = -1;
+		goto out;
 	}
 	memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
 	io_hdr.interface_id = 'S';
@@ -80,7 +77,7 @@
 	io_hdr.sbp = sb;
 	io_hdr.timeout = 60000;
 	io_hdr.pack_id = 0;
-	if (ioctl(ctxt->fd, SG_IO, &io_hdr) < 0) {
+	if (ioctl(fd, SG_IO, &io_hdr) < 0) {
 		MSG("emc_clariion_checker: sending query command failed");
 		ret = PATH_DOWN;
 		goto out;
@@ -149,9 +146,8 @@
 	 * caller told us he doesn't want to keep the context :
 	 * free it
 	 */
-	if (!context) {
-		close(ctxt->fd);
+	if (!context)
 		free(ctxt);
-	}
+
 	return(ret);
 }

Modified: multipath-tools/trunk/libcheckers/path_state.h
==============================================================================
--- multipath-tools/trunk/libcheckers/path_state.h	(original)
+++ multipath-tools/trunk/libcheckers/path_state.h	Sat Aug 13 15:23:02 2005
@@ -1,3 +1,4 @@
-#define PATH_DOWN	0
-#define PATH_UP		1
-#define PATH_SHAKY	2
+#define PATH_UNCHECKED	0
+#define PATH_DOWN	1
+#define PATH_UP		2
+#define PATH_SHAKY	3

Modified: multipath-tools/trunk/libcheckers/readsector0.c
==============================================================================
--- multipath-tools/trunk/libcheckers/readsector0.c	(original)
+++ multipath-tools/trunk/libcheckers/readsector0.c	Sat Aug 13 15:23:02 2005
@@ -8,10 +8,11 @@
 #include <sys/ioctl.h>
 #include <errno.h>
 
-#include "sg_include.h"
 #include "path_state.h"
 #include "checkers.h"
 
+#include "../libmultipath/sg_include.h"
+
 #define SENSE_BUFF_LEN 32
 #define DEF_TIMEOUT 60000
 
@@ -19,7 +20,7 @@
 #define MSG_READSECTOR0_DOWN	"readsector0 checker reports path is down"
 
 struct readsector0_checker_context {
-	int fd;
+	void * dummy;
 };
 
 static int
@@ -82,7 +83,7 @@
 }
 
 extern int
-readsector0 (char *devt, char *msg, void **context)
+readsector0 (int fd, char *msg, void **context)
 {
 	char buf[512];
 	struct readsector0_checker_context * ctxt = NULL;
@@ -109,16 +110,12 @@
 		if (context)
 			*context = ctxt;
 	}
-	if (!ctxt->fd) {
-		if (devnode(CREATE_NODE, devt)) {
-			MSG("cannot create node");
-			ret = -1;
-			goto out;
-		}
-		ctxt->fd = devnode(OPEN_NODE, devt);
-		devnode(UNLINK_NODE, devt);
+	if (fd <= 0) {
+		MSG("no usable fd");
+		ret = -1;
+		goto out;
 	}
-	ret = sg_read(ctxt->fd, &buf[0]);
+	ret = sg_read(fd, &buf[0]);
 
 	switch (ret)
 	{
@@ -136,9 +133,8 @@
 	 * caller told us he doesn't want to keep the context :
 	 * free it
 	 */
-	if (!context) {
-		close(ctxt->fd);
+	if (!context)
 		free(ctxt);
-	}
+
 	return ret;
 }

Modified: multipath-tools/trunk/libcheckers/selector.c
==============================================================================
--- multipath-tools/trunk/libcheckers/selector.c	(original)
+++ multipath-tools/trunk/libcheckers/selector.c	Sat Aug 13 15:23:02 2005
@@ -18,7 +18,7 @@
 extern void * 
 get_checker_addr (int id)
 {
-	int (*checker) (char *, char *, void **);
+	int (*checker) (int, char *, void **);
 
 	switch (id) {
 	case TUR:

Modified: multipath-tools/trunk/libcheckers/tur.c
==============================================================================
--- multipath-tools/trunk/libcheckers/tur.c	(original)
+++ multipath-tools/trunk/libcheckers/tur.c	Sat Aug 13 15:23:02 2005
@@ -8,10 +8,11 @@
 #include <sys/ioctl.h>
 #include <errno.h>
 
-#include "sg_include.h"
 #include "path_state.h"
 #include "checkers.h"
 
+#include "../libmultipath/sg_include.h"
+
 #define TUR_CMD_LEN 6
 #define HEAVY_CHECK_COUNT       10
 
@@ -19,13 +20,12 @@
 #define MSG_TUR_DOWN	"tur checker reports path is down"
 
 struct tur_checker_context {
-	int fd;
 	int run_count;
 };
 
 
 extern int
-tur (char *devt, char *msg, void **context)
+tur (int fd, char *msg, void **context)
 {
         unsigned char turCmdBlk[TUR_CMD_LEN] = { 0x00, 0, 0, 0, 0, 0 };
         struct sg_io_hdr io_hdr;
@@ -60,14 +60,10 @@
 		ctxt->run_count = 0;
 		/* do stuff */
 	}
-	if (!ctxt->fd) {
-		if (devnode(CREATE_NODE, devt)) {
-			MSG("cannot create node");
-			ret = -1;
-			goto out;
-		}
-		ctxt->fd = devnode(OPEN_NODE, devt);
-		devnode(UNLINK_NODE, devt);
+	if (fd <= 0) {
+		MSG("no usable fd");
+		ret = -1;
+		goto out;
 	}
 	
         memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
@@ -79,7 +75,7 @@
         io_hdr.sbp = sense_buffer;
         io_hdr.timeout = 20000;
         io_hdr.pack_id = 0;
-        if (ioctl(ctxt->fd, SG_IO, &io_hdr) < 0) {
+        if (ioctl(fd, SG_IO, &io_hdr) < 0) {
 		MSG(MSG_TUR_DOWN);
                 ret = PATH_DOWN;
 		goto out;
@@ -97,9 +93,8 @@
 	 * caller told us he doesn't want to keep the context :
 	 * free it
 	 */
-	if (!context) {
-		close(ctxt->fd);
+	if (!context)
 		free(ctxt);
-	}
+
 	return(ret);
 }

Modified: multipath-tools/trunk/libmultipath/Makefile
==============================================================================
--- multipath-tools/trunk/libmultipath/Makefile	(original)
+++ multipath-tools/trunk/libmultipath/Makefile	Sat Aug 13 15:23:02 2005
@@ -7,12 +7,14 @@
 include ../Makefile.inc
 
 OBJS = memory.o parser.o vector.o devmapper.o callout.o \
-       sysfs_devinfo.o hwtable.o blacklist.o util.o
+       hwtable.o blacklist.o util.o dmparser.o config.o \
+       structs.o cache.o discovery.o propsel.o dict.o \
+       pgpolicies.o debug.o regex.o defaults.o
 
-ifeq ($(strip $(BUILD)),klibc)
-	CFLAGS = ${REQFLAGS} ${OPTFLAGS} -I$(libdmdir)
-else
-	CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes
+CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes
+
+ifeq ($(strip $(DAEMON)),1)
+	CFLAGS += -DDAEMON
 endif
 
 all: $(BUILD)
@@ -20,17 +22,12 @@
 prepare:
 	rm -f core *.o *.gz
 
-klibc: prepare $(KLIBC) $(OBJS)
+klibc: prepare $(OBJS)
 	ar rs libmultipath-klibc.a *.o
 
 glibc: prepare $(OBJS)
 	ar rs libmultipath-glibc.a *.o
 
-$(KLIBC):
-	rm -f $(klibcdir)/linux
-	ln -s $(KERNEL_BUILD) $(klibcdir)/linux
-	make -C $(klibcdir)
-
 install:
 
 uninstall:

Modified: multipath-tools/trunk/libmultipath/blacklist.c
==============================================================================
--- multipath-tools/trunk/libmultipath/blacklist.c	(original)
+++ multipath-tools/trunk/libmultipath/blacklist.c	Sat Aug 13 15:23:02 2005
@@ -1,46 +1,111 @@
-#include <string.h>
 #include <stdio.h>
 
 #include "memory.h"
 #include "vector.h"
 #include "util.h"
+#include "debug.h"
+#include "regex.h"
 #include "blacklist.h"
 
-void
+static int
+store_ble (vector blist, char * str)
+{
+	struct blentry * ble;
+	
+	if (!str)
+		return 0;
+
+	ble = (struct blentry *)MALLOC(sizeof(struct blentry));
+
+	if (!ble)
+		goto out;
+
+	ble->preg = MALLOC(sizeof(regex_t));
+
+	if (!ble->preg)
+		goto out1;
+
+	ble->str = (char *)MALLOC(strlen(str) + 1);
+
+	if (!ble->str)
+		goto out2;
+
+	strcpy(ble->str, str);
+
+	if (regcomp((regex_t *)ble->preg, ble->str, REG_EXTENDED|REG_NOSUB))
+		goto out3;
+
+	if (!vector_alloc_slot(blist))
+		goto out3;
+
+	vector_set_slot(blist, ble);
+	return 0;
+out3:
+	FREE(ble->str);
+out2:
+	FREE(ble->preg);
+out1:
+	FREE(ble);
+out:
+	return 1;
+}
+
+int
 setup_default_blist (vector blist)
 {
-	char * str;
+	int r = 0;
 
-	VECTOR_ADDSTR(blist, "c0d");
-	VECTOR_ADDSTR(blist, "c1d");
-	VECTOR_ADDSTR(blist, "c2d");
-	VECTOR_ADDSTR(blist, "fd");
-	VECTOR_ADDSTR(blist, "hd");
-	VECTOR_ADDSTR(blist, "md");
-	VECTOR_ADDSTR(blist, "dm");
-	VECTOR_ADDSTR(blist, "sr");
-	VECTOR_ADDSTR(blist, "scd");
-	VECTOR_ADDSTR(blist, "st");
-	VECTOR_ADDSTR(blist, "ram");
-	VECTOR_ADDSTR(blist, "raw");
-	VECTOR_ADDSTR(blist, "loop");
-	VECTOR_ADDSTR(blist, "nbd");
-	VECTOR_ADDSTR(blist, "ub");
+	r += store_ble(blist, "(ram|raw|loop|fd|md|dm-|sr|scd|st)[0-9]*");
+	r += store_ble(blist, "hd[a-z]");
+	r += store_ble(blist, "cciss!c[0-9]d[0-9]*");
+
+	return r;
 }
 
 int
 blacklist (vector blist, char * dev)
 {
 	int i;
-	char *p;
-	char buff[BLIST_ENTRY_SIZE];
-
-	basename(dev, buff);
+	struct blentry *ble;
 
-	vector_foreach_slot (blist, p, i) {
-		if (memcmp(buff, p, strlen(p)) == 0)
+	vector_foreach_slot (blist, ble, i) {
+		if (!regexec(ble->preg, dev, 0, NULL, 0)) {
+			condlog(3, "%s blacklisted", dev);
 			return 1;
+		}
 	}
 	return 0;
 }
 
+int
+store_regex (vector blist, char * regex)
+{
+	if (!blist)
+		return 1;
+
+	if (!regex)
+		return 1;
+
+	return store_ble(blist, regex);
+}	
+
+void
+free_blacklist (vector blist)
+{
+	struct blentry * ble;
+	int i;
+
+	if (!blist)
+		return;
+
+	vector_foreach_slot (blist, ble, i) {
+		if (ble->str)
+			FREE(ble->str);
+
+		if (ble->preg)
+			FREE(ble->preg);
+
+		FREE(ble);
+	}
+	vector_free(blist);
+}

Modified: multipath-tools/trunk/libmultipath/blacklist.h
==============================================================================
--- multipath-tools/trunk/libmultipath/blacklist.h	(original)
+++ multipath-tools/trunk/libmultipath/blacklist.h	Sat Aug 13 15:23:02 2005
@@ -3,13 +3,14 @@
 
 #define BLIST_ENTRY_SIZE 255
 
-#define VECTOR_ADDSTR(a, b) \
-	str = zalloc(6 * sizeof(char)); \
-	snprintf(str, 6, b); \
-	vector_alloc_slot(a); \
-	vector_set_slot(a, str);
+struct blentry {
+	char * str;
+	void * preg;
+};
 
-void setup_default_blist (vector blist);
+int setup_default_blist (vector blist);
 int blacklist (vector blist, char * dev);
+int store_regex (vector blist, char * regex);
+void free_blacklist (vector blist);
 
 #endif /* _BLACKLIST_H */

Added: multipath-tools/trunk/libmultipath/cache.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/cache.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,129 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <time.h>
+
+#include "vector.h"
+#include "structs.h"
+#include "debug.h"
+#include "cache.h"
+
+static void
+revoke_cache_info(struct path * pp)
+{
+	pp->fd = 0;
+}
+
+static int
+lock_fd (int fd, int flag)
+{
+	struct flock fl;
+
+	fl.l_type = flag;
+	fl.l_whence = 0;
+	fl.l_start = 0;
+	fl.l_len = 0;
+
+	alarm(MAX_WAIT);
+
+	if (fcntl(fd, F_SETLKW, &fl) == -1) {
+		condlog(0, "can't take a write lease on cache file\n");
+		return 1;
+	}
+	alarm(0);
+	return 0;
+}
+
+int
+cache_load (vector pathvec)
+{
+	int fd;
+	int r = 1;
+	off_t record_len;
+	struct path record;
+	struct path * pp;
+
+	fd = open(CACHE_FILE, O_RDONLY);
+
+	if (fd < 0)
+		return 1;
+
+	if (lock_fd(fd, F_RDLCK))
+		goto out;
+
+	record_len = sizeof(struct path);
+
+	while (read(fd, &record, record_len)) {
+		pp = alloc_path();
+
+		if (!pp)
+			goto out;
+
+		if (!vector_alloc_slot(pathvec)) {
+			free_path(pp);
+			goto out;
+		}
+		vector_set_slot(pathvec, pp);
+		memcpy(pp, &record, record_len);
+		revoke_cache_info(pp);
+	}
+	r = 0;
+	lock_fd(fd, F_UNLCK);
+out:
+	close(fd);
+	return r;
+}
+
+int
+cache_dump (vector pathvec)
+{
+	int i;
+	int fd;
+	int r = 1;
+	off_t record_len;
+	struct path * pp;
+
+	fd = open(CACHE_TMPFILE, O_RDWR|O_CREAT, 0600);
+
+	if (fd < 0)
+		return 1;
+
+	if (lock_fd(fd, F_WRLCK))
+		goto out;
+
+	ftruncate(fd, 0); 
+	record_len = sizeof(struct path);
+
+	vector_foreach_slot (pathvec, pp, i) {
+		if (write(fd, pp, record_len) < record_len)
+			goto out1;
+	}
+	rename(CACHE_TMPFILE, CACHE_FILE);
+	r = 0;
+out1:
+	lock_fd(fd, F_UNLCK);
+out:
+	close(fd);
+	return r;
+}
+
+int
+cache_cold (int expire)
+{
+	time_t t;
+	struct stat s;
+
+	if (time(&t) < 0)
+		return 1;
+
+	if(stat(CACHE_FILE, &s))
+		return 1;
+
+	if ((t - s.st_mtime) < expire)
+		return 0;
+
+	return 1;
+}

Added: multipath-tools/trunk/libmultipath/cache.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/cache.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,8 @@
+#define CACHE_FILE	"/var/cache/multipath/.multipath.cache"
+#define CACHE_TMPFILE	"/var/cache/multipath/.multipath.cache.tmp"
+#define CACHE_EXPIRE	5
+#define MAX_WAIT	5
+
+int cache_load (vector pathvec);
+int cache_dump (vector pathvec);
+int cache_cold (int expire);

Modified: multipath-tools/trunk/libmultipath/callout.c
==============================================================================
--- multipath-tools/trunk/libmultipath/callout.c	(original)
+++ multipath-tools/trunk/libmultipath/callout.c	Sat Aug 13 15:23:02 2005
@@ -92,8 +92,8 @@
 			i--;
 		value[i] = '\0';
 
-		close(fds[0]);
 		wait(&status);
+		close(fds[0]);
 
 		if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
 			retval = -1;

Added: multipath-tools/trunk/libmultipath/config.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/config.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,464 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "memory.h"
+#include "util.h"
+#include "debug.h"
+#include "parser.h"
+#include "dict.h"
+#include "hwtable.h"
+#include "vector.h"
+#include "blacklist.h"
+#include "defaults.h"
+#include "config.h"
+
+#include "../libcheckers/checkers.h"
+
+/*
+ * helper function to draw a list of callout binaries found in the config file
+ */
+extern int
+push_callout(char * callout)
+{
+	int i;
+	char * bin;
+	char * p;
+
+	/*
+	 * purge command line arguments
+	 */
+	p = callout;
+
+	while (*p != ' ' && *p != '\0')
+		p++;
+
+	if (!conf->binvec)
+		conf->binvec = vector_alloc();
+
+
+	if (!conf->binvec)
+		return 1;
+
+	/*
+	 * if this callout is already stored in binvec, don't store it twice
+	 */
+	vector_foreach_slot (conf->binvec, bin, i)
+		if (memcmp(bin, callout, p - callout) == 0)
+			return 0;
+
+	/*
+	 * else, store it
+	 */
+	bin = MALLOC((p - callout) + 1);
+
+	if (!bin)
+		return 1;
+
+	strncpy(bin, callout, p - callout);
+
+	if (!vector_alloc_slot(conf->binvec))
+		return 1;
+
+	vector_set_slot(conf->binvec, bin);
+
+	return 0;
+}
+
+struct hwentry *
+find_hwe (vector hwtable, char * vendor, char * product)
+{
+	int i;
+	struct hwentry * hwe;
+
+	vector_foreach_slot (hwtable, hwe, i) {
+		if (strcmp_chomp(hwe->vendor, vendor) == 0 &&
+		    (hwe->product[0] == '*' ||
+		    strcmp_chomp(hwe->product, product) == 0))
+			return hwe;
+	}
+	return NULL;
+}
+
+extern struct mpentry *
+find_mpe (char * wwid)
+{
+	int i;
+	struct mpentry * mpe;
+
+	if (!wwid)
+		return NULL;
+
+	vector_foreach_slot (conf->mptable, mpe, i)
+		if (mpe->wwid && strcmp(mpe->wwid, wwid) == 0)
+			return mpe;
+
+	return NULL;
+}
+
+extern char *
+get_mpe_wwid (char * alias)
+{
+	int i;
+	struct mpentry * mpe;
+
+	if (!alias)
+		return NULL;
+
+	vector_foreach_slot (conf->mptable, mpe, i)
+		if (mpe->alias && strcmp(mpe->alias, alias) == 0)
+			return mpe->wwid;
+
+	return NULL;
+}
+
+void
+free_hwe (struct hwentry * hwe)
+{
+	if (!hwe)
+		return;
+
+	if (hwe->vendor)
+		FREE(hwe->vendor);
+
+	if (hwe->product)
+		FREE(hwe->product);
+
+	if (hwe->selector)
+		FREE(hwe->selector);
+
+	if (hwe->getuid)
+		FREE(hwe->getuid);
+
+	if (hwe->getprio)
+		FREE(hwe->getprio);
+
+	if (hwe->features)
+		FREE(hwe->features);
+
+	if (hwe->hwhandler)
+		FREE(hwe->hwhandler);
+
+	FREE(hwe);
+}
+
+void
+free_hwtable (vector hwtable)
+{
+	int i;
+	struct hwentry * hwe;
+
+	if (!hwtable)
+		return;
+
+	vector_foreach_slot (hwtable, hwe, i)
+		free_hwe(hwe);
+
+	vector_free(hwtable);
+}
+
+void
+free_mpe (struct mpentry * mpe)
+{
+	if (!mpe)
+		return;
+
+	if (mpe->wwid)
+		FREE(mpe->wwid);
+
+	if (mpe->selector)
+		FREE(mpe->selector);
+
+	if (mpe->getuid)
+		FREE(mpe->getuid);
+
+	if (mpe->alias)
+		FREE(mpe->alias);
+
+	FREE(mpe);
+}
+
+void
+free_mptable (vector mptable)
+{
+	int i;
+	struct mpentry * mpe;
+
+	if (!mptable)
+		return;
+
+	vector_foreach_slot (mptable, mpe, i)
+		free_mpe(mpe);
+
+	vector_free(mptable);
+}
+
+static struct hwentry *
+alloc_hwe (void)
+{
+	return (struct hwentry *)MALLOC(sizeof(struct hwentry));
+}
+
+static char *
+set_param_str(char * str)
+{
+	char * dst;
+	int len;
+
+	if (!str)
+		return NULL;
+
+	len = strlen(str);
+
+	if (!len)
+		return NULL;
+
+	dst = (char *)MALLOC(len + 1);
+
+	if (!dst)
+		return NULL;
+
+	strcpy(dst, str);
+	return dst;
+}
+
+int
+store_hwe (vector hwtable, char * vendor, char * product, int pgp,
+	   char * getuid)
+{
+	struct hwentry * hwe;
+
+	hwe = alloc_hwe();
+
+	if (!hwe)
+		return 1;
+
+	hwe->vendor = set_param_str(vendor);
+
+	if (!hwe->vendor)
+		goto out;
+	
+	hwe->product = set_param_str(product);
+
+	if (!hwe->product)
+		goto out;
+	
+	if (pgp)
+		hwe->pgpolicy = pgp;
+
+	if (getuid) {
+		hwe->getuid = set_param_str(getuid);
+		push_callout(getuid);
+	} else {
+		hwe->getuid = set_default(DEFAULT_GETUID);
+		push_callout(DEFAULT_GETUID);
+	}
+
+	if (!hwe->getuid)
+		goto out;
+	
+	if (!vector_alloc_slot(hwtable))
+		goto out;
+
+	vector_set_slot(hwtable, hwe);
+	return 0;
+out:
+	free_hwe(hwe);
+	return 1;
+}
+
+int
+store_hwe_ext (vector hwtable, char * vendor, char * product, int pgp,
+	   char * getuid, char * getprio, char * hwhandler,
+	   char * features, char * checker)
+{
+	struct hwentry * hwe;
+
+	hwe = alloc_hwe();
+
+	if (!hwe)
+		return 1;
+
+	hwe->vendor = set_param_str(vendor);
+
+	if (!hwe->vendor)
+		goto out;
+	
+	hwe->product = set_param_str(product);
+
+	if (!hwe->product)
+		goto out;
+	
+	if (pgp)
+		hwe->pgpolicy = pgp;
+
+	if (getuid) {
+		hwe->getuid = set_param_str(getuid);
+		push_callout(getuid);
+	} else {
+		hwe->getuid = set_default(DEFAULT_GETUID);
+		push_callout(DEFAULT_GETUID);
+	}
+
+	if (!hwe->getuid)
+		goto out;
+	
+	if (getprio) {
+		hwe->getprio = set_param_str(getprio);
+		push_callout(getprio);
+	} else
+		hwe->getprio = NULL;
+
+	if (hwhandler)	
+		hwe->hwhandler = set_param_str(hwhandler);
+	else
+		hwe->hwhandler = set_default(DEFAULT_HWHANDLER);
+
+	if (!hwe->hwhandler)
+		goto out;
+
+	if (features)
+		hwe->features = set_param_str(features);
+	else
+		hwe->features = set_default(DEFAULT_FEATURES);
+
+	if (!hwe->features)
+		goto out;
+
+	if (checker)
+		hwe->checker_index = get_checker_id(checker);
+	else
+		hwe->checker_index = get_checker_id(DEFAULT_CHECKER);
+
+	if (!vector_alloc_slot(hwtable))
+		goto out;
+
+	vector_set_slot(hwtable, hwe);
+	return 0;
+out:
+	free_hwe(hwe);
+	return 1;
+}
+
+struct config *
+alloc_config (void)
+{
+	return (struct config *)MALLOC(sizeof(struct config));
+}
+
+void
+free_config (struct config * conf)
+{
+	if (!conf)
+		return;
+
+	if (conf->dev)
+		FREE(conf->dev);
+
+	if (conf->multipath)
+		FREE(conf->multipath);
+
+	if (conf->udev_dir)
+		FREE(conf->udev_dir);
+
+	if (conf->default_selector)
+		FREE(conf->default_selector);
+
+	if (conf->default_getuid)
+		FREE(conf->default_getuid);
+
+	if (conf->default_getprio)
+		FREE(conf->default_getprio);
+
+	if (conf->default_features)
+		FREE(conf->default_features);
+
+	if (conf->default_hwhandler)
+		FREE(conf->default_hwhandler);
+
+	free_blacklist(conf->blist);
+	free_mptable(conf->mptable);
+	free_hwtable(conf->hwtable);
+	free_strvec(conf->binvec);
+
+	FREE(conf);
+}
+
+int
+load_config (char * file)
+{
+	conf = alloc_config();
+
+	if (!conf)
+		return 1;
+
+	/*
+	 * internal defaults
+	 */
+	conf->verbosity = 2;
+	conf->signal = 1;		/* 1 == Send a signal to multipathd */
+	conf->dev_type = DEV_NONE;
+	conf->minio = 1000;
+
+	/*
+	 * read the config file
+	 */
+	if (filepresent(file)) {
+		if (init_data(file, init_keywords)) {
+			condlog(0, "error parsing config file");
+			goto out;
+		}
+	}
+	
+	/*
+	 * fill the voids left in the config file
+	 */
+	if (conf->hwtable == NULL) {
+		conf->hwtable = vector_alloc();
+		
+		if (!conf->hwtable)
+			goto out;
+		
+		if (setup_default_hwtable(conf->hwtable))
+			goto out;
+	}
+	if (conf->blist == NULL) {
+		conf->blist = vector_alloc();
+		
+		if (!conf->blist)
+			goto out;
+		
+		if (setup_default_blist(conf->blist))
+			goto out;
+	}
+	if (conf->mptable == NULL) {
+		conf->mptable = vector_alloc();
+
+		if (!conf->mptable)
+			goto out;
+	}
+	if (conf->default_selector == NULL)
+		conf->default_selector = set_default(DEFAULT_SELECTOR);
+
+	if (conf->udev_dir == NULL)
+		conf->udev_dir = set_default(DEFAULT_UDEVDIR);
+
+	if (conf->default_getuid == NULL)
+		conf->default_getuid = set_default(DEFAULT_GETUID);
+
+	if (conf->default_features == NULL)
+		conf->default_features = set_default(DEFAULT_FEATURES);
+
+	if (conf->default_hwhandler == NULL)
+		conf->default_hwhandler = set_default(DEFAULT_HWHANDLER);
+
+	if (!conf->default_selector  || !conf->udev_dir         ||
+	    !conf->default_getuid    || !conf->default_features ||
+	    !conf->default_hwhandler)
+		goto out;
+
+	return 0;
+out:
+	free_config(conf);
+	return 1;
+}
+

Added: multipath-tools/trunk/libmultipath/config.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/config.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,90 @@
+#ifndef _CONFIG_H
+#define _CONFIG_H
+
+#ifndef _VECTOR_H
+#include "vector.h"
+#endif
+
+enum devtypes {
+	DEV_NONE,
+	DEV_DEVT,
+	DEV_DEVNODE,
+	DEV_DEVMAP
+};
+
+struct hwentry {
+	int selector_args;
+	int pgpolicy;
+	int checker_index;
+
+	char * vendor;
+	char * product;
+	char * selector;
+	char * getuid;
+	char * getprio;
+	char * features;
+	char * hwhandler;
+};
+
+struct mpentry {
+	int selector_args;
+	int pgpolicy;
+
+	char * wwid;
+	char * selector;
+	char * getuid;
+	char * alias;
+};
+
+struct config {
+	int verbosity;
+	int dry_run;
+	int list;
+	int signal;
+	int pgpolicy_flag;
+	int with_sysfs;
+	int default_selector_args;
+	int default_pgpolicy;
+	int dev_type;
+	int minio;
+	int checkint;
+
+	char * dev;
+	char * multipath;
+	char * udev_dir;
+	char * default_selector;
+	char * default_getuid;
+	char * default_getprio;
+	char * default_features;
+	char * default_hwhandler;
+
+	vector mptable;
+	vector hwtable;
+	vector blist;
+	vector binvec;
+};
+
+struct config * conf;
+
+extern int push_callout(char * callout);
+
+struct hwentry * find_hwe (vector hwtable, char * vendor, char * product);
+struct mpentry * find_mpe (char * wwid);
+char * get_mpe_wwid (char * alias);
+
+void free_hwe (struct hwentry * hwe);
+void free_hwtable (vector hwtable);
+void free_mpe (struct mpentry * mpe);
+void free_mptable (vector mptable);
+
+int store_hwe (vector hwtable, char * vendor, char * product, int pgp,
+		char * getuid);
+int store_hwe_ext (vector hwtable, char * vendor, char * product, int pgp,
+		char * getuid, char * getprio, char * hwhandler,
+		char * features, char * checker);
+
+int load_config (char * file);
+struct config * alloc_config (void);
+void free_config (struct config * conf);
+
+#endif

Added: multipath-tools/trunk/libmultipath/debug.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/debug.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#include "config.h"
+
+void condlog (int prio, char * fmt, ...)
+{
+	va_list ap;
+	int thres;
+
+	if (!conf)
+		thres = 0;
+	else
+		thres = conf->verbosity;
+
+	va_start(ap, fmt);
+
+	if (prio <= thres) {
+		vfprintf(stdout, fmt, ap);
+		fprintf(stdout, "\n");
+	}
+	va_end(ap);
+}

Added: multipath-tools/trunk/libmultipath/debug.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/debug.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,8 @@
+void condlog (int prio, char * fmt, ...);
+
+#if DAEMON
+#include <pthread.h>
+#include "../multipathd/log_pthread.h"
+#define condlog(prio, fmt, args...) \
+	log_safe(prio + 3, fmt, ##args)
+#endif

Added: multipath-tools/trunk/libmultipath/defaults.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/defaults.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,20 @@
+#include <string.h>
+
+#include "memory.h"
+
+char *
+set_default (char * str)
+{
+	int len;
+	char * p;
+
+	len = strlen(str);
+	p = MALLOC(len + 1);
+
+	if (!p)
+		return NULL;
+
+	strncat(p, str, len);
+
+	return p;
+}

Modified: multipath-tools/trunk/libmultipath/defaults.h
==============================================================================
--- multipath-tools/trunk/libmultipath/defaults.h	(original)
+++ multipath-tools/trunk/libmultipath/defaults.h	Sat Aug 13 15:23:02 2005
@@ -1,11 +1,13 @@
 #define DEFAULT_GETUID		"/sbin/scsi_id -g -u -s /block/%n"
-#define DEFAULT_UDEVDIR		"/udev"
+#define DEFAULT_UDEVDIR		"/dev"
 #define DEFAULT_SELECTOR	"round-robin 0"
 #define DEFAULT_FEATURES	"0"
 #define DEFAULT_HWHANDLER	"0"
+#define DEFAULT_CHECKER		"readsector0"
 
 #define DEFAULT_TARGET		"multipath"
 #define DEFAULT_PIDFILE		"/var/run/multipathd.pid"
 #define DEFAULT_RUNFILE		"/var/run/multipath.run"
 #define DEFAULT_CONFIGFILE	"/etc/multipath.conf"
 
+char * set_default (char * str);

Modified: multipath-tools/trunk/libmultipath/devmapper.c
==============================================================================
--- multipath-tools/trunk/libmultipath/devmapper.c	(original)
+++ multipath-tools/trunk/libmultipath/devmapper.c	Sat Aug 13 15:23:02 2005
@@ -2,6 +2,13 @@
 #include <stdlib.h>
 #include <string.h>
 #include <libdevmapper.h>
+#include <ctype.h>
+#include <linux/kdev_t.h>
+
+#include "vector.h"
+#include "structs.h"
+#include "debug.h"
+#include "memory.h"
 
 extern int
 dm_prereq (char * str, int x, int y, int z)
@@ -14,6 +21,8 @@
 	if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
 		return 1;
 
+	dm_task_no_open_count(dmt);
+
 	if (!dm_task_run(dmt))
 		goto out;
 
@@ -50,6 +59,8 @@
 	if (!dm_task_set_name (dmt, name))
 		goto out;
 
+	dm_task_no_open_count(dmt);
+
 	r = dm_task_run (dmt);
 
 	out:
@@ -72,6 +83,8 @@
 	if (!dm_task_add_target (dmt, 0, size, target, params))
 		goto addout;
 
+	dm_task_no_open_count(dmt);
+
 	r = dm_task_run (dmt);
 
 	addout:
@@ -84,53 +97,47 @@
 {
 	int r = 0;
 	struct dm_task *dmt;
-	struct dm_names *names;
-	unsigned next = 0;
+	struct dm_info info;
 
-	if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
+	if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
 		return 0;
 
-	if (!dm_task_run (dmt))
+	if (!dm_task_set_name(dmt, str))
 		goto out;
 
-	if (!(names = dm_task_get_names (dmt)))
-		goto out;
+	dm_task_no_open_count(dmt);
 
-	if (!names->dev)
+	if (!dm_task_run(dmt))
 		goto out;
 
-	do {
-		if (0 == strcmp (names->name, str))
-			r = 1;
-
-		next = names->next;
-		names = (void *) names + next;
-	} while (next);
+	if (!dm_task_get_info(dmt, &info))
+		goto out;
 
-	out:
-	dm_task_destroy (dmt);
+	if (info.exists)
+		r = 1;
+out:
+	dm_task_destroy(dmt);
 	return r;
 }
 
 extern int
-dm_get_map(char * name, unsigned long * size, char ** outparams)
+dm_get_map(char * name, unsigned long * size, char * outparams)
 {
-	int r = 0;
+	int r = 1;
 	struct dm_task *dmt;
 	void *next = NULL;
 	uint64_t start, length;
 	char *target_type = NULL;
-	char *params;
-	int cmd;
-
-	cmd = DM_DEVICE_TABLE;
+	char *params = NULL;
 
-	if (!(dmt = dm_task_create(cmd)))
-		return 0;
+	if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
+		return 1;
 
 	if (!dm_task_set_name(dmt, name))
 		goto out;
 
+	dm_task_no_open_count(dmt);
+
 	if (!dm_task_run(dmt))
 		goto out;
 
@@ -141,40 +148,31 @@
 	if (size)
 		*size = length;
 
-	*outparams = malloc(strlen(params) + 1);
-
-	if (!*outparams)
-		goto out;
-
-	if (sprintf(*outparams, params))
-		goto out;
-
-	r = 1;
-
-	out:
+	if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
+		r = 0;
+out:
 	dm_task_destroy(dmt);
 	return r;
 }
 
 extern int
-dm_get_status(char * name, char ** outstatus)
+dm_get_status(char * name, char * outstatus)
 {
-	int r = 0;
+	int r = 1;
 	struct dm_task *dmt;
 	void *next = NULL;
 	uint64_t start, length;
-	char *target_type = NULL;
+	char *target_type;
 	char *status;
-	int cmd;
-
-	cmd = DM_DEVICE_STATUS;
 
-	if (!(dmt = dm_task_create(cmd)))
-		return 0;
+	if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
+		return 1;
 
 	if (!dm_task_set_name(dmt, name))
 		goto out;
 
+	dm_task_no_open_count(dmt);
+
 	if (!dm_task_run(dmt))
 		goto out;
 
@@ -182,17 +180,9 @@
 	next = dm_get_next_target(dmt, next, &start, &length,
 				  &target_type, &status);
 
-	*outstatus = malloc(strlen(status) + 1);
-
-	if (!*outstatus)
-		goto out;
-
-	if (sprintf(*outstatus, status))
-		goto out;
-
-	r = 1;
-
-	out:
+	if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
+		r = 0;
+out:
 	dm_task_destroy(dmt);
 	return r;
 }
@@ -206,16 +196,15 @@
 	uint64_t start, length;
 	char *target_type = NULL;
 	char *params;
-	int cmd;
-
-	cmd = DM_DEVICE_TABLE;
 
-	if (!(dmt = dm_task_create(cmd)))
+	if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
 		return 0;
 
 	if (!dm_task_set_name(dmt, name))
 		goto out;
 
+	dm_task_no_open_count(dmt);
+
 	if (!dm_task_run(dmt))
 		goto out;
 
@@ -223,16 +212,39 @@
 	next = dm_get_next_target(dmt, next, &start, &length,
 				  &target_type, &params);
 
-	if (0 == strcmp(target_type, type)) {
+	if (0 == strcmp(target_type, type))
 		r = 1;
-		goto out;
-	}
 
-	out:
+out:
 	dm_task_destroy(dmt);
 	return r;
 }
 
+int
+dm_get_opencount (char * mapname)
+{
+	int r = -1;
+	struct dm_task *dmt;
+	struct dm_info info;
+
+	if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
+		return 0;
+
+	if (!dm_task_set_name(dmt, mapname))
+		goto out;
+
+	if (!dm_task_run(dmt))
+		goto out;
+
+	if (!dm_task_get_info(dmt, &info))
+		goto out;
+
+	r = info.open_count;
+out:
+	dm_task_destroy(dmt);
+	return r;
+}
+	
 extern int
 dm_flush_maps (char * type)
 {
@@ -244,6 +256,8 @@
 	if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
 		return 0;
 
+	dm_task_no_open_count(dmt);
+
 	if (!dm_task_run (dmt))
 		goto out;
 
@@ -255,6 +269,7 @@
 
 	do {
 		if (dm_type(names->name, type) &&
+		    dm_get_opencount(names->name) == 0 &&
 		    !dm_simplecmd(DM_DEVICE_REMOVE, names->name))
 			r++;
 
@@ -266,3 +281,205 @@
 	dm_task_destroy (dmt);
 	return r;
 }
+
+int
+dm_fail_path(char * mapname, char * path)
+{
+	int r = 1;
+	struct dm_task *dmt;
+	char str[32];
+
+	if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
+		return 1;
+
+	if (!dm_task_set_name(dmt, mapname))
+		goto out;
+
+	if (!dm_task_set_sector(dmt, 0))
+		goto out;
+
+	if (snprintf(str, 32, "fail_path %s\n", path) > 32)
+		goto out;
+
+	if (!dm_task_set_message(dmt, str))
+		goto out;
+
+	dm_task_no_open_count(dmt);
+
+	if (!dm_task_run(dmt))
+		goto out;
+
+	r = 0;
+out:
+	dm_task_destroy(dmt);
+	return r;
+}
+
+int
+dm_reinstate(char * mapname, char * path)
+{
+	int r = 1;
+	struct dm_task *dmt;
+	char str[32];
+
+	if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
+		return 1;
+
+	if (!dm_task_set_name(dmt, mapname))
+		goto out;
+
+	if (!dm_task_set_sector(dmt, 0))
+		goto out;
+
+	if (snprintf(str, 32, "reinstate_path %s\n", path) > 32)
+		goto out;
+
+	if (!dm_task_set_message(dmt, str))
+		goto out;
+
+	dm_task_no_open_count(dmt);
+
+	if (!dm_task_run(dmt))
+		goto out;
+
+	r = 0;
+out:
+	dm_task_destroy(dmt);
+	return r;
+}
+
+int
+dm_switchgroup(char * mapname, int index)
+{
+	int r = 0;
+	struct dm_task *dmt;
+	char str[24];
+
+	if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
+		return 0;
+
+	if (!dm_task_set_name(dmt, mapname))
+		goto out;
+
+	if (!dm_task_set_sector(dmt, 0))
+		goto out;
+
+	snprintf(str, 24, "switch_group %i\n", index);
+	condlog(3, "message %s 0 %s", mapname, str);
+
+	if (!dm_task_set_message(dmt, str))
+		goto out;
+
+	dm_task_no_open_count(dmt);
+
+	if (!dm_task_run(dmt))
+		goto out;
+
+	r = 1;
+
+	out:
+	dm_task_destroy(dmt);
+
+	return r;
+}
+
+int
+dm_get_maps (vector mp, char * type)
+{
+	struct multipath * mpp;
+	int r = 1;
+	struct dm_task *dmt;
+	struct dm_names *names;
+	unsigned next = 0;
+
+	if (!type)
+		return 1;
+
+	if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
+		return 1;
+
+	dm_task_no_open_count(dmt);
+
+	if (!dm_task_run(dmt))
+		goto out;
+
+	if (!(names = dm_task_get_names(dmt)))
+		goto out;
+
+	if (!names->dev) {
+		r = 0; /* this is perfectly valid */
+		goto out;
+	}
+
+	do {
+		if (dm_type(names->name, type)) {
+			mpp = (struct multipath *)
+				MALLOC(sizeof(struct multipath));
+
+			if (!mpp)
+				goto out;
+
+			if (dm_get_map(names->name, &mpp->size, mpp->params))
+				goto out1;
+
+			if (dm_get_status(names->name, mpp->status))
+				goto out1;
+
+			mpp->alias = MALLOC(strlen(names->name) + 1);
+
+			if (!mpp->alias)
+				goto out1;
+
+			strncat(mpp->alias, names->name, strlen(names->name));
+
+			if (!vector_alloc_slot(mp))
+				goto out1;
+			
+			vector_set_slot(mp, mpp);
+			mpp = NULL;
+		}
+                next = names->next;
+                names = (void *) names + next;
+	} while (next);
+
+	r = 0;
+	goto out;
+out1:
+	free_multipath(mpp, KEEP_PATHS);
+out:
+	dm_task_destroy (dmt);
+	return r;
+}
+
+int
+dm_geteventnr (char *name)
+{
+	struct dm_task *dmt;
+	struct dm_info info;
+
+	if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
+		return 0;
+
+	if (!dm_task_set_name(dmt, name))
+		goto out;
+
+	dm_task_no_open_count(dmt);
+
+	if (!dm_task_run(dmt))
+		goto out;
+
+	if (!dm_task_get_info(dmt, &info)) {
+		info.event_nr = 0;
+		goto out;
+	}
+
+	if (!info.exists) {
+		info.event_nr = 0;
+		goto out;
+	}
+
+out:
+	dm_task_destroy(dmt);
+
+	return info.event_nr;
+}

Modified: multipath-tools/trunk/libmultipath/devmapper.h
==============================================================================
--- multipath-tools/trunk/libmultipath/devmapper.h	(original)
+++ multipath-tools/trunk/libmultipath/devmapper.h	Sat Aug 13 15:23:02 2005
@@ -2,7 +2,12 @@
 int dm_simplecmd (int, const char *);
 int dm_addmap (int, const char *, const char *, const char *, unsigned long);
 int dm_map_present (char *);
-int dm_get_map(char *, unsigned long *, char **);
-int dm_get_status(char *, char **);
+int dm_get_map(char *, unsigned long *, char *);
+int dm_get_status(char *, char *);
 int dm_type(char *, char *);
 int dm_flush_maps (char *);
+int dm_fail_path(char * mapname, char * path);
+int dm_reinstate(char * mapname, char * path);
+int dm_switchgroup(char * mapname, int index);
+int dm_get_maps (vector mp, char * type);
+int dm_geteventnr (char *name);

Added: multipath-tools/trunk/libmultipath/dict.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/dict.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,480 @@
+#include "vector.h"
+#include "hwtable.h"
+#include "structs.h"
+#include "parser.h"
+#include "config.h"
+#include "debug.h"
+#include "memory.h"
+#include "pgpolicies.h"
+#include "blacklist.h"
+
+#include "../libcheckers/checkers.h"
+
+/*
+ * default block handlers
+ */
+static int
+multipath_tool_handler(vector strvec)
+{
+	conf->multipath = set_value(strvec);
+
+	if (!conf->multipath)
+		return 1;
+
+	return push_callout(conf->multipath);
+}
+
+static int
+polling_interval_handler(vector strvec)
+{
+	char * buff;
+
+	buff = VECTOR_SLOT(strvec, 1);
+	conf->checkint = atoi(buff);
+
+	return 0;
+}
+
+static int
+udev_dir_handler(vector strvec)
+{
+	conf->udev_dir = set_value(strvec);
+
+	if (!conf->udev_dir)
+		return 1;
+
+	return 0;
+}
+
+static int
+def_selector_handler(vector strvec)
+{
+	conf->default_selector = set_value(strvec);
+
+	if (!conf->default_selector)
+		return 1;
+
+	return 0;
+}
+
+static int
+def_pgpolicy_handler(vector strvec)
+{
+	char * buff;
+
+	buff = set_value(strvec);
+
+	if (!buff)
+		return 1;
+
+	conf->default_pgpolicy = get_pgpolicy_id(buff);
+	FREE(buff);
+
+	return 0;
+}
+
+static int
+def_getuid_callout_handler(vector strvec)
+{
+	conf->default_getuid = set_value(strvec);
+
+	if (!conf->default_getuid)
+		return 1;
+	
+	return push_callout(conf->default_getuid);
+}
+
+static int
+def_prio_callout_handler(vector strvec)
+{
+	conf->default_getprio = set_value(strvec);
+
+	if (!conf->default_getprio)
+		return 1;
+	
+	return push_callout(conf->default_getprio);
+}
+
+static int
+def_features_handler(vector strvec)
+{
+	conf->default_features = set_value(strvec);
+
+	if (!conf->default_features)
+		return 1;
+
+	return 0;
+}
+
+static int
+def_minio_handler(vector strvec)
+{
+	char * buff;
+
+	buff = set_value(strvec);
+
+	if (!buff)
+		return 1;
+
+	conf->minio = atoi(buff);
+	FREE(buff);
+
+	return 0;
+}
+
+/*
+ * blacklist block handlers
+ */
+static int
+blacklist_handler(vector strvec)
+{
+	conf->blist = vector_alloc();
+
+	if (!conf->blist)
+		return 1;
+
+	return 0;
+}
+
+static int
+ble_handler(vector strvec)
+{
+	char * buff;
+	int ret;
+
+	buff = set_value(strvec);
+
+	if (!buff)
+		return 1;
+
+	ret = store_regex(conf->blist, buff);
+	FREE(buff);
+
+	return ret;
+}
+
+/*
+ * devices block handlers
+ */
+static int
+devices_handler(vector strvec)
+{
+	conf->hwtable = vector_alloc();
+
+	if (!conf->hwtable)
+		return 1;
+
+	return 0;
+}
+
+static int
+device_handler(vector strvec)
+{
+	struct hwentry * hwe;
+
+	hwe = (struct hwentry *)MALLOC(sizeof(struct hwentry));
+
+	if (!hwe)
+		return 1;
+
+	if (!vector_alloc_slot(conf->hwtable)) {
+		FREE(hwe);
+		return 1;
+	}
+	vector_set_slot(conf->hwtable, hwe);
+
+	return 0;
+}
+
+static int
+vendor_handler(vector strvec)
+{
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);
+
+	if (!hwe)
+		return 1;
+	
+	hwe->vendor = set_value(strvec);
+
+	if (!hwe->vendor)
+		return 1;
+
+	return 0;
+}
+
+static int
+product_handler(vector strvec)
+{
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);
+
+	if (!hwe)
+		return 1;
+	
+	hwe->product = set_value(strvec);
+
+	if (!hwe->product)
+		return 1;
+
+	return 0;
+}
+
+static int
+hw_pgpolicy_handler(vector strvec)
+{
+	char * buff;
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);
+
+	buff = set_value(strvec);
+
+	if (!buff)
+		return 1;
+
+	hwe->pgpolicy = get_pgpolicy_id(buff);
+	FREE(buff);
+
+	return 0;
+}
+
+static int
+hw_getuid_callout_handler(vector strvec)
+{
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);
+
+	hwe->getuid = set_value(strvec);
+
+	if (!hwe->getuid)
+		return 1;
+
+	return push_callout(hwe->getuid);
+}
+
+static int
+hw_selector_handler(vector strvec)
+{
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);
+	
+	if (!hwe)
+		return 1;
+
+	hwe->selector = set_value(strvec);
+
+	if (!hwe->selector)
+		return 1;
+
+	return 0;
+}
+
+static int
+hw_path_checker_handler(vector strvec)
+{
+	char * buff;
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);
+
+	if (!hwe)
+		return 1;
+
+	buff = set_value(strvec);
+
+	if (!buff)
+		return 1;
+	
+	hwe->checker_index = get_checker_id(buff);
+	FREE(buff);
+
+	return 0;
+}
+
+static int
+hw_features_handler(vector strvec)
+{
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);
+	
+	if (!hwe)
+		return 1;
+
+	hwe->features = set_value(strvec);
+
+	if (!hwe->features)
+		return 1;
+
+	return 0;
+}
+
+static int
+hw_handler_handler(vector strvec)
+{
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);
+	
+	if (!hwe)
+		return 1;
+
+	hwe->hwhandler = set_value(strvec);
+
+	if (!hwe->hwhandler)
+		return 1;
+
+	return 0;
+}
+
+static int
+prio_callout_handler(vector strvec)
+{
+	struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);
+	
+	if (!hwe)
+		return 1;
+
+	hwe->getprio = set_value(strvec);
+
+	if (!hwe->getprio)
+		return 1;
+
+	return push_callout(hwe->getprio);
+}
+
+/*
+ * multipaths block handlers
+ */
+static int
+multipaths_handler(vector strvec)
+{
+	conf->mptable = vector_alloc();
+
+	if (!conf->mptable)
+		return 1;
+
+	return 0;
+}
+
+static int
+multipath_handler(vector strvec)
+{
+	struct mpentry * mpe;
+
+	mpe = (struct mpentry *)MALLOC(sizeof(struct mpentry));
+
+	if (!mpe)
+		return 1;
+
+	if (!vector_alloc_slot(conf->mptable)) {
+		FREE(mpe);
+		return 1;
+	}
+	vector_set_slot(conf->mptable, mpe);
+
+	return 0;
+}
+
+static int
+wwid_handler(vector strvec)
+{
+	struct mpentry * mpe = VECTOR_LAST_SLOT(conf->mptable);
+
+	if (!mpe)
+		return 1;
+
+	mpe->wwid = set_value(strvec);
+
+	if (!mpe->wwid)
+		return 1;
+
+	return 0;
+}
+
+static int
+alias_handler(vector strvec)
+{
+	struct mpentry * mpe = VECTOR_LAST_SLOT(conf->mptable);
+
+	if (!mpe)
+		return 1;
+
+        mpe->alias = set_value(strvec);
+
+	if (!mpe->alias)
+		return 1;
+
+	return 0;
+}
+
+static int
+mp_pgpolicy_handler(vector strvec)
+{
+	char * buff;
+	struct mpentry * mpe = VECTOR_LAST_SLOT(conf->mptable);
+
+	if (!mpe)
+		return 1;
+
+	buff = set_value(strvec);
+
+	if (!buff)
+		return 1;
+
+	mpe->pgpolicy = get_pgpolicy_id(buff);
+	FREE(buff);
+
+	return 0;
+}
+
+static int
+mp_selector_handler(vector strvec)
+{
+	struct mpentry * mpe = VECTOR_LAST_SLOT(conf->mptable);
+	
+	if (!mpe)
+		return 1;
+
+	mpe->selector = set_value(strvec);
+
+	if (!mpe->selector)
+		return 1;
+
+	return 0;
+}
+
+vector
+init_keywords(void)
+{
+	keywords = vector_alloc();
+
+	install_keyword_root("defaults", NULL);
+	install_keyword("polling_interval", &polling_interval_handler);
+	install_keyword("multipath_tool", &multipath_tool_handler);
+	install_keyword("udev_dir", &udev_dir_handler);
+	install_keyword("default_selector", &def_selector_handler);
+	install_keyword("default_path_grouping_policy", &def_pgpolicy_handler);
+	install_keyword("default_getuid_callout", &def_getuid_callout_handler);
+	install_keyword("default_prio_callout", &def_prio_callout_handler);
+	install_keyword("default_features", &def_features_handler);
+	install_keyword("rr_min_io", &def_minio_handler);
+	
+	install_keyword_root("devnode_blacklist", &blacklist_handler);
+	install_keyword("devnode", &ble_handler);
+	install_keyword("wwid", &ble_handler);
+
+	install_keyword_root("devices", &devices_handler);
+	install_keyword("device", &device_handler);
+	install_sublevel();
+	install_keyword("vendor", &vendor_handler);
+	install_keyword("product", &product_handler);
+	install_keyword("path_grouping_policy", &hw_pgpolicy_handler);
+	install_keyword("getuid_callout", &hw_getuid_callout_handler);
+	install_keyword("path_selector", &hw_selector_handler);
+	install_keyword("path_checker", &hw_path_checker_handler);
+	install_keyword("features", &hw_features_handler);
+	install_keyword("hardware_handler", &hw_handler_handler);
+	install_keyword("prio_callout", &prio_callout_handler);
+	install_sublevel_end();
+
+	install_keyword_root("multipaths", &multipaths_handler);
+	install_keyword("multipath", &multipath_handler);
+	install_sublevel();
+	install_keyword("wwid", &wwid_handler);
+	install_keyword("alias", &alias_handler);
+	install_keyword("path_grouping_policy", &mp_pgpolicy_handler);
+	install_keyword("path_selector", &mp_selector_handler);
+	install_sublevel_end();
+
+	return keywords;
+}

Added: multipath-tools/trunk/libmultipath/dict.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/dict.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,10 @@
+#ifndef _DICT_H
+#define _DICT_H
+
+#ifndef _VECTOR_H
+#include "vector.h"
+#endif
+
+vector init_keywords(void);
+
+#endif /* _DICT_H */

Added: multipath-tools/trunk/libmultipath/discovery.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/discovery.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,514 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+
+#include <sysfs/dlist.h>
+#include <sysfs/libsysfs.h>
+
+#include "vector.h"
+#include "memory.h"
+#include "blacklist.h"
+#include "util.h"
+#include "structs.h"
+#include "callout.h"
+#include "config.h"
+#include "debug.h"
+#include "propsel.h"
+#include "sg_include.h"
+#include "discovery.h"
+
+#define readattr(a,b) \
+	sysfs_read_attribute_value(a, b, sizeof(b))
+
+int
+path_discovery (vector pathvec, struct config * conf, int flag)
+{
+	struct sysfs_directory * sdir;
+	struct sysfs_directory * devp;
+	char path[FILE_NAME_SIZE];
+	struct path * curpath;
+	int r = 1;
+
+	if(safe_sprintf(path, "%s/block", sysfs_path)) {
+		fprintf(stderr, "path too small\n");
+		exit(1);
+	}
+	sdir = sysfs_open_directory(path);
+	sysfs_read_directory(sdir);
+
+	dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
+		if (blacklist(conf->blist, devp->name))
+			continue;
+
+		if(safe_sprintf(path, "%s/block/%s/device", sysfs_path,
+				devp->name)) {
+			fprintf(stderr, "path too small\n");
+			exit(1);
+		}
+				
+		if (!filepresent(path))
+			continue;
+
+		curpath = find_path_by_dev(pathvec, devp->name);
+
+		if (!curpath) {
+			curpath = alloc_path();
+
+			if (!curpath)
+				goto out;
+
+			if (store_path(pathvec, curpath)) {
+				free_path(curpath);
+				goto out;
+			}
+			if(safe_sprintf(curpath->dev, "%s", devp->name)) {
+				fprintf(stderr, "curpath->dev too small\n");
+				exit(1);
+			}
+			devinfo(curpath, conf->hwtable, DI_ALL);
+		} else {
+			/*
+			 * path already known,
+			 * refresh only what the caller wants
+			 */
+			devinfo(curpath, conf->hwtable, flag);
+		}
+	}
+	r = 0;
+out:
+	sysfs_close_directory(sdir);
+	return r;
+}
+
+#define declare_sysfs_get_str(fname, fmt) \
+extern int \
+sysfs_get_##fname (char * sysfs_path, char * dev, char * buff, int len) \
+{ \
+	char attr_path[SYSFS_PATH_SIZE]; \
+	char attr_buff[SYSFS_PATH_SIZE]; \
+	int attr_len; \
+\
+	if(safe_sprintf(attr_path, fmt, sysfs_path, dev)) \
+		return 1; \
+	if (0 > sysfs_read_attribute_value(attr_path, attr_buff, sizeof(attr_buff))) \
+		return 1; \
+\
+	attr_len = strlen(attr_buff); \
+	if (attr_len < 2 || attr_len - 1 > len) \
+		return 1; \
+\
+	strncpy(buff, attr_buff, attr_len - 1); \
+	buff[attr_len - 1] = '\0'; \
+	return 0; \
+}
+
+declare_sysfs_get_str(vendor, "%s/block/%s/device/vendor");
+declare_sysfs_get_str(model, "%s/block/%s/device/model");
+declare_sysfs_get_str(rev, "%s/block/%s/device/rev");
+declare_sysfs_get_str(dev, "%s/block/%s/dev");
+
+#define declare_sysfs_get_val(fname, fmt) \
+extern unsigned long  \
+sysfs_get_##fname (char * sysfs_path, char * dev) \
+{ \
+	char attr_path[SYSFS_PATH_SIZE]; \
+	char attr_buff[SYSFS_PATH_SIZE]; \
+\
+	if(safe_sprintf(attr_path, fmt, sysfs_path, dev)) \
+		return 0; \
+	if (0 > sysfs_read_attribute_value(attr_path, attr_buff, sizeof(attr_buff))) \
+		return 0; \
+\
+	return strtoul(attr_buff, NULL, 0); \
+}
+
+declare_sysfs_get_val(size, "%s/block/%s/size");
+
+static int
+opennode (char * dev, int mode)
+{
+	char devpath[FILE_NAME_SIZE];
+	int fd;
+
+	if (safe_sprintf(devpath, "%s/%s", conf->udev_dir, dev)) {
+		fprintf(stderr, "devpath too small\n");
+		return -1;
+	}
+	fd = open(devpath, mode);
+
+	if (fd <= 0)
+		condlog(0, "open(%s) failed", devpath);
+	
+	return fd;
+}
+
+#if 0
+int
+get_claimed(int fd)
+{
+	/*
+	 * FIXME : O_EXCL always fails ?
+	 */
+	return 0;
+}	
+#endif
+
+extern int
+devt2devname (char *devname, char *devt)
+{
+	struct sysfs_directory * sdir;
+	struct sysfs_directory * devp;
+	char block_path[FILE_NAME_SIZE];
+	char attr_path[FILE_NAME_SIZE];
+	char attr_value[16];
+	int len;
+
+	if(safe_sprintf(block_path, "%s/block", sysfs_path)) {
+		fprintf(stderr, "block_path too small\n");
+		exit(1);
+	}
+	sdir = sysfs_open_directory(block_path);
+	sysfs_read_directory(sdir);
+
+	dlist_for_each_data (sdir->subdirs, devp, struct sysfs_directory) {
+		if(safe_sprintf(attr_path, "%s/%s/dev",
+				block_path, devp->name)) {
+			fprintf(stderr, "attr_path too small\n");
+			exit(1);
+		}
+		sysfs_read_attribute_value(attr_path, attr_value,
+					   sizeof(attr_value));
+
+		len = strlen(attr_value);
+
+		/* discard newline */
+		if (len > 1) len--;
+
+		if (strlen(devt) == len &&
+		    strncmp(attr_value, devt, len) == 0) {
+			if(safe_sprintf(attr_path, "%s/%s",
+					block_path, devp->name)) {
+				fprintf(stderr, "attr_path too small\n");
+				exit(1);
+			}
+			sysfs_get_name_from_path(attr_path, devname,
+						 FILE_NAME_SIZE);
+			sysfs_close_directory(sdir);
+			return 0;
+		}
+	}
+	sysfs_close_directory(sdir);
+	return 1;
+}
+
+static int
+do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
+       void *resp, int mx_resp_len, int noisy)
+{
+        unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
+            { INQUIRY_CMD, 0, 0, 0, 0, 0 };
+        unsigned char sense_b[SENSE_BUFF_LEN];
+        struct sg_io_hdr io_hdr;
+                                                                                                                 
+        if (cmddt)
+                inqCmdBlk[1] |= 2;
+        if (evpd)
+                inqCmdBlk[1] |= 1;
+        inqCmdBlk[2] = (unsigned char) pg_op;
+	inqCmdBlk[3] = (unsigned char)((mx_resp_len >> 8) & 0xff);
+	inqCmdBlk[4] = (unsigned char) (mx_resp_len & 0xff);
+        memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
+        io_hdr.interface_id = 'S';
+        io_hdr.cmd_len = sizeof (inqCmdBlk);
+        io_hdr.mx_sb_len = sizeof (sense_b);
+        io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
+        io_hdr.dxfer_len = mx_resp_len;
+        io_hdr.dxferp = resp;
+        io_hdr.cmdp = inqCmdBlk;
+        io_hdr.sbp = sense_b;
+        io_hdr.timeout = DEF_TIMEOUT;
+ 
+        if (ioctl(sg_fd, SG_IO, &io_hdr) < 0)
+                return -1;
+ 
+        /* treat SG_ERR here to get rid of sg_err.[ch] */
+        io_hdr.status &= 0x7e;
+        if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
+            (0 == io_hdr.driver_status))
+                return 0;
+        if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
+            (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
+            (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
+                if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
+                        int sense_key;
+                        unsigned char * sense_buffer = io_hdr.sbp;
+                        if (sense_buffer[0] & 0x2)
+                                sense_key = sense_buffer[1] & 0xf;
+                        else
+                                sense_key = sense_buffer[2] & 0xf;
+                        if(RECOVERED_ERROR == sense_key)
+                                return 0;
+                }
+        }
+        return -1;
+}
+
+int
+get_serial (char * str, int fd)
+{
+        int len;
+        char buff[MX_ALLOC_LEN + 1];
+
+	if (fd < 0)
+                return 0;
+
+	if (0 == do_inq(fd, 0, 1, 0x80, buff, MX_ALLOC_LEN, 0)) {
+		len = buff[3];
+		if (len > 0) {
+			memcpy(str, buff + 4, len);
+			buff[len] = '\0';
+		}
+		return 1;
+	}
+        return 0;
+}
+
+extern int
+sysfs_devinfo(struct path * curpath)
+{
+	char attr_path[FILE_NAME_SIZE];
+	char attr_buff[FILE_NAME_SIZE];
+
+	if (sysfs_get_vendor(sysfs_path, curpath->dev,
+			     curpath->vendor_id, SCSI_VENDOR_SIZE))
+		return 1;
+	condlog(3, "vendor = %s", curpath->vendor_id);
+
+	if (sysfs_get_model(sysfs_path, curpath->dev,
+			    curpath->product_id, SCSI_PRODUCT_SIZE))
+		return 1;
+	condlog(3, "product = %s", curpath->product_id);
+
+	if (sysfs_get_rev(sysfs_path, curpath->dev,
+			  curpath->rev, SCSI_REV_SIZE))
+		return 1;
+	condlog(3, "rev = %s", curpath->rev);
+
+	if (sysfs_get_dev(sysfs_path, curpath->dev,
+			  curpath->dev_t, BLK_DEV_SIZE))
+		return 1;
+	condlog(3, "dev_t = %s", curpath->dev_t);
+
+	curpath->size = sysfs_get_size(sysfs_path, curpath->dev);
+
+	if (curpath->size == 0)
+		return 1;
+	condlog(3, "size = %lu", curpath->size);
+
+	/*
+	 * host / bus / target / lun
+	 */
+	if(safe_sprintf(attr_path, "%s/block/%s/device",
+			sysfs_path, curpath->dev)) {
+		fprintf(stderr, "attr_path too small\n");
+		return 1;
+	}
+	if (0 > sysfs_get_link(attr_path, attr_buff, sizeof(attr_buff)))
+		return 1;
+	basename(attr_buff, attr_path);
+	sscanf(attr_path, "%i:%i:%i:%i",
+			&curpath->sg_id.host_no,
+			&curpath->sg_id.channel,
+			&curpath->sg_id.scsi_id,
+			&curpath->sg_id.lun);
+	condlog(3, "h:b:t:l = %i:%i:%i:%i",
+			curpath->sg_id.host_no,
+			curpath->sg_id.channel,
+			curpath->sg_id.scsi_id,
+			curpath->sg_id.lun);
+
+	/*
+	 * target node name
+	 */
+	if(safe_sprintf(attr_path,
+			"%s/class/fc_transport/target%i:%i:%i/node_name",
+			sysfs_path,
+			curpath->sg_id.host_no,
+			curpath->sg_id.channel,
+			curpath->sg_id.scsi_id)) {
+		fprintf(stderr, "attr_path too small\n");
+		return 1;
+	}
+	if (0 <= readattr(attr_path, attr_buff) && strlen(attr_buff) > 0)
+		strncpy(curpath->tgt_node_name, attr_buff,
+			strlen(attr_buff) - 1);
+	condlog(3, "tgt_node_name = %s", curpath->tgt_node_name);
+
+	return 0;
+}
+
+static int
+apply_format (char * string, char * cmd, struct path * pp)
+{
+	char * pos;
+	char * dst;
+	char * p;
+	int len;
+	int myfree;
+
+	if (!string)
+		return 1;
+
+	if (!cmd)
+		return 1;
+
+	dst = cmd;
+
+	if (!dst)
+		return 1;
+
+	p = dst;
+	pos = strchr(string, '%');
+	myfree = CALLOUT_MAX_SIZE;
+
+	if (!pos) {
+		strcpy(dst, string);
+		return 0;
+	}
+
+	len = (int) (pos - string) + 1;
+	myfree -= len;
+
+	if (myfree < 2)
+		return 1;
+
+	snprintf(p, len, "%s", string);
+	p += len - 1;
+	pos++;
+
+	switch (*pos) {
+	case 'n':
+		len = strlen(pp->dev) + 1;
+		myfree -= len;
+
+		if (myfree < 2)
+			return 1;
+
+		snprintf(p, len, "%s", pp->dev);
+		p += len - 1;
+		break;
+	case 'd':
+		len = strlen(pp->dev_t) + 1;
+		myfree -= len;
+
+		if (myfree < 2)
+			return 1;
+
+		snprintf(p, len, "%s", pp->dev_t);
+		p += len - 1;
+		break;
+	default:
+		break;
+	}
+	pos++;
+
+	if (!*pos)
+		return 0;
+
+	len = strlen(pos) + 1;
+	myfree -= len;
+
+	if (myfree < 2)
+		return 1;
+
+	snprintf(p, len, "%s", pos);
+	condlog(3, "reformated callout = %s", dst);
+	return 0;
+}
+
+extern int
+devinfo (struct path *pp, vector hwtable, int mask)
+{
+	char buff[CALLOUT_MAX_SIZE];
+	char prio[16];
+
+	condlog(3, "===== path %s =====", pp->dev);
+
+	/*
+	 * fetch info available in sysfs
+	 */
+	if (mask & DI_SYSFS && sysfs_devinfo(pp))
+		return 1;
+
+	/*
+	 * then those not available through sysfs
+	 */
+	if (pp->fd <= 0)
+		pp->fd = opennode(pp->dev, O_RDONLY);
+
+	if (pp->fd <= 0)
+		return 1;
+
+	if (mask & DI_SERIAL) {
+		get_serial(pp->serial, pp->fd);
+		condlog(3, "serial = %s", pp->serial);
+	}
+#if 0
+	if (mask & DI_CLAIMED) {
+		pp->claimed = get_claimed(pp->fd);
+		condlog(3, "claimed = %i", pp->claimed);
+	}
+#endif
+
+	/* get and store hwe pointer */
+	pp->hwe = find_hwe(hwtable, pp->vendor_id, pp->product_id);
+
+	/*
+	 * get path state, no message collection, no context
+	 */
+	select_checkfn(pp);
+
+	if (mask & DI_CHECKER) {
+		pp->state = pp->checkfn(pp->fd, NULL, NULL);
+		condlog(3, "state = %i", pp->state);
+	}
+	
+	/*
+	 * get path prio
+	 */
+	if (mask & DI_PRIO) {
+		select_getprio(pp);
+
+		if (apply_format(pp->getprio, &buff[0], pp)) {
+			condlog(0, "error formatting prio callout command");
+			pp->priority = -1;
+		} else if (execute_program(buff, prio, 16)) {
+			condlog(0, "error calling out %s", buff);
+			pp->priority = -1;
+		} else
+			pp->priority = atoi(prio);
+
+		condlog(3, "prio = %u", pp->priority);
+	}
+
+	/*
+	 * get path uid
+	 */
+	if (mask & DI_WWID && !strlen(pp->wwid)) {
+		select_getuid(pp);
+
+		if (apply_format(pp->getuid, &buff[0], pp)) {
+			condlog(0, "error formatting uid callout command");
+			memset(pp->wwid, 0, WWID_SIZE);
+		} else if (execute_program(buff, pp->wwid, WWID_SIZE)) {
+			condlog(0, "error calling out %s", buff);
+			memset(pp->wwid, 0, WWID_SIZE);
+		}
+		condlog(3, "uid = %s (callout)", pp->wwid);
+	}
+	else if (strlen(pp->wwid))
+		condlog(3, "uid = %s (cache)", pp->wwid);
+
+	return 0;
+}

Added: multipath-tools/trunk/libmultipath/discovery.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/discovery.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,55 @@
+#ifndef DISCOVERY_H
+#define DISCOVERY_H
+
+#define SYSFS_PATH_SIZE 255
+#define INQUIRY_CMDLEN  6
+#define INQUIRY_CMD     0x12
+#define SENSE_BUFF_LEN  32
+#define DEF_TIMEOUT     60000
+#define RECOVERED_ERROR 0x01
+#define MX_ALLOC_LEN    255
+#define TUR_CMD_LEN     6
+
+#ifndef BLKGETSIZE
+#define BLKGETSIZE      _IO(0x12,96)
+#endif
+
+/*
+ * exerpt from sg_err.h
+ */
+#define SCSI_CHECK_CONDITION    0x2
+#define SCSI_COMMAND_TERMINATED 0x22
+#define SG_ERR_DRIVER_SENSE     0x08
+
+int sysfs_get_vendor (char * sysfs_path, char * dev, char * buff, int len);
+int sysfs_get_model (char * sysfs_path, char * dev, char * buff, int len);
+int sysfs_get_rev (char * sysfs_path, char * dev, char * buff, int len);
+int sysfs_get_dev (char * sysfs_path, char * dev, char * buff, int len);
+
+unsigned long sysfs_get_size (char * sysfs_path, char * dev);
+int path_discovery (vector pathvec, struct config * conf, int flag);
+
+void basename (char *, char *);
+int get_serial (char * buff, int fd);
+int do_tur (char *);
+int devt2devname (char *, char *);
+int devinfo (struct path *, vector hwtable, int mask);
+
+#if 0
+int get_claimed(int fd);
+#endif
+
+/*
+ * discovery bitmask
+ */
+#define DI_SYSFS	1
+#define DI_SERIAL	2
+#define DI_CLAIMED	4
+#define DI_CHECKER	8
+#define DI_PRIO		16
+#define DI_WWID		32
+
+#define DI_ALL		(DI_SYSFS  | DI_SERIAL | DI_CLAIMED | DI_CHECKER | \
+			 DI_PRIO   | DI_WWID)
+
+#endif /* DISCOVERY_H */

Added: multipath-tools/trunk/libmultipath/dmparser.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/dmparser.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,443 @@
+/*
+ * Christophe Varoqui (2004)
+ * This code is GPLv2, see license file
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "vector.h"
+#include "memory.h"
+#include "structs.h"
+#include "util.h"
+#include "debug.h"
+
+#define WORD_SIZE 64
+
+static int
+get_word (char * sentence, char ** word)
+{
+	char * p;
+	int len;
+	int skip = 0;
+	
+	while (*sentence ==  ' ') {
+		sentence++;
+		skip++;
+	}
+	if (*sentence == '\0')
+		return 0;
+
+	p = sentence;
+
+	while (*p !=  ' ' && *p != '\0')
+		p++;
+
+	len = (int) (p - sentence);
+
+	if (!word)
+		return skip + len;
+
+	*word = MALLOC(len + 1);
+
+	if (!*word) {
+		condlog(0, "get_word : oom\n");
+		return 0;
+	}
+	strncpy(*word, sentence, len);
+	condlog(4, "*word = %s, len = %i", *word, len);
+
+	if (*p == '\0')
+		return 0;
+
+	return skip + len;
+}
+
+static int
+merge_words (char ** dst, char * word, int space)
+{
+	char * p;
+	int len;
+
+	len = strlen(*dst) + strlen(word) + space;
+	*dst = REALLOC(*dst, len + 1);
+
+	if (!*dst)
+		return 1;
+
+	p = *dst;
+
+	while (*p != '\0')
+		p++;
+
+	while (space) {
+		*p = ' ';
+		p++;
+		space--;
+	}
+	strncpy(p, word, strlen(word) + 1);
+
+	return 0;
+}
+
+extern int
+disassemble_map (vector pathvec, char * params, struct multipath * mpp)
+{
+	char * word;
+	char * p;
+	int i, j, k;
+	int num_features = 0;
+	int num_hwhandler = 0;
+	int num_pg = 0;
+	int num_pg_args = 0;
+	int num_paths = 0;
+	int num_paths_args = 0;
+	struct path * pp;
+	struct pathgroup * pgp;
+
+	p = params;
+
+	/*
+	 * features
+	 */
+	p += get_word(p, &mpp->features);
+
+	if (!mpp->features)
+		return 1;
+
+	num_features = atoi(mpp->features);
+
+	for (i = 0; i < num_features; i++) {
+		p += get_word(p, &word);
+
+		if (!word)
+			return 1;
+
+		if (merge_words(&mpp->features, word, 1)) {
+			FREE(word);
+			return 1;
+		}
+		FREE(word);
+	}
+
+	/*
+	 * hwhandler
+	 */
+	p += get_word(p, &mpp->hwhandler);
+
+	if (!mpp->hwhandler)
+		return 1;
+
+	num_hwhandler = atoi(mpp->hwhandler);
+
+	for (i = 0; i < num_hwhandler; i++) {
+		p += get_word(p, &word);
+
+		if (!word)
+			return 1;
+
+		if (merge_words(&mpp->hwhandler, word, 1)) {
+			FREE(word);
+			return 1;
+		}
+		FREE(word);
+	}
+
+	/*
+	 * nb of path groups
+	 */
+	p += get_word(p, &word);
+
+	if (!word)
+		return 1;
+
+	num_pg = atoi(word);
+	FREE(word);
+
+	if (num_pg > 0 && !mpp->pg)
+		mpp->pg = vector_alloc();
+	
+	if (!mpp->pg)
+		return 1;
+	/*
+	 * first pg to try
+	 */
+	p += get_word(p, &word);
+
+	if (!word)
+		goto out;
+
+	mpp->nextpg = atoi(word);
+	FREE(word);
+
+	for (i = 0; i < num_pg; i++) {
+		/*
+		 * selector
+		 */
+
+		if (!mpp->selector) {
+			p += get_word(p, &mpp->selector);
+
+			if (!mpp->selector)
+				goto out;
+
+			/*
+			 * selector args
+			 */
+			p += get_word(p, &word);
+
+			if (!word)
+				goto out;
+
+			num_pg_args = atoi(word);
+			
+			if (merge_words(&mpp->selector, word, 1)) {
+				FREE(word);
+				goto out1;
+			}
+			FREE(word);
+		} else {
+			p += get_word(p, NULL);
+			p += get_word(p, NULL);
+		}
+
+		for (j = 0; j < num_pg_args; j++)
+			p += get_word(p, NULL);
+
+		/*
+		 * paths
+		 */
+		pgp = alloc_pathgroup();
+		
+		if (!pgp)
+			goto out;
+
+		if (store_pathgroup(mpp->pg, pgp))
+			goto out;
+
+		p += get_word(p, &word);
+
+		if (!word)
+			goto out;
+
+		num_paths = atoi(word);
+		FREE(word);
+
+		p += get_word(p, &word);
+
+		if (!word)
+			goto out;
+
+		num_paths_args = atoi(word);
+		FREE(word);
+
+		for (j = 0; j < num_paths; j++) {
+			pp = NULL;
+			p += get_word(p, &word);
+
+			if (!word)
+				goto out;
+
+			if (pathvec)
+				pp = find_path_by_devt(pathvec, word);
+
+			if (!pp) {
+				pp = alloc_path();
+
+				if (!pp)
+					goto out1;
+
+				strncpy(pp->dev_t, word, BLK_DEV_SIZE);
+			}
+			FREE(word);
+
+			if (store_path(pgp->paths, pp))
+				goto out;
+
+			pgp->id ^= (long)pp;
+
+			if (!strlen(mpp->wwid))
+				strncpy(mpp->wwid, pp->wwid, WWID_SIZE);
+
+			for (k = 0; k < num_paths_args; k++)
+				p += get_word(p, NULL);
+		}
+	}
+	return 0;
+out1:
+	FREE(word);
+out:
+	free_pgvec(mpp->pg, KEEP_PATHS);
+	return 1;
+}
+
+extern int
+disassemble_status (char * params, struct multipath * mpp)
+{
+	char * word;
+	char * p;
+	int i, j;
+	int num_feature_args;
+	int num_hwhandler_args;
+	int num_pg;
+	int num_pg_args;
+	int num_paths;
+	struct path * pp;
+	struct pathgroup * pgp;
+
+	p = params;
+
+	/*
+	 * features
+	 */
+	p += get_word(p, &word);
+
+	if (!word)
+		return 1;
+
+	num_feature_args = atoi(word);
+	FREE(word);
+
+	for (i = 0; i < num_feature_args; i++) {
+		if (i == 1) {
+			p += get_word(p, &word);
+
+			if (!word)
+				return 1;
+
+			mpp->queuedio = atoi(word);
+			FREE(word);
+			continue;
+		}
+		/* unknown */
+		p += get_word(p, NULL);
+	}
+	/*
+	 * hwhandler
+	 */
+	p += get_word(p, &word);
+
+	if (!word)
+		return 1;
+
+	num_hwhandler_args = atoi(word);
+	FREE(word);
+
+	for (i = 0; i < num_hwhandler_args; i++)
+		p += get_word(p, NULL);
+
+	/*
+	 * nb of path groups
+	 */
+	p += get_word(p, &word);
+
+	if (!word)
+		return 1;
+
+	num_pg = atoi(word);
+	FREE(word);
+
+	/*
+	 * next pg to try
+	 */
+	p += get_word(p, NULL);
+
+	if (VECTOR_SIZE(mpp->pg) < num_pg)
+		return 1;
+
+	for (i = 0; i < num_pg; i++) {
+		pgp = VECTOR_SLOT(mpp->pg, i);
+		/*
+		 * PG status
+		 */
+		p += get_word(p, &word);
+
+		if (!word)
+			return 1;
+
+		switch (*word) {
+		case 'D':
+			pgp->status = PGSTATE_DISABLED;
+			break;
+		case 'A':
+			pgp->status = PGSTATE_ACTIVE;
+			break;
+		case 'E':
+			pgp->status = PGSTATE_ENABLED;
+			break;
+		default:
+			pgp->status = PGSTATE_RESERVED;
+			break;
+		}
+		FREE(word);
+
+		/*
+		 * undef ?
+		 */
+		p += get_word(p, NULL);
+
+		p += get_word(p, &word);
+
+		if (!word)
+			return 1;
+
+		num_paths = atoi(word);
+		FREE(word);
+
+		p += get_word(p, &word);
+
+		if (!word)
+			return 1;
+
+		num_pg_args = atoi(word);
+		FREE(word);
+
+		if (VECTOR_SIZE(pgp->paths) < num_paths)
+			return 1;
+
+		for (j = 0; j < num_paths; j++) {
+			pp = VECTOR_SLOT(pgp->paths, j);
+			/*
+			 * path
+			 */
+			p += get_word(p, NULL);
+
+			/*
+			 * path status
+			 */
+			p += get_word(p, &word);
+
+			if (!word)
+				return 1;
+
+			switch (*word) {
+			case 'F':
+				pp->dmstate = PSTATE_FAILED;
+				break;
+			case 'A':
+				pp->dmstate = PSTATE_ACTIVE;
+				break;
+			default:
+				break;
+			}
+			FREE(word);
+			/*
+			 * fail count
+			 */
+			p += get_word(p, &word);
+
+			if (!word)
+				return 1;
+
+			pp->failcount = atoi(word);
+			FREE(word);
+		}
+		/*
+		 * selector args
+		 */
+		for (j = 0; j < num_pg_args; j++)
+			p += get_word(p, NULL);
+	}
+	return 0;
+}

Added: multipath-tools/trunk/libmultipath/dmparser.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/dmparser.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,2 @@
+int disassemble_map (vector, char *, struct multipath *);
+int disassemble_status (char *, struct multipath *);

Modified: multipath-tools/trunk/libmultipath/hwtable.c
==============================================================================
--- multipath-tools/trunk/libmultipath/hwtable.c	(original)
+++ multipath-tools/trunk/libmultipath/hwtable.c	Sat Aug 13 15:23:02 2005
@@ -1,59 +1,47 @@
 #include <stdio.h>
 
-#include "memory.h"
 #include "vector.h"
-#include "structs.h"
 #include "defaults.h"
-#include "hwtable.h"
-
-#include "../libcheckers/checkers.h"
-#include "../multipath/pgpolicies.h"
-
-extern struct hwentry *
-find_hw (vector hwtable, char * vendor, char * product)
-{
-	int i;
-	struct hwentry * hwe;
-
-	vector_foreach_slot (hwtable, hwe, i)
-		if (hwe->vendor && hwe->product &&
-		    strcmp(hwe->vendor, vendor) == 0 &&
-		    (hwe->product[0] == '*' ||
-			strcmp(hwe->product, product) == 0))
-			return hwe;
-	return NULL;
-}
+#include "structs.h"
+#include "config.h"
+#include "pgpolicies.h"
 
-void
+extern int
 setup_default_hwtable (vector hw)
 {
-	struct hwentry * hwe;
-	ADDHWE(hw, "COMPAQ", "HSV110 (C)COMPAQ", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "COMPAQ", "MSA1000", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "COMPAQ", "MSA1000 VOLUME", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "DEC", "HSG80", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "HP", "HSV110", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "HP", "A6189A", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "HP", "OPEN-", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "DDN", "SAN DataDirector", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "EMC", "SYMMETRIX", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "FSC", "CentricStor", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "HITACHI", "DF400", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "HITACHI", "DF500", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "HITACHI", "DF600", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "IBM", "ProFibre 4000R", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "SGI", "TP9100", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "SGI", "TP9300", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "SGI", "TP9400", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "SGI", "TP9500", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "3PARdata", "VV", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "STK", "OPENstorage D280", GROUP_BY_SERIAL, DEFAULT_GETUID);
-	ADDHWE(hw, "SUN", "StorEdge 3510", MULTIBUS, DEFAULT_GETUID);
-	ADDHWE(hw, "SUN", "T4", MULTIBUS, DEFAULT_GETUID);
-
-	ADDHWE_EXT(hw, "DGC", "*", GROUP_BY_PRIO, DEFAULT_GETUID,
-		   DEFAULT_GETUID " -p 0xc0", "1 emc", "0", "emc_clariion");
-	ADDHWE_EXT(hw, "IBM", "3542", GROUP_BY_SERIAL, DEFAULT_GETUID,
+	int r = 0;
+
+	r += store_hwe(hw, "3PARdata", "VV", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "COMPAQ", "HSV110 (C)COMPAQ", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "COMPAQ", "MSA1000", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "COMPAQ", "MSA1000 VOLUME", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "DDN", "SAN DataDirector", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "DEC", "HSG80", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "EMC", "SYMMETRIX", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "FSC", "CentricStor", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "HITACHI", "DF400", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "HITACHI", "DF500", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "HITACHI", "DF600", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "HP", "HSV110", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "HP", "A6189A", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "HP", "OPEN-", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "IBM", "ProFibre 4000R", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "NETAPP", "LUN", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "SGI", "TP9100", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "SGI", "TP9300", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "STK", "OPENstorage D280", GROUP_BY_SERIAL, DEFAULT_GETUID);
+	r += store_hwe(hw, "SUN", "StorEdge 3510", MULTIBUS, DEFAULT_GETUID);
+	r += store_hwe(hw, "SUN", "T4", MULTIBUS, DEFAULT_GETUID);
+
+	r += store_hwe_ext(hw, "DGC", "*", GROUP_BY_PRIO, DEFAULT_GETUID,
+		   "/sbin/pp_emc /dev/%n", "1 emc", "0", "emc_clariion");
+	r += store_hwe_ext(hw, "IBM", "3542", GROUP_BY_SERIAL, DEFAULT_GETUID,
+		   NULL, "0", "0", "tur");
+	r += store_hwe_ext(hw, "SGI", "TP9400", MULTIBUS, DEFAULT_GETUID,
 		   NULL, "0", "0", "tur");
+	r += store_hwe_ext(hw, "SGI", "TP9500", FAILOVER, DEFAULT_GETUID,
+		   NULL, "0", "0", "tur");
+
+	return r;
 }
 

Modified: multipath-tools/trunk/libmultipath/hwtable.h
==============================================================================
--- multipath-tools/trunk/libmultipath/hwtable.h	(original)
+++ multipath-tools/trunk/libmultipath/hwtable.h	Sat Aug 13 15:23:02 2005
@@ -1,47 +1,6 @@
 #ifndef _HWTABLE_H
 #define _HWTABLE_H
 
-#define ADDHWE(a, b, c, d, e) \
-	hwe = zalloc (sizeof(struct hwentry)); \
-	hwe->vendor = zalloc (SCSI_VENDOR_SIZE * sizeof(char)); \
-	snprintf (hwe->vendor, SCSI_VENDOR_SIZE, "%-8s", b); \
-	hwe->product = zalloc (SCSI_PRODUCT_SIZE * sizeof(char)); \
-	snprintf (hwe->product, SCSI_PRODUCT_SIZE, "%-17s", c); \
-	hwe->pgpolicy = d; \
-	hwe->getuid = e; \
-	vector_alloc_slot(a); \
-	vector_set_slot(a, hwe);
-
-#define ADDHWE_EXT(a, b, c, d, e, f, g, i, j) \
-	hwe = zalloc (sizeof(struct hwentry)); \
-	hwe->vendor = zalloc (SCSI_VENDOR_SIZE * sizeof(char)); \
-	snprintf (hwe->vendor, SCSI_VENDOR_SIZE, "%-8s", b); \
-	hwe->product = zalloc (SCSI_PRODUCT_SIZE * sizeof(char)); \
-	snprintf (hwe->product, SCSI_PRODUCT_SIZE, "%-17s", c); \
-	hwe->pgpolicy = d; \
-	hwe->getuid = e; \
-	hwe->getprio = f; \
-	hwe->hwhandler = g; \
-	hwe->features = i; \
-	hwe->checker_index = get_checker_id(j); \
-	vector_alloc_slot(a); \
-	vector_set_slot(a, hwe);
-
-struct hwentry {
-        int selector_args;
-        int pgpolicy;
-        int checker_index;
-
-        char * vendor;
-        char * product;
-        char * selector;
-        char * getuid;
-        char * getprio;
-        char * features;
-        char * hwhandler;
-};
-
-void setup_default_hwtable (vector hw);
-struct hwentry * find_hw (vector hwtable, char * vendor, char * product);
+int setup_default_hwtable (vector hw);
 
 #endif /* _HWTABLE_H */

Modified: multipath-tools/trunk/libmultipath/memory.c
==============================================================================
--- multipath-tools/trunk/libmultipath/memory.c	(original)
+++ multipath-tools/trunk/libmultipath/memory.c	Sat Aug 13 15:23:02 2005
@@ -1,12 +1,432 @@
+/*
+ * Part:        Memory management framework. This framework is used to
+ *              find any memory leak.
+ *
+ * Version:     $Id: memory.c,v 1.1.11 2005/03/01 01:22:13 acassen Exp $
+ *
+ * Authors:     Alexandre Cassen, <acassen at linux-vs.org>
+ *              Jan Holmberg, <jan at artech.net>
+ *
+ *              This program is distributed in the hope that it will be useful,
+ *              but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *              See the GNU General Public License for more details.
+ *
+ *              This program is free software; you can redistribute it and/or
+ *              modify it under the terms of the GNU General Public License
+ *              as published by the Free Software Foundation; either version
+ *              2 of the License, or (at your option) any later version.
+ *
+ * Copyright (C) 2001-2005 Alexandre Cassen, <acassen at linux-vs.org>
+ */
+
 #include "memory.h"
 
+/* Global var */
+unsigned long mem_allocated;	/* Total memory used in Bytes */
+
 void *
-zalloc(unsigned long size)
+xalloc(unsigned long size)
 {
 	void *mem;
-
 	if ((mem = malloc(size)))
+		mem_allocated += size;
+	return mem;
+}
+
+void *
+zalloc(unsigned long size)
+{
+	void *mem;
+	if ((mem = malloc(size))) {
 		memset(mem, 0, size);
-	
+		mem_allocated += size;
+	}
 	return mem;
 }
+
+void
+xfree(void *p)
+{
+	mem_allocated -= sizeof (p);
+	free(p);
+	p = NULL;
+}
+
+/*
+ * Memory management. in debug mode,
+ * help finding eventual memory leak.
+ * Allocation memory types manipulated are :
+ *
+ * +type+--------meaning--------+
+ * ! 0  ! Free slot             !
+ * ! 1  ! Overrun               !
+ * ! 2  ! free null             !
+ * ! 3  ! realloc null          !
+ * ! 4  ! Not previus allocated !
+ * ! 8  ! Last free list        !
+ * ! 9  ! Allocated             !
+ * +----+-----------------------+
+ *
+ * global variabel debug bit 9 ( 512 ) used to
+ * flag some memory error.
+ *
+ */
+
+#ifdef _DEBUG_
+
+typedef struct {
+	int type;
+	int line;
+	char *func;
+	char *file;
+	void *ptr;
+	unsigned long size;
+	long csum;
+} MEMCHECK;
+
+/* Last free pointers */
+static MEMCHECK free_list[256];
+
+static MEMCHECK alloc_list[MAX_ALLOC_LIST];
+static int number_alloc_list = 0;
+static int n = 0;		/* Alloc list pointer */
+static int f = 0;		/* Free list pointer */
+
+char *
+dbg_malloc(unsigned long size, char *file, char *function, int line)
+{
+	void *buf;
+	int i = 0;
+	long check;
+
+	buf = zalloc(size + sizeof (long));
+
+	check = 0xa5a5 + size;
+	*(long *) ((char *) buf + size) = check;
+
+	while (i < number_alloc_list) {
+		if (alloc_list[i].type == 0)
+			break;
+		i++;
+	}
+
+	if (i == number_alloc_list)
+		number_alloc_list++;
+
+	assert(number_alloc_list < MAX_ALLOC_LIST);
+
+	alloc_list[i].ptr = buf;
+	alloc_list[i].size = size;
+	alloc_list[i].file = file;
+	alloc_list[i].func = function;
+	alloc_list[i].line = line;
+	alloc_list[i].csum = check;
+	alloc_list[i].type = 9;
+
+	if (debug & 1)
+		printf("zalloc[%3d:%3d], %p, %4ld at %s, %3d, %s\n",
+		       i, number_alloc_list, buf, size, file, line,
+		       function);
+
+	n++;
+	return buf;
+}
+
+
+
+/* Display a buffer into a HEXA formated output */
+static void
+dump_buffer(char *buff, int count)
+{
+	int i, j, c;
+	int printnext = 1;
+
+	if (count % 16)
+		c = count + (16 - count % 16);
+	else
+		c = count;
+
+	for (i = 0; i < c; i++) {
+		if (printnext) {
+			printnext--;
+			printf("%.4x ", i & 0xffff);
+		}
+		if (i < count)
+			printf("%3.2x", buff[i] & 0xff);
+		else
+			printf("   ");
+		if (!((i + 1) % 8)) {
+			if ((i + 1) % 16)
+				printf(" -");
+			else {
+				printf("   ");
+				for (j = i - 15; j <= i; j++)
+					if (j < count) {
+						if ((buff[j] & 0xff) >= 0x20
+						    && (buff[j] & 0xff) <= 0x7e)
+							printf("%c",
+							       buff[j] & 0xff);
+						else
+							printf(".");
+					} else
+						printf(" ");
+				printf("\n");
+				printnext = 1;
+			}
+		}
+	}
+}
+
+int
+dbg_free(void *buffer, char *file, char *function, int line)
+{
+	int i = 0;
+	void *buf;
+
+	/* If nullpointer remember */
+	if (buffer == NULL) {
+		i = number_alloc_list++;
+
+		assert(number_alloc_list < MAX_ALLOC_LIST);
+
+		alloc_list[i].ptr = buffer;
+		alloc_list[i].size = 0;
+		alloc_list[i].file = file;
+		alloc_list[i].func = function;
+		alloc_list[i].line = line;
+		alloc_list[i].type = 2;
+		if (debug & 1)
+			printf("free NULL in %s, %3d, %s\n", file,
+			       line, function);
+
+		debug |= 512;	/* Memory Error detect */
+
+		return n;
+	} else
+		buf = buffer;
+
+	while (i < number_alloc_list) {
+		if (alloc_list[i].type == 9 && alloc_list[i].ptr == buf) {
+			if (*
+			    ((long *) ((char *) alloc_list[i].ptr +
+				       alloc_list[i].size)) ==
+			    alloc_list[i].csum)
+				alloc_list[i].type = 0;	/* Release */
+			else {
+				alloc_list[i].type = 1;	/* Overrun */
+				if (debug & 1) {
+					printf("free corrupt, buffer overrun [%3d:%3d], %p, %4ld at %s, %3d, %s\n",
+					       i, number_alloc_list,
+					       buf, alloc_list[i].size, file,
+					       line, function);
+					dump_buffer(alloc_list[i].ptr,
+						    alloc_list[i].size + sizeof (long));
+					printf("Check_sum\n");
+					dump_buffer((char *) &alloc_list[i].csum,
+						    sizeof(long));
+
+					debug |= 512;	/* Memory Error detect */
+				}
+			}
+			break;
+		}
+		i++;
+	}
+
+	/*  Not found */
+	if (i == number_alloc_list) {
+		printf("Free ERROR %p\n", buffer);
+		number_alloc_list++;
+
+		assert(number_alloc_list < MAX_ALLOC_LIST);
+
+		alloc_list[i].ptr = buf;
+		alloc_list[i].size = 0;
+		alloc_list[i].file = file;
+		alloc_list[i].func = function;
+		alloc_list[i].line = line;
+		alloc_list[i].type = 4;
+		debug |= 512;
+
+		return n;
+	}
+
+	if (buffer != NULL)
+		xfree(buffer);
+
+	if (debug & 1)
+		printf("free  [%3d:%3d], %p, %4ld at %s, %3d, %s\n",
+		       i, number_alloc_list, buf,
+		       alloc_list[i].size, file, line, function);
+
+	free_list[f].file = file;
+	free_list[f].line = line;
+	free_list[f].func = function;
+	free_list[f].ptr = buffer;
+	free_list[f].type = 8;
+	free_list[f].csum = i;	/* Using this field for row id */
+
+	f++;
+	f &= 255;
+	n--;
+
+	return n;
+}
+
+void
+dbg_free_final(char *banner)
+{
+	unsigned int sum = 0, overrun = 0, badptr = 0;
+	int i, j;
+	i = 0;
+
+	printf("\n---[ Memory dump for (%s)]---\n\n", banner);
+
+	while (i < number_alloc_list) {
+		switch (alloc_list[i].type) {
+		case 3:
+			badptr++;
+			printf
+			    ("null pointer to realloc(nil,%ld)! at %s, %3d, %s\n",
+			     alloc_list[i].size, alloc_list[i].file,
+			     alloc_list[i].line, alloc_list[i].func);
+			break;
+		case 4:
+			badptr++;
+			printf
+			    ("pointer not found in table to free(%p) [%3d:%3d], at %s, %3d, %s\n",
+			     alloc_list[i].ptr, i, number_alloc_list,
+			     alloc_list[i].file, alloc_list[i].line,
+			     alloc_list[i].func);
+			for (j = 0; j < 256; j++)
+				if (free_list[j].ptr == alloc_list[i].ptr)
+					if (free_list[j].type == 8)
+						printf
+						    ("  -> pointer already released at [%3d:%3d], at %s, %3d, %s\n",
+						     (int) free_list[j].csum,
+						     number_alloc_list,
+						     free_list[j].file,
+						     free_list[j].line,
+						     free_list[j].func);
+			break;
+		case 2:
+			badptr++;
+			printf("null pointer to free(nil)! at %s, %3d, %s\n",
+			       alloc_list[i].file, alloc_list[i].line,
+			       alloc_list[i].func);
+			break;
+		case 1:
+			overrun++;
+			printf("%p [%3d:%3d], %4ld buffer overrun!:\n",
+			       alloc_list[i].ptr, i, number_alloc_list,
+			       alloc_list[i].size);
+			printf(" --> source of malloc: %s, %3d, %s\n",
+			       alloc_list[i].file, alloc_list[i].line,
+			       alloc_list[i].func);
+			break;
+		case 9:
+			sum += alloc_list[i].size;
+			printf("%p [%3d:%3d], %4ld not released!:\n",
+			       alloc_list[i].ptr, i, number_alloc_list,
+			       alloc_list[i].size);
+			printf(" --> source of malloc: %s, %3d, %s\n",
+			       alloc_list[i].file, alloc_list[i].line,
+			       alloc_list[i].func);
+			break;
+		}
+		i++;
+	}
+
+	printf("\n\n---[ Memory dump summary for (%s) ]---\n", banner);
+	printf("Total number of bytes not freed...: %d\n", sum);
+	printf("Number of entries not freed.......: %d\n", n);
+	printf("Maximum allocated entries.........: %d\n", number_alloc_list);
+	printf("Number of bad entries.............: %d\n", badptr);
+	printf("Number of buffer overrun..........: %d\n\n", overrun);
+
+	if (sum || n || badptr || overrun)
+		printf("=> Program seems to have some memory problem !!!\n\n");
+	else
+		printf("=> Program seems to be memory allocation safe...\n\n");
+}
+
+void *
+dbg_realloc(void *buffer, unsigned long size, char *file, char *function,
+		   int line)
+{
+	int i = 0;
+	void *buf, *buf2;
+	long check;
+
+	if (buffer == NULL) {
+		printf("realloc %p %s, %3d %s\n", buffer, file, line, function);
+		i = number_alloc_list++;
+
+		assert(number_alloc_list < MAX_ALLOC_LIST);
+
+		alloc_list[i].ptr = NULL;
+		alloc_list[i].size = 0;
+		alloc_list[i].file = file;
+		alloc_list[i].func = function;
+		alloc_list[i].line = line;
+		alloc_list[i].type = 3;
+		return dbg_malloc(size, file, function, line);
+	}
+
+	buf = buffer;
+
+	while (i < number_alloc_list) {
+		if (alloc_list[i].ptr == buf) {
+			buf = alloc_list[i].ptr;
+			break;
+		}
+		i++;
+	}
+
+	/* not found */
+	if (i == number_alloc_list) {
+		printf("realloc ERROR no matching zalloc %p \n", buffer);
+		number_alloc_list++;
+
+		assert(number_alloc_list < MAX_ALLOC_LIST);
+
+		alloc_list[i].ptr = buf;
+		alloc_list[i].size = 0;
+		alloc_list[i].file = file;
+		alloc_list[i].func = function;
+		alloc_list[i].line = line;
+		alloc_list[i].type = 9;
+		debug |= 512;	/* Memory Error detect */
+		return NULL;
+	}
+
+	buf2 = ((char *) buf) + alloc_list[i].size;
+
+	if (*(long *) (buf2) != alloc_list[i].csum) {
+		alloc_list[i].type = 1;
+		debug |= 512;	/* Memory Error detect */
+	}
+	buf = realloc(buffer, size + sizeof (long));
+
+	check = 0xa5a5 + size;
+	*(long *) ((char *) buf + size) = check;
+	alloc_list[i].csum = check;
+
+	if (debug & 1)
+		printf("realloc [%3d:%3d] %p, %4ld %s %d %s -> %p %4ld %s %d %s\n",
+		       i, number_alloc_list, alloc_list[i].ptr,
+		       alloc_list[i].size, file, line, function, buf, size,
+		       alloc_list[i].file, alloc_list[i].line,
+		       alloc_list[i].func);
+
+	alloc_list[i].ptr = buf;
+	alloc_list[i].size = size;
+	alloc_list[i].file = file;
+	alloc_list[i].line = line;
+	alloc_list[i].func = function;
+
+	return buf;
+}
+
+#endif

Modified: multipath-tools/trunk/libmultipath/memory.h
==============================================================================
--- multipath-tools/trunk/libmultipath/memory.h	(original)
+++ multipath-tools/trunk/libmultipath/memory.h	Sat Aug 13 15:23:02 2005
@@ -1,12 +1,72 @@
+/*
+ * Part:        memory.c include file.
+ *
+ * Version:     $Id: memory.h,v 1.1.11 2005/03/01 01:22:13 acassen Exp $
+ *
+ * Authors:     Alexandre Cassen, <acassen at linux-vs.org>
+ *              Jan Holmberg, <jan at artech.net>
+ *
+ *              This program is distributed in the hope that it will be useful,
+ *              but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *              See the GNU General Public License for more details.
+ *
+ *              This program is free software; you can redistribute it and/or
+ *              modify it under the terms of the GNU General Public License
+ *              as published by the Free Software Foundation; either version
+ *              2 of the License, or (at your option) any later version.
+ *
+ * Copyright (C) 2001-2005 Alexandre Cassen, <acassen at linux-vs.org>
+ */
+
 #ifndef _MEMORY_H
 #define _MEMORY_H
 
+/* system includes */
+#include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <assert.h>
+
+/* extern types */
+extern unsigned long mem_allocated;
+extern void *xalloc(unsigned long size);
+extern void *zalloc(unsigned long size);
+extern void xfree(void *p);
+
+/* Global alloc macro */
+#define ALLOC(n) (xalloc(n))
+
+/* Local defines */
+#ifdef _DEBUG_
+
+int debug;
+
+#define MAX_ALLOC_LIST 2048
+
+#define MALLOC(n)    ( dbg_malloc((n), \
+                      (__FILE__), (char *)(__FUNCTION__), (__LINE__)) )
+#define FREE(b)      ( dbg_free((b), \
+                      (__FILE__), (char *)(__FUNCTION__), (__LINE__)) )
+#define REALLOC(b,n) ( dbg_realloc((b), (n), \
+                      (__FILE__), (char *)(__FUNCTION__), (__LINE__)) )
+
+/* Memory debug prototypes defs */
+extern char *dbg_malloc(unsigned long, char *, char *, int);
+extern int dbg_free(void *, char *, char *, int);
+extern void *dbg_realloc(void *, unsigned long, char *, char *, int);
+extern void dbg_free_final(char *);
+
+#else
+
+#define MALLOC(n)    (zalloc(n))
+#define FREE(p)      (xfree(p))
+#define REALLOC(p,n) (realloc((p),(n)))
 
-#define MALLOC(a) zalloc(a)
-#define FREE(a) free(a)
+#endif
 
-extern void * zalloc (unsigned long size);
+/* Common defines */
+#define FREE_PTR(P) if((P)) FREE((P));
 
-#endif /* _MEMORY_H */
+#endif

Modified: multipath-tools/trunk/libmultipath/parser.c
==============================================================================
--- multipath-tools/trunk/libmultipath/parser.c	(original)
+++ multipath-tools/trunk/libmultipath/parser.c	Sat Aug 13 15:23:02 2005
@@ -25,24 +25,32 @@
 /* local vars */
 static int sublevel = 0;
 
-void
-keyword_alloc(vector keywords, char *string, void (*handler) (vector))
+int
+keyword_alloc(vector keywords, char *string, int (*handler) (vector))
 {
 	struct keyword *keyword;
 
-	vector_alloc_slot(keywords);
+	keyword = (struct keyword *) MALLOC(sizeof (struct keyword));
 
-	keyword = (struct keyword *) zalloc(sizeof (struct keyword));
+	if (!keyword)
+		return 1;
+	
+	if (!vector_alloc_slot(keywords)) {
+		FREE(keyword);
+		return 1;
+	}
 	keyword->string = string;
 	keyword->handler = handler;
 
 	vector_set_slot(keywords, keyword);
+
+	return 0;
 }
 
-void
-install_keyword_root(char *string, void (*handler) (vector))
+int
+install_keyword_root(char *string, int (*handler) (vector))
 {
-	keyword_alloc(keywords, string, handler);
+	return keyword_alloc(keywords, string, handler);
 }
 
 void
@@ -57,8 +65,8 @@
 	sublevel--;
 }
 
-void
-install_keyword(char *string, void (*handler) (vector))
+int
+install_keyword(char *string, int (*handler) (vector))
 {
 	int i = 0;
 	struct keyword *keyword;
@@ -75,8 +83,11 @@
 	if (!keyword->sub)
 		keyword->sub = vector_alloc();
 
+	if (!keyword->sub)
+		return 1;
+
 	/* add new sub keyword */
-	keyword_alloc(keyword->sub, string, handler);
+	return keyword_alloc(keyword->sub, string, handler);
 }
 
 void
@@ -89,7 +100,7 @@
 		keyword = VECTOR_SLOT(keywords, i);
 		if (keyword->sub)
 			free_keywords(keyword->sub);
-		free(keyword);
+		FREE(keyword);
 	}
 	vector_free(keywords);
 }
@@ -122,12 +133,22 @@
 	/* Create a vector and alloc each command piece */
 	strvec = vector_alloc();
 
+	if (!strvec)
+		return NULL;
+
 	in_string = 0;
 	while (1) {
+		if (!vector_alloc_slot(strvec))
+			goto out;
+
 		start = cp;
 		if (*cp == '"') {
 			cp++;
-			token = zalloc(2);
+			token = MALLOC(2);
+
+			if (!token)
+				goto out;
+
 			*(token) = '"';
 			*(token + 1) = '\0';
 			if (in_string)
@@ -140,13 +161,14 @@
 				!= '\0' && *cp != '"')
 				cp++;
 			strlen = cp - start;
-			token = zalloc(strlen + 1);
+			token = MALLOC(strlen + 1);
+
+			if (!token)
+				goto out;
+
 			memcpy(token, start, strlen);
 			*(token + strlen) = '\0';
 		}
-
-		/* Alloc & set the slot */
-		vector_alloc_slot(strvec);
 		vector_set_slot(strvec, token);
 
 		while (isspace((int) *cp) && *cp != '\0')
@@ -154,6 +176,9 @@
 		if (*cp == '\0' || *cp == '!' || *cp == '#')
 			return strvec;
 	}
+out:
+	vector_free(strvec);
+	return NULL;
 }
 
 int
@@ -183,7 +208,14 @@
 	vector vec = NULL;
 	vector elements = vector_alloc();
 
-	buf = (char *) zalloc(MAXBUF);
+	buf = (char *) MALLOC(MAXBUF);
+
+	if (!buf)
+		return NULL;
+
+	if (!elements)
+		goto out;
+
 	while (read_line(buf, MAXBUF)) {
 		vec = alloc_strvec(buf);
 		if (vec) {
@@ -196,28 +228,39 @@
 			if (VECTOR_SIZE(vec))
 				for (i = 0; i < VECTOR_SIZE(vec); i++) {
 					str = VECTOR_SLOT(vec, i);
-					dup = (char *) zalloc(strlen(str) + 1);
+					dup = (char *) MALLOC(strlen(str) + 1);
 					memcpy(dup, str, strlen(str));
-					vector_alloc_slot(elements);
+
+					if (!vector_alloc_slot(elements))
+						goto out1;
+
 					vector_set_slot(elements, dup);
 				}
 			free_strvec(vec);
 		}
 		memset(buf, 0, MAXBUF);
 	}
-
-	free(buf);
+	FREE(buf);
 	return elements;
+out1:
+	FREE(dup);
+out:
+	FREE(buf);
+	return NULL;
 }
 
-void
+int
 alloc_value_block(vector strvec, void (*alloc_func) (vector))
 {
 	char *buf;
 	char *str = NULL;
 	vector vec = NULL;
 
-	buf = (char *) zalloc(MAXBUF);
+	buf = (char *) MALLOC(MAXBUF);
+
+	if (!buf)
+		return 1;
+
 	while (read_line(buf, MAXBUF)) {
 		vec = alloc_strvec(buf);
 		if (vec) {
@@ -234,7 +277,8 @@
 		}
 		memset(buf, 0, MAXBUF);
 	}
-	free(buf);
+	FREE(buf);
+	return 0;
 }
 
 void *
@@ -253,11 +297,11 @@
 			len += strlen(str);
 			if (!alloc)
 				alloc =
-				    (char *) zalloc(sizeof (char *) *
+				    (char *) MALLOC(sizeof (char *) *
 						    (len + 1));
 			else {
 				alloc =
-				    realloc(alloc, sizeof (char *) * (len + 1));
+				    REALLOC(alloc, sizeof (char *) * (len + 1));
 				tmp = VECTOR_SLOT(strvec, i-1);
 				if (*str != '"' && *tmp != '"')
 					strncat(alloc, " ", 1);
@@ -267,7 +311,7 @@
 				strncat(alloc, str, strlen(str));
 		}
 	} else {
-		alloc = zalloc(sizeof (char *) * (size + 1));
+		alloc = MALLOC(sizeof (char *) * (size + 1));
 		memcpy(alloc, str, size);
 	}
 	return alloc;
@@ -275,23 +319,27 @@
 
 /* non-recursive configuration stream handler */
 static int kw_level = 0;
-void
+int
 process_stream(vector keywords)
 {
 	int i;
+	int r = 0;
 	struct keyword *keyword;
 	char *str;
 	char *buf;
 	vector strvec;
 
-	buf = zalloc(MAXBUF);
+	buf = MALLOC(MAXBUF);
+
+	if (!buf)
+		return 1;
+
 	while (read_line(buf, MAXBUF)) {
 		strvec = alloc_strvec(buf);
 		memset(buf,0, MAXBUF);
 
-		if (!strvec) {
+		if (!strvec)
 			continue;
-		}
 
 		str = VECTOR_SLOT(strvec, 0);
 
@@ -305,11 +353,11 @@
 
 			if (!strcmp(keyword->string, str)) {
 				if (keyword->handler)
-					(*keyword->handler) (strvec);
+					r += (*keyword->handler) (strvec);
 
 				if (keyword->sub) {
 					kw_level++;
-					process_stream(keyword->sub);
+					r += process_stream(keyword->sub);
 					kw_level--;
 				}
 				break;
@@ -319,19 +367,20 @@
 		free_strvec(strvec);
 	}
 
-	free(buf);
-	return;
-
+	FREE(buf);
+	return r;
 }
 
 /* Data initialization */
-void
+int
 init_data(char *conf_file, vector (*init_keywords) (void))
 {
+	int r;
+
 	stream = fopen(conf_file, "r");
 	if (!stream) {
 		syslog(LOG_WARNING, "Configuration file open problem");
-		return;
+		return 1;
 	}
 
 	/* Init Keywords structure */
@@ -343,7 +392,9 @@
 */
 
 	/* Stream handling */
-	process_stream(keywords);
+	r = process_stream(keywords);
 	fclose(stream);
 	free_keywords(keywords);
+
+	return r;
 }

Modified: multipath-tools/trunk/libmultipath/parser.h
==============================================================================
--- multipath-tools/trunk/libmultipath/parser.h	(original)
+++ multipath-tools/trunk/libmultipath/parser.h	Sat Aug 13 15:23:02 2005
@@ -41,7 +41,7 @@
 /* ketword definition */
 struct keyword {
 	char *string;
-	void (*handler) (vector);
+	int (*handler) (vector);
 	vector sub;
 };
 
@@ -55,19 +55,19 @@
 FILE *stream;
 
 /* Prototypes */
-extern void keyword_alloc(vector keywords, char *string, void (*handler) (vector));
-extern void install_keyword_root(char *string, void (*handler) (vector));
+extern int keyword_alloc(vector keywords, char *string, int (*handler) (vector));
+extern int install_keyword_root(char *string, int (*handler) (vector));
 extern void install_sublevel(void);
 extern void install_sublevel_end(void);
-extern void install_keyword(char *string, void (*handler) (vector));
+extern int install_keyword(char *string, int (*handler) (vector));
 extern void dump_keywords(vector keydump, int level);
 extern void free_keywords(vector keywords);
 extern vector alloc_strvec(char *string);
 extern int read_line(char *buf, int size);
 extern vector read_value_block(void);
-extern void alloc_value_block(vector strvec, void (*alloc_func) (vector));
+extern int alloc_value_block(vector strvec, void (*alloc_func) (vector));
 extern void *set_value(vector strvec);
-extern void process_stream(vector keywords);
-extern void init_data(char *conf_file, vector (*init_keywords) (void));
+extern int process_stream(vector keywords);
+extern int init_data(char *conf_file, vector (*init_keywords) (void));
 
 #endif

Added: multipath-tools/trunk/libmultipath/pgpolicies.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/pgpolicies.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,346 @@
+/*
+ * Here we define the path grouping policies
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "util.h"
+#include "memory.h"
+#include "vector.h"
+#include "structs.h"
+#include "pgpolicies.h"
+
+#include "../libcheckers/path_state.h"
+
+extern int
+get_pgpolicy_id (char * str)
+{
+	if (0 == strncmp(str, "failover", 8))
+		return FAILOVER;
+	if (0 == strncmp(str, "multibus", 8))
+		return MULTIBUS;
+	if (0 == strncmp(str, "group_by_serial", 15))
+		return GROUP_BY_SERIAL;
+	if (0 == strncmp(str, "group_by_prio", 13))
+		return GROUP_BY_PRIO;
+	if (0 == strncmp(str, "group_by_node_name", 18))
+		return GROUP_BY_NODE_NAME;
+
+	return -1;
+}
+
+extern void
+get_pgpolicy_name (char * buff, int id)
+{
+	char * s;
+
+	switch (id) {
+	case FAILOVER:
+		s = "failover";
+		break;
+	case MULTIBUS:
+		s = "multibus";
+		break;
+	case GROUP_BY_SERIAL:
+		s = "group_by_serial";
+		break;
+	case GROUP_BY_PRIO:
+		s = "group_by_prio";
+		break;
+	case GROUP_BY_NODE_NAME:
+		s = "group_by_node_name";
+		break;
+	default:
+		s = "undefined";
+		break;
+	}
+	if(safe_snprintf(buff, POLICY_NAME_SIZE, "%s", s)) {
+		fprintf(stderr, "get_pgpolicy_name: buff too small\n");
+		exit(1);
+	}
+}
+
+/*
+ * One path group per unique tgt_node_name present in the path vector
+ */
+extern int
+group_by_node_name (struct multipath * mp) {
+	int i, j;
+	int * bitmap;
+	struct path * pp;
+	struct pathgroup * pgp;
+	struct path * pp2;
+	
+	if (!mp->pg)
+		mp->pg = vector_alloc();
+
+	if (!mp->pg)
+		return 1;
+
+	/* init the bitmap */
+	bitmap = (int *)MALLOC(VECTOR_SIZE(mp->paths) * sizeof (int));
+
+	if (!bitmap)
+		goto out;
+
+	for (i = 0; i < VECTOR_SIZE(mp->paths); i++) {
+
+		if (bitmap[i])
+			continue;
+
+		pp = VECTOR_SLOT(mp->paths, i);
+
+		/* here, we really got a new pg */
+		pgp = alloc_pathgroup();
+
+		if (!pgp)
+			goto out1;
+
+		if (store_pathgroup(mp->pg, pgp))
+			goto out1;
+
+		/* feed the first path */
+		if (store_path(pgp->paths, pp))
+			goto out1;
+
+		bitmap[i] = 1;
+
+		for (j = i + 1; j < VECTOR_SIZE(mp->paths); j++) {
+			
+			if (bitmap[j])
+				continue;
+
+			pp2 = VECTOR_SLOT(mp->paths, j);
+			
+			if (!strncmp(pp->tgt_node_name, pp2->tgt_node_name,
+					NODE_NAME_SIZE)) {
+				if (store_path(pgp->paths, pp2))
+					goto out1;
+
+				bitmap[j] = 1;
+			}
+		}
+	}
+	FREE(bitmap);
+	free_pathvec(mp->paths, KEEP_PATHS);
+	mp->paths = NULL;
+	return 0;
+out1:
+	FREE(bitmap);
+out:
+	free_pgvec(mp->pg, KEEP_PATHS);
+	return 1;
+}
+
+/*
+ * One path group per unique serial number present in the path vector
+ */
+extern int
+group_by_serial (struct multipath * mp) {
+	int i, j;
+	int * bitmap;
+	struct path * pp;
+	struct pathgroup * pgp;
+	struct path * pp2;
+	
+	if (!mp->pg)
+		mp->pg = vector_alloc();
+
+	if (!mp->pg)
+		return 1;
+
+	/* init the bitmap */
+	bitmap = (int *)MALLOC(VECTOR_SIZE(mp->paths) * sizeof (int));
+
+	if (!bitmap)
+		goto out;
+
+	for (i = 0; i < VECTOR_SIZE(mp->paths); i++) {
+
+		if (bitmap[i])
+			continue;
+
+		pp = VECTOR_SLOT(mp->paths, i);
+
+		/* here, we really got a new pg */
+		pgp = alloc_pathgroup();
+
+		if (!pgp)
+			goto out1;
+
+		if (store_pathgroup(mp->pg, pgp))
+			goto out1;
+
+		/* feed the first path */
+		if (store_path(pgp->paths, pp))
+			goto out1;
+				
+		bitmap[i] = 1;
+
+		for (j = i + 1; j < VECTOR_SIZE(mp->paths); j++) {
+			
+			if (bitmap[j])
+				continue;
+
+			pp2 = VECTOR_SLOT(mp->paths, j);
+			
+			if (0 == strcmp(pp->serial, pp2->serial)) {
+				if (store_path(pgp->paths, pp2))
+					goto out1;
+
+				bitmap[j] = 1;
+			}
+		}
+	}
+	FREE(bitmap);
+	free_pathvec(mp->paths, KEEP_PATHS);
+	mp->paths = NULL;
+	return 0;
+out1:
+	FREE(bitmap);
+out:
+	free_pgvec(mp->pg, KEEP_PATHS);
+	return 1;
+}
+
+extern int
+one_path_per_group (struct multipath * mp)
+{
+	int i;
+	struct path * pp;
+	struct pathgroup * pgp;
+
+	if (!mp->pg)
+		mp->pg = vector_alloc();
+	
+	if (!mp->pg)
+		return 1;
+
+	for (i = 0; i < VECTOR_SIZE(mp->paths); i++) {
+		pp = VECTOR_SLOT(mp->paths, i);
+		pgp = alloc_pathgroup();
+
+		if (!pgp)
+			goto out;
+
+		if (store_pathgroup(mp->pg, pgp))
+			goto out;
+		
+		if (store_path(pgp->paths, pp))
+			goto out;
+	}
+	free_pathvec(mp->paths, KEEP_PATHS);
+	mp->paths = NULL;
+	return 0;
+out:
+	free_pgvec(mp->pg, KEEP_PATHS);
+	return 1;
+}
+
+extern int
+one_group (struct multipath * mp)	/* aka multibus */
+{
+	struct pathgroup * pgp;
+
+	if (VECTOR_SIZE(pgp->paths) < 0)
+		return 0;
+
+	if (!mp->pg)
+		mp->pg = vector_alloc();
+
+	if (!mp->pg)
+		return 1;
+
+	pgp = alloc_pathgroup();
+	
+	if (!pgp)
+		goto out;
+	
+	vector_free(pgp->paths);
+	pgp->paths = mp->paths;
+	mp->paths = NULL;
+
+	if (store_pathgroup(mp->pg, pgp))
+		goto out;
+
+	return 0;
+out:
+	free_pgvec(mp->pg, KEEP_PATHS);
+	return 1;
+}
+
+extern int
+group_by_prio (struct multipath * mp)
+{
+	int i;
+	unsigned int prio;
+	struct path * pp;
+	struct pathgroup * pgp;
+
+	if (!mp->pg)
+		mp->pg = vector_alloc();
+
+	if (!mp->pg)
+		return 1;
+
+	while (VECTOR_SIZE(mp->paths) > 0) {
+		pp = VECTOR_SLOT(mp->paths, 0);
+		prio = pp->priority;
+
+		/*
+		 * Find the position to insert the new path group. All groups
+		 * are ordered by the priority value (higher value first).
+		 */
+		vector_foreach_slot(mp->pg, pgp, i) {
+			pp  = VECTOR_SLOT(pgp->paths, 0);
+
+			if (prio > pp->priority)
+				break;
+		}
+
+		/*
+		 * Initialize the new path group.
+		 */
+		pgp = alloc_pathgroup();
+
+		if (!pgp)
+			goto out;
+		
+		if (store_path(pgp->paths, VECTOR_SLOT(mp->paths, 0)))
+				goto out;
+
+		vector_del_slot(mp->paths, 0);
+
+		/*
+		 * Store the new path group into the vector.
+		 */
+		if (i < VECTOR_SIZE(mp->pg)) {
+			if (!vector_insert_slot(mp->pg, i, pgp))
+				goto out;
+		} else {
+			if (store_pathgroup(mp->pg, pgp))
+				goto out;
+		}
+
+		/*
+		 * add the other paths with the same prio
+		 */
+		vector_foreach_slot(mp->paths, pp, i) {
+			if (pp->priority == prio) {
+				if (store_path(pgp->paths, pp))
+					goto out;
+
+				vector_del_slot(mp->paths, i);
+				i--;
+			}
+		}
+	}
+	free_pathvec(mp->paths, KEEP_PATHS);
+	mp->paths = NULL;
+	return 0;
+out:
+	free_pgvec(mp->pg, KEEP_PATHS);
+	return 1;
+
+}

Added: multipath-tools/trunk/libmultipath/pgpolicies.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/pgpolicies.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,34 @@
+#ifndef _PGPOLICIES_H
+#define _PGPOLICIES_H
+
+#if 0
+#ifndef _MAIN_H
+#include "main.h"
+#endif
+#endif
+
+#define POLICY_NAME_SIZE 32
+
+/* Storage controlers capabilities */
+enum iopolicies { 
+	IOPOLICY_RESERVED,
+	FAILOVER,
+	MULTIBUS,
+	GROUP_BY_SERIAL,
+	GROUP_BY_PRIO,
+	GROUP_BY_NODE_NAME
+};
+
+int get_pgpolicy_id(char *);
+void get_pgpolicy_name (char *, int);
+
+/*
+ * policies
+ */
+int one_path_per_group(struct multipath *);
+int one_group(struct multipath *);
+int group_by_serial(struct multipath *);
+int group_by_prio(struct multipath *);
+int group_by_node_name(struct multipath *);
+
+#endif

Added: multipath-tools/trunk/libmultipath/propsel.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/propsel.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,151 @@
+#include <stdio.h>
+
+#include "vector.h"
+#include "structs.h"
+#include "config.h"
+#include "debug.h"
+#include "pgpolicies.h"
+
+#include "../libcheckers/checkers.h"
+
+/*
+ * selectors :
+ * traverse the configuration layers from most specific to most generic
+ * stop at first explicit setting found
+ */
+extern int
+select_pgpolicy (struct multipath * mp)
+{
+	struct path * pp;
+	char pgpolicy_name[POLICY_NAME_SIZE];
+
+	pp = VECTOR_SLOT(mp->paths, 0);
+
+	if (conf->pgpolicy_flag > 0) {
+		mp->pgpolicy = conf->pgpolicy_flag;
+		get_pgpolicy_name(pgpolicy_name, mp->pgpolicy);
+		condlog(3, "pgpolicy = %s (cmd line flag)", pgpolicy_name);
+		return 0;
+	}
+	if (mp->mpe && mp->mpe->pgpolicy > 0) {
+		mp->pgpolicy = mp->mpe->pgpolicy;
+		get_pgpolicy_name(pgpolicy_name, mp->pgpolicy);
+		condlog(3, "pgpolicy = %s (LUN setting)", pgpolicy_name);
+		return 0;
+	}
+	if (mp->hwe && mp->hwe->pgpolicy > 0) {
+		mp->pgpolicy = mp->hwe->pgpolicy;
+		get_pgpolicy_name(pgpolicy_name, mp->pgpolicy);
+		condlog(3, "pgpolicy = %s (controler setting)", pgpolicy_name);
+		return 0;
+	}
+	if (conf->default_pgpolicy > 0) {
+		mp->pgpolicy = conf->default_pgpolicy;
+		get_pgpolicy_name(pgpolicy_name, mp->pgpolicy);
+		condlog(3, "pgpolicy = %s (config file default)", pgpolicy_name);
+		return 0;
+	}
+	mp->pgpolicy = FAILOVER;
+	get_pgpolicy_name(pgpolicy_name, FAILOVER);
+	condlog(3, "pgpolicy = %s (internal default)", pgpolicy_name);
+	return 0;
+}
+
+extern int
+select_selector (struct multipath * mp)
+{
+	if (mp->mpe && mp->mpe->selector) {
+		mp->selector = mp->mpe->selector;
+		condlog(3, "selector = %s (LUN setting)", mp->selector);
+		return 0;
+	}
+	if (mp->hwe && mp->hwe->selector) {
+		mp->selector = mp->hwe->selector;
+		condlog(3, "selector = %s (controler setting)", mp->selector);
+		return 0;
+	}
+	mp->selector = conf->default_selector;
+	condlog(3, "selector = %s (internal default)", mp->selector);
+	return 0;
+}
+
+extern int
+select_alias (struct multipath * mp)
+{
+	if (mp->mpe && mp->mpe->alias)
+		mp->alias = mp->mpe->alias;
+	else
+		mp->alias = mp->wwid;
+
+	return 0;
+}
+
+extern int
+select_features (struct multipath * mp)
+{
+	if (mp->hwe && mp->hwe->features) {
+		mp->features = mp->hwe->features;
+		condlog(3, "features = %s (controler setting)", mp->features);
+		return 0;
+	}
+	mp->features = conf->default_features;
+	condlog(3, "features = %s (internal default)", mp->features);
+	return 0;
+}
+
+extern int
+select_hwhandler (struct multipath * mp)
+{
+	if (mp->hwe && mp->hwe->hwhandler) {
+		mp->hwhandler = mp->hwe->hwhandler;
+		condlog(3, "hwhandler = %s (controler setting)", mp->hwhandler);
+		return 0;
+	}
+	mp->hwhandler = conf->default_hwhandler;
+	condlog(3, "hwhandler = %s (internal default)", mp->hwhandler);
+	return 0;
+}
+
+extern int
+select_checkfn(struct path *pp)
+{
+	char checker_name[CHECKER_NAME_SIZE];
+
+	if (pp->hwe && pp->hwe->checker_index > 0) {
+		get_checker_name(checker_name, pp->hwe->checker_index);
+		condlog(3, "path checker = %s (controler setting)", checker_name);
+		pp->checkfn = get_checker_addr(pp->hwe->checker_index);
+		return 0;
+	}
+	pp->checkfn = &readsector0;
+	get_checker_name(checker_name, READSECTOR0);
+	condlog(3, "path checker = %s (internal default)", checker_name);
+	return 0;
+}
+
+extern int
+select_getuid (struct path * pp)
+{
+	if (pp->hwe && pp->hwe->getuid) {
+		pp->getuid = pp->hwe->getuid;
+		condlog(3, "getuid = %s (controler setting)", pp->getuid);
+		return 0;
+	}
+	pp->getuid = conf->default_getuid;
+	condlog(3, "getuid = %s (internal default)", pp->getuid);
+	return 0;
+}
+
+extern int
+select_getprio (struct path * pp)
+{
+	if (pp->hwe && pp->hwe->getprio) {
+		pp->getprio = pp->hwe->getprio;
+		condlog(3, "getprio = %s (controler setting)", pp->getprio);
+		return 0;
+	}
+	pp->getprio = conf->default_getprio;
+	condlog(3, "getprio = %s (internal default)", pp->getprio);
+	return 0;
+}
+

Added: multipath-tools/trunk/libmultipath/propsel.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/propsel.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,9 @@
+int select_pgpolicy (struct multipath * mp);
+int select_selector (struct multipath * mp);
+int select_alias (struct multipath * mp);
+int select_features (struct multipath * mp);
+int select_hwhandler (struct multipath * mp);
+int select_checkfn(struct path *pp);
+int select_getuid (struct path * pp);
+int select_getprio (struct path * pp);
+

Added: multipath-tools/trunk/libmultipath/regex.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/regex.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,4030 @@
+/* Extended regular expression matching and search library,
+   version 0.12.
+   (Implements POSIX draft P10003.2/D11.2, except for
+   internationalization features.)
+
+   Copyright (C) 1993 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#define _GNU_SOURCE
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef bcmp
+#define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
+#endif
+#ifndef bcopy
+#define bcopy(s, d, n)  memcpy ((d), (s), (n))
+#endif
+#ifndef bzero
+#define bzero(s, n)     memset ((s), 0, (n))
+#endif
+
+/* Define the syntax stuff for \<, \>, etc.  */
+
+#ifndef Sword
+#define Sword 1
+#endif
+
+#define CHAR_SET_SIZE 256
+
+static char re_syntax_table[CHAR_SET_SIZE];
+
+static void init_syntax_once(void)
+{
+	register int c;
+	static int done = 0;
+
+	if (done)
+		return;
+
+	bzero(re_syntax_table, sizeof re_syntax_table);
+
+	for (c = 'a'; c <= 'z'; c++)
+		re_syntax_table[c] = Sword;
+
+	for (c = 'A'; c <= 'Z'; c++)
+		re_syntax_table[c] = Sword;
+
+	for (c = '0'; c <= '9'; c++)
+		re_syntax_table[c] = Sword;
+
+	re_syntax_table['_'] = Sword;
+
+	done = 1;
+}
+
+#define SYNTAX(c) re_syntax_table[c]
+
+#include "regex.h"
+#include <ctype.h>
+
+#ifdef isblank
+#define ISBLANK(c) (isascii (c) && isblank (c))
+#else
+#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
+#endif
+#ifdef isgraph
+#define ISGRAPH(c) (isascii (c) && isgraph (c))
+#else
+#define ISGRAPH(c) (isascii (c) && isprint (c) && !isspace (c))
+#endif
+
+#define ISPRINT(c) (isascii (c) && isprint (c))
+#define ISDIGIT(c) (isascii (c) && isdigit (c))
+#define ISALNUM(c) (isascii (c) && isalnum (c))
+#define ISALPHA(c) (isascii (c) && isalpha (c))
+#define ISCNTRL(c) (isascii (c) && iscntrl (c))
+#define ISLOWER(c) (isascii (c) && islower (c))
+#define ISPUNCT(c) (isascii (c) && ispunct (c))
+#define ISSPACE(c) (isascii (c) && isspace (c))
+#define ISUPPER(c) (isascii (c) && isupper (c))
+#define ISXDIGIT(c) (isascii (c) && isxdigit (c))
+
+#undef SIGN_EXTEND_CHAR
+#define SIGN_EXTEND_CHAR(c) ((signed char) (c))
+
+#ifndef alloca
+#ifdef __GNUC__
+#define alloca __builtin_alloca
+#endif				/* not __GNUC__ */
+#endif				/* not alloca */
+
+#define REGEX_ALLOCATE alloca
+
+/* Assumes a `char *destination' variable.  */
+#define REGEX_REALLOCATE(source, osize, nsize)				\
+	(destination = (char *) alloca (nsize),				\
+	 bcopy (source, destination, osize),				\
+	 destination)
+
+/* True if `size1' is non-NULL and PTR is pointing anywhere inside
+   `string1' or just past its end.  This works if PTR is NULL, which is
+   a good thing.  */
+#define FIRST_STRING_P(ptr) 						\
+	(size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
+
+/* (Re)Allocate N items of type T using malloc, or fail.  */
+#define TALLOC(n, t)	     ((t *) malloc ((n) * sizeof (t)))
+#define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
+#define REGEX_TALLOC(n, t)   ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
+
+#define BYTEWIDTH 8		/* In bits.  */
+
+#define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
+
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
+typedef char boolean;
+#define false 0
+#define true 1
+
+typedef enum {
+	no_op = 0,
+	exactn = 1,
+	anychar,
+	charset,
+	charset_not,
+	start_memory,
+	stop_memory,
+	duplicate,
+	begline,
+	endline,
+	begbuf,
+	endbuf,
+	jump,
+	jump_past_alt,
+	on_failure_jump,
+	on_failure_keep_string_jump,
+	pop_failure_jump,
+	maybe_pop_jump,
+	dummy_failure_jump,
+	push_dummy_failure,
+	succeed_n,
+	jump_n,
+	set_number_at,
+	wordchar,
+	notwordchar,
+	wordbeg,
+	wordend,
+	wordbound,
+	notwordbound
+} re_opcode_t;
+
+#define STORE_NUMBER(destination, number)				\
+  do {									\
+    (destination)[0] = (number) & 0377;					\
+    (destination)[1] = (number) >> 8;					\
+  } while (0)
+
+#define STORE_NUMBER_AND_INCR(destination, number)			\
+  do {									\
+    STORE_NUMBER (destination, number);					\
+    (destination) += 2;							\
+  } while (0)
+
+#define EXTRACT_NUMBER(destination, source)				\
+  do {									\
+    (destination) = *(source) & 0377;					\
+    (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8;		\
+  } while (0)
+
+#define EXTRACT_NUMBER_AND_INCR(destination, source)			\
+  do {									\
+    EXTRACT_NUMBER (destination, source);				\
+    (source) += 2; 							\
+  } while (0)
+
+#undef assert
+#define assert(e)
+
+#define DEBUG_STATEMENT(e)
+#define DEBUG_PRINT1(x)
+#define DEBUG_PRINT2(x1, x2)
+#define DEBUG_PRINT3(x1, x2, x3)
+#define DEBUG_PRINT4(x1, x2, x3, x4)
+#define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
+#define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
+
+reg_syntax_t re_syntax_options = RE_SYNTAX_EMACS;
+reg_syntax_t re_set_syntax(syntax)
+reg_syntax_t syntax;
+{
+	reg_syntax_t ret = re_syntax_options;
+
+	re_syntax_options = syntax;
+	return ret;
+}
+
+/* This table gives an error message for each of the error codes listed
+   in regex.h.  Obviously the order here has to be same as there.  */
+
+static const char *re_error_msg[] = { NULL,	/* REG_NOERROR */
+	"No match",		/* REG_NOMATCH */
+	"Invalid regular expression",	/* REG_BADPAT */
+	"Invalid collation character",	/* REG_ECOLLATE */
+	"Invalid character class name",	/* REG_ECTYPE */
+	"Trailing backslash",	/* REG_EESCAPE */
+	"Invalid back reference",	/* REG_ESUBREG */
+	"Unmatched [ or [^",	/* REG_EBRACK */
+	"Unmatched ( or \\(",	/* REG_EPAREN */
+	"Unmatched \\{",	/* REG_EBRACE */
+	"Invalid content of \\{\\}",	/* REG_BADBR */
+	"Invalid range end",	/* REG_ERANGE */
+	"Memory exhausted",	/* REG_ESPACE */
+	"Invalid preceding regular expression",	/* REG_BADRPT */
+	"Premature end of regular expression",	/* REG_EEND */
+	"Regular expression too big",	/* REG_ESIZE */
+	"Unmatched ) or \\)",	/* REG_ERPAREN */
+};
+
+/* Subroutine declarations and macros for regex_compile.  */
+
+static reg_errcode_t regex_compile (const char *pattern, size_t size,
+				    reg_syntax_t syntax,
+				    struct re_pattern_buffer * bufp);
+
+static void store_op1 (re_opcode_t op, unsigned char *loc, int arg);
+
+static void store_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2);
+
+static void insert_op1 (re_opcode_t op, unsigned char *loc, int arg,
+			unsigned char *end);
+
+static void insert_op2 (re_opcode_t op, unsigned char *loc, int arg1, int arg2,
+			unsigned char *end);
+
+static boolean at_begline_loc_p (const char *pattern, const char *p,
+				 reg_syntax_t syntax);
+
+static boolean at_endline_loc_p (const char *p, const char *pend,
+				 reg_syntax_t syntax);
+
+static reg_errcode_t compile_range (const char **p_ptr, const char *pend,
+				    char *translate, reg_syntax_t syntax,
+				    unsigned char *b);
+
+/* Fetch the next character in the uncompiled pattern---translating it
+   if necessary.  Also cast from a signed character in the constant
+   string passed to us by the user to an unsigned char that we can use
+   as an array index (in, e.g., `translate').  */
+#define PATFETCH(c)							\
+  do {if (p == pend) return REG_EEND;					\
+    c = (unsigned char) *p++;						\
+    if (translate) c = translate[c]; 					\
+  } while (0)
+
+/* Fetch the next character in the uncompiled pattern, with no
+   translation.  */
+#define PATFETCH_RAW(c)							\
+  do {if (p == pend) return REG_EEND;					\
+    c = (unsigned char) *p++; 						\
+  } while (0)
+
+/* Go backwards one character in the pattern.  */
+#define PATUNFETCH p--
+
+
+/* If `translate' is non-null, return translate[D], else just D.  We
+   cast the subscript to translate because some data is declared as
+   `char *', to avoid warnings when a string constant is passed.  But
+   when we use a character as a subscript we must make it unsigned.  */
+#define TRANSLATE(d) (translate ? translate[(unsigned char) (d)] : (d))
+
+
+/* Macros for outputting the compiled pattern into `buffer'.  */
+
+/* If the buffer isn't allocated when it comes in, use this.  */
+#define INIT_BUF_SIZE  32
+
+/* Make sure we have at least N more bytes of space in buffer.  */
+#define GET_BUFFER_SPACE(n)						\
+    while (b - bufp->buffer + (n) > bufp->allocated)			\
+      EXTEND_BUFFER ()
+
+/* Make sure we have one more byte of buffer space and then add C to it.  */
+#define BUF_PUSH(c)							\
+  do {									\
+    GET_BUFFER_SPACE (1);						\
+    *b++ = (unsigned char) (c);						\
+  } while (0)
+
+
+/* Ensure we have two more bytes of buffer space and then append C1 and C2.  */
+#define BUF_PUSH_2(c1, c2)						\
+  do {									\
+    GET_BUFFER_SPACE (2);						\
+    *b++ = (unsigned char) (c1);					\
+    *b++ = (unsigned char) (c2);					\
+  } while (0)
+
+
+/* As with BUF_PUSH_2, except for three bytes.  */
+#define BUF_PUSH_3(c1, c2, c3)						\
+  do {									\
+    GET_BUFFER_SPACE (3);						\
+    *b++ = (unsigned char) (c1);					\
+    *b++ = (unsigned char) (c2);					\
+    *b++ = (unsigned char) (c3);					\
+  } while (0)
+
+
+/* Store a jump with opcode OP at LOC to location TO.  We store a
+   relative address offset by the three bytes the jump itself occupies.  */
+#define STORE_JUMP(op, loc, to) \
+  store_op1 (op, loc, (int)((to) - (loc) - 3))
+
+/* Likewise, for a two-argument jump.  */
+#define STORE_JUMP2(op, loc, to, arg) \
+  store_op2 (op, loc, (int)((to) - (loc) - 3), arg)
+
+/* Like `STORE_JUMP', but for inserting.  Assume `b' is the buffer end.  */
+#define INSERT_JUMP(op, loc, to) \
+  insert_op1 (op, loc, (int)((to) - (loc) - 3), b)
+
+/* Like `STORE_JUMP2', but for inserting.  Assume `b' is the buffer end.  */
+#define INSERT_JUMP2(op, loc, to, arg) \
+  insert_op2 (op, loc, (int)((to) - (loc) - 3), arg, b)
+
+
+/* This is not an arbitrary limit: the arguments which represent offsets
+   into the pattern are two bytes long.  So if 2^16 bytes turns out to
+   be too small, many things would have to change.  */
+#define MAX_BUF_SIZE (1L << 16)
+#define REALLOC realloc
+
+/* Extend the buffer by twice its current size via realloc and
+   reset the pointers that pointed into the old block to point to the
+   correct places in the new one.  If extending the buffer results in it
+   being larger than MAX_BUF_SIZE, then flag memory exhausted.  */
+#define EXTEND_BUFFER()							\
+  do { 									\
+    unsigned char *old_buffer = bufp->buffer;				\
+    if (bufp->allocated == MAX_BUF_SIZE) 				\
+      return REG_ESIZE;							\
+    bufp->allocated <<= 1;						\
+    if (bufp->allocated > MAX_BUF_SIZE)					\
+      bufp->allocated = MAX_BUF_SIZE; 					\
+    bufp->buffer = (unsigned char *) REALLOC(bufp->buffer, bufp->allocated);\
+    if (bufp->buffer == NULL)						\
+      return REG_ESPACE;						\
+    /* If the buffer moved, move all the pointers into it.  */		\
+    if (old_buffer != bufp->buffer)					\
+      {									\
+        b = (b - old_buffer) + bufp->buffer;				\
+        begalt = (begalt - old_buffer) + bufp->buffer;			\
+        if (fixup_alt_jump)						\
+          fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
+        if (laststart)							\
+          laststart = (laststart - old_buffer) + bufp->buffer;		\
+        if (pending_exact)						\
+          pending_exact = (pending_exact - old_buffer) + bufp->buffer;	\
+      }									\
+  } while (0)
+
+
+/* Since we have one byte reserved for the register number argument to
+   {start,stop}_memory, the maximum number of groups we can report
+   things about is what fits in that byte.  */
+#define MAX_REGNUM 255
+
+/* But patterns can have more than `MAX_REGNUM' registers.  We just
+   ignore the excess.  */
+typedef unsigned regnum_t;
+
+
+/* Macros for the compile stack.  */
+
+/* Since offsets can go either forwards or backwards, this type needs to
+   be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1.  */
+/* int may be not enough when sizeof(int) == 2                           */
+typedef long pattern_offset_t;
+
+typedef struct {
+	pattern_offset_t begalt_offset;
+	pattern_offset_t fixup_alt_jump;
+	pattern_offset_t inner_group_offset;
+	pattern_offset_t laststart_offset;
+	regnum_t regnum;
+} compile_stack_elt_t;
+
+
+typedef struct {
+	compile_stack_elt_t *stack;
+	unsigned size;
+	unsigned avail;		/* Offset of next open position.  */
+} compile_stack_type;
+
+
+#define INIT_COMPILE_STACK_SIZE 32
+
+#define COMPILE_STACK_EMPTY  (compile_stack.avail == 0)
+#define COMPILE_STACK_FULL  (compile_stack.avail == compile_stack.size)
+
+/* The next available element.  */
+#define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
+
+
+/* Set the bit for character C in a list.  */
+#define SET_LIST_BIT(c)                               \
+  (b[((unsigned char) (c)) / BYTEWIDTH]               \
+   |= 1 << (((unsigned char) c) % BYTEWIDTH))
+
+
+/* Get the next unsigned number in the uncompiled pattern.  */
+#define GET_UNSIGNED_NUMBER(num) 					\
+  { if (p != pend)							\
+     {									\
+       PATFETCH (c); 							\
+       while (ISDIGIT (c)) 						\
+         { 								\
+           if (num < 0)							\
+              num = 0;							\
+           num = num * 10 + c - '0'; 					\
+           if (p == pend) 						\
+              break; 							\
+           PATFETCH (c);						\
+         } 								\
+       } 								\
+    }
+
+#define CHAR_CLASS_MAX_LENGTH  6	/* Namely, `xdigit'.  */
+
+#define IS_CHAR_CLASS(string)						\
+   (STREQ (string, "alpha") || STREQ (string, "upper")			\
+    || STREQ (string, "lower") || STREQ (string, "digit")		\
+    || STREQ (string, "alnum") || STREQ (string, "xdigit")		\
+    || STREQ (string, "space") || STREQ (string, "print")		\
+    || STREQ (string, "punct") || STREQ (string, "graph")		\
+    || STREQ (string, "cntrl") || STREQ (string, "blank"))
+
+static boolean group_in_compile_stack (compile_stack_type
+				       compile_stack, regnum_t regnum);
+
+/* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
+   Returns one of error codes defined in `regex.h', or zero for success  */
+
+static reg_errcode_t regex_compile(pattern, size, syntax, bufp)
+const char *pattern;
+size_t size;
+reg_syntax_t syntax;
+struct re_pattern_buffer *bufp;
+{
+	/* We fetch characters from PATTERN here.  Even though PATTERN is
+	   `char *' (i.e., signed), we declare these variables as unsigned, so
+	   they can be reliably used as array indices.  */
+	register unsigned char c, c1;
+
+	/* A random tempory spot in PATTERN.  */
+	const char *p1;
+
+	/* Points to the end of the buffer, where we should append.  */
+	register unsigned char *b;
+
+	/* Keeps track of unclosed groups.  */
+	compile_stack_type compile_stack;
+
+	/* Points to the current (ending) position in the pattern.  */
+	const char *p = pattern;
+	const char *pend = pattern + size;
+
+	/* How to translate the characters in the pattern.  */
+	char *translate = bufp->translate;
+
+	/* Address of the count-byte of the most recently inserted `exactn'
+	   command.  This makes it possible to tell if a new exact-match
+	   character can be added to that command or if the character requires
+	   a new `exactn' command.  */
+	unsigned char *pending_exact = 0;
+
+	/* Address of start of the most recently finished expression.
+	   This tells, e.g., postfix * where to find the start of its
+	   operand.  Reset at the beginning of groups and alternatives.  */
+	unsigned char *laststart = 0;
+
+	/* Address of beginning of regexp, or inside of last group.  */
+	unsigned char *begalt;
+
+	/* Place in the uncompiled pattern (i.e., the {) to
+	   which to go back if the interval is invalid.  */
+	const char *beg_interval;
+
+	/* Address of the place where a forward jump should go to the end of
+	   the containing expression.  Each alternative of an `or' -- except the
+	   last -- ends with a forward jump of this sort.  */
+	unsigned char *fixup_alt_jump = 0;
+
+	/* Counts open-groups as they are encountered.  Remembered for the
+	   matching close-group on the compile stack, so the same register
+	   number is put in the stop_memory as the start_memory.  */
+	regnum_t regnum = 0;
+
+	/* Initialize the compile stack.  */
+	compile_stack.stack =
+	    TALLOC(INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
+	if (compile_stack.stack == NULL)
+		return REG_ESPACE;
+
+	compile_stack.size = INIT_COMPILE_STACK_SIZE;
+	compile_stack.avail = 0;
+
+	/* Initialize the pattern buffer.  */
+	bufp->syntax = syntax;
+	bufp->fastmap_accurate = 0;
+	bufp->not_bol = bufp->not_eol = 0;
+
+	/* Set `used' to zero, so that if we return an error, the pattern
+	   printer (for debugging) will think there's no pattern.  We reset it
+	   at the end.  */
+	bufp->used = 0;
+
+	/* Always count groups, whether or not bufp->no_sub is set.  */
+	bufp->re_nsub = 0;
+
+	/* Initialize the syntax table.  */
+	init_syntax_once();
+
+	if (bufp->allocated == 0) {
+		if (bufp->buffer) {
+			RETALLOC(bufp->buffer, INIT_BUF_SIZE,
+				 unsigned char);
+		} else { /* Caller did not allocate a buffer. Do it for them. */
+			bufp->buffer =
+			    TALLOC(INIT_BUF_SIZE, unsigned char);
+		}
+		if (!bufp->buffer)
+			return REG_ESPACE;
+
+		bufp->allocated = INIT_BUF_SIZE;
+	}
+
+	begalt = b = bufp->buffer;
+
+	/* Loop through the uncompiled pattern until we're at the end.  */
+	while (p != pend) {
+		PATFETCH(c);
+
+		switch (c) {
+		case '^':
+		{
+			if (p == pattern + 1 ||
+			    syntax & RE_CONTEXT_INDEP_ANCHORS ||
+			    at_begline_loc_p(pattern, p, syntax))
+				BUF_PUSH(begline);
+			else
+				goto normal_char;
+		}
+		break;
+
+		case '$':
+		{
+			if (p == pend ||
+			    syntax & RE_CONTEXT_INDEP_ANCHORS ||
+			    at_endline_loc_p(p, pend, syntax))
+				BUF_PUSH(endline);
+			else
+				goto normal_char;
+		}
+		break;
+
+		case '+':
+
+		case '?':
+		if ((syntax & RE_BK_PLUS_QM) ||
+		    (syntax & RE_LIMITED_OPS))
+			goto normal_char;
+		handle_plus:
+
+		case '*':
+		/* If there is no previous pattern... */
+		if (!laststart) {
+			if (syntax & RE_CONTEXT_INVALID_OPS)
+				return REG_BADRPT;
+			else if (!(syntax & RE_CONTEXT_INDEP_OPS))
+				goto normal_char;
+		}
+
+		{
+			/* Are we optimizing this jump?  */
+			boolean keep_string_p = false;
+
+			/* 1 means zero (many) matches is allowed.  */
+			char zero_times_ok = 0, many_times_ok = 0;
+
+			for (;;) {
+				zero_times_ok |= c != '+';
+				many_times_ok |= c != '?';
+
+				if (p == pend)
+					break;
+
+				PATFETCH(c);
+
+				if (c == '*' || (!(syntax & RE_BK_PLUS_QM) &&
+				    (c == '+' || c == '?')));
+
+				else if (syntax & RE_BK_PLUS_QM && c == '\\') {
+					if (p == pend)
+						return REG_EESCAPE;
+
+					PATFETCH(c1);
+					if (!(c1 == '+' || c1 == '?')) {
+						PATUNFETCH;
+						PATUNFETCH;
+						break;
+					}
+
+					c = c1;
+				} else {
+					PATUNFETCH;
+					break;
+				}
+			}
+
+			if (!laststart)
+				break;
+
+			if (many_times_ok) {
+				assert(p - 1 > pattern);
+
+				/* Allocate the space for the jump.  */
+				GET_BUFFER_SPACE(3);
+
+				if (TRANSLATE(*(p - 2)) == TRANSLATE('.') &&
+				    zero_times_ok && p < pend &&
+				    TRANSLATE(*p) == TRANSLATE('\n') &&
+				    !(syntax & RE_DOT_NEWLINE)) {
+					/* We have .*\n.  */
+					STORE_JUMP(jump, b, laststart);
+					keep_string_p = true;
+				} else
+					STORE_JUMP(maybe_pop_jump, b,
+						   laststart - 3);
+
+					b += 3;
+				}
+
+				GET_BUFFER_SPACE(3);
+				INSERT_JUMP(keep_string_p ?
+					    on_failure_keep_string_jump :
+					    on_failure_jump, laststart,
+					    b + 3);
+				pending_exact = 0;
+				b += 3;
+
+				if (!zero_times_ok) {
+					GET_BUFFER_SPACE(3);
+					INSERT_JUMP(dummy_failure_jump,
+						    laststart,
+						    laststart + 6);
+					b += 3;
+				}
+			}
+			break;
+
+
+		case '.':
+		laststart = b;
+		BUF_PUSH(anychar);
+		break;
+
+		case '[':
+		{
+			boolean had_char_class = false;
+
+			if (p == pend)
+				return REG_EBRACK;
+
+			GET_BUFFER_SPACE(34);
+
+			laststart = b;
+
+			/* We test `*p == '^' twice, instead of using an if
+			   statement, so we only need one BUF_PUSH.  */
+			BUF_PUSH(*p == '^' ? charset_not : charset);
+			if (*p == '^')
+				p++;
+
+			p1 = p;
+
+			/* Push the number of bytes in the bitmap.  */
+			BUF_PUSH((1 << BYTEWIDTH) / BYTEWIDTH);
+
+			/* Clear the whole map.  */
+			bzero(b, (1 << BYTEWIDTH) / BYTEWIDTH);
+
+			if ((re_opcode_t) b[-2] == charset_not
+			    && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
+				SET_LIST_BIT('\n');
+
+			/* Read in characters and ranges, setting map bits.  */
+			for (;;) {
+				if (p == pend)
+					return REG_EBRACK;
+
+				PATFETCH(c);
+
+				if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) &&
+				    c == '\\') {
+					if (p == pend)
+						return REG_EESCAPE;
+
+					PATFETCH(c1);
+					SET_LIST_BIT(c1);
+					continue;
+				}
+
+				if (c == ']' && p != p1 + 1)
+					break;
+
+				if (had_char_class && c == '-' && *p != ']')
+					return REG_ERANGE;
+
+				if (c == '-' && !(p - 2 >= pattern &&
+				    p[-2] == '[') && !(p - 3 >= pattern &&
+				    p[-3] == '[' && p[-2] == '^') &&
+				    *p != ']') {
+					reg_errcode_t ret =
+					    compile_range(&p, pend, translate,
+							  syntax, b);
+					if (ret != REG_NOERROR)
+						return ret;
+				}
+
+				else if (p[0] == '-' && p[1] != ']') {
+					reg_errcode_t ret;
+
+					/* Move past the `-'.  */
+					PATFETCH(c1);
+
+					ret = compile_range(&p, pend, translate,
+							    syntax, b);
+					if (ret != REG_NOERROR)
+						return ret;
+				}
+
+				else if (syntax & RE_CHAR_CLASSES &&
+					 c == '[' && *p == ':') {
+					char str[CHAR_CLASS_MAX_LENGTH + 1];
+
+					PATFETCH(c);
+					c1 = 0;
+
+					/* If pattern is `[[:'.  */
+					if (p == pend)
+						return REG_EBRACK;
+
+					for (;;) {
+						PATFETCH(c);
+						if (c == ':' || c == ']' ||
+						    p == pend || c1 ==
+						    CHAR_CLASS_MAX_LENGTH)
+							break;
+						str[c1++] = c;
+					}
+					str[c1] = '\0';
+
+					if (c == ':' && *p == ']') {
+						int ch;
+						boolean is_alnum =
+						    STREQ(str, "alnum");
+						boolean is_alpha =
+						    STREQ(str, "alpha");
+						boolean is_blank =
+						    STREQ(str, "blank");
+						boolean is_cntrl =
+						    STREQ(str, "cntrl");
+						boolean is_digit =
+						    STREQ(str, "digit");
+						boolean is_graph =
+						    STREQ(str, "graph");
+						boolean is_lower =
+						    STREQ(str, "lower");
+						boolean is_print =
+						    STREQ(str, "print");
+						boolean is_punct =
+						    STREQ(str, "punct");
+						boolean is_space =
+						    STREQ(str, "space");
+						boolean is_upper =
+						    STREQ(str, "upper");
+						boolean is_xdigit =
+						    STREQ(str, "xdigit");
+
+						if (!IS_CHAR_CLASS(str))
+							return REG_ECTYPE;
+
+						PATFETCH(c);
+
+						if (p == pend)
+							return REG_EBRACK;
+
+						for (ch = 0; ch < 1 <<
+						     BYTEWIDTH; ch++) {
+							if ((is_alnum &&
+							     ISALNUM(ch)) ||
+							    (is_alpha &&
+							     ISALPHA(ch)) ||
+							    (is_blank &&
+							     ISBLANK(ch)) ||
+							    (is_cntrl &&
+							     ISCNTRL(ch)) ||
+							    (is_digit &&
+							     ISDIGIT(ch)) ||
+							    (is_graph &&
+							     ISGRAPH(ch)) ||
+							    (is_lower &&
+							     ISLOWER(ch)) ||
+							    (is_print &&
+							     ISPRINT(ch)) ||
+							    (is_punct &&
+							     ISPUNCT(ch)) ||
+							    (is_space &&
+							     ISSPACE(ch)) ||
+							    (is_upper &&
+							     ISUPPER(ch)) ||
+							    (is_xdigit &&
+							     ISXDIGIT(ch)))
+								SET_LIST_BIT(ch);
+						}
+						had_char_class =
+						    true;
+					} else {
+						c1++;
+						while (c1--)
+							PATUNFETCH;
+						SET_LIST_BIT('[');
+						SET_LIST_BIT(':');
+						had_char_class = false;
+					}
+				} else {
+					had_char_class = false;
+					SET_LIST_BIT(c);
+				}
+			}
+
+			while ((int) b[-1] > 0
+			       && b[b[-1] - 1] == 0)
+				b[-1]--;
+			b += b[-1];
+		}
+		break;
+
+		case '(':
+		if (syntax & RE_NO_BK_PARENS)
+			goto handle_open;
+		else
+			goto normal_char;
+
+
+		case ')':
+		if (syntax & RE_NO_BK_PARENS)
+			goto handle_close;
+		else
+			goto normal_char;
+
+
+		case '\n':
+		if (syntax & RE_NEWLINE_ALT)
+			goto handle_alt;
+		else
+			goto normal_char;
+
+
+		case '|':
+		if (syntax & RE_NO_BK_VBAR)
+			goto handle_alt;
+		else
+			goto normal_char;
+
+
+		case '{':
+		if (syntax & RE_INTERVALS
+		    && syntax & RE_NO_BK_BRACES)
+			goto handle_interval;
+		else
+			goto normal_char;
+
+
+		case '\\':
+		if (p == pend)
+			return REG_EESCAPE;
+
+		PATFETCH_RAW(c);
+
+		switch (c) {
+			case '(':
+			if (syntax & RE_NO_BK_PARENS)
+				goto normal_backslash;
+
+		      handle_open:
+			bufp->re_nsub++;
+			regnum++;
+
+			if (COMPILE_STACK_FULL) {
+				RETALLOC(compile_stack.stack,
+					 compile_stack.size << 1,
+					 compile_stack_elt_t);
+				if (compile_stack.stack == NULL)
+					return REG_ESPACE;
+
+				compile_stack.size <<= 1;
+			}
+
+			COMPILE_STACK_TOP.begalt_offset =
+			    begalt - bufp->buffer;
+			COMPILE_STACK_TOP.fixup_alt_jump =
+			    fixup_alt_jump ? fixup_alt_jump -
+			    bufp->buffer + 1 : 0;
+			COMPILE_STACK_TOP.laststart_offset =
+			    b - bufp->buffer;
+			COMPILE_STACK_TOP.regnum = regnum;
+
+			if (regnum <= MAX_REGNUM) {
+				COMPILE_STACK_TOP.inner_group_offset =
+				    b - bufp->buffer + 2;
+				BUF_PUSH_3(start_memory, regnum, 0);
+			}
+
+			compile_stack.avail++;
+
+			fixup_alt_jump = 0;
+			laststart = 0;
+			begalt = b;
+			pending_exact = 0;
+			break;
+
+			case ')':
+			if (syntax & RE_NO_BK_PARENS)
+				goto normal_backslash;
+
+			if (COMPILE_STACK_EMPTY) {
+				if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
+					goto normal_backslash;
+				else
+					return REG_ERPAREN;
+			}
+
+		      handle_close:
+			if (fixup_alt_jump) {
+				BUF_PUSH(push_dummy_failure);
+				STORE_JUMP(jump_past_alt,
+					   fixup_alt_jump, b - 1);
+			}
+
+			if (COMPILE_STACK_EMPTY) {
+				if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
+					goto normal_char;
+				else
+					return REG_ERPAREN;
+			}
+
+			assert(compile_stack.avail != 0);
+			{
+				regnum_t this_group_regnum;
+
+				compile_stack.avail--;
+				begalt = bufp->buffer +
+					    COMPILE_STACK_TOP.begalt_offset;
+				fixup_alt_jump =
+					    COMPILE_STACK_TOP.fixup_alt_jump ?
+					    bufp->buffer + COMPILE_STACK_TOP.
+					    fixup_alt_jump - 1 : 0;
+				laststart = bufp->buffer +
+					    COMPILE_STACK_TOP.laststart_offset;
+				this_group_regnum = COMPILE_STACK_TOP.regnum;
+				pending_exact = 0;
+
+				if (this_group_regnum <= MAX_REGNUM) {
+					unsigned char
+					*inner_group_loc = bufp->buffer +
+						COMPILE_STACK_TOP.
+						inner_group_offset;
+
+					*inner_group_loc = regnum -
+						this_group_regnum;
+					BUF_PUSH_3(stop_memory,
+						   this_group_regnum,
+						   regnum - this_group_regnum);
+				}
+			}
+			break;
+
+
+			case '|':	/* `\|'.  */
+			if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
+				goto normal_backslash;
+		      handle_alt:
+			if (syntax & RE_LIMITED_OPS)
+				goto normal_char;
+
+			GET_BUFFER_SPACE(3);
+			INSERT_JUMP(on_failure_jump, begalt, b + 6);
+			pending_exact = 0;
+			b += 3;
+
+			if (fixup_alt_jump)
+				STORE_JUMP(jump_past_alt, fixup_alt_jump, b);
+
+			fixup_alt_jump = b;
+			GET_BUFFER_SPACE(3);
+			b += 3;
+
+			laststart = 0;
+			begalt = b;
+			break;
+
+
+			case '{':
+			/* If \{ is a literal.  */
+			if (!(syntax & RE_INTERVALS) || ((syntax & RE_INTERVALS)
+			    && (syntax & RE_NO_BK_BRACES))
+			    || (p - 2 == pattern && p == pend))
+				goto normal_backslash;
+
+		      handle_interval:
+			{
+				int lower_bound = -1, upper_bound = -1;
+				beg_interval = p - 1;
+
+				if (p == pend) {
+					if (syntax & RE_NO_BK_BRACES)
+						goto unfetch_interval;
+					else
+						return REG_EBRACE;
+				}
+
+				GET_UNSIGNED_NUMBER(lower_bound);
+
+				if (c == ',') {
+					GET_UNSIGNED_NUMBER(upper_bound);
+					if (upper_bound < 0)
+						upper_bound = RE_DUP_MAX;
+				} else
+					upper_bound = lower_bound;
+
+				if (lower_bound < 0 || upper_bound > RE_DUP_MAX
+				    || lower_bound > upper_bound) {
+					if (syntax & RE_NO_BK_BRACES)
+						goto unfetch_interval;
+					else
+						return REG_BADBR;
+				}
+
+				if (!(syntax & RE_NO_BK_BRACES)) {
+					if (c != '\\')
+						return REG_EBRACE;
+
+					PATFETCH(c);
+				}
+
+				if (c != '}') {
+					if (syntax & RE_NO_BK_BRACES)
+						goto unfetch_interval;
+					else
+						return REG_BADBR;
+				}
+
+				if (!laststart) {
+					if (syntax & RE_CONTEXT_INVALID_OPS)
+						return REG_BADRPT;
+					else if (syntax & RE_CONTEXT_INDEP_OPS)
+						laststart = b;
+					else
+						goto unfetch_interval;
+				}
+
+				if (upper_bound == 0) {
+					GET_BUFFER_SPACE(3);
+					INSERT_JUMP(jump, laststart, b + 3);
+					b += 3;
+				}
+
+				else {
+					unsigned nbytes =
+					    10 + (upper_bound > 1) * 10;
+
+					GET_BUFFER_SPACE(nbytes);
+
+					INSERT_JUMP2(succeed_n, laststart,
+						     b + 5 + (upper_bound >
+						      1) * 5, lower_bound);
+					b += 5;
+
+					insert_op2(set_number_at, laststart, 5,
+						   lower_bound, b);
+					b += 5;
+
+					if (upper_bound > 1) {
+						STORE_JUMP2(jump_n, b,
+							    laststart + 5,
+							    upper_bound - 1);
+						b += 5;
+
+						insert_op2(set_number_at,
+							   laststart,
+							   b - laststart,
+							   upper_bound - 1, b);
+						b += 5;
+					}
+				}
+				pending_exact = 0;
+				beg_interval = NULL;
+			}
+			break;
+
+		      unfetch_interval:
+			assert(beg_interval);
+			p = beg_interval;
+			beg_interval = NULL;
+
+			/* normal_char and normal_backslash need `c'.  */
+			PATFETCH(c);
+
+			if (!(syntax & RE_NO_BK_BRACES)) {
+				if (p > pattern && p[-1] == '\\')
+					goto normal_backslash;
+			}
+			goto normal_char;
+
+			case 'w':
+				if (re_syntax_options & RE_NO_GNU_OPS)
+					goto normal_char;
+				laststart = b;
+				BUF_PUSH(wordchar);
+				break;
+
+
+			case 'W':
+				if (re_syntax_options & RE_NO_GNU_OPS)
+					goto normal_char;
+				laststart = b;
+				BUF_PUSH(notwordchar);
+				break;
+
+
+			case '<':
+				if (re_syntax_options & RE_NO_GNU_OPS)
+					goto normal_char;
+				BUF_PUSH(wordbeg);
+				break;
+
+			case '>':
+				if (re_syntax_options & RE_NO_GNU_OPS)
+					goto normal_char;
+				BUF_PUSH(wordend);
+				break;
+
+			case 'b':
+				if (re_syntax_options & RE_NO_GNU_OPS)
+					goto normal_char;
+				BUF_PUSH(wordbound);
+				break;
+
+			case 'B':
+				if (re_syntax_options & RE_NO_GNU_OPS)
+					goto normal_char;
+				BUF_PUSH(notwordbound);
+				break;
+
+			case '`':
+				if (re_syntax_options & RE_NO_GNU_OPS)
+					goto normal_char;
+				BUF_PUSH(begbuf);
+				break;
+
+			case '\'':
+				if (re_syntax_options & RE_NO_GNU_OPS)
+					goto normal_char;
+				BUF_PUSH(endbuf);
+				break;
+
+			case '1':
+			case '2':
+			case '3':
+			case '4':
+			case '5':
+			case '6':
+			case '7':
+			case '8':
+			case '9':
+				if (syntax & RE_NO_BK_REFS)
+					goto normal_char;
+
+				c1 = c - '0';
+
+				if (c1 > regnum)
+					return REG_ESUBREG;
+
+				/* Can't back reference to a subexpression if inside of it.  */
+				if (group_in_compile_stack
+				    (compile_stack, (regnum_t) c1))
+					goto normal_char;
+
+				laststart = b;
+				BUF_PUSH_2(duplicate, c1);
+				break;
+
+
+			case '+':
+			case '?':
+				if (syntax & RE_BK_PLUS_QM)
+					goto handle_plus;
+				else
+					goto normal_backslash;
+
+			default:
+			      normal_backslash:
+				/* You might think it would be useful for \ to mean
+				   not to translate; but if we don't translate it
+				   it will never match anything.  */
+				c = TRANSLATE(c);
+				goto normal_char;
+			}
+			break;
+
+
+		default:
+			/* Expects the character in `c'.  */
+		      normal_char:
+			/* If no exactn currently being built.  */
+			if (!pending_exact
+			    /* If last exactn not at current position.  */
+			    || pending_exact + *pending_exact + 1 != b
+			    /* We have only one byte following the exactn for the count.  */
+			    || *pending_exact == (1 << BYTEWIDTH) - 1
+			    /* If followed by a repetition operator.  */
+			    || *p == '*' || *p == '^'
+			    || ((syntax & RE_BK_PLUS_QM)
+				? *p == '\\' && (p[1] == '+'
+						 || p[1] == '?')
+				: (*p == '+' || *p == '?'))
+			    || ((syntax & RE_INTERVALS)
+				&& ((syntax & RE_NO_BK_BRACES)
+				    ? *p == '{'
+				    : (p[0] == '\\' && p[1] == '{')))) {
+				/* Start building a new exactn.  */
+
+				laststart = b;
+
+				BUF_PUSH_2(exactn, 0);
+				pending_exact = b - 1;
+			}
+
+			BUF_PUSH(c);
+			(*pending_exact)++;
+			break;
+		}		/* switch (c) */
+	}			/* while p != pend */
+
+
+	/* Through the pattern now.  */
+
+	if (fixup_alt_jump)
+		STORE_JUMP(jump_past_alt, fixup_alt_jump, b);
+
+	if (!COMPILE_STACK_EMPTY)
+		return REG_EPAREN;
+
+	free(compile_stack.stack);
+
+	/* We have succeeded; set the length of the buffer.  */
+	bufp->used = b - bufp->buffer;
+
+	return REG_NOERROR;
+}				/* regex_compile */
+
+/* Subroutines for `regex_compile'.  */
+
+/* Store OP at LOC followed by two-byte integer parameter ARG.  */
+
+static void store_op1(op, loc, arg)
+re_opcode_t op;
+unsigned char *loc;
+int arg;
+{
+	*loc = (unsigned char) op;
+	STORE_NUMBER(loc + 1, arg);
+}
+
+
+/* Like `store_op1', but for two two-byte parameters ARG1 and ARG2.  */
+
+static void store_op2(op, loc, arg1, arg2)
+re_opcode_t op;
+unsigned char *loc;
+int arg1, arg2;
+{
+	*loc = (unsigned char) op;
+	STORE_NUMBER(loc + 1, arg1);
+	STORE_NUMBER(loc + 3, arg2);
+}
+
+
+/* Copy the bytes from LOC to END to open up three bytes of space at LOC
+   for OP followed by two-byte integer parameter ARG.  */
+
+static void insert_op1(op, loc, arg, end)
+re_opcode_t op;
+unsigned char *loc;
+int arg;
+unsigned char *end;
+{
+	register unsigned char *pfrom = end;
+	register unsigned char *pto = end + 3;
+
+	while (pfrom != loc)
+		*--pto = *--pfrom;
+
+	store_op1(op, loc, arg);
+}
+
+
+/* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2.  */
+
+static void insert_op2(op, loc, arg1, arg2, end)
+re_opcode_t op;
+unsigned char *loc;
+int arg1, arg2;
+unsigned char *end;
+{
+	register unsigned char *pfrom = end;
+	register unsigned char *pto = end + 5;
+
+	while (pfrom != loc)
+		*--pto = *--pfrom;
+
+	store_op2(op, loc, arg1, arg2);
+}
+
+
+/* P points to just after a ^ in PATTERN.  Return true if that ^ comes
+   after an alternative or a begin-subexpression.  We assume there is at
+   least one character before the ^.  */
+
+static boolean at_begline_loc_p(pattern, p, syntax)
+const char *pattern, *p;
+reg_syntax_t syntax;
+{
+	const char *prev = p - 2;
+	boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
+
+	return
+	    /* After a subexpression?  */
+	    (*prev == '('
+	     && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
+	    /* After an alternative?  */
+	    || (*prev == '|'
+		&& (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
+}
+
+
+/* The dual of at_begline_loc_p.  This one is for $.  We assume there is
+   at least one character after the $, i.e., `P < PEND'.  */
+
+static boolean at_endline_loc_p(p, pend, syntax)
+const char *p, *pend;
+reg_syntax_t syntax;
+{
+	const char *next = p;
+	boolean next_backslash = *next == '\\';
+	const char *next_next = p + 1 < pend ? p + 1 : NULL;
+
+	return
+	    /* Before a subexpression?  */
+	    (syntax & RE_NO_BK_PARENS ? *next == ')'
+	     : next_backslash && next_next && *next_next == ')')
+	    /* Before an alternative?  */
+	    || (syntax & RE_NO_BK_VBAR ? *next == '|'
+		: next_backslash && next_next && *next_next == '|');
+}
+
+
+/* Returns true if REGNUM is in one of COMPILE_STACK's elements and
+   false if it's not.  */
+
+static boolean group_in_compile_stack(compile_stack, regnum)
+compile_stack_type compile_stack;
+regnum_t regnum;
+{
+	int this_element;
+
+	for (this_element = compile_stack.avail - 1;
+	     this_element >= 0; this_element--)
+		if (compile_stack.stack[this_element].regnum == regnum)
+			return true;
+
+	return false;
+}
+
+
+/* Read the ending character of a range (in a bracket expression) from the
+   uncompiled pattern *P_PTR (which ends at PEND).  We assume the
+   starting character is in `P[-2]'.  (`P[-1]' is the character `-'.)
+   Then we set the translation of all bits between the starting and
+   ending characters (inclusive) in the compiled pattern B.
+
+   Return an error code.
+
+   We use these short variable names so we can use the same macros as
+   `regex_compile' itself.  */
+
+static reg_errcode_t compile_range(p_ptr, pend, translate, syntax, b)
+const char **p_ptr, *pend;
+char *translate;
+reg_syntax_t syntax;
+unsigned char *b;
+{
+	unsigned this_char;
+
+	const char *p = *p_ptr;
+	int range_start, range_end;
+
+	if (p == pend)
+		return REG_ERANGE;
+
+	/* Even though the pattern is a signed `char *', we need to fetch
+	   with unsigned char *'s; if the high bit of the pattern character
+	   is set, the range endpoints will be negative if we fetch using a
+	   signed char *.
+
+	   We also want to fetch the endpoints without translating them; the
+	   appropriate translation is done in the bit-setting loop below.  */
+	range_start = ((unsigned char *) p)[-2];
+	range_end = ((unsigned char *) p)[0];
+
+	/* Have to increment the pointer into the pattern string, so the
+	   caller isn't still at the ending character.  */
+	(*p_ptr)++;
+
+	/* If the start is after the end, the range is empty.  */
+	if (range_start > range_end)
+		return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE :
+		    REG_NOERROR;
+
+	/* Here we see why `this_char' has to be larger than an `unsigned
+	   char' -- the range is inclusive, so if `range_end' == 0xff
+	   (assuming 8-bit characters), we would otherwise go into an infinite
+	   loop, since all characters <= 0xff.  */
+	for (this_char = range_start; this_char <= range_end; this_char++) {
+		SET_LIST_BIT(TRANSLATE(this_char));
+	}
+	return REG_NOERROR;
+}
+
+/* Failure stack declarations and macros; both re_compile_fastmap and
+   re_match_2 use a failure stack.  These have to be macros because of
+   REGEX_ALLOCATE.  */
+
+
+/* Number of failure points for which to initially allocate space
+   when matching.  If this number is exceeded, we allocate more
+   space, so it is not a hard limit.  */
+#define INIT_FAILURE_ALLOC 5
+
+/* Roughly the maximum number of failure points on the stack.  Would be
+   exactly that if always used MAX_FAILURE_SPACE each time we failed.
+   This is a variable only so users of regex can assign to it; we never
+   change it ourselves.  */
+int re_max_failures = 2000;
+
+typedef const unsigned char *fail_stack_elt_t;
+
+typedef struct {
+	fail_stack_elt_t *stack;
+	unsigned size;
+	unsigned avail;		/* Offset of next open position.  */
+} fail_stack_type;
+
+#define FAIL_STACK_EMPTY()     (fail_stack.avail == 0)
+#define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
+#define FAIL_STACK_FULL()      (fail_stack.avail == fail_stack.size)
+#define FAIL_STACK_TOP()       (fail_stack.stack[fail_stack.avail])
+
+
+/* Initialize `fail_stack'.  Do `return -2' if the alloc fails.  */
+
+#define INIT_FAIL_STACK()						\
+  do {									\
+    fail_stack.stack = (fail_stack_elt_t *)				\
+      REGEX_ALLOCATE (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t));	\
+									\
+    if (fail_stack.stack == NULL)					\
+      return -2;							\
+									\
+    fail_stack.size = INIT_FAILURE_ALLOC;				\
+    fail_stack.avail = 0;						\
+  } while (0)
+
+
+/* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
+
+   Return 1 if succeeds, and 0 if either ran out of memory
+   allocating space for it or it was already too large.
+
+   REGEX_REALLOCATE requires `destination' be declared.   */
+
+#define DOUBLE_FAIL_STACK(fail_stack)					\
+  ((fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS		\
+   ? 0									\
+   : ((fail_stack).stack = (fail_stack_elt_t *)				\
+        REGEX_REALLOCATE ((fail_stack).stack, 				\
+          (fail_stack).size * sizeof (fail_stack_elt_t),		\
+          ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)),	\
+									\
+      (fail_stack).stack == NULL					\
+      ? 0								\
+      : ((fail_stack).size <<= 1, 					\
+         1)))
+
+
+/* Push PATTERN_OP on FAIL_STACK.
+
+   Return 1 if was able to do so and 0 if ran out of memory allocating
+   space to do so.  */
+#define PUSH_PATTERN_OP(pattern_op, fail_stack)				\
+  ((FAIL_STACK_FULL ()							\
+    && !DOUBLE_FAIL_STACK (fail_stack))					\
+    ? 0									\
+    : ((fail_stack).stack[(fail_stack).avail++] = pattern_op,		\
+       1))
+
+/* This pushes an item onto the failure stack.  Must be a four-byte
+   value.  Assumes the variable `fail_stack'.  Probably should only
+   be called from within `PUSH_FAILURE_POINT'.  */
+#define PUSH_FAILURE_ITEM(item)						\
+  fail_stack.stack[fail_stack.avail++] = (fail_stack_elt_t) item
+
+/* The complement operation.  Assumes `fail_stack' is nonempty.  */
+#define POP_FAILURE_ITEM() fail_stack.stack[--fail_stack.avail]
+
+/* Used to omit pushing failure point id's when we're not debugging.  */
+#define DEBUG_PUSH(item)
+#define DEBUG_POP(item_addr)
+
+
+/* Push the information about the state we will need
+   if we ever fail back to it.
+
+   Requires variables fail_stack, regstart, regend, reg_info, and
+   num_regs be declared.  DOUBLE_FAIL_STACK requires `destination' be
+   declared.
+
+   Does `return FAILURE_CODE' if runs out of memory.  */
+
+#define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code)	\
+  do {									\
+    char *destination;							\
+    /* Must be int, so when we don't save any registers, the arithmetic	\
+       of 0 + -1 isn't done as unsigned.  */				\
+    /* Can't be int, since there is not a shred of a guarantee that int \
+       is wide enough to hold a value of something to which pointer can \
+       be assigned */							\
+    s_reg_t this_reg;							\
+    									\
+    DEBUG_STATEMENT (failure_id++);					\
+    DEBUG_STATEMENT (nfailure_points_pushed++);				\
+    DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id);		\
+    DEBUG_PRINT2 ("  Before push, next avail: %d\n", (fail_stack).avail);\
+    DEBUG_PRINT2 ("                     size: %d\n", (fail_stack).size);\
+									\
+    DEBUG_PRINT2 ("  slots needed: %d\n", NUM_FAILURE_ITEMS);		\
+    DEBUG_PRINT2 ("     available: %d\n", REMAINING_AVAIL_SLOTS);	\
+									\
+    /* Ensure we have enough space allocated for what we will push.  */	\
+    while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS)			\
+      {									\
+	if (!DOUBLE_FAIL_STACK (fail_stack))			\
+	  return failure_code;						\
+									\
+	DEBUG_PRINT2 ("\n  Doubled stack; size now: %d\n",		\
+		       (fail_stack).size);				\
+	DEBUG_PRINT2 ("  slots available: %d\n", REMAINING_AVAIL_SLOTS);\
+      }
+
+#define PUSH_FAILURE_POINT2(pattern_place, string_place, failure_code)	\
+    /* Push the info, starting with the registers.  */			\
+    DEBUG_PRINT1 ("\n");						\
+									\
+    PUSH_FAILURE_POINT_LOOP ();						\
+									\
+    DEBUG_PRINT2 ("  Pushing  low active reg: %d\n", lowest_active_reg);\
+    PUSH_FAILURE_ITEM (lowest_active_reg);				\
+									\
+    DEBUG_PRINT2 ("  Pushing high active reg: %d\n", highest_active_reg);\
+    PUSH_FAILURE_ITEM (highest_active_reg);				\
+									\
+    DEBUG_PRINT2 ("  Pushing pattern 0x%x: ", pattern_place);		\
+    DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend);		\
+    PUSH_FAILURE_ITEM (pattern_place);					\
+									\
+    DEBUG_PRINT2 ("  Pushing string 0x%x: `", string_place);		\
+    DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2,	\
+				 size2);				\
+    DEBUG_PRINT1 ("'\n");						\
+    PUSH_FAILURE_ITEM (string_place);					\
+									\
+    DEBUG_PRINT2 ("  Pushing failure id: %u\n", failure_id);		\
+    DEBUG_PUSH (failure_id);						\
+  } while (0)
+
+/*  Pulled out of PUSH_FAILURE_POINT() to shorten the definition
+    of that macro.  (for VAX C) */
+#define PUSH_FAILURE_POINT_LOOP()					\
+    for (this_reg = lowest_active_reg; this_reg <= highest_active_reg;	\
+	 this_reg++)							\
+      {									\
+	DEBUG_PRINT2 ("  Pushing reg: %d\n", this_reg);			\
+	DEBUG_STATEMENT (num_regs_pushed++);				\
+									\
+	DEBUG_PRINT2 ("    start: 0x%x\n", regstart[this_reg]);		\
+	PUSH_FAILURE_ITEM (regstart[this_reg]);				\
+									\
+	DEBUG_PRINT2 ("    end: 0x%x\n", regend[this_reg]);		\
+	PUSH_FAILURE_ITEM (regend[this_reg]);				\
+									\
+	DEBUG_PRINT2 ("    info: 0x%x\n      ", reg_info[this_reg]);	\
+	DEBUG_PRINT2 (" match_null=%d",					\
+		      REG_MATCH_NULL_STRING_P (reg_info[this_reg]));	\
+	DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg]));	\
+	DEBUG_PRINT2 (" matched_something=%d",				\
+		      MATCHED_SOMETHING (reg_info[this_reg]));		\
+	DEBUG_PRINT2 (" ever_matched=%d",				\
+		      EVER_MATCHED_SOMETHING (reg_info[this_reg]));	\
+	DEBUG_PRINT1 ("\n");						\
+	PUSH_FAILURE_ITEM (reg_info[this_reg].word);			\
+      }
+
+/* This is the number of items that are pushed and popped on the stack
+   for each register.  */
+#define NUM_REG_ITEMS  3
+
+/* Individual items aside from the registers.  */
+#define NUM_NONREG_ITEMS 4
+
+/* We push at most this many items on the stack.  */
+#define MAX_FAILURE_ITEMS ((num_regs - 1) * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
+
+/* We actually push this many items.  */
+#define NUM_FAILURE_ITEMS						\
+  ((highest_active_reg - lowest_active_reg + 1) * NUM_REG_ITEMS 	\
+    + NUM_NONREG_ITEMS)
+
+/* How many items can still be added to the stack without overflowing it.  */
+#define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
+
+
+/* Pops what PUSH_FAIL_STACK pushes.
+
+   We restore into the parameters, all of which should be lvalues:
+     STR -- the saved data position.
+     PAT -- the saved pattern position.
+     LOW_REG, HIGH_REG -- the highest and lowest active registers.
+     REGSTART, REGEND -- arrays of string positions.
+     REG_INFO -- array of information about each subexpression.
+
+   Also assumes the variables `fail_stack' and (if debugging), `bufp',
+   `pend', `string1', `size1', `string2', and `size2'.  */
+
+#define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
+{									\
+  DEBUG_STATEMENT (fail_stack_elt_t failure_id;)			\
+  s_reg_t this_reg;							\
+  const unsigned char *string_temp;					\
+									\
+  assert (!FAIL_STACK_EMPTY ());					\
+									\
+  /* Remove failure points and point to how many regs pushed.  */	\
+  DEBUG_PRINT1 ("POP_FAILURE_POINT:\n");				\
+  DEBUG_PRINT2 ("  Before pop, next avail: %d\n", fail_stack.avail);	\
+  DEBUG_PRINT2 ("                    size: %d\n", fail_stack.size);	\
+									\
+  assert (fail_stack.avail >= NUM_NONREG_ITEMS);			\
+									\
+  DEBUG_POP (&failure_id);						\
+  DEBUG_PRINT2 ("  Popping failure id: %u\n", failure_id);		\
+									\
+  /* If the saved string location is NULL, it came from an		\
+     on_failure_keep_string_jump opcode, and we want to throw away the	\
+     saved NULL, thus retaining our current position in the string.  */	\
+  string_temp = POP_FAILURE_ITEM ();					\
+  if (string_temp != NULL)						\
+    str = (const char *) string_temp;					\
+									\
+  DEBUG_PRINT2 ("  Popping string 0x%x: `", str);			\
+  DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2);	\
+  DEBUG_PRINT1 ("'\n");							\
+									\
+  pat = (unsigned char *) POP_FAILURE_ITEM ();				\
+  DEBUG_PRINT2 ("  Popping pattern 0x%x: ", pat);			\
+  DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend);			\
+									\
+  POP_FAILURE_POINT2 (low_reg, high_reg, regstart, regend, reg_info);
+
+/*  Pulled out of POP_FAILURE_POINT() to shorten the definition
+    of that macro.  (for MSC 5.1) */
+#define POP_FAILURE_POINT2(low_reg, high_reg, regstart, regend, reg_info) \
+									\
+  /* Restore register info.  */						\
+  high_reg = (active_reg_t) POP_FAILURE_ITEM ();			\
+  DEBUG_PRINT2 ("  Popping high active reg: %d\n", high_reg);		\
+									\
+  low_reg = (active_reg_t) POP_FAILURE_ITEM ();				\
+  DEBUG_PRINT2 ("  Popping  low active reg: %d\n", low_reg);		\
+									\
+  for (this_reg = high_reg; this_reg >= low_reg; this_reg--)		\
+    {									\
+      DEBUG_PRINT2 ("    Popping reg: %d\n", this_reg);			\
+									\
+      reg_info[this_reg].word = POP_FAILURE_ITEM ();			\
+      DEBUG_PRINT2 ("      info: 0x%x\n", reg_info[this_reg]);		\
+									\
+      regend[this_reg] = (const char *) POP_FAILURE_ITEM ();		\
+      DEBUG_PRINT2 ("      end: 0x%x\n", regend[this_reg]);		\
+									\
+      regstart[this_reg] = (const char *) POP_FAILURE_ITEM ();		\
+      DEBUG_PRINT2 ("      start: 0x%x\n", regstart[this_reg]);		\
+    }									\
+									\
+  DEBUG_STATEMENT (nfailure_points_popped++);				\
+}				/* POP_FAILURE_POINT */
+
+/* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
+   BUFP.  A fastmap records which of the (1 << BYTEWIDTH) possible
+   characters can start a string that matches the pattern.  This fastmap
+   is used by re_search to skip quickly over impossible starting points.
+
+   The caller must supply the address of a (1 << BYTEWIDTH)-byte data
+   area as BUFP->fastmap.
+
+   We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
+   the pattern buffer.
+
+   Returns 0 if we succeed, -2 if an internal error.   */
+
+int re_compile_fastmap(bufp)
+struct re_pattern_buffer *bufp;
+{
+	int j, k;
+	fail_stack_type fail_stack;
+	char *destination;
+	/* We don't push any register information onto the failure stack.  */
+	unsigned num_regs = 0;
+
+	register char *fastmap = bufp->fastmap;
+	unsigned char *pattern = bufp->buffer;
+	const unsigned char *p = pattern;
+	register unsigned char *pend = pattern + bufp->used;
+
+	/* Assume that each path through the pattern can be null until
+	   proven otherwise.  We set this false at the bottom of switch
+	   statement, to which we get only if a particular path doesn't
+	   match the empty string.  */
+	boolean path_can_be_null = true;
+
+	/* We aren't doing a `succeed_n' to begin with.  */
+	boolean succeed_n_p = false;
+
+	assert(fastmap != NULL && p != NULL);
+
+	INIT_FAIL_STACK();
+	bzero(fastmap, 1 << BYTEWIDTH);	/* Assume nothing's valid.  */
+	bufp->fastmap_accurate = 1;	/* It will be when we're done.  */
+	bufp->can_be_null = 0;
+
+	while (p != pend || !FAIL_STACK_EMPTY()) {
+		if (p == pend) {
+			bufp->can_be_null |= path_can_be_null;
+
+			/* Reset for next path.  */
+			path_can_be_null = true;
+
+			p = fail_stack.stack[--fail_stack.avail];
+		}
+
+		/* We should never be about to go beyond the end of the pattern.  */
+		assert(p < pend);
+
+		switch ((re_opcode_t) * p++) {
+
+			/* I guess the idea here is to simply not bother with a fastmap
+			   if a backreference is used, since it's too hard to figure out
+			   the fastmap for the corresponding group.  Setting
+			   `can_be_null' stops `re_search_2' from using the fastmap, so
+			   that is all we do.  */
+		case duplicate:
+			bufp->can_be_null = 1;
+			return 0;
+
+
+			/* Following are the cases which match a character.  These end
+			   with `break'.  */
+
+		case exactn:
+			fastmap[p[1]] = 1;
+			break;
+
+
+		case charset:
+			for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
+				if (p[j / BYTEWIDTH] &
+				    (1 << (j % BYTEWIDTH)))
+					fastmap[j] = 1;
+			break;
+
+
+		case charset_not:
+			/* Chars beyond end of map must be allowed.  */
+			for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
+				fastmap[j] = 1;
+
+			for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
+				if (!
+				    (p[j / BYTEWIDTH] &
+				     (1 << (j % BYTEWIDTH))))
+					fastmap[j] = 1;
+			break;
+
+
+		case wordchar:
+			for (j = 0; j < (1 << BYTEWIDTH); j++)
+				if (SYNTAX(j) == Sword)
+					fastmap[j] = 1;
+			break;
+
+
+		case notwordchar:
+			for (j = 0; j < (1 << BYTEWIDTH); j++)
+				if (SYNTAX(j) != Sword)
+					fastmap[j] = 1;
+			break;
+
+
+		case anychar:
+			/* `.' matches anything ...  */
+			for (j = 0; j < (1 << BYTEWIDTH); j++)
+				fastmap[j] = 1;
+
+			/* ... except perhaps newline.  */
+			if (!(bufp->syntax & RE_DOT_NEWLINE))
+				fastmap['\n'] = 0;
+
+			/* Return if we have already set `can_be_null'; if we have,
+			   then the fastmap is irrelevant.  Something's wrong here.  */
+			else if (bufp->can_be_null)
+				return 0;
+
+			/* Otherwise, have to check alternative paths.  */
+			break;
+
+		case no_op:
+		case begline:
+		case endline:
+		case begbuf:
+		case endbuf:
+		case wordbound:
+		case notwordbound:
+		case wordbeg:
+		case wordend:
+		case push_dummy_failure:
+			continue;
+
+
+		case jump_n:
+		case pop_failure_jump:
+		case maybe_pop_jump:
+		case jump:
+		case jump_past_alt:
+		case dummy_failure_jump:
+			EXTRACT_NUMBER_AND_INCR(j, p);
+			p += j;
+			if (j > 0)
+				continue;
+
+			/* Jump backward implies we just went through the body of a
+			   loop and matched nothing.  Opcode jumped to should be
+			   `on_failure_jump' or `succeed_n'.  Just treat it like an
+			   ordinary jump.  For a * loop, it has pushed its failure
+			   point already; if so, discard that as redundant.  */
+			if ((re_opcode_t) * p != on_failure_jump
+			    && (re_opcode_t) * p != succeed_n)
+				continue;
+
+			p++;
+			EXTRACT_NUMBER_AND_INCR(j, p);
+			p += j;
+
+			/* If what's on the stack is where we are now, pop it.  */
+			if (!FAIL_STACK_EMPTY()
+			    && fail_stack.stack[fail_stack.avail - 1] == p)
+				fail_stack.avail--;
+
+			continue;
+
+
+		case on_failure_jump:
+		case on_failure_keep_string_jump:
+		      handle_on_failure_jump:
+			EXTRACT_NUMBER_AND_INCR(j, p);
+
+			/* For some patterns, e.g., `(a?)?', `p+j' here points to the
+			   end of the pattern.  We don't want to push such a point,
+			   since when we restore it above, entering the switch will
+			   increment `p' past the end of the pattern.  We don't need
+			   to push such a point since we obviously won't find any more
+			   fastmap entries beyond `pend'.  Such a pattern can match
+			   the null string, though.  */
+			if (p + j < pend) {
+				if (!PUSH_PATTERN_OP(p + j, fail_stack))
+					return -2;
+			} else
+				bufp->can_be_null = 1;
+
+			if (succeed_n_p) {
+				EXTRACT_NUMBER_AND_INCR(k, p);	/* Skip the n.  */
+				succeed_n_p = false;
+			}
+
+			continue;
+
+
+		case succeed_n:
+			/* Get to the number of times to succeed.  */
+			p += 2;
+
+			/* Increment p past the n for when k != 0.  */
+			EXTRACT_NUMBER_AND_INCR(k, p);
+			if (k == 0) {
+				p -= 4;
+				succeed_n_p = true;	/* Spaghetti code alert.  */
+				goto handle_on_failure_jump;
+			}
+			continue;
+
+
+		case set_number_at:
+			p += 4;
+			continue;
+
+
+		case start_memory:
+		case stop_memory:
+			p += 2;
+			continue;
+
+
+		default:
+			abort();	/* We have listed all the cases.  */
+		}		/* switch *p++ */
+
+		/* Getting here means we have found the possible starting
+		   characters for one path of the pattern -- and that the empty
+		   string does not match.  We need not follow this path further.
+		   Instead, look at the next alternative (remembered on the
+		   stack), or quit if no more.  The test at the top of the loop
+		   does these things.  */
+		path_can_be_null = false;
+		p = pend;
+	}			/* while p */
+
+	/* Set `can_be_null' for the last path (also the first path, if the
+	   pattern is empty).  */
+	bufp->can_be_null |= path_can_be_null;
+	return 0;
+}				/* re_compile_fastmap */
+
+/* Set REGS to hold NUM_REGS registers, storing them in STARTS and
+   ENDS.  Subsequent matches using PATTERN_BUFFER and REGS will use
+   this memory for recording register information.  STARTS and ENDS
+   must be allocated using the malloc library routine, and must each
+   be at least NUM_REGS * sizeof (regoff_t) bytes long.
+
+   If NUM_REGS == 0, then subsequent matches should allocate their own
+   register data.
+
+   Unless this function is called, the first search or match using
+   PATTERN_BUFFER will allocate its own register data, without
+   freeing the old data.  */
+
+void re_set_registers(bufp, regs, num_regs, starts, ends)
+struct re_pattern_buffer *bufp;
+struct re_registers *regs;
+unsigned num_regs;
+regoff_t *starts, *ends;
+{
+	if (num_regs) {
+		bufp->regs_allocated = REGS_REALLOCATE;
+		regs->num_regs = num_regs;
+		regs->start = starts;
+		regs->end = ends;
+	} else {
+		bufp->regs_allocated = REGS_UNALLOCATED;
+		regs->num_regs = 0;
+		regs->start = regs->end = 0;
+	}
+}
+
+/* Searching routines.  */
+
+/* Like re_search_2, below, but only one string is specified, and
+   doesn't let you say where to stop matching. */
+
+int re_search(bufp, string, size, startpos, range, regs)
+struct re_pattern_buffer *bufp;
+const char *string;
+int size, startpos, range;
+struct re_registers *regs;
+{
+	return re_search_2(bufp, NULL, 0, string, size, startpos, range,
+			   regs, size);
+}
+
+
+/* Using the compiled pattern in BUFP->buffer, first tries to match the
+   virtual concatenation of STRING1 and STRING2, starting first at index
+   STARTPOS, then at STARTPOS + 1, and so on.
+
+   STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
+
+   RANGE is how far to scan while trying to match.  RANGE = 0 means try
+   only at STARTPOS; in general, the last start tried is STARTPOS +
+   RANGE.
+
+   In REGS, return the indices of the virtual concatenation of STRING1
+   and STRING2 that matched the entire BUFP->buffer and its contained
+   subexpressions.
+
+   Do not consider matching one past the index STOP in the virtual
+   concatenation of STRING1 and STRING2.
+
+   We return either the position in the strings at which the match was
+   found, -1 if no match, or -2 if error (such as failure
+   stack overflow).  */
+
+int
+re_search_2(bufp, string1, size1, string2, size2, startpos, range, regs,
+	    stop)
+struct re_pattern_buffer *bufp;
+const char *string1, *string2;
+int size1, size2;
+int startpos;
+int range;
+struct re_registers *regs;
+int stop;
+{
+	int val;
+	register char *fastmap = bufp->fastmap;
+	register char *translate = bufp->translate;
+	int total_size = size1 + size2;
+	int endpos = startpos + range;
+
+	/* Check for out-of-range STARTPOS.  */
+	if (startpos < 0 || startpos > total_size)
+		return -1;
+
+	/* Fix up RANGE if it might eventually take us outside
+	   the virtual concatenation of STRING1 and STRING2.  */
+	if (endpos < -1)
+		range = -1 - startpos;
+	else if (endpos > total_size)
+		range = total_size - startpos;
+
+	/* If the search isn't to be a backwards one, don't waste time in a
+	   search for a pattern that must be anchored.  */
+	if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf
+	    && range > 0) {
+		if (startpos > 0)
+			return -1;
+		else
+			range = 1;
+	}
+
+	/* Update the fastmap now if not correct already.  */
+	if (fastmap && !bufp->fastmap_accurate)
+		if (re_compile_fastmap(bufp) == -2)
+			return -2;
+
+	/* Loop through the string, looking for a place to start matching.  */
+	for (;;) {
+		/* If a fastmap is supplied, skip quickly over characters that
+		   cannot be the start of a match.  If the pattern can match the
+		   null string, however, we don't need to skip characters; we want
+		   the first null string.  */
+		if (fastmap && startpos < total_size && !bufp->can_be_null) {
+			if (range > 0) {	/* Searching forwards.  */
+				register const char *d;
+				register int lim = 0;
+				int irange = range;
+
+				if (startpos < size1
+				    && startpos + range >= size1)
+					lim = range - (size1 - startpos);
+
+				d = (startpos >=
+				     size1 ? string2 - size1 : string1) +
+				    startpos;
+
+				/* Written out as an if-else to avoid testing `translate'
+				   inside the loop.  */
+				if (translate)
+					while (range > lim
+					       && !fastmap[(unsigned char)
+							   translate[(unsigned char) *d++]])
+						range--;
+				else
+					while (range > lim
+					       && !fastmap[(unsigned char)
+							   *d++])
+						range--;
+
+				startpos += irange - range;
+			} else {	/* Searching backwards.  */
+
+				register char c = (size1 == 0
+						   || startpos >=
+						   size1 ? string2[startpos
+								   - size1]
+						   : string1[startpos]);
+
+				if (!fastmap[(unsigned char) TRANSLATE(c)])
+					goto advance;
+			}
+		}
+
+		/* If can't match the null string, and that's all we have left, fail.  */
+		if (range >= 0 && startpos == total_size && fastmap
+		    && !bufp->can_be_null)
+			return -1;
+
+		val = re_match_2(bufp, string1, size1, string2, size2,
+				 startpos, regs, stop);
+		if (val >= 0)
+			return startpos;
+
+		if (val == -2)
+			return -2;
+
+	      advance:
+		if (!range)
+			break;
+		else if (range > 0) {
+			range--;
+			startpos++;
+		} else {
+			range++;
+			startpos--;
+		}
+	}
+	return -1;
+}				/* re_search_2 */
+
+/* Structure for per-register (a.k.a. per-group) information.
+   This must not be longer than one word, because we push this value
+   onto the failure stack.  Other register information, such as the
+   starting and ending positions (which are addresses), and the list of
+   inner groups (which is a bits list) are maintained in separate
+   variables.
+
+   We are making a (strictly speaking) nonportable assumption here: that
+   the compiler will pack our bit fields into something that fits into
+   the type of `word', i.e., is something that fits into one item on the
+   failure stack.  */
+
+/* Declarations and macros for re_match_2.  */
+
+typedef union {
+	fail_stack_elt_t word;
+	struct {
+		/* This field is one if this group can match the empty string,
+		   zero if not.  If not yet determined,  `MATCH_NULL_UNSET_VALUE'.  */
+#define MATCH_NULL_UNSET_VALUE 3
+		unsigned match_null_string_p:2;
+		unsigned is_active:1;
+		unsigned matched_something:1;
+		unsigned ever_matched_something:1;
+	} bits;
+} register_info_type;
+
+#define REG_MATCH_NULL_STRING_P(R)  ((R).bits.match_null_string_p)
+#define IS_ACTIVE(R)  ((R).bits.is_active)
+#define MATCHED_SOMETHING(R)  ((R).bits.matched_something)
+#define EVER_MATCHED_SOMETHING(R)  ((R).bits.ever_matched_something)
+
+static boolean group_match_null_string_p (unsigned char **p,
+					  unsigned char *end,
+					  register_info_type *
+					  reg_info);
+
+static boolean alt_match_null_string_p (unsigned char *p, unsigned char *end,
+					register_info_type * reg_info);
+
+static boolean common_op_match_null_string_p (unsigned char **p,
+					      unsigned char *end,
+					      register_info_type * reg_info);
+
+static int bcmp_translate (const char *s1, const char *s2,
+			   int len, char *translate);
+
+/* Call this when have matched a real character; it sets `matched' flags
+   for the subexpressions which we are currently inside.  Also records
+   that those subexprs have matched.  */
+#define SET_REGS_MATCHED()						\
+  do									\
+    {									\
+      active_reg_t r;							\
+      for (r = lowest_active_reg; r <= highest_active_reg; r++)		\
+        {								\
+          MATCHED_SOMETHING (reg_info[r])				\
+            = EVER_MATCHED_SOMETHING (reg_info[r])			\
+            = 1;							\
+        }								\
+    }									\
+  while (0)
+
+
+/* This converts PTR, a pointer into one of the search strings `string1'
+   and `string2' into an offset from the beginning of that string.  */
+#define POINTER_TO_OFFSET(ptr)						\
+  (FIRST_STRING_P (ptr) ? (ptr) - string1 : (ptr) - string2 + size1)
+
+/* Registers are set to a sentinel when they haven't yet matched.  */
+#define REG_UNSET_VALUE ((char *) -1)
+#define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
+
+
+/* Macros for dealing with the split strings in re_match_2.  */
+
+#define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
+
+/* Call before fetching a character with *d.  This switches over to
+   string2 if necessary.  */
+#define PREFETCH()							\
+  while (d == dend)						    	\
+    {									\
+      /* End of string2 => fail.  */					\
+      if (dend == end_match_2) 						\
+        goto fail;							\
+      /* End of string1 => advance to string2.  */ 			\
+      d = string2;						        \
+      dend = end_match_2;						\
+    }
+
+
+/* Test if at very beginning or at very end of the virtual concatenation
+   of `string1' and `string2'.  If only one string, it's `string2'.  */
+#define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
+#define AT_STRINGS_END(d) ((d) == end2)
+
+
+/* Test if D points to a character which is word-constituent.  We have
+   two special cases to check for: if past the end of string1, look at
+   the first character in string2; and if before the beginning of
+   string2, look at the last character in string1.  */
+#define WORDCHAR_P(d)							\
+  (SYNTAX ((d) == end1 ? *string2					\
+           : (d) == string2 - 1 ? *(end1 - 1) : *(d))			\
+   == Sword)
+
+/* Test if the character before D and the one at D differ with respect
+   to being word-constituent.  */
+#define AT_WORD_BOUNDARY(d)						\
+  (AT_STRINGS_BEG (d) || AT_STRINGS_END (d)				\
+   || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
+
+
+/* Free everything we malloc.  */
+#define FREE_VARIABLES() alloca (0)
+
+/* These values must meet several constraints.  They must not be valid
+   register values; since we have a limit of 255 registers (because
+   we use only one byte in the pattern for the register number), we can
+   use numbers larger than 255.  They must differ by 1, because of
+   NUM_FAILURE_ITEMS above.  And the value for the lowest register must
+   be larger than the value for the highest register, so we do not try
+   to actually save any registers when none are active.  */
+#define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
+#define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
+
+/* Matching routines.  */
+
+/* re_match is like re_match_2 except it takes only a single string.  */
+
+int re_match(bufp, string, size, pos, regs)
+struct re_pattern_buffer *bufp;
+const char *string;
+int size, pos;
+struct re_registers *regs;
+{
+	return re_match_2(bufp, NULL, 0, string, size, pos, regs, size);
+}
+
+/* re_match_2 matches the compiled pattern in BUFP against the
+   the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
+   and SIZE2, respectively).  We start matching at POS, and stop
+   matching at STOP.
+
+   If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
+   store offsets for the substring each group matched in REGS.  See the
+   documentation for exactly how many groups we fill.
+
+   We return -1 if no match, -2 if an internal error (such as the
+   failure stack overflowing).  Otherwise, we return the length of the
+   matched substring.  */
+
+int re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop)
+struct re_pattern_buffer *bufp;
+const char *string1, *string2;
+int size1, size2;
+int pos;
+struct re_registers *regs;
+int stop;
+{
+	/* General temporaries.  */
+	int mcnt;
+	unsigned char *p1;
+
+	/* Just past the end of the corresponding string.  */
+	const char *end1, *end2;
+
+	/* Pointers into string1 and string2, just past the last characters in
+	   each to consider matching.  */
+	const char *end_match_1, *end_match_2;
+
+	/* Where we are in the data, and the end of the current string.  */
+	const char *d, *dend;
+
+	/* Where we are in the pattern, and the end of the pattern.  */
+	unsigned char *p = bufp->buffer;
+	register unsigned char *pend = p + bufp->used;
+
+	/* We use this to map every character in the string.  */
+	char *translate = bufp->translate;
+
+	/* Failure point stack.  Each place that can handle a failure further
+	   down the line pushes a failure point on this stack.  It consists of
+	   restart, regend, and reg_info for all registers corresponding to
+	   the subexpressions we're currently inside, plus the number of such
+	   registers, and, finally, two char *'s.  The first char * is where
+	   to resume scanning the pattern; the second one is where to resume
+	   scanning the strings.  If the latter is zero, the failure point is
+	   a ``dummy''; if a failure happens and the failure point is a dummy,
+	   it gets discarded and the next next one is tried.  */
+	fail_stack_type fail_stack;
+
+	/* We fill all the registers internally, independent of what we
+	   return, for use in backreferences.  The number here includes
+	   an element for register zero.  */
+	size_t num_regs = bufp->re_nsub + 1;
+
+	/* The currently active registers.  */
+	active_reg_t lowest_active_reg = NO_LOWEST_ACTIVE_REG;
+	active_reg_t highest_active_reg = NO_HIGHEST_ACTIVE_REG;
+
+	/* Information on the contents of registers. These are pointers into
+	   the input strings; they record just what was matched (on this
+	   attempt) by a subexpression part of the pattern, that is, the
+	   regnum-th regstart pointer points to where in the pattern we began
+	   matching and the regnum-th regend points to right after where we
+	   stopped matching the regnum-th subexpression.  (The zeroth register
+	   keeps track of what the whole pattern matches.)  */
+	const char **regstart = 0, **regend = 0;
+
+	/* If a group that's operated upon by a repetition operator fails to
+	   match anything, then the register for its start will need to be
+	   restored because it will have been set to wherever in the string we
+	   are when we last see its open-group operator.  Similarly for a
+	   register's end.  */
+	const char **old_regstart = 0, **old_regend = 0;
+
+	/* The is_active field of reg_info helps us keep track of which (possibly
+	   nested) subexpressions we are currently in. The matched_something
+	   field of reg_info[reg_num] helps us tell whether or not we have
+	   matched any of the pattern so far this time through the reg_num-th
+	   subexpression.  These two fields get reset each time through any
+	   loop their register is in.  */
+	register_info_type *reg_info = 0;
+
+	/* The following record the register info as found in the above
+	   variables when we find a match better than any we've seen before.
+	   This happens as we backtrack through the failure points, which in
+	   turn happens only if we have not yet matched the entire string. */
+	unsigned best_regs_set = false;
+	const char **best_regstart = 0, **best_regend = 0;
+
+	/* Logically, this is `best_regend[0]'.  But we don't want to have to
+	   allocate space for that if we're not allocating space for anything
+	   else (see below).  Also, we never need info about register 0 for
+	   any of the other register vectors, and it seems rather a kludge to
+	   treat `best_regend' differently than the rest.  So we keep track of
+	   the end of the best match so far in a separate variable.  We
+	   initialize this to NULL so that when we backtrack the first time
+	   and need to test it, it's not garbage.  */
+	const char *match_end = NULL;
+
+	/* Used when we pop values we don't care about.  */
+	const char **reg_dummy = 0;
+	register_info_type *reg_info_dummy = 0;
+
+	DEBUG_PRINT1("\n\nEntering re_match_2.\n");
+
+	INIT_FAIL_STACK();
+
+	/* Do not bother to initialize all the register variables if there are
+	   no groups in the pattern, as it takes a fair amount of time.  If
+	   there are groups, we include space for register 0 (the whole
+	   pattern), even though we never use it, since it simplifies the
+	   array indexing.  We should fix this.  */
+	if (bufp->re_nsub) {
+		regstart = REGEX_TALLOC(num_regs, const char *);
+		regend = REGEX_TALLOC(num_regs, const char *);
+		old_regstart = REGEX_TALLOC(num_regs, const char *);
+		old_regend = REGEX_TALLOC(num_regs, const char *);
+		best_regstart = REGEX_TALLOC(num_regs, const char *);
+		best_regend = REGEX_TALLOC(num_regs, const char *);
+		reg_info = REGEX_TALLOC(num_regs, register_info_type);
+		reg_dummy = REGEX_TALLOC(num_regs, const char *);
+		reg_info_dummy =
+		    REGEX_TALLOC(num_regs, register_info_type);
+
+		if (!
+		    (regstart && regend && old_regstart && old_regend
+		     && reg_info && best_regstart && best_regend
+		     && reg_dummy && reg_info_dummy)) {
+			FREE_VARIABLES();
+			return -2;
+		}
+	}
+
+	/* The starting position is bogus.  */
+	if (pos < 0 || pos > size1 + size2) {
+		FREE_VARIABLES();
+		return -1;
+	}
+
+	/* Initialize subexpression text positions to -1 to mark ones that no
+	   start_memory/stop_memory has been seen for. Also initialize the
+	   register information struct.  */
+	for (mcnt = 1; mcnt < num_regs; mcnt++) {
+		regstart[mcnt] = regend[mcnt]
+		    = old_regstart[mcnt] = old_regend[mcnt] =
+		    REG_UNSET_VALUE;
+
+		REG_MATCH_NULL_STRING_P(reg_info[mcnt]) =
+		    MATCH_NULL_UNSET_VALUE;
+		IS_ACTIVE(reg_info[mcnt]) = 0;
+		MATCHED_SOMETHING(reg_info[mcnt]) = 0;
+		EVER_MATCHED_SOMETHING(reg_info[mcnt]) = 0;
+	}
+
+	/* We move `string1' into `string2' if the latter's empty -- but not if
+	   `string1' is null.  */
+	if (size2 == 0 && string1 != NULL) {
+		string2 = string1;
+		size2 = size1;
+		string1 = 0;
+		size1 = 0;
+	}
+	end1 = string1 + size1;
+	end2 = string2 + size2;
+
+	/* Compute where to stop matching, within the two strings.  */
+	if (stop <= size1) {
+		end_match_1 = string1 + stop;
+		end_match_2 = string2;
+	} else {
+		end_match_1 = end1;
+		end_match_2 = string2 + stop - size1;
+	}
+
+	/* `p' scans through the pattern as `d' scans through the data.
+	   `dend' is the end of the input string that `d' points within.  `d'
+	   is advanced into the following input string whenever necessary, but
+	   this happens before fetching; therefore, at the beginning of the
+	   loop, `d' can be pointing at the end of a string, but it cannot
+	   equal `string2'.  */
+	if (size1 > 0 && pos <= size1) {
+		d = string1 + pos;
+		dend = end_match_1;
+	} else {
+		d = string2 + pos - size1;
+		dend = end_match_2;
+	}
+
+	DEBUG_PRINT1("The compiled pattern is: ");
+	DEBUG_PRINT_COMPILED_PATTERN(bufp, p, pend);
+	DEBUG_PRINT1("The string to match is: `");
+	DEBUG_PRINT_DOUBLE_STRING(d, string1, size1, string2, size2);
+	DEBUG_PRINT1("'\n");
+
+	/* This loops over pattern commands.  It exits by returning from the
+	   function if the match is complete, or it drops through if the match
+	   fails at this starting point in the input data.  */
+	for (;;) {
+		DEBUG_PRINT2("\n0x%x: ", p);
+
+		if (p == pend) {	/* End of pattern means we might have succeeded.  */
+			DEBUG_PRINT1("end of pattern ... ");
+
+			/* If we haven't matched the entire string, and we want the
+			   longest match, try backtracking.  */
+			if (d != end_match_2) {
+				DEBUG_PRINT1("backtracking.\n");
+
+				if (!FAIL_STACK_EMPTY()) {	/* More failure points to try.  */
+					boolean same_str_p =
+					    (FIRST_STRING_P(match_end)
+					     == MATCHING_IN_FIRST_STRING);
+
+					/* If exceeds best match so far, save it.  */
+					if (!best_regs_set
+					    || (same_str_p
+						&& d > match_end)
+					    || (!same_str_p
+						&&
+						!MATCHING_IN_FIRST_STRING))
+					{
+						best_regs_set = true;
+						match_end = d;
+
+						DEBUG_PRINT1
+						    ("\nSAVING match as best so far.\n");
+
+						for (mcnt = 1;
+						     mcnt < num_regs;
+						     mcnt++) {
+							best_regstart[mcnt]
+							    =
+							    regstart[mcnt];
+							best_regend[mcnt] =
+							    regend[mcnt];
+						}
+					}
+					goto fail;
+				}
+
+				/* If no failure points, don't restore garbage.  */
+				else if (best_regs_set) {
+				      restore_best_regs:
+					/* Restore best match.  It may happen that `dend ==
+					   end_match_1' while the restored d is in string2.
+					   For example, the pattern `x.*y.*z' against the
+					   strings `x-' and `y-z-', if the two strings are
+					   not consecutive in memory.  */
+					DEBUG_PRINT1
+					    ("Restoring best registers.\n");
+
+					d = match_end;
+					dend = ((d >= string1 && d <= end1)
+						? end_match_1 :
+						end_match_2);
+
+					for (mcnt = 1; mcnt < num_regs;
+					     mcnt++) {
+						regstart[mcnt] =
+						    best_regstart[mcnt];
+						regend[mcnt] =
+						    best_regend[mcnt];
+					}
+				}
+			}
+			/* d != end_match_2 */
+			DEBUG_PRINT1("Accepting match.\n");
+
+			/* If caller wants register contents data back, do it.  */
+			if (regs && !bufp->no_sub) {
+				/* Have the register data arrays been allocated?  */
+				if (bufp->regs_allocated == REGS_UNALLOCATED) {	/* No.  So allocate them with malloc.  We need one
+										   extra element beyond `num_regs' for the `-1' marker
+										   GNU code uses.  */
+					regs->num_regs =
+					    MAX(RE_NREGS, num_regs + 1);
+					regs->start =
+					    TALLOC(regs->num_regs,
+						   regoff_t);
+					regs->end =
+					    TALLOC(regs->num_regs,
+						   regoff_t);
+					if (regs->start == NULL
+					    || regs->end == NULL)
+						return -2;
+					bufp->regs_allocated =
+					    REGS_REALLOCATE;
+				} else if (bufp->regs_allocated == REGS_REALLOCATE) {	/* Yes.  If we need more elements than were already
+											   allocated, reallocate them.  If we need fewer, just
+											   leave it alone.  */
+					if (regs->num_regs < num_regs + 1) {
+						regs->num_regs =
+						    num_regs + 1;
+						RETALLOC(regs->start,
+							 regs->num_regs,
+							 regoff_t);
+						RETALLOC(regs->end,
+							 regs->num_regs,
+							 regoff_t);
+						if (regs->start == NULL
+						    || regs->end == NULL)
+							return -2;
+					}
+				} else {
+					/* These braces fend off a "empty body in an else-statement"
+					   warning under GCC when assert expands to nothing.  */
+					assert(bufp->regs_allocated ==
+					       REGS_FIXED);
+				}
+
+				/* Convert the pointer data in `regstart' and `regend' to
+				   indices.  Register zero has to be set differently,
+				   since we haven't kept track of any info for it.  */
+				if (regs->num_regs > 0) {
+					regs->start[0] = pos;
+					regs->end[0] =
+					    (MATCHING_IN_FIRST_STRING ? d -
+					     string1 : d - string2 +
+					     size1);
+				}
+
+				/* Go through the first `min (num_regs, regs->num_regs)'
+				   registers, since that is all we initialized.  */
+				for (mcnt = 1;
+				     mcnt < MIN(num_regs, regs->num_regs);
+				     mcnt++) {
+					if (REG_UNSET(regstart[mcnt])
+					    || REG_UNSET(regend[mcnt]))
+						regs->start[mcnt] =
+						    regs->end[mcnt] = -1;
+					else {
+						regs->start[mcnt] =
+						    POINTER_TO_OFFSET
+						    (regstart[mcnt]);
+						regs->end[mcnt] =
+						    POINTER_TO_OFFSET
+						    (regend[mcnt]);
+					}
+				}
+
+				/* If the regs structure we return has more elements than
+				   were in the pattern, set the extra elements to -1.  If
+				   we (re)allocated the registers, this is the case,
+				   because we always allocate enough to have at least one
+				   -1 at the end.  */
+				for (mcnt = num_regs;
+				     mcnt < regs->num_regs; mcnt++)
+					regs->start[mcnt] =
+					    regs->end[mcnt] = -1;
+			}
+			/* regs && !bufp->no_sub */
+			FREE_VARIABLES();
+			DEBUG_PRINT4
+			    ("%u failure points pushed, %u popped (%u remain).\n",
+			     nfailure_points_pushed,
+			     nfailure_points_popped,
+			     nfailure_points_pushed -
+			     nfailure_points_popped);
+			DEBUG_PRINT2("%u registers pushed.\n",
+				     num_regs_pushed);
+
+			mcnt = d - pos - (MATCHING_IN_FIRST_STRING
+					  ? string1 : string2 - size1);
+
+			DEBUG_PRINT2("Returning %d from re_match_2.\n",
+				     mcnt);
+
+			return mcnt;
+		}
+
+		/* Otherwise match next pattern command.  */
+		switch ((re_opcode_t) * p++) {
+			/* Ignore these.  Used to ignore the n of succeed_n's which
+			   currently have n == 0.  */
+		case no_op:
+			DEBUG_PRINT1("EXECUTING no_op.\n");
+			break;
+
+
+			/* Match the next n pattern characters exactly.  The following
+			   byte in the pattern defines n, and the n bytes after that
+			   are the characters to match.  */
+		case exactn:
+			mcnt = *p++;
+			DEBUG_PRINT2("EXECUTING exactn %d.\n", mcnt);
+
+			/* This is written out as an if-else so we don't waste time
+			   testing `translate' inside the loop.  */
+			if (translate) {
+				do {
+					PREFETCH();
+					if (translate[(unsigned char) *d++]
+					    != (char) *p++)
+						goto fail;
+				}
+				while (--mcnt);
+			} else {
+				do {
+					PREFETCH();
+					if (*d++ != (char) *p++)
+						goto fail;
+				}
+				while (--mcnt);
+			}
+			SET_REGS_MATCHED();
+			break;
+
+
+			/* Match any character except possibly a newline or a null.  */
+		case anychar:
+			DEBUG_PRINT1("EXECUTING anychar.\n");
+
+			PREFETCH();
+
+			if ((!(bufp->syntax & RE_DOT_NEWLINE)
+			     && TRANSLATE(*d) == '\n')
+			    || (bufp->syntax & RE_DOT_NOT_NULL
+				&& TRANSLATE(*d) == '\000'))
+				goto fail;
+
+			SET_REGS_MATCHED();
+			DEBUG_PRINT2("  Matched `%d'.\n", *d);
+			d++;
+			break;
+
+
+		case charset:
+		case charset_not:
+			{
+				register unsigned char c;
+				boolean not =
+				    (re_opcode_t) * (p - 1) == charset_not;
+
+				DEBUG_PRINT2("EXECUTING charset%s.\n",
+					     not ? "_not" : "");
+
+				PREFETCH();
+				c = TRANSLATE(*d);	/* The character to match.  */
+
+				/* Cast to `unsigned' instead of `unsigned char' in case the
+				   bit list is a full 32 bytes long.  */
+				if (c < (unsigned) (*p * BYTEWIDTH)
+				    && p[1 +
+					 c / BYTEWIDTH] & (1 << (c %
+								 BYTEWIDTH)))
+					not = !not;
+
+				p += 1 + *p;
+
+				if (!not)
+					goto fail;
+
+				SET_REGS_MATCHED();
+				d++;
+				break;
+			}
+
+
+			/* The beginning of a group is represented by start_memory.
+			   The arguments are the register number in the next byte, and the
+			   number of groups inner to this one in the next.  The text
+			   matched within the group is recorded (in the internal
+			   registers data structure) under the register number.  */
+		case start_memory:
+			DEBUG_PRINT3("EXECUTING start_memory %d (%d):\n",
+				     *p, p[1]);
+
+			/* Find out if this group can match the empty string.  */
+			p1 = p;	/* To send to group_match_null_string_p.  */
+
+			if (REG_MATCH_NULL_STRING_P(reg_info[*p]) ==
+			    MATCH_NULL_UNSET_VALUE)
+				REG_MATCH_NULL_STRING_P(reg_info[*p])
+				    = group_match_null_string_p(&p1, pend,
+								reg_info);
+
+			/* Save the position in the string where we were the last time
+			   we were at this open-group operator in case the group is
+			   operated upon by a repetition operator, e.g., with `(a*)*b'
+			   against `ab'; then we want to ignore where we are now in
+			   the string in case this attempt to match fails.  */
+			old_regstart[*p] =
+			    REG_MATCH_NULL_STRING_P(reg_info[*p])
+			    ? REG_UNSET(regstart[*p]) ? d : regstart[*p]
+			    : regstart[*p];
+			DEBUG_PRINT2("  old_regstart: %d\n",
+				     POINTER_TO_OFFSET(old_regstart[*p]));
+
+			regstart[*p] = d;
+			DEBUG_PRINT2("  regstart: %d\n",
+				     POINTER_TO_OFFSET(regstart[*p]));
+
+			IS_ACTIVE(reg_info[*p]) = 1;
+			MATCHED_SOMETHING(reg_info[*p]) = 0;
+
+			/* This is the new highest active register.  */
+			highest_active_reg = *p;
+
+			/* If nothing was active before, this is the new lowest active
+			   register.  */
+			if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
+				lowest_active_reg = *p;
+
+			/* Move past the register number and inner group count.  */
+			p += 2;
+			break;
+
+
+			/* The stop_memory opcode represents the end of a group.  Its
+			   arguments are the same as start_memory's: the register
+			   number, and the number of inner groups.  */
+		case stop_memory:
+			DEBUG_PRINT3("EXECUTING stop_memory %d (%d):\n",
+				     *p, p[1]);
+
+			/* We need to save the string position the last time we were at
+			   this close-group operator in case the group is operated
+			   upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
+			   against `aba'; then we want to ignore where we are now in
+			   the string in case this attempt to match fails.  */
+			old_regend[*p] =
+			    REG_MATCH_NULL_STRING_P(reg_info[*p])
+			    ? REG_UNSET(regend[*p]) ? d : regend[*p]
+			    : regend[*p];
+			DEBUG_PRINT2("      old_regend: %d\n",
+				     POINTER_TO_OFFSET(old_regend[*p]));
+
+			regend[*p] = d;
+			DEBUG_PRINT2("      regend: %d\n",
+				     POINTER_TO_OFFSET(regend[*p]));
+
+			/* This register isn't active anymore.  */
+			IS_ACTIVE(reg_info[*p]) = 0;
+
+			/* If this was the only register active, nothing is active
+			   anymore.  */
+			if (lowest_active_reg == highest_active_reg) {
+				lowest_active_reg = NO_LOWEST_ACTIVE_REG;
+				highest_active_reg = NO_HIGHEST_ACTIVE_REG;
+			} else {	/* We must scan for the new highest active register, since
+					   it isn't necessarily one less than now: consider
+					   (a(b)c(d(e)f)g).  When group 3 ends, after the f), the
+					   new highest active register is 1.  */
+				unsigned char r = *p - 1;
+				while (r > 0 && !IS_ACTIVE(reg_info[r]))
+					r--;
+
+				/* If we end up at register zero, that means that we saved
+				   the registers as the result of an `on_failure_jump', not
+				   a `start_memory', and we jumped to past the innermost
+				   `stop_memory'.  For example, in ((.)*) we save
+				   registers 1 and 2 as a result of the *, but when we pop
+				   back to the second ), we are at the stop_memory 1.
+				   Thus, nothing is active.  */
+				if (r == 0) {
+					lowest_active_reg =
+					    NO_LOWEST_ACTIVE_REG;
+					highest_active_reg =
+					    NO_HIGHEST_ACTIVE_REG;
+				} else
+					highest_active_reg = r;
+			}
+
+			/* If just failed to match something this time around with a
+			   group that's operated on by a repetition operator, try to
+			   force exit from the ``loop'', and restore the register
+			   information for this group that we had before trying this
+			   last match.  */
+			if ((!MATCHED_SOMETHING(reg_info[*p])
+			     || (re_opcode_t) p[-3] == start_memory)
+			    && (p + 2) < pend) {
+				boolean is_a_jump_n = false;
+
+				p1 = p + 2;
+				mcnt = 0;
+				switch ((re_opcode_t) * p1++) {
+				case jump_n:
+					is_a_jump_n = true;
+				case pop_failure_jump:
+				case maybe_pop_jump:
+				case jump:
+				case dummy_failure_jump:
+					EXTRACT_NUMBER_AND_INCR(mcnt, p1);
+					if (is_a_jump_n)
+						p1 += 2;
+					break;
+
+				default:
+					/* do nothing */ ;
+				}
+				p1 += mcnt;
+
+				/* If the next operation is a jump backwards in the pattern
+				   to an on_failure_jump right before the start_memory
+				   corresponding to this stop_memory, exit from the loop
+				   by forcing a failure after pushing on the stack the
+				   on_failure_jump's jump in the pattern, and d.  */
+				if (mcnt < 0
+				    && (re_opcode_t) * p1 ==
+				    on_failure_jump
+				    && (re_opcode_t) p1[3] == start_memory
+				    && p1[4] == *p) {
+					/* If this group ever matched anything, then restore
+					   what its registers were before trying this last
+					   failed match, e.g., with `(a*)*b' against `ab' for
+					   regstart[1], and, e.g., with `((a*)*(b*)*)*'
+					   against `aba' for regend[3].
+
+					   Also restore the registers for inner groups for,
+					   e.g., `((a*)(b*))*' against `aba' (register 3 would
+					   otherwise get trashed).  */
+
+					if (EVER_MATCHED_SOMETHING
+					    (reg_info[*p])) {
+						unsigned r;
+
+						EVER_MATCHED_SOMETHING
+						    (reg_info[*p]) = 0;
+
+						/* Restore this and inner groups' (if any) registers.  */
+						for (r = *p;
+						     r < *p + *(p + 1);
+						     r++) {
+							regstart[r] =
+							    old_regstart
+							    [r];
+
+							/* xx why this test?  */
+							if ((s_reg_t)
+							    old_regend[r]
+							    >=
+							    (s_reg_t)
+							    regstart[r])
+								regend[r] =
+								    old_regend
+								    [r];
+						}
+					}
+					p1++;
+					EXTRACT_NUMBER_AND_INCR(mcnt, p1);
+					PUSH_FAILURE_POINT(p1 + mcnt, d,
+							   -2);
+					PUSH_FAILURE_POINT2(p1 + mcnt, d,
+							    -2);
+
+					goto fail;
+				}
+			}
+
+			/* Move past the register number and the inner group count.  */
+			p += 2;
+			break;
+
+
+			/* \<digit> has been turned into a `duplicate' command which is
+			   followed by the numeric value of <digit> as the register number.  */
+		case duplicate:
+			{
+				register const char *d2, *dend2;
+				int regno = *p++;	/* Get which register to match against.  */
+				DEBUG_PRINT2("EXECUTING duplicate %d.\n",
+					     regno);
+
+				/* Can't back reference a group which we've never matched.  */
+				if (REG_UNSET(regstart[regno])
+				    || REG_UNSET(regend[regno]))
+					goto fail;
+
+				/* Where in input to try to start matching.  */
+				d2 = regstart[regno];
+
+				/* Where to stop matching; if both the place to start and
+				   the place to stop matching are in the same string, then
+				   set to the place to stop, otherwise, for now have to use
+				   the end of the first string.  */
+
+				dend2 = ((FIRST_STRING_P(regstart[regno])
+					  == FIRST_STRING_P(regend[regno]))
+					 ? regend[regno] : end_match_1);
+				for (;;) {
+					/* If necessary, advance to next segment in register
+					   contents.  */
+					while (d2 == dend2) {
+						if (dend2 == end_match_2)
+							break;
+						if (dend2 == regend[regno])
+							break;
+
+						/* End of string1 => advance to string2. */
+						d2 = string2;
+						dend2 = regend[regno];
+					}
+					/* At end of register contents => success */
+					if (d2 == dend2)
+						break;
+
+					/* If necessary, advance to next segment in data.  */
+					PREFETCH();
+
+					/* How many characters left in this segment to match.  */
+					mcnt = dend - d;
+
+					/* Want how many consecutive characters we can match in
+					   one shot, so, if necessary, adjust the count.  */
+					if (mcnt > dend2 - d2)
+						mcnt = dend2 - d2;
+
+					/* Compare that many; failure if mismatch, else move
+					   past them.  */
+					if (translate
+					    ? bcmp_translate(d, d2, mcnt,
+							     translate)
+					    : bcmp(d, d2, mcnt))
+						goto fail;
+					d += mcnt, d2 += mcnt;
+				}
+			}
+			break;
+
+
+			/* begline matches the empty string at the beginning of the string
+			   (unless `not_bol' is set in `bufp'), and, if
+			   `newline_anchor' is set, after newlines.  */
+		case begline:
+			DEBUG_PRINT1("EXECUTING begline.\n");
+
+			if (AT_STRINGS_BEG(d)) {
+				if (!bufp->not_bol)
+					break;
+			} else if (d[-1] == '\n' && bufp->newline_anchor) {
+				break;
+			}
+			/* In all other cases, we fail.  */
+			goto fail;
+
+
+			/* endline is the dual of begline.  */
+		case endline:
+			DEBUG_PRINT1("EXECUTING endline.\n");
+
+			if (AT_STRINGS_END(d)) {
+				if (!bufp->not_eol)
+					break;
+			}
+
+			/* We have to ``prefetch'' the next character.  */
+			else if ((d == end1 ? *string2 : *d) == '\n'
+				 && bufp->newline_anchor) {
+				break;
+			}
+			goto fail;
+
+
+			/* Match at the very beginning of the data.  */
+		case begbuf:
+			DEBUG_PRINT1("EXECUTING begbuf.\n");
+			if (AT_STRINGS_BEG(d))
+				break;
+			goto fail;
+
+
+			/* Match at the very end of the data.  */
+		case endbuf:
+			DEBUG_PRINT1("EXECUTING endbuf.\n");
+			if (AT_STRINGS_END(d))
+				break;
+			goto fail;
+
+
+			/* on_failure_keep_string_jump is used to optimize `.*\n'.  It
+			   pushes NULL as the value for the string on the stack.  Then
+			   `pop_failure_point' will keep the current value for the
+			   string, instead of restoring it.  To see why, consider
+			   matching `foo\nbar' against `.*\n'.  The .* matches the foo;
+			   then the . fails against the \n.  But the next thing we want
+			   to do is match the \n against the \n; if we restored the
+			   string value, we would be back at the foo.
+
+			   Because this is used only in specific cases, we don't need to
+			   check all the things that `on_failure_jump' does, to make
+			   sure the right things get saved on the stack.  Hence we don't
+			   share its code.  The only reason to push anything on the
+			   stack at all is that otherwise we would have to change
+			   `anychar's code to do something besides goto fail in this
+			   case; that seems worse than this.  */
+		case on_failure_keep_string_jump:
+			DEBUG_PRINT1
+			    ("EXECUTING on_failure_keep_string_jump");
+
+			EXTRACT_NUMBER_AND_INCR(mcnt, p);
+			DEBUG_PRINT3(" %d (to 0x%x):\n", mcnt, p + mcnt);
+
+			PUSH_FAILURE_POINT(p + mcnt, NULL, -2);
+			PUSH_FAILURE_POINT2(p + mcnt, NULL, -2);
+			break;
+
+
+			/* Uses of on_failure_jump:
+
+			   Each alternative starts with an on_failure_jump that points
+			   to the beginning of the next alternative.  Each alternative
+			   except the last ends with a jump that in effect jumps past
+			   the rest of the alternatives.  (They really jump to the
+			   ending jump of the following alternative, because tensioning
+			   these jumps is a hassle.)
+
+			   Repeats start with an on_failure_jump that points past both
+			   the repetition text and either the following jump or
+			   pop_failure_jump back to this on_failure_jump.  */
+		case on_failure_jump:
+		      on_failure:
+			DEBUG_PRINT1("EXECUTING on_failure_jump");
+
+			EXTRACT_NUMBER_AND_INCR(mcnt, p);
+			DEBUG_PRINT3(" %d (to 0x%x)", mcnt, p + mcnt);
+
+			/* If this on_failure_jump comes right before a group (i.e.,
+			   the original * applied to a group), save the information
+			   for that group and all inner ones, so that if we fail back
+			   to this point, the group's information will be correct.
+			   For example, in \(a*\)*\1, we need the preceding group,
+			   and in \(\(a*\)b*\)\2, we need the inner group.  */
+
+			/* We can't use `p' to check ahead because we push
+			   a failure point to `p + mcnt' after we do this.  */
+			p1 = p;
+
+			/* We need to skip no_op's before we look for the
+			   start_memory in case this on_failure_jump is happening as
+			   the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
+			   against aba.  */
+			while (p1 < pend && (re_opcode_t) * p1 == no_op)
+				p1++;
+
+			if (p1 < pend
+			    && (re_opcode_t) * p1 == start_memory) {
+				/* We have a new highest active register now.  This will
+				   get reset at the start_memory we are about to get to,
+				   but we will have saved all the registers relevant to
+				   this repetition op, as described above.  */
+				highest_active_reg = *(p1 + 1) + *(p1 + 2);
+				if (lowest_active_reg ==
+				    NO_LOWEST_ACTIVE_REG)
+					lowest_active_reg = *(p1 + 1);
+			}
+
+			DEBUG_PRINT1(":\n");
+			PUSH_FAILURE_POINT(p + mcnt, d, -2);
+			PUSH_FAILURE_POINT2(p + mcnt, d, -2);
+			break;
+
+
+			/* A smart repeat ends with `maybe_pop_jump'.
+			   We change it to either `pop_failure_jump' or `jump'.  */
+		case maybe_pop_jump:
+			EXTRACT_NUMBER_AND_INCR(mcnt, p);
+			DEBUG_PRINT2("EXECUTING maybe_pop_jump %d.\n",
+				     mcnt);
+			{
+				register unsigned char *p2 = p;
+
+				/* Compare the beginning of the repeat with what in the
+				   pattern follows its end. If we can establish that there
+				   is nothing that they would both match, i.e., that we
+				   would have to backtrack because of (as in, e.g., `a*a')
+				   then we can change to pop_failure_jump, because we'll
+				   never have to backtrack.
+
+				   This is not true in the case of alternatives: in
+				   `(a|ab)*' we do need to backtrack to the `ab' alternative
+				   (e.g., if the string was `ab').  But instead of trying to
+				   detect that here, the alternative has put on a dummy
+				   failure point which is what we will end up popping.  */
+
+				/* Skip over open/close-group commands.  */
+				while (p2 + 2 < pend
+				       && ((re_opcode_t) * p2 ==
+					   stop_memory
+					   || (re_opcode_t) * p2 ==
+					   start_memory))
+					p2 += 3;	/* Skip over args, too.  */
+
+				/* If we're at the end of the pattern, we can change.  */
+				if (p2 == pend) {
+					/* Consider what happens when matching ":\(.*\)"
+					   against ":/".  I don't really understand this code
+					   yet.  */
+					p[-3] =
+					    (unsigned char)
+					    pop_failure_jump;
+					DEBUG_PRINT1
+					    ("  End of pattern: change to `pop_failure_jump'.\n");
+				}
+
+				else if ((re_opcode_t) * p2 == exactn
+					 || (bufp->newline_anchor
+					     && (re_opcode_t) * p2 ==
+					     endline)) {
+					register unsigned char c =
+					    *p2 ==
+					    (unsigned char) endline ? '\n'
+					    : p2[2];
+					p1 = p + mcnt;
+
+					/* p1[0] ... p1[2] are the `on_failure_jump' corresponding
+					   to the `maybe_finalize_jump' of this case.  Examine what
+					   follows.  */
+					if ((re_opcode_t) p1[3] == exactn
+					    && p1[5] != c) {
+						p[-3] =
+						    (unsigned char)
+						    pop_failure_jump;
+						DEBUG_PRINT3
+						    ("  %c != %c => pop_failure_jump.\n",
+						     c, p1[5]);
+					}
+
+					else if ((re_opcode_t) p1[3] ==
+						 charset
+						 || (re_opcode_t) p1[3] ==
+						 charset_not) {
+						int not =
+						    (re_opcode_t) p1[3] ==
+						    charset_not;
+
+						if (c <
+						    (unsigned char) (p1[4]
+								     *
+								     BYTEWIDTH)
+						    && p1[5 +
+							  c /
+							  BYTEWIDTH] & (1
+									<<
+									(c
+									 %
+									 BYTEWIDTH)))
+							not = !not;
+
+						/* `not' is equal to 1 if c would match, which means
+						   that we can't change to pop_failure_jump.  */
+						if (!not) {
+							p[-3] =
+							    (unsigned char)
+							    pop_failure_jump;
+							DEBUG_PRINT1
+							    ("  No match => pop_failure_jump.\n");
+						}
+					}
+				}
+			}
+			p -= 2;	/* Point at relative address again.  */
+			if ((re_opcode_t) p[-1] != pop_failure_jump) {
+				p[-1] = (unsigned char) jump;
+				DEBUG_PRINT1("  Match => jump.\n");
+				goto unconditional_jump;
+			}
+			/* Note fall through.  */
+
+
+			/* The end of a simple repeat has a pop_failure_jump back to
+			   its matching on_failure_jump, where the latter will push a
+			   failure point.  The pop_failure_jump takes off failure
+			   points put on by this pop_failure_jump's matching
+			   on_failure_jump; we got through the pattern to here from the
+			   matching on_failure_jump, so didn't fail.  */
+		case pop_failure_jump:
+			{
+				/* We need to pass separate storage for the lowest and
+				   highest registers, even though we don't care about the
+				   actual values.  Otherwise, we will restore only one
+				   register from the stack, since lowest will == highest in
+				   `pop_failure_point'.  */
+				active_reg_t dummy_low_reg, dummy_high_reg;
+				unsigned char *pdummy;
+				const char *sdummy;
+
+				DEBUG_PRINT1
+				    ("EXECUTING pop_failure_jump.\n");
+				POP_FAILURE_POINT(sdummy, pdummy,
+						  dummy_low_reg,
+						  dummy_high_reg,
+						  reg_dummy, reg_dummy,
+						  reg_info_dummy);
+			}
+			/* Note fall through.  */
+
+
+			/* Unconditionally jump (without popping any failure points).  */
+		case jump:
+		      unconditional_jump:
+			EXTRACT_NUMBER_AND_INCR(mcnt, p);	/* Get the amount to jump.  */
+			DEBUG_PRINT2("EXECUTING jump %d ", mcnt);
+			p += mcnt;	/* Do the jump.  */
+			DEBUG_PRINT2("(to 0x%x).\n", p);
+			break;
+
+
+			/* We need this opcode so we can detect where alternatives end
+			   in `group_match_null_string_p' et al.  */
+		case jump_past_alt:
+			DEBUG_PRINT1("EXECUTING jump_past_alt.\n");
+			goto unconditional_jump;
+
+
+			/* Normally, the on_failure_jump pushes a failure point, which
+			   then gets popped at pop_failure_jump.  We will end up at
+			   pop_failure_jump, also, and with a pattern of, say, `a+', we
+			   are skipping over the on_failure_jump, so we have to push
+			   something meaningless for pop_failure_jump to pop.  */
+		case dummy_failure_jump:
+			DEBUG_PRINT1("EXECUTING dummy_failure_jump.\n");
+			/* It doesn't matter what we push for the string here.  What
+			   the code at `fail' tests is the value for the pattern.  */
+			PUSH_FAILURE_POINT(0, 0, -2);
+			PUSH_FAILURE_POINT2(0, 0, -2);
+			goto unconditional_jump;
+
+
+			/* At the end of an alternative, we need to push a dummy failure
+			   point in case we are followed by a `pop_failure_jump', because
+			   we don't want the failure point for the alternative to be
+			   popped.  For example, matching `(a|ab)*' against `aab'
+			   requires that we match the `ab' alternative.  */
+		case push_dummy_failure:
+			DEBUG_PRINT1("EXECUTING push_dummy_failure.\n");
+			/* See comments just above at `dummy_failure_jump' about the
+			   two zeroes.  */
+			PUSH_FAILURE_POINT(0, 0, -2);
+			PUSH_FAILURE_POINT2(0, 0, -2);
+			break;
+
+			/* Have to succeed matching what follows at least n times.
+			   After that, handle like `on_failure_jump'.  */
+		case succeed_n:
+			EXTRACT_NUMBER(mcnt, p + 2);
+			DEBUG_PRINT2("EXECUTING succeed_n %d.\n", mcnt);
+
+			assert(mcnt >= 0);
+			/* Originally, this is how many times we HAVE to succeed.  */
+			if (mcnt > 0) {
+				mcnt--;
+				p += 2;
+				STORE_NUMBER_AND_INCR(p, mcnt);
+				DEBUG_PRINT3("  Setting 0x%x to %d.\n", p,
+					     mcnt);
+			} else if (mcnt == 0) {
+				DEBUG_PRINT2
+				    ("  Setting two bytes from 0x%x to no_op.\n",
+				     p + 2);
+				p[2] = (unsigned char) no_op;
+				p[3] = (unsigned char) no_op;
+				goto on_failure;
+			}
+			break;
+
+		case jump_n:
+			EXTRACT_NUMBER(mcnt, p + 2);
+			DEBUG_PRINT2("EXECUTING jump_n %d.\n", mcnt);
+
+			/* Originally, this is how many times we CAN jump.  */
+			if (mcnt) {
+				mcnt--;
+				STORE_NUMBER(p + 2, mcnt);
+				goto unconditional_jump;
+			}
+			/* If don't have to jump any more, skip over the rest of command.  */
+			else
+				p += 4;
+			break;
+
+		case set_number_at:
+			{
+				DEBUG_PRINT1("EXECUTING set_number_at.\n");
+
+				EXTRACT_NUMBER_AND_INCR(mcnt, p);
+				p1 = p + mcnt;
+				EXTRACT_NUMBER_AND_INCR(mcnt, p);
+				DEBUG_PRINT3("  Setting 0x%x to %d.\n", p1,
+					     mcnt);
+				STORE_NUMBER(p1, mcnt);
+				break;
+			}
+
+		case wordbound:
+			DEBUG_PRINT1("EXECUTING wordbound.\n");
+			if (AT_WORD_BOUNDARY(d))
+				break;
+			goto fail;
+
+		case notwordbound:
+			DEBUG_PRINT1("EXECUTING notwordbound.\n");
+			if (AT_WORD_BOUNDARY(d))
+				goto fail;
+			break;
+
+		case wordbeg:
+			DEBUG_PRINT1("EXECUTING wordbeg.\n");
+			if (WORDCHAR_P(d)
+			    && (AT_STRINGS_BEG(d) || !WORDCHAR_P(d - 1)))
+				break;
+			goto fail;
+
+		case wordend:
+			DEBUG_PRINT1("EXECUTING wordend.\n");
+			if (!AT_STRINGS_BEG(d) && WORDCHAR_P(d - 1)
+			    && (!WORDCHAR_P(d) || AT_STRINGS_END(d)))
+				break;
+			goto fail;
+
+		case wordchar:
+			DEBUG_PRINT1("EXECUTING non-Emacs wordchar.\n");
+			PREFETCH();
+			if (!WORDCHAR_P(d))
+				goto fail;
+			SET_REGS_MATCHED();
+			d++;
+			break;
+
+		case notwordchar:
+			DEBUG_PRINT1("EXECUTING non-Emacs notwordchar.\n");
+			PREFETCH();
+			if (WORDCHAR_P(d))
+				goto fail;
+			SET_REGS_MATCHED();
+			d++;
+			break;
+
+		default:
+			abort();
+		}
+		continue;	/* Successfully executed one pattern command; keep going.  */
+
+
+		/* We goto here if a matching operation fails. */
+	      fail:
+		if (!FAIL_STACK_EMPTY()) {	/* A restart point is known.  Restore to that state.  */
+			DEBUG_PRINT1("\nFAIL:\n");
+			POP_FAILURE_POINT(d, p,
+					  lowest_active_reg,
+					  highest_active_reg, regstart,
+					  regend, reg_info);
+
+			/* If this failure point is a dummy, try the next one.  */
+			if (!p)
+				goto fail;
+
+			/* If we failed to the end of the pattern, don't examine *p.  */
+			assert(p <= pend);
+			if (p < pend) {
+				boolean is_a_jump_n = false;
+
+				/* If failed to a backwards jump that's part of a repetition
+				   loop, need to pop this failure point and use the next one.  */
+				switch ((re_opcode_t) * p) {
+				case jump_n:
+					is_a_jump_n = true;
+				case maybe_pop_jump:
+				case pop_failure_jump:
+				case jump:
+					p1 = p + 1;
+					EXTRACT_NUMBER_AND_INCR(mcnt, p1);
+					p1 += mcnt;
+
+					if ((is_a_jump_n
+					     && (re_opcode_t) * p1 ==
+					     succeed_n)
+					    || (!is_a_jump_n
+						&& (re_opcode_t) * p1 ==
+						on_failure_jump))
+						goto fail;
+					break;
+				default:
+					/* do nothing */ ;
+				}
+			}
+
+			if (d >= string1 && d <= end1)
+				dend = end_match_1;
+		} else
+			break;	/* Matching at this starting point really fails.  */
+	}			/* for (;;) */
+
+	if (best_regs_set)
+		goto restore_best_regs;
+
+	FREE_VARIABLES();
+
+	return -1;		/* Failure to match.  */
+}				/* re_match_2 */
+
+/* Subroutine definitions for re_match_2.  */
+
+
+/* We are passed P pointing to a register number after a start_memory.
+
+   Return true if the pattern up to the corresponding stop_memory can
+   match the empty string, and false otherwise.
+
+   If we find the matching stop_memory, sets P to point to one past its number.
+   Otherwise, sets P to an undefined byte less than or equal to END.
+
+   We don't handle duplicates properly (yet).  */
+
+static boolean group_match_null_string_p(p, end, reg_info)
+unsigned char **p, *end;
+register_info_type *reg_info;
+{
+	int mcnt;
+	/* Point to after the args to the start_memory.  */
+	unsigned char *p1 = *p + 2;
+
+	while (p1 < end) {
+		/* Skip over opcodes that can match nothing, and return true or
+		   false, as appropriate, when we get to one that can't, or to the
+		   matching stop_memory.  */
+
+		switch ((re_opcode_t) * p1) {
+			/* Could be either a loop or a series of alternatives.  */
+		case on_failure_jump:
+			p1++;
+			EXTRACT_NUMBER_AND_INCR(mcnt, p1);
+
+			/* If the next operation is not a jump backwards in the
+			   pattern.  */
+
+			if (mcnt >= 0) {
+				/* Go through the on_failure_jumps of the alternatives,
+				   seeing if any of the alternatives cannot match nothing.
+				   The last alternative starts with only a jump,
+				   whereas the rest start with on_failure_jump and end
+				   with a jump, e.g., here is the pattern for `a|b|c':
+
+				   /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
+				   /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
+				   /exactn/1/c
+
+				   So, we have to first go through the first (n-1)
+				   alternatives and then deal with the last one separately.  */
+
+
+				/* Deal with the first (n-1) alternatives, which start
+				   with an on_failure_jump (see above) that jumps to right
+				   past a jump_past_alt.  */
+
+				while ((re_opcode_t) p1[mcnt - 3] ==
+				       jump_past_alt) {
+					/* `mcnt' holds how many bytes long the alternative
+					   is, including the ending `jump_past_alt' and
+					   its number.  */
+
+					if (!alt_match_null_string_p
+					    (p1, p1 + mcnt - 3, reg_info))
+						return false;
+
+					/* Move to right after this alternative, including the
+					   jump_past_alt.  */
+					p1 += mcnt;
+
+					/* Break if it's the beginning of an n-th alternative
+					   that doesn't begin with an on_failure_jump.  */
+					if ((re_opcode_t) * p1 !=
+					    on_failure_jump)
+						break;
+
+					/* Still have to check that it's not an n-th
+					   alternative that starts with an on_failure_jump.  */
+					p1++;
+					EXTRACT_NUMBER_AND_INCR(mcnt, p1);
+					if ((re_opcode_t) p1[mcnt - 3] !=
+					    jump_past_alt) {
+						/* Get to the beginning of the n-th alternative.  */
+						p1 -= 3;
+						break;
+					}
+				}
+
+				/* Deal with the last alternative: go back and get number
+				   of the `jump_past_alt' just before it.  `mcnt' contains
+				   the length of the alternative.  */
+				EXTRACT_NUMBER(mcnt, p1 - 2);
+
+				if (!alt_match_null_string_p
+				    (p1, p1 + mcnt, reg_info))
+					return false;
+
+				p1 += mcnt;	/* Get past the n-th alternative.  */
+			}	/* if mcnt > 0 */
+			break;
+
+
+		case stop_memory:
+			assert(p1[1] == **p);
+			*p = p1 + 2;
+			return true;
+
+
+		default:
+			if (!common_op_match_null_string_p
+			    (&p1, end, reg_info))
+				return false;
+		}
+	}			/* while p1 < end */
+
+	return false;
+}				/* group_match_null_string_p */
+
+
+/* Similar to group_match_null_string_p, but doesn't deal with alternatives:
+   It expects P to be the first byte of a single alternative and END one
+   byte past the last. The alternative can contain groups.  */
+
+static boolean alt_match_null_string_p(p, end, reg_info)
+unsigned char *p, *end;
+register_info_type *reg_info;
+{
+	int mcnt;
+	unsigned char *p1 = p;
+
+	while (p1 < end) {
+		/* Skip over opcodes that can match nothing, and break when we get
+		   to one that can't.  */
+
+		switch ((re_opcode_t) * p1) {
+			/* It's a loop.  */
+		case on_failure_jump:
+			p1++;
+			EXTRACT_NUMBER_AND_INCR(mcnt, p1);
+			p1 += mcnt;
+			break;
+
+		default:
+			if (!common_op_match_null_string_p
+			    (&p1, end, reg_info))
+				return false;
+		}
+	}			/* while p1 < end */
+
+	return true;
+}				/* alt_match_null_string_p */
+
+
+/* Deals with the ops common to group_match_null_string_p and
+   alt_match_null_string_p.
+
+   Sets P to one after the op and its arguments, if any.  */
+
+static boolean common_op_match_null_string_p(p, end, reg_info)
+unsigned char **p, *end;
+register_info_type *reg_info;
+{
+	int mcnt;
+	boolean ret;
+	int reg_no;
+	unsigned char *p1 = *p;
+
+	switch ((re_opcode_t) * p1++) {
+	case no_op:
+	case begline:
+	case endline:
+	case begbuf:
+	case endbuf:
+	case wordbeg:
+	case wordend:
+	case wordbound:
+	case notwordbound:
+		break;
+
+	case start_memory:
+		reg_no = *p1;
+		assert(reg_no > 0 && reg_no <= MAX_REGNUM);
+		ret = group_match_null_string_p(&p1, end, reg_info);
+
+		/* Have to set this here in case we're checking a group which
+		   contains a group and a back reference to it.  */
+
+		if (REG_MATCH_NULL_STRING_P(reg_info[reg_no]) ==
+		    MATCH_NULL_UNSET_VALUE)
+			REG_MATCH_NULL_STRING_P(reg_info[reg_no]) = ret;
+
+		if (!ret)
+			return false;
+		break;
+
+		/* If this is an optimized succeed_n for zero times, make the jump.  */
+	case jump:
+		EXTRACT_NUMBER_AND_INCR(mcnt, p1);
+		if (mcnt >= 0)
+			p1 += mcnt;
+		else
+			return false;
+		break;
+
+	case succeed_n:
+		/* Get to the number of times to succeed.  */
+		p1 += 2;
+		EXTRACT_NUMBER_AND_INCR(mcnt, p1);
+
+		if (mcnt == 0) {
+			p1 -= 4;
+			EXTRACT_NUMBER_AND_INCR(mcnt, p1);
+			p1 += mcnt;
+		} else
+			return false;
+		break;
+
+	case duplicate:
+		if (!REG_MATCH_NULL_STRING_P(reg_info[*p1]))
+			return false;
+		break;
+
+	case set_number_at:
+		p1 += 4;
+
+	default:
+		/* All other opcodes mean we cannot match the empty string.  */
+		return false;
+	}
+
+	*p = p1;
+	return true;
+}				/* common_op_match_null_string_p */
+
+
+/* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
+   bytes; nonzero otherwise.  */
+
+static int bcmp_translate(s1, s2, len, translate)
+const char *s1, *s2;
+register int len;
+char *translate;
+{
+	register const unsigned char *p1 = (const unsigned char *) s1,
+	    *p2 = (const unsigned char *) s2;
+	while (len) {
+		if (translate[*p1++] != translate[*p2++])
+			return 1;
+		len--;
+	}
+	return 0;
+}
+
+/* Entry points for GNU code.  */
+
+/* re_compile_pattern is the GNU regular expression compiler: it
+   compiles PATTERN (of length SIZE) and puts the result in BUFP.
+   Returns 0 if the pattern was valid, otherwise an error string.
+
+   Assumes the `allocated' (and perhaps `buffer') and `translate' fields
+   are set in BUFP on entry.
+
+   We call regex_compile to do the actual compilation.  */
+
+const char *re_compile_pattern(pattern, length, bufp)
+const char *pattern;
+size_t length;
+struct re_pattern_buffer *bufp;
+{
+	reg_errcode_t ret;
+
+	/* GNU code is written to assume at least RE_NREGS registers will be set
+	   (and at least one extra will be -1).  */
+	bufp->regs_allocated = REGS_UNALLOCATED;
+
+	/* And GNU code determines whether or not to get register information
+	   by passing null for the REGS argument to re_match, etc., not by
+	   setting no_sub.  */
+	bufp->no_sub = 0;
+
+	/* Match anchors at newline.  */
+	bufp->newline_anchor = 1;
+
+	ret = regex_compile(pattern, length, re_syntax_options, bufp);
+
+	return re_error_msg[(int) ret];
+}
+
+/* Entry points compatible with 4.2 BSD regex library.  We don't define
+   them if this is an Emacs or POSIX compilation.  */
+
+/* POSIX.2 functions.  Don't define these for Emacs.  */
+
+#if !NO_POSIX_COMPAT
+
+/* regcomp takes a regular expression as a string and compiles it.
+
+   PREG is a regex_t *.  We do not expect any fields to be initialized,
+   since POSIX says we shouldn't.  Thus, we set
+
+     `buffer' to the compiled pattern;
+     `used' to the length of the compiled pattern;
+     `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
+       REG_EXTENDED bit in CFLAGS is set; otherwise, to
+       RE_SYNTAX_POSIX_BASIC;
+     `newline_anchor' to REG_NEWLINE being set in CFLAGS;
+     `fastmap' and `fastmap_accurate' to zero;
+     `re_nsub' to the number of subexpressions in PATTERN.
+
+   PATTERN is the address of the pattern string.
+
+   CFLAGS is a series of bits which affect compilation.
+
+     If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
+     use POSIX basic syntax.
+
+     If REG_NEWLINE is set, then . and [^...] don't match newline.
+     Also, regexec will try a match beginning after every newline.
+
+     If REG_ICASE is set, then we considers upper- and lowercase
+     versions of letters to be equivalent when matching.
+
+     If REG_NOSUB is set, then when PREG is passed to regexec, that
+     routine will report only success or failure, and nothing about the
+     registers.
+
+   It returns 0 if it succeeds, nonzero if it doesn't.  (See regex.h for
+   the return codes and their meanings.)  */
+
+int regcomp(preg, pattern, cflags)
+regex_t *preg;
+const char *pattern;
+int cflags;
+{
+	reg_errcode_t ret;
+	reg_syntax_t syntax
+	    = (cflags & REG_EXTENDED) ?
+	    RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
+
+	/* regex_compile will allocate the space for the compiled pattern.  */
+	preg->buffer = 0;
+	preg->allocated = 0;
+	preg->used = 0;
+
+	/* Don't bother to use a fastmap when searching.  This simplifies the
+	   REG_NEWLINE case: if we used a fastmap, we'd have to put all the
+	   characters after newlines into the fastmap.  This way, we just try
+	   every character.  */
+	preg->fastmap = 0;
+
+	if (cflags & REG_ICASE) {
+		unsigned i;
+
+		preg->translate = (char *) malloc(CHAR_SET_SIZE);
+		if (preg->translate == NULL)
+			return (int) REG_ESPACE;
+
+		/* Map uppercase characters to corresponding lowercase ones.  */
+		for (i = 0; i < CHAR_SET_SIZE; i++)
+			preg->translate[i] = ISUPPER(i) ? tolower(i) : i;
+	} else
+		preg->translate = NULL;
+
+	/* If REG_NEWLINE is set, newlines are treated differently.  */
+	if (cflags & REG_NEWLINE) {	/* REG_NEWLINE implies neither . nor [^...] match newline.  */
+		syntax &= ~RE_DOT_NEWLINE;
+		syntax |= RE_HAT_LISTS_NOT_NEWLINE;
+		/* It also changes the matching behavior.  */
+		preg->newline_anchor = 1;
+	} else
+		preg->newline_anchor = 0;
+
+	preg->no_sub = !!(cflags & REG_NOSUB);
+
+	/* POSIX says a null character in the pattern terminates it, so we
+	   can use strlen here in compiling the pattern.  */
+	ret = regex_compile(pattern, strlen(pattern), syntax, preg);
+
+	/* POSIX doesn't distinguish between an unmatched open-group and an
+	   unmatched close-group: both are REG_EPAREN.  */
+	if (ret == REG_ERPAREN)
+		ret = REG_EPAREN;
+
+	return (int) ret;
+}
+
+
+/* regexec searches for a given pattern, specified by PREG, in the
+   string STRING.
+
+   If NMATCH is zero or REG_NOSUB was set in the cflags argument to
+   `regcomp', we ignore PMATCH.  Otherwise, we assume PMATCH has at
+   least NMATCH elements, and we set them to the offsets of the
+   corresponding matched substrings.
+
+   EFLAGS specifies `execution flags' which affect matching: if
+   REG_NOTBOL is set, then ^ does not match at the beginning of the
+   string; if REG_NOTEOL is set, then $ does not match at the end.
+
+   We return 0 if we find a match and REG_NOMATCH if not.  */
+
+int regexec(preg, string, nmatch, pmatch, eflags)
+const regex_t *preg;
+const char *string;
+size_t nmatch;
+regmatch_t pmatch[];
+int eflags;
+{
+	int ret;
+	struct re_registers regs;
+	regex_t private_preg;
+	int len = strlen(string);
+	boolean want_reg_info = !preg->no_sub && nmatch > 0;
+
+	private_preg = *preg;
+
+	private_preg.not_bol = !!(eflags & REG_NOTBOL);
+	private_preg.not_eol = !!(eflags & REG_NOTEOL);
+
+	/* The user has told us exactly how many registers to return
+	   information about, via `nmatch'.  We have to pass that on to the
+	   matching routines.  */
+	private_preg.regs_allocated = REGS_FIXED;
+
+	if (want_reg_info) {
+		regs.num_regs = nmatch;
+		regs.start = TALLOC(nmatch, regoff_t);
+		regs.end = TALLOC(nmatch, regoff_t);
+		if (regs.start == NULL || regs.end == NULL)
+			return (int) REG_NOMATCH;
+	}
+
+	/* Perform the searching operation.  */
+	ret = re_search(&private_preg, string, len,
+			/* start: */ 0, /* range: */ len,
+			want_reg_info ? &regs : (struct re_registers *) 0);
+
+	/* Copy the register information to the POSIX structure.  */
+	if (want_reg_info) {
+		if (ret >= 0) {
+			unsigned r;
+
+			for (r = 0; r < nmatch; r++) {
+				pmatch[r].rm_so = regs.start[r];
+				pmatch[r].rm_eo = regs.end[r];
+			}
+		}
+
+		/* If we needed the temporary register info, free the space now.  */
+		free(regs.start);
+		free(regs.end);
+	}
+
+	/* We want zero return to mean success, unlike `re_search'.  */
+	return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
+}
+
+
+/* Returns a message corresponding to an error code, ERRCODE, returned
+   from either regcomp or regexec.   We don't use PREG here.  */
+
+size_t regerror(errcode, preg, errbuf, errbuf_size)
+int errcode;
+const regex_t *preg;
+char *errbuf;
+size_t errbuf_size;
+{
+	const char *msg;
+	size_t msg_size;
+
+	if (errcode < 0
+	    || errcode >= (sizeof(re_error_msg) / sizeof(re_error_msg[0])))
+		/* Only error codes returned by the rest of the code should be passed
+		   to this routine.  If we are given anything else, or if other regex
+		   code generates an invalid error code, then the program has a bug.
+		   Dump core so we can fix it.  */
+		abort();
+
+	msg = re_error_msg[errcode];
+
+	/* POSIX doesn't require that we do anything in this case, but why
+	   not be nice.  */
+	if (!msg)
+		msg = "Success";
+
+	msg_size = strlen(msg) + 1;	/* Includes the null.  */
+
+	if (errbuf_size != 0) {
+		if (msg_size > errbuf_size) {
+			strncpy(errbuf, msg, errbuf_size - 1);
+			errbuf[errbuf_size - 1] = 0;
+		} else
+			strcpy(errbuf, msg);
+	}
+
+	return msg_size;
+}
+
+
+/* Free dynamically allocated space used by PREG.  */
+
+void regfree(preg)
+regex_t *preg;
+{
+	if (preg->buffer != NULL)
+		free(preg->buffer);
+	preg->buffer = NULL;
+
+	preg->allocated = 0;
+	preg->used = 0;
+
+	if (preg->fastmap != NULL)
+		free(preg->fastmap);
+	preg->fastmap = NULL;
+	preg->fastmap_accurate = 0;
+
+	if (preg->translate != NULL)
+		free(preg->translate);
+	preg->translate = NULL;
+}
+
+#endif				/* !NO_POSIX_COMPAT */

Added: multipath-tools/trunk/libmultipath/regex.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/regex.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,252 @@
+/* Definitions for data structures and routines for the regular
+   expression library, version 0.12.
+
+   Copyright (C) 1985, 1989, 1990, 1991, 1992, 1993
+   Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#ifndef __REGEXP_LIBRARY_H__
+#define __REGEXP_LIBRARY_H__
+
+typedef long s_reg_t;
+typedef unsigned long active_reg_t;
+
+typedef unsigned long reg_syntax_t;
+
+#define RE_BACKSLASH_ESCAPE_IN_LISTS	(1L)
+#define RE_BK_PLUS_QM			(RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
+#define RE_CHAR_CLASSES			(RE_BK_PLUS_QM << 1)
+#define RE_CONTEXT_INDEP_ANCHORS	(RE_CHAR_CLASSES << 1)
+#define RE_CONTEXT_INDEP_OPS		(RE_CONTEXT_INDEP_ANCHORS << 1)
+#define RE_CONTEXT_INVALID_OPS		(RE_CONTEXT_INDEP_OPS << 1)
+#define RE_DOT_NEWLINE			(RE_CONTEXT_INVALID_OPS << 1)
+#define RE_DOT_NOT_NULL			(RE_DOT_NEWLINE << 1)
+#define RE_HAT_LISTS_NOT_NEWLINE	(RE_DOT_NOT_NULL << 1)
+#define RE_INTERVALS			(RE_HAT_LISTS_NOT_NEWLINE << 1)
+#define RE_LIMITED_OPS			(RE_INTERVALS << 1)
+#define RE_NEWLINE_ALT			(RE_LIMITED_OPS << 1)
+#define RE_NO_BK_BRACES			(RE_NEWLINE_ALT << 1)
+#define RE_NO_BK_PARENS			(RE_NO_BK_BRACES << 1)
+#define RE_NO_BK_REFS			(RE_NO_BK_PARENS << 1)
+#define RE_NO_BK_VBAR			(RE_NO_BK_REFS << 1)
+#define RE_NO_EMPTY_RANGES		(RE_NO_BK_VBAR << 1)
+#define RE_UNMATCHED_RIGHT_PAREN_ORD	(RE_NO_EMPTY_RANGES << 1)
+#define RE_NO_GNU_OPS			(RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
+
+extern reg_syntax_t re_syntax_options;
+
+#define RE_SYNTAX_EMACS 0
+
+#define RE_SYNTAX_AWK							  \
+	(RE_BACKSLASH_ESCAPE_IN_LISTS	| RE_DOT_NOT_NULL		| \
+	 RE_NO_BK_PARENS		| RE_NO_BK_REFS			| \
+	 RE_NO_BK_VBAR			| RE_NO_EMPTY_RANGES		| \
+	 RE_DOT_NEWLINE			| RE_CONTEXT_INDEP_ANCHORS	| \
+	 RE_UNMATCHED_RIGHT_PAREN_ORD	| RE_NO_GNU_OPS)
+
+#define RE_SYNTAX_GNU_AWK 						  \
+	((RE_SYNTAX_POSIX_EXTENDED	| RE_BACKSLASH_ESCAPE_IN_LISTS)	| \
+	& ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS))
+
+#define RE_SYNTAX_POSIX_AWK 						  \
+	(RE_SYNTAX_POSIX_EXTENDED	| RE_BACKSLASH_ESCAPE_IN_LISTS	| \
+	 RE_INTERVALS			| RE_NO_GNU_OPS)
+
+#define RE_SYNTAX_GREP							  \
+	(RE_BK_PLUS_QM			| RE_CHAR_CLASSES		| \
+	 RE_HAT_LISTS_NOT_NEWLINE	| RE_INTERVALS			| \
+	 RE_NEWLINE_ALT)
+
+#define RE_SYNTAX_EGREP							  \
+	(RE_CHAR_CLASSES		| RE_CONTEXT_INDEP_ANCHORS	| \
+	 RE_CONTEXT_INDEP_OPS		| RE_HAT_LISTS_NOT_NEWLINE	| \
+	 RE_NEWLINE_ALT			| RE_NO_BK_PARENS		| \
+	 RE_NO_BK_VBAR)
+
+#define RE_SYNTAX_POSIX_EGREP						  \
+	(RE_SYNTAX_EGREP		| RE_INTERVALS			| \
+	 RE_NO_BK_BRACES)
+
+#define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
+
+#define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
+
+#define _RE_SYNTAX_POSIX_COMMON						  \
+	(RE_CHAR_CLASSES		| RE_DOT_NEWLINE		| \
+	 RE_DOT_NOT_NULL		| RE_INTERVALS			| \
+	 RE_NO_EMPTY_RANGES)
+
+#define RE_SYNTAX_POSIX_BASIC						  \
+	(_RE_SYNTAX_POSIX_COMMON	| RE_BK_PLUS_QM)
+
+#define RE_SYNTAX_POSIX_MINIMAL_BASIC					  \
+	(_RE_SYNTAX_POSIX_COMMON	| RE_LIMITED_OPS)
+
+#define RE_SYNTAX_POSIX_EXTENDED					  \
+	(_RE_SYNTAX_POSIX_COMMON	| RE_CONTEXT_INDEP_ANCHORS	| \
+	 RE_CONTEXT_INDEP_OPS		| RE_NO_BK_BRACES		| \
+	 RE_NO_BK_PARENS		| RE_NO_BK_VBAR			| \
+	 RE_UNMATCHED_RIGHT_PAREN_ORD)
+
+#define RE_SYNTAX_POSIX_MINIMAL_EXTENDED				  \
+	(_RE_SYNTAX_POSIX_COMMON	| RE_CONTEXT_INDEP_ANCHORS	| \
+	 RE_CONTEXT_INVALID_OPS		| RE_NO_BK_BRACES		| \
+	 RE_NO_BK_PARENS		| RE_NO_BK_REFS			| \
+	 RE_NO_BK_VBAR			| RE_UNMATCHED_RIGHT_PAREN_ORD)
+
+/* Maximum number of duplicates an interval can allow */
+#define RE_DUP_MAX  (0x7fff)
+
+/* POSIX 'cflags' bits */
+#define REG_EXTENDED	1
+#define REG_ICASE	(REG_EXTENDED << 1)
+#define REG_NEWLINE	(REG_ICASE << 1)
+#define REG_NOSUB	(REG_NEWLINE << 1)
+
+
+/* POSIX `eflags' bits */
+#define REG_NOTBOL	1
+#define REG_NOTEOL	(1 << 1)
+
+/* If any error codes are removed, changed, or added, update the
+   `re_error_msg' table in regex.c.  */
+typedef enum
+{
+  REG_NOERROR = 0,	/* Success.  */
+  REG_NOMATCH,		/* Didn't find a match (for regexec).  */
+
+  /* POSIX regcomp return error codes */
+  REG_BADPAT,		/* Invalid pattern.  */
+  REG_ECOLLATE,		/* Not implemented.  */
+  REG_ECTYPE,		/* Invalid character class name.  */
+  REG_EESCAPE,		/* Trailing backslash.  */
+  REG_ESUBREG,		/* Invalid back reference.  */
+  REG_EBRACK,		/* Unmatched left bracket.  */
+  REG_EPAREN,		/* Parenthesis imbalance.  */
+  REG_EBRACE,		/* Unmatched \{.  */
+  REG_BADBR,		/* Invalid contents of \{\}.  */
+  REG_ERANGE,		/* Invalid range end.  */
+  REG_ESPACE,		/* Ran out of memory.  */
+  REG_BADRPT,		/* No preceding re for repetition op.  */
+
+  /* Error codes we've added */
+  REG_EEND,		/* Premature end.  */
+  REG_ESIZE,		/* Compiled pattern bigger than 2^16 bytes.  */
+  REG_ERPAREN		/* Unmatched ) or \); not returned from regcomp.  */
+} reg_errcode_t;
+
+#define REGS_UNALLOCATED	0
+#define REGS_REALLOCATE		1
+#define REGS_FIXED		2
+
+/* This data structure represents a compiled pattern */
+struct re_pattern_buffer
+{
+  unsigned char *buffer;
+  unsigned long allocated;
+  unsigned long used;
+  reg_syntax_t syntax;
+  char *fastmap;
+  char *translate;
+  size_t re_nsub;
+  unsigned can_be_null : 1;
+  unsigned regs_allocated : 2;
+  unsigned fastmap_accurate : 1;
+  unsigned no_sub : 1;
+  unsigned not_bol : 1;
+  unsigned not_eol : 1;
+  unsigned newline_anchor : 1;
+};
+
+typedef struct re_pattern_buffer regex_t;
+
+/* search.c (search_buffer) in Emacs needs this one opcode value.  It is
+   defined both in `regex.c' and here.  */
+#define RE_EXACTN_VALUE 1
+
+/* Type for byte offsets within the string.  POSIX mandates this.  */
+typedef int regoff_t;
+
+
+/* This is the structure we store register match data in.  See
+   regex.texinfo for a full description of what registers match.  */
+struct re_registers
+{
+  unsigned num_regs;
+  regoff_t *start;
+  regoff_t *end;
+};
+
+
+#ifndef RE_NREGS
+#define RE_NREGS 30
+#endif
+
+
+/* POSIX specification for registers.  Aside from the different names than
+   `re_registers', POSIX uses an array of structures, instead of a
+   structure of arrays.  */
+typedef struct
+{
+  regoff_t rm_so;  /* Byte offset from string's start to substring's start.  */
+  regoff_t rm_eo;  /* Byte offset from string's start to substring's end.  */
+} regmatch_t;
+
+/* Declarations for routines.  */
+
+extern reg_syntax_t re_set_syntax (reg_syntax_t syntax);
+
+extern const char *re_compile_pattern (const char *pattern, size_t length,
+				       struct re_pattern_buffer *buffer);
+
+extern int re_compile_fastmap (struct re_pattern_buffer *buffer);
+
+extern int re_search (struct re_pattern_buffer *buffer, const char *string,
+		      int length, int start, int range,
+		      struct re_registers *regs);
+
+extern int re_search_2 (struct re_pattern_buffer *buffer, const char *string1,
+			int length1, const char *string2, int length2,
+			int start, int range, struct re_registers *regs,
+			int stop);
+
+extern int re_match (struct re_pattern_buffer *buffer, const char *string,
+		     int length, int start, struct re_registers *regs);
+
+extern int re_match_2 (struct re_pattern_buffer *buffer, const char *string1,
+		       int length1, const char *string2, int length2,
+		       int start, struct re_registers *regs, int stop);
+
+extern void re_set_registers (struct re_pattern_buffer *buffer,
+			      struct re_registers *regs, unsigned num_regs,
+			      regoff_t *starts, regoff_t *ends);
+
+/* 4.2 bsd compatibility.  */
+extern char *re_comp (const char *);
+extern int re_exec (const char *);
+
+/* POSIX compatibility.  */
+extern int regcomp (regex_t *preg, const char *pattern, int cflags);
+
+extern int regexec (const regex_t *preg, const char *string, size_t nmatch,
+		    regmatch_t pmatch[], int eflags);
+
+extern size_t regerror (int errcode, const regex_t *preg, char *errbuf,
+			size_t errbuf_size);
+
+extern void regfree (regex_t *preg);
+
+#endif /* not __REGEXP_LIBRARY_H__ */

Added: multipath-tools/trunk/libmultipath/sg_include.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/sg_include.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,2 @@
+#define __user
+#include <scsi/sg.h>

Added: multipath-tools/trunk/libmultipath/structs.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/libmultipath/structs.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,213 @@
+#include <stdio.h>
+#include <unistd.h>
+
+#include "memory.h"
+#include "vector.h"
+#include "util.h"
+#include "structs.h"
+#include "config.h"
+#include "debug.h"
+
+struct path *
+alloc_path (void)
+{
+	return (struct path *)MALLOC(sizeof(struct path));
+}
+
+void
+free_path (struct path * pp)
+{
+	if (!pp)
+		return;
+
+	if (pp->checker_context)
+		FREE(pp->checker_context);
+
+	if (pp->fd > 0)
+		close(pp->fd);
+
+	FREE(pp);
+}
+
+void
+free_pathvec (vector vec, int free_paths)
+{
+	int i;
+	struct path * pp;
+
+	if (!vec)
+		return;
+
+	if (free_paths)
+		vector_foreach_slot(vec, pp, i)
+			free_path(pp);
+
+	vector_free(vec);
+}
+
+struct pathgroup *
+alloc_pathgroup (void)
+{
+	struct pathgroup * pgp;
+
+	pgp = (struct pathgroup *)MALLOC(sizeof(struct pathgroup));
+
+	if (!pgp)
+		return NULL;
+
+	pgp->paths = vector_alloc();
+
+	if (!pgp->paths)
+		FREE(pgp);
+
+	return pgp;
+}
+
+void
+free_pathgroup (struct pathgroup * pgp, int free_paths)
+{
+	if (!pgp)
+		return;
+
+	free_pathvec(pgp->paths, free_paths);
+	FREE(pgp);
+}
+
+void
+free_pgvec (vector pgvec, int free_paths)
+{
+	int i;
+	struct pathgroup * pgp;
+
+	if (!pgvec)
+		return;
+
+	vector_foreach_slot(pgvec, pgp, i)
+		free_pathgroup(pgp, free_paths);
+
+	vector_free(pgvec);
+}
+
+struct multipath *
+alloc_multipath (void)
+{
+	return (struct multipath *)MALLOC(sizeof(struct multipath));
+}
+
+void
+free_multipath (struct multipath * mpp, int free_paths)
+{
+	if (!mpp)
+		return;
+
+	if (mpp->selector &&
+	    mpp->selector != conf->default_selector &&
+	    (!mpp->mpe || (mpp->mpe && mpp->selector != mpp->mpe->selector)) &&
+	    (!mpp->hwe || (mpp->hwe && mpp->selector != mpp->hwe->selector)))
+		FREE(mpp->selector);
+
+	if (mpp->alias &&
+	    (!mpp->mpe || (mpp->mpe && mpp->alias != mpp->mpe->alias)) &&
+	    (mpp->wwid && mpp->alias != mpp->wwid))
+		FREE(mpp->alias);
+
+	if (mpp->features &&
+	    mpp->features != conf->default_features &&
+	    (!mpp->hwe || (mpp->hwe && mpp->features != mpp->hwe->features)))
+		FREE(mpp->features);
+
+	if (mpp->hwhandler &&
+	    mpp->hwhandler != conf->default_hwhandler &&
+	    (!mpp->hwe || (mpp->hwe && mpp->hwhandler != mpp->hwe->hwhandler)))
+		FREE(mpp->hwhandler);
+
+	free_pathvec(mpp->paths, free_paths);
+	free_pgvec(mpp->pg, free_paths);
+	FREE(mpp);
+}
+
+void
+free_multipathvec (vector mpvec, int free_paths)
+{
+	int i;
+	struct multipath * mpp;
+
+	if (!mpvec)
+		return;
+
+	vector_foreach_slot (mpvec, mpp, i)
+		free_multipath(mpp, free_paths);
+
+	vector_free(mpvec);
+}
+
+int
+store_path (vector pathvec, struct path * pp)
+{
+	if (!vector_alloc_slot(pathvec))
+		return 1;
+
+	vector_set_slot(pathvec, pp);
+
+	return 0;
+}
+
+int
+store_pathgroup (vector pgvec, struct pathgroup * pgp)
+{
+	if (!vector_alloc_slot(pgvec))
+		return 1;
+
+	vector_set_slot(pgvec, pgp);
+
+	return 0;
+}
+
+struct multipath *
+find_mp (vector mp, char * alias)
+{
+	int i;
+	int len;
+	struct multipath * mpp;
+	
+	len = strlen(alias);
+
+	if (!len)
+		return NULL;
+	
+	vector_foreach_slot (mp, mpp, i) {
+		if (strlen(mpp->alias) == len &&
+		    !strncmp(mpp->alias, alias, len))
+			return mpp;
+	}
+	return NULL;
+}
+
+struct path *
+find_path_by_dev (vector pathvec, char * dev)
+{
+	int i;
+	struct path * pp;
+	
+	vector_foreach_slot (pathvec, pp, i)
+		if (!strcmp_chomp(pp->dev, dev))
+			return pp;
+
+	condlog(3, "path %s not found in pathvec\n", dev);
+	return NULL;
+}
+
+struct path *
+find_path_by_devt (vector pathvec, char * dev_t)
+{
+	int i;
+	struct path * pp;
+
+	vector_foreach_slot (pathvec, pp, i)
+		if (!strcmp_chomp(pp->dev_t, dev_t))
+			return pp;
+
+	condlog(3, "path %s not found in pathvec\n", dev_t);
+	return NULL;
+}
+

Modified: multipath-tools/trunk/libmultipath/structs.h
==============================================================================
--- multipath-tools/trunk/libmultipath/structs.h	(original)
+++ multipath-tools/trunk/libmultipath/structs.h	Sat Aug 13 15:23:02 2005
@@ -5,7 +5,7 @@
 #define SERIAL_SIZE		17
 #define NODE_NAME_SIZE		19
 #define PATH_STR_SIZE  		16
-#define PARAMS_SIZE		256
+#define PARAMS_SIZE		1024
 #define FILE_NAME_SIZE		256
 #define CALLOUT_MAX_SIZE	128
 #define BLK_DEV_SIZE		33
@@ -14,6 +14,9 @@
 #define SCSI_PRODUCT_SIZE	17
 #define SCSI_REV_SIZE		5
 
+#define KEEP_PATHS		0
+#define FREE_PATHS		1
+
 enum pathstates {
 	PSTATE_RESERVED,
 	PSTATE_FAILED,
@@ -69,14 +72,17 @@
 	int claimed;
 	char * getuid;
 	char * getprio;
-	int (*checkfn) (char *, char *, void **);
+	int (*checkfn) (int, char *, void **);
 	void * checker_context;
 	struct multipath * mpp;
+	int fd;
+	
+	/* configlet pointers */
+	struct hwentry * hwe;
 };
 
 struct multipath {
 	char wwid[WWID_SIZE];
-	char * alias;
 	int pgpolicy;
 	int nextpg;
 	int queuedio;
@@ -86,11 +92,12 @@
 	vector pg;
 	char params[PARAMS_SIZE];
 	char status[PARAMS_SIZE];
+
+	/* configlet pointers */
+	char * alias;
 	char * selector;
 	char * features;
 	char * hwhandler;
-
-	/* configlet pointers */
 	struct mpentry * mpe;
 	struct hwentry * hwe;
 };
@@ -102,4 +109,23 @@
 	vector paths;
 };
 
+struct path * alloc_path (void);
+struct pathgroup * alloc_pathgroup (void);
+struct multipath * alloc_multipath (void);
+void free_path (struct path *);
+void free_pathvec (vector vec, int free_paths);
+void free_pathgroup (struct pathgroup * pgp, int free_paths);
+void free_pgvec (vector pgvec, int free_paths);
+void free_multipath (struct multipath *, int free_paths);
+void free_multipathvec (vector mpvec, int free_paths);
+
+int store_path (vector pathvec, struct path * pp);
+int store_pathgroup (vector pgvec, struct pathgroup * pgp);
+
+struct multipath * find_mp (vector mp, char * alias);
+struct path * find_path_by_devt (vector pathvec, char * devt);
+struct path * find_path_by_dev (vector pathvec, char * dev);
+
+char sysfs_path[FILE_NAME_SIZE];
+
 #endif

Modified: multipath-tools/trunk/libmultipath/util.c
==============================================================================
--- multipath-tools/trunk/libmultipath/util.c	(original)
+++ multipath-tools/trunk/libmultipath/util.c	Sat Aug 13 15:23:02 2005
@@ -1,4 +1,30 @@
 #include <string.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define PARAMS_SIZE 255
+
+int
+strcmp_chomp(char *str1, char *str2)
+{
+	int i;
+	char s1[PARAMS_SIZE],s2[PARAMS_SIZE];
+ 
+	if(!str1 || !str2)
+		return 1;
+
+	strncpy(s1, str1, PARAMS_SIZE);
+	strncpy(s2, str2, PARAMS_SIZE);
+
+	for (i=strlen(s1)-1; i >=0 && isspace(s1[i]); --i) ;
+	s1[++i] = '\0';
+	for (i=strlen(s2)-1; i >=0 && isspace(s2[i]); --i) ;
+	s2[++i] = '\0';
+
+	return(strcmp(s1,s2));
+}
 
 void
 basename (char * str1, char * str2)
@@ -14,3 +40,12 @@
 	strcpy(str2, p);
 }
 
+int
+filepresent (char * run) {
+	struct stat buf;
+
+	if(!stat(run, &buf))
+		return 1;
+	return 0;
+}
+

Modified: multipath-tools/trunk/libmultipath/util.h
==============================================================================
--- multipath-tools/trunk/libmultipath/util.h	(original)
+++ multipath-tools/trunk/libmultipath/util.h	Sat Aug 13 15:23:02 2005
@@ -1,2 +1,13 @@
+#ifndef _UTIL_H
+#define _UTIL_H
+
+int strcmp_chomp(char *, char *);
 void basename (char * src, char * dst);
+int filepresent (char * run);
+
+#define safe_sprintf(var, format, args...)	\
+	snprintf(var, sizeof(var), format, ##args) >= sizeof(var)
+#define safe_snprintf(var, size, format, args...)      \
+	snprintf(var, size, format, ##args) >= size
 
+#endif /* _UTIL_H */

Modified: multipath-tools/trunk/libmultipath/vector.c
==============================================================================
--- multipath-tools/trunk/libmultipath/vector.c	(original)
+++ multipath-tools/trunk/libmultipath/vector.c	Sat Aug 13 15:23:02 2005
@@ -1,8 +1,4 @@
 /* 
- * Soft:        Keepalived is a failover program for the LVS project
- *              <www.linuxvirtualserver.org>. It monitor & manipulate
- *              a loadbalanced server pool using multi-layer checks.
- * 
  * Part:        Vector structure manipulation.
  *  
  * Version:     $Id: vector.c,v 1.0.3 2003/05/11 02:28:03 acassen Exp $
@@ -31,32 +27,40 @@
 vector
 vector_alloc(void)
 {
-	vector v = (vector) zalloc(sizeof (struct _vector));
+	vector v = (vector) MALLOC(sizeof (struct _vector));
 	return v;
 }
 
 /* allocated one slot */
-void
+void *
 vector_alloc_slot(vector v)
 {
 	v->allocated += VECTOR_DEFAULT_SIZE;
 	if (v->slot)
-		v->slot = realloc(v->slot, sizeof (void *) * v->allocated);
+		v->slot = REALLOC(v->slot, sizeof (void *) * v->allocated);
 	else
-		v->slot = (void *) zalloc(sizeof (void *) * v->allocated);
+		v->slot = (void *) MALLOC(sizeof (void *) * v->allocated);
+
+	if (!v->slot)
+		v->allocated -= VECTOR_DEFAULT_SIZE;
+
+	return v->slot;
 }
 
-void
+void *
 vector_insert_slot(vector v, int slot, void *value)
 {
 	int i;
 	
-	vector_alloc_slot(v);
+	if (!vector_alloc_slot(v))
+		return NULL;
 
 	for (i = (v->allocated /VECTOR_DEFAULT_SIZE) - 2; i >= slot; i--)
 		v->slot[i + 1] = v->slot[i];
 
 	v->slot[slot] = value;
+
+	return v->slot[slot];
 }
 
 void
@@ -75,15 +79,20 @@
 	if (!v->allocated)
 		v->slot = NULL;
 	else
-		v = realloc(v->slot, sizeof (void *) * v->allocated);
+		v = REALLOC(v->slot, sizeof (void *) * v->allocated);
 }
 
 /* Free memory vector allocation */
 void
 vector_free(vector v)
 {
-	free(v->slot);
-	free(v);
+	if (!v)
+		return;
+
+	if (v->slot)
+		FREE(v->slot);
+
+	FREE(v);
 }
 
 void
@@ -97,7 +106,7 @@
 
 	for (i = 0; i < VECTOR_SIZE(strvec); i++)
 		if ((str = VECTOR_SLOT(strvec, i)) != NULL)
-			free(str);
+			FREE(str);
 
 	vector_free(strvec);
 }

Modified: multipath-tools/trunk/libmultipath/vector.h
==============================================================================
--- multipath-tools/trunk/libmultipath/vector.h	(original)
+++ multipath-tools/trunk/libmultipath/vector.h	Sat Aug 13 15:23:02 2005
@@ -40,12 +40,12 @@
 
 /* Prototypes */
 extern vector vector_alloc(void);
-extern void vector_alloc_slot(vector v);
+extern void *vector_alloc_slot(vector v);
 extern void vector_free(vector v);
 extern void free_strvec(vector strvec);
 extern void vector_set_slot(vector v, void *value);
 extern void vector_del_slot(vector v, int slot);
-extern void vector_insert_slot(vector v, int slot, void *value);
+extern void *vector_insert_slot(vector v, int slot, void *value);
 extern void vector_dump(vector v);
 extern void dump_strvec(vector strvec);
 

Modified: multipath-tools/trunk/multipath.conf.annotated
==============================================================================
--- multipath-tools/trunk/multipath.conf.annotated	(original)
+++ multipath-tools/trunk/multipath.conf.annotated	Sat Aug 13 15:23:02 2005
@@ -63,30 +63,28 @@
 	# default : /bin/false
 	#
 	default_prio_callout	"/bin/false"
+
+	#
+	# name    : rr_min_io
+	# scope   : multipath
+	# desc    : the number of IO to route to a path before switching
+	#           to the next in the same path group
+	# default : 1000
+	#
+	r_min_io	100
 }
 	
 #
-# name    : devnode_blacklist
+# name    : blacklist
 # scope   : multipath & multipathd
 # desc    : list of device names to discard as not multipath candidates
 # default : cciss, fd, hd, md, dm, sr, scd, st, ram, raw, loop
 #
-devnode_blacklist {
-	devnode c0d
-	devnode c1d
-	devnode c2d
-	devnode ub
-	devnode nbd
-	devnode fd
-	devnode hd
-	devnode md
-	devnode dm
-	devnode sr
-	devnode scd
-	devnode st
-	devnode ram
-	devnode raw
-	devnode loop
+blacklist {
+        wwid 26353900f02796769
+	devnode "(ram|raw|loop|fd|md|dm-|sr|scd|st)[0-9]*"
+	devnode "hd[a-z][[0-9]*]"
+	devnode "cciss!c[0-9]d[0-9]*[p[0-9]*]"
 }
 
 #

Modified: multipath-tools/trunk/multipath.conf.synthetic
==============================================================================
--- multipath-tools/trunk/multipath.conf.synthetic	(original)
+++ multipath-tools/trunk/multipath.conf.synthetic	Sat Aug 13 15:23:02 2005
@@ -7,23 +7,13 @@
 	default_getuid_callout	"/sbin/scsi_id -g -u -s /block/%n"
 	default_prio_callout	"/bin/false"
 	default_features	"0"
+	rr_wmin_io		100
 }
 devnode_blacklist {
-	devnode c0d
-	devnode c1d
-	devnode c2d
-	devnode ub
-	devnode nbd
-	devnode fd
-	devnode hd
-	devnode md
-	devnode dm
-	devnode sr
-	devnode scd
-	devnode st
-	devnode ram
-	devnode raw
-	devnode loop
+        wwid 26353900f02796769
+	devnode "(ram|raw|loop|fd|md|dm-|sr|scd|st)[0-9]*"
+	devnode "hd[a-z][[0-9]*]"
+	devnode "cciss!c[0-9]d[0-9]*[p[0-9]*]"
 }
 multipaths {
 	multipath {

Modified: multipath-tools/trunk/multipath/Makefile
==============================================================================
--- multipath-tools/trunk/multipath/Makefile	(original)
+++ multipath-tools/trunk/multipath/Makefile	Sat Aug 13 15:23:02 2005
@@ -5,27 +5,23 @@
 
 include ../Makefile.inc
 
-OBJS = pgpolicies.o propsel.o devinfo.o main.o dict.o dmparser.o \
-       $(MULTIPATHLIB)-$(BUILD).a $(CHECKERSLIB)-$(BUILD).a
+OBJS = main.o $(MULTIPATHLIB)-$(BUILD).a $(CHECKERSLIB)-$(BUILD).a
+
+CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes \
+	 -I$(multipathdir) -I$(checkersdir)
 
 ifeq ($(strip $(BUILD)),klibc)
-	CFLAGS = ${REQFLAGS} ${OPTFLAGS} -DDEBUG=${DEBUG} \
-		 -DVERSION=$(VERSION) -I$(multipathdir) \
-		 -I$(libdmdir) -I$(sysfsdir) -I$(checkersdir)
-	OBJS += $(SYSFSLIB)-$(BUILD).a $(DMLIB)-$(BUILD).a
+	OBJS += $(libdm) $(libsysfs)
 else
-	CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes \
-		 -DDEBUG=$(DEBUG) -I$(multipathdir) -I$(checkersdir) \
-		 -DVERSION=$(VERSION)
-	LDFLAGS = -ldevmapper -lsysfs
+	LDFLAGS += -ldevmapper -lsysfs
 endif
 
 EXEC = multipath
-DEBUG = 0
 
 all: $(BUILD)
 
 prepare:
+	make -C $(multipathdir) clean
 	rm -f core *.o *.gz
 
 glibc: prepare $(OBJS)
@@ -33,8 +29,8 @@
 	$(STRIP) $(EXEC)
 	$(GZIP) $(EXEC).8 > $(EXEC).8.gz
 	
-klibc: prepare $(KLIBC) $(OBJS)
-	$(LD) -o $(EXEC) $(CRT0) $(OBJS) $(KLIBC) $(LIBGCC)
+klibc: prepare $(OBJS)
+	$(CC) -static -o $(EXEC) $(CRT0) $(OBJS) $(KLIBC) $(LIBGCC)
 	$(STRIP) $(EXEC)
 	$(GZIP) $(EXEC).8 > $(EXEC).8.gz
 
@@ -44,20 +40,10 @@
 $(MULTIPATHLIB)-$(BUILD).a:
 	make -C $(multipathdir) BUILD=$(BUILD) $(BUILD)
 
-$(SYSFSLIB)-$(BUILD).a:
-	make -C $(sysfsdir) BUILD=$(BUILD) $(BUILD)
-
-$(DMLIB)-$(BUILD).a:
-	make -C $(libdmdir) BUILD=$(BUILD) $(BUILD)
-
-$(KLIBC):
-	rm -f $(klibcdir)/linux
-	ln -s $(KERNEL_BUILD) $(klibcdir)/linux
-	make -C $(klibcdir)
-
 install:
 	install -d $(DESTDIR)$(bindir)
 	install -m 755 $(EXEC) $(DESTDIR)$(bindir)/
+	install -d $(DESTDIR)/var/cache/multipath/
 	install -d $(DESTDIR)/etc/dev.d/block/
 	install -m 755 multipath.dev $(DESTDIR)/etc/dev.d/block/
 	install -d $(DESTDIR)/etc/udev/rules.d

Modified: multipath-tools/trunk/multipath/main.c
==============================================================================
--- multipath-tools/trunk/multipath/main.c	(original)
+++ multipath-tools/trunk/multipath/main.c	Sat Aug 13 15:23:02 2005
@@ -19,151 +19,126 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <string.h>
 #include <signal.h>
-#include <sys/ioctl.h>
 
 #include <parser.h>
 #include <vector.h>
 #include <memory.h>
-#include <sysfs/dlist.h>
-#include <sysfs/libsysfs.h>
 #include <libdevmapper.h>
 #include <devmapper.h>
 #include <checkers.h>
 #include <path_state.h>
-#include <safe_printf.h>
 #include <blacklist.h>
 #include <hwtable.h>
 #include <util.h>
 #include <defaults.h>
 #include <structs.h>
+#include <dmparser.h>
+#include <cache.h>
+#include <config.h>
+#include <propsel.h>
+#include <discovery.h>
+#include <debug.h>
+#include <sysfs/libsysfs.h>
 
 #include "main.h"
-#include "devinfo.h"
-#include "config.h"
 #include "pgpolicies.h"
 #include "dict.h"
-#include "debug.h"
-#include "dmparser.h"
-#include "propsel.h"
 
-/* helpers */
-#define argis(x) if (0 == strcmp(x, argv[i]))
+static char *
+get_refwwid (vector pathvec)
+{
+	struct path * pp;
+	char buff[FILE_NAME_SIZE];
+	char * refwwid;
 
-static int
-filepresent (char * run) {
-	struct stat buf;
+	if (conf->dev_type == DEV_NONE)
+		return NULL;
 
-	if(!stat(run, &buf))
-		return 1;
-	return 0;
-}
+	if (conf->dev_type == DEV_DEVNODE) {
+		condlog(3, "limited scope = %s", conf->dev);
+		basename(conf->dev, buff);
+		pp = find_path_by_dev(pathvec, buff);
+		
+		if (!pp) {
+			pp = alloc_path();
 
-static int
-get_pathvec_sysfs (vector pathvec)
-{
-	struct sysfs_directory * sdir;
-	struct sysfs_directory * devp;
-	struct sysfs_link * linkp;
-	char refwwid[WWID_SIZE];
-	char empty_buff[WWID_SIZE];
-	char buff[FILE_NAME_SIZE];
-	char path[FILE_NAME_SIZE];
-	char sysfs_path[FILE_NAME_SIZE];
-	struct path * curpath;
+			if (!pp)
+				return NULL;
 
-	memset (empty_buff, 0, WWID_SIZE);
-	memset (refwwid, 0, WWID_SIZE);
+			if (store_path(pathvec, pp)) {
+				free_path(pp);
+				return NULL;
+			}
+			strncpy(pp->dev, buff, FILE_NAME_SIZE);
+			if (devinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
+				return NULL;
+		}
 
-	if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
-		fprintf(stderr, "multipath tools need sysfs\n");
-		exit(1);
-	}
-	
-	/*
-	 * if called from /etc/dev.d or pathcheckers, only consider the paths
-	 * that relate to the device pointed by conf->dev
-	 */
-	if (conf->dev && filepresent(conf->dev)) {
-		dbg("limited scope = %s", conf->dev);
-		curpath = zalloc(sizeof (struct path));
-		basename(conf->dev, curpath->dev);
+		refwwid = MALLOC(WWID_SIZE);
 
-		if (devinfo(curpath))
-			return 1;
+		if (!refwwid)
+			return NULL;
 
-		memcpy(refwwid, curpath->wwid, WWID_SIZE);
-		free(curpath);
+		memcpy(refwwid, pp->wwid, WWID_SIZE);
+		return refwwid;
 	}
 
-	/*
-	 * if devt specified on the cmd line,
-	 * only consider affiliated paths
-	 */
-	if (conf->dev && !devt2devname(buff, conf->dev)) {
-		dbg("limited scope = %s", conf->dev);
-		curpath = zalloc(sizeof (struct path));
-		if(safe_sprintf(curpath->dev, "%s", buff)) {
-			fprintf(stderr, "curpath->dev too small\n");
-			exit(1);
+	if (conf->dev_type == DEV_DEVT) {
+		condlog(3, "limited scope = %s", conf->dev);
+		pp = find_path_by_devt(pathvec, conf->dev);
+		
+		if (!pp) {
+			pp = alloc_path();
+
+			if (!pp)
+				return NULL;
+
+			if (store_path(pathvec, pp)) {
+				free_path(pp);
+				return NULL;
+			}
+			devt2devname(conf->dev, buff);
+
+			if(safe_sprintf(pp->dev, "%s", buff)) {
+				fprintf(stderr, "pp->dev too small\n");
+				exit(1);
+			}
+			if (devinfo(pp, conf->hwtable, DI_SYSFS | DI_WWID))
+				return NULL;
 		}
-		if (devinfo(curpath))
-			return 1;
 
-		memcpy(refwwid, curpath->wwid, WWID_SIZE);
-		free(curpath);
-	}
+		refwwid = MALLOC(WWID_SIZE);
+
+		if (!refwwid)
+			return NULL;
 		
-	if(safe_sprintf(path, "%s/block", sysfs_path)) {
-		fprintf(stderr, "path too small\n");
-		exit(1);
+		memcpy(refwwid, pp->wwid, WWID_SIZE);
+		return refwwid;
 	}
-	sdir = sysfs_open_directory(path);
-	sysfs_read_directory(sdir);
-
-	dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
-		if (blacklist(conf->blist, devp->name))
-			continue;
-
-		sysfs_read_directory(devp);
+	if (conf->dev_type == DEV_DEVMAP) {
+		condlog(3, "limited scope = %s", conf->dev);
+		/*
+		 * may be an alias
+		 */
+		refwwid = get_mpe_wwid(conf->dev);
 
-		if (devp->links == NULL)
-			continue;
+		if (refwwid)
+			return refwwid;
+		
+		/*
+		 * or directly a wwid
+		 */
+		refwwid = MALLOC(WWID_SIZE);
 
-		dlist_for_each_data(devp->links, linkp, struct sysfs_link) {
-			if (!strncmp(linkp->name, "device", 6))
-				break;
-		}
-		if (linkp == NULL) {
-			continue;
-		}
-		basename(devp->path, buff);
-		curpath = zalloc(sizeof(struct path));
+		if (!refwwid)
+			return NULL;
 
-		if(safe_sprintf(curpath->dev, "%s", buff)) {
-			fprintf(stderr, "curpath->dev too small\n");
-			exit(1);
-		}
-		if (devinfo(curpath)) {
-			free (curpath);
-			fprintf(stderr, "pb getting path info, free\n");
-			continue;
-		}
-		if (memcmp(empty_buff, refwwid, WWID_SIZE) != 0 && 
-		    memcmp(curpath->wwid, refwwid, WWID_SIZE) != 0) {
-			dbg("skip path %s : out of scope", curpath->dev);
-			free(curpath);
-			continue;
-		}
-		vector_alloc_slot(pathvec);
-		vector_set_slot(pathvec, curpath);
+		strncpy(refwwid, conf->dev, WWID_SIZE);
+		return refwwid;
 	}
-	sysfs_close_directory(sdir);
-	return 0;
+	return NULL;
 }
 
 /*
@@ -225,7 +200,6 @@
 	fprintf(stdout, "\n");
 }
 
-#if DEBUG
 static void
 print_map (struct multipath * mpp)
 {
@@ -239,31 +213,11 @@
 print_all_paths (vector pathvec)
 {
 	int i;
-	char empty_buff[WWID_SIZE];
 	struct path * pp;
 
-	/* initialize a cmp 0-filled buffer */
-	memset(empty_buff, 0, WWID_SIZE);
-
-	vector_foreach_slot (pathvec, pp, i) {
-		/* leave out paths with incomplete devinfo */
-		if (memcmp(empty_buff, pp->wwid, WWID_SIZE) == 0)
-			continue;
-
+	vector_foreach_slot (pathvec, pp, i)
 		print_path(pp, PRINT_PATH_ALL);
-	}
-}
-
-static void
-print_all_maps (vector mp)
-{
-	int i;
-	struct multipath * mpp;
-
-	vector_foreach_slot (mp, mpp, i)
-		print_map(mpp);
 }
-#endif
 
 static void
 print_mp (struct multipath * mpp)
@@ -356,157 +310,30 @@
 	printf("\n");
 }
 
-static void
-print_all_mp (vector mp)
-{
-	int k;
-	struct multipath * mpp;
-
-	if (conf->verbosity == 0)
-		return;
-
-	vector_foreach_slot (mp, mpp, k) 
-		print_mp(mpp);
-}
-
-static void
-coalesce_paths (vector mp, vector pathvec)
-{
-	int k, i;
-	char empty_buff[WWID_SIZE];
-	struct multipath * mpp;
-	struct path * pp1;
-	struct path * pp2;
-
-	memset(empty_buff, 0, WWID_SIZE);
-
-	vector_foreach_slot (pathvec, pp1, k) {
-		/* skip this path for some reason */
-
-		/* 1. if path has no unique id */
-		if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0)
-			continue;
-
-		/* 2. if path already coalesced */
-		if (pp1->mpp)
-			continue;
-
-		/*
-		 * at this point, we know we really got a new mp
-		 */
-		mpp = zalloc(sizeof(struct multipath));
-		pp1->mpp = mpp;
-		strcpy(mpp->wwid, pp1->wwid);
-		mpp->size = pp1->size;
-
-		mpp->mpe = find_mp(pp1->wwid);
-		mpp->hwe = find_hw(conf->hwtable,
-				   pp1->vendor_id, pp1->product_id);
-
-		mpp->paths = vector_alloc();
-		vector_alloc_slot (mpp->paths);
-		vector_set_slot (mpp->paths, pp1);
-
-		for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
-			pp2 = VECTOR_SLOT(pathvec, i);
-
-			if (strcmp(pp1->wwid, pp2->wwid))
-				continue;
-			
-			pp2->mpp = mpp;
-
-			if (pp2->size != mpp->size) {
-				/*
-				 * ouch, avoid feeding that to the DM
-				 */
-				dbg("path size mismatch : discard %s",
-				     mpp->wwid);
-				mpp->action = ACT_NOTHING;
-			}
-			vector_alloc_slot(mpp->paths);
-			vector_set_slot(mpp->paths, VECTOR_SLOT(pathvec, i));
-		}
-		if (mpp) {
-			vector_alloc_slot(mp);
-			vector_set_slot(mp, mpp);
-		}
-	}
-}
-
-static int
-dm_switchgroup(char * mapname, int index)
-{
-	int r = 0;
-	struct dm_task *dmt;
-	char str[24];
-
-	if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
-		return 0;
-
-	if (!dm_task_set_name(dmt, mapname))
-		goto out;
-
-	if (!dm_task_set_sector(dmt, 0))
-		goto out;
-
-	snprintf(str, 24, "switch_group %i\n", index);
-	dbg("message %s 0 %s", mapname, str);
-
-	if (!dm_task_set_message(dmt, str))
-		goto out;
-
-	if (!dm_task_run(dmt))
-		goto out;
-
-	r = 1;
-
-	out:
-	dm_task_destroy(dmt);
-
-	return r;
-}
 static int
-dm_reinstate(char * mapname, char * path)
+filter_pathvec (vector pathvec, char * refwwid)
 {
-	int r = 0;
-	int sz;
-	struct dm_task *dmt;
-	char *str;
+	int i;
+	struct path * pp;
 
-	if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
+	if (!refwwid || !strlen(refwwid))
 		return 0;
 
-	if (!dm_task_set_name(dmt, mapname))
-		goto out;
-
-	if (!dm_task_set_sector(dmt, 0))
-		goto out;
-
-	sz = strlen(path) + 16;
-	str = zalloc(sz);
-
-	snprintf(str, sz, "reinstate_path %s\n", path);
-
-	if (!dm_task_set_message(dmt, str))
-		goto out;
-
-	free(str);
-
-	if (!dm_task_run(dmt))
-		goto out;
-
-	r = 1;
-
-	out:
-	dm_task_destroy(dmt);
-
-	return r;
+	vector_foreach_slot (pathvec, pp, i) {
+		if (memcmp(pp->wwid, refwwid, WWID_SIZE) != 0) {
+			condlog(3, "skip path %s : out of scope", pp->dev);
+			free_path(pp);
+			vector_del_slot(pathvec, i);
+			i--;
+		}
+	}
+	return 0;
 }
 
 /*
  * Transforms the path group vector into a proper device map string
  */
-void
+int
 assemble_map (struct multipath * mp)
 {
 	int i, j;
@@ -524,7 +351,7 @@
 
 	if (shift >= freechar) {
 		fprintf(stderr, "mp->params too small\n");
-		exit(1);
+		return 1;
 	}
 	p += shift;
 	freechar -= shift;
@@ -535,16 +362,17 @@
 				 VECTOR_SIZE(pgp->paths));
 		if (shift >= freechar) {
 			fprintf(stderr, "mp->params too small\n");
-			exit(1);
+			return 1;
 		}
 		p += shift;
 		freechar -= shift;
 
 		vector_foreach_slot (pgp->paths, pp, j) {
-			shift = snprintf(p, freechar, " %s 1000" ,pp->dev_t);
+			shift = snprintf(p, freechar, " %s %d",
+					 pp->dev_t, conf->minio);
 			if (shift >= freechar) {
 				fprintf(stderr, "mp->params too small\n");
-				exit(1);
+				return 1;
 			}
 			p += shift;
 			freechar -= shift;
@@ -552,9 +380,14 @@
 	}
 	if (freechar < 1) {
 		fprintf(stderr, "mp->params too small\n");
-		exit(1);
+		return 1;
 	}
 	snprintf(p, 1, "\n");
+
+	if (conf->verbosity > 2)
+		print_map(mp);
+
+	return 0;
 }
 
 static int
@@ -568,16 +401,21 @@
 	/*
 	 * don't bother if devmap size is unknown
 	 */
-	if (mpp->size <= 0)
+	if (mpp->size <= 0) {
+		condlog(3, "%s devmap size is unknown", mpp->alias);
 		return 1;
+	}
 
 	/*
 	 * don't bother if a constituant path is claimed
 	 * FIXME : claimed detection broken, always unclaimed for now
 	 */
-	vector_foreach_slot (mpp->paths, pp, i)
-		if (pp->claimed)
+	vector_foreach_slot (mpp->paths, pp, i) {
+		if (pp->claimed) {
+			condlog(3, "%s claimed", pp->dev);
 			return 1;
+		}
+	}
 
 	/*
 	 * properties selectors
@@ -586,7 +424,6 @@
 	select_selector(mpp);
 	select_features(mpp);
 	select_hwhandler(mpp);
-	select_alias(mpp);
 
 	/*
 	 * apply selected grouping policy to valid paths
@@ -610,8 +447,9 @@
 	default:
 		break;
 	}
+
 	if (mpp->pg == NULL) {
-		dbg("pgpolicy failed to produce a ->pg vector");
+		condlog(3, "pgpolicy failed to produce a pg vector");
 		return 1;
 	}
 
@@ -635,10 +473,28 @@
 	 * transform the mp->pg vector of vectors of paths
 	 * into a mp->params strings to feed the device-mapper
 	 */
-	assemble_map(mpp);
+	if (assemble_map(mpp)) {
+		condlog(3, "problem assembing map");
+		return 1;
+	}
 	return 0;
 }
 
+static int
+pathcount (struct multipath * mpp, int state)
+{
+	struct pathgroup *pgp;
+	struct path *pp;
+	int i, j;
+	int count = 0;
+
+	vector_foreach_slot (mpp->pg, pgp, i)
+		vector_foreach_slot (pgp->paths, pp, j)
+			if (pp->state == state)
+				count++;
+	return count;
+}
+
 /*
  * detect if a path is in the map we are about to create but not in the
  * current one (triggers a valid reload)
@@ -667,92 +523,70 @@
 				if (found)
 					break;
 			}
-			if (found) {
+			if (found)
 				found = 0;
-				break;
-			} else
+			else
 				return 1;
 		}
 	}
 	return 0;
 }
 
-#if 0
-static int
-pgcmp (struct multipath * mpp, struct multipath * cmpp)
-{
-	int i, j;
-	struct pathgroup * pgp;
-	struct pathgroup * cpgp;
-	int r = 0;
-
-	vector_foreach_slot (mpp->pg, pgp, i) {
-		vector_foreach_slot (cmpp->pg, cpgp, j) {
-			if (pgp->id == cpgp->id) {
-				r = 0;
-				break;
-			}
-			r++;
-		}
-		if (r)
-			return r;
-	}
-	return r;
-}
-#endif
-
 static void
 select_action (struct multipath * mpp, vector curmp)
 {
-	int i;
 	struct multipath * cmpp;
 
-	vector_foreach_slot (curmp, cmpp, i) {
-		if (strncmp(cmpp->alias, mpp->alias, strlen(mpp->alias)))
-			continue;
+	cmpp = find_mp(curmp, mpp->alias);
 
-		if (cmpp->size != mpp->size) {
-			dbg("size different than current");
-			mpp->action = ACT_RELOAD;
-			return;
-		}
-		if (strncmp(cmpp->features, mpp->features,
-			    strlen(mpp->features))) {
-			dbg("features different than current");
-			mpp->action =  ACT_RELOAD;
-			return;
-		}
-		if (strncmp(cmpp->hwhandler, mpp->hwhandler,
-			    strlen(mpp->hwhandler))) {
-			dbg("hwhandler different than current");
-			mpp->action = ACT_RELOAD;
-			return;
-		}
-		if (strncmp(cmpp->selector, mpp->selector,
-			    strlen(mpp->selector))) {
-			dbg("selector different than current");
-			mpp->action = ACT_RELOAD;
-			return;
-		}
-		if (VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
-			dbg("different number of PG");
-			mpp->action = ACT_RELOAD;
-			return;
-		}
-		if (pgcmp2(mpp, cmpp)) {
-			dbg("different path group topology");
-			mpp->action = ACT_RELOAD;
-			return;
-		}
-		if (cmpp->nextpg != mpp->nextpg) {
-			dbg("nextpg different than current");
-			mpp->action = ACT_SWITCHPG;
-			return;
-		}
+	if (!cmpp) {
+		mpp->action = ACT_CREATE;
+		return;
+	}
+	if (pathcount(mpp, PATH_UP) == 0) {
+		condlog(3, "no good path");
 		mpp->action = ACT_NOTHING;
 		return;
 	}
-	mpp->action = ACT_CREATE;
+	if (cmpp->size != mpp->size) {
+		condlog(3, "size different than current");
+		mpp->action = ACT_RELOAD;
+		return;
+	}
+	if (strncmp(cmpp->features, mpp->features,
+		    strlen(mpp->features))) {
+		condlog(3, "features different than current");
+		mpp->action =  ACT_RELOAD;
+		return;
+	}
+	if (strncmp(cmpp->hwhandler, mpp->hwhandler,
+		    strlen(mpp->hwhandler))) {
+		condlog(3, "hwhandler different than current");
+		mpp->action = ACT_RELOAD;
+		return;
+	}
+	if (strncmp(cmpp->selector, mpp->selector,
+		    strlen(mpp->selector))) {
+		condlog(3, "selector different than current");
+		mpp->action = ACT_RELOAD;
+		return;
+	}
+	if (VECTOR_SIZE(cmpp->pg) != VECTOR_SIZE(mpp->pg)) {
+		condlog(3, "different number of PG");
+		mpp->action = ACT_RELOAD;
+		return;
+	}
+	if (pgcmp2(mpp, cmpp)) {
+		condlog(3, "different path group topology");
+		mpp->action = ACT_RELOAD;
+		return;
+	}
+	if (cmpp->nextpg != mpp->nextpg) {
+		condlog(3, "nextpg different than current");
+		mpp->action = ACT_SWITCHPG;
+		return;
+	}
+	mpp->action = ACT_NOTHING;
 	return;
 }
 
@@ -771,7 +605,9 @@
 				continue;
 
 			if (pp->dmstate == PSTATE_FAILED) {
-				dm_reinstate(mpp->alias, pp->dev_t);
+				if (dm_reinstate(mpp->alias, pp->dev_t))
+					condlog(0, "error reinstating %s",
+						pp->dev);
 			}
 		}
 	}
@@ -781,7 +617,7 @@
 static int
 domap (struct multipath * mpp)
 {
-	int op;
+	int op = ACT_NOTHING;
 	int r = 0;
 
 	print_mp(mpp);
@@ -796,7 +632,8 @@
 		dm_switchgroup(mpp->alias, mpp->nextpg);
 		/*
 		 * we may have avoided reinstating paths because there where in
-		 * active or disabled PG. Now that the topology has changed, retry.
+		 * active or disabled PG. Now that the topology has changed,
+		 * retry.
 		 */
 		reinstate_paths(mpp);
 		return 0;
@@ -834,56 +671,91 @@
 }
 
 static int
-dm_get_maps (vector mp, char * type)
+coalesce_paths (vector curmp, vector pathvec)
 {
+	int k, i;
+	char empty_buff[WWID_SIZE];
 	struct multipath * mpp;
-	int r = 0;
-	struct dm_task *dmt;
-	struct dm_names *names;
-	unsigned next = 0;
-	unsigned long length;
-	char *params;
-	char *status;
+	struct path * pp1;
+	struct path * pp2;
 
-	if (!(dmt = dm_task_create (DM_DEVICE_LIST)))
-		return 0;
+	memset(empty_buff, 0, WWID_SIZE);
 
-	if (!dm_task_run (dmt))
-		goto out;
+	vector_foreach_slot (pathvec, pp1, k) {
+		/* skip this path for some reason */
 
-	if (!(names = dm_task_get_names (dmt)))
-		goto out;
+		/* 1. if path has no unique id or wwid blacklisted */
+		if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
+		    blacklist(conf->blist, pp1->wwid))
+			continue;
 
-	if (!names->dev)
-		goto out;
+		/* 2. if path already coalesced */
+		if (pp1->mpp)
+			continue;
+
+		/*
+		 * at this point, we know we really got a new mp
+		 */
+		mpp = alloc_multipath();
 
-	do {
-		if (dm_type(names->name, DEFAULT_TARGET)) {
-			dm_get_map(names->name, &length, &params);
-			dm_get_status(names->name, &status);
-			mpp = zalloc(sizeof(struct multipath));
-
-			if (!mpp) {
-				r = 1;
-				goto out;
+		if (!mpp)
+			return 1;
+
+		mpp->mpe = find_mpe(pp1->wwid);
+		mpp->hwe = pp1->hwe;
+		select_alias(mpp);
+
+		pp1->mpp = mpp;
+		strcpy(mpp->wwid, pp1->wwid);
+		mpp->size = pp1->size;
+		mpp->paths = vector_alloc();
+
+		if (pp1->priority < 0)
+			mpp->action = ACT_NOTHING;
+
+		if (!mpp->paths)
+			return 1;
+		
+		if (store_path(mpp->paths, pp1))
+			return 1;
+
+		for (i = k + 1; i < VECTOR_SIZE(pathvec); i++) {
+			pp2 = VECTOR_SLOT(pathvec, i);
+
+			if (strcmp(pp1->wwid, pp2->wwid))
+				continue;
+			
+			pp2->mpp = mpp;
+
+			if (pp2->size != mpp->size) {
+				/*
+				 * ouch, avoid feeding that to the DM
+				 */
+				condlog(3, "path size mismatch : discard %s",
+				     mpp->wwid);
+				mpp->action = ACT_NOTHING;
 			}
-			mpp->size = length;
-			mpp->alias = zalloc(strlen(names->name) + 1);
-			strncat(mpp->alias, names->name, strlen(names->name));
-			strncat(mpp->params, params, PARAMS_SIZE);
-			strncat(mpp->status, status, PARAMS_SIZE);
-
-			vector_alloc_slot(mp);
-			vector_set_slot(mp, mpp);
-			mpp = NULL;
+			if (pp2->priority < 0)
+				mpp->action = ACT_NOTHING;
+
+			if (store_path(mpp->paths, pp2))
+				return 1;
 		}
-                next = names->next;
-                names = (void *) names + next;
-	} while (next);
+		if (setup_map(mpp)) {
+			free_multipath(mpp, KEEP_PATHS);
+			continue;
+		}
+		condlog(3, "action preset to %i", mpp->action);
 
-	out:
-	dm_task_destroy (dmt);
-	return r;
+		if (mpp->action == ACT_UNDEF)
+			select_action(mpp, curmp);
+
+		condlog(3, "action set to %i", mpp->action);
+
+		domap(mpp);
+		free_multipath(mpp, KEEP_PATHS);
+	}
+	return 0;
 }
 
 static void
@@ -893,31 +765,33 @@
 	pid_t pid;
 	char *buf;
 
-	buf = malloc (8);
+	buf = MALLOC(8);
+
+	if (!buf)
+		return;
 
-	file = fopen (DEFAULT_PIDFILE, "r");
+	file = fopen(DEFAULT_PIDFILE, "r");
 
 	if (!file) {
-		if (conf->verbosity > 0)
-			fprintf (stderr, "cannot signal daemon, "
-					 "pidfile not found\n");
+		condlog(1, "cannot signal daemon, pidfile not found");
+		FREE(buf);
 		return;
 	}
 
-	buf = fgets (buf, 8, file);
-	fclose (file);
+	buf = fgets(buf, 8, file);
+	fclose(file);
 
-	pid = (pid_t) atol (buf);
-	free (buf);
+	pid = (pid_t)atol(buf);
+	FREE(buf);
 
-	kill (pid, SIGHUP);
+	kill(pid, SIGHUP);
 }
 
 static void
 usage (char * progname)
 {
 	fprintf (stderr, VERSION_STRING);
-	fprintf (stderr, "Usage: %s\t[-v level] [-d] [-S]\n",
+	fprintf (stderr, "Usage: %s\t[-v level] [-d] [-l] [-S]\n",
 		progname);
 	fprintf (stderr,
 		"\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
@@ -926,8 +800,10 @@
 		"\t-v level\tverbosty level\n" \
 		"\t   0\t\t\tno output\n" \
 		"\t   1\t\t\tprint created devmap names only\n" \
-		"\t   2\t\t\tprint all paths and multipaths\n" \
+		"\t   2\t\t\tdefault verbosity\n" \
+		"\t   3\t\t\tprint debug information\n" \
 		"\t-d\t\tdry run, do not create or update devmaps\n" \
+		"\t-l\t\tlist the current multipath topology\n" \
 		"\t-S\t\tinhibit signal sending to multipathd\n"
 		"\t-F\t\tflush all multipath device maps\n" \
 		"\t-p policy\tforce all maps to specified policy :\n" \
@@ -945,88 +821,84 @@
 	exit(1);
 }
 
-int try_lock (char * file)
+static int
+update_pathvec (vector pathvec)
 {
-	int fd;
-	struct flock fl;
-
-	/*
-	 * create the file to lock if it does not exist
-	 */
-	fd = open(file, O_CREAT|O_RDWR);
+	int i;
+	struct path * pp;
 
-	if (fd < 0) {
-		fprintf(stderr, "can't create runfile\n");
-		exit(1);
+	vector_foreach_slot (pathvec, pp, i) {
+		if (pp->dev && pp->dev_t && strlen(pp->dev) == 0) {
+			devt2devname(pp->dev, pp->dev_t);
+			devinfo(pp, conf->hwtable,
+				DI_SYSFS | DI_CHECKER | DI_SERIAL | DI_PRIO);
+		}
+		if (pp->checkfn && pp->state == PATH_UNCHECKED)
+			pp->state = pp->checkfn(pp->fd, NULL, NULL);
 	}
-	fl.l_type = F_WRLCK;
-	fl.l_whence = 0;
-	fl.l_start = 0;
-	fl.l_len = 0;
+	return 0;
+}
 
-	/*
-	 * set a max wait time
-	 */
-	alarm(2);
-	
-	if (fcntl(fd, F_SETLKW, &fl) == -1) {
-		fprintf(stderr, "can't take a write lease on %s\n", file);
+static int
+get_current_mp (vector curmp, vector pathvec, char * refwwid)
+{
+	int i;
+	struct multipath * mpp;
+	char * wwid;
+
+	if (dm_get_maps(curmp, DEFAULT_TARGET))
 		return 1;
-	}
-	alarm(0);
 
+	vector_foreach_slot (curmp, mpp, i) {
+		wwid = get_mpe_wwid(mpp->alias);
+
+		if (wwid) {
+			strncpy(mpp->wwid, wwid, WWID_SIZE);
+			wwid = NULL;
+		} else
+			strncpy(mpp->wwid, mpp->alias, WWID_SIZE);
+
+		if (refwwid && strncmp(mpp->wwid, refwwid, WWID_SIZE))
+			continue;
+
+		condlog(3, "params = %s", mpp->params);
+		condlog(3, "status = %s", mpp->status);
+		disassemble_map(pathvec, mpp->params, mpp);
+		update_pathvec(pathvec);
+		disassemble_status(mpp->status, mpp);
+
+		if (conf->list)
+			print_mp(mpp);
+
+		if (!conf->dry_run)
+			reinstate_paths(mpp);
+	}
 	return 0;
 }
 
 int
 main (int argc, char *argv[])
 {
-	vector mp, curmp;
-	vector pathvec;
-	struct multipath * mpp;
-	int k;
+	vector curmp = NULL;
+	vector pathvec = NULL;
+	int i;
 	int arg;
 	extern char *optarg;
 	extern int optind;
+	char * refwwid = NULL;
 
 	if (dm_prereq(DEFAULT_TARGET, 1, 0, 3)) {
-		fprintf(stderr, "device mapper prerequisites not met.\n");
+		condlog(0, "device mapper prerequisites not met");
 		exit(1);
 	}
-
-	/*
-	 * Don't run in parallel
-	 * ie, acquire a F_WRLCK-type lock on DEFAULT_RUNFILE
-	 */
-	if (try_lock(DEFAULT_RUNFILE)) {
-		fprintf(stderr, "waited for to long. exiting\n");
+	if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
+		condlog(0, "multipath tools need sysfs mounted");
 		exit(1);
 	}
-	
-	/*
-	 * alloc config struct
-	 */
-	conf = zalloc(sizeof(struct config));
-				
-	/*
-	 * internal defaults
-	 */
-	conf->list = 0;
-	conf->dry_run = 0;		/* 1 == Do not Create/Update devmaps */
-	conf->verbosity = 1;
-	conf->pgpolicy_flag = 0;	/* do not override defaults */
-	conf->signal = 1;		/* 1 == Send a signal to multipathd */
-	conf->dev = NULL;
-	conf->default_selector = NULL;
-	conf->default_selector_args = 0;
-	conf->default_pgpolicy = 0;
-	conf->mptable = NULL;
-	conf->hwtable = NULL;
-	conf->blist = NULL;
-	conf->default_features = NULL;
-	conf->default_hwhandler = NULL;
+	if (load_config(DEFAULT_CONFIGFILE))
+		exit(1);
 
-	while ((arg = getopt(argc, argv, ":qdlFSi:v:p:")) != EOF ) {
+	while ((arg = getopt(argc, argv, ":qdlFSi:M:v:p:")) != EOF ) {
 		switch(arg) {
 		case 1: printf("optarg : %s\n",optarg);
 			break;
@@ -1050,6 +922,11 @@
 			conf->dry_run = 1;
 			conf->signal = 0;
 			break;
+		case 'M':
+#if _DEBUG_
+			debug = atoi(optarg);
+#endif
+			break;
 		case 'S':
 			conf->signal = 0;
 			break;
@@ -1070,125 +947,72 @@
 			usage(argv[0]);
 		}
 	}        
-	if (optind<argc) {
-		conf->dev = zalloc(FILE_NAME_SIZE);
+	if (optind < argc) {
+		conf->dev = MALLOC(FILE_NAME_SIZE);
+
+		if (!conf->dev)
+			goto out;
+
 		strncpy(conf->dev, argv[optind], FILE_NAME_SIZE);
+
+		if (filepresent(conf->dev))
+			conf->dev_type = DEV_DEVNODE;
+		else if (sscanf(conf->dev, "%d:%d", &i, &i) == 2)
+			conf->dev_type = DEV_DEVT;
+		else
+			conf->dev_type = DEV_DEVMAP;
+
 	}
 
 	/*
 	 * allocate core vectors to store paths and multipaths
 	 */
-	mp = vector_alloc();
 	curmp = vector_alloc();
 	pathvec = vector_alloc();
 
-	if (!mp || !curmp || !pathvec) {
-		fprintf(stderr, "can not allocate memory\n");
-		exit(1);
+	if (!curmp || !pathvec) {
+		condlog(0, "can not allocate memory");
+		goto out;
 	}
 
 	/*
-	 * read the config file
+	 * if we have a blacklisted device parameter, exit early
 	 */
-	if (filepresent(DEFAULT_CONFIGFILE))
-		init_data (DEFAULT_CONFIGFILE, init_keywords);
+	if (conf->dev && blacklist(conf->blist, conf->dev))
+		goto out;
 	
-	/*
-	 * fill the voids left in the config file
-	 */
-	if (conf->hwtable == NULL) {
-		conf->hwtable = vector_alloc();
-		setup_default_hwtable(conf->hwtable);
+	if (!cache_cold(CACHE_EXPIRE)) {
+		condlog(3, "load path identifiers cache");
+		cache_load(pathvec);
 	}
-	if (conf->blist == NULL) {
-		conf->blist = vector_alloc();
-		setup_default_blist(conf->blist);
-	}
-	if (conf->mptable == NULL)
-		conf->mptable = vector_alloc();
-
-	if (conf->default_selector == NULL)
-		conf->default_selector = DEFAULT_SELECTOR;
-
-	if (conf->udev_dir == NULL)
-		conf->udev_dir = DEFAULT_UDEVDIR;
-
-	if (conf->default_getuid == NULL)
-		conf->default_getuid = DEFAULT_GETUID;
-
-	if (conf->default_features == NULL)
-		conf->default_features = DEFAULT_FEATURES;
-
-	if (conf->default_hwhandler == NULL)
-		conf->default_hwhandler = DEFAULT_HWHANDLER;
 
 	/*
-	 * if we have a blacklisted device parameterexit early
+	 * get a path list
 	 */
-	if (conf->dev && blacklist(conf->blist, conf->dev))
-		exit(0);
-	
-	/*
-	 * get a path list and group them as multipaths
-	 */
-	if (get_pathvec_sysfs(pathvec) || VECTOR_SIZE(pathvec) == 0)
-		exit(1);
-
-	dm_get_maps(curmp, DEFAULT_TARGET);
+	if (path_discovery(pathvec, conf, DI_CHECKER) || VECTOR_SIZE(pathvec) == 0)
+		goto out;
 
-	if (VECTOR_SIZE(pathvec) == 0 && conf->verbosity > 0) {
-		fprintf(stdout, "no path found\n");
-		exit(0);
+	if (conf->verbosity > 2) {
+		fprintf(stdout, "#\n# all paths :\n#\n");
+		print_all_paths(pathvec);
 	}
 
-	vector_foreach_slot (curmp, mpp, k) {
-		dbg("params = %s", mpp->params);
-		dbg("status = %s", mpp->status);
-		disassemble_map(pathvec, mpp->params, mpp);
-		disassemble_status(mpp->status, mpp);
+	refwwid = get_refwwid(pathvec);
 
-		if (!conf->dry_run)
-			reinstate_paths(mpp);
-	}
-	if (conf->list) {
-		print_all_mp(curmp);
+	if (get_current_mp(curmp, pathvec, refwwid))
 		goto out;
-	}
 
-	coalesce_paths(mp, pathvec);
+	cache_dump(pathvec);
+	filter_pathvec(pathvec, refwwid);
+
+	if (conf->list)
+		goto out;
 
 	/*
-	 * conf->dev can be a mapname
-	 * if so, only reconfigure this map
+	 * core logic entry point
 	 */
-	vector_foreach_slot (mp, mpp, k) {
-		if (conf->dev && (
-		    (mpp->alias &&
-		    0 == strncmp(mpp->alias, conf->dev, FILE_NAME_SIZE)) ||
-		    0 == strncmp(mpp->wwid, conf->dev, FILE_NAME_SIZE))) {
-			setup_map(mpp);
-
-			if (mpp->action == 0)
-				select_action(mpp, curmp);
-			domap(mpp);
-			goto out;
-		}
-	}
+	coalesce_paths(curmp, pathvec);
 
-	vector_foreach_slot (mp, mpp, k) {
-		setup_map(mpp);
-
-		if (mpp->action == 0)
-			select_action(mpp, curmp);
-		domap(mpp);
-	}
-
-#if DEBUG
-	fprintf(stdout, "#\n# all paths :\n#\n");
-	print_all_paths(pathvec);
-	fprintf(stdout, "#\n# device maps :\n#\n");
-	print_all_maps(mp);
-#endif
 out:
 	/*
 	 * signal multipathd that new devmaps may have come up
@@ -1196,5 +1020,14 @@
 	if (conf->signal)
 		signal_daemon();
 	
+	if (refwwid)
+		FREE(refwwid);
+
+	free_multipathvec(curmp, KEEP_PATHS);
+	free_pathvec(pathvec, FREE_PATHS);
+	free_config(conf);
+#ifdef _DEBUG_
+	dbg_free_final(NULL);
+#endif
 	exit(0);
 }

Modified: multipath-tools/trunk/multipath/main.h
==============================================================================
--- multipath-tools/trunk/multipath/main.h	(original)
+++ multipath-tools/trunk/multipath/main.h	Sat Aug 13 15:23:02 2005
@@ -28,7 +28,7 @@
 #define ACT_CREATE_STR		"create"
 
 enum actions {
-	ACT_RESERVED,
+	ACT_UNDEF,
 	ACT_NOTHING,
 	ACT_RELOAD,
 	ACT_SWITCHPG,
@@ -40,8 +40,8 @@
  */
 #define PROG    "multipath"
 
-#define VERSION_CODE 0x000402
-#define DATE_CODE    0x170105
+#define VERSION_CODE 0x000404
+#define DATE_CODE    0x100405
 
 #define MULTIPATH_VERSION(version)	\
 	(version >> 16) & 0xFF,		\

Modified: multipath-tools/trunk/multipath/multipath.dev
==============================================================================
--- multipath-tools/trunk/multipath/multipath.dev	(original)
+++ multipath-tools/trunk/multipath/multipath.dev	Sat Aug 13 15:23:02 2005
@@ -1,15 +1,13 @@
 #!/bin/sh -e
 
-map=$(/sbin/multipath -S -v 1 $DEVNAME)
-
-if [ -z $map ]
-then
+if [ ! "${ACTION}" = add ] ; then
 	exit
 fi
 
-echo "path $DEVNAME : multipath $map"
-
-if [ -b /dev/$map ] 
-then
-	kpartx -v -a /dev/$map
+if [ "${DEVPATH:7:3}" = "dm-" ] ; then
+	dev=$(</sys${DEVPATH}/dev)
+	map=$(/sbin/devmap_name $dev)
+	/sbin/kpartx -v -a /dev/$map
+else
+	/sbin/multipath -v0 ${DEVNAME}
 fi

Modified: multipath-tools/trunk/multipathd/Makefile
==============================================================================
--- multipath-tools/trunk/multipathd/Makefile	(original)
+++ multipath-tools/trunk/multipathd/Makefile	Sat Aug 13 15:23:02 2005
@@ -1,12 +1,6 @@
 BUILD = glibc
 EXEC = multipathd
 
-#
-# debug verbosity, same as syslog.h
-# nothing to below 5, full debug at 7
-#
-LOGLEVEL = 7
-
 include ../Makefile.inc
 
 #
@@ -19,16 +13,17 @@
 #
 # basic flags setting
 #
-CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes -DLOGLEVEL=$(LOGLEVEL) \
-	 -I$(multipathdir) -I$(checkersdir)
+CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes \
+	 -DDAEMON -I$(multipathdir) -I$(checkersdir)
 LDFLAGS = -lpthread -ldevmapper -lsysfs
 
 #
 # object files
 #
-OBJS = main.o copy.o devinfo.o dict.o \
-       $(CHECKERSLIB)-glibc.a \
+OBJS = main.o copy.o log.o log_pthread.o pidfile.o \
        $(MULTIPATHLIB)-glibc.a \
+       $(CHECKERSLIB)-glibc.a \
+
 
 #
 # directives
@@ -37,9 +32,10 @@
 
 glibc: $(EXEC)
 
-klibc: glibc
+klibc:
+	$(MAKE) BUILD=glibc glibc
 
-$(EXEC): $(OBJS)
+$(EXEC): clean $(OBJS)
 	$(CC) $(OBJS) -o $(EXEC) $(LDFLAGS)
 	strip $(EXEC)
 	$(GZIP) $(EXEC).8 > $(EXEC).8.gz
@@ -48,13 +44,12 @@
 	$(MAKE) -C $(checkersdir) BUILD=glibc glibc
 
 $(MULTIPATHLIB)-glibc.a:
-	$(MAKE) -C $(multipathdir) BUILD=glibc glibc
+	$(MAKE) -C $(multipathdir) DAEMON=1 BUILD=glibc glibc
 
 install:
 	install -d $(DESTDIR)$(bindir)
 	install -m 755 $(EXEC) $(DESTDIR)$(bindir)
 	install -d $(DESTDIR)$(rcdir)
-	install -m 755 multipathd.init $(DESTDIR)$(rcdir)/$(EXEC)
 	install -d $(DESTDIR)$(mandir)
 	install -m 644 $(EXEC).8.gz $(DESTDIR)$(mandir)
 
@@ -64,5 +59,6 @@
 	rm -f $(DESTDIR)$(mandir)/$(EXEC).8.gz
 
 clean:
+	$(MAKE) -C $(multipathdir) clean
 	rm -f core *.o $(EXEC) *.gz
 

Modified: multipath-tools/trunk/multipathd/copy.c
==============================================================================
--- multipath-tools/trunk/multipathd/copy.c	(original)
+++ multipath-tools/trunk/multipathd/copy.c	Sat Aug 13 15:23:02 2005
@@ -6,10 +6,11 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <string.h>
-#include <syslog.h>
 #include <errno.h>
 #include <util.h>
 
+#include "log_pthread.h"
+
 #define FILESIZE 128
 
 int
@@ -24,14 +25,14 @@
 	fdin = open (src, O_RDONLY);
 
 	if (fdin < 0) {
-		syslog(3, "[copy.c] cannot open %s", src);
+		log_safe(3, "[copy.c] cannot open %s", src);
 		return -1;
 	}
 	/*
 	 * Stat the input file to obtain its size
 	 */
 	if (fstat (fdin, &statbuf) < 0) {
-		syslog(3, "[copy.c] cannot stat %s", src);
+		log_safe(3, "[copy.c] cannot stat %s", src);
 		goto out1;
 	}
 	/*
@@ -41,17 +42,17 @@
 	fdout = open (dst, O_RDWR | O_CREAT | O_TRUNC, statbuf.st_mode);
 
 	if (fdout < 0) {
-		syslog(3, "[copy.c] cannot open %s", dst);
+		log_safe(3, "[copy.c] cannot open %s", dst);
 		goto out1;
 	}
 
 	if (lseek (fdout, statbuf.st_size - 1, SEEK_SET) == -1) {
-		syslog(3, "[copy.c] cannot lseek %s", dst);
+		log_safe(3, "[copy.c] cannot lseek %s", dst);
 		goto out2;
 	}
 
 	if (write (fdout, "", 1) != 1) {
-		syslog(3, "[copy.c] cannot write dummy char");
+		log_safe(3, "[copy.c] cannot write dummy char");
 		goto out2;
 	}
 	/*
@@ -59,13 +60,13 @@
 	 */
 	if ((mmsrc = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0))
 		== (caddr_t) -1) {
-		syslog(3, "[copy.c] cannot mmap %s", src);
+		log_safe(3, "[copy.c] cannot mmap %s", src);
 		goto out2;
 	}
 	
 	if ((mmdst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
 		MAP_SHARED, fdout, 0)) == (caddr_t) -1) {
-		syslog(3, "[copy.c] cannot mmap %s", dst);
+		log_safe(3, "[copy.c] cannot mmap %s", dst);
 		goto out3;
 	}
 	memcpy(mmdst, mmsrc, statbuf.st_size);
@@ -90,7 +91,7 @@
 	
 	basename(src, filename);
 	if (FILESIZE <= snprintf(dst, FILESIZE, "%s/%s", dstdir, filename)) {
-		syslog(3, "[copy.c] filename buffer overflow : %s ", filename);
+		log_safe(3, "[copy.c] filename buffer overflow : %s ", filename);
 		return -1;
 	}
 

Added: multipath-tools/trunk/multipathd/log.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/multipathd/log.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,181 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <syslog.h>
+
+#include "log.h"
+
+#if LOGDBG
+static void dump_logarea (void)
+{
+	struct logmsg * msg;
+	
+	logdbg(stderr, "\n==== area: start addr = %p, end addr = %p ====\n",
+		la->start, la->end);
+	logdbg(stderr, "|addr     |next     |prio|msg\n");
+
+	for (msg = (struct logmsg *)la->head; (void *)msg != la->tail;
+	     msg = msg->next)
+		logdbg(stderr, "|%p |%p |%i   |%s\n", (void *)msg, msg->next,
+				msg->prio, (char *)&msg->str);
+
+	logdbg(stderr, "|%p |%p |%i   |%s\n", (void *)msg, msg->next,
+			msg->prio, (char *)&msg->str);
+
+	logdbg(stderr, "\n\n");
+}
+#endif
+		
+static int logarea_init (int size)
+{
+	logdbg(stderr,"enter logarea_init\n");
+	la = malloc(sizeof(struct logarea));
+	
+	if (!la)
+		return 1;
+
+	if (size < MAX_MSG_SIZE)
+		size = DEFAULT_AREA_SIZE;
+
+	la->start = malloc(size);
+	memset(la->start, 0, size);
+
+	if (!la->start) {
+		free(la);
+		return 1;
+	}
+
+	la->empty = 1;
+	la->end = la->start + size;
+	la->head = la->start;
+	la->tail = la->start;
+
+	la->buff = malloc(MAX_MSG_SIZE + sizeof(struct logmsg));
+
+	if (!la->buff) {
+		free(la->start);
+		free(la);
+		return 1;
+	}
+	return 0;
+
+}
+
+int log_init(char *program_name, int size)
+{
+	logdbg(stderr,"enter log_init\n");
+	openlog(program_name, 0, LOG_DAEMON);
+
+	if (logarea_init(size))
+		return 1;
+
+	return 0;
+}
+
+void free_logarea (void)
+{
+	free(la->start);
+	free(la->buff);
+	free(la);
+	return;
+}
+
+void log_close (void)
+{
+	free_logarea();
+	closelog();
+
+	return;
+}
+
+int log_enqueue (int prio, const char * fmt, va_list ap)
+{
+	int len, fwd;
+	char buff[MAX_MSG_SIZE];
+	struct logmsg * msg;
+	struct logmsg * lastmsg;
+
+	lastmsg = (struct logmsg *)la->tail;
+
+	if (!la->empty) {
+		fwd = sizeof(struct logmsg) + 
+		      strlen((char *)&lastmsg->str) * sizeof(char) + 1;
+		la->tail += fwd;
+	}
+	vsnprintf(buff, MAX_MSG_SIZE, fmt, ap);
+	len = strlen(buff) * sizeof(char) + 1;
+
+	/* not enough space on tail : rewind */
+	if (la->head <= la->tail &&
+	    (len + sizeof(struct logmsg)) > (la->end - la->tail)) {
+		logdbg(stderr, "enqueue: rewind tail to %p\n", la->tail);
+		la->tail = la->start;
+	}
+
+	/* not enough space on head : drop msg */
+	if (la->head > la->tail &&
+	    (len + sizeof(struct logmsg)) > (la->head - la->tail)) {
+		logdbg(stderr, "enqueue: log area overrun, drop msg\n");
+
+		if (!la->empty)
+			la->tail = lastmsg;
+
+		return 1;
+	}
+
+	/* ok, we can stage the msg in the area */
+	la->empty = 0;
+	msg = (struct logmsg *)la->tail;
+	msg->prio = prio;
+	memcpy((void *)&msg->str, buff, len);
+	lastmsg->next = la->tail;
+	msg->next = la->head;
+
+	logdbg(stderr, "enqueue: %p, %p, %i, %s\n", (void *)msg, msg->next,
+		msg->prio, (char *)&msg->str);
+
+#if LOGDBG
+	dump_logarea();
+#endif
+	return 0;
+}
+
+int log_dequeue (void * buff)
+{
+	struct logmsg * src = (struct logmsg *)la->head;
+	struct logmsg * dst = (struct logmsg *)buff;
+	struct logmsg * lst = (struct logmsg *)la->tail;
+
+	if (la->empty)
+		return 1;
+	
+	int len = strlen((char *)&src->str) * sizeof(char) +
+		  sizeof(struct logmsg) + 1;
+
+	dst->prio = src->prio;
+	memcpy(dst, src,  len);
+
+	if (la->tail == la->head)
+		la->empty = 1; /* we purge the last logmsg */
+	else {
+		la->head = src->next;
+		lst->next = la->head;
+	}
+	logdbg(stderr, "dequeue: %p, %p, %i, %s\n",
+		(void *)src, src->next, src->prio, (char *)&src->str);
+
+	memset((void *)src, 0,  len);
+
+	return la->empty;
+}
+
+/*
+ * this one can block under memory pressure
+ */
+void log_syslog (void * buff)
+{
+	struct logmsg * msg = (struct logmsg *)buff;
+
+	syslog(msg->prio, "%s", (char *)&msg->str);
+}

Added: multipath-tools/trunk/multipathd/log.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/multipathd/log.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,42 @@
+#ifndef LOG_H
+#define LOG_H
+
+#define DEFAULT_AREA_SIZE 4096
+#define MAX_MSG_SIZE 128
+
+#ifndef LOGLEVEL
+#define LOGLEVEL 5
+#endif
+
+#if LOGDBG
+#define logdbg(file, fmt, args...) fprintf(file, fmt, ##args)
+#else
+#define logdbg(file, fmt, args...) do {} while (0)
+#endif
+
+struct logmsg {
+	short int prio;
+	void * next;
+	char * str;
+};
+
+struct logarea {
+	int empty;
+	void * head;
+	void * tail;
+	void * start;
+	void * end;
+	char * buff;
+};
+
+struct logarea * la;
+
+int log_init (char * progname, int size);
+void log_close (void);
+int log_enqueue (int prio, const char * fmt, va_list ap);
+int log_dequeue (void *);
+void log_syslog (void *);
+void dump_logmsg (void *);
+void free_logarea (void);
+
+#endif /* LOG_H */

Added: multipath-tools/trunk/multipathd/log_pthread.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/multipathd/log_pthread.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,92 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <pthread.h>
+#include <sys/mman.h>
+
+#include "log_pthread.h"
+#include "log.h"
+
+void log_safe (int prio, char * fmt, ...)
+{
+	va_list ap;
+
+	pthread_mutex_lock(logq_lock);
+	va_start(ap, fmt);
+	log_enqueue(prio, fmt, ap);
+	va_end(ap);
+	pthread_mutex_unlock(logq_lock);
+
+	pthread_mutex_lock(logev_lock);
+	pthread_cond_signal(logev_cond);
+	pthread_mutex_unlock(logev_lock);
+}
+
+static void flush_logqueue (void)
+{
+	int empty;
+
+	do {
+		pthread_mutex_lock(logq_lock);
+		empty = log_dequeue(la->buff);
+		pthread_mutex_unlock(logq_lock);
+		log_syslog(la->buff);
+	} while (empty == 0);
+}
+
+static void * log_thread (void * et)
+{
+	mlockall(MCL_CURRENT | MCL_FUTURE);
+	logdbg(stderr,"enter log_thread\n");
+
+	while (1) {
+		pthread_mutex_lock(logev_lock);
+		pthread_cond_wait(logev_cond, logev_lock);
+		pthread_mutex_unlock(logev_lock);
+
+		flush_logqueue();
+	}
+}
+
+void log_thread_start (void)
+{
+	pthread_attr_t attr;
+	
+	logdbg(stderr,"enter log_thread_start\n");
+
+	logq_lock = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
+	logev_lock = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
+	logev_cond = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
+	
+	pthread_mutex_init(logq_lock, NULL);
+	pthread_mutex_init(logev_lock, NULL);
+	pthread_cond_init(logev_cond, NULL);
+	
+	pthread_attr_init(&attr);
+	pthread_attr_setstacksize(&attr, 64 * 1024);
+
+	if (log_init("multipathd", 0)) {
+		fprintf(stderr,"can't initialize log buffer\n");
+		exit(1);
+	}
+	pthread_create(&log_thr, &attr, log_thread, NULL);
+
+	return;
+}
+
+void log_thread_stop (void)
+{
+	logdbg(stderr,"enter log_thread_stop\n");
+
+	pthread_mutex_lock(logq_lock);
+	pthread_cancel(log_thr);
+	pthread_mutex_unlock(logq_lock);
+
+	flush_logqueue();
+
+	pthread_mutex_destroy(logq_lock);
+	pthread_mutex_destroy(logev_lock);
+	pthread_cond_destroy(logev_cond);
+
+	free_logarea();
+}	

Added: multipath-tools/trunk/multipathd/log_pthread.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/multipathd/log_pthread.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,14 @@
+#ifndef _LOG_PTHREAD_H
+#define _LOG_PTHREAD_H
+
+pthread_t log_thr;
+
+pthread_mutex_t *logq_lock;
+pthread_mutex_t *logev_lock;
+pthread_cond_t *logev_cond;
+
+void log_safe(int prio, char * fmt, ...);
+void log_thread_start(void);
+void log_thread_stop(void);
+
+#endif /* _LOG_PTHREAD_H */

Modified: multipath-tools/trunk/multipathd/main.c
==============================================================================
--- multipath-tools/trunk/multipathd/main.c	(original)
+++ multipath-tools/trunk/multipathd/main.c	Sat Aug 13 15:23:02 2005
@@ -12,7 +12,6 @@
 #include <wait.h>
 #include <sched.h>
 #include <errno.h>
-#include <syslog.h>
 #include <sys/mount.h>
 #include <sys/mman.h>
 
@@ -31,41 +30,55 @@
 /*
  * libmultipath
  */
-#include <sysfs_devinfo.h>
 #include <parser.h>
 #include <vector.h>
 #include <memory.h>
+#include <config.h>
 #include <callout.h>
-#include <safe_printf.h>
+#include <util.h>
 #include <blacklist.h>
 #include <hwtable.h>
 #include <defaults.h>
 #include <structs.h>
+#include <dmparser.h>
+#include <devmapper.h>
+#include <dict.h>
+#include <discovery.h>
+#include <debug.h>
+#include <propsel.h>
 
 #include "main.h"
-#include "dict.h"
-#include "devinfo.h"
 #include "copy.h"
 #include "clone_platform.h"
+#include "pidfile.h"
 
 #define FILE_NAME_SIZE 256
 #define CMDSIZE 160
 
 #define CALLOUT_DIR "/var/cache/multipathd"
 
-#ifndef LOGLEVEL
-#define LOGLEVEL 5
-#endif
-
 #define LOG_MSG(a,b) \
 	if (strlen(a)) { \
-		syslog(LOG_WARNING, "%s: %s", b, a); \
+		log_safe(LOG_WARNING, "%s: %s", b, a); \
 		memset(a, 0, MAX_CHECKER_MSG_SIZE); \
 	}
 
+#ifdef LCKDBG
+#define lock(a) \
+	fprintf(stderr, "%s:%s(%i) lock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
+	pthread_mutex_lock(a)
+#define unlock(a) \
+	fprintf(stderr, "%s:%s(%i) unlock %p\n", __FILE__, __FUNCTION__, __LINE__, a); \
+	pthread_mutex_unlock(a)
+#else
+#define lock(a) pthread_mutex_lock(a)
+#define unlock(a) pthread_mutex_unlock(a)
+#endif
+
 /*
  * global vars
  */
+int pending_event = 0;
 int from_sighup;
 pthread_mutex_t *event_lock;
 pthread_cond_t *event;
@@ -81,8 +94,10 @@
 struct event_thread {
 	pthread_t *thread;
 	pthread_mutex_t *waiter_lock;
+	int lease;
 	int event_nr;
 	char mapname[WWID_SIZE];
+	struct paths *allpaths;
 };
 
 /*
@@ -102,53 +117,19 @@
 }
 
 static int
-select_checkfn(struct path *pp, char *devname)
-{
-	char vendor[SCSI_VENDOR_SIZE];
-	char product[SCSI_PRODUCT_SIZE];
-	char rev[SCSI_REV_SIZE];
-	char checker_name[CHECKER_NAME_SIZE];
-	int r;
-	struct hwentry * hwe;
-
-	/*
-	 * default checkfn
-	 */
-	pp->checkfn = &readsector0;
-	
-	r = get_lun_strings(vendor, product, rev, devname);
-
-	if (r) {
-		syslog(LOG_ERR, "can not get scsi strings");
-		return r;
-	}
-	hwe = find_hw(hwtable, vendor, product);
-
-	if (hwe && hwe->checker_index > 0) {
-		get_checker_name(checker_name, hwe->checker_index);
-		syslog(LOG_INFO, "set %s path checker for %s",
-		     checker_name, devname);
-		pp->checkfn = get_checker_addr(hwe->checker_index);
-		return 0;
-	}
-	syslog(LOG_INFO, "set readsector0 path checker for %s (default)",
-		devname);
-	return 0;
-}
-
-static int
 exit_daemon (int status)
 {
 	if (status != 0)
 		fprintf(stderr, "bad exit status. see daemon.log\n");
 
-	syslog(LOG_INFO, "umount ramfs");
+	log_safe(LOG_INFO, "umount ramfs");
 	umount(CALLOUT_DIR);
 
-	syslog(LOG_INFO, "unlink pidfile");
+	log_safe(LOG_INFO, "unlink pidfile");
 	unlink(DEFAULT_PIDFILE);
 
-	syslog(LOG_NOTICE, "--------shut down-------");
+	log_safe(LOG_NOTICE, "--------shut down-------");
+	log_thread_stop();
 	exit(status);
 }
 
@@ -156,227 +137,154 @@
 get_devmaps (void)
 {
 	char *devmap;
-	struct dm_task *dmt, *dmt1;
+	struct dm_task *dmt;
 	struct dm_names *names = NULL;
 	unsigned next = 0;
 	void *nexttgt;
-	uint64_t start, length;
-	char *target_type = NULL;
-	char *params;
 	vector devmaps;
 
 	devmaps = vector_alloc();
 
-	if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
+	if (!devmaps)
 		return NULL;
 
-	if (!dm_task_run(dmt)) {
-		devmaps = NULL;
+	if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
 		goto out;
-	}
 
-	if (!(names = dm_task_get_names(dmt))) {
-		devmaps = NULL;
-		goto out;
-	}
+	dm_task_no_open_count(dmt);
+
+	if (!dm_task_run(dmt))
+		goto out1;
+
+	if (!(names = dm_task_get_names(dmt)))
+		goto out1;
 
 	if (!names->dev) {
-		syslog(LOG_WARNING, "no devmap found");
-		goto out;
+		log_safe(LOG_WARNING, "no devmap found");
+		goto out1;
 	}
-
 	do {
-		/*
-		 * keep only multipath maps
-		 */
 		names = (void *) names + next;
 		nexttgt = NULL;
-		syslog(LOG_DEBUG, "devmap %s :", names->name);
 
-		if (!(dmt1 = dm_task_create(DM_DEVICE_STATUS)))
-			goto out;
-		
-		if (!dm_task_set_name(dmt1, names->name))
-			goto out1;
+		/*
+		 * keep only multipath maps
+		 */
+		log_safe(LOG_DEBUG, "devmap %s", names->name);
 		
-		if (!dm_task_run(dmt1))
-			goto out1;
+		if (!dm_type(names->name, "multipath")) {
+			log_safe(LOG_DEBUG,
+			       "   skip non multipath target");
+			goto out2;
+		}
 
-		do {
-			nexttgt = dm_get_next_target(dmt1, nexttgt,
-						   &start,
-						   &length,
-						   &target_type,
-						   &params);
-			syslog(LOG_DEBUG, "\\_ %lu %lu %s",
-			       (unsigned long) start,
-						  (unsigned long) length,
-						  target_type);
-
-			if (!target_type) {
-				syslog(LOG_INFO, "   unknown target type");
-				goto out1;
-			}
-			
-			if (!strncmp(target_type, "multipath", 9)) {
-				devmap = MALLOC(WWID_SIZE);
-				strcpy(devmap, names->name);
-				vector_alloc_slot(devmaps);
-				vector_set_slot(devmaps, devmap);
-			} else
-				syslog(LOG_DEBUG,
-				       "   skip non multipath target");
-		} while (nexttgt);
+		/*
+		 * new map
+		 */
+		devmap = MALLOC(WWID_SIZE);
 
-out1:
-		dm_task_destroy(dmt1);
-		next = names->next;
+		if (!devmap)
+			goto out2;
 
+		strcpy(devmap, names->name);
+			
+		if (!vector_alloc_slot(devmaps)) {
+			free(devmap);
+			goto out2;
+		}
+		vector_set_slot(devmaps, devmap);
+out2:
+		next = names->next;
 	} while (next);
 
-out:
 	dm_task_destroy(dmt);
 	return devmaps;
+out1:
+	dm_task_destroy(dmt);
+out:
+	strvec_free(devmaps);
+	return NULL;
 }
 
 static int
 updatepaths (struct paths *allpaths, char *sysfs_path)
 {
-	int i;
-	struct path *pp;
-	struct sysfs_directory * sdir;
-	struct sysfs_directory * devp;
-	char path[FILE_NAME_SIZE];
-	char attr_path[FILE_NAME_SIZE];
-	char attr_buff[17];
-	
-	if (safe_sprintf(path, "%s/block", sysfs_path)) {
-		fprintf(stderr, "updatepaths: path too small\n");
-		return 1;
-	}
-	sdir = sysfs_open_directory(path);
-
-	if (!sdir) {
-		syslog(LOG_ERR, "cannot open %s/block", sysfs_path);
-		return 1;
-	}
-	if (sysfs_read_dir_subdirs(sdir) < 0) {
-		syslog(LOG_ERR, "cannot open %s/block subdirs", sysfs_path);
-		sysfs_close_directory(sdir);
-		return 1;
-	}
-	pthread_mutex_lock(allpaths->lock);
-
-	dlist_for_each_data(sdir->subdirs, devp, struct sysfs_directory) {
-		if (blacklist(blist, devp->name)) {
-			syslog(LOG_DEBUG, "%s blacklisted", devp->name);
-			continue;
-		}
-		memset(attr_buff, 0, sizeof (attr_buff));
-		memset(attr_path, 0, sizeof (attr_path));
-
-		if (safe_sprintf(attr_path, "%s/block/%s/dev",
-			sysfs_path, devp->name)) {
-			fprintf(stderr, "updatepaths: attr_path too small\n");
-			continue;
-		}
-		if (0 > sysfs_read_attribute_value(attr_path, attr_buff, 17)) {
-			syslog(LOG_ERR, "no such attribute : %s",
-				attr_path);
-			continue;
-		}
-		/*
-		 * detect if path already exists in path vector
-		 * if so, keep the old one for for checker context and
-		 * state persistance
-		 */
-		vector_foreach_slot (allpaths->pathvec, pp, i)
-			if (!strncmp(pp->dev_t, attr_buff,
-					strlen(pp->dev_t) + 1))
-				break;
-
-		if (i < VECTOR_SIZE(allpaths->pathvec)) {
-			syslog(LOG_INFO, "path checker already active : %s",
-				pp->dev_t);
-			continue;
-		}
-
-		/*
-		 * ok, really allocate a path
-		 */
-		pp = MALLOC(sizeof(struct path));
+	lock(allpaths->lock);
+	path_discovery(allpaths->pathvec, conf, 0);
+	unlock(allpaths->lock);
 
-		if (safe_snprintf(pp->dev_t, BLK_DEV_SIZE, "%s",
-				  attr_buff)) {
-			fprintf(stderr, "dev_t too small\n");
-			FREE(pp);
-			continue;
-		}
-		if (select_checkfn(pp, devp->name)) {
-			FREE(pp);
-			continue;
-		}
-		pp->state = pp->checkfn(pp->dev_t, NULL, &pp->checker_context);
-		vector_alloc_slot(allpaths->pathvec);
-		vector_set_slot(allpaths->pathvec, pp);
-		syslog(LOG_NOTICE, "path checker startup : %s", pp->dev_t);
-	}
-	pthread_mutex_unlock(allpaths->lock);
-	sysfs_close_directory(sdir);
 	return 0;
 }
 
 static int
-geteventnr (char *name)
+mark_failed_path (struct paths *allpaths, char *mapname)
 {
-	struct dm_task *dmt;
-	struct dm_info info;
-	
-	if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
+	struct multipath *mpp;
+	struct pathgroup  *pgp;
+	struct path *pp;
+	struct path *app;
+	int i, j;
+	int r = 1;
+
+	if (!dm_map_present(mapname))
 		return 0;
 
-	if (!dm_task_set_name(dmt, name))
-		goto out;
+	mpp = alloc_multipath();
 
-	if (!dm_task_run(dmt))
+	if (!mpp)
+		return 1;
+
+	if (dm_get_map(mapname, &mpp->size, (char *)mpp->params))
 		goto out;
 
-	if (!dm_task_get_info(dmt, &info)) {
-		info.event_nr = 0;
+	if (dm_get_status(mapname, mpp->status))
 		goto out;
-	}
+	
+	lock(allpaths->lock);
+	r = disassemble_map(allpaths->pathvec, mpp->params, mpp);
+	unlock(allpaths->lock);
+	
+	if (r)
+		goto out;
+
+	r = disassemble_status(mpp->status, mpp);
 
-	if (!info.exists) {
-		syslog(LOG_ERR, "Device %s does not exist", name);
-		info.event_nr = 0;
+	if (r)
 		goto out;
-	}
 
+	r = 0; /* can't fail from here on */
+	lock(allpaths->lock);
+
+	vector_foreach_slot (mpp->pg, pgp, i) {
+		vector_foreach_slot (pgp->paths, pp, j) {
+			if (pp->dmstate != PSTATE_FAILED)
+				continue;
+
+			app = find_path_by_devt(allpaths->pathvec, pp->dev_t);
+
+			if (app && app->state != PATH_DOWN) {
+				log_safe(LOG_NOTICE, "mark %s as failed",
+					pp->dev_t);
+				app->state = PATH_DOWN;
+			}
+		}
+	}
+	unlock(allpaths->lock);
 out:
-	dm_task_destroy(dmt);
+	free_multipath(mpp, KEEP_PATHS);
 
-	return info.event_nr;
+	return r;
 }
 
 static void *
-waitevent (void * et)
+waiteventloop (struct event_thread * waiter, char * cmd)
 {
-	struct event_thread *waiter;
-	char buff[1];
-	char cmd[CMDSIZE];
+	//char buff[1];
 	struct dm_task *dmt;
+	int event_nr;
 
-	mlockall(MCL_CURRENT | MCL_FUTURE);
-	waiter = (struct event_thread *)et;
-
-	if (safe_snprintf(cmd, CMDSIZE, "%s %s", multipath, waiter->mapname)) {
-		syslog(LOG_ERR, "command too long, abord reconfigure");
-		return NULL;
-	}
-	pthread_mutex_lock (waiter->waiter_lock);
-
-	waiter->event_nr = geteventnr (waiter->mapname);
-	syslog(LOG_DEBUG, "waiter->event_nr = %i", waiter->event_nr);
+	if (!waiter->event_nr)
+		waiter->event_nr = dm_geteventnr(waiter->mapname);
 
 	if (!(dmt = dm_task_create(DM_DEVICE_WAITEVENT)))
 		return 0;
@@ -387,50 +295,132 @@
 	if (waiter->event_nr && !dm_task_set_event_nr(dmt, waiter->event_nr))
 		goto out;
 
+	dm_task_no_open_count(dmt);
+
 	dm_task_run(dmt);
 
+	waiter->event_nr++;
+
+	/*
+	 * upon event ...
+	 */
+	while (1) {
+		log_safe(LOG_DEBUG, "%s", cmd);
+		log_safe(LOG_NOTICE, "devmap event (%i) on %s",
+				waiter->event_nr, waiter->mapname);
+
+		mark_failed_path(waiter->allpaths, waiter->mapname);
+		//execute_program(cmd, buff, 1);
+
+		event_nr = dm_geteventnr(waiter->mapname);
+
+		if (waiter->event_nr == event_nr)
+			break;
+
+		waiter->event_nr = event_nr;
+	}
+
 out:
 	dm_task_destroy(dmt);
 
-	syslog(LOG_DEBUG, "%s", cmd);
-	syslog(LOG_NOTICE, "devmap event on %s", waiter->mapname);
-	execute_program(cmd, buff, 1);
-
 	/*
 	 * tell waiterloop we have an event
 	 */
-	pthread_mutex_lock (event_lock);
+	lock(event_lock);
+	pending_event++;
 	pthread_cond_signal(event);
-	pthread_mutex_unlock (event_lock);
+	unlock(event_lock);
 	
+	return NULL;
+}
+
+static void *
+waitevent (void * et)
+{
+	struct event_thread *waiter;
+	char cmd[CMDSIZE];
+
+	mlockall(MCL_CURRENT | MCL_FUTURE);
+
+	waiter = (struct event_thread *)et;
+	lock(waiter->waiter_lock);
+	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+
+	if (safe_snprintf(cmd, CMDSIZE, "%s %s",
+			  conf->multipath, waiter->mapname)) {
+		log_safe(LOG_ERR, "command too long, abord reconfigure");
+		goto out;
+	}
+	while (1)
+		waiteventloop(waiter, cmd);
+
+out:
 	/*
 	 * release waiter_lock so that waiterloop knows we are gone
 	 */
-	pthread_mutex_unlock (waiter->waiter_lock);
+	unlock(waiter->waiter_lock);
 	pthread_exit(waiter->thread);
 
-	return (NULL);
+	return NULL;
+}
+
+static void *
+alloc_waiter (void)
+{
+
+	struct event_thread * wp;
+
+	wp = MALLOC(sizeof(struct event_thread));
+
+	if (!wp)
+		return NULL;
+
+	wp->thread = MALLOC(sizeof(pthread_t));
+
+	if (!wp->thread)
+		goto out;
+		
+	wp->waiter_lock = (pthread_mutex_t *)MALLOC(sizeof(pthread_mutex_t));
+
+	if (!wp->waiter_lock)
+		goto out1;
+
+	pthread_mutex_init(wp->waiter_lock, NULL);
+	return wp;
+
+out1:
+	free(wp->thread);
+out:
+	free(wp);
+	return NULL;
+}
+
+static void
+free_waiter (struct event_thread * wp)
+{
+	pthread_mutex_destroy(wp->waiter_lock);
+	free(wp->thread);
+	free(wp);
 }
 
 static void *
 waiterloop (void *ap)
 {
-	struct paths * allpaths;
+	struct paths *allpaths;
 	vector devmaps = NULL;
-	char * devmap;
+	char *devmap;
 	vector waiters;
 	struct event_thread *wp;
 	pthread_attr_t attr;
 	int r;
 	char buff[1];
 	int i, j;
-	char sysfs_path[FILE_NAME_SIZE];
 
-	syslog(LOG_NOTICE, "start DM events thread");
 	mlockall(MCL_CURRENT | MCL_FUTURE);
+	log_safe(LOG_NOTICE, "start DM events thread");
 
 	if (sysfs_get_mnt_path(sysfs_path, FILE_NAME_SIZE)) {
-		syslog(LOG_ERR, "can not find sysfs mount point");
+		log_safe(LOG_ERR, "can not find sysfs mount point");
 		return NULL;
 	}
 
@@ -439,42 +429,55 @@
 	 */
 	allpaths = (struct paths *)ap;
 	waiters = vector_alloc();
-	pthread_attr_init(&attr);
+
+	if (!waiters)
+		return NULL;
+
+	if (pthread_attr_init(&attr))
+		return NULL;
+
 	pthread_attr_setstacksize(&attr, 32 * 1024);
 
-	syslog(LOG_NOTICE, "initial reconfigure multipath maps");
-	execute_program(multipath, buff, 1);
+	log_safe(LOG_NOTICE, "initial reconfigure multipath maps");
+	execute_program(conf->multipath, buff, 1);
 
 	while (1) {
 		/*
+		 * revoke the leases
+		 */
+		vector_foreach_slot (waiters, wp, i)
+			wp->lease = 0;
+
+		/*
 		 * update devmap list
 		 */
-		syslog(LOG_INFO, "refresh devmaps list");
-		if (devmaps != NULL)
-			strvec_free(devmaps);
+		log_safe(LOG_INFO, "refresh devmaps list");
+
+		if (devmaps)
+			free_strvec(devmaps);
 
 		while ((devmaps = get_devmaps()) == NULL) {
 			/*
 			 * we're not allowed to fail here
 			 */
-			syslog(LOG_ERR, "can't get devmaps ... retry");
+			log_safe(LOG_ERR, "can't get devmaps ... retry");
 			sleep(5);
 		}
 
 		/*
 		 * update paths list
 		 */
-		syslog(LOG_INFO, "refresh failpaths list");
+		log_safe(LOG_INFO, "refresh paths list");
 
 		while(updatepaths(allpaths, sysfs_path)) {
-			syslog(LOG_ERR, "can't update path list ... retry");
+			log_safe(LOG_ERR, "can't update path list ... retry");
 			sleep(5);
 		}
 
 		/*
 		 * start waiters on all devmaps
 		 */
-		syslog(LOG_INFO, "start up event loops");
+		log_safe(LOG_INFO, "start up event loops");
 
 		vector_foreach_slot (devmaps, devmap, i) {
 			/*
@@ -482,52 +485,89 @@
 			 * a running waiter thread
 			 */
 			vector_foreach_slot (waiters, wp, j)
-				if (!strcmp (wp->mapname, devmap))
+				if (!strcmp(wp->mapname, devmap))
 					break;
 					
 			/*
 			 * no event_thread struct : init it
 			 */
 			if (j == VECTOR_SIZE(waiters)) {
-				wp = MALLOC (sizeof (struct event_thread));
-				strncpy (wp->mapname, devmap, WWID_SIZE);
-				wp->thread = MALLOC (sizeof (pthread_t));
-				wp->waiter_lock = (pthread_mutex_t *) 
-					MALLOC (sizeof (pthread_mutex_t));
-				pthread_mutex_init (wp->waiter_lock, NULL);
-				vector_alloc_slot (waiters);
-				vector_set_slot (waiters, wp);
+				wp = alloc_waiter();
+
+				if (!wp)
+					continue;
+
+				strncpy(wp->mapname, devmap, WWID_SIZE);
+				wp->allpaths = allpaths;
+
+				if (!vector_alloc_slot(waiters)) {
+					free_waiter(wp);
+					continue;
+				}
+				vector_set_slot(waiters, wp);
 			}
 			
 			/*
 			 * event_thread struct found
 			 */
-			if (j < VECTOR_SIZE(waiters)) {
-				r = pthread_mutex_trylock (wp->waiter_lock);
+			else if (j < VECTOR_SIZE(waiters)) {
+				r = pthread_mutex_trylock(wp->waiter_lock);
 
 				/*
 				 * thread already running : next devmap
 				 */
-				if (r)
+				if (r) {
+					log_safe(LOG_DEBUG,
+						 "event checker running : %s",
+						 wp->mapname);
+
+					/*
+					 * renew the lease
+					 */
+					wp->lease = 1;
 					continue;
-				
-				pthread_mutex_unlock (wp->waiter_lock);
+				}
+				pthread_mutex_unlock(wp->waiter_lock);
 			}
 			
-			syslog(LOG_NOTICE, "event checker startup : %s",
+			if (pthread_create(wp->thread, &attr, waitevent, wp)) {
+				log_safe(LOG_ERR,
+					 "cannot create event checker : %s",
+					 wp->mapname);
+				free_waiter(wp);
+				vector_del_slot(waiters, j);
+				continue;
+			}
+			wp->lease = 1;
+			log_safe(LOG_NOTICE, "event checker started : %s",
 					wp->mapname);
-			pthread_create (wp->thread, &attr, waitevent, wp);
-			pthread_detach (*wp->thread);
+		}
+		vector_foreach_slot (waiters, wp, i) {
+			if (wp->lease == 0) {
+				log_safe(LOG_NOTICE, "reap event checker : %s",
+					wp->mapname);
+
+				pthread_cancel(*wp->thread);
+				free_waiter(wp);
+				vector_del_slot(waiters, i);
+				i--;
+			}
 		}
 		/*
 		 * wait event condition
 		 */
-		pthread_mutex_lock (event_lock);
-		pthread_cond_wait(event, event_lock);
-		pthread_mutex_unlock (event_lock);
-	}
+		lock(event_lock);
 
-	return (NULL);
+		if (pending_event > 0)
+			pending_event--;
+
+		log_safe(LOG_INFO, "%i pending event(s)", pending_event);
+		if(pending_event == 0)
+			pthread_cond_wait(event, event_lock);
+
+		unlock(event_lock);
+	}
+	return NULL;
 }
 
 static void *
@@ -541,23 +581,29 @@
 	char cmd[CMDSIZE];
 	char checker_msg[MAX_CHECKER_MSG_SIZE];
 
-	memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
-
 	mlockall(MCL_CURRENT | MCL_FUTURE);
+
+	memset(checker_msg, 0, MAX_CHECKER_MSG_SIZE);
 	allpaths = (struct paths *)ap;
 
-	syslog(LOG_NOTICE, "path checkers start up");
+	log_safe(LOG_NOTICE, "path checkers start up");
 
 	while (1) {
-		pthread_mutex_lock(allpaths->lock);
-		syslog(LOG_DEBUG, "checking paths");
+		lock(allpaths->lock);
+		log_safe(LOG_DEBUG, "checking paths");
 
 		vector_foreach_slot (allpaths->pathvec, pp, i) {
 			if (!pp->checkfn) {
-				syslog(LOG_ERR, "checkfn is void");
+				devinfo(pp, conf->hwtable, DI_SYSFS);
+				select_checkfn(pp);
+			}
+
+			if (!pp->checkfn) {
+				log_safe(LOG_ERR, "%s: checkfn is void",
+					 pp->dev);
 				continue;
 			}
-			newstate = pp->checkfn(pp->dev_t, checker_msg,
+			newstate = pp->checkfn(pp->fd, checker_msg,
 					       &pp->checker_context);
 			
 			if (newstate != pp->state) {
@@ -577,12 +623,12 @@
 				 * reconfigure map now
 				 */
 				if (safe_snprintf(cmd, CMDSIZE, "%s %s",
-						  multipath, pp->dev_t)) {
-					syslog(LOG_ERR, "command too long,"
+						  conf->multipath, pp->dev_t)) {
+					log_safe(LOG_ERR, "command too long,"
 							" abord reconfigure");
 				} else {
-					syslog(LOG_DEBUG, "%s", cmd);
-					syslog(LOG_INFO,
+					log_safe(LOG_DEBUG, "%s", cmd);
+					log_safe(LOG_INFO,
 						"reconfigure %s multipath",
 						pp->dev_t);
 					execute_program(cmd, buff, 1);
@@ -591,16 +637,17 @@
 				/*
 				 * tell waiterloop we have an event
 				 */
-				pthread_mutex_lock (event_lock);
+				lock (event_lock);
+				pending_event++;
 				pthread_cond_signal(event);
-				pthread_mutex_unlock (event_lock);
+				unlock (event_lock);
 			}
 			pp->state = newstate;
 		}
-		pthread_mutex_unlock(allpaths->lock);
-		sleep(checkint);
+		unlock(allpaths->lock);
+		sleep(conf->checkint);
 	}
-	return (NULL);
+	return NULL;
 }
 
 static struct paths *
@@ -632,7 +679,7 @@
 static int
 prepare_namespace(void)
 {
-	mode_t mode;
+	mode_t mode = S_IRWXU;
 	struct stat *buf;
 	char ramfs_args[64];
 	int i;
@@ -646,30 +693,30 @@
 	/*
 	 * create a temp mount point for ramfs
 	 */
-	if (stat (CALLOUT_DIR, buf) < 0) {
+	if (stat(CALLOUT_DIR, buf) < 0) {
 		if (mkdir(CALLOUT_DIR, mode) < 0) {
-			syslog(LOG_ERR, "cannot create " CALLOUT_DIR);
+			log_safe(LOG_ERR, "cannot create " CALLOUT_DIR);
 			return -1;
 		}
-		syslog(LOG_DEBUG, "created " CALLOUT_DIR);
+		log_safe(LOG_DEBUG, "created " CALLOUT_DIR);
 	}
 
 	/*
 	 * compute the optimal ramdisk size
 	 */
-	vector_foreach_slot (binvec, bin,i) {
+	vector_foreach_slot (conf->binvec, bin,i) {
 		if ((fd = open(bin, O_RDONLY)) < 0) {
-			syslog(LOG_ERR, "cannot open %s", bin);
+			log_safe(LOG_ERR, "cannot open %s", bin);
 			return -1;
 		}
 		if (fstat(fd, &statbuf) < 0) {
-			syslog(LOG_ERR, "cannot stat %s", bin);
+			log_safe(LOG_ERR, "cannot stat %s", bin);
 			return -1;
 		}
 		size += statbuf.st_size;
 		close(fd);
 	}
-	syslog(LOG_INFO, "ramfs maxsize is %u", (unsigned int) size);
+	log_safe(LOG_INFO, "ramfs maxsize is %u", (unsigned int) size);
 	
 	/*
 	 * mount the ramfs
@@ -679,77 +726,49 @@
 		return -1;
 	}
 	if (mount(NULL, CALLOUT_DIR, "ramfs", MS_SYNCHRONOUS, ramfs_args) < 0) {
-		syslog(LOG_ERR, "cannot mount ramfs on " CALLOUT_DIR);
+		log_safe(LOG_ERR, "cannot mount ramfs on " CALLOUT_DIR);
 		return -1;
 	}
-	syslog(LOG_DEBUG, "mount ramfs on " CALLOUT_DIR);
+	log_safe(LOG_DEBUG, "mount ramfs on " CALLOUT_DIR);
 
 	/*
 	 * populate the ramfs with callout binaries
 	 */
-	vector_foreach_slot (binvec, bin,i) {
+	vector_foreach_slot (conf->binvec, bin,i) {
 		if (copytodir(bin, CALLOUT_DIR) < 0) {
-			syslog(LOG_ERR, "cannot copy %s in ramfs", bin);
+			log_safe(LOG_ERR, "cannot copy %s in ramfs", bin);
 			exit_daemon(1);
 		}
-		syslog(LOG_DEBUG, "cp %s in ramfs", bin);
+		log_safe(LOG_DEBUG, "cp %s in ramfs", bin);
 	}
-	strvec_free(binvec);
+	strvec_free(conf->binvec);
 
 	/*
 	 * bind the ramfs to :
 	 * /sbin : default home of multipath ...
 	 * /bin  : default home of scsi_id ...
-	 * /tmp  : home of tools temp files
+	 * /tmp  : home of scsi_id temp files
 	 */
 	if (mount(CALLOUT_DIR, "/sbin", NULL, MS_BIND, NULL) < 0) {
-		syslog(LOG_ERR, "cannot bind ramfs on /sbin");
+		log_safe(LOG_ERR, "cannot bind ramfs on /sbin");
 		return -1;
 	}
-	syslog(LOG_DEBUG, "bind ramfs on /sbin");
+	log_safe(LOG_DEBUG, "bind ramfs on /sbin");
 	if (mount(CALLOUT_DIR, "/bin", NULL, MS_BIND, NULL) < 0) {
-		syslog(LOG_ERR, "cannot bind ramfs on /bin");
+		log_safe(LOG_ERR, "cannot bind ramfs on /bin");
 		return -1;
 	}
-	syslog(LOG_DEBUG, "bind ramfs on /bin");
+	log_safe(LOG_DEBUG, "bind ramfs on /bin");
 	if (mount(CALLOUT_DIR, "/tmp", NULL, MS_BIND, NULL) < 0) {
-		syslog(LOG_ERR, "cannot bind ramfs on /tmp");
+		log_safe(LOG_ERR, "cannot bind ramfs on /tmp");
 		return -1;
 	}
-	syslog(LOG_DEBUG, "bind ramfs on /tmp");
+	log_safe(LOG_DEBUG, "bind ramfs on /tmp");
 
 	return 0;
 }
 #endif
 
-static void
-pidfile (pid_t pid)
-{
-	FILE *file;
-	struct stat *buf;
-
-	buf = MALLOC(sizeof(struct stat));
-
-	if (!stat(DEFAULT_PIDFILE, buf)) {
-		syslog(LOG_ERR, "already running : out");
-		FREE(buf);
-		exit(1);
-	}
-		
-	umask(022);
-	pid = setsid();
-
-	if (pid < -1) {
-		syslog(LOG_ERR, "setsid() error");
-		exit(1);
-	}
-	
-	file = fopen(DEFAULT_PIDFILE, "w");
-	fprintf(file, "%d\n", pid);
-	fclose(file);
-	FREE(buf);
-}
-
 static void *
 signal_set(int signo, void (*func) (int))
 {
@@ -772,7 +791,11 @@
 static void
 sighup (int sig)
 {
-	syslog(LOG_NOTICE, "SIGHUP received from multipath or operator");
+	log_safe(LOG_NOTICE, "SIGHUP received from multipath or operator");
+
+#ifdef _DEBUG_
+	        dbg_free_final(NULL);
+#endif
 
 	/*
 	 * signal updatepaths() that we come from SIGHUP
@@ -782,9 +805,10 @@
 	/*
 	 * ask for allpaths refresh
 	 */
-	pthread_mutex_lock (event_lock);
+	lock(event_lock);
+	pending_event++;
 	pthread_cond_signal(event);
-	pthread_mutex_unlock (event_lock);
+	unlock(event_lock);
 }
 
 static void
@@ -813,10 +837,24 @@
         res = sched_setscheduler (0, SCHED_RR, &sched_param);
 
         if (res == -1)
-                syslog(LOG_WARNING, "Could not set SCHED_RR at priority 99");
+                log_safe(LOG_WARNING, "Could not set SCHED_RR at priority 99");
 	return;
 }
 
+static void
+set_oom_adj (int val)
+{
+	FILE *fp;
+
+	fp = fopen("/proc/self/oom_adj", "w");
+
+	if (!fp)
+		return;
+
+	fprintf(fp, "%i", val);
+	fclose(fp);
+}
+	
 static int
 child (void * param)
 {
@@ -824,50 +862,69 @@
 	pthread_attr_t attr;
 	struct paths *allpaths;
 
-	openlog("multipathd", 0, LOG_DAEMON);
-	setlogmask(LOG_UPTO(LOGLEVEL));
-	syslog(LOG_NOTICE, "--------start up--------");
+	mlockall(MCL_CURRENT | MCL_FUTURE);
+
+	log_thread_start();
+	log_safe(LOG_NOTICE, "--------start up--------");
 
-	/* pidfile(getpid()); */
+	if (pidfile_create(DEFAULT_PIDFILE, getpid())) {
+		log_thread_stop();
+		exit(1);
+	}
 	signal_init();
 	setscheduler();
+	set_oom_adj(-17);
 	allpaths = initpaths();
-	checkint = CHECKINT;
+	
+	conf->checkint = CHECKINT;
 
-	syslog(LOG_INFO, "read " DEFAULT_CONFIGFILE);
+	setlogmask(LOG_UPTO(conf->verbosity + 3));
+
+	condlog(2, "read " DEFAULT_CONFIGFILE);
 	init_data(DEFAULT_CONFIGFILE, init_keywords);
 
 	/*
 	 * fill the voids left in the config file
 	 */
-	if (binvec == NULL) {
-		binvec = vector_alloc();
+	if (conf->binvec == NULL) {
+		conf->binvec = vector_alloc();
 		push_callout("/sbin/scsi_id");
 	}
-
-	if (multipath == NULL) {
-		multipath = MULTIPATH;
-		push_callout(multipath);
+	if (conf->multipath == NULL) {
+		conf->multipath = MULTIPATH;
+		push_callout(conf->multipath);
 	}
-
-	if (hwtable == NULL) {
-		hwtable = vector_alloc();
-		setup_default_hwtable(hwtable);
+	if (conf->hwtable == NULL) {
+		conf->hwtable = vector_alloc();
+		setup_default_hwtable(conf->hwtable);
 	}
-
-	if (blist == NULL) {
-		blist = vector_alloc();
-		setup_default_blist(blist);
+	if (conf->blist == NULL) {
+		conf->blist = vector_alloc();
+		setup_default_blist(conf->blist);
 	}
+	if (conf->default_selector == NULL)
+		conf->default_selector = set_default(DEFAULT_SELECTOR);
+
+	if (conf->udev_dir == NULL)
+		conf->udev_dir = set_default(DEFAULT_UDEVDIR);
+
+	if (conf->default_getuid == NULL)
+		conf->default_getuid = set_default(DEFAULT_GETUID);
+
+	if (conf->default_features == NULL)
+		conf->default_features = set_default(DEFAULT_FEATURES);
+
+	if (conf->default_hwhandler == NULL)
+		conf->default_hwhandler = set_default(DEFAULT_HWHANDLER);
+
 
 #ifdef CLONE_NEWNS
 	if (prepare_namespace() < 0) {
-		syslog(LOG_ERR, "cannot prepare namespace");
+		log_safe(LOG_ERR, "cannot prepare namespace");
 		exit_daemon(1);
 	}
 #endif
 
-	mlockall(MCL_CURRENT | MCL_FUTURE);
 	/*
 	 * start threads
 	 */
@@ -885,8 +942,46 @@
 int
 main (int argc, char *argv[])
 {
+	extern char *optarg;
+	extern int optind;
+	int arg;
 	int err;
-	void * child_stack = (void *)malloc(CHILD_STACK_SIZE);
+	void * child_stack;
+	
+	if (getuid() != 0) {
+		fprintf(stderr, "need to be root, exit");
+		exit(1);
+	}
+
+	/* make sure we don't lock any path */
+	chdir("/");
+	umask(umask(077) | 022);
+
+	child_stack = (void *)malloc(CHILD_STACK_SIZE);
+
+	if (!child_stack)
+		exit(1);
+
+	conf = alloc_config();
+
+	if (!conf)
+		exit(1);
+
+	conf->verbosity = 2;
+
+	while ((arg = getopt(argc, argv, ":qdlFSi:v:p:")) != EOF ) {
+	switch(arg) {
+		case 'v':
+			if (sizeof(optarg) > sizeof(char *) ||
+			    !isdigit(optarg[0]))
+				exit(1);
+
+			conf->verbosity = atoi(optarg);
+			break;
+		default:
+			;
+		}
+	}
 
 #ifdef CLONE_NEWNS	/* recent systems have clone() */
 

Modified: multipath-tools/trunk/multipathd/main.h
==============================================================================
--- multipath-tools/trunk/multipathd/main.h	(original)
+++ multipath-tools/trunk/multipathd/main.h	Sat Aug 13 15:23:02 2005
@@ -1,14 +1,8 @@
 #ifndef HWTABLE_H
 #define HWTABLE_H
 
+#define DAEMON 1
 #define CHECKINT 5
-#define MULTIPATH "/sbin/multipath -v 0 -S"
-
-/* External vars */ 
-vector hwtable;
-vector blist;
-vector binvec;
-int checkint;
-char * multipath;
+#define MULTIPATH "/sbin/multipath -v0 -S"
 
 #endif

Added: multipath-tools/trunk/multipathd/multipathd.init.debian
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/multipathd/multipathd.init.debian	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+DAEMON=/usr/bin/multipathd
+PIDFILE=/var/run/multipathd.pid
+
+test -x $DAEMON || exit 0
+
+case "$1" in
+  start)
+	echo -n "Starting multipath daemon: multipathd"
+        $DAEMON
+	echo "."
+	;;
+  stop)
+	echo -n "Stopping multipath daemon: multipathd"
+	echo "."
+	if [ -f $PIDFILE ]
+	then
+		kill `cat $PIDFILE`
+	else
+		echo "multipathd not running: Nothing to stop..."
+	fi
+	;;
+  force-reload|restart)
+	$0 stop
+	$0 start
+	;;
+  *)
+	echo "Usage: /etc/init.d/multipathd {start|stop|restart|force-reload}"
+	exit 1
+	;;
+esac
+
+exit 0

Added: multipath-tools/trunk/multipathd/multipathd.init.redhat
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/multipathd/multipathd.init.redhat	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,91 @@
+#!/bin/bash
+
+#
+#	/etc/rc.d/init.d/multipathd
+#
+# Starts the multipath daemon
+#
+# chkconfig: 2345 13 87
+# description: Manage device-mapper multipath devices
+# processname: multipathd
+
+DAEMON=/sbin/multipathd
+prog=`basename $DAEMON`
+initdir=/etc/rc.d/init.d
+lockdir=/var/lock/subsys
+sysconfig=/etc/sysconfig
+
+ 
+system=redhat
+ 
+if [ $system = redhat ]; then
+	# Source function library.
+	. $initdir/functions
+fi
+ 
+test -x $DAEMON || exit 0
+test -r $sysconfig/$prog && . $sysconfig/$prog
+
+RETVAL=0
+
+#
+# See how we were called.
+#
+
+start() {
+	echo -n $"Starting $prog daemon: "
+	daemon $DAEMON
+	RETVAL=$?
+	[ $RETVAL -eq 0 ] && touch $lockdir/$prog
+	echo
+}
+
+stop() {
+	echo -n $"Stopping $prog daemon: "
+	killproc $DAEMON
+	RETVAL=$?
+	[ $RETVAL -eq 0 ] && rm -f $lockdir/$prog
+	echo
+}
+
+restart() {
+	stop
+	start
+}	
+
+reload() {
+	echo -n "Reloading $prog: "
+	trap "" SIGHUP
+	killproc $DAEMON -HUP
+	RETVAL=$?
+	echo
+}	
+
+case "$1" in
+start)
+	start
+	;;
+stop)
+	stop
+	;;
+reload)
+	reload
+	;;
+restart)
+	restart
+	;;
+condrestart)
+	if [ -f $lockdir/$prog ]; then
+	    restart
+	fi
+	;;
+status)
+	status $prog
+	RETVAL=$?
+	;;
+*)
+	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
+	RETVAL=1
+esac
+
+exit $RETVAL

Added: multipath-tools/trunk/multipathd/pidfile.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/multipathd/pidfile.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,67 @@
+#include <sys/types.h> /* for pid_t */
+#include <sys/stat.h>  /* for open */
+#include <signal.h>    /* for kill() */
+#include <errno.h>     /* for ESHRC */
+#include <stdio.h>     /* for f...() */
+#include <string.h>    /* for memset() */
+#include <stdlib.h>    /* for atoi() */
+#include <unistd.h>    /* for unlink() */
+#include <fcntl.h>     /* for fcntl() */
+
+#include <debug.h>
+
+#include "pidfile.h"
+
+int pidfile_create(const char *pidFile, pid_t pid)
+{
+	char buf[20];
+	struct flock lock;
+	int fd, value;
+
+	if((fd = open(pidFile, O_WRONLY | O_CREAT,
+		       (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) < 0) {
+		condlog(0, "Cannot open pidfile [%s], error was [%s]",
+			pidFile, strerror(errno));
+		return 1;
+	}
+	lock.l_type = F_WRLCK;
+	lock.l_start = 0;
+	lock.l_whence = SEEK_SET;
+	lock.l_len = 0;
+
+	if (fcntl(fd, F_SETLK, &lock) < 0) {
+		if (errno != EACCES && errno != EAGAIN)
+			condlog(0, "Cannot lock pidfile [%s], error was [%s]",
+				pidFile, strerror(errno));
+		else
+			condlog(0, "process is already running");
+		goto fail;
+	}
+	if (ftruncate(fd, 0) < 0) {
+		condlog(0, "Cannot truncate pidfile [%s], error was [%s]",
+			pidFile, strerror(errno));
+		goto fail;
+	}
+	memset(buf, 0, sizeof(buf));
+	snprintf(buf, sizeof(buf)-1, "%u", pid);
+	if (write(fd, buf, strlen(buf)) != strlen(buf)) {
+		condlog(0, "Cannot write pid to pidfile [%s], error was [%s]",
+			pidFile, strerror(errno));
+		goto fail;
+	}
+	if ((value = fcntl(fd, F_GETFD, 0)) < 0) {
+		condlog(0, "Cannot get close-on-exec flag from pidfile [%s], "
+			"error was [%s]", pidFile, strerror(errno));
+		goto fail;
+	}
+	value |= FD_CLOEXEC;
+	if (fcntl(fd, F_SETFD, value) < 0) {
+		condlog(0, "Cannot set close-on-exec flag from pidfile [%s], "
+			"error was [%s]", pidFile, strerror(errno));
+		goto fail;
+	}
+	return 0;
+fail:
+	close(fd);
+	return 1;
+}

Added: multipath-tools/trunk/multipathd/pidfile.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/multipathd/pidfile.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1 @@
+int pidfile_create(const char *pidFile, pid_t pid);

Modified: multipath-tools/trunk/path_priority/Makefile
==============================================================================
--- multipath-tools/trunk/path_priority/Makefile	(original)
+++ multipath-tools/trunk/path_priority/Makefile	Sat Aug 13 15:23:02 2005
@@ -4,13 +4,12 @@
 BUILD = glibc
 DEBUG = 0
 
-include ../Makefile.inc
+TOPDIR	= ..
+include $(TOPDIR)/Makefile.inc
 
 ifeq ($(strip $(BUILD)),klibc)
-	CFLAGS = ${REQFLAGS} ${OPTFLAGS} -I$(libdmdir) \
-		 -I$(multipathdir) -DDEBUG=$(DEBUG)
-	OBJS = pp_balance_units.o $(DMLIB)-$(BUILD).a \
-	       $(MULTIPATHLIB)-$(BUILD).a
+	CFLAGS = -I/usr/include -DDEBUG=$(DEBUG)
+	OBJS = pp_balance_units.o $(MULTIPATHLIB)-$(BUILD).a
 else
 	CFLAGS = -pipe -g -Wall -Wunused -Wstrict-prototypes \
 		 -I$(multipathdir) -DDEBUG=$(DEBUG)
@@ -19,18 +18,22 @@
 endif
 
 EXEC = pp_balance_units
+SUBDIRS	= $(shell find . -type d -mindepth 1 -maxdepth 1|cut -c3-)
 
 all: $(BUILD)
+	@for DIR in $(SUBDIRS); do \
+		$(MAKE) -C $$DIR BUILD=$(BUILD) VERSION=$(VERSION); \
+	done
 
 prepare:
 	rm -f core *.o *.gz
 
 glibc: prepare $(OBJS)
 	$(CC) -o $(EXEC) $(OBJS) $(LDFLAGS)
-#	$(STRIP) $(EXEC)
+	$(STRIP) $(EXEC)
 	
-klibc: prepare $(KLIBC) $(OBJS)
-	$(LD) -o $(EXEC) $(CRT0) $(OBJS) $(KLIBC) $(LIBGCC)
+klibc: prepare $(OBJS)
+	$(CC) -static -o $(EXEC) $(CRT0) $(OBJS) $(KLIBC) $(LIBGCC)
 	$(STRIP) $(EXEC)
 
 $(DMLIB)-$(BUILD).a:
@@ -39,17 +42,21 @@
 $(MULTIPATHLIB)-$(BUILD).a:
 	make -C $(multipathdir) BUILD=$(BUILD) $(BUILD)
 
-$(KLIBC):
-	rm -f $(klibcdir)/linux
-	ln -s $(KERNEL_BUILD) $(klibcdir)/linux
-	make -C $(klibcdir)
-
 install:
 	install -d $(DESTDIR)$(bindir)
 	install -m 755 $(EXEC) $(DESTDIR)$(bindir)/
+	@for DIR in $(SUBDIRS); do \
+		$(MAKE) -C $$DIR install; \
+	done
 
 uninstall:
+	@for DIR in $(SUBDIRS); do \
+		$(MAKE) -C $$DIR uninstall; \
+	done
 	rm $(DESTDIR)$(bindir)/$(EXEC)
 
 clean:
+	@for DIR in $(SUBDIRS); do \
+		$(MAKE) -C $$DIR clean; \
+	done
 	rm -f core *.o $(EXEC) *.gz

Added: multipath-tools/trunk/path_priority/pp_alua/LICENSE
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/path_priority/pp_alua/LICENSE	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,483 @@
+
+		  GNU LIBRARY GENERAL PUBLIC LICENSE
+		       Version 2, June 1991
+
+ Copyright (C) 1991 Free Software Foundation, Inc.
+ 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the library GPL.  It is
+ numbered 2 because it goes with version 2 of the ordinary GPL.]
+
+			    Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+  This license, the Library General Public License, applies to some
+specially designated Free Software Foundation software, and to any
+other libraries whose authors decide to use it.  You can use it for
+your libraries, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if
+you distribute copies of the library, or if you modify it.
+
+  For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you.  You must make sure that they, too, receive or can get the source
+code.  If you link a program with the library, you must provide
+complete object files to the recipients so that they can relink them
+with the library, after making changes to the library and recompiling
+it.  And you must show them these terms so they know their rights.
+
+  Our method of protecting your rights has two steps: (1) copyright
+the library, and (2) offer you this license which gives you legal
+permission to copy, distribute and/or modify the library.
+
+  Also, for each distributor's protection, we want to make certain
+that everyone understands that there is no warranty for this free
+library.  If the library is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original
+version, so that any problems introduced by others will not reflect on
+the original authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that companies distributing free
+software will individually obtain patent licenses, thus in effect
+transforming the program into proprietary software.  To prevent this,
+we have made it clear that any patent must be licensed for everyone's
+free use or not licensed at all.
+
+  Most GNU software, including some libraries, is covered by the ordinary
+GNU General Public License, which was designed for utility programs.  This
+license, the GNU Library General Public License, applies to certain
+designated libraries.  This license is quite different from the ordinary
+one; be sure to read it in full, and don't assume that anything in it is
+the same as in the ordinary license.
+
+  The reason we have a separate public license for some libraries is that
+they blur the distinction we usually make between modifying or adding to a
+program and simply using it.  Linking a program with a library, without
+changing the library, is in some sense simply using the library, and is
+analogous to running a utility program or application program.  However, in
+a textual and legal sense, the linked executable is a combined work, a
+derivative of the original library, and the ordinary General Public License
+treats it as such.
+
+  Because of this blurred distinction, using the ordinary General
+Public License for libraries did not effectively promote software
+sharing, because most developers did not use the libraries.  We
+concluded that weaker conditions might promote sharing better.
+
+  However, unrestricted linking of non-free programs would deprive the
+users of those programs of all benefit from the free status of the
+libraries themselves.  This Library General Public License is intended to
+permit developers of non-free programs to use free libraries, while
+preserving your freedom as a user of such programs to change the free
+libraries that are incorporated in them.  (We have not seen how to achieve
+this as regards changes in header files, but we have achieved it as regards
+changes in the actual functions of the Library.)  The hope is that this
+will lead to faster development of free libraries.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.  Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library".  The
+former contains code derived from the library, while the latter only
+works together with the library.
+
+  Note that it is possible for a library to be covered by the ordinary
+General Public License rather than by this special one.
+
+		  GNU LIBRARY GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License Agreement applies to any software library which
+contains a notice placed by the copyright holder or other authorized
+party saying it may be distributed under the terms of this Library
+General Public License (also called "this License").  Each licensee is
+addressed as "you".
+
+  A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+  The "Library", below, refers to any such software library or work
+which has been distributed under these terms.  A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language.  (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+  "Source code" for a work means the preferred form of the work for
+making modifications to it.  For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+  Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it).  Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+  
+  1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+  You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+  2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) The modified work must itself be a software library.
+
+    b) You must cause the files modified to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    c) You must cause the whole of the work to be licensed at no
+    charge to all third parties under the terms of this License.
+
+    d) If a facility in the modified Library refers to a function or a
+    table of data to be supplied by an application program that uses
+    the facility, other than as an argument passed when the facility
+    is invoked, then you must make a good faith effort to ensure that,
+    in the event an application does not supply such function or
+    table, the facility still operates, and performs whatever part of
+    its purpose remains meaningful.
+
+    (For example, a function in a library to compute square roots has
+    a purpose that is entirely well-defined independent of the
+    application.  Therefore, Subsection 2d requires that any
+    application-supplied function or table used by this function must
+    be optional: if the application does not supply it, the square
+    root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library.  To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License.  (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.)  Do not make any other change in
+these notices.
+
+  Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+  This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+  4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+  If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library".  Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+  However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library".  The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+  When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library.  The
+threshold for this to be true is not precisely defined by law.
+
+  If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work.  (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+  Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+  6. As an exception to the Sections above, you may also compile or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+  You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License.  You must supply a copy of this License.  If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License.  Also, you must do one
+of these things:
+
+    a) Accompany the work with the complete corresponding
+    machine-readable source code for the Library including whatever
+    changes were used in the work (which must be distributed under
+    Sections 1 and 2 above); and, if the work is an executable linked
+    with the Library, with the complete machine-readable "work that
+    uses the Library", as object code and/or source code, so that the
+    user can modify the Library and then relink to produce a modified
+    executable containing the modified Library.  (It is understood
+    that the user who changes the contents of definitions files in the
+    Library will not necessarily be able to recompile the application
+    to use the modified definitions.)
+
+    b) Accompany the work with a written offer, valid for at
+    least three years, to give the same user the materials
+    specified in Subsection 6a, above, for a charge no more
+    than the cost of performing this distribution.
+
+    c) If distribution of the work is made by offering access to copy
+    from a designated place, offer equivalent access to copy the above
+    specified materials from the same place.
+
+    d) Verify that the user has already received a copy of these
+    materials or that you have already sent this user a copy.
+
+  For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it.  However, as a special exception,
+the source code distributed need not include anything that is normally
+distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+  It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system.  Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+  7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+    a) Accompany the combined library with a copy of the same work
+    based on the Library, uncombined with any other library
+    facilities.  This must be distributed under the terms of the
+    Sections above.
+
+    b) Give prominent notice with the combined library of the fact
+    that part of it is a work based on the Library, and explaining
+    where to find the accompanying uncombined form of the same work.
+
+  8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License.  Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License.  However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+  9. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Library or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+  10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded.  In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+  13. The Free Software Foundation may publish revised and/or new
+versions of the Library General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation.  If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+  14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission.  For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this.  Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+			    NO WARRANTY
+
+  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+		     END OF TERMS AND CONDITIONS
+
+     Appendix: How to Apply These Terms to Your New Libraries
+
+  If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change.  You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+  To apply these terms, attach the following notices to the library.  It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the library's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Library General Public
+    License as published by the Free Software Foundation; either
+    version 2 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Library General Public License for more details.
+
+    You should have received a copy of the GNU Library General Public
+    License along with this library; if not, write to the Free
+    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+    MA 02111-1307, USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the
+  library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+  <signature of Ty Coon>, 1 April 1990
+  Ty Coon, President of Vice
+
+That's all there is to it!

Added: multipath-tools/trunk/path_priority/pp_alua/Makefile
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/path_priority/pp_alua/Makefile	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,55 @@
+#==============================================================================
+# (C) Copyright IBM Corp. 2004, 2005   All Rights Reserved.
+#
+# Makefile
+#
+# Tool to make use of a SCSI-feature called Asymmetric Logical Unit Access.
+# It determines the ALUA state of a device and prints a priority value to
+# stdout.
+#
+# Author(s): Jan Kunigk
+#            S. Bader <shbader at de.ibm.com>
+#
+# This file is released under the GPL.
+#==============================================================================
+EXEC		= pp_alua
+BUILD		= glibc
+DEBUG		= 0
+DEBUG_DUMPHEX	= 0
+OBJS		= main.o rtpg.o
+
+TOPDIR	= ../..
+
+ifneq ($(shell ls $(TOPDIR)/Makefile.inc 2>/dev/null),)
+include $(TOPDIR)/Makefile.inc
+else
+# "out of tree building"
+STRIP		= strip --strip-all -R .comment -R .note
+endif
+
+CFLAGS = -pipe -g -O2 -Wall -Wunused -Wstrict-prototypes -DDEBUG=$(DEBUG)
+
+all: $(BUILD)
+
+glibc:	$(OBJS)
+	$(CC) -o $(EXEC) $(OBJS) $(LDFLAGS)
+	$(STRIP) $(EXEC)
+
+klibc:	$(OBJS)
+	$(CC) -static -o $(EXEC) $(OBJS)
+	$(STRIP) $(EXEC)
+
+install: $(EXEC)
+	install -m 755 $(EXEC) $(DESTDIR)$(bindir)/$(EXEC)
+
+uninstall:
+	rm $(DESTDIR)$(bindir)/$(EXEC)
+clean:	
+	rm -f *.o $(EXEC)
+
+%.o:	%.c
+	$(CC) $(CFLAGS) -c -o $@ $<
+
+main.o:	main.c rtpg.h spc3.h
+
+rtpg.o:	rtpg.c rtpg.h spc3.h

Added: multipath-tools/trunk/path_priority/pp_alua/main.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/path_priority/pp_alua/main.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,229 @@
+/*
+ * (C) Copyright IBM Corp. 2004, 2005   All Rights Reserved.
+ *
+ * main.c
+ *
+ * Tool to make use of a SCSI-feature called Asymmetric Logical Unit Access.
+ * It determines the ALUA state of a device and prints a priority value to
+ * stdout.
+ *
+ * Author(s): Jan Kunigk
+ *            S. Bader <shbader at de.ibm.com>
+ * 
+ * This file is released under the GPL.
+ */
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+#include "rtpg.h"
+
+#define ALUA_PRIO_SUCCESS			0
+#define ALUA_PRIO_INVALID_COMMANDLINE		1
+#define ALUA_PRIO_OPEN_FAILED			2
+#define ALUA_PRIO_NOT_SUPPORTED			3
+#define ALUA_PRIO_RTPG_FAILED			4
+#define ALUA_PRIO_GETAAS_FAILED			5
+
+#define ALUA_PRIO_MAJOR				0
+#define ALUA_PRIO_MINOR				4
+
+#define PRINT_ERROR(f, a...) \
+		if (verbose) \
+		fprintf(stderr, "ERROR: " f, ##a)
+#define PRINT_VERBOSE(f, a...) \
+		if (verbose) \
+			printf(f, ##a)
+
+char *	devicename	= NULL;
+int	verbose		= 0;
+
+char *basename(char *p)
+{
+	char *r;
+
+	for(r = p; *r != '\0'; r++);
+	for(; r > p && *(r - 1) != '/'; r--);
+
+	return r;
+}
+
+void
+print_help(char *command)
+{
+	printf("Usage: %s <options> <device> [<device> [...]]\n\n",
+		basename(command));
+	printf("Options are:\n");
+
+	printf("\t-v\n");
+	printf("\t\tTurn on verbose output.\n");
+
+	printf("\t-V\n");
+	printf("\t\tPrints the version number and exits.\n");
+}
+
+void
+print_version(char *command)
+{
+	printf("(C) Copyright IBM Corp. 2004, 2005  All Rights Reserved.\n");
+	printf("This is %s version %u.%u\n",
+		basename(command),
+		ALUA_PRIO_MAJOR,
+		ALUA_PRIO_MINOR
+	);
+}
+
+int
+open_block_device(char *name)
+{
+	int		fd;
+	struct stat	st;
+
+	if (stat(name, &st) != 0) {
+		PRINT_ERROR("Cannot get file status from %s (errno = %i)!\n",
+			name, errno);
+		return -ALUA_PRIO_OPEN_FAILED;
+	}
+	if (!S_ISBLK(st.st_mode)) {
+		PRINT_ERROR("%s is not a block device!\n", name);
+		return -ALUA_PRIO_OPEN_FAILED;
+	}
+	fd = open(name, O_RDONLY);
+	if (fd < 0) {
+		PRINT_ERROR("Couldn't open %s (errno = %i)!\n", name, errno);
+		return -ALUA_PRIO_OPEN_FAILED;
+	}
+	return fd;
+}
+
+int
+close_block_device(int fd)
+{
+	return close(fd);
+}
+
+int
+get_alua_info(int fd)
+{
+	char *	aas_string[] = {
+		[AAS_OPTIMIZED]		= "active/optimized",
+		[AAS_NON_OPTIMIZED]	= "active/non-optimized",
+		[AAS_STANDBY]		= "standby",
+		[AAS_UNAVAILABLE]	= "unavailable",
+		[AAS_TRANSITIONING]	= "transitioning between states",
+	};
+	int	rc;
+	int	tpg;
+
+	rc = get_target_port_group_support(fd);
+	if (rc < 0)
+		return rc;
+
+	if (verbose) {
+		printf("Target port groups are ");
+		switch(rc) {
+			case TPGS_NONE:
+				printf("not");
+				break;
+			case TPGS_IMPLICIT:
+				printf("implicitly");
+				break;
+			case TPGS_EXPLICIT:
+				printf("explicitly");
+				break;
+			case TPGS_BOTH:
+				printf("implicitly and explicitly");
+				break;
+		}
+		printf(" supported.\n");
+	}
+
+	if (rc == TPGS_NONE)
+		return -ALUA_PRIO_NOT_SUPPORTED;
+
+	tpg = get_target_port_group(fd);
+	if (tpg < 0) {
+		PRINT_ERROR("Couldn't get target port group!\n");
+		return -ALUA_PRIO_RTPG_FAILED;
+	}
+	PRINT_VERBOSE("Reported target port group is %i", tpg);
+
+	rc = get_asymmetric_access_state(fd, tpg);
+	if (rc < 0) {
+		PRINT_VERBOSE(" [get AAS failed]\n");
+		PRINT_ERROR("Couln't get asymmetric access state!\n");
+		return -ALUA_PRIO_GETAAS_FAILED;
+	}
+	PRINT_VERBOSE(" [%s]\n",
+		(aas_string[rc]) ? aas_string[rc] : "invalid/reserved"
+	);
+
+	return rc;
+}
+
+int
+main (int argc, char **argv)
+{
+	char *		s_opts = "hvV";
+	int		fd;
+	int		rc;
+	int		c;
+
+	while ((c = getopt(argc, argv, s_opts)) >= 0) {
+		switch(c) {
+			case 'h':
+				print_help(argv[0]);
+				return ALUA_PRIO_SUCCESS;
+			case 'V':
+				print_version(argv[0]);
+				return ALUA_PRIO_SUCCESS;
+			case 'v':
+				verbose = 1;
+				break;
+			case '?':
+			case ':':
+			default:
+				return ALUA_PRIO_INVALID_COMMANDLINE;
+		}
+	}
+
+	if (optind == argc) {
+		print_help(argv[0]);
+		printf("\n");
+		PRINT_ERROR("No device specified!\n");
+		return ALUA_PRIO_INVALID_COMMANDLINE;
+	}
+
+	rc = ALUA_PRIO_SUCCESS;
+	for(c = optind; c < argc && !rc; c++) {
+		fd = open_block_device(argv[c]);
+		if (fd < 0) {
+			return -fd;
+		}
+		rc = get_alua_info(fd);
+		if (rc >= 0) {
+			switch(rc) {
+				case AAS_OPTIMIZED:
+					rc = 50;
+					break;
+				case AAS_NON_OPTIMIZED:
+					rc = 10;
+					break;
+				case AAS_STANDBY:
+					rc = 1;
+					break;
+				default:
+					rc = 0;
+			}
+			printf("%u\n", rc);
+			rc = ALUA_PRIO_SUCCESS;
+		}
+		close_block_device(fd);
+	}
+
+	return -rc;
+}

Added: multipath-tools/trunk/path_priority/pp_alua/rtpg.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/path_priority/pp_alua/rtpg.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,280 @@
+/*
+ * (C) Copyright IBM Corp. 2004, 2005   All Rights Reserved.
+ *
+ * rtpg.c
+ *
+ * Tool to make use of a SCSI-feature called Asymmetric Logical Unit Access.
+ * It determines the ALUA state of a device and prints a priority value to
+ * stdout.
+ *
+ * Author(s): Jan Kunigk
+ *            S. Bader <shbader at de.ibm.com>
+ * 
+ * This file is released under the GPL.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+
+#define __user
+#include <scsi/sg.h>
+
+#include "rtpg.h"
+
+#define SENSE_BUFF_LEN  32
+#define DEF_TIMEOUT     60000
+
+/*
+ * Macro used to print debug messaged.
+ */
+#if DEBUG > 0
+#define PRINT_DEBUG(f, a...) \
+		fprintf(stderr, "DEBUG: " f, ##a)
+#else
+#define PRINT_DEBUG(f, a...)
+#endif
+
+/*
+ * Optionally print the commands sent and the data received a hex dump.
+ */
+#if DEBUG > 0
+#if DEBUG_DUMPHEX > 0
+#define PRINT_HEX(p, l)	print_hex(p, l)
+void
+print_hex(unsigned char *p, unsigned long len)
+{
+	int	i;
+
+	for(i = 0; i < len; i++) {
+		if (i % 16 == 0)
+			printf("%04x: ", i);
+		printf("%02x%s", p[i], (((i + 1) % 16) == 0) ? "\n" : " ");
+	}
+	printf("\n");
+}
+#else
+#define PRINT_HEX(p, l)
+#endif
+#else
+#define PRINT_HEX(p, l)
+#endif
+
+/*
+ * Returns 0 if the SCSI command either was successful or if the an error was
+ * recovered, otherwise 1. (definitions taken from sg_err.h)
+ */
+#define SCSI_CHECK_CONDITION    0x2
+#define SCSI_COMMAND_TERMINATED 0x22
+#define SG_ERR_DRIVER_SENSE     0x08
+#define RECOVERED_ERROR 0x01
+
+static int
+scsi_error(struct sg_io_hdr *hdr)
+{
+	/* Treat SG_ERR here to get rid of sg_err.[ch] */
+        hdr->status &= 0x7e;
+
+	if (
+		(hdr->status == 0)        &&
+		(hdr->host_status == 0)   &&
+		(hdr->driver_status == 0)
+	) {
+		return 0;
+	}
+
+	if (
+		(hdr->status == SCSI_CHECK_CONDITION)    ||
+		(hdr->status == SCSI_COMMAND_TERMINATED) ||
+		((hdr->driver_status & 0xf) == SG_ERR_DRIVER_SENSE)
+	) {
+		if (hdr->sbp && (hdr->sb_len_wr > 2)) {
+			int		sense_key;
+			unsigned char *	sense_buffer = hdr->sbp;
+
+			if (sense_buffer[0] & 0x2)
+				sense_key = sense_buffer[1] & 0xf;
+			else
+				sense_key = sense_buffer[2] & 0xf;
+
+			if (sense_key == RECOVERED_ERROR)
+				return 0;
+		}
+	}
+
+	return 1;
+}
+
+/*
+ * Helper function to setup and run a SCSI inquiry command.
+ */
+int
+do_inquiry(int fd, int evpd, unsigned int codepage, void *resp, int resplen)
+{
+	struct inquiry_command	cmd;
+	struct sg_io_hdr	hdr;
+	unsigned char		sense[SENSE_BUFF_LEN];
+
+	memset(&cmd, 0, sizeof(cmd));
+	cmd.op = OPERATION_CODE_INQUIRY;
+	if (evpd) {
+		cmd.evpd = 1;
+		cmd.page = codepage;
+	}
+	set_uint16(cmd.length, resplen);
+	PRINT_HEX((unsigned char *) &cmd, sizeof(cmd));
+
+        memset(&hdr, 0, sizeof(hdr));
+        hdr.interface_id	= 'S';
+        hdr.cmdp		= (unsigned char *) &cmd;
+        hdr.cmd_len		= sizeof(cmd);
+        hdr.dxfer_direction	= SG_DXFER_FROM_DEV;
+        hdr.dxferp		= resp;
+        hdr.dxfer_len		= resplen;
+        hdr.sbp			= sense;
+        hdr.mx_sb_len		= sizeof(sense);
+        hdr.timeout		= DEF_TIMEOUT;
+ 
+        if (ioctl(fd, SG_IO, &hdr) < 0) {
+		PRINT_DEBUG("do_inquiry: IOCTL failed!\n");
+		return -RTPG_INQUIRY_FAILED;
+	}
+
+	if (scsi_error(&hdr)) {
+		PRINT_DEBUG("do_inquiry: SCSI error!\n");
+		return -RTPG_INQUIRY_FAILED;
+	}
+	PRINT_HEX((unsigned char *) resp, resplen);
+ 
+        return 0;
+}
+
+/*
+ * This function returns the support for target port groups by evaluating the
+ * data returned by the standard inquiry command.
+ */
+int
+get_target_port_group_support(int fd)
+{
+	struct inquiry_data	inq;
+	int			rc;
+
+	rc = do_inquiry(fd, 0, 0x00, &inq, sizeof(inq));
+	if (!rc) {
+		rc = inq.tpgs;
+	}
+
+	return rc;
+}
+
+int
+get_target_port_group(int fd)
+{
+	unsigned char		buf[128];
+	struct vpd83_data *	vpd83;
+	struct vpd83_dscr *	dscr;
+	int			rc;
+
+	rc = do_inquiry(fd, 1, 0x83, buf, sizeof(buf));
+	if (!rc) {
+		vpd83 = (struct vpd83_data *) buf;
+
+		rc = -RTPG_NO_TPG_IDENTIFIER;
+		FOR_EACH_VPD83_DSCR(vpd83, dscr) {
+			if ((((char *) dscr) - ((char *) vpd83)) > sizeof(buf))
+				break;
+
+			if (dscr->id_type == IDTYPE_TARGET_PORT_GROUP) {
+				struct vpd83_tpg_dscr *	p;
+
+				if (rc != -RTPG_NO_TPG_IDENTIFIER) {
+					PRINT_DEBUG("get_target_port_group: "
+						"more than one TPG identifier "
+						"found!\n");
+					continue;
+				}
+
+				p  = (struct vpd83_tpg_dscr *) dscr->data;
+				rc = get_uint16(p->tpg);
+			}
+		}
+		if (rc == -RTPG_NO_TPG_IDENTIFIER) {
+			PRINT_DEBUG("get_target_port_group: "
+				"no TPG identifier found!\n");
+		}
+	}
+
+	return rc;
+}
+
+int
+do_rtpg(int fd, void* resp, long resplen)
+{
+	struct rtpg_command	cmd;
+	struct sg_io_hdr	hdr;
+	unsigned char		sense[SENSE_BUFF_LEN];
+
+	memset(&cmd, 0, sizeof(cmd));
+	cmd.op			= OPERATION_CODE_RTPG;
+	cmd.service_action	= SERVICE_ACTION_RTPG;
+	set_uint32(cmd.length, resplen);
+	PRINT_HEX((unsigned char *) &cmd, sizeof(cmd));
+
+        memset(&hdr, 0, sizeof(hdr));
+	hdr.interface_id	= 'S';
+        hdr.cmdp		= (unsigned char *) &cmd;
+        hdr.cmd_len		= sizeof(cmd);
+        hdr.dxfer_direction	= SG_DXFER_FROM_DEV;
+        hdr.dxferp		= resp;
+        hdr.dxfer_len		= resplen;
+        hdr.mx_sb_len		= sizeof(sense);
+        hdr.sbp			= sense;
+        hdr.timeout		= DEF_TIMEOUT;
+ 
+	if (ioctl(fd, SG_IO, &hdr) < 0)
+                return -RTPG_RTPG_FAILED;
+
+	if (scsi_error(&hdr)) {
+		PRINT_DEBUG("do_rtpg: SCSI error!\n");
+		return -RTPG_RTPG_FAILED;
+	}
+	PRINT_HEX(resp, resplen);
+
+        return 0;
+} 
+
+int
+get_asymmetric_access_state(int fd, unsigned int tpg)
+{
+	unsigned char		buf[128];
+	struct rtpg_data *	tpgd;
+	struct rtpg_tpg_dscr *	dscr;
+	int			rc;
+
+	rc = do_rtpg(fd, buf, sizeof(buf));
+	if (rc < 0)
+		return rc;
+
+	tpgd = (struct rtpg_data *) buf;
+	rc   = -RTPG_TPG_NOT_FOUND;
+	RTPG_FOR_EACH_PORT_GROUP(tpgd, dscr) {
+		if (get_uint16(dscr->tpg) == tpg) {
+			if (rc != -RTPG_TPG_NOT_FOUND) {
+				PRINT_DEBUG("get_asymmetric_access_state: "
+					"more than one entry with same port "
+					"group.\n");
+			} else {
+				PRINT_DEBUG("pref=%i\n", dscr->pref);
+				rc = dscr->aas;
+			}
+		}
+	}
+
+	return rc;
+}
+

Added: multipath-tools/trunk/path_priority/pp_alua/rtpg.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/path_priority/pp_alua/rtpg.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,30 @@
+/*
+ * (C) Copyright IBM Corp. 2004, 2005   All Rights Reserved.
+ *
+ * rtpg.h
+ *
+ * Tool to make use of a SCSI-feature called Asymmetric Logical Unit Access.
+ * It determines the ALUA state of a device and prints a priority value to
+ * stdout.
+ *
+ * Author(s): Jan Kunigk
+ *            S. Bader <shbader at de.ibm.com>
+ * 
+ * This file is released under the GPL.
+ */
+#ifndef __RTPG_H__
+#define __RTPG_H__
+#include "spc3.h"
+
+#define RTPG_SUCCESS				0
+#define RTPG_INQUIRY_FAILED			1
+#define RTPG_NO_TPG_IDENTIFIER			2
+#define RTPG_RTPG_FAILED			3
+#define RTPG_TPG_NOT_FOUND			4
+
+int get_target_port_group_support(int fd);
+int get_target_port_group(int fd);
+int get_asymmetric_access_state(int fd, unsigned int tpg);
+
+#endif /* __RTPG_H__ */
+

Added: multipath-tools/trunk/path_priority/pp_alua/spc3.h
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/path_priority/pp_alua/spc3.h	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,305 @@
+/*
+ * (C) Copyright IBM Corp. 2004, 2005   All Rights Reserved.
+ *
+ * spc3.h
+ *
+ * Tool to make use of a SCSI-feature called Asymmetric Logical Unit Access.
+ * It determines the ALUA state of a device and prints a priority value to
+ * stdout.
+ *
+ * Author(s): Jan Kunigk
+ *            S. Bader <shbader at de.ibm.com>
+ * 
+ * This file is released under the GPL.
+ */
+#ifndef __SPC3_H__
+#define __SPC3_H__
+/*=============================================================================
+ * Some helper functions for getting and setting 16 and 32 bit values.
+ *=============================================================================
+ */
+static inline unsigned short
+get_uint16(unsigned char *p)
+{
+	return (p[0] << 8) + p[1];
+}
+
+static inline void
+set_uint16(unsigned char *p, unsigned short v)
+{
+	p[0] = (v >> 8) & 0xff;
+	p[1] = v & 0xff;
+}
+
+static inline unsigned int
+get_uint32(unsigned char *p)
+{
+	return (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
+}
+
+static inline void
+set_uint32(unsigned char *p, unsigned int v)
+{
+	p[0] = (v >> 24) & 0xff;
+	p[1] = (v >> 16) & 0xff;
+	p[2] = (v >>  8) & 0xff;
+	p[3] = v & 0xff;
+}
+
+/*=============================================================================
+ * Definitions to support the standard inquiry command as defined in SPC-3.
+ * If the evpd (enable vital product data) bit is set the data that will be
+ * returned is selected by the page field. This field must be 0 if the evpd
+ * bit is not set.
+ *=============================================================================
+ */
+#define OPERATION_CODE_INQUIRY		0x12
+
+struct inquiry_command {
+	unsigned char	op;
+	unsigned char	reserved1			: 6;
+	unsigned char	obsolete1			: 1;
+	unsigned char	evpd				: 1;
+	unsigned char	page;
+	unsigned char	length[2];
+	unsigned char	control;
+} __attribute__((packed));
+
+/*-----------------------------------------------------------------------------
+ * Data returned by the standard inquiry command.
+ *-----------------------------------------------------------------------------
+ *
+ * Peripheral qualifier codes.
+ */
+#define PQ_CONNECTED					0x0
+#define PQ_DISCONNECTED					0x1
+#define PQ_UNSUPPORTED					0x3
+
+/* Defined peripheral device types. */
+#define PDT_DIRECT_ACCESS				0x00
+#define PDT_SEQUENTIAL_ACCESS				0x01
+#define PDT_PRINTER					0x02
+#define PDT_PROCESSOR					0x03
+#define PDT_WRITE_ONCE					0x04
+#define PDT_CD_DVD					0x05
+#define PDT_SCANNER					0x06
+#define PDT_OPTICAL_MEMORY				0x07
+#define PDT_MEDIUM_CHANGER				0x08
+#define PDT_COMMUNICATIONS				0x09
+#define PDT_STORAGE_ARRAY_CONTROLLER			0x0c
+#define PDT_ENCLOSURE_SERVICES				0x0d
+#define PDT_SIMPLIFIED_DIRECT_ACCESS			0x0e
+#define PDT_OPTICAL_CARD_READER_WRITER			0x0f
+#define PDT_BRIDGE_CONTROLLER				0x10
+#define PDT_OBJECT_BASED				0x11
+#define PDT_AUTOMATION_INTERFACE			0x12
+#define PDT_LUN						0x1e
+#define PDT_UNKNOWN					0x1f
+
+/* Defined version codes. */
+#define VERSION_NONE					0x00
+#define VERSION_SPC					0x03
+#define VERSION_SPC2					0x04
+#define VERSION_SPC3					0x05
+
+/* Defined TPGS field values. */
+#define TPGS_NONE					0x0
+#define TPGS_IMPLICIT					0x1
+#define TPGS_EXPLICIT					0x2
+#define TPGS_BOTH					0x3
+
+struct inquiry_data {
+	unsigned char	peripheral_qualifier		: 3;
+	unsigned char	peripheral_device_type		: 5;
+	/* Removable Medium Bit (1 == removable) */
+	unsigned char	rmb				: 1;
+	unsigned char	reserved1			: 7;
+	unsigned char	version;
+	unsigned char	obsolete1			: 2;
+	/* Normal ACA Supported */
+	unsigned char	norm_aca			: 1;
+	/* Hierarchical LUN assignment support */
+	unsigned char	hi_sup				: 1;
+	/* If 2 then response data is as defined in SPC-3. */
+	unsigned char	response_data_format		: 4;
+	unsigned char	length;
+	/* Storage Controller Component Supported. */
+	unsigned char	sccs				: 1;
+	/* Access Controls Cordinator. */
+	unsigned char	acc				: 1;
+	/* Target Port Group Support */
+	unsigned char	tpgs				: 2;
+	/* Third Party Copy support. */
+	unsigned char	tpc				: 1;
+	unsigned char	reserved2			: 2;
+	/* PROTECTion information supported. */
+	unsigned char	protect				: 1;
+	/* Basic task management model supported (CmdQue must be 0). */
+	unsigned char	bque				: 1;
+	/* ENClosure SERVices supported. */
+	unsigned char	encserv				: 1;
+	unsigned char	vs1				: 1;
+	/* MULTIPort support. */
+	unsigned char	multip				: 1;
+	/* Medium CHaNGeR. */
+	unsigned char	mchngr				: 1;
+	unsigned char	obsolete2			: 2;
+	unsigned char	addr16				: 1;
+	unsigned char	obsolete3			: 2;
+	unsigned char	wbus16				: 1;
+	unsigned char	sync				: 1;
+	/* LINKed commands supported. */
+	unsigned char	link				: 1;
+	unsigned char	obsolete4			: 1;
+	unsigned char	cmdque				: 1;
+	unsigned char	vs2				: 1;
+	unsigned char	vendor_identification[8];
+	unsigned char	product_identification[8];
+	unsigned char	product_revision[4];
+	unsigned char	vendor_specific[20];
+	unsigned char	reserved3			: 4;
+	unsigned char	clocking			: 2;
+	unsigned char	qas				: 1;
+	unsigned char	ius				: 1;
+	unsigned char	reserved4;
+	unsigned char	version_descriptor[8][2];
+	unsigned char	reserved5[22];
+	unsigned char	vendor_parameters[0];
+} __attribute__((packed));
+
+/*-----------------------------------------------------------------------------
+ * Inquiry data returned when requesting vital product data page 0x83.
+ *-----------------------------------------------------------------------------
+ */
+#define CODESET_BINARY			0x1
+#define CODESET_ACSII			0x2
+#define CODESET_UTF8			0x3
+
+#define ASSOCIATION_UNIT		0x0
+#define ASSOCIATION_PORT		0x1
+#define ASSOCIATION_DEVICE		0x2
+
+#define IDTYPE_VENDOR_SPECIFIC		0x0
+#define IDTYPE_T10_VENDOR_ID		0x1
+#define IDTYPE_EUI64			0x2
+#define IDTYPE_NAA			0x3
+#define IDTYPE_RELATIVE_TPG_ID		0x4
+#define IDTYPE_TARGET_PORT_GROUP	0x5
+#define IDTYPE_LUN_GROUP		0x6
+#define IDTYPE_MD5_LUN_ID		0x7
+#define IDTYPE_SCSI_NAME_STRING		0x8
+
+struct vpd83_tpg_dscr {
+	unsigned char		reserved1[2];
+	unsigned char		tpg[2];
+} __attribute__((packed));
+
+struct vpd83_dscr {
+	unsigned char		protocol_id			: 4;
+	unsigned char		codeset				: 4;
+	/* Set if the protocol_id field is valid. */
+	unsigned char		piv				: 1;
+	unsigned char		reserved1			: 1;
+	unsigned char		association			: 2;
+	unsigned char		id_type				: 4;
+	unsigned char		reserved2;
+	unsigned char		length;				/* size-4 */
+	unsigned char		data[0];
+} __attribute__((packed));
+
+struct vpd83_data {
+	unsigned char		peripheral_qualifier		: 3;
+	unsigned char		peripheral_device_type		: 5;
+	unsigned char		page_code;			/* 0x83 */
+	unsigned char		length[2];			/* size-4 */
+	struct vpd83_dscr	data[0];
+} __attribute__((packed));
+
+/*-----------------------------------------------------------------------------
+ * This macro should be used to walk through all identification descriptors
+ * defined in the code page 0x83.
+ * The argument p is a pointer to the code page 0x83 data and d is used to
+ * point to the current descriptor.
+ *-----------------------------------------------------------------------------
+ */
+#define FOR_EACH_VPD83_DSCR(p, d) \
+		for( \
+			d = p->data; \
+			(((char *) d) - ((char *) p)) < \
+			get_uint16(p->length); \
+			d = (struct vpd83_dscr *) \
+				((char *) d + d->length + 4) \
+		)
+
+/*=============================================================================
+ * The following stuctures and macros are used to call the report target port
+ * groups command defined in SPC-3.
+ * This command is used to get information about the target port groups (which
+ * states are supported, which ports belong to this group, and so on) and the
+ * current state of each target port group.
+ *=============================================================================
+ */
+#define OPERATION_CODE_RTPG		0xa3
+#define SERVICE_ACTION_RTPG		0x0a
+
+struct rtpg_command {
+	unsigned char			op;			/* 0xa3 */
+	unsigned char			reserved1	: 3;
+	unsigned char			service_action	: 5;	/* 0x0a */
+	unsigned char			reserved2[4];
+	unsigned char			length[4];
+	unsigned char			reserved3;
+	unsigned char			control;
+} __attribute__((packed));
+
+struct rtpg_tp_dscr {
+	unsigned char			obsolete1[2];
+	/* The Relative Target Port Identifier of a target port. */
+	unsigned char			rtpi[2];
+} __attribute__((packed));
+
+#define AAS_OPTIMIZED			0x0
+#define AAS_NON_OPTIMIZED		0x1
+#define AAS_STANDBY			0x2
+#define AAS_UNAVAILABLE			0x3
+#define AAS_TRANSITIONING		0xf
+
+#define TPG_STATUS_NONE			0x0
+#define TPG_STATUS_SET			0x1
+#define TPG_STATUS_IMPLICIT_CHANGE	0x2
+
+struct rtpg_tpg_dscr {
+	unsigned char			pref		: 1;
+	unsigned char			reserved1	: 3;
+	unsigned char			aas		: 4;
+	unsigned char			reserved2	: 4;
+	unsigned char			u_sup		: 1;
+	unsigned char			s_sup		: 1;
+	unsigned char			an_sup		: 1;
+	unsigned char			ao_sup		: 1;
+	unsigned char			tpg[2];
+	unsigned char			reserved3;
+	unsigned char			status;
+	unsigned char			vendor_unique;
+	unsigned char			port_count;
+	struct rtpg_tp_dscr		data[0];
+} __attribute__((packed));
+
+struct rtpg_data {
+	unsigned char			length[4];		/* size-4 */
+	struct rtpg_tpg_dscr		data[0];
+} __attribute__((packed));
+
+#define RTPG_FOR_EACH_PORT_GROUP(p, g) \
+		for( \
+			g = &(p->data[0]); \
+			(((char *) g) - ((char *) p)) < get_uint32(p->length); \
+			g = (struct rtpg_tpg_dscr *) ( \
+				((char *) g) + \
+				sizeof(struct rtpg_tpg_dscr) + \
+				g->port_count * sizeof(struct rtpg_tp_dscr) \
+			) \
+		)
+
+#endif /* __SPC3_H__ */
+

Added: multipath-tools/trunk/path_priority/pp_emc/Makefile
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/path_priority/pp_emc/Makefile	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,29 @@
+EXEC		= pp_emc
+BUILD		= glibc
+OBJS		= pp_emc.o
+
+TOPDIR		= ../..
+include $(TOPDIR)/Makefile.inc
+
+CFLAGS = -pipe -g -O2 -Wall -Wunused -Wstrict-prototypes
+
+all: $(BUILD)
+
+glibc:	$(OBJS)
+	$(CC) -o $(EXEC) $(OBJS) $(LDFLAGS)
+	$(STRIP) $(EXEC)
+
+klibc:	$(OBJS)
+	$(CC) -static -o $(EXEC) $(OBJS)
+	$(STRIP) $(EXEC)
+
+install: $(EXEC)
+	install -m 755 $(EXEC) $(DESTDIR)$(bindir)/$(EXEC)
+
+uninstall:
+	rm $(DESTDIR)$(bindir)/$(EXEC)
+clean:	
+	rm -f *.o $(EXEC)
+
+%.o:	%.c
+	$(CC) $(CFLAGS) -c -o $@ $<

Added: multipath-tools/trunk/path_priority/pp_emc/pp_emc.c
==============================================================================
--- (empty file)
+++ multipath-tools/trunk/path_priority/pp_emc/pp_emc.c	Sat Aug 13 15:23:02 2005
@@ -0,0 +1,97 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+
+#include "../../libmultipath/sg_include.h"
+
+#define INQUIRY_CMD     0x12
+#define INQUIRY_CMDLEN  6
+
+int emc_clariion_prio(const char *dev)
+{
+	unsigned char sense_buffer[256];
+	unsigned char sb[128];
+	unsigned char inqCmdBlk[INQUIRY_CMDLEN] = {INQUIRY_CMD, 1, 0xC0, 0,
+						sizeof(sb), 0};
+	struct sg_io_hdr io_hdr;
+	int ret = 0;
+	int fd;
+
+	fd = open(dev, O_RDWR|O_NONBLOCK);
+
+	if (fd <= 0) {
+		fprintf(stderr, "Opening the device failed.\n");
+		goto out;
+	}
+
+	memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
+	io_hdr.interface_id = 'S';
+	io_hdr.cmd_len = sizeof (inqCmdBlk);
+	io_hdr.mx_sb_len = sizeof (sb);
+	io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
+	io_hdr.dxfer_len = sizeof (sense_buffer);
+	io_hdr.dxferp = sense_buffer;
+	io_hdr.cmdp = inqCmdBlk;
+	io_hdr.sbp = sb;
+	io_hdr.timeout = 60000;
+	io_hdr.pack_id = 0;
+	if (ioctl(fd, SG_IO, &io_hdr) < 0) {
+		fprintf(stderr, "sending query command failed\n");
+		goto out;
+	}
+	if (io_hdr.info & SG_INFO_OK_MASK) {
+		fprintf(stderr, "query command indicates error");
+		goto out;
+	}
+
+	close(fd);
+	
+	if (/* Verify the code page - right page & revision */
+	    sense_buffer[1] != 0xc0 || sense_buffer[9] != 0x00) {
+		fprintf(stderr, "Path unit report page in unknown format");
+		goto out;
+	}
+	
+	if ( /* Effective initiator type */
+	    	sense_buffer[27] != 0x03
+		/* Failover mode should be set to 1 */        
+		|| (sense_buffer[28] & 0x07) != 0x04
+		/* Arraycommpath should be set to 1 */
+		|| (sense_buffer[30] & 0x04) != 0x04) {
+		fprintf(stderr, "Path not correctly configured for failover");
+	}
+
+	if ( /* LUN operations should indicate normal operations */
+		sense_buffer[48] != 0x00) {
+		fprintf(stderr, "Path not available for normal operations");
+	}
+
+	/* Is the default owner equal to this path? */
+	/* Note this will switch to the default priority group, even if
+	 * it is not the currently active one. */
+	ret = (sense_buffer[5] == sense_buffer[8]) ? 1 : 0;
+	
+out:
+	return(ret);
+}
+
+int
+main (int argc, char **argv)
+{
+	int prio;
+	if (argc != 2) {
+		fprintf(stderr, "Arguments wrong!\n");
+		prio = 0;
+	} else
+		prio = emc_clariion_prio(argv[1]);
+
+	printf("%d\n", prio);
+	exit(0);
+}
+



More information about the pkg-lvm-commits mailing list