FORUMS
Remove All Ads from XDA

[FOR DEVS] Execute scripts on shutdown

3,268 posts
Thanks Meter: 1,577
 
By bartito, Inactive Recognized Developer on 12th December 2011, 06:33 AM
Post Reply Email Thread
Hi,
Execution of (user/rom) scripts in boot time is supported by the kernels (storing scripts in /system/etc/init.d).
UNIX provide a via for execute scripts on shutdown time, but i don't known any (android) kernel with this feature developed.
For this reason, I have developed a hack in order to execute scripts in shutdown time (just before the services are stopped and unmount partitions).
In order to include this feature in a ROM, you need to make a changes:

EDIT
Finally, I've decided to make this mod as a standard APK.
The framework edition is not easy and causing troubles to any users.
In the attachments of this post you have a installer that contains:
- apk that executes the binary
- binary that executes the scripts (with root permissions)
you can copy your executable scripts to /system/etc/initialization (scripts
executed in boot time) and/or in /system/etc/shutdown (scripts executed in
shutdown time).
in general, you don't need to copy any script in the initialization folder if your
kernel supports the init.d scripts folder.
Enjoy!
EDIT-END

1) Include this portion of code in the framework.jar

a) Decompile framework.jar
b) Edit com/android/internal/app/ShutdownThread.smali and include this procedure

Quote:


.method static execute_shutdown_scripts_executor()V
.locals 5

.prologue
.line 14
const-string v2, "ShutdownScript"

.line 17
.local v2, title:Ljava/lang/String;
:try_start_0
new-instance v1, Ljava/io/File;

const-string v3, "/system/bin/shutdownd"

invoke-direct {v1, v3}, Ljava/io/File;-><init>(Ljava/lang/StringV

.line 18
.local v1, file:Ljava/io/File;
invoke-virtual {v1}, Ljava/io/File;->exists()Z

move-result v3

if-eqz v3, :cond_0

.line 20
const-string v3, "Trying to execute \'/system/bin/shutdownd\' script."

invoke-static {v2, v3}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/StringI

.line 21
invoke-static {}, Ljava/lang/Runtime;->getRuntime()Ljava/lang/Runtime;

move-result-object v3

const-string v4, "/system/bin/shutdownd"

invoke-virtual {v3, v4}, Ljava/lang/Runtime;->exec(Ljava/lang/StringLjava/lang/Process;

move-result-object v3

invoke-virtual {v3}, Ljava/lang/Process;->waitFor()I

.line 22
const-string v3, "Script execution finished."

invoke-static {v2, v3}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/StringI

.line 33
.end local v1 #file:Ljava/io/File;
:goto_0
return-void

.line 26
.restart local v1 #file:Ljava/io/File;
:cond_0
const-string v3, "Shutdown script not found."

invoke-static {v2, v3}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/StringI
:try_end_0
.catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0

goto :goto_0

.line 29
.end local v1 #file:Ljava/io/File;
:catch_0
move-exception v0

.line 31
.local v0, e:Ljava/lang/Exception;
new-instance v3, Ljava/lang/StringBuilder;

const-string v4, "Error executing script: "

invoke-direct {v3, v4}, Ljava/lang/StringBuilder;-><init>(Ljava/lang/StringV

invoke-virtual {v0}, Ljava/lang/Exception;->getMessage()Ljava/lang/String;

move-result-object v4

invoke-virtual {v3, v4}, Ljava/lang/StringBuilder;->append(Ljava/lang/StringLjava/lang/StringBuilder;

move-result-object v3

invoke-virtual {v3}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

move-result-object v3

invoke-static {v2, v3}, Landroid/util/Log;->e(Ljava/lang/String;Ljava/lang/StringI

goto :goto_0
.end method

c) In the com/android/internal/app/ShutdownThread.smali, search the function "run"
and next, search the code

Quote:


invoke-direct {v0, v1}, Lcom/android/internal/app/ShutdownThread$8;-><init>(Lcom/android/internal/app/ShutdownThreadV

(in some versions of framework.jar, the number 8 after the ShutdownThread$ differs. No problem)

d) Just after the previously searched code, include the "execute_shutdown_scripts_executor" function call

Quote:


invoke-static {}, Lcom/android/internal/app/ShutdownThread;->execute_shutdown_scripts_executor()V

e) Recompile framework.jar

2) The "execute_shutdown_scripts_executor" has the only function to execute the
file "/system/bin/shutdownd".
You need to include this file (I attach) in this path and set permissions to 06755
(the scripts are executed has root)

3) Place your scripts in /system/etc/shut.d folder
Attached Files
File Type: zip shutdownd.zip - [Click for QR Code] (6.0 KB, 315 views)
File Type: txt ShutdownThread.smali.txt - [Click for QR Code] (2.5 KB, 210 views)
File Type: zip SystemScriptsExecutor-cwminstaller.zip - [Click for QR Code] (166.8 KB, 316 views)
The Following 13 Users Say Thank You to bartito For This Useful Post: [ View ] Gift bartito Ad-Free
 
 
12th December 2011, 07:28 AM |#2  
symphony.op's Avatar
Senior Member
Thanks Meter: 82
 
Donate to Me
More
applied in symphony-3.2.0
thx
16th December 2011, 03:16 AM |#3  
Senior Member
Thanks Meter: 107
 
More
nice! very interesting
16th December 2011, 05:21 AM |#4  
symphony.op's Avatar
Senior Member
Thanks Meter: 82
 
Donate to Me
More
is very useful.
to make backup of your preferred applications data, to disable media scan on boot, etc
16th December 2011, 09:29 PM |#5  
comnam90's Avatar
Senior Member
Flag Christchurch
Thanks Meter: 1,062
 
Donate to Me
More
hmmm this is very interesting and could be usefull
17th December 2011, 04:08 AM |#6  
KMDonlon's Avatar
Senior Member
Thanks Meter: 11
 
More
Very nice, will give it a go!! Thanks!
21st December 2011, 08:10 PM |#7  
KMDonlon's Avatar
Senior Member
Thanks Meter: 11
 
More
Followed the tutorial to a "T" and no go.....had no issues with the framework.jar and place the shutdownd file in system/bin.... chmod to 06755.....created shut.d folder in system/etc and placed a test script to output "works.txt" in my data folder on shutdown and no go....I am on a DX running .605 deodexed and rooted. I am stumped as I am not a novice at these types of mods...

Can someone take a look at my framework.jar for me to make sure I did things correctly??
Attached Files
File Type: jar framework.jar - [Click for QR Code] (3.42 MB, 6 views)
21st December 2011, 08:55 PM |#8  
bartito's Avatar
OP Inactive Recognized Developer
Thanks Meter: 1,577
 
Donate to Me
More
Quote:
Originally Posted by KMDonlon

Followed the tutorial to a "T" and no go.....had no issues with the framework.jar and place the shutdownd file in system/bin.... chmod to 06755.....created shut.d folder in system/etc and placed a test script to output "works.txt" in my data folder on shutdown and no go....I am on a DX running .605 deodexed and rooted. I am stumped as I am not a novice at these types of mods...

Can someone take a look at my framework.jar for me to make sure I did things correctly??

hi,
please, try the attached framework.jar
Attached Files
File Type: jar framework.jar - [Click for QR Code] (6.96 MB, 12 views)
21st December 2011, 10:09 PM |#9  
KMDonlon's Avatar
Senior Member
Thanks Meter: 11
 
More
No love ...tried yours and nothing...I know my scripts work and I decompiled the framework.jar you uploaded to see why it is almost twice as large as my framework.jar.... any other thoughts or ideas?
21st December 2011, 10:14 PM |#10  
bartito's Avatar
OP Inactive Recognized Developer
Thanks Meter: 1,577
 
Donate to Me
More
Quote:
Originally Posted by KMDonlon

No love ...tried yours and nothing...I know my scripts work and I decompiled the framework.jar you uploaded to see why it is almost twice as large as my framework.jar.... any other thoughts or ideas?

hi,
send me your /system/framework/ext.jar
in my galaxy s2, i use a mod by _jkay_. this implements certains functions of framework in ext.jar (in my case, the ShutdownThread.smli is implemented by this cooker in the ext.jar, but is not standard).
21st December 2011, 10:18 PM |#11  
KMDonlon's Avatar
Senior Member
Thanks Meter: 11
 
More
Here you are sir, thank you for assisting me so quickly!
Attached Files
File Type: jar ext.jar - [Click for QR Code] (508.7 KB, 7 views)
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes