系統服務的啟動
基本可以認為: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的啟動配置文件
由上面兩個章節可知:
一個系統服務至少需要兩個配置文件:
- init啟動配置文件
- 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 目標的原因。
- 可以,但會失去 SAMgr 統一發現、類型化 IPC、依賴編排與權限治理帶來的平臺收益。通常建議:對內用 SA;對外協議(MQTT/HTTP/TCP)由 SA 內部服務化暴露。