by 323 · 3y ago · 0 comments
Because of this Linux "feature" I experienced data loss - I zipped a file and deleted it, but the process which created it was still writing to it. But all the data that it wrote now ended up in the void since the file didn't exist anymore. And there was no error/warning that the process was writing to a non-linked file, so I discovered the data loss much later.
And Microsoft's been trying to manage that insanity since 1995.
So, you experienced data loss because you deleted a file you didn't mean to.
...and?
In the example I gave, the other process was continuously writing to this file while I was zipping it.
If the process creating the zip file runs faster, and finishes, and closes the file, your attempt to delete it would have worked anyway. Because the program that created it didn't have it open any more.
The zip creation and me deleting the file were in parallel with another process writing to the file.
And I didn't delete the zip file, I deleted the original file that I zipped.
In this case, you can have your cake (delete the directory entry) and eat it too (get the data back through /proc).
When the last directory entry for a file is passed to the unlink() system call, the file is removed from the directory hierarchy.
However, the data on media will be deleted only after all open file descriptors are closed.
One exploitation of this feature that I have seen is the SQLite temporary tablespace, which is created and immediately unlinked, ensuring that it will not persist after the program terminates.
Yes, it does. Windows is POSIX/FIPS-151
More precisely, what data was lost, and how did it get lost?
Process P, the data producer, writes to file foo.
Process Z, the zip-process, reads file foo and produces foo.zip
Process rm, removes file foo.
I imagine it went a bit like this:
P --output foo &
Z < foo > foo.zip
rm foo
# time passes
# process P terminates
Any data written to foo since Z was run is now lost.User 323 assumed that either rm would fail, letting them know that foo cannot be deleted, or that P would throw an error when the file was deleted.
(I don't think this is a reasonable expectation, and it's a failure on 323's hand of not learning the UNIX file model, but there's the situation).
I can update glibc or other core system libraries on Linux without rebooting. Processes using the old library will show "(deleted)" in their /proc/self/maps files, but they will continue to use the old code. New processes will use the new library, and downtime was not required.
This is not possible on Windows because of mandatory locking on core system libraries. As a consequence, Windows must reboot for every Patch Tuesday.
There are only a few places that Linux has mandatory locking, and this is a good thing.
Here is a script that will show you all the deleted libraries that are still in use on Linux:
# cat stale_libraries.sh
#!/bin/sh
awk '$NF=="(deleted)" && $(NF-1) ~ /[.]so/ {
sub(/;.*$/,"",$(NF-1))
print $(NF-1), $NF}' /proc/*/maps | sort -u
Here is a script that will show you all PIDs that are running with deleted libraries: # cat stale_programs.sh
#!/bin/sh
grep '[.]so.*deleted)' /proc/*/maps |
sed 's/[:][^/]*//' |
sort -u |
while read -r line
do pid=${line#/} pid=${pid#*/} pid=${pid%%/*}
xargs -0 printf '%s ' < "/proc/$pid/cmdline" | sed 's/[ ]$//'
printf :%s\\n "$line"
done
Some of the maps files can only be read by root.Linux doesn't need reboots only in theory. I have an Ubuntu server box and not a week passes in which I don't see a "system reboot is required" prompt when I SSH into it.
Someone with more knowledge please correct me if I got the above wrong.
That being said, the issue with Windows is that it prevents me from deleting a file if it's open in a text editor or any program, which should not be the default in my opinion. A directory gets locked simply because you have explorer open in that path, or a terminal cd'd into that path, which is annoying and counter productive most of the time.
File locking on Linux is advisory, it excludes other processes which try to lock the same region of the same file, but not operations other than locking. Mandatory locks were optional (required a mount option, see https://lwn.net/Articles/667210/ for details), and were removed in 5.15 (https://lwn.net/Articles/874493/).
Of course that's assuming the compression program used does attempt to apply file lock, which may not be the case according to another comment.
The standard Linux compression programs, zip, gzip, tar, don't lock by default. I don't even think they have such an option.