When the android init process is initialized, in addition to some necessary initialization of the system, it is to start the service. The service is defined in the init script, so it is necessary to understand how to process the service in init.
Struct service
The data structure stores service-related information. You can define options under the service. These options are stored in this struct. In addition, the runtime management information of the service is saved as follows:
[Cpp]
Struct service {
/* List of all services */
Struct listnode slist;
Const char * name; // name
Const char * classname; // category: default
Unsigned flags; // option, see SVC _ macro definition in init. h
Pid_t pid; // pid of the process where the service is located
Time_t time_started;/* time of last start */
Time_t time_crashed;/* first crash within inspection window */
Int nr_crashed;/* number of times crashed within window */
Uid_t uid; // valid user ID
Gid_t gid; // valid group ID
Gid_t supp_gids [NR_SVC_SUPP_GIDS]; // supplementary group IDs
Size_t nr_supp_gids; // size of supp_gids
Struct socketinfo * sockets; // ETS created for the service
Struct svcenvinfo * envvars; // environment variable set for the service
Struct action onrestart;/* Actions to execute on restart .*/
/* Keycodes for triggering this service via/dev/keychord */
Int * keycodes;
Int nkeycodes;
Int keychord_id;
Int ioprio_class; // io priority
Int ioprio_pri;
Int nargs; // the number of parameters. See the following description.
/* "Must be at the end of the struct "*/
Char * args [1]; // service [service name] [args] NULL # number of parameters containing the service + 1
};/* ^ ------- 'Args' MUST be at the end of this struct! */
Resolution service Section
Define service: service <name> <parthname> [<argument>] *. Pathname indicates the command used to start the service.
The function for parsing the service section is parse_service (). The main function of this function is to create a service object, parse and define the service row, and set the default class of the service to default.
Resolution service option
The option of the Resolution service is located in the parse_line_service () function. The options supported by this function are as follows:
Capability # not implemented yet
Class <name> # Set the class name to name. It feels a bit like the priority of the startup service. The default class name is default.
Console # Open the console on the android Screen
Disabled # After setting, it cannot be automatically started by class name. It must be explicitly started by service name
Ioprio <rt | be | idle> <0-7> # Set the io priority
Group <groupname> [<groupname>] * # Set the valid group ID (first parameter) and supplementary group IDs (second to last) of the Service Process)
Keycodes <keycodes> [<keycodes>] * # keycodes related
Oneshot # When the service exits, it will not start again, but it can be started by name
Onrestart # The Command executed when the service is restarted may be executed when the service is restarted.
Critical # Is the device-critical service and exits more than 4 times in 4 minutes. Then the device restarts to recover mode.
Setenv <name> <value> # Set the service environment variable
Socket <name> <type> <perm> [<user> [<group>] # create a socket for the service. You can create multiple
User <effectuserid> # Set the valid user ID of a service process
Start service
The service Startup function is service_start (struct service * svc, const char * dynamic_args ). Dynamic_args is used only when oneshot exists in the service option. In this case, the service will be started by replacing the command parameter for starting the service.
The service option is recorded in the struct service. Therefore, when starting the service, consider these options. The service pid and status are recorded.
In the init process, you can start the service in the following ways:
1. Add the command related to starting the Service under the action. Commands related to operation services in action include:
Class_start <serviceclass> # Start all services of the specified class
Class_stop <serviceclass> # Stop all services of the specified class and cannot be started through class_start in the future
Class_reset <serviceclass> # Stop the service, which can be started later through class_start
Restart <servicename> # restart the service with the specified name. stop the service first, and then start
Start <servicename> # start the service with the specified name
Stop <servicename> # stop the service with the specified name
2. In the restart_processes () function. This function is located in the main thread loop of init to check whether the service needs to be restarted. For more information, see init. c.
3. handle_property_set_fd () function. Send control messages to the property service named property_service of the socket to enter this function. For more information, see property_service.c.
4. handle_keychord () function. This function is related to the chorded keyboard. For more information, see
Service in init. rc
The service class can be divided into two categories: class core and class main. The class core contains the following services:
Ueventd/sbin/ueventd # process kernel uevent messages
Console/system/bin/sh # console service
Adbd/sbin/adbd # adb debugging server
Servicemanager/system/bin/servicemanager # manage services. managed services are usually used by the Supply Application.
Vold/system/bin/vold # manage storage devices
The service of class main includes:
Netd/system/bin/netd # Network Manager
Debugadh/system/bin/debugadh # debug information can be output in logcat
Ril-deamon/system/bin/rild # Call Service
Surfaceflinger/system/bin/surfacefliger # service for merging framebuffer
Zygote/system/bin/app_process # service for incubating java application processes
Drm/system/bin/drmserver # DRM service, frameworks/base/drm
Media/system/bin/mediaserver # Multimedia Service
Bootanim/system/bin/bootanimation # boot animation Service
Executor/system/bin/executor-daemon # services used for inter-process communication
Effecthd/system/bin/effecthd # Bluetooth
Installd/system/bin/installd # services installed by apk
Flash_recovery/system/etc/install-recovery.sh # recover recovery Partition
Racoon/system/bin/racoon # key management daemon
Mtpd/system/bin/mtpd # MTP (Media Transfer Protocol) daemon
Keystore/system/bin/keystore # application Signature
Dumpstate/system/bin/dumpstate # Performance Testing Tool