Executable file uninstall on Linux

As the superuser, I ran the following command in linux

rm rm 

which removes itself. Because when the process is running, its reference count is non-zero. It cannot be deleted. So I am confused, how and why is this happening?

I tried the same with chown 0000 chown .

 cp -r Dir1/ Dir2/ 

The above command also happens what happens when I delete the source directory only when doing the copy.

+4
source share
3 answers

This is the same as for temporary files.

Recall that the usual way to create some temporary file is to open (2) the file (keeping its file descriptor), then unlink (2) (while still having the open file descriptor). Then, the file data remains in the file system until the process is executed, and close (2) -d this file descriptor.

This is because files are really inodes - not file names in directories. (directories contain entries that associate names with inodes).

The kernel controls the set of "used" (or "open") inodes, and this set contains the inodes executed by the processes (actually, the inodes involved in some address mapping, for example thru mmap (2) or execve (2) )

So, immediately after running /bin/rm /bin/rm kernel has one link to the rm binary as an executable file of the process.

When it processes syscall unlink , it temporarily has two links (one of which is a process in progress, the other path /bin/rm passed to the unlink kernel implementation) and reduces it to unity.

Of course, you should avoid typing /bin/rm /bin/rm , but then you usually have a separate shell, such as sash , to restore your system.

+2
source

On Windows, "rm rm" is probably not possible because of the reference account you mentioned. However, on most * nix systems this is the case. "rm" as well as "chmod" is loaded into memory and only then will execute everything that is indicated on the command line. Another example: edit the file in one window and, editing this file, delete it in another window. This should also be possible on most * nix systems, regardless of the number of links.

+1
source

You cannot delete a directory with rm until it is empty.

0
source

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


All Articles