Win32\'s CreateFile has FILE_FLAG_DELETE_ON_CLOSE
, but I\'m on Linux.
I want to open a temporary file which will always be deleted upon program terminat
I don't see ways to do it any way simpler. This is the boilerplate any production quality program must go through; +500 lines easily.
Do you really need the name to remain visible?
Suppose you take the option of immediately unlinking the file. Then:
preemptive unlink(2): this is pretty good except that I need the file to remain visible in the filesystem (otherwise the system is harder to monitor/troubleshoot).
You can still debug on a deleted file, since it will still be visible under /proc/$pid/fd/
. As long as you know the pids of your processes, enumerating their open files should be easy.
the names need to remain visible during normal operation because they are shared between programs.
You can still share the deleted open file between processes by passing around the file descriptor over Unix domain sockets. See Portable way to pass file descriptor between different processes for more information.