Loading init/Android.mk +1 −0 Original line number Diff line number Diff line Loading @@ -145,6 +145,7 @@ LOCAL_SRC_FILES := \ init_parser_test.cpp \ init_test.cpp \ property_service_test.cpp \ service_test.cpp \ util_test.cpp \ LOCAL_SHARED_LIBRARIES += \ Loading init/service.cpp +2 −0 Original line number Diff line number Diff line Loading @@ -158,6 +158,7 @@ Service::Service(const std::string& name, const std::vector<std::string>& args) namespace_flags_(0), seclabel_(""), onrestart_(false, "<Service '" + name + "' onrestart>", 0), keychord_id_(0), ioprio_class_(IoSchedClass_NONE), ioprio_pri_(0), priority_(0), Loading @@ -182,6 +183,7 @@ Service::Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, namespace_flags_(namespace_flags), seclabel_(seclabel), onrestart_(false, "<Service '" + name + "' onrestart>", 0), keychord_id_(0), ioprio_class_(IoSchedClass_NONE), ioprio_pri_(0), priority_(0), Loading init/service.h +9 −3 Original line number Diff line number Diff line Loading @@ -94,14 +94,19 @@ class Service { const std::set<std::string>& classnames() const { return classnames_; } unsigned flags() const { return flags_; } pid_t pid() const { return pid_; } int crash_count() const { return crash_count_; } uid_t uid() const { return uid_; } gid_t gid() const { return gid_; } int priority() const { return priority_; } unsigned namespace_flags() const { return namespace_flags_; } const std::vector<gid_t>& supp_gids() const { return supp_gids_; } const std::string& seclabel() const { return seclabel_; } const std::vector<int>& keycodes() const { return keycodes_; } int keychord_id() const { return keychord_id_; } void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; } IoSchedClass ioprio_class() const { return ioprio_class_; } int ioprio_pri() const { return ioprio_pri_; } int priority() const { return priority_; } int oom_score_adjust() const { return oom_score_adjust_; } const std::vector<std::string>& args() const { return args_; } private: Loading Loading @@ -181,6 +186,9 @@ class ServiceManager { public: static ServiceManager& GetInstance(); // Exposed for testing ServiceManager(); void AddService(std::unique_ptr<Service> service); Service* MakeExecOneshotService(const std::vector<std::string>& args); bool Exec(const std::vector<std::string>& args); Loading @@ -199,8 +207,6 @@ public: void DumpState() const; private: ServiceManager(); // Cleans up a child process that exited. // Returns true iff a children was cleaned up. bool ReapOneProcess(); Loading init/service_test.cpp 0 → 100644 +67 −0 Original line number Diff line number Diff line /* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "service.h" #include <algorithm> #include <memory> #include <type_traits> #include <vector> #include <gtest/gtest.h> TEST(service, pod_initialized) { constexpr auto memory_size = sizeof(Service); alignas(alignof(Service)) char old_memory[memory_size]; for (std::size_t i = 0; i < memory_size; ++i) { old_memory[i] = 0xFF; } std::vector<std::string> dummy_args{"/bin/test"}; Service* service_in_old_memory = new (old_memory) Service("test_old_memory", dummy_args); EXPECT_EQ(0U, service_in_old_memory->flags()); EXPECT_EQ(0, service_in_old_memory->pid()); EXPECT_EQ(0, service_in_old_memory->crash_count()); EXPECT_EQ(0U, service_in_old_memory->uid()); EXPECT_EQ(0U, service_in_old_memory->gid()); EXPECT_EQ(0U, service_in_old_memory->namespace_flags()); EXPECT_EQ(0, service_in_old_memory->keychord_id()); EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory->ioprio_class()); EXPECT_EQ(0, service_in_old_memory->ioprio_pri()); EXPECT_EQ(0, service_in_old_memory->priority()); EXPECT_EQ(-1000, service_in_old_memory->oom_score_adjust()); for (std::size_t i = 0; i < memory_size; ++i) { old_memory[i] = 0xFF; } Service* service_in_old_memory2 = new (old_memory) Service("test_old_memory", 0U, 0U, 0U, std::vector<gid_t>(), CapSet(), 0U, "", dummy_args); EXPECT_EQ(0U, service_in_old_memory2->flags()); EXPECT_EQ(0, service_in_old_memory2->pid()); EXPECT_EQ(0, service_in_old_memory2->crash_count()); EXPECT_EQ(0U, service_in_old_memory2->uid()); EXPECT_EQ(0U, service_in_old_memory2->gid()); EXPECT_EQ(0U, service_in_old_memory2->namespace_flags()); EXPECT_EQ(0, service_in_old_memory2->keychord_id()); EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory2->ioprio_class()); EXPECT_EQ(0, service_in_old_memory2->ioprio_pri()); EXPECT_EQ(0, service_in_old_memory2->priority()); EXPECT_EQ(-1000, service_in_old_memory2->oom_score_adjust()); } Loading
init/Android.mk +1 −0 Original line number Diff line number Diff line Loading @@ -145,6 +145,7 @@ LOCAL_SRC_FILES := \ init_parser_test.cpp \ init_test.cpp \ property_service_test.cpp \ service_test.cpp \ util_test.cpp \ LOCAL_SHARED_LIBRARIES += \ Loading
init/service.cpp +2 −0 Original line number Diff line number Diff line Loading @@ -158,6 +158,7 @@ Service::Service(const std::string& name, const std::vector<std::string>& args) namespace_flags_(0), seclabel_(""), onrestart_(false, "<Service '" + name + "' onrestart>", 0), keychord_id_(0), ioprio_class_(IoSchedClass_NONE), ioprio_pri_(0), priority_(0), Loading @@ -182,6 +183,7 @@ Service::Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, namespace_flags_(namespace_flags), seclabel_(seclabel), onrestart_(false, "<Service '" + name + "' onrestart>", 0), keychord_id_(0), ioprio_class_(IoSchedClass_NONE), ioprio_pri_(0), priority_(0), Loading
init/service.h +9 −3 Original line number Diff line number Diff line Loading @@ -94,14 +94,19 @@ class Service { const std::set<std::string>& classnames() const { return classnames_; } unsigned flags() const { return flags_; } pid_t pid() const { return pid_; } int crash_count() const { return crash_count_; } uid_t uid() const { return uid_; } gid_t gid() const { return gid_; } int priority() const { return priority_; } unsigned namespace_flags() const { return namespace_flags_; } const std::vector<gid_t>& supp_gids() const { return supp_gids_; } const std::string& seclabel() const { return seclabel_; } const std::vector<int>& keycodes() const { return keycodes_; } int keychord_id() const { return keychord_id_; } void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; } IoSchedClass ioprio_class() const { return ioprio_class_; } int ioprio_pri() const { return ioprio_pri_; } int priority() const { return priority_; } int oom_score_adjust() const { return oom_score_adjust_; } const std::vector<std::string>& args() const { return args_; } private: Loading Loading @@ -181,6 +186,9 @@ class ServiceManager { public: static ServiceManager& GetInstance(); // Exposed for testing ServiceManager(); void AddService(std::unique_ptr<Service> service); Service* MakeExecOneshotService(const std::vector<std::string>& args); bool Exec(const std::vector<std::string>& args); Loading @@ -199,8 +207,6 @@ public: void DumpState() const; private: ServiceManager(); // Cleans up a child process that exited. // Returns true iff a children was cleaned up. bool ReapOneProcess(); Loading
init/service_test.cpp 0 → 100644 +67 −0 Original line number Diff line number Diff line /* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "service.h" #include <algorithm> #include <memory> #include <type_traits> #include <vector> #include <gtest/gtest.h> TEST(service, pod_initialized) { constexpr auto memory_size = sizeof(Service); alignas(alignof(Service)) char old_memory[memory_size]; for (std::size_t i = 0; i < memory_size; ++i) { old_memory[i] = 0xFF; } std::vector<std::string> dummy_args{"/bin/test"}; Service* service_in_old_memory = new (old_memory) Service("test_old_memory", dummy_args); EXPECT_EQ(0U, service_in_old_memory->flags()); EXPECT_EQ(0, service_in_old_memory->pid()); EXPECT_EQ(0, service_in_old_memory->crash_count()); EXPECT_EQ(0U, service_in_old_memory->uid()); EXPECT_EQ(0U, service_in_old_memory->gid()); EXPECT_EQ(0U, service_in_old_memory->namespace_flags()); EXPECT_EQ(0, service_in_old_memory->keychord_id()); EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory->ioprio_class()); EXPECT_EQ(0, service_in_old_memory->ioprio_pri()); EXPECT_EQ(0, service_in_old_memory->priority()); EXPECT_EQ(-1000, service_in_old_memory->oom_score_adjust()); for (std::size_t i = 0; i < memory_size; ++i) { old_memory[i] = 0xFF; } Service* service_in_old_memory2 = new (old_memory) Service("test_old_memory", 0U, 0U, 0U, std::vector<gid_t>(), CapSet(), 0U, "", dummy_args); EXPECT_EQ(0U, service_in_old_memory2->flags()); EXPECT_EQ(0, service_in_old_memory2->pid()); EXPECT_EQ(0, service_in_old_memory2->crash_count()); EXPECT_EQ(0U, service_in_old_memory2->uid()); EXPECT_EQ(0U, service_in_old_memory2->gid()); EXPECT_EQ(0U, service_in_old_memory2->namespace_flags()); EXPECT_EQ(0, service_in_old_memory2->keychord_id()); EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory2->ioprio_class()); EXPECT_EQ(0, service_in_old_memory2->ioprio_pri()); EXPECT_EQ(0, service_in_old_memory2->priority()); EXPECT_EQ(-1000, service_in_old_memory2->oom_score_adjust()); }