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

Commit 095792c3 authored by Luis Hector Chavez's avatar Luis Hector Chavez
Browse files

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
parent c20c8500
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -156,11 +156,6 @@ int create_jdwp_connection_fd(int jdwp_pid);

int handle_forward_request(const char* service, atransport* transport, int reply_fd);

#if !ADB_HOST
void framebuffer_service(int fd, void* cookie);
void set_verity_enabled_state_service(int fd, void* cookie);
#endif

/* packet allocator */
apacket* get_apacket(void);
void put_apacket(apacket* p);
+2 −3
Original line number Diff line number Diff line
@@ -525,12 +525,11 @@ static bool handle_sync_command(int fd, std::vector<char>& buffer) {
    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);
}
+6 −7
Original line number Diff line number Diff line
@@ -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 @@ struct fbinfo {
    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 @@ void framebuffer_service(int fd, void *cookie)
    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 @@ void framebuffer_service(int fd, void *cookie)
    }

    /* 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 @@ void framebuffer_service(int fd, void *cookie)
      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);
}
+24 −0
Original line number Diff line number Diff line
/*
 * 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_
+20 −25
Original line number Diff line number Diff line
@@ -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 @@ static void reboot_for_remount(int fd, bool need_fsck) {
    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 @@ void remount_service(int fd, void* cookie) {
        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 @@ void remount_service(int fd, void* cookie) {
    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 @@ void remount_service(int fd, void* cookie) {
    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 @@ void remount_service(int fd, void* cookie) {
        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");
    }
}
Loading