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

Commit 61ffcf73 authored by Ecco Park's avatar Ecco Park
Browse files

expose the GetPidByName API of dumpstate



Bug: 32248316

This API is used by external dumpstate module
1) add unittest for GetPidByName
2) change the API name to c++ style
3) Test: dumpstate_test pass

Change-Id: I5eb80e0d1f1ef0f09b77ac3affd8a9b41d64f679
Signed-off-by: default avatarEcco Park <eccopark@google.com>
parent e0ea99cc
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#include "DumpstateUtil.h"

#include <dirent.h>
#include <fcntl.h>
#include <sys/prctl.h>
#include <sys/wait.h>
@@ -25,7 +26,10 @@

#include <vector>

#include <android-base/file.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <cutils/log.h>

#include "DumpstateInternal.h"
@@ -342,3 +346,31 @@ int RunCommandToFd(int fd, const std::string& title, const std::vector<std::stri

    return status;
}

int GetPidByName(const std::string& ps_name) {
    DIR* proc_dir;
    struct dirent* ps;
    unsigned int pid;
    std::string cmdline;

    if (!(proc_dir = opendir("/proc"))) {
        MYLOGE("Can't open /proc\n");
        return -1;
    }

    while ((ps = readdir(proc_dir))) {
        if (!(pid = atoi(ps->d_name))) {
            continue;
        }
        android::base::ReadFileToString("/proc/" + std::string(ps->d_name) + "/cmdline", &cmdline);
        if (cmdline.find(ps_name) == std::string::npos) {
            continue;
        } else {
            closedir(proc_dir);
            return pid;
        }
    }
    MYLOGE("can't find the pid\n");
    closedir(proc_dir);
    return -1;
}
+6 −0
Original line number Diff line number Diff line
@@ -174,4 +174,10 @@ int RunCommandToFd(int fd, const std::string& title, const std::vector<std::stri
 */
int DumpFileToFd(int fd, const std::string& title, const std::string& path);

/*
 * Finds the process id by process name.
 * |ps_name| the process name we want to search for
 */
int GetPidByName(const std::string& ps_name);

#endif  // FRAMEWORK_NATIVE_CMD_DUMPSTATE_UTIL_H_
+1 −28
Original line number Diff line number Diff line
@@ -189,34 +189,7 @@ static void dump_dev_files(const char *title, const char *driverpath, const char
    closedir(d);
}

// return pid of a userspace process. If not found or error, return 0.
static unsigned int pid_of_process(const char* ps_name) {
    DIR *proc_dir;
    struct dirent *ps;
    unsigned int pid;
    std::string cmdline;

    if (!(proc_dir = opendir("/proc"))) {
        MYLOGE("Can't open /proc\n");
        return 0;
    }

    while ((ps = readdir(proc_dir))) {
        if (!(pid = atoi(ps->d_name))) {
            continue;
        }
        android::base::ReadFileToString("/proc/"
                + std::string(ps->d_name) + "/cmdline", &cmdline);
        if (cmdline.find(ps_name) == std::string::npos) {
            continue;
        } else {
            closedir(proc_dir);
            return pid;
        }
    }
    closedir(proc_dir);
    return 0;
}

// dump anrd's trace and add to the zip file.
// 1. check if anrd is running on this device.
@@ -239,7 +212,7 @@ static bool dump_anrd_trace() {
    }

    // find anrd's pid if it is running.
    pid = pid_of_process("/system/xbin/anrd");
    pid = GetPidByName("/system/xbin/anrd");

    if (pid > 0) {
        if (stat(trace_path, &st) == 0) {
+20 −0
Original line number Diff line number Diff line
@@ -845,6 +845,14 @@ class DumpstateUtilTest : public DumpstateBaseTest {
        return status;
    }

    // Find out the pid of the process_name
    int FindPidOfProcess(const std::string& process_name) {
        CaptureStderr();
        int status = GetPidByName(process_name);
        err = GetCapturedStderr();
        return status;
    }

    int fd;

    // 'fd` output and `stderr` from the last command ran.
@@ -1137,3 +1145,15 @@ TEST_F(DumpstateUtilTest, DumpFileOnDryRun) {
        out, StartsWith("------ Might as well dump. Dump! (" + kTestDataPath + "single-line.txt:"));
    EXPECT_THAT(out, EndsWith("skipped on dry run\n"));
}

TEST_F(DumpstateUtilTest, FindingPidWithExistingProcess) {
    // init process always has pid 1.
    EXPECT_EQ(1, FindPidOfProcess("init"));
    EXPECT_THAT(err, IsEmpty());
}

TEST_F(DumpstateUtilTest, FindingPidWithNotExistingProcess) {
    // find the process with abnormal name.
    EXPECT_EQ(-1, FindPidOfProcess("abcdef12345-543"));
    EXPECT_THAT(err, StrEq("can't find the pid\n"));
}