Ce tips vous permettra de savoir comment échanger la sortie de deux descripteurs de fichier sous Linux et sous FreeBSD, l'exemple sera fait ici avec un simple /bin/cat.
Nota: sous FreeBSD, il est nécessaire de monter procfs via la commande suivante :
% sudo mount -t procfs procfs /proc
% cat > /tmp/foo1
Ici, l'utilisateur en question est sbz, changer le pour mettre le vôtre
% ps aux -U sbz | head -n1 ; ps aux -U sbz | grep cat USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND sbz 1891 0,0 0,2 3140 820 p3 S+ 17:34 0:00,00 cat
% ls -la /proc/1891/ total 0 ... lr--r--r-- 1 sbz wheel 0 27 fév 17:34 file -> /bin/cat ... -r--r--r-- 1 sbz wheel 0 27 fév 17:34 status
Ici, on utilisera gdb pour débuger le process id 1891, fermera son descripteur d'entrée (stdout) pour ouvrir un nouveau descripteur pour son entrée.
Ceux sont tout simplement les utilisations des appels systèmes close(2) et creat(2) [note on aurait trés bien pu aussi utiliser open(2) avec le flag O_CREAT ]
% gdb -p 1891 /bin/cat GNU gdb 6.1.1 [FreeBSD] Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-marcel-freebsd"...(no debugging symbols found)... Attaching to program: /bin/cat, process 1891 Reading symbols from /lib/libc.so.7...(no debugging symbols found)...done. Loaded symbols for /lib/libc.so.7 Reading symbols from /libexec/ld-elf.so.1...(no debugging symbols found)...done. Loaded symbols for /libexec/ld-elf.so.1 0x2815d4a1 in read () from /lib/libc.so.7 (gdb) p close(1) $1 = 0 (gdb) p creat("/tmp/foo3",0600) $2 = 1 (gdb) q The program is running. Quit anyway (and detach it)? (y or n) y Detaching from program: /bin/cat, process 1891
Il suffit de reprendre la commande cat lancée plus haut, et d'écrire ce que l'on souhaite dedans
% cat > /tmp/foo1 I love the swap beetween 2 files descriptors witch gdb :)
% cat /tmp/foo3 I love the swap beetween 2 files descriptor witch gdb :)
Je tiens à préciser que cette méthode en aucun cas n'a été trouvé par moi, je suis juste la personne qui francise cela et l'adapte pour FreeBSD, voici la véritable source pastée par gaston sur le chan.