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

Commit 2cbbe9f7 authored by Tom Cherry's avatar Tom Cherry
Browse files

init: do not log directly from read_file() and write_file()

Their callers may be able to add more context, so use an error string
to record the error.

Bug: 38038887
Test: boot bullhead
Test: Init unit tests
Change-Id: I46690d1c66e00a4b15cadc6fd0d6b50e990388c3
parent 517e1f17
Loading
Loading
Loading
Loading
+27 −6
Original line number Diff line number Diff line
@@ -150,7 +150,12 @@ static int do_class_restart(const std::vector<std::string>& args) {
}

static int do_domainname(const std::vector<std::string>& args) {
    return write_file("/proc/sys/kernel/domainname", args[1]) ? 0 : 1;
    std::string err;
    if (!WriteFile("/proc/sys/kernel/domainname", args[1], &err)) {
        LOG(ERROR) << err;
        return -1;
    }
    return 0;
}

static int do_enable(const std::vector<std::string>& args) {
@@ -174,7 +179,12 @@ static int do_export(const std::vector<std::string>& args) {
}

static int do_hostname(const std::vector<std::string>& args) {
    return write_file("/proc/sys/kernel/hostname", args[1]) ? 0 : 1;
    std::string err;
    if (!WriteFile("/proc/sys/kernel/hostname", args[1], &err)) {
        LOG(ERROR) << err;
        return -1;
    }
    return 0;
}

static int do_ifup(const std::vector<std::string>& args) {
@@ -651,15 +661,26 @@ static int do_verity_update_state(const std::vector<std::string>& args) {
}

static int do_write(const std::vector<std::string>& args) {
    return write_file(args[1], args[2]) ? 0 : 1;
    std::string err;
    if (!WriteFile(args[1], args[2], &err)) {
        LOG(ERROR) << err;
        return -1;
    }
    return 0;
}

static int do_copy(const std::vector<std::string>& args) {
    std::string data;
    if (read_file(args[1], &data)) {
        return write_file(args[2], data) ? 0 : 1;
    std::string err;
    if (!ReadFile(args[1], &data, &err)) {
        LOG(ERROR) << err;
        return -1;
    }
    if (!WriteFile(args[2], data, &err)) {
        LOG(ERROR) << err;
        return -1;
    }
    return 1;
    return 0;
}

static int do_chown(const std::vector<std::string>& args) {
+3 −1
Original line number Diff line number Diff line
@@ -853,7 +853,9 @@ static void selinux_initialize(bool in_kernel_domain) {
            }
        }

        if (!write_file("/sys/fs/selinux/checkreqprot", "0")) {
        std::string err;
        if (!WriteFile("/sys/fs/selinux/checkreqprot", "0", &err)) {
            LOG(ERROR) << err;
            security_failure();
        }

+3 −1
Original line number Diff line number Diff line
@@ -110,7 +110,9 @@ bool Parser::ParseConfigFile(const std::string& path) {
    LOG(INFO) << "Parsing file " << path << "...";
    Timer t;
    std::string data;
    if (!read_file(path, &data)) {
    std::string err;
    if (!ReadFile(path, &data, &err)) {
        LOG(ERROR) << err;
        return false;
    }

+4 −3
Original line number Diff line number Diff line
@@ -152,10 +152,11 @@ TEST(init, EventTriggerOrderMultipleFiles) {
                               "on boot\n"
                               "execute 3";
    // clang-format on
    // write_file() ensures the right mode is set
    ASSERT_TRUE(write_file(std::string(dir.path) + "/a.rc", dir_a_script));
    // WriteFile() ensures the right mode is set
    std::string err;
    ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script, &err));

    ASSERT_TRUE(write_file(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
    ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5", &err));

    // clang-format off
    std::string start_script = "import " + std::string(first_import.path) + "\n"
+3 −2
Original line number Diff line number Diff line
@@ -510,8 +510,9 @@ static void load_properties(char *data, const char *filter)
static void load_properties_from_file(const char* filename, const char* filter) {
    Timer t;
    std::string data;
    if (!read_file(filename, &data)) {
        PLOG(WARNING) << "Couldn't load properties from " << filename;
    std::string err;
    if (!ReadFile(filename, &data, &err)) {
        PLOG(WARNING) << "Couldn't load property file: " << err;
        return;
    }
    data.push_back('\n');
Loading