linux中可执行文件为什么不能拷贝覆盖

对于一个普通的文件,假如有两个文件,分别是file和file1,我们使用 cp file1 file的方式使用file1的内容来覆盖file的内容,这样是可以的。

但是对于可执行文件来说,当这个文件在执行的时候,是不能通过cp的方式来覆盖的,为什么呢 ?

如下的代码,使用 gcc hello.c -o hello, gcc hello.c -o hello1分别编译出两个可执行文件hello和hello1,然后执行hello,再执行cp hello1 hello,这样会报错。如果可执行文件没有被执行,那么是cp命令是可以执行成功的。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main() {
  int i = 0;
  while (i < 3000) {
    sleep(1);
    i++;
  }
  return 0;
}

可执行文件被执行的时候,通过execve系统调用来进行。在execve中,会调用到 deny_write_access()来禁止写权限,而使用cp命令,会打开文件进行写,所以操作失败。

static struct file *do_open_execat(int fd, struct filename *name, int flags)
{
    ...
	err = deny_write_access(file);
    ...
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 13:40:06       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 13:40:06       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 13:40:06       90 阅读
  4. Python语言-面向对象

    2024-07-10 13:40:06       98 阅读

热门阅读

  1. Day2--每日一练

    2024-07-10 13:40:06       28 阅读
  2. 东方博宜1626 - 暑假的旅游计划

    2024-07-10 13:40:06       28 阅读
  3. react小白面试不得不会的20个问题——第二篇

    2024-07-10 13:40:06       32 阅读
  4. 简单滤波算法伪码

    2024-07-10 13:40:06       32 阅读
  5. Mongodb索引简介

    2024-07-10 13:40:06       24 阅读
  6. Linux 6种日志查看方法

    2024-07-10 13:40:06       26 阅读
  7. 案例研究(Case Study)是什么?怎么写?

    2024-07-10 13:40:06       29 阅读
  8. Linux虚拟化技术:从Xen到KVM

    2024-07-10 13:40:06       33 阅读
  9. 深度学习图片增强方式

    2024-07-10 13:40:06       28 阅读
  10. 什么是DNS欺骗

    2024-07-10 13:40:06       30 阅读
  11. leetcode hot 100 刷题记录

    2024-07-10 13:40:06       25 阅读
  12. 全面解析C#:现代编程语言

    2024-07-10 13:40:06       24 阅读
  13. 【深入探索】揭秘SQL Server的多重身份验证模式

    2024-07-10 13:40:06       30 阅读