有没有办法优雅地停止 init.rc 中的 android 服务(没有 SIGKILL)?

Is there a way to stop android services in init.rc gracefully (without SIGKILL)?

我有一个本机 Android 服务,它由通过 GUI 设置的 属性 启动和停止。 init.rc中启动和停止服务的命令如下:

service myservice /system/bin/myservice
    class late_start
    user my_service
    disabled

on property:persist.myapp.myservice.enable=1
    start myservice

on property:persist.myapp.myservice.enable=0
    stop myservice

如果服务停止,有需要清理的JNI内存资源,但似乎是通过SIGKILL停止了服务:

6944  6974 V myservice: IMyProcessA::onTransact()
 712   712 I AP_KERNEL: init: Service 'myservice' is being killed...
 510   510 I ServiceManager: service 'ProcessA.MyService' died
 656   656 V ProcessA: ProcessA::NotificationClient::binderDied()
 656   656 V ProcessA: removeNotificationClient() 0xb6bc60a8, pid 6944
 656   656 V ProcessA: ProcessA::NotificationClient::~NotificationClient()
 712   712 I AP_KERNEL: init: Service 'myservice' (pid 6944) killed by signal 9
 712   712 I AP_KERNEL: init: Service 'myservice' (pid 6944) killing any children in process group 

因为没有办法拦截 SIGKILL 来清理,所以 init.rc 'stop myservice' 是否有任何选项可以将终止信号更改为我可以拦截的东西,例如 SIGTERM,例如?

在与同事讨论后,我们的共识是在 SIGKILL 之后,内核将回收与进程关联的所有内存,并且不会有内存泄漏。

如果有人有任何与之相矛盾的信息,请告诉我。

你的"no memory leak"站在内核端,但是很多工作需要在用户space中完成,才能在一个进程结束之前,比如下面的,

  • 关闭文件并将缓存数据刷新到内核层。
  • 删除一些临时文件。
  • 通知其他进程我快死了。
  • 将RAM中缓存的部分数据写入磁盘,否则磁盘中的数据会不一致。

…… 以上,发送SIGKILL是很粗暴的。