開源鴻蒙北向框架開發:系統服務理論詳解

系統服務的啟動

基本可以認為: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…