Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hacking a program's file descriptors on the fly using gdb

1,980 views
Skip to first unread message

Robert McKay

unread,
Aug 14, 2007, 6:56:19 PM8/14/07
to

I had a problem today where a logfile was getting too big. I didn't want
to restart the program and if I were to simply truncate the logfile it
would soon fill up the small partition again. Wouldn't it be nice if you
could just change where the file descriptor points? Well.. as it turns
out, you can!

ObHack:

-------------[cut here]------------
#!/bin/bash
#
# Swap/Roll a logfile
#
# Usage: <old logfile> <new logfile> [ optional pids ]
# ./swap.sh /var/log/logfile /tmp/logfile [pids]
#
# Author: Robert McKay <rob...@mckay.com>
# Date: Tue Aug 14 13:36:35 BST 2007

src=$1
dst=$2
shift
shift
pids=$*

for pid in ${pids:=$( /sbin/fuser $src )};
do
echo "$src has $pid using it"
(
echo "attach $pid"
echo 'call open("'$dst'", 66, 0666)'
for ufd in $(ls -l /proc/$pid/fd | grep $src\$ | awk ' { print $9; } ');
do
echo 'call dup2($1,'$ufd')'
done
echo 'call close($1)'
echo 'detach'
echo 'quit'
sleep 3
) | gdb -q -x -
done
-------------[cut here]------------

Happy Hacking,

Robert.

0 new messages