Android ServiceManager and Service Management

Source: Internet
Author: User

It is an important process in android. It is started after the init process is started. It can be seen from its name that it is used to manage the service in the system. For example, InputMethodService and ActivityManagerService. There are two important methods in ServiceManager: add_service and check_service. The system service needs to register its own information to ServiceManager through add_service. when it needs to be used, check whether the service exists through check_service.

Main function (anrdroid4.0/frameworks/base/cmds/servicemanager/service_manager.c)


Starting from its main function code:

Int main (int argc, char ** argv)
{
Struct binder_state * bs;
Void * svcmgr = BINDER_SERVICE_MANAGER;
Bs = binder_open (128*1024 );
If (binder_become_context_manager (bs )){
LOGE ("cannot become context manager (% s) \ n", strerror (errno ));
Return-1;
}
Svcmgr_handle = svcmgr;
Binder_loop (bs, svcmgr_handler );
Return 0;
}
It can be seen from the main function that it mainly does three things:

Open the/dev/binder device and map the space of kb in the memory.
Notify the Binder device to change itself to context_manager.
Go to the loop and constantly read the Binder device to check whether there are service requests. If so, call the svcmgr_handler function to handle the requests.
Service Registration


Let's take a look at how ServiceManager registers the service. First, when there is a request for service, the callback function svcmgr_handler called:

Int svcmgr_handler (struct binder_state * bs,
Struct binder_txn * txn,
Struct binder_io * msg,
Struct binder_io * reply)
{
Struct svcinfo * si;
Uint16_t * s;
Unsigned len;
Void * ptr;
Uint32_t strict_policy;
// LOGI ("target = % p code = % d pid = % d uid = % d \ n ",
// Txn-> target, txn-> code, txn-> sender_pid, txn-> sender_euid );
If (txn-> target! = Svcmgr_handle)
Return-1;
// Equivalent to Parcel: enforceInterface (), reading the RPC
// Header with the strict mode policy mask and the interface name.
// Note that we ignore the strict_policy and don't propagate it
// Further (since we do no outbound RPCs anyway ).
Strict_policy = bio_get_uint32 (msg );
S = bio_get_string16 (msg, & len );
If (len! = (Sizeof (svcmgr_id)/2) |
Memcmp (svcmgr_id, s, sizeof (svcmgr_id ))){
Fprintf (stderr, "invalid id % s \ n", str8 (s ));
Return-1;
}
Switch (txn-> code ){
Case SVC_MGR_GET_SERVICE:
Case SVC_MGR_CHECK_SERVICE:
S = bio_get_string16 (msg, & len );
Ptr = do_find_service (bs, s, len );
If (! Ptr)
Break;
Bio_put_ref (reply, ptr );
Return 0;
Case SVC_MGR_ADD_SERVICE:
S = bio_get_string16 (msg, & len );
Ptr = bio_get_ref (msg );
If (do_add_service (bs, s, len, ptr, txn-> sender_euid ))
Return-1;
Break;
Case SVC_MGR_LIST_SERVICES :{
Unsigned n = bio_get_uint32 (msg );
Si = svclist;
While (n --> 0) & si)
Si = si-> next;
If (si ){
Bio_put_string16 (reply, si-> name );
Return 0;
}
Return-1;
}
Default:
LOGE ("unknown code % d \ n", txn-> code );
Return-1;
}
Bio_put_uint32 (reply, 0 );
Return 0;
}
In this callback function, the system will determine the Service needs. If a service is requested to be registered, it will be executed for a long time:


Case SVC_MGR_ADD_SERVICE:
S = bio_get_string16 (msg, & len );
Ptr = bio_get_ref (msg );
If (do_add_service (bs, s, len, ptr, txn-> sender_euid ))
Return-1;
Break;
Let's take a look at what do_add_service has done:

Int do_add_service (struct binder_state * bs,
Uint16_t * s, unsigned len,
Void * ptr, unsigned uid)
{
Struct svcinfo * si;
// LOGI ("add_service ('% s', % p) uid = % d \ n", str8 (s), ptr, uid );
If (! Ptr | (len = 0) | (len> 127 ))
Return-1;
If (! Svc_can_register (uid, s )){
LOGE ("add_service ('% s', % p) uid = % d-permission denied \ n ",
Str8 (s), ptr, uid );
Return-1;
}
Si = find_svc (s, len );
If (si ){
If (si-> ptr ){
LOGE ("add_service ('% s', % p) uid = % d-already registered \ n ",
Str8 (s), ptr, uid );
Return-1;
}
Si-> ptr = ptr;
} Else {
Si = malloc (sizeof (* si) + (len + 1) * sizeof (uint16_t ));
If (! Si ){
LOGE ("add_service ('% s', % p) uid = % d-out of memory \ n ",
Str8 (s), ptr, uid );
Return-1;
}
Si-> ptr = ptr;
Si-> len = len;
Memcpy (si-> name, s, (len + 1) * sizeof (uint16_t ));
Si-> name [len] = '\ 0 ';
Si-> death. func = svcinfo_death;
Si-> death. ptr = si;
Si-> next = svclist;
Svclist = si;
}
Binder_acquire (bs, ptr );
Binder_link_to_death (bs, ptr, & si-> death );
Return 0;
}
In this function, the system first checks whether you have the permission to register the service. If you do not have the permission, the system returns directly and cannot register the service.


If (! Svc_can_register (uid, s )){
LOGE ("add_service ('% s', % p) uid = % d-permission denied \ n ",
Str8 (s), ptr, uid );
Return-1;
}
Then, the system checks whether the service has been registered. If you have already registered the service, you cannot register it again:


Si = find_svc (s, len );
If (si ){
If (si-> ptr ){
LOGE ("add_service ('% s', % p) uid = % d-already registered \ n ",
Str8 (s), ptr, uid );
Return-1;
}
Si-> ptr = ptr;
}
Then judge whether the memory is sufficient:

Si = malloc (sizeof (* si) + (len + 1) * sizeof (uint16_t ));
If (! Si ){
LOGE ("add_service ('% s', % p) uid = % d-out of memory \ n ",
Str8 (s), ptr, uid );
Return-1;
}
If there is no problem, the service will be registered and added to svcList. Note: svclist is used to maintain service information in ServiceManager. The service name and handler are saved.

Service acquisition


After performing the preceding steps, the service is successfully registered. How to handle this service. Let's take a look at the judgment in the callback function:

Case SVC_MGR_CHECK_SERVICE:
S = bio_get_string16 (msg, & len );
Ptr = do_find_service (bs, s, len );
If (! Ptr)
Break;
Bio_put_ref (reply, ptr );
Return 0;

To obtain the service, run SVC_MGR_CHECK_SERVICE, write the returned data to reply, and return it to the client.

The do_find_service function mainly performs service search.


Void * do_find_service (struct binder_state * bs, uint16_t * s, unsigned len)
{
Struct svcinfo * si;
Si = find_svc (s, len );
// LOGI ("check_service ('% s') ptr = % p \ n", str8 (s), si? Si-> ptr: 0 );
If (si & si-> ptr ){
Return si-> ptr;
} Else {
Return 0;
}
}
In this way, the service registration and search are completed in ServiceManager. Let's take a look at the ServiceManager function diagram:

 
 

 


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.