In the previous android--4.2 Vold Mount Management _ Main building main (a) has a structure diagram representation, Vold is kernel and the user layer of an interactive management module,
android--4.2 Vold Mount Management _volumemanager (iii) A brief introduction to the construction of the core Volumemanager, the key to the analysis from the kernel process to the Volumemanager process: Netlinkmanager
Writing is not easy, reproduced please specify the source: http://blog.csdn.net/jscese/article/details/38586021
One: Netlinkmanager construction
Still from the main in/system/vold/main.cpp:
if (! ( NM = Netlinkmanager::instance ())) { Sloge ("Unable to create Netlinkmanager"); Exit (1); }; ... if (Nm->start ()) { Sloge ("Unable to start Netlinkmanager (%s)", Strerror (errno)); Exit (1); }
Constructors don't do anything, the main build starts with this start function
In/system/vold/netlinkmanager.cpp:
int Netlinkmanager::start () {struct SOCKADDR_NL nladdr;//uses the socket structure to communicate with kernel process int SZ = 64 * 1024; int on = 1; memset (&nladdr, 0, sizeof (NLADDR));//Initialize nladdr.nl_family = Af_netlink; Nladdr.nl_pid = Getpid (); Nladdr.nl_groups = 0xFFFFFFFF; if (Msock = socket (pf_netlink,//creation type Pf_netlink sock_dgram,netlink_kobject_uevent) < 0) { Sloge ("Unable to create uevent socket:%s", Strerror (errno)); return-1; } if (setsockopt (Msock, Sol_socket, So_rcvbufforce, &sz, sizeof (SZ)) < 0) {//config size sloge ("Unable to set UE Vent socket so_recbufforce Option:%s ", Strerror (errno)); return-1; } if (setsockopt (Msock, Sol_socket, so_passcred, &on, sizeof (ON)) < 0) {Sloge ("Unable to set uevent sock Et so_passcred option:%s ", Strerror (errno)); return-1; } if (Bind (Msock, (struct sockaddr *) &nladdr, sizeof (NLADDR)) < 0) {//Bind socket address Sloge ("Unable to bind uevent Socket:%s ", Strerror (errno)); return-1; } Mhandler = new Netlinkhandler (msock);//The identity of the socket being created constructs a Netlinkhandler instance if (Mhandler->start ()) {//Open Socket Monitor Sloge ("Unable to start Netlinkhandler:%s", Strerror (errno)); return-1; } return 0;}
The NetLink socket is used here, and the netlink socket is a special interprocess communication (IPC) used to communicate the user process to the kernel process, and is the most commonly used interface for network applications to communicate with the kernel, with a structure definition:
struct SOCKADDR_NL {__kernel_sa_family_tnl_family;/* af_netlink*/unsigned shortnl_pad;/* zero*/__u32nl_pid;/* Port ID */ __u32nl_groups;/* Multicast groups mask */};
Look at the structure of Netlinkhandler:
Netlinkhandler::netlinkhandler (int listenersocket): Netlinklistener (listenersocket) {}
Follow the parent class:
Netlinklistener::netlinklistener (int socket): SocketListener (socket, false) { Mformat = Netlink_format_ ASCII;}
And here we have constructed aSocklistener, and the socket ID created above is passed in.
The start () function, which is then called, is also the final implementation of the Startlistener () in Socklistener.
Inheritance relationship:netlinkhandler-->netlinklistener-->socketlistener
The implementation flow of the Socklistener and Startlistener () function to open the socket listener is analyzed in the previous android--4.2 Vold Mount Management _commandlistener (ii),
The difference is that the socket is different, and is not a normal listening socket, here the Mlisten for False,commandlistener is true, no longer do analysis!
Two: Netlinkmanager realization:
When the supervisor hears the socket event, as with Commandlistener, it invokes the implementation in the subclass of the virtual function ondataavailable of the SocketListener instance at that time ,
Here is the System/core/libsysutils/src/netlinklistener.cpp:
BOOL Netlinklistener::ondataavailable (socketclient *cli) { int socket = Cli->getsocket (); ssize_t count; uid_t uid =-1; Count = Temp_failure_retry (UEVENT_KERNEL_MULTICAST_UID_RECV ( socket, mbuffer, sizeof (Mbuffer), &uid));// Extract the EVENT buffer if (count < 0) { if (uid > 0) log_event_int (65537, UID) from the socket; Sloge ("Recvmsg failed (%s)", Strerror (errno)); return false; } Netlinkevent *evt = new Netlinkevent (); if (!evt->decode (Mbuffer, Count, Mformat)) {//To Netlinkeent instance resolution buffer, save parameter sloge ("Error decoding Netlinkevent "); } else { onEvent (evt);//virtual function ~ Pass EVT to subclass Netlinkhandler implementation } delete evt; return true;}
To NetlinkHandler.cpp:
void Netlinkhandler::onevent (Netlinkevent *evt) { Volumemanager *VM = Volumemanager::instance (); const char *subsys = Evt->getsubsystem (); if (!subsys) { SLOGW ("No subsystem found in NetLink event"); return; } if (!strcmp (Subsys, "block")) { vm->handleblockevent (evt);//Send event events to Volumemanager }}
Here really passed to the Volumemanager, realized from the kernel to Vold, see Volumemanager handle Processing:
void Volumemanager::handleblockevent (Netlinkevent *evt) { const char *devpath = Evt->findparam ("Devpath"); /* Lookup a volume to handle this device */ volumecollection::iterator it; BOOL hit = false; for (it = Mvolumes->begin (); it = Mvolumes->end (); ++it) {//traverse the volume instance in the container to pass in the event if (! ( *it)->handleblockevent (evt)) {//volume virtual function of class, subclass Directvolume implementation #ifdef Netlink_debug slogd ("Device '%s ' event Handled by volume%s\n ", Devpath, (*it)->getlabel ()); #endif hit = true; break; } } if (!hit) {#ifdef netlink_debug slogw ("No volumes handled block event for '%s '", Devpath); #endif }}
You can see that this is used in the android--4.2 Vold Mount Management _volumemanager (iii) resolves the volume added into the volume container.
Draw a netlinkmanager part of the general functional structure diagram:
The entire Netlinkmanager part of the implementation is generally the case, as for the kernel layer if the storage device is detected hot-swappable uevent and volume processing follow-up analysis!