How to Make Android or android applications execute shell scripts

Source: Internet
Author: User
1. How to write the script for the Android app to start the service and execute the script?/device/tegatech/tegav2/init in the root directory of the android source code. the RC file is believed to be familiar to everyone (if you do not understand it, carefully study the android startup process ). If the following services are added to the script file:

Service usblp_test/data/setip/init. usblpmod. Sh

Oneshot

Disabled

Note: Each device has its own init. RC, init. device name. RC Script file. Oneshot disabled tells us that this service will not be automatically started when the system starts. The purpose of this service is to execute the/data/setip/init. usblpmod. Sh script. You can write the script content as long as it complies with the shell syntax. For example, the script can be simply set eth0:

#! The start of/system/bin/sh // script must be written in this way.

Ifconfig eth0 172.16.100.206 netmask 255.255.0.0 up // set the IP address command

2. How to start the service in the Application

1) first understand the process of starting a service.

1. Start the service added in init. RC in your application.

First, understand the process of starting the service:


Init. c In the Device directory (remember it is not system/CORE/init. Rc)

The for (;) loop of the main function has a handle_property_set_fd (), function:

      for (i = 0; i < fd_count; i++) {            if (ufds[i].revents == POLLIN) {                if (ufds[i].fd == get_property_set_fd())                    handle_property_set_fd();                else if (ufds[i].fd == get_keychord_fd())                    handle_keychord();                else if (ufds[i].fd == get_signal_fd())                    handle_signal();            }        }

 

The implementation of this function is also under the system/CORE/init directory, in which check_control_perms (MSG. value, Cr. UID, Cr. GID) function is to check whether the UID has the permission to start the service (MSG. value is the name of your service). If the application is root or system, 1 is returned directly. then handle_control_message (char *) is called *)
MSG. Name + 4, (char *) msg. value), the parameter of this function is to remove start and 2 after 1. CTL. Your service name. Details of this function:

void handle_control_message(const char *msg, const char *arg){    if (!strcmp(msg,"start")) {        msg_start(arg);    } else if (!strcmp(msg,"stop")) {        msg_stop(arg);    } else if (!strcmp(msg,"restart")) {        msg_stop(arg);        msg_start(arg);    } else {        ERROR("unknown control msg '%s'\n", msg);    }}


 

Call msg_start after matching start. the service is like this. Our solution is to "Work hard" in the permission check area. Because we are not sure about the UID, let the check_control_perms function not check our uid, check the name of our service and see the function:

static int check_control_perms(const char *name, unsigned int uid, unsigned int gid) {    int i;    if (uid == AID_SYSTEM || uid == AID_ROOT)        return 1;    /* Search the ACL */    for (i = 0; control_perms[i].service; i++) {        if (strcmp(control_perms[i].service, name) == 0) {            if ((uid && control_perms[i].uid == uid) ||                (gid && control_perms[i].gid == gid)) {                return 1;            }        }    }    return 0;}

The UID must be checked in this function. We only need to write it on the for loop.

If (strcmp ("usblp_test", name) = 0) // usblp_test is the name of our service.

Return 1;

This will not damage the original structure of Android and will not cause any side effects.

Init. C and init. RC have been modified. Now you can compile the source code and install it on the sub-Development Board.

2. Start the service in the Application

Call do_exec (start usblp_test) in the application );

The implementation of do_exec is as follows:

 private  String do_exec(String cmd) {          String s = "/n";          try {              Process p = Runtime.getRuntime().exec(cmd);              BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));              String line = null;              while ((line = in.readLine()) != null) {                  s += line + "/n";                             }          } catch (IOException e) {              // TODO Auto-generated catch block               e.printStackTrace();          }          text.setText(s);          return cmd;           }  

Application Source: http://download.csdn.net/detail/weijing331/4878065

2. Compile the source code and execute the shell script

This method is very simple. The script has been written. The problem to be solved now is how to execute the script. After verification, the best position is in system/CORE/init. c, main function in the following position,

Queue_builtin_action (queue_property_triggers_action, "queue_propety_triggers ");

# If bootchart

Queue_builtin_action (bootchart_init_action, "bootchart_init ");

# Endif

/* Add By weijing */

System ("exec/system/bin/sh/data/setip/init. djstava. Sh ");

/* End by weijing */

For (;;){

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.