[FOR DEVS] Execute scripts on shutdown

Search This thread

bartito

Inactive Recognized Developer
  • Dec 1, 2005
    3,904
    1,777
    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

    .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/String;)V

    .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/String;)I

    .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/String;)Ljava/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/String;)I

    .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/String;)I
    :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/String;)V

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

    move-result-object v4

    invoke-virtual {v3, v4}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/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/String;)I

    goto :goto_0
    .end method

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

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

    (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

    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
     

    Attachments

    • shutdownd.zip
      6 KB · Views: 438
    • ShutdownThread.smali.txt
      2.5 KB · Views: 314
    • SystemScriptsExecutor-cwminstaller.zip
      166.8 KB · Views: 414
    Last edited:

    KMDonlon

    Senior Member
    Feb 22, 2011
    104
    10
    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??
     

    Attachments

    • framework.jar
      3.4 MB · Views: 13

    bartito

    Inactive Recognized Developer
  • Dec 1, 2005
    3,904
    1,777
    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
     

    Attachments

    • framework.jar
      7 MB · Views: 21

    KMDonlon

    Senior Member
    Feb 22, 2011
    104
    10
    No love :confused:...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?
     

    bartito

    Inactive Recognized Developer
  • Dec 1, 2005
    3,904
    1,777
    No love :confused:...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).
     

    bartito

    Inactive Recognized Developer
  • Dec 1, 2005
    3,904
    1,777
    your ext.jar not contains shutdown code.
    it's on framework.
    i don't known the reason of the malfunction.
    this mod works fine in emulator and samsung galaxy s2.
    sorry :(
     

    bartito

    Inactive Recognized Developer
  • Dec 1, 2005
    3,904
    1,777
    Could it be DX kernel simply does not support it? ...init.d scripts on boot work fine....I was hoping to get this working :(

    this feature not depends of the kernel.
    please, let me known if the file /system/bin/sh exists and you have
    enabled the usb depuratiin in settings - applications - development
     

    Top Liked Posts

    • There are no posts matching your filters.
    • 12
      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

      .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/String;)V

      .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/String;)I

      .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/String;)Ljava/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/String;)I

      .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/String;)I
      :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/String;)V

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

      move-result-object v4

      invoke-virtual {v3, v4}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/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/String;)I

      goto :goto_0
      .end method

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

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

      (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

      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
      1
      I will stay tuned!!

      first post updated with a standard app.
      you don't need to edit the framework.jar anymore.
      please, download the "SystemScriptsExecutor-cwminstaller.zip"
      and install in recovery mode.
      included files in zip are:
      -apk that executes the binary in boot/shutdown time
      -binary that executes the files in the init/shutdown directories (this
      binary is executed with setuid/setgid permissions) [root is not needed]
      enjoy!
      1
      This is so cool!

      Thanks a lot for sharing this :D

      May I have the permissions to use your scripts in a mod that I have made?

      of course, you can use it! :)
      1
      Quick question: Is this detecting shutdown event via BroadcastReceiver to receive Android OS shutdown events? No more based on some Shutdown*java hack right? It would really mean much better compatibility.

      Anyway, I managed to get it working on CM 10.2 (Android 4.3). Here's my workaround:
      Create a text file caleld SystemScriptsExecutor-cwminstaller.sh in your device, having following content:

      ========================================
      #!/system/bin/sh

      cd /
      mount -o rw,remount /system
      cd -

      mkdir /system/etc/osinit.d
      chmod 775 /system/etc/osinit.d
      mkdir /system/etc/osshut.d
      chmod 775 /system/etc/osshut.d

      cd /system/bin
      chmod 06755 SystemScriptsExecutor 2>/dev/null
      echo '#!/system/bin/sh' > SystemScriptsExecutor
      echo '' >> SystemScriptsExecutor
      echo '# Trigger prompt for superuser access always.' >> SystemScriptsExecutor
      echo 'su -c true' >> SystemScriptsExecutor
      echo '' >> SystemScriptsExecutor
      echo 'if test $1 = INITIALIZATION ; then' >> SystemScriptsExecutor
      echo ' if test -d /system/etc/osinit.d ; then' >> SystemScriptsExecutor
      echo ' su -c /system/etc/osinit.d/*' >> SystemScriptsExecutor
      echo ' fi;' >> SystemScriptsExecutor
      echo 'fi;' >> SystemScriptsExecutor
      echo '' >> SystemScriptsExecutor
      echo 'if test $1 = SHUTDOWN ; then' >> SystemScriptsExecutor
      echo ' if test -d /system/etc/osshut.d ; then' >> SystemScriptsExecutor
      echo ' su -c /system/etc/osshut.d/*' >> SystemScriptsExecutor
      echo ' fi;' >> SystemScriptsExecutor
      echo 'fi;' >> SystemScriptsExecutor
      echo '' >> SystemScriptsExecutor
      chmod 06755 SystemScriptsExecutor
      cd -

      cd /
      mount -o ro,remount /system
      cd -
      ========================================

      . Install SystemScriptsExecutor-cwminstaller.zip as per included instruction.
      . Ensure nothing is using the /system folder throughout.
      . Inside device, as root, invoke SystemScriptsExecutor-cwminstaller.sh.
      ... Init & shutdown script folders are now:
      ..... /system/etc/osinit.d
      ..... /system/etc/osshut.d
      . In your super user manager (Superuser or SuperSU), configure to auto allow su/root access.
      . Shutdown device. SystemScriptsExecutor.apk app will be invoked upon shutdown & super user manager will notify that root access has been auto provided.
      . Boot up the device.
      . In your super user manager (Superuser or SuperSU), configure to its previous state.
      . Place scripts as desired into below folders. Do mount -o rw,remount /system as necessary.
      ... /system/etc/osinit.d
      ... /system/etc/osshut.d

      Hope this helps. SystemScriptsExecutor-cwminstaller.zip is good stuff.

      kh
      1
      Can someone fix this for 4.4? I've noticed the changes need to be done to services.jar instead to framework.jar bc ShutdownThread is moved. I tried to decompile services, modify (paying atention to the run function to reflect the correct address) and recompile but i have errors :(
      The apk doesn't work for me, i don't know why.
      Any ideas?
      Is the source code available for the apk? thanks!
    Our Apps
    Get our official app!
    The best way to access XDA on your phone
    Nav Gestures
    Add swipe gestures to any Android
    One Handed Mode
    Eases uses one hand with your phone