adb: Modernize the service creation
This change removes the void* argument passing and instead uses C++11
features to avoid having to handle memory manually.
Bug: None
Test: python ./system/core/adb/test_device.py
Change-Id: I6380245b2ca583591810e3e363c67c993a107621
diff --git a/daemon/file_sync_service.cpp b/daemon/file_sync_service.cpp
index f7be8f9..0b71363 100644
--- a/daemon/file_sync_service.cpp
+++ b/daemon/file_sync_service.cpp
@@ -525,12 +525,11 @@
return true;
}
-void file_sync_service(int fd, void*) {
+void file_sync_service(android::base::unique_fd fd) {
std::vector<char> buffer(SYNC_DATA_MAX);
- while (handle_sync_command(fd, buffer)) {
+ while (handle_sync_command(fd.get(), buffer)) {
}
D("sync: done");
- adb_close(fd);
}
diff --git a/daemon/framebuffer_service.cpp b/daemon/framebuffer_service.cpp
index 20e03f9..9a620ab 100644
--- a/daemon/framebuffer_service.cpp
+++ b/daemon/framebuffer_service.cpp
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+#include "framebuffer_service.h"
+
#include <errno.h>
#include <fcntl.h>
#include <linux/fb.h>
@@ -55,8 +57,7 @@
unsigned int alpha_length;
} __attribute__((packed));
-void framebuffer_service(int fd, void *cookie)
-{
+void framebuffer_service(android::base::unique_fd fd) {
struct fbinfo fbinfo;
unsigned int i, bsize;
char buf[640];
@@ -65,7 +66,7 @@
int fds[2];
pid_t pid;
- if (pipe2(fds, O_CLOEXEC) < 0) goto pipefail;
+ if (pipe2(fds, O_CLOEXEC) < 0) return;
pid = fork();
if (pid < 0) goto done;
@@ -168,7 +169,7 @@
}
/* write header */
- if(!WriteFdExactly(fd, &fbinfo, sizeof(fbinfo))) goto done;
+ if (!WriteFdExactly(fd.get(), &fbinfo, sizeof(fbinfo))) goto done;
/* write data */
for(i = 0; i < fbinfo.size; i += bsize) {
@@ -176,13 +177,11 @@
if (i + bsize > fbinfo.size)
bsize = fbinfo.size - i;
if(!ReadFdExactly(fd_screencap, buf, bsize)) goto done;
- if(!WriteFdExactly(fd, buf, bsize)) goto done;
+ if (!WriteFdExactly(fd.get(), buf, bsize)) goto done;
}
done:
adb_close(fds[0]);
TEMP_FAILURE_RETRY(waitpid(pid, nullptr, 0));
-pipefail:
- adb_close(fd);
}
diff --git a/daemon/framebuffer_service.h b/daemon/framebuffer_service.h
new file mode 100644
index 0000000..d99c6fe
--- /dev/null
+++ b/daemon/framebuffer_service.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef _DAEMON_FRAMEBUFFER_SERVICE_H_
+#define _DAEMON_FRAMEBUFFER_SERVICE_H_
+
+#include <android-base/unique_fd.h>
+
+void framebuffer_service(android::base::unique_fd fd);
+
+#endif // _DAEMON_FRAMEBUFFER_SERVICE_H_
diff --git a/daemon/remount_service.cpp b/daemon/remount_service.cpp
index d6bba0d..dfdc365 100644
--- a/daemon/remount_service.cpp
+++ b/daemon/remount_service.cpp
@@ -44,6 +44,7 @@
#include "adb_unique_fd.h"
#include "adb_utils.h"
#include "fs_mgr.h"
+#include "set_verity_enable_state_service.h"
// Returns the device used to mount a directory in /proc/mounts.
static std::string find_proc_mount(const char* dir) {
@@ -218,14 +219,11 @@
android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd.c_str());
}
-void remount_service(int fd, void* cookie) {
- unique_fd close_fd(fd);
-
- const char* cmd = reinterpret_cast<const char*>(cookie);
- bool user_requested_reboot = cmd && !strcmp(cmd, "-R");
+void remount_service(android::base::unique_fd fd, const std::string& cmd) {
+ bool user_requested_reboot = cmd != "-R";
if (getuid() != 0) {
- WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
+ WriteFdExactly(fd.get(), "Not running as root. Try \"adb root\" first.\n");
return;
}
@@ -246,7 +244,7 @@
if (dev.empty() || !fs_has_shared_blocks(dev.c_str())) {
continue;
}
- if (can_unshare_blocks(fd, dev.c_str())) {
+ if (can_unshare_blocks(fd.get(), dev.c_str())) {
dedup.emplace(partition);
}
}
@@ -257,12 +255,12 @@
if (user_requested_reboot) {
if (!dedup.empty() || verity_enabled) {
if (verity_enabled) {
- set_verity_enabled_state_service(fd, nullptr);
+ set_verity_enabled_state_service(android::base::unique_fd(dup(fd.get())), false);
}
- reboot_for_remount(fd, !dedup.empty());
+ reboot_for_remount(fd.get(), !dedup.empty());
return;
}
- WriteFdExactly(fd, "No reboot needed, skipping -R.\n");
+ WriteFdExactly(fd.get(), "No reboot needed, skipping -R.\n");
}
// If we need to disable-verity, but we also need to perform a recovery
@@ -271,17 +269,14 @@
if (verity_enabled && dedup.empty()) {
// Allow remount but warn of likely bad effects
bool both = system_verified && vendor_verified;
- WriteFdFmt(fd,
- "dm_verity is enabled on the %s%s%s partition%s.\n",
- system_verified ? "system" : "",
- both ? " and " : "",
- vendor_verified ? "vendor" : "",
- both ? "s" : "");
- WriteFdExactly(fd,
+ WriteFdFmt(fd.get(), "dm_verity is enabled on the %s%s%s partition%s.\n",
+ system_verified ? "system" : "", both ? " and " : "",
+ vendor_verified ? "vendor" : "", both ? "s" : "");
+ WriteFdExactly(fd.get(),
"Use \"adb disable-verity\" to disable verity.\n"
"If you do not, remount may succeed, however, you will still "
"not be able to write to these volumes.\n");
- WriteFdExactly(fd,
+ WriteFdExactly(fd.get(),
"Alternately, use \"adb remount -R\" to disable verity "
"and automatically reboot.\n");
}
@@ -292,29 +287,29 @@
if (dedup.count(partition)) {
continue;
}
- success &= remount_partition(fd, partition.c_str());
+ success &= remount_partition(fd.get(), partition.c_str());
}
if (!dedup.empty()) {
- WriteFdExactly(fd,
+ WriteFdExactly(fd.get(),
"The following partitions are deduplicated and cannot "
"yet be remounted:\n");
for (const std::string& name : dedup) {
- WriteFdFmt(fd, " %s\n", name.c_str());
+ WriteFdFmt(fd.get(), " %s\n", name.c_str());
}
- WriteFdExactly(fd,
+ WriteFdExactly(fd.get(),
"To reboot and un-deduplicate the listed partitions, "
"please retry with adb remount -R.\n");
if (system_verified || vendor_verified) {
- WriteFdExactly(fd, "Note: verity will be automatically disabled after reboot.\n");
+ WriteFdExactly(fd.get(), "Note: verity will be automatically disabled after reboot.\n");
}
return;
}
if (!success) {
- WriteFdExactly(fd, "remount failed\n");
+ WriteFdExactly(fd.get(), "remount failed\n");
} else {
- WriteFdExactly(fd, "remount succeeded\n");
+ WriteFdExactly(fd.get(), "remount succeeded\n");
}
}
diff --git a/daemon/set_verity_enable_state_service.cpp b/daemon/set_verity_enable_state_service.cpp
index dbeee28..0f804e9 100644
--- a/daemon/set_verity_enable_state_service.cpp
+++ b/daemon/set_verity_enable_state_service.cpp
@@ -16,6 +16,7 @@
#define TRACE_TAG ADB
+#include "set_verity_enable_state_service.h"
#include "sysdeps.h"
#include <fcntl.h>
@@ -25,8 +26,8 @@
#include <stdio.h>
#include <sys/stat.h>
-#include "android-base/properties.h"
-#include "android-base/stringprintf.h"
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
#include <log/log_properties.h>
#include "adb.h"
@@ -131,12 +132,9 @@
return true;
}
-void set_verity_enabled_state_service(int fd, void* cookie) {
- unique_fd closer(fd);
+void set_verity_enabled_state_service(android::base::unique_fd fd, bool enable) {
bool any_changed = false;
- bool enable = (cookie != nullptr);
-
// Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
// contract, androidboot.vbmeta.digest is set by the bootloader
// when using AVB).
@@ -147,12 +145,12 @@
// VB1.0 dm-verity is only enabled on certain builds.
if (!using_avb) {
if (!kAllowDisableVerity) {
- WriteFdFmt(fd, "%s-verity only works for userdebug builds\n",
+ WriteFdFmt(fd.get(), "%s-verity only works for userdebug builds\n",
enable ? "enable" : "disable");
}
if (!android::base::GetBoolProperty("ro.secure", false)) {
- WriteFdFmt(fd, "verity not enabled - ENG build\n");
+ WriteFdFmt(fd.get(), "verity not enabled - ENG build\n");
return;
}
}
@@ -160,7 +158,7 @@
// Should never be possible to disable dm-verity on a USER build
// regardless of using AVB or VB1.0.
if (!__android_log_is_debuggable()) {
- WriteFdFmt(fd, "verity cannot be disabled/enabled - USER build\n");
+ WriteFdFmt(fd.get(), "verity cannot be disabled/enabled - USER build\n");
return;
}
@@ -168,10 +166,10 @@
// Yep, the system is using AVB.
AvbOps* ops = avb_ops_user_new();
if (ops == nullptr) {
- WriteFdFmt(fd, "Error getting AVB ops\n");
+ WriteFdFmt(fd.get(), "Error getting AVB ops\n");
return;
}
- if (set_avb_verity_enabled_state(fd, ops, enable)) {
+ if (set_avb_verity_enabled_state(fd.get(), ops, enable)) {
any_changed = true;
}
avb_ops_user_free(ops);
@@ -181,14 +179,14 @@
// read all fstab entries at once from all sources
fstab = fs_mgr_read_fstab_default();
if (!fstab) {
- WriteFdFmt(fd, "Failed to read fstab\nMaybe run adb root?\n");
+ WriteFdFmt(fd.get(), "Failed to read fstab\nMaybe run adb root?\n");
return;
}
// Loop through entries looking for ones that vold manages.
for (int i = 0; i < fstab->num_entries; i++) {
if (fs_mgr_is_verified(&fstab->recs[i])) {
- if (set_verity_enabled_state(fd, fstab->recs[i].blk_device,
+ if (set_verity_enabled_state(fd.get(), fstab->recs[i].blk_device,
fstab->recs[i].mount_point, enable)) {
any_changed = true;
}
@@ -197,6 +195,6 @@
}
if (any_changed) {
- WriteFdFmt(fd, "Now reboot your device for settings to take effect\n");
+ WriteFdFmt(fd.get(), "Now reboot your device for settings to take effect\n");
}
}
diff --git a/daemon/set_verity_enable_state_service.h b/daemon/set_verity_enable_state_service.h
new file mode 100644
index 0000000..9f84f35
--- /dev/null
+++ b/daemon/set_verity_enable_state_service.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#ifndef _DAEMON_SET_VERITY_ENABLED_STATE_SERVICE_H_
+#define _DAEMON_SET_VERITY_ENABLED_STATE_SERVICE_H_
+
+#include <android-base/unique_fd.h>
+
+void set_verity_enabled_state_service(android::base::unique_fd fd, bool enable);
+
+#endif // _DAEMON_SET_VERITY_ENABLED_STATE_SERVICE_H_