How to create temporary files on linux that will automatically be cleaned after themselves no matter what?

I want to create a temporary file on Linux, making sure the file disappears after my program finishes, even if it was killed or someone made a hard reset at the wrong time. Does tmpfile() support all this for me?

+4
source share
6 answers

You seem to be preoccupied with the idea that files may stay away from some due to some race conditions, I don’t see an explanation of why this is troubling.

"A race condition occurs when a program does not work as intended, due to an unexpected ordering of events that give rise to a conflict over the same resource."

I assumed that from your comments on other answers, your concern was specifically about the deadlock, which is the result of an attempt to fix the state of the race (approval of a shared resource). It is not yet clear what your problem is, calling tmpfile() and causing the program to run abnormally before this function calls the unlink() call is the least of your problems if your application is really fragile.

Given that there is no mention of concurrency, streaming, or other processes sharing this file descriptor in this temporary file, I still do not see the possibility of a race condition, perhaps the concept of an incomplete logical transaction, but this can be detected and cleared.

The right way to make sure that all the allocated file system resources are cleared, not only on the application output , but also on before . All my server code, make sure everything is cleared from the previous run before , and it becomes available.

Put your temporary files in a sub-dir in /tmp to make sure your application clears this sub-directory at startup and shutdown normally. You can complete the launch of the application using a shell script that detects abnormal ( kill -9 ) PID based shutdowns and also cleans up actions.

+3
source

according to the tmpfile () page:

The file will be automatically deleted when it is closed, or the program terminates.

I have not tested, but it seems like it should do what you want.

Wherein:

By default, if TMPDIR is not installed, it is / tmp.

Then, when the reboot is done, /tmp will be empty.

+2
source

If you do not want to use tmpfile() , you can unlink() create the file immediately after it is created. It will remain open and be present and distributed until its closure.

But with a hard reboot, fsck may be needed to recover the space. But since this is always the case, this is not a particular disadvantage of this approach.

+2
source

Linux uses mktemp .

+1
source

EDIT: Yes

I checked the source of tmpfile and it really uses the glglgl trick and instantly unlocks the file.

Original:

I would say no. The murdered one should work, but I would suggest that it could happen that after a hard reboot (for example, due to power) the file is still there. But it depends on your Linux distribution and the settings used.

If a temporary file is created in ramdisk, it disappears (there is unix arithmetic there, for example, use ram-based tmpfs for temporary files).

Or, if you use an environment with a specific tmp policy, it may also disappear (maybe not instantaneous, but often there are policies, for example, to delete all files in / tmp that are not available within one month), but it can also be on a standard file system where such rules do not apply. In this case, the file will remain.

0
source

The traditional approach is to configure the signal handler to clear if the program is interrupted. This will not handle kill -9 or a physical reboot that cannot be captured. Create temporary files in /tmp , which are usually cleared when the system boots. All that remains is to teach people not to use kill -9 when they don’t need it, but it looks like a tough battle.

0
source

Source: https://habr.com/ru/post/1403651/


All Articles