Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit de76688e authored by Daniel Rosenberg's avatar Daniel Rosenberg
Browse files

init: Add gentle_kill service property

If a service specifies gentle_kill, attempt to stop it will send SIGTERM
instead of SIGKILL. After 200ms, it will issue a SIGKILL.

Bug: 249043036
Test: atest CtsInitTestCases:init#GentleKill
      Added in next patch
Change-Id: Ieb0e4e24d31780aca1cf291f9d21d49cee181cf2
parent fe9d8325
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -244,6 +244,10 @@ runs the service.
  "r", "w" or "rw".  For native executables see libcutils
  android\_get\_control\_file().

`gentle_kill`
> This service will be sent SIGTERM instead of SIGKILL when stopped. After a 200 ms timeout, it will
  be sent SIGKILL.

`group <groupname> [ <groupname>\* ]`
> Change to 'groupname' before exec'ing this service.  Additional
  groupnames beyond the (required) first one are used to set the
+5 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#include <thread>

#include <android-base/file.h>
#include <android-base/logging.h>
@@ -887,6 +888,10 @@ void Service::StopOrReset(int how) {
    }

    if (pid_) {
        if (flags_ & SVC_GENTLE_KILL) {
            KillProcessGroup(SIGTERM);
            if (!process_cgroup_empty()) std::this_thread::sleep_for(200ms);
        }
        KillProcessGroup(SIGKILL);
        NotifyStateChange("stopping");
    } else {
+2 −0
Original line number Diff line number Diff line
@@ -56,6 +56,8 @@
                                     // should not be killed during shutdown
#define SVC_TEMPORARY 0x1000  // This service was started by 'exec' and should be removed from the
                              // service list once it is reaped.
#define SVC_GENTLE_KILL 0x2000  // This service should be stopped with SIGTERM instead of SIGKILL
                                // Will still be SIGKILLed after timeout period of 200 ms

#define NR_SVC_SUPP_GIDS 12    // twelve supplementary groups

+6 −0
Original line number Diff line number Diff line
@@ -151,6 +151,11 @@ Result<void> ServiceParser::ParseEnterNamespace(std::vector<std::string>&& args)
    return {};
}

Result<void> ServiceParser::ParseGentleKill(std::vector<std::string>&& args) {
    service_->flags_ |= SVC_GENTLE_KILL;
    return {};
}

Result<void> ServiceParser::ParseGroup(std::vector<std::string>&& args) {
    auto gid = DecodeUid(args[1]);
    if (!gid.ok()) {
@@ -584,6 +589,7 @@ const KeywordMap<ServiceParser::OptionParser>& ServiceParser::GetParserMap() con
        {"disabled",                {0,     0,    &ServiceParser::ParseDisabled}},
        {"enter_namespace",         {2,     2,    &ServiceParser::ParseEnterNamespace}},
        {"file",                    {2,     2,    &ServiceParser::ParseFile}},
        {"gentle_kill",             {0,     0,    &ServiceParser::ParseGentleKill}},
        {"group",                   {1,     NR_SVC_SUPP_GIDS + 1, &ServiceParser::ParseGroup}},
        {"interface",               {2,     2,    &ServiceParser::ParseInterface}},
        {"ioprio",                  {2,     2,    &ServiceParser::ParseIoprio}},
+1 −0
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ class ServiceParser : public SectionParser {
    Result<void> ParseDisabled(std::vector<std::string>&& args);
    Result<void> ParseEnterNamespace(std::vector<std::string>&& args);
    Result<void> ParseGroup(std::vector<std::string>&& args);
    Result<void> ParseGentleKill(std::vector<std::string>&& args);
    Result<void> ParsePriority(std::vector<std::string>&& args);
    Result<void> ParseInterface(std::vector<std::string>&& args);
    Result<void> ParseIoprio(std::vector<std::string>&& args);