From 7bc48f847670293d1acb70c7dcbceaf55fef1e40 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 15 Apr 2022 12:54:20 +0800 Subject: Update --- tools/unix/fdswap | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 tools/unix/fdswap (limited to 'tools/unix') diff --git a/tools/unix/fdswap b/tools/unix/fdswap new file mode 100755 index 0000000..8b7d0bd --- /dev/null +++ b/tools/unix/fdswap @@ -0,0 +1,35 @@ +#!/bin/bash +# +# fdswap +# +if [ "$2" = "" ]; then + echo " + Usage: $0 /path/to/oldfile /path/to/newfile [pids] + Example: $0 /var/log/daemon.log /var/log/newvolume/daemon.log 1234 + Example: $0 /dev/pts/53 /dev/null 2345"; exit 0 +fi + +if gdb --version > /dev/null 2>&1; then true +else 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 -- cgit v1.2.3-70-g09d2 From a948dd82f3df7003b3ae0fc16a3bcf866f3d7a9f Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 15 Apr 2022 13:56:25 +0800 Subject: Update --- tools/unix/fdswap | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) (limited to 'tools/unix') diff --git a/tools/unix/fdswap b/tools/unix/fdswap index 8b7d0bd..aef47b6 100755 --- a/tools/unix/fdswap +++ b/tools/unix/fdswap @@ -1,16 +1,22 @@ #!/bin/bash # # fdswap -# -if [ "$2" = "" ]; then - echo " - Usage: $0 /path/to/oldfile /path/to/newfile [pids] - Example: $0 /var/log/daemon.log /var/log/newvolume/daemon.log 1234 - Example: $0 /dev/pts/53 /dev/null 2345"; exit 0 +# +# Orignal author: ingvarha +# ref: https://ingvarha.wordpress.com/2010/07/10/changing-a-process-file-descriptor-on-the-fly/ + +if [ "$2" = "" ]; then +< /dev/null 2>&1; then true -else echo "Unable to find gdb."; exit 1 +if ! gdb --version &>/dev/null; then + echo "Unable to find gdb." + exit 1 fi src="$1"; dst="$2"; shift; shift @@ -18,18 +24,18 @@ 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 + 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 -- cgit v1.2.3-70-g09d2 From e3757d41bfa94dd79266a002def83601204010ae Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 15 Apr 2022 14:03:20 +0800 Subject: Update --- tools/unix/fdswap | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tools/unix') diff --git a/tools/unix/fdswap b/tools/unix/fdswap index aef47b6..251cdea 100755 --- a/tools/unix/fdswap +++ b/tools/unix/fdswap @@ -7,9 +7,9 @@ if [ "$2" = "" ]; then <