Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove mandatory busybox injection #24

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion binharness/common/busybox.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def install(self: BusyboxInjection, environment: Environment) -> None:
self._environment = environment
else:
raise
environment.busybox_injection = self

def mktemp(
self: BusyboxInjection, directory: bool = False # noqa: FBT001, FBT002
Expand Down
13 changes: 8 additions & 5 deletions binharness/common/qemu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Generator
from typing import TYPE_CHECKING, Generator, cast

from binharness.types.executor import InjectableExecutor
from binharness.types.injection import ExecutableInjection
from binharness.util import read_lines
from binharness.types.io import IO
from binharness.util import generate_random_suffix, read_lines

if TYPE_CHECKING:
from binharness import Process, Target
Expand Down Expand Up @@ -35,7 +36,9 @@ def run_with_log(
cwd: Path | None = None,
) -> tuple[Process, Generator[bytes, None, None]]:
"""Run the injection in the environment and return a log generator."""
logfile = self.environment.busybox_injection.mktemp()
logfile = (
self.environment.get_tempdir() / f"qemu-{generate_random_suffix()}.log"
)
proc = self.run(
"-D",
str(logfile),
Expand All @@ -45,8 +48,8 @@ def run_with_log(
)

def log_generator() -> Generator[bytes, None, None]:
cat_proc = self.environment.busybox_injection.cat(logfile)
yield from read_lines(cat_proc.stdout)
file = self.environment.open_file(logfile, "rb")
yield from read_lines(cast(IO[bytes], file))

# Cleanup log file
self.environment.run_command("rm", logfile)
Expand Down
27 changes: 1 addition & 26 deletions binharness/types/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
if TYPE_CHECKING:
from pathlib import Path

from binharness import IO, BusyboxInjection, Process


class BusyboxInjectionNotInstalledError(Exception):
"""BusyBox is not installed in the environment."""
from binharness import IO, Process


class Environment(ABC):
Expand All @@ -22,27 +18,6 @@ class Environment(ABC):
well as running commands.
"""

_busybox_injection: BusyboxInjection | None

def __init__(self: Environment) -> None:
"""Create an Environment."""
self._busybox_injection = None

@property
def busybox_injection(self: Environment) -> BusyboxInjection:
"""Return the BusyboxInjection for the environment."""
if self._busybox_injection is None:
raise BusyboxInjectionNotInstalledError
return self._busybox_injection

@busybox_injection.setter
def busybox_injection(
self: Environment,
busybox_injection: BusyboxInjection,
) -> None:
"""Set the BusyboxInjection for the environment."""
self._busybox_injection = busybox_injection

@abstractmethod
def run_command(
self: Environment,
Expand Down
17 changes: 7 additions & 10 deletions binharness/types/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import TYPE_CHECKING

from binharness.types.environment import BusyboxInjectionNotInstalledError
from binharness.util import generate_random_suffix

if TYPE_CHECKING:
from pathlib import Path
Expand Down Expand Up @@ -47,16 +47,13 @@ def install(self: Injection, environment: Environment) -> None:
if self._environment is not None:
raise InjectionAlreadyInstalledError
if self.env_path is None:
try:
bb_injection = environment.busybox_injection
except BusyboxInjectionNotInstalledError:
from binharness.common.busybox import BusyboxInjection
self.env_path = environment.get_tempdir() / (
f"{self.host_path.name}-{generate_random_suffix()}.bh-inj"
)

bb_injection = BusyboxInjection()
bb_injection.install(environment)
self.env_path = bb_injection.mktemp(directory=True)

environment.inject_files([(self.host_path, self.env_path)])
environment.inject_files(
[(self.host_path, self.env_path / self.host_path.name)]
)
self._environment = environment

def is_installed(self: Injection) -> bool:
Expand Down
7 changes: 7 additions & 0 deletions binharness/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""binharness.util - Utility functions for binharness."""
from __future__ import annotations

import random
import shlex
import string
from pathlib import Path
from typing import TYPE_CHECKING, Generator, Sequence

Expand Down Expand Up @@ -43,3 +45,8 @@ def read_lines(file: IO[bytes]) -> Generator[bytes, None, None]:
while b"\n" in buffer:
line, buffer = buffer.split(b"\n", 1)
yield line + b"\n"


def generate_random_suffix(n: int = 6) -> str:
"""Generate a random suffix."""
return "".join(random.choices(string.ascii_letters, k=n)) # noqa: S311