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