LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2012, 09:34 PM   #1
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Rep: Reputation: Disabled
Remove the execute file when it executes!


Question 1:
I write a c program which just only has a while infinite loop, while it executes, in another shell login, I remove the executable file. The linux OS doesn't complain anything, and the executable file continues executing. In windows box, the OS wouldn't let you remove the executing files at all.
So can some guru explain how does Linux work in such situation?

Question 2:
Before, when I update a program, I use the steps below:
1. Change the code 2.Stop the executing program 3.compile and overwrite the executing program 4.start the program

According to the test of Question 1, Can I do as follows? Is it always safe?
1.Change the code 2.compile and overwrite the executing program 3.Stop the executing program 4.start the program
Thanks in advance!

Last edited by fantasy1215; 06-07-2012 at 09:37 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 06-07-2012, 10:23 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

1. The running program has already been loaded into ram so it makes no difference if the file is removed from the disk.
2. a) Yes you can.
b) Not sure, but I guess so.

HTH,

Evo2.
 
Old 06-08-2012, 03:21 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: not every Unix supports overwriting running program-files. The correct way is delete+recreate. (Files in use aren't actually deleted when you call unlink(1), only when no more process uses them. (Directory entries, on the other hand, are deleted by unlink(1) immediately.))
 
Old 06-08-2012, 09:44 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by fantasy1215 View Post
Question 1:
In Linux, Mac OS X, and many other Unix-like systems, filesystems use inodes. Basically, the name and the actual file are separate entities. When you delete a file, the kernel does not immediately delete the file if some process has it open, it just marks the inode deleted. Only when the last handle to the inode is closed, is the inode actually deleted.

All inodes the kernel is currently executing are marked busy. You won't be able to modify such files, but you can remove them. Because of the mechanism I described above, removing the file causes the file to become inaccessible, but will not release the disk space until the file is no longer being executed (and has no open handles to it).

Script files can be edited, because they're not executed by the kernel, but by a script interpreter.

In Linux and Unix, it is a very common, traditional technique to create a temporary file, then delete it immediately, but not close it. You can still read and write to the file normally, truncate or extend it, but as soon as the program exits, the file contents will vanish too. You can even give other processes access to the file in Linux by using name /proc/PID/fd/DESCRIPTOR where PID is the process ID of the creator process, and DESCRIPTOR is the file descriptor number (the handle to the open file).

Quote:
Originally Posted by fantasy1215 View Post
3.compile and overwrite the executing program 4.
Nope. You do not overwrite the file, not really: the file gets replaced. The data in the file does not get overwritten, it is replaced by a new file.

Quote:
Originally Posted by fantasy1215 View Post
Can I do as follows? Is it always safe?
1.Change the code 2.compile and overwrite the executing program 3.Stop the executing program 4.start the program
Yes, it is always safe in Linux, and in all Unix-like systems I've ever used.
 
3 members found this post helpful.
Old 06-08-2012, 12:17 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: in AIX, if I try to link an already running executable, 'ld' will give an error message (ETEXTBUSY), then deletes the executable, so the second try will work... The correct solution: remove the existing file first, then perform linking.
 
Old 06-08-2012, 12:32 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
When a program is executing, portions of the executable file are often memory-mapped into the process address space. Even if the directory-entry pointing to the inode can be removed, the inode itself probably cannot. Generally speaking, you can't in any operating system succeed in causing a program to remove itself from disk.
 
Old 06-08-2012, 04:37 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Nominal Animal View Post
All inodes the kernel is currently executing are marked busy. You won't be able to modify such files, but you can remove them. Because of the mechanism I described above, removing the file causes the file to become inaccessible, but will not release the disk space until the file is no longer being executed (and has no open handles to it).
You can access it via procfs, actually. But there's no "normal" way to access it once it's been deleted.
Kevin Barry
 
Old 06-08-2012, 07:20 PM   #8
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by ta0kira View Post
You can access it via procfs, actually. But there's no "normal" way to access it once it's been deleted.
Good point.

/proc/PID/exe is actually quite an interesting feature. When the binary is deleted, the symlink will show up as /path/to/binary (deleted). However, when opened, the kernel returns a handle to the original inode instead. Many of the files in /proc/ behave this way in Linux, particularly the /proc/PID/fd/DESCRIPTOR ones: they look like symlinks, but provide access to process (or kernel) internals.

FreeBSD no longer provides procfs by default, so anything involving /proc/ should probably be considered more or less Linux-specific.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc will not execute, responds with 'no such file or directory' and 'no input file' nckeecho Ubuntu 9 07-24-2016 01:04 PM
not able to execute file in external hard disk of NTFS file system seismo18 Linux - General 7 01-16-2012 07:43 AM
Can I remove read/write/execute permeation from directories in '/' for 'others' ? peter1234 Linux - Security 3 05-04-2010 06:06 AM
how to execute a script file? Have file/directory not found error sirius57 Linux - Software 2 11-21-2007 11:43 PM
Add/remove programs that execute on bootup c_olin3404 Debian 1 09-07-2005 02:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration