git » libfiu » commit 91b40eb

Add a new utility, "fiu-ctrl", to make remote control easier

author Alberto Bertogli
2009-06-16 19:13:43 UTC
committer Alberto Bertogli
2009-06-17 01:10:09 UTC
parent 72c474d8d7f37a0cdf2c2bed07b36b042323f54d

Add a new utility, "fiu-ctrl", to make remote control easier

This patch adds a new script, fiu-ctrl that makes it easier to remote
control running applications.

It is strongly related to fiu-run, and provides a very similar interface.

See the documentation for more detail.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

Makefile +15 -4
utils/Makefile +18 -0
utils/fiu-ctrl +158 -0
utils/fiu-ctrl.1 +73 -0

diff --git a/Makefile b/Makefile
index ddef2ed..4aa8326 100644
--- a/Makefile
+++ b/Makefile
@@ -4,9 +4,9 @@ default: all
 
 install: all_install
 
-all: libfiu preload
+all: libfiu preload utils
 
-all_install: libfiu_install preload_install
+all_install: libfiu_install preload_install utils_install
 
 
 libfiu:
@@ -30,6 +30,16 @@ preload_install: preload
 	$(MAKE) -C preload install
 
 
+utils:
+	$(MAKE) -C utils
+
+utils_clean:
+	$(MAKE) -C utils clean
+
+utils_install: utils
+	$(MAKE) -C utils install
+
+
 bindings: python2 python3
 
 bindings_install: python2_install python3_install
@@ -52,13 +62,14 @@ python_clean:
 	cd bindings/python && rm -rf build/
 
 
-clean: python_clean preload_clean libfiu_clean
+clean: python_clean preload_clean libfiu_clean utils_clean
 
 
 .PHONY: default all clean install all_install \
 	libfiu libfiu_clean libfiu_install \
 	python2 python2_install python3 python3_install python_clean \
 	bindings bindings_install bindings_clean \
-	preload preload_clean preload_install
+	preload preload_clean preload_install \
+	utils utils_clean utils_install
 
 
diff --git a/utils/Makefile b/utils/Makefile
new file mode 100644
index 0000000..a0ac66e
--- /dev/null
+++ b/utils/Makefile
@@ -0,0 +1,18 @@
+
+# prefix for installing the binaries
+PREFIX=/usr/local
+
+
+default:
+	
+install:
+	install -d $(PREFIX)/bin
+	install -m 0755 fiu-ctrl $(PREFIX)/bin
+	install -d $(PREFIX)/man/man1
+	install -m 0644 fiu-ctrl.1 $(PREFIX)/man/man1/
+
+clean:
+
+.PHONY: default install clean
+
+
diff --git a/utils/fiu-ctrl b/utils/fiu-ctrl
new file mode 100755
index 0000000..d2feeac
--- /dev/null
+++ b/utils/fiu-ctrl
@@ -0,0 +1,158 @@
+#!/bin/bash
+
+# This scripts present a friendly interface to the fiu remote control
+# capabilities. Currently, it supports only the named pipe method (the only
+# one implemented).
+
+# default remote control over named pipes prefix; we use the same one as
+# fiu-run so it's easier to use
+FIFO_PREFIX="${TMPDIR:-/tmp}/fiu-ctrl"
+
+# commands to send, will be filled by options processing; must be in the
+# format supported by the fiu remote control (see fiu-rc.c for more details)
+declare -a CMDS
+
+
+HELP_MSG="
+Usage: fiu-ctrl [options] PID [PID ...]
+
+The following options are supported:
+
+  -e fpname	Enable the given failure point name.
+  -p prob	... with the given probability (defaults to 100%).
+  -u failnum	... and this failnum (must be != 0) (defaults to 1).
+  -i failinfo	... and this failinfo (defaults to 0).
+  -d fpname	Disable the given failure point name.
+  -f ctrlpath	Set the default prefix for remote control over named pipes.
+		(defaults to \"$FIFO_PREFIX\", which is usually correct if
+		the program was run using fiu-run(1)).
+
+The -p, -u and -i options must come after the -e they affect.
+
+For example:
+
+  fiu-ctrl -e posix/io/read -p 25 -e libc/mm/malloc -p 5 12345
+
+will tell the process with pid 12345 to enable the failure point
+'posix/io/read' with a 25% of probability to fail, and the failure point
+'libc/mm/malloc' with a 5% of probability to fail. And:
+
+  fiu-ctrl -d posix/io/read 12345
+
+will tell the same process to disable the previously enabled failure point.
+
+You can control multiple processes at once by specifiying more than one
+process ID.
+"
+
+
+#
+# Parse the options
+#
+
+if [ $# -lt 1 ]; then
+	echo "$HELP_MSG"
+	exit 1
+fi
+
+function opts_reset() {
+	# variables to store what we know so far; after a new name is found
+	# the old one is added to $ENABLE
+	NAME=""
+	PROB=-1
+	FAILNUM=1
+	FAILINFO=0
+}
+
+function add_cmd() {
+	if [ "$NAME" != "" ]; then
+		if [ $PROB -ge 0 ]; then
+			CMDS+="enable_random $NAME $PROB $FAILNUM $FAILINFO"
+		else
+			CMDS+="enable $NAME $FAILNUM $FAILINFO"
+		fi
+		opts_reset;
+	fi
+}
+
+opts_reset;
+while getopts "+e:p:u:i:d:f:h" opt; do
+	case $opt in
+	e)
+		# add the current one, if any
+		add_cmd;
+		opts_reset;
+		NAME="$OPTARG"
+		;;
+	p)
+		PROB="$OPTARG"
+		;;
+	u)
+		FAILNUM="$OPTARG"
+		;;
+	i)
+		FAILINFO="$OPTARG"
+		;;
+	f)
+		FIFO_PREFIX="$OPTARG"
+		;;
+	d)
+		CMDS+="disable $OPTARG"
+		opts_reset;
+		;;
+	h|*)
+		echo "$HELP_MSG"
+		exit 1
+		;;
+	esac;
+done
+
+# add leftovers
+if [ "$NAME" != "" ]; then
+	add_cmd;
+fi
+
+# eat the parameters we already processed
+shift $(( $OPTIND - 1 ))
+
+PIDS=""
+PREFIXES=""
+
+for i in "$@"; do
+	if test -p "$i.out"; then
+		PREFIXES="$PREFIXES $i"
+	elif kill -0 $i > /dev/null 2> /dev/null && \
+			test -p	"$FIFO_PREFIX-$i.out"; then
+		PIDS="$PIDS $i"
+	else
+		echo "Error: unknown pid or named pipe $i, skipping"
+		echo "Note that options must come before the PID"
+	fi
+done
+
+#
+# Send the commands
+#
+
+function send_cmd_fifo() {
+	# $1 = complete fifo prefix
+	# $2+ = command to send
+	# echoes the reply
+	P=$1
+	shift 1
+	echo $@ > $P.in
+	R="`cat $P.out`"
+	if [ "$R" -eq -1 ]; then
+		echo "$P: Command returned error"
+	fi
+}
+
+for c in "${CMDS[@]}"; do
+	for i in $PIDS; do
+		send_cmd_fifo $FIFO_PREFIX-$i "$c"
+	done
+	for i in $PREFIXES; do
+		send_cmd_fifo $i "$c"
+	done
+done
+
diff --git a/utils/fiu-ctrl.1 b/utils/fiu-ctrl.1
new file mode 100644
index 0000000..ceafd6b
--- /dev/null
+++ b/utils/fiu-ctrl.1
@@ -0,0 +1,73 @@
+.TH fiu-ctrl 1 "16/Jun/2009"
+.SH NAME
+fiu-ctrl - a script to remote control programs using libfiu
+.SH SYNOPSIS
+fiu-ctrl [options] PID [PID ...]
+
+.SH DESCRIPTION
+fiu-ctrl is a script to enable/disable failure points in running programs that
+are using \fBlibfiu\fR(3).
+
+Programs are usually launched using \fBfiu-run\fR(1), which enables
+libfiu's remote control capabilities without the need to modify the
+program's code.
+
+For additional documentation, go to the project's website at
+.IR http://blitiri.com.ar/p/libfiu .
+
+.SH OPTIONS
+.TP
+.B "-e fpname"
+Enable the given failure point name.
+.TP
+.B "-p prob"
+Use the given probability for the previous failure point. In percent, defaults
+to 100, which means "always enabled". Must come \fIafter\fR the \fB-e\fR it
+affects.
+.TP
+.B "-u failnum"
+Use the given number as the failnum for the previous failure point. Must be !=
+0, defaults to 1. Must come \fIafter\fR the \fB-e\fR it affects.
+.TP
+.B "-i failinfo"
+Use the given number as the failinfo for the previous failure point. Defaults
+to 0. Must come \fIafter\fR the \fB-e\fR it affects.
+.TP
+.B -d
+Disable the given failure point name.
+.TP
+.B "-f ctrlpath"
+Set the default prefix for remote control over named pipes. Defaults to
+"$TMPDIR/fiu-ctrl", or "/tmp/fiu-ctrl" if "$TMPDIR" is not set, which is the
+usually correct for programs launched using \fBfiu-run\fR(1).
+
+
+.SH EXAMPLES
+The following command will tell the process running with PID 12345 to enable
+the failure point \fIposix/io/read\fR with a 25% of probability to fail, and the
+failure point \fIlibc/mm/malloc\fR with a 5% of probability to fail:
+
+.RS
+.nf
+fiu-ctrl -e posix/io/read -p 25 -e libc/mm/malloc -p 5 12345
+.fi
+.RE
+
+And the following will tell the same process to disable the previously enabled
+failure point \fIposix/io/read\fR:
+
+.RS
+.nf
+fiu-ctrl -d posix/io/read 12345
+.fi
+.RE
+
+.SH SEE ALSO
+.BR libfiu (3),
+.BR fiu-run (1).
+
+.SH BUGS
+If you want to report bugs, or have any questions or comments, just let me
+know at albertito@blitiri.com.ar. For more information about libfiu, you can
+go to http://blitiri.com.ar/p/libfiu.
+