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

Commit 878560da authored by Bart Van Assche's avatar Bart Van Assche
Browse files

init_test: Fix a race condition



Wait until strace has attached to the service instead of assuming that
it has attached after one second.

Change-Id: Ifb71fa2419563e1334d8500ea867ec92121395e0
Signed-off-by: default avatarBart Van Assche <bvanassche@google.com>
parent 947d75f0
Loading
Loading
Loading
Loading
+19 −2
Original line number Original line Diff line number Diff line
@@ -14,6 +14,7 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


#include <fstream>
#include <functional>
#include <functional>
#include <string_view>
#include <string_view>
#include <thread>
#include <thread>
@@ -22,6 +23,7 @@
#include <android-base/file.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android/api-level.h>
#include <android/api-level.h>
#include <gtest/gtest.h>
#include <gtest/gtest.h>
#include <selinux/selinux.h>
#include <selinux/selinux.h>
@@ -44,6 +46,7 @@
using android::base::GetIntProperty;
using android::base::GetIntProperty;
using android::base::GetProperty;
using android::base::GetProperty;
using android::base::SetProperty;
using android::base::SetProperty;
using android::base::StringPrintf;
using android::base::StringReplace;
using android::base::StringReplace;
using android::base::WaitForProperty;
using android::base::WaitForProperty;
using namespace std::literals;
using namespace std::literals;
@@ -663,6 +666,18 @@ pid_t ForkExecvpAsync(const char* argv[]) {
    return pid;
    return pid;
}
}


pid_t TracerPid(pid_t pid) {
    static constexpr std::string_view prefix{"TracerPid:"};
    std::ifstream is(StringPrintf("/proc/%d/status", pid));
    std::string line;
    while (std::getline(is, line)) {
        if (line.find(prefix) == 0) {
            return atoi(line.substr(prefix.length()).c_str());
        }
    }
    return -1;
}

TEST(init, GentleKill) {
TEST(init, GentleKill) {
    if (getuid() != 0) {
    if (getuid() != 0) {
        GTEST_SKIP() << "Must be run as root.";
        GTEST_SKIP() << "Must be run as root.";
@@ -699,8 +714,10 @@ service test_gentle_kill /system/bin/sleep 1000
                          pid_str.c_str(),      nullptr};
                          pid_str.c_str(),      nullptr};
    pid_t strace_pid = ForkExecvpAsync(argv);
    pid_t strace_pid = ForkExecvpAsync(argv);


    // Give strace a moment to connect
    // Give strace the chance to connect
    std::this_thread::sleep_for(1s);
    while (TracerPid(pid) == 0) {
        std::this_thread::sleep_for(10ms);
    }
    service->Stop();
    service->Stop();


    int status;
    int status;