~mser/pkg.mser.at

434e4be8d02dd2a9bc9869fffc1be87a77b9a01f — cryzed 2 years ago 669ef65
unshare-net: Improve logging
2 files changed, 8 insertions(+), 8 deletions(-)

M packages/unshare-net/PKGBUILD
M packages/unshare-net/unshare-net
M packages/unshare-net/PKGBUILD => packages/unshare-net/PKGBUILD +1 -1
@@ 1,6 1,6 @@
# Maintainer: Michael Serajnik <m at mser dot at>
pkgname=unshare-net
pkgver=13
pkgver=14
pkgrel=1
pkgdesc='Selectively whitelist traffic to specified IPs and domains for target applications'
arch=('any')

M packages/unshare-net/unshare-net => packages/unshare-net/unshare-net +7 -7
@@ 50,20 50,20 @@ class ExitCode(enum.IntEnum):

def cgroup_create(name: str, mount_path: pathlib.Path) -> pathlib.Path:
    path = mount_path / name
    logger.debug("creating: %r", str(path))
    logger.debug("Creating cgroup %r", str(path))
    path.mkdir()
    return path


def cgroup_remove(name: str, mount_path: pathlib.Path) -> None:
    path = mount_path / name
    logger.debug("removing %r", str(path))
    logger.debug("Removing cgroup %r", str(path))
    path.rmdir()


def cgroup_add_process(name: str, pid: int, mount_path: pathlib.Path) -> None:
    path = mount_path / name / "cgroup.procs"
    logger.debug("%d -> %r", pid, str(path))
    logger.debug("Adding PID %d to cgroup %r", pid, str(path))
    path.write_text(str(pid), encoding="ascii")




@@ 76,7 76,7 @@ def shell_escape(command: abc.Iterable[str]) -> str:


def run_command(command: abc.Sequence[str], **run_kwargs: T.Any) -> subprocess.CompletedProcess:
    logger.debug(shell_escape(command))
    logger.debug("Running %r", shell_escape(command))
    return subprocess.run(command, **run_kwargs)




@@ 172,7 172,7 @@ def run(arguments: argparse.Namespace, command: list[str]) -> ExitCode:
        cgroup_mount_path = cgroup_mount_paths[0]
    else:
        cgroup_mount_path = pathlib.Path(tempfile.mkdtemp(prefix=f"{APPLICATION_NAME}-"))
        logger.debug("created %r", str(cgroup_mount_path))
        logger.debug("Created cgroup mount path %r", str(cgroup_mount_path))
        # These are the mount options used by Arch Linux (systemd), so I assume they are fine
        mount("--types", "cgroup2", identifier, str(cgroup_mount_path), "--options", "defaults,nosuid,nodev,noexec")
        cgroup_mount_created = True


@@ 222,7 222,7 @@ def run(arguments: argparse.Namespace, command: list[str]) -> ExitCode:

    # We use --session-command so we can spawn a shell with job control too
    su_command = "su", arguments.user, "--session-command", *command
    logger.debug(shell_escape(su_command))
    logger.debug("Running %r", shell_escape(su_command))
    process = subprocess.Popen(su_command)
    cgroup_add_process(identifier, process.pid, cgroup_mount_path)



@@ 249,7 249,7 @@ def run(arguments: argparse.Namespace, command: list[str]) -> ExitCode:
    cgroup_remove(identifier, cgroup_mount_path)

    if cgroup_mount_created:
        logger.debug("removing %r", str(cgroup_mount_path))
        logger.debug("Removing cgroup mount path %r", str(cgroup_mount_path))
        umount(cgroup_mount_path)
        cgroup_mount_path.rmdir()