2009/08/26

Using Proc

In /proc, there are subdirectories for each process running on the system, named based on the PID number of the process. In each of these directories, there is a fd/ subdirectory that contains files that represent the file descriptors the process currently has open. These files are actually symlinks that point to the actual device, socket, or other file the process currently has open and mapped to that file descriptor.

If you have a program that can read input from a file but not from standard input, or that can write to a file but not to standard output, you may be able to cheat by taking advantage of these special files:

/proc/self/fd/0 is standard input of the current process
/proc/self/fd/1 is standard output of the current process
/proc/self/fd/2 is standard error of the current process

For example if 'myfilter' can only read from a file, which it takes as its first argument, you can make it read from standard input instead with:

'myfilter /proc/self/fd/0'

Another example: 'cat filename > /proc/self/fd/2' sends the contents of filename out standard
error instead of standard output. Whether these tricks will behave in a sane manner will depend on how the process actually handles the file it opens.

No comments:

Post a Comment