Self-overwriting Scripts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PhilMorton
Forum Newbie
Posts: 3
Joined: Tue Aug 29, 2006 11:49 pm

Self-overwriting Scripts

Post by PhilMorton »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, I'm currently designing a system which has one central script (core.php) and several plugin scripts that it can load and execute. This is a command line tool, not a web-application (however there is a web-server that hosts the most up-to-date versions of the scripts to allow for auto-updating).

When a plugin is loaded, its md5 is calculated and compared with an md5 from the server. If the values dont match, a new version of the plugin is downloaded from the server and written over the old plugin.

My problem has arisen because now I need to do this same procedure for the core itself. So basically i run my script ("php.exe core.php"), and then the script has to open itself (fopen('core.php')) and overwrite itself with a newer version of the script!

Coming from a C++ background, these seems totally illogical, but its the task i have been given...

So, to test whether this is even possible, I wrote the following test program:
[b]self_update.php[/b]

Code: Select all

<?php
define( 'MESSAGE_1', 'i like the rice' );
define( 'MESSAGE_2', 'the rice is nice' );

define( "MESSAGE", MESSAGE_2 );

echo MESSAGE."\n";

$self = "./self_update.php";
$contents = @file_get_contents( $self );

$string_1 = "define( \"MESSAGE\", MESSAGE_1 );";
$string_2 = "define( \"MESSAGE\", MESSAGE_2 );";

if( strpos( $contents, $string_1 ) !== false )
	$contents = str_replace( $string_1, $string_2, $contents );
else
	$contents = str_replace( $string_2, $string_1, $contents );

$fp = @fopen( $self, 'w' );
fwrite( $fp, $contents );
fclose( $fp );
?>
I have only been able to test this on Windows. On WinXP it works, each time you run the script it outputs a different message, because it has overwritten itself with a new script.

Does anyone know if this will also work on Linux/Unix? Or will the file be locked?


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Re: Self-overwriting Scripts

Post by nickvd »

Ubuntu 6.06 (PHP 5.1.2 (cli) (built: Jul 19 2006 00:07:28)) wrote:nickvd@exile:~/test$ php self_update.php
the rice is nice
nickvd@exile:~/test$ php self_update.php
i like the rice
nickvd@exile:~/test$ php self_update.php
the rice is nice
nickvd@exile:~/test$ php self_update.php
i like the rice
nickvd@exile:~/test$ php self_update.php
the rice is nice
nickvd@exile:~/test$ php self_update.php
i like the rice
nickvd@exile:~/test$
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Post by georgeoc »

Code: Select all

george-macbook:~/Sites/test george$ php self_update.php 
the rice is nice
george-macbook:~/Sites/test george$ php self_update.php 
i like the rice
george-macbook:~/Sites/test george$ php self_update.php 
the rice is nice
george-macbook:~/Sites/test george$ php self_update.php 
i like the rice
george-macbook:~/Sites/test george$ php self_update.php 
the rice is nice
george-macbook:~/Sites/test george$ php self_update.php 
i like the rice
george-macbook:~/Sites/test george$ php self_update.php 
the rice is nice
george-macbook:~/Sites/test george$ php self_update.php 
i like the rice
PHP 4.4.1 (cli) (built: Mar 12 2006 16:47:35) on Mac OS X 10.4.7
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Coming from a C++ background, these seems totally illogical, but its the task i have been given...
Why does that seem illogical?
PhilMorton
Forum Newbie
Posts: 3
Joined: Tue Aug 29, 2006 11:49 pm

Post by PhilMorton »

Thanks for testing georgeoc and nickvd!

Weirdan - Coz theres a long established convetion that code will not be self-modifying. Compiled code is usually placed into read-only memory to enforce this convention... I guess I just assumed that the code on disk should be read only as well as the code in memory.

But seeing as this is the land of interpreted languages, i guess the whole file has been read into memory and the flie pointers closed by the time my script actually starts executing.

Oh and thanks for editing my post so that the code is in PHP tags :wink:
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Weirdan wrote:
Coming from a C++ background, these seems totally illogical, but its the task i have been given...
Why does that seem illogical?
Because it would be very difficult to do in C++...your'd have to basically write machine code (which i'm sure you know if you have the common sense to ask) or build a compiler into your application...either or...it's alot of work...

PHP it's a breeze, except for file permissions, etc...it's pretty easy...
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Coz theres a long established convetion that code will not be self-modifying. Compiled code is usually placed into read-only memory to enforce this convention... I guess I just assumed that the code on disk should be read only as well as the code in memory
Convention? I wouldn't agree...more like standard practice...but I suppose they could mean something very similar...matter of opinion...nothing more...

Compiled code is stored in ROM...wtf...only the BIOS...and even that now a days can be Flashed...

RAM is where almost all code is stored...or the HDD if the swap file is being used...for virtual RAM, etc...

Cheers :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

The script is executed from memory, not from the disk, therefore the file is not locked, and can be written to.
PhilMorton
Forum Newbie
Posts: 3
Joined: Tue Aug 29, 2006 11:49 pm

Post by PhilMorton »

Hockey wrote:
Coz theres a long established convetion that code will not be self-modifying. Compiled code is usually placed into read-only memory to enforce this convention... I guess I just assumed that the code on disk should be read only as well as the code in memory
Convention? I wouldn't agree...more like standard practice...but I suppose they could mean something very similar...matter of opinion...nothing more...

Compiled code is stored in ROM...wtf...only the BIOS...and even that now a days can be Flashed...

RAM is where almost all code is stored...or the HDD if the swap file is being used...for virtual RAM, etc...

Cheers :)
"Compiled code is usually placed into read-only memory to enforce this convention" <- when i said read-only memory, i didnt mean CMOS/EPRROM style ROM... I meant a read only section of RAM...
When most OS's load your compiled code into RAM, they protect the code segments from writing, hence it is placed into read only memory. Sorry for the confusion.
Post Reply