Android Init process-uevent

Source: Internet
Author: User

Reprint!

Init is the first process initiated by Linux kernel, and understanding of init is important for familiarity with the Android system.

Each Android directory has a very important file android.mk, which is responsible for compiling the code below the directory.

System/core/init/android.mk

[CPP]View Plaincopy
  1. <span style="FONT-SIZE:18PX;" > </span><span style="FONT-SIZE:18PX;" ><span style="color: #ff0000;" >local_module:= Init
  2. </span>
  3. Local_force_static_executable: = true
  4. Local_module_path: = $ (target_root_out)
  5. <span style="color: #ff0000;" >include $ (build_executable</span>)
  6. Symlinks: = $ (target_root_out)/sbin/ueventd
  7. $ (symlinks): init_binary: = $ (Local_module)
  8. $ (symlinks): $ (local_installed_module) $ (local_path)/android.mk
  9. @echo "Symlink: [email protected]. /$ (init_binary) "
  10. @mkdir-P $ (dir [email protected])
  11. @rm-RF [email protected]
  12. </span><span style="Font-size:18px;color: #ff0000;" >$ (hide) ln-sf. /$ (init_binary) [email protected]
  13. </span>


The code above generates an executable called Init, which will be placed in/below, and will produce a symbolic link/sbin/eventd, pointing to/init. We can't help but ask, why do you do this?

Init is a script interpreter that parses two files under the target system,

/init.rc

/init.xxx.rc (XXX stands for Platform platform name)

First look at the source code directory/device/xxx/init.rc

[CPP]View Plaincopy
    1. <span style="FONT-SIZE:18PX;" > On Early-init
    2. Start Ueventd
    3. </span>

It seems that Init started its own process when parsing the script, but the process name became UEVENTD.

System/core/init/init.c/main

[CPP]View Plaincopy
    1. <span style="FONT-SIZE:18PX;" > if (!strcmp (basename (argv[0]), "Ueventd"))
    2. return Ueventd_main (argc, argv);
    3. </span>

The program execution path differs depending on the process name. Ueventd as the name implies should be to receive uvent daemon, here its main role is to create or delete/dev/xxx (XXX device name) under Uevent, we know that under Linux to create a Device node interface Mknod, Let's go in and see where this interface is called.

System/core/init/ueventd.c/ueventd_main

[CPP]View Plaincopy
    1. <span style="FONT-SIZE:18PX;"  >ueventd_parse_config_file ("/ueventd.rc");
    2. SNPRINTF (TMP, sizeof (TMP), "/ueventd.%s.rc", hardware);
    3. Ueventd_parse_config_file (TMP);
    4. Device_init ();
    5. </span>


UEVENTD has two scripts to parse, ueventd.rc,ueventd.xxx.rc, script, and script this script allows the client to set permissions for the/dev or/sys directories and subdirectories.

System/core/rootdir/ueventd.rc

[CPP]View Plaincopy
    1. <span style="FONT-SIZE:18PX;" >/dev/binder 0666 root root</span>

Note here that Ueventd_parse_config_file does not create a device node, it is intended to provide a database, and when a device node is generated, EVENTD will refer to this database to set the device node's permissions.

System/core/init/devices.c/device_init

[CPP]View Plaincopy
    1. <span style="FONT-SIZE:18PX;"  > device_fd = Open_uevent_socket ();
    2. Coldboot ("/sys/class");
    3. Coldboot ("/sys/block");
    4. Coldboot ("/sys/devices");
    5. </span>

This function is very simple, the main is to create a uevent socket handle, while triggering/sys/clas,/sys/block,/sys/devices these three directories and their subdirectories uevent, and then accept and create the device node, At this point the device node is created, coldboot inside there is a very interesting function do_coldboot, this is a recursive call function, the implementation is very interesting, we can look at.

System/core/init/ueventd.c/ueventd_main

[CPP]View Plaincopy
  1. <span style="FONT-SIZE:18PX;" > While (1) {
  2. ufd.revents = 0;
  3. NR = Poll (&UFD, 1,-1);
  4. if (nr <= 0)
  5. continue;
  6. if (ufd.revents = = Pollin)
  7. HANDLE_DEVICE_FD ();
  8. }
  9. </span>


The dead loop, accepting kernel uevent, dynamically create or delete nodes.

HANDLE_DEVICE_FD will eventually call Mknod to create the device node, as follows:

handle_device_fd-> handle_device_event-> make_device-> Mknod

Android Init process-uevent

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.