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

Commit 46b85da7 authored by Felipe Leme's avatar Felipe Leme
Browse files

Lotta of small dumpstate fixes...

- Fixed RunCommandToFd() so it respects DROP_ROOT.
- Renamed enums to be more consistent with fd model.
- Added tests to RunCommandToFd() and DumpFileToFd().
- Fixed RunCommandToFd() and DumpFileToFd(), which were rushed in.
- Disable tests that fail when running as suite.

BUG: 31982882
Test: manual verification
Test: dumpstate_tests pass

Change-Id: I1d8352a17be10a707a101fc1ac9c7d735e38f9fe
parent 6f674aef
Loading
Loading
Loading
Loading
+19 −22
Original line number Diff line number Diff line
@@ -19,9 +19,9 @@
// TODO: use android::os::dumpstate (must wait until device code is refactored)

/*
 * Defines the Linux user that should be executing a command.
 * Defines the Linux account that should be executing a command.
 */
enum RootMode {
enum PrivilegeMode {
    /* Explicitly change the `uid` and `gid` to be `shell`.*/
    DROP_ROOT,
    /* Don't change the `uid` and `gid`. */
@@ -31,12 +31,12 @@ enum RootMode {
};

/*
 * Defines what should happen with the `stdout` stream of a command.
 * Defines what should happen with the main output stream (`stdout` or fd) of a command.
 */
enum StdoutMode {
    /* Don't change `stdout`. */
    NORMAL_STDOUT,
    /* Redirect `stdout` to `stderr`. */
enum OutputMode {
    /* Don't change main output. */
    NORMAL_OUTPUT,
    /* Redirect main output to `stderr`. */
    REDIRECT_TO_STDERR
};

@@ -65,8 +65,8 @@ class CommandOptions {

        int64_t timeout_;
        bool always_;
        RootMode root_mode_;
        StdoutMode stdout_mode_;
        PrivilegeMode account_mode_;
        OutputMode output_mode_;
        std::string logging_message_;

        friend class CommandOptions;
@@ -82,11 +82,11 @@ class CommandOptions {
      public:
        /* Sets the command to always run, even on `dry-run` mode. */
        CommandOptionsBuilder& Always();
        /* Sets the command's RootMode as `SU_ROOT` */
        /* Sets the command's PrivilegeMode as `SU_ROOT` */
        CommandOptionsBuilder& AsRoot();
        /* Sets the command's RootMode as `DROP_ROOT` */
        /* Sets the command's PrivilegeMode as `DROP_ROOT` */
        CommandOptionsBuilder& DropRoot();
        /* Sets the command's StdoutMode `REDIRECT_TO_STDERR` */
        /* Sets the command's OutputMode as `REDIRECT_TO_STDERR` */
        CommandOptionsBuilder& RedirectStderr();
        /* When not empty, logs a message before executing the command.
         * Must contain a `%s`, which will be replaced by the full command line, and end on `\n`. */
@@ -104,10 +104,10 @@ class CommandOptions {
    int64_t Timeout() const;
    /* Checks whether the command should always be run, even on dry-run mode. */
    bool Always() const;
    /** Gets the RootMode of the command. */
    RootMode RootMode() const;
    /** Gets the StdoutMode of the command. */
    StdoutMode StdoutMode() const;
    /** Gets the PrivilegeMode of the command. */
    PrivilegeMode PrivilegeMode() const;
    /** Gets the OutputMode of the command. */
    OutputMode OutputMode() const;
    /** Gets the logging message header, it any. */
    std::string LoggingMessage() const;

@@ -128,12 +128,9 @@ class CommandOptions {
 * |full_command| array containing the command (first entry) and its arguments.
 *                Must contain at least one element.
 * |options| optional argument defining the command's behavior.
 * |description| optional description of the command to be used on log messages. If empty,
 * the command path (without arguments) will be used instead.
 */
int RunCommandToFd(int fd, const std::vector<const char*>& full_command,
                   const CommandOptions& options = CommandOptions::DEFAULT,
                   const std::string& description = "");
int RunCommandToFd(int fd, const std::vector<std::string>& full_command,
                   const CommandOptions& options = CommandOptions::DEFAULT);

/*
 * Dumps the contents of a file into a file descriptor.
+5 −2
Original line number Diff line number Diff line
@@ -159,7 +159,7 @@ void add_mountinfo() {
    if (!ds.IsZipping()) return;
    std::string title = "MOUNT INFO";
    mount_points.clear();
    DurationReporter duration_reporter(title, nullptr);
    DurationReporter duration_reporter(title, true);
    for_each_pid(do_mountinfo, nullptr);
    MYLOGD("%s: %d entries added to zip file\n", title.c_str(), (int)mount_points.size());
}
@@ -692,6 +692,7 @@ void Dumpstate::PrintHeader() const {
    printf("Dumpstate info: id=%d pid=%d dry_run=%d args=%s extra_options=%s\n", id_, pid_,
           dry_run_, args_.c_str(), extra_options_.c_str());
    printf("\n");
    fflush(stdout);
}

// List of file extensions that can cause a zip file attachment to be rejected by some email
@@ -778,7 +779,7 @@ void Dumpstate::AddDir(const std::string& dir, bool recursive) {
        return;
    }
    MYLOGD("Adding dir %s (recursive: %d)\n", dir.c_str(), recursive);
    DurationReporter duration_reporter(dir, nullptr);
    DurationReporter duration_reporter(dir, true);
    dump_files("", dir.c_str(), recursive ? skip_none : is_dir, _add_file_from_fd);
}

@@ -1201,6 +1202,8 @@ void Dumpstate::DumpstateBoard() {
    dumpstate_device->dumpstateBoard(handle);

    AddZipEntry("dumpstate-board.txt", path);
    printf("*** See dumpstate-board.txt entry ***\n");
    fflush(stdout);

    native_handle_close(handle);
    native_handle_delete(handle);
+3 −5
Original line number Diff line number Diff line
@@ -63,18 +63,15 @@ extern "C" {
 */
class DurationReporter {
  public:
    DurationReporter(const std::string& title);
    DurationReporter(const std::string& title, FILE* out);
    DurationReporter(const std::string& title, bool log_only = false);

    ~DurationReporter();

    static uint64_t Nanotime();

  private:
    // TODO: use std::string for title, once dump_files() and other places that pass a char* are
    // refactored as well.
    std::string title_;
    FILE* out_;
    bool log_only_;
    uint64_t started_;

    DISALLOW_COPY_AND_ASSIGN(DurationReporter);
@@ -167,6 +164,7 @@ static std::string VERSION_DEFAULT = "default";
 */
class Dumpstate {
    friend class DumpstateTest;
    friend class DumpstateUtilTest;

  public:
    static CommandOptions DEFAULT_DUMPSYS;
+387 −25

File changed.

Preview size limit exceeded, changes collapsed.

+138 −101

File changed.

Preview size limit exceeded, changes collapsed.