5

Being a super user , I executed the following command on linux

rm rm

which removes itself. Because when process is in execution , its reference count is not zero.Hence it cannot be deleted. So I am bemused, how and why does it happen?

I tried the same with chown 0000 chown as well.

cp -r Dir1/ Dir2/

In above command also , what happens when i delete the source directory only when copying is in progress???

Matt Bryant
  • 4,841
  • 4
  • 31
  • 46
Luv Singh
  • 51
  • 2

3 Answers3

2

It is the same as for temporary files.

Recall that a usual way to create some temporary file is to open(2) a file (keeping its file descriptor), then unlink(2) (while still having an open file descriptor). Then the data of the file remains in the file system as long as the process is running and have not close(2)-d that file descriptor.

This is because files really are inodes -not file names in directories. (directories contain entries associating names to inodes).

The kernel manages the set of "used" (or "opened") inodes, and that set contains the inodes executed by processes (actually, the inodes involved in some address mapping like thru mmap(2) or execve(2))

So just after /bin/rm /bin/rm starts, the kernel has one reference to rm binary as the executable of the process.

When it processes the unlink syscall, it has temporarily two references (one being the process in execution, the other the path /bin/rm passed to unlink kernel implementation) and decreases it to one.

Of course you should avoid typing /bin/rm /bin/rm but then you usually have some standalone shell like sash to be able to repair your system.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

On Windows, "rm rm" is probably not possible, because of the reference count you mentioned. On most *nix systems however, it is. "rm" and also "chmod" is loaded into memory and only then will execute whatever the commandline specified. Another example: edit a file in one window and while editing that file, remove it in another window. That too should be possible on most *nix systems, regardless of reference counts.

ckujau
  • 225
  • 1
  • 15
0

You cant delete a directory using rm until its empty..

Abhishek
  • 874
  • 2
  • 8
  • 23