系统服务的启动

基本可以认为:OpenHarmony 的系统服务进程都是“由 init 直接或间接拉起”的。

  • 直接方式:
    • init 按 /system/etc/init/.cfg 启动可执行(如 /system/bin/sa_main、/system/bin/samgr 等),这些进程的 PPid=1。
  • 间接方式:
    • 应用进程由 appspawn 拉起,但 appspawn 自身也是 init 启动的;系统能力(SA)一般不走 appspawn,而是走 sa_main 宿主进程。
  • 特殊/例外
    • 开发测试可手动启动进程(不通过 init);但系统正式启动路径都以 init 为根。
    • 同一宿主可承载多个 SA,这些 SA 并非独立进程,但宿主仍由 init 启动。

通过OpenHarmony系统进程pid可证明如上的观点:
输入如下命令:

  • ps -o pid,ppid,comm,args -A | grep -E 'samgr|sa_main'
    输出如下:
# ps -o pid,ppid,comm,args -A | grep -E 'samgr|sa_main'275     1 sa_main                     storage_manager277     1 sa_main                     khstabilitymgr278     1 samgr                       samgr279     1 sa_main                     resource_metrics280     1 sa_main                     param_watcher284     1 sa_main                     khmonitorservice286     1 sa_main                     khdeviceauth_service287     1 sa_main                     khcontainermanager_service290     1 sa_main                     cadaemon460     1 sa_main                     accessibility461     1 sa_main                     audio_server462     1 sa_main                     bluetooth_service468     1 sa_main                     concurrent_task_service506     1 sa_main                     huks_service510     1 sa_main                     kh_dids_agent_service521     1 sa_main                     khlink_service523     1 sa_main                     kh_sle_service542     1 sa_main                     memmgrservice546     1 sa_main                     multimodalinput578     1 sa_main                     resource_schedule_executor579     1 sa_main                     resource_schedule_service620     1 sa_main                     netmanager637     1 sa_main                     telephony638     1 sa_main                     wallpaper_service735     1 sa_main                     bgtaskmgr_service748     1 sa_main                     msdp770     1 sa_main                     time_service771     1 sa_main                     uart_service803     1 sa_main                     device_manager843     1 sa_main                     useriam853     1 sa_main                     ui_service854     1 sa_main                     thingmodel_service856     1 sa_main                     thing_accessmanager_service859     1 sa_main                     superdevice_thm860     1 sa_main                     superdevice_cfg862     1 sa_main                     superdevice_acs863     1 sa_main                     streaming_service874     1 sa_main                     softbus_server875     1 sa_main                     sensors876     1 sa_main                     security_guard878     1 sa_main                     khremoteops879     1 sa_main                     privacy_service880     1 sa_main                     powermgr882     1 sa_main                     pki_manager_service885     1 sa_main                     netsysnative887     1 sa_main                     miracast_service888     1 sa_main                     media_service889     1 sa_main                     media_monitor890     1 sa_main                     kloudaccount891     1 sa_main                     kh_update_sa892     1 sa_main                     khthingaccessmanager_peripherals894     1 sa_main                     khability_service895     1 sa_main                     khsharing_service896     1 sa_main                     khrule_engine_service897     1 sa_main                     khqrdetector_service898     1 sa_main                     license_sa899     1 sa_main                     khdlp_permission_service901     1 sa_main                     khcontainercomm_service911     1 sa_main                     kh_update913     1 sa_main                     kh_dids_manager_service916     1 sa_main                     inputmethod_service931     1 sa_main                     foundation932     1 sa_main                     drm_service934     1 sa_main                     distributeddata935     1 sa_main                     kh_dscreen936     1 sa_main                     deviceinfoservice938     1 sa_main                     camera_service940     1 sa_main                     av_session941     1 sa_main                     av_codec_service953     1 sa_main                     audio_magic_service956     1 sa_main                     accountmgr962     1 sa_main                     accesstoken_service1013     1 sa_main                     superdevice_khcomm1470     1 sa_main                     usb_service1471     1 sa_main                     hdf_ext_devmgr1788     1 sa_main                     kloudauth1789     1 sa_main                     otpauth1791     1 sa_main                     pinauth1886     1 sa_main                     wifi_manager_service2065     1 sa_main                     khwms_service2114     1 sa_main                     asset_service2207     1 sa_main                     download_server2245     1 sa_main                     cert_manager_service2879     1 sa_main                     deviceprofile6567  4997 toybox                      grep -E samgr|sa_main

可以看到很多sa_main,和一个samgr ,为什么呢?
分析命令:ps -o pid,ppid,comm,args -A | grep -E 'samgr|sa_main'
这里是使用ps查看当前的全部进程信息,且查看的信息是指定的4个字段

  • pid 当前的进程号
  • ppid 父进程号
  • comm 可执行文件的文件名(原始文件名)
  • args 完整的启动参数

通过上面的信息可知,所有的sa_main和samgr都是由init进程拉起的,因为init进程的进程号是1,且有很多个sa_main
查看init的进程号:

# ps -elf | grep init
root             1     0 0 07:59:59 ?     00:01:02 init --second-stage 2101515
root          7662  4997 1 01:42:09 pts/0 00:00:00 grep init

查看init的配置:
grep -Rni 'sa_main\|samgr' /system/etc/init

/system/etc/init/access_token.cfg:17:            "path" : ["/system/bin/sa_main", "/system/profile/accesstoken_service.json"],
/system/etc/init/accessibility.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/accessibility.json"],
/system/etc/init/accountmgr.cfg:18:            "path" : ["/system/bin/sa_main", "/system/profile/accountmgr.json"],
/system/etc/init/app_domain_verify_agent.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/app_domain_verify_agent.json"],
/system/etc/init/app_fwk_update_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/app_fwk_update_service.json"],
/system/etc/init/asset_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/asset_service.json"],
/system/etc/init/audio_magic_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/audio_magic_service.json"],
/system/etc/init/audio_server.cfg:44:      "path" : ["/system/bin/sa_main", "/system/profile/audio_server.json"],
/system/etc/init/av_codec_service.cfg:4:        "path" : ["/system/bin/sa_main", "/system/profile/av_codec_service.json"],
/system/etc/init/avsession_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/av_session.json"],
/system/etc/init/backup.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/backup_sa.json"],
/system/etc/init/bgtaskmgr_service.cfg:15:            "path" : ["/system/bin/sa_main", "/system/profile/bgtaskmgr_service.json"],
/system/etc/init/bluetooth_service.cfg:16:            "path" : ["/system/bin/sa_main", "/system/profile/bluetooth_service.json"],
/system/etc/init/cadaemon.cfg:16:            "path" : ["/system/bin/sa_main", "/system/profile/cadaemon.json"],
/system/etc/init/camera_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/camera_service.json"],
/system/etc/init/cert_manager_service.cfg:20:            "path" : ["/system/bin/sa_main", "/system/profile/cert_manager_service.json"],
/system/etc/init/compiler_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/compiler_service.json"],
/system/etc/init/concurrent_task_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/concurrent_task_service.json"],
/system/etc/init/daudio.cfg:4:        "path" : ["/system/bin/sa_main", "/system/profile/daudio.json"],
/system/etc/init/dcamera.cfg:4:        "path" : ["/system/bin/sa_main","/system/profile/dcamera.json"],
/system/etc/init/devattest_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/devattest_service.json"],
/system/etc/init/device_manager.cfg:12:        "path" : ["/system/bin/sa_main", "/system/profile/device_manager.json"],
/system/etc/init/deviceinfoservice.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/deviceinfoservice.json"],
/system/etc/init/deviceprofile.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/deviceprofile.json"],
/system/etc/init/dhardware.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/dhardware.json"],
/system/etc/init/dhardwarecommon_service.cfg:10:        "path" : ["/system/bin/sa_main", "/system/profile/kh_dscreen.json"],
/system/etc/init/dinput.cfg:4:        "path" : ["/system/bin/sa_main", "/system/profile/dinput.json"],
/system/etc/init/distributed_data.cfg:18:        "path" : ["/system/bin/sa_main","/system/profile/distributeddata.json"],
/system/etc/init/distributedbms.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/d-bms.json"],
/system/etc/init/distributedfile.cfg:11:        "path": ["/system/bin/sa_main", "/system/profile/distributedfiledaemon.json"],
/system/etc/init/distributedfile.cfg:29:        "path": ["/system/bin/sa_main", "/system/profile/cloudfiledaemon.json"],
/system/etc/init/distributedfile.cfg:46:        "path": ["/system/bin/sa_main", "/system/profile/cloudfileservice.json"],
/system/etc/init/distributedsched.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/distributedsched.json"],
/system/etc/init/dlp_permission_service.cfg:17:            "path" : ["/system/bin/sa_main", "/system/profile/dlp_permission_service.json"],
/system/etc/init/downloadservice.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/download_server.json"],
/system/etc/init/drm_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/drm_service.json"],
/system/etc/init/dscreen.cfg:4:        "path" : ["/system/bin/sa_main", "/system/profile/dscreen.json"],
/system/etc/init/dslm_service.cfg:7:                "/system/bin/sa_main",
/system/etc/init/edm.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/edm.json"],
/system/etc/init/el5_filekey_manager.cfg:15:        "/system/bin/sa_main",
/system/etc/init/file_access_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/file_access_service.json"],
/system/etc/init/foundation.cfg:43:            "path" : ["/system/bin/sa_main", "/system/profile/foundation.json"],
/system/etc/init/hdf_ext_devmgr.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/hdf_ext_devmgr.json"],
/system/etc/init/hidumper_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/hidumper_service.json"],
/system/etc/init/huks_service.cfg:17:            "path" : ["/system/bin/sa_main", "/system/profile/huks_service.json"],
/system/etc/init/i18n_service.cfg:17:            "path": ["/system/bin/sa_main", "/system/profile/i18n_service.json"],
/system/etc/init/inputmethodservice.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/inputmethod_service.json"],
/system/etc/init/installs.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/installs.json"],
/system/etc/init/intell_voice_service.cfg:16:            "path" : ["/system/bin/sa_main", "/system/profile/intell_voice_service.json"],
/system/etc/init/kh_dids_agent_service.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/kh_dids_agent_service.json"],
/system/etc/init/kh_dids_manager_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/kh_dids_manager_service.json"],
/system/etc/init/kh_dinput.cfg:4:        "path" : ["/system/bin/sa_main", "/system/profile/kh_dinput.json"],
/system/etc/init/kh_update.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/kh_update.json"],
/system/etc/init/khcontainercomm_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/khcontainercomm_service.json"],
/system/etc/init/khcontainermanager_service.cfg:26:            "path" : ["/system/bin/sa_main", "/system/profile/khcontainermanager_service.json"]
/system/etc/init/khdeviceauthservice.cfg:13:      "path": [ "/system/bin/sa_main", "/system/profile/khdeviceauth_service.json" ],
/system/etc/init/khdlp_permission_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/khdlp_permission_service.json"],
/system/etc/init/khlicense_sa.cfg:5:            "path" : ["/system/bin/sa_main", "/system/profile/license_sa.json"],
/system/etc/init/khlink.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/khlink_service.json"],
/system/etc/init/khmonitor.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/khmonitorservice.json"],
/system/etc/init/khqrdetector_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/khqrdetector_service.json"],
/system/etc/init/khrule_engine_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/khrule_engine_service.json"],
/system/etc/init/khsharing_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/khsharing_service.json"],
/system/etc/init/khsle.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/kh_sle_service.json"],
/system/etc/init/khsysabilityservice_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/khability_service.json"],
/system/etc/init/khthingaccessmanager_peripherals.cfg:11:        "path" : ["/system/bin/sa_main", "/system/profile/khthingaccessmanager_peripherals.json"],
/system/etc/init/khupdate_sa.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/kh_update_sa.json"],
/system/etc/init/khwms_service.cfg:14:            "path" : ["/system/bin/sa_main", "/system/profile/khwms_service.json"],
/system/etc/init/kloud_account.cfg:10:            "path" : ["/system/bin/sa_main", "/system/profile/kloudaccount.json"],
/system/etc/init/kloud_auth.cfg:17:            "path" : ["/system/bin/sa_main", "/system/profile/kloudauth.json"],
/system/etc/init/local_code_sign.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/local_code_sign.json"],
/system/etc/init/locationsa.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/locationhub.json"],
/system/etc/init/mdnsmanager.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/mdnsmanager.json"],
/system/etc/init/media_monitor.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/media_monitor.json"],
/system/etc/init/media_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/media_service.json"],
/system/etc/init/memmgrservice.cfg:29:            "path" : ["/system/bin/sa_main", "/system/profile/memmgrservice.json"],
/system/etc/init/miracast_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/miracast_service.json"],
/system/etc/init/module_update_sa.cfg:15:            "path" : ["/system/bin/sa_main", "/system/profile/module_update_sa.json"],
/system/etc/init/msdp_musl.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/msdp.json"],
/system/etc/init/multimodalinput.cfg:26:            "path" : ["/system/bin/sa_main", "/system/profile/multimodalinput.json"],
/system/etc/init/netmanager_base.cfg:23:            "path" : ["/system/bin/sa_main", "/system/profile/netmanager.json"],
/system/etc/init/netsysnative.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/netsysnative.json"],
/system/etc/init/oaidservice.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/oaid_service.json"],
/system/etc/init/otp_auth.cfg:17:            "path" : ["/system/bin/sa_main", "/system/profile/otpauth.json"],
/system/etc/init/param_watcher.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/param_watcher.json"],
/system/etc/init/pasteboardservice.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/pasteboard_service.json"],
/system/etc/init/pinauth_sa_profile.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/pinauth.json"],
/system/etc/init/pki_manager_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/pki_manager_service.json"],
/system/etc/init/powermgr.cfg:20:            "path" : ["/system/bin/sa_main", "/system/profile/powermgr.json"],
/system/etc/init/printservice.cfg:30:            "path" : ["/system/bin/sa_main", "/system/profile/print_service.json"],
/system/etc/init/privacy.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/privacy_service.json"],
/system/etc/init/quick_fix.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/quick_fix.json"],
/system/etc/init/remote_ops.cfg:18:            "path" : ["/system/bin/sa_main", "/system/profile/khremoteops.json"],
/system/etc/init/resource_metrics_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/resource_metrics.json"],
/system/etc/init/resource_schedule_executor.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/resource_schedule_executor.json"],
/system/etc/init/resource_schedule_service.cfg:119:            "path" : ["/system/bin/sa_main", "/system/profile/resource_schedule_service.json"],
/system/etc/init/samgr_standard.cfg:5:                "mkdir /data/samgr 0740 samgr samgr"
/system/etc/init/samgr_standard.cfg:10:            "name" : "samgr",
/system/etc/init/samgr_standard.cfg:11:            "path" : ["/system/bin/samgr"],
/system/etc/init/samgr_standard.cfg:13:            "uid" : "samgr",
/system/etc/init/samgr_standard.cfg:14:            "gid" : ["samgr", "system", "readproc", "data_reserve"],
/system/etc/init/samgr_standard.cfg:16:            "bootevents":"bootevent.samgr.ready",
/system/etc/init/samgr_standard.cfg:41:            "secon" : "u:r:samgr:s0",
/system/etc/init/sandbox_manager_service.cfg:10:        "path" : ["/system/bin/sa_main", "/system/profile/sandbox_manager_service.json"],
/system/etc/init/scanservice.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/scan_service.json"],
/system/etc/init/scanservice.rc:16:service scan_service /system/bin/sa_main /system/profile/scan_service.json
/system/etc/init/security_collector.cfg:6:        "/system/bin/sa_main",
/system/etc/init/security_component_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/security_component_service.json"],
/system/etc/init/security_guard.cfg:15:            "path" : ["/system/bin/sa_main", "/system/profile/security_guard.json"],
/system/etc/init/sensors_musl.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/sensors.json"],
/system/etc/init/softbus_server_musl.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/softbus_server.json"],
/system/etc/init/srms.cfg:5:            "path" : ["/system/bin/sa_main", "/system/profile/service_router.json"],
/system/etc/init/stability_mgr_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/khstabilitymgr.json"],
/system/etc/init/storage_manager.cfg:4:         "path": ["/system/bin/sa_main", "/system/profile/storage_manager.json"],
/system/etc/init/streaming_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/streaming_service.json"],
/system/etc/init/superdevice_acs.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/superdevice_acs.json"],
/system/etc/init/superdevice_cfg.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/superdevice_cfg.json"],
/system/etc/init/superdevice_khcloudcomm.cfg:19:            "path" : ["/system/bin/sa_main", "/system/profile/superdevice_khcomm.json"],
/system/etc/init/superdevice_thm.cfg:15:            "path" : ["/system/bin/sa_main", "/system/profile/superdevice_thm.json"],
/system/etc/init/sys_installer_sa.cfg:25:            "path" : ["/system/bin/sa_main", "/system/profile/sys_installer_sa.json"],
/system/etc/init/telephony.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/telephony.json"],
/system/etc/init/testserver.cfg:6:        "/system/bin/sa_main",
/system/etc/init/thing_accessmanager.cfg:16:            "path" : ["/system/bin/sa_main", "/system/profile/thing_accessmanager_service.json"],
/system/etc/init/thingmodel_service.cfg:14:            "path" : ["/system/bin/sa_main", "/system/profile/thingmodel_service.json"],
/system/etc/init/timeservice.cfg:14:            "path" : ["/system/bin/sa_main", "/system/profile/time_service.json"],
/system/etc/init/token_sync.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/token_sync_service.json"],
/system/etc/init/uart.cfg:15:            "path" : ["/system/bin/sa_main", "/system/profile/uart_service.json"],
/system/etc/init/ui_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/ui_service.json"],
/system/etc/init/updater_sa.cfg:24:            "path" : ["/system/bin/sa_main", "/system/profile/updater_sa.json"],
/system/etc/init/usb_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/usb_service.json"],
/system/etc/init/useriam.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/useriam.json"],
/system/etc/init/wallpaperservice.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/wallpaper_service.json"],
/system/etc/init/wifi_standard.cfg:23:            "path" : ["/system/bin/sa_main", "/system/profile/wifi_manager_service.json"],

可以看到形如:"path": ["/system/bin/sa_main", "/system/profile/xxx.json"] 等配置行
这表示init拉起的程序是sa_main,给sa_main的启动参数是/system/profile/xxx.json文件
至此可以得出如下结论:OpenHarmony 的系统服务进程都是“由 init 直接或间接拉起”的
那么我们自己创建的服务也应该由init进程去拉起,此时就需要配置init的拉起配置文件

系统服务启动的疑点

上面的命令行查出来的都是sa_main,但是实际上查看进程信息的时候压根就没有看到sa_main,比如如下命令:
ps -elf | grep storage_manager

# ps -elf | grep storage_manager
storage_manager 275    1 0 08:00:06 ?     00:00:02 storage_manager
root          8686  4997 0 01:56:53 pts/0 00:00:00 grep storage_manager

如上是我们最常用的查看进程信息的方法,但是查看storage_manager进程的时候没有发现sa_main字段,这是怎么回事呢?上面明明出现了

# ps -o pid,ppid,comm,args -A | grep -E 'samgr|sa_main'275     1 sa_main                     storage_manager

这个表示storage_manager只是启动的命令,为什么会变成进程名呢?
现在根据以上的信息我们可知:sa服务是由init拉起,且init拉起的可执行程序是sa_main,只是在sa_main的执行逻辑中把进程名改成了启动参数(storage_manager)
那么现在是不是还需要考虑下sa_main的启动配置文件

由上面两个章节可知:
一个系统服务至少需要两个配置文件:

  1. init启动配置文件
  2. sa_main的载入配置文件

什么是sa_main

sa_main 是 OpenHarmony 的“系统能力(SA)通用宿主可执行程序”,路径通常为 /system/bin/sa_main。

# pwd
/system/bin
# find ./ -name "*main*"
./dnsdomainname
./sa_main

职责:
启动后解析传入的SA Profile(如/system/profile/xxx.json)。
按libpath/depend/run-on-create 在同一进程内dlopen各 SA 动态库,创建实例并调用 OnStart。
将SA注册到系统能力管理器(SAMgr),用于跨进程发现与调用。
启动链路:init 通过 init 配置(.cfg)拉起 /system/bin/sa_main /system/profile/xxx.json → sa_main 装载和启动多个 SA → SA 对外经 SAMgr 提供 IPC。
特点:

  • 一个 sa_main 进程可承载多个 SA(共驻进程,省进程/内存)。
  • sa_main 不负责“创建进程”(这是 init 的事),负责“进程内加载 SA 并管理其生命周期”。

为什么OpenHarmony需要把系统服务做成sa形式而不是像linux那样使用systemd+守护进程

  • 定位与约束不同
    • OpenHarmony面向端侧/嵌入式/多内核(Linux/LiteOS),不依赖systemd生态;需要更轻量、可裁剪、统一于大小系统的机制。
    • 资源敏感与启动确定性要求高,偏好“多服务共驻”与精细依赖编排。
  • SA(System Ability)带来的架构收益
    • 统一宿主与装载:sa_main+profile 在同一进程内 dlopen 多个 SA,减少进程数与内存占用,提升冷启速度。
    • 依赖与生命周期:profile 的 depend/run-on-create/depend-time-out + OnStart/OnStop/OnAddSystemAbility,内置依赖等待与“后补初始化”回调。
    • 统一发现与调用:SAMgr 以 SA_ID 做注册/发现,客户端经 iface_cast 获取 Proxy,天然走统一 IPC(Binder/LiteIPC)模型,类型安全、带死亡通知。
    • 跨版本与多形态一致:同一套 SA/IPC 模型覆盖 AOSP 风格设备与轻量设备(LiteIPC),开发者心智统一。
    • 安全一体化:与 AccessToken 权限、SELinux 域、init 能力位(caps)等打通,易做最小权限与进程级管控。
  • 为什么不走“systemd+守护进程”常规做法
    • systemd 体量与依赖链对端侧并不总适配;OHOS 自带 init 与参数服务、事件/权限体系,更易裁剪和跨内核迁移。
    • 守护进程各自为政:依赖/发现/IPC/权限需自行约定;SA 模型把“发现(SAMgr)+ 调用(Proxy/Stub)+ 生命周期”做了平台级统一,降低系统集成与维护成本。
    • 分布式场景友好:SA_ID 语义、跨设备能力扩展更顺滑(与分布式总线/能力发现结合)。
  • 能否用套接字/自定义守护
    • 可以,但会失去 SAMgr 统一发现、类型化 IPC、依赖编排与权限治理带来的平台收益。通常建议:对内用 SA;对外协议(MQTT/HTTP/TCP)由 SA 内部服务化暴露。
      一句话:SA 是 OHOS 为端侧/多形态场景量身设计的“统一系统服务模型”,在资源、依赖、IPC、安全、分布式等维度提供平台级一体化能力,这是它相对“systemd+daemon”更契合 OHOS 目标的原因。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/96007.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/96007.shtml
英文地址,请注明出处:http://en.pswp.cn/pingmian/96007.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

龙虎榜——20250909

上证指数今天缩量收阴线,跌破10日均线,目前日线总体处于高位宽幅震荡中,小级别暂未明确方向,指数面临方向选择,需要注意高位股的风险。 深证指数今天缩量收阴线,跌破5日均线,接下来几天方向的选…

基于dijkstra算法的WSN网络MAC协议matlab仿真,分析网络延迟与网络开销

目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.部分程序 4.算法理论概述 5.完整程序 1.程序功能描述 无线传感器网络(WSN, Wireless Sensor Network)是由大量低成本、低功耗的传感器节点组成,通过无线通信实现数据采集、传输与…

ES数据库启动时可以设置1G一下内存吗

可以,但强烈不建议在生产环境中这样做。ES 7.0 版本确实可以设置小于 1GB 的堆内存,但这会带来严重的性能问题和稳定性风险。 快速回答 # 最小化配置示例 - 仅用于测试或开发环境 export ES_JAVA_OPTS"-Xms512m -Xmx512m" ./bin/elasticsearch…

TI-92 Plus计算器:单位换算功能介绍

1 TI-92 Plus计算器:单位换算功能介绍 TI-92 Plus 内置了全面的单位换算功能,支持长度、质量、时间、温度、面积、体积、速度等数十种物理量的单位转换,操作直观,无需手动输入换算系数。以下是具体使用方法、示例及功能特点&#…

雪球科技Java开发工程师笔试题

单选 1.下列哪些语句关于内存回收的说明是正确的?( C ) A.内存回收程序允许程序员直接释放内存 B.程序员必须创建一个线程来释放内存 C.内存回收程序负责释放无用内存 D.内存回收程序可以在指定的时间释放内存对象 2.以下哪项不是Java基础类型(A) A.String B.int C.b…

NV3041A-01芯片屏幕

1. 核心概览这是一款集成了电源管理、显示内存(RAM)、时序控制等多种功能的单片显示驱动芯片(通常称为Driver IC)。它采用COG(Chip-On-Glass) 工艺,直接将芯片绑定在玻璃基板上,使得…

aiagent知识点

一、MCP (Model Context Protocol) 1. 核心概念是什么:MCP是一个开放协议,用于在应用(如IDE、Agent) 和工具/数据源(如服务器、数据库) 之间建立标准化的通信。目标:解决AI工具生态的碎片化问题…

第2节-过滤表中的行-WHERE

摘要:在本教程中,您将学习如何使用 PostgreSQL 的 WHERE 子句来筛选表中的行。 PostgreSQL WHERE 子句 SELECT FROM 语句从表中所有行的一个或多个列中查询数据。实际上,你经常需要选择满足某个条件的行。 要根据条件从表中筛选行&#xf…

IACheck赋能AI环评报告审核,推动环保设备制造行业发展

在“双碳目标”和绿色制造的背景下,环保设备制造行业正在迎来快速发展。然而,环评报告作为项目合规的“通行证”,却一直是企业最头疼的环节之一:编写复杂、审核周期长、错误率高。传统的审核模式不仅耗时耗力,还容易出…

常见的多态

一、核心概念多态(Polymorphism) 的字面意思是“多种形态”。在Java中,它指的是:同一个行为(方法)具有多个不同表现形式或形态的能力。更具体地说,它允许你:父类的引用指向子类的对象…

20250909_排查10.1.1.190档案库房综合管理系统20250908备份缺失问题+优化scp脚本(把失败原因记录进日志)并测试脚本执行情况

目录 一、现象(图中现象) 二、可能原因 1、本地源文件没生成(最可能原因) 2、清理逻辑误删 三、时间同步检查 1、运行命令查看当前时间源:w32tm /query /source 2、修改为内网 NTP 服务器 10.1.10.251: 四、脚本优化 1、scp.bat vs roboscp.bat 对比表 2、robo…

OpenHarmony之有源NFC-connected_nfc_tag模块详解

1. 模块概述 Connected NFC Tag模块是OpenHarmony NFC子系统的一部分,专门用于处理有源NFC标签的读写操作。与基础NFC模块不同,该模块专注于与已连接的有源NFC标签进行交互,提供更高层次的API供应用层使用。 设备必须具备有源NFC标签芯片,才能使用有源NFC标签的读和写服务…

MySql案例详解之事务

下面我会从“事务是什么”→“为什么需要事务”→“事务的四大特性(ACID)”→“MySQL中怎么用事务”→“常见坑与调试技巧”→“完整实战案例(含代码、输出、讲解)”六个层次,给你一个“看完就能上手”的MySQL事务速查…

Linux之环境变量(内容由浅入深,层层递进)

一、概念介绍(来源:比特就业课)环境变量一般是指在操作系统中用来指定操作系统运行环境的一些参数环境变量通常具有某些特殊用途,并且在系统中通常具有全局特性二、现象引入与解答 1.为什么像ls这样的系统指令可以直接执行&#x…

监控 Linux 服务器资源

使用 Bash 脚本监控 Linux 服务器资源并发送告警邮件前言一、🛠️ 脚本功能概览二、 📜 脚本核心逻辑分解2.1. 变量初始化2.2. CPU 使用率监控2.3. 内存使用率监控2.4. 磁盘使用率监控2.5. 磁盘 IO 延迟监控(await)2.6. 网络流量监…

随机获取数组内任意元素

Math.random() * arr.length 是 JavaScript 中常用的表达式,用于生成一个范围在 [0, arr.length) 之间的随机浮点数(包含 0,但不包含数组长度本身)。 作用说明: Math.random() 生成一个 [0, 1) 区间的随机浮点数&#…

android studio gradle 访问不了

1.通过国内镜像站 2.通过本地部署 参考这个搞定 https://blog.csdn.net/2401_82819685/article/details/144542784

科普:企业微信的第三方应用涉及的“配置应用权限”

企业微信的第三方应用涉及“配置应用权限”,它在不同阶段含义不同:开发阶段意指应用自身所需的功能权限,安装阶段意指企业管理员对应用使用范围的控制,产生此歧义的问题,归根到低还是语言的缩写问题,设想一…

YOLOv11改进大全:从卷积层到检测头,全方位提升目标检测性能

## 1 引言YOLO(You Only Look Once)系列作为目标检测领域的重要算法,以其**高效推理**和**良好精度**赢得了广泛认可。2024年9月,Ultralytics团队正式发布了YOLOv11,在先前版本基础上引入了**多项架构改进**和**训练优…

JWT全面理解

目录 一、JWT是什么 1、身份认证(最核心用途) 2、信息交换 3、授权控制 二、JWT的核心价值 三、如何理解JWT的结构和工作原理 1、三部分结构解析 2、核心工作流程 四、JWT的使用步骤 1、添加依赖 2、添加配置文件 3、创建实体类 4、创建JWT…