summaryrefslogtreecommitdiff
path: root/init/builtins.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'init/builtins.cpp')
-rw-r--r--init/builtins.cpp58
1 files changed, 27 insertions, 31 deletions
diff --git a/init/builtins.cpp b/init/builtins.cpp
index a1eab06f1..2d4d78bc7 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -151,9 +151,8 @@ static int do_class_restart(const std::vector<std::string>& args) {
}
static int do_domainname(const std::vector<std::string>& args) {
- std::string err;
- if (!WriteFile("/proc/sys/kernel/domainname", args[1], &err)) {
- LOG(ERROR) << err;
+ if (auto result = WriteFile("/proc/sys/kernel/domainname", args[1]); !result) {
+ LOG(ERROR) << "Unable to write to /proc/sys/kernel/domainname: " << result.error();
return -1;
}
return 0;
@@ -199,9 +198,8 @@ static int do_export(const std::vector<std::string>& args) {
}
static int do_hostname(const std::vector<std::string>& args) {
- std::string err;
- if (!WriteFile("/proc/sys/kernel/hostname", args[1], &err)) {
- LOG(ERROR) << err;
+ if (auto result = WriteFile("/proc/sys/kernel/hostname", args[1]); !result) {
+ LOG(ERROR) << "Unable to write to /proc/sys/kernel/hostname: " << result.error();
return -1;
}
return 0;
@@ -244,22 +242,22 @@ static int do_mkdir(const std::vector<std::string>& args) {
}
if (args.size() >= 4) {
- uid_t uid;
- std::string decode_uid_err;
- if (!DecodeUid(args[3], &uid, &decode_uid_err)) {
- LOG(ERROR) << "Unable to find UID for '" << args[3] << "': " << decode_uid_err;
+ auto uid = DecodeUid(args[3]);
+ if (!uid) {
+ LOG(ERROR) << "Unable to decode UID for '" << args[3] << "': " << uid.error();
return -1;
}
- gid_t gid = -1;
+ Result<gid_t> gid = -1;
if (args.size() == 5) {
- if (!DecodeUid(args[4], &gid, &decode_uid_err)) {
- LOG(ERROR) << "Unable to find GID for '" << args[3] << "': " << decode_uid_err;
+ gid = DecodeUid(args[4]);
+ if (!gid) {
+ LOG(ERROR) << "Unable to decode GID for '" << args[3] << "': " << gid.error();
return -1;
}
}
- if (lchown(args[1].c_str(), uid, gid) == -1) {
+ if (lchown(args[1].c_str(), *uid, *gid) == -1) {
return -errno;
}
@@ -668,9 +666,8 @@ static int do_verity_update_state(const std::vector<std::string>& args) {
}
static int do_write(const std::vector<std::string>& args) {
- std::string err;
- if (!WriteFile(args[1], args[2], &err)) {
- LOG(ERROR) << err;
+ if (auto result = WriteFile(args[1], args[2]); !result) {
+ LOG(ERROR) << "Unable to write to file '" << args[1] << "': " << result.error();
return -1;
}
return 0;
@@ -737,39 +734,38 @@ static int do_readahead(const std::vector<std::string>& args) {
}
static int do_copy(const std::vector<std::string>& args) {
- std::string data;
- std::string err;
- if (!ReadFile(args[1], &data, &err)) {
- LOG(ERROR) << err;
+ auto file_contents = ReadFile(args[1]);
+ if (!file_contents) {
+ LOG(ERROR) << "Could not read input file '" << args[1] << "': " << file_contents.error();
return -1;
}
- if (!WriteFile(args[2], data, &err)) {
- LOG(ERROR) << err;
+ if (auto result = WriteFile(args[2], *file_contents); !result) {
+ LOG(ERROR) << "Could not write to output file '" << args[2] << "': " << result.error();
return -1;
}
return 0;
}
static int do_chown(const std::vector<std::string>& args) {
- uid_t uid;
- std::string decode_uid_err;
- if (!DecodeUid(args[1], &uid, &decode_uid_err)) {
- LOG(ERROR) << "Unable to find UID for '" << args[1] << "': " << decode_uid_err;
+ auto uid = DecodeUid(args[1]);
+ if (!uid) {
+ LOG(ERROR) << "Unable to decode UID for '" << args[1] << "': " << uid.error();
return -1;
}
// GID is optional and pushes the index of path out by one if specified.
const std::string& path = (args.size() == 4) ? args[3] : args[2];
- gid_t gid = -1;
+ Result<gid_t> gid = -1;
if (args.size() == 4) {
- if (!DecodeUid(args[2], &gid, &decode_uid_err)) {
- LOG(ERROR) << "Unable to find GID for '" << args[2] << "': " << decode_uid_err;
+ gid = DecodeUid(args[2]);
+ if (!gid) {
+ LOG(ERROR) << "Unable to decode GID for '" << args[2] << "': " << gid.error();
return -1;
}
}
- if (lchown(path.c_str(), uid, gid) == -1) return -errno;
+ if (lchown(path.c_str(), *uid, *gid) == -1) return -errno;
return 0;
}