diff options
Diffstat (limited to 'bin/unix/fdswap')
| -rwxr-xr-x | bin/unix/fdswap | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/bin/unix/fdswap b/bin/unix/fdswap new file mode 100755 index 0000000..251cdea --- /dev/null +++ b/bin/unix/fdswap | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | # | ||
| 3 | # fdswap | ||
| 4 | # | ||
| 5 | # Orignal author: ingvarha | ||
| 6 | # ref: https://ingvarha.wordpress.com/2010/07/10/changing-a-process-file-descriptor-on-the-fly/ | ||
| 7 | |||
| 8 | if [ "$2" = "" ]; then | ||
| 9 | <<HELP cat | ||
| 10 | Usage: ${0##*/} /path/to/oldfile /path/to/newfile [pids] | ||
| 11 | Example: ${0##*/} /var/log/daemon.log /var/log/newvolume/daemon.log 1234 | ||
| 12 | Example: ${0##*/} /dev/pts/53 /dev/null 2345 | ||
| 13 | HELP | ||
| 14 | exit 0 | ||
| 15 | fi | ||
| 16 | |||
| 17 | if ! gdb --version &>/dev/null; then | ||
| 18 | echo "Unable to find gdb." | ||
| 19 | exit 1 | ||
| 20 | fi | ||
| 21 | |||
| 22 | src="$1"; dst="$2"; shift; shift | ||
| 23 | pids=$* | ||
| 24 | |||
| 25 | for pid in ${pids:=$( /sbin/fuser $src | cut -d ':' -f 2 )}; | ||
| 26 | do | ||
| 27 | echo "src=$src, dst=$dst" | ||
| 28 | echo "$src has $pid using it" | ||
| 29 | cmd=$(mktemp) | ||
| 30 | { | ||
| 31 | echo "attach $pid" | ||
| 32 | echo 'call (int)open("'$dst'", 66, 0666)' | ||
| 33 | for ufd in $(LANG=C ls -l /proc/$pid/fd | \ | ||
| 34 | grep "$src"\$ | awk ' { print $9; } '); | ||
| 35 | do echo 'call (int)dup2($1,'"$ufd"')'; done | ||
| 36 | echo 'call (int)close($1)' | ||
| 37 | echo 'detach'; echo 'quit' | ||
| 38 | sleep 5 | ||
| 39 | } | tee /dev/tty >$cmd | ||
| 40 | gdb -x $cmd | ||
| 41 | done | ||