Text file is busy linux

Write to own executable in Linux C program, error «Text file busy»

My problem is that the only way I have been able to open the executable is in read-only mode «rb» . If I try to open the file for writing in mode «wb» or «r+b» I get back the error «Text file busy» . Is there anyway for me to write to a process’s own executable in C? Can I do this by changing the permissions somehow?

EDIT: What I am trying to accomplish is to have an executable which will encrypt part of itself each time that it runs so that it will have a new checksum after every time it runs.

After reading data from the executable binary, how can I either write back to it or remove it and replace it with a new file with the same filename?

Try copying yourself (in read only mode), executing the copy, then deleting the original from the copy.

2 Answers 2

You cannot write to a file that is currently mapped as an executable. However, you can write to a file that has the same path as the current executable, so long as it isn’t actually the same file — try unlinking the file you’re being executed from and creating a new file in its place, for instance.

Can you elaborate a little bit? By the same path do you mean a new file created in the same directory as the current executable, but with a different file name? By unlink do you mean using this unlink function?

Thanks, I was able to do what I needed with with the unlink function. I first open the executable binary in mode «rb» , read in the data, close the file, unlink the file, open a new file to be written at the same path in mode «wb» , write the modified binary data, close the file, then I use chmod to mark the new file as executable.

In order to do self modification too, I wrote a small code in nasm (which can be used as a stub), which opens itself and at the middle of the code (right after the mmap), we have a pointer which points to the bytes of the executable that we can modify.

BITS 64 section .text global _start _start: call _main__ mov rax, 60 mov rdi, 0x0 syscall ; exit(0); _main__: push rbp mov rbp, rsp sub rsp, 144 ; stat_file mov rdi, [rbp+0x18] lea rsi, [rsp] call _open_self ; open self push r12 ; len file push rax ; addr mov r14, rsi mov rdi, [rbp+0x18] ; pathname pop rsi ; addr pop rdx ; len push rdx push rsi call __create mov r13, rax ; second fd mov rdi, r14 ; fd pop rsi ; addr -> mmap pop rdx ; len_file call __close_unmap mov rax, 87 mov rdi, [rbp+0x18] syscall mov rax, 0x3 ; close(scnd_fd); mov rdi, r13 syscall mov rax, 86 push 'nasm' lea rdi, [rsp] mov rsi, [rbp+0x18] syscall ; link tmp name to original name mov rax, 87 lea rdi, [rsp] syscall ; delete old tmp file leave ret ; =============================== ; Open himself _open_self: push rbp mov rbp, rsp mov r15, rsi ; &stat_file mov r12, rdi ; *pathname mov rax, 0x2 mov rsi, 0x0 ; 0_RD mov rdx, 509 syscall push rax ; fd mov rdi, rax ; fd mov rsi, r15 ; struct stat mov rax, 5 ; fstat syscall xor rdi, rdi mov rsi, qword [r15+48] mov rdx, 0x4 mov r10, 0x2 pop r8 push r8 mov r9, 0x0 mov rax, 9 syscall ; mmap ; rax -> byte of the executable that we gonna dump mov r12, qword [r15+48] pop rsi ; fd leave ret ; =============================== ; int __create(const char *pathname, void *addr, ssize_t len_bytes_mapped); __create: push rbp mov rbp, rsp push rsi ; addr push rcx ; len push 'nasm' lea rdi, [rsp] mov rax, 0x2 mov rsi, 0x42 ; 0_CREAT | O_RDWR mov rdx, 509 syscall ; sys_open add rsp, 0x8 ; 'nasm' mov r9, rax ; fd mov rdi, rax ; fd mov rax, 0x1 pop rdx pop rsi syscall ; sys_write mov rax, r9 ; fd final leave ret ; int __close_unmap(int fd, unsigned lon addr, ssize_t len_file); __close_unmap: push rbp mov rbp, rsp push rdi mov rdi, rsi mov rsi, rdx mov rax, 11 syscall ; munmap(addr, len_file) pop rdi mov rax, 3 syscall ; close(fd); leave ret 

It is a bit long but it makes just :

Читайте также:  Узнать количество открытых файлов linux

-Open it self in read mode (O_RD == 0x0)

-And then a mmap(0, buffer_struct_stat.st_size, 0x4, MAP_PRIVATE, fd_read_only, 0);

-Here you can edit your executable by editing the bytes at the address returned by mmap

-Create a tmp file named «nasm»

-Do a write(fd_tmp, address_of_mmap, buffer_struct_stat.st_size)

-Close the two file descriptors and munmap the mmap

-Now it’s cool : unlink(pathname) and link(«nasm», «pathname»)

Источник

Text file busy when I copy some files

Text file busy is shown because some other process is accessing it. lsof will show you what’s accessing the file.

cp -f is not safe as it may result in removed/missing files when you are done. Better use star with the option -install at the extract side as this will first unpack the new file under a random name and then finally replace the old file by the new one by calling the atomic rename() . This also applies if you are using the original AT&T cpio by default or when star emulates cpio, but not to the GNU cpio clone.

2 Answers 2

I have seen this happen to me when I was copying files from one hard drive to another through a connection such as NFS or an SSH tunnel.

What happens is that the file being copied becomes part of the destination directory. That means the destination directory needs to be locked, updated with the new information, and then unlocked.

If the next file (which in your case would be libtiny.a ) arrives too soon, it tries to lock the directory and fails with the «File busy» error. That then prevents the copy of that file and anything further.

Since libtiny.a is a static library, there are no reasons why it would ever be locked against a copy. As far as I know, the compiler does not lock the files it is working on, and really it would only happen if you were compiling something in the target directories.

Now this is assuming that all the disks use a normal file system. If you used NTFS, then files cannot be replaced while opened because that system does not allow for such to happen.

Читайте также:  Атол драйвер для линукс

Under Linux, opening a file locks that file’s data in place, but it does not prevent you from unlinking it, renaming it, replacing it. If the file was deleted, the locked data will be released from the hard drive once all the handles to that file get closed.

This means you can write a program which, when it gets executed, deletes itself from the hard drive and yet it will continue to function as if nothing had happened.

Источник

What generates the «text file busy» message in Unix?

What operation generates the error «text file busy»? I am unable to tell exactly. I think it is related to the fact that I’m creating a temporary python script (using tempfile) and using execl from it, but I think that execl changes the file being run.

15 Answers 15

This error means you are trying to modify an executable while it is executing. The «Text» here refers to the fact that the file being modified is the text segment for a running program. Use lsof to check what other processes are using it. You can use kill command to kill it if needed.

The Text file busy error in specific is about trying to modify an executable while it is executing. The «Text» here refers to the fact that the file being modified is the text segment for a running program. This is a very special case, and not the generic one that your answer seems to suggest. Even so, your answer isn’t entirely incorrect.

I think the fact that unix assumes files are «text files» is ilogical, in my case it was a binary file which prompted this error.

@FelipeValdes The name is historical from the terminology of a half century ago. For example, in multics the text segment of a program was distinct from the link segment, and even earlier people talked about binary text. stackoverflow.com/a/1282540/833300

It’s a while since I’ve seen that message, but it used to be prevalent in System V R3 or thereabouts a good couple of decades ago. Back then, it meant that you could not change a program executable while it was running.

For example, I was building a make workalike called rmk , and after a while it was self-maintaining. I would run the development version and have it build a new version. To get it to work, it was necessary to use the workaround:

gcc -g -Wall -o rmk1 main.o -L. -lrmk -L/Users/jleffler/lib/64 -ljl if [ -f rmk ] ; then mv rmk rmk2 ; else true; fi ; mv rmk1 rmk 

So, to avoid problems with the ‘text file busy’, the build created a new file rmk1 , then moved the old rmk to rmk2 (rename wasn’t a problem; unlink was), and then moved the newly built rmk1 to rmk .

I haven’t seen the error on a modern system in quite a while. but I don’t all that often have programs rebuilding themselves.

Источник

/usr/bin/perl: bad interpreter: Text file busy

There were a couple of disk-intensive processes running at the time, but I’ve never seen that message before—in fact, this is the first time that I can remember getting an error when trying to run a Perl script. After a few seconds of waiting, I was able to run it, and haven’t seen the issue since, but it would be nice to have an explanation for this. Running Ubuntu 9.04, file system is ext3.

Читайте также:  How to archive folder linux

6 Answers 6

The Linux kernel will generate a bad interpreter: Text file busy error if your Perl script (or any other kind of script) is open for writing when you try to execute it.

You don’t say what the disk-intensive processes were doing. Is it possible one of them had the script open for read+write access (even if it wasn’t actually writing anything)?

That sounds reasonable — I was working on the script, and this happened just as I saved it and then tried running it.

So probably the «disk-intensive process» didn’t do anything to your script directly, but it kept the disk busy enough that your stalled while writing out the script and kept it open for several seconds.

This can mean that either the script is being written to, or that the interpreter itself is being written to — the same error happens in either case.

Thanks. A coworker had the same problem. It turned out that the graphical FTP client he used for uploading the scripts to the server was still had a grasp on the files. Once I closed the FTP conection the I was able to run the scripts.

This happens because the script file is open for writing, possibly by a rogue process which has not terminated.

Solution: Check what process is still accessing the file, and terminate it.

# /root/wordpress_plugin_updater/updater.pl --wp-path=/var/www/virtual/joel.co.in/drjoel.in/htdocs -bash: /root/wordpress_plugin_updater/updater.pl: /root/perl/bin/perl: bad interpreter: Text file busy 

Run lsof (list open files command) on the script name:

# lsof | grep updater.pl sftp-serv 4416 root 3r REG 144,103 11043 33046751 /root/wordpress_plugin_updater/updater.pl 

Kill the process by its PID:

Now try running the script again. It works now.

# /root/wordpress_plugin_updater/updater.pl --wp-path=/www/htdocs WordPress Plugin Updater script v3.0.1.0. Processing 24 plugins from 

Источник

How do you deliberately trigger a «text file busy» error?

Very occasionally I encounter «text file busy» error while using my Linux 4.0.4 computer. I read that when the error happens, kernel is preventing modification to a file that is being «used». So I make sure a file is open (as listed in lsof) and then make modifications to it, everything is fine and «text file busy» error does not happen. So how do you trigger the an error?

1 Answer 1

«Text file busy» means a process is trying to modify an executable while it’s running («text» is about a .text segment, not a text file). To trigger it:

$ cp /usr/bin/yes . $ ./yes >/dev/null & [1] 27417 $ cat /dev/null >yes -bash: yes: Text file busy $ kill %1 [1]+ Terminated ./yes > /dev/null $ cat /dev/null >yes $ ls -s yes 0 yes 

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector