Does pipe have to write temporary file?
I found that if I transfer a great amount of data between two processes via pipe, some temporary file will be created by linux in /tmp directory. If the pipe operation succeeds, the corresponding temporary file will be removed by OS automatically. But if the operation failed, the tmp file remains there.
For some reason, I don't want the user have the oppotunity to get the data I transfered trhough pipe, so I don't want anything left on harddisk even if my program crashed. How can I do this?
pipes don't store data on disk. /bin/echo foo | grep bar doesn't create any files. try
strace -f sh -c '/bin/echo foo | grep bar'
to see all the system calls made by a shell when running a pipeline.echo
is a shell builtin, so I suggested/bin/echo
to make the shell run an executable./tmp
doesn't have to be on disk. It can be mounted on tmpfs (i.e. backed by virtual memory). Note that a reboot will empty/tmp
in that case, so use/var/tmp
for anything you want to leave around.
If what you're doing is putting data into a file, then it's not using a pipe. If the file is a fifo, not a regular file, then it's just a named rendezvous, and doesn't contain data. Use ls -l to find out.
And note that if you're hoping to stop users from seeing what's going through pipes in processes they own, you are pretty much SOL, because strace
can inspect everything a process does that interacts with anything outside the process, except for reading/writing mmapped shared memory. ltrace
is even more invasive. If your program will run on systems where the local user has root, you can't stop them at all. On Unix, root can do anything, and has powerful tools for the purpose.
A true pipe is a block of memory in the kernel, a buffer that is read/written by some processes. It does not create files anywhere.
Some apps have options that switch between using pipes (faster, no hitting disk, takes a little more memory) and using temp files (takes a bit less memory, allows you to possibly see the temp files, a touch slower). gcc
is one such application, though probably are others.