aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/unix
diff options
context:
space:
mode:
authorHsieh Chin Fan <typebrook@gmail.com>2022-04-15 13:56:25 +0800
committerHsieh Chin Fan <typebrook@gmail.com>2022-04-15 13:56:25 +0800
commita948dd82f3df7003b3ae0fc16a3bcf866f3d7a9f (patch)
tree45cc6dfbd2232c1b8adbbdf8959d57d61022098a /tools/unix
parent7bc48f847670293d1acb70c7dcbceaf55fef1e40 (diff)
Update
Diffstat (limited to 'tools/unix')
-rwxr-xr-xtools/unix/fdswap50
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#
5if [ "$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 8if [ "$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
13HELP
14 exit 0
10fi 15fi
11 16
12if gdb --version > /dev/null 2>&1; then true 17if ! gdb --version &>/dev/null; then
13else echo "Unable to find gdb."; exit 1 18 echo "Unable to find gdb."
19 exit 1
14fi 20fi
15 21
16src="$1"; dst="$2"; shift; shift 22src="$1"; dst="$2"; shift; shift
@@ -18,18 +24,18 @@ pids=$*
18 24
19for pid in ${pids:=$( /sbin/fuser $src | cut -d ':' -f 2 )}; 25for pid in ${pids:=$( /sbin/fuser $src | cut -d ':' -f 2 )};
20do 26do
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
35done 41done