Storage System of Android-Vold and MountService Analysis (2), voldmountservice

Source: Internet
Author: User

Storage System of Android-Vold and MountService Analysis (2), voldmountservice

Android storage system (2)

Review:The previous post mainly analyzes the architecture and schematic diagram of the Android storage system, and briefly introduces the data transmission process between the Kernel --> Vold --> upper MountService. On this basis, let's start today's source code analysis!

[Source code analysis]

1. main Function of Vold

  Vold is also started through the init process. Its definition in init. rc is as follows:

1 service vold /system/bin/vold2     class core3     socket vold stream 0660 root mount4     ioprio be 2

The Vold service is added to the core group, which means that the system will be started by the init process when the system is started. The socket defined here is mainly used for communication between Vold and MountService on the Java layer.

The source code of the Vold module is located in system/vold. Let's take a look at the entry function main (). The Code is as follows:

1 int main () {2 VolumeManager * vm; 3 CommandListener * cl; 4 NetlinkManager * nm; 56 SLOGI ("Vold 2.1 (the revenge) firing up "); 7 8 mkdir ("/dev/block/vold", 0755); // create the vold directory 910 klog_set_level (6); 1112 if (! (Vm = VolumeManager: Instance () {// create the VolumeManager object 13 exit (1); 14}; 15 16 if (! (Nm = NetlinkManager: Instance () {// create the NetlinkManager object 17 exit (1); 18}; 1922 cl = new CommandListener (); // create a CommandListener object 23 vm-> setBroadcaster (SocketListener *) cl); // create a connection between vm and cl 24 nm-> setBroadcaster (SocketListener *) cl ); // establish contact between nm and cl 2526 if (vm-> start () {// start VolumeManager27 exit (1); 28} 2930 if (process_config (vm )) {// create a file/fstab. the Volume object 31 SLOGE ("Error reading configuration (% s) defined in xxx )... continuing anyways ", strerror (errno); 32} 3334 cryptfs_pfe_boot (); 3536 if (nm-> start () {// start NetlinkManager, the start () of NetlinkManager is called () method, it creates a PF_NETLINK socket, and enables the thread to read data from this socket 37 exit (1); 38} 3940 coldboot ("/sys/block"); // cold start, create a node file under/sys/block 4142 if (cl-> startListener () {// start listening to the Framework's socket43 exit (1); 44} 45 46 while (1) {// enter loop 47 sleep (1000); // The main thread enters sleep 48} 49
50 SLOGI ("Vold exiting"); 51 exit (0); 52}

The main function mainly creates three objects: VolumeManager, NetlinkManager, and CommandListener, and sets the CommandListener object to the VolumeManager object and NetlinkManager object respectively.

In the previous architecture diagram, we can find that the CommandListener object is used to communicate with the NativeDaemonConnector object on the Java layer through socket. Therefore, both the VolumeManager object and the NetlinkManager object must have reference to the CommandListener object.

2. Listen to the NetlinkManager object of the Vold message sent by the driver.

The main task of the NetlinkManager object is to listen to the uevent message sent by the driver.

The main () function calls the static function Instance () of the NetlinkManager class to create the NetlinkManager object. The Code is as follows:

1 NetlinkManager * NetlinkManager: Instance () {2 if (! SInstance) 3 sInstance = new NetlinkManager (); // The NetlinkManager object is referenced by the static variable sInstance, which means that there is only one NetlinkManager object in the vold process. 4 return sInstance; 5}

Take a look at the NetlinkManager constructor. The Code is as follows:

1 NetlinkManager::NetlinkManager() {2       mBroadcaster = NULL;3 }

The NetlinkManager constructor only initializes mBroadcaster. We can find that the main () function calls the setBroadcaster () function of NetlinkManager to assign a value to the variable mBroadcaster.

nm->setBroadcaster((SocketListener *) cl);

The main () function also calls the start () function of NetlinkManager. Let's take a look at the start () method in NetlinkManager. The Code is as follows:

1 int NetlinkManager: start () {2 struct sockaddr_nl nladdr; 3 int sz = 64*1024; 4 int on = 1; 5 6 memset (& nladdr, 0, sizeof (nladdr); 7 nladdr. nl_family = AF_NETLINK; 8 nladdr. nl_pid = getpid (); 9 nladdr. nl_groups = 0 xffffffff; 10/* Create a socket for asynchronous communication between the kernel space and the user space, and monitor the system's hotplug event */11 if (mSock = socket (PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT) <0) {12 SLOGE ("Unable to create uevent socket: % s", strerror (errno); 13 return-1; 14} 15/* set the buffer size to 64KB */16 if (setsockopt (mSock, SOL_SOCKET, SO_RCVBUFFORCE, & sz, sizeof (sz) <0) {17 SLOGE ("Unable to set uevent socket SO_RCVBUFFORCE option: % s", strerror (errno); 18 goto out; 19} 20/* set to allow SCM_CREDENTIALS to Control Message receipt */21 if (setsockopt (mSock, SOL_SOCKET, SO_PASSCRED, & on, sizeof (on) <0) {22 SLOGE ("Unable to set uevent socket SO_PASSCRED option: % s", strerror (errno); 23 goto out; 24} 25/* bind the socket address */26 if (bind (mSock, (struct sockaddr *) & nladdr, sizeof (nladdr) <0) {27 SLOGE ("Unable to bind uevent socket: % s", strerror (errno); 28 goto out; 29} 30/* use the newly created socket to instantiate a NetlinkHandler Class Object for listening to the socket. NetlinkHandler inherits the NetlinkListener class, netlinkListener inherits SocketListener */31 mHandler = new NetlinkHandler (mSock); 32 if (mHandler-> start () {// start NetlinkHandler and call start () of NetlinkHandler () function 33 SLOGE ("Unable to start NetlinkHandler: % s", strerror (errno); 34 goto out; 35} 36 37 return 0; 38 39 out: 40 close (mSock); 41 return-1; 42}

Let's take a look at the NetlinkManager family relationship, such:

The dotted line above shows the call process at startup:

(1) class NetlinkManager (the NetlinkHandler object is created in the start function and the socket is used as the parameter)

(2) class NetlinkHandler: public NetlinkListener (onEvent implemented)

(3) class NetlinkListener: public SocketListener (onDataAvailable)

(4) class SocketListener (runListener is implemented to check which sockets have data in a thread through select and read data by calling onDataAvailable ).

Summary:This post mainly analyzes the main () function of Vold and the source code of the NetlinkManager object. The source code shows the object creation time and function calling process. The next post will continue to start () from NetlinkHandler () in-depth analysis of the methods, continue to study the source code, and will soon meet with you. You are welcome to criticize and correct them. We will learn from each other.

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.