可执行文件在Linux上自行删除 [英] Executable Deleting Itself on linux

查看:170
本文介绍了可执行文件在Linux上自行删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为超级用户,我在linux上执行了以下命令

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?

我也尝试过使用chown 0000 chown.

cp -r Dir1/ Dir2/

在上述命令中,仅在复制过程中删除源目录时会发生什么情况?

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

推荐答案

与临时文件相同.

回想一下,创建一些临时文件的常用方法是 open( 2)一个文件(保留其文件描述符),然后取消链接(2)(同时还有一个打开的文件描述符).然后,只要进程正在运行,文件的数据就会保留在文件系统中,并且没有关闭(2) -d文件描述符.

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.

这是因为文件实际上是 inodes -而不是目录中的文件名. (目录包含将名称与inode关联的条目).

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

内核管理已使用"(或已打开")inode的集合,并且该集合包含进程执行的inode(实际上,inode涉及某些地址映射,例如通过

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))

因此,在/bin/rm /bin/rm启动之后,内核便将一个对rm二进制文件的引用作为该进程的可执行文件.

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

在处理unlink系统调用时,它临时具有两个引用(一个是正在执行的进程,另一个是/bin/rm传递给unlink内核实现的路径)并将其减小为一个.

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.

当然,您应该避免键入/bin/rm /bin/rm,但是通常您会拥有一些独立的外壳,例如sash才能修复系统.

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.

这篇关于可执行文件在Linux上自行删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆