In linux, cp reports Textfilebusy

Source: Internet
Author: User
Some colleagues asked me: when using the cp command, if the file to be overwritten (binary executable file) is running, the cp will report an error, how does the cp command know that the file is being executed? Label: Linux cp

My first thought was: Maybe ioctl can check the file. Unfortunately, I guess it is wrong. I checked the cp source code. if a file is running and another process is open again to obtain the write permission, the open itself will fail and return-1, errno is ETXTBSY.

So why can cp-f be used? Because cp-f deletes the target file (the file to be overwritten) and rename the source file as the target file name.

From the kernel code, when a binary file is run

Sys_execve ()

Do_execve ()

Open_exec ()

Deny_write_access ()

Here, deny_write_access reduces the I _writecount member of the inode file by 1. Generally, the I _writecount value is changed to-1 (initially 0)

At this time, another process wants to open in write mode:

Do_sys_open ()

Do_filp_open ()

Path_openat ()

Do_last ()

Nameidata_to_filp ()

_ Dentry_open ()

_ Get_file_write_access ()

Get_write_access ()

Get_write_access returns the-ETXTBSY

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.