#!/bin/bash # # fdswap # # Orignal author: ingvarha # ref: https://ingvarha.wordpress.com/2010/07/10/changing-a-process-file-descriptor-on-the-fly/ if [ "$2" = "" ]; then </dev/null; then echo "Unable to find gdb." exit 1 fi src="$1"; dst="$2"; shift; shift pids=$* for pid in ${pids:=$( /sbin/fuser $src | cut -d ':' -f 2 )}; do echo "src=$src, dst=$dst" echo "$src has $pid using it" cmd=$(mktemp) { echo "attach $pid" echo 'call (int)open("'$dst'", 66, 0666)' for ufd in $(LANG=C ls -l /proc/$pid/fd | \ grep "$src"\$ | awk ' { print $9; } '); do echo 'call (int)dup2($1,'"$ufd"')'; done echo 'call (int)close($1)' echo 'detach'; echo 'quit' sleep 5 } | tee /dev/tty >$cmd gdb -x $cmd done