commit: c5547b30e92b23b6efba9bade55ab68019cee9ee
parent 7835f19d8a4206670491707e484afe550f2f4cdc
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 4 Aug 2023 22:51:45 +0200
cmd/chroot: New command
Note: While coreutils supports options on chroot(1), BusyBox doesn't and after
a quick search on codesearch.debian.net with `\bchroot --\w` as a regex it
seems to be very rare usage.
Diffstat:
3 files changed, 115 insertions(+), 1 deletion(-)
diff --git a/cmd/chroot.1 b/cmd/chroot.1
@@ -0,0 +1,52 @@
+.\" Collection of Unix tools, comparable to coreutils
+.\" Copyright 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+.\" SPDX-License-Identifier: MPL-2.0
+.Dd 2023-08-04
+.Dt CHROOT 1
+.Os
+.Sh NAME
+.Nm chroot
+.Nd run in another root directory
+.Sh SYNOPSIS
+.Nm
+.Ar newroot
+.Op Ar command Op Ar args ...
+.Sh DESCRIPTION
+.Nm
+runs
+.Ar command
+in the root directory at
+.Ar newroot .
+If no
+.Ar command
+was given,
+.Nm
+instead runs
+.Lc $SHELL -i
+with
+.Ev SHELL
+itself defaulting to
+.Pa /bin/sh .
+.Sh EXIT STATUS
+If
+.Ar command
+is invoked, the exit status of
+.Nm
+shall be the exit status of
+.Ar command ;
+Otherwise, the
+.Nm
+utility shall exit with one of the following values:
+.Bl -tag -width Ds
+.It 125
+An error occured in
+.Nm
+.It 126
+.Ar command
+was found but couldn't be invoked.
+.It 127
+.Ar command
+wasn't found.
+.El
+.Sh AUTHORS
+.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me
diff --git a/cmd/chroot.c b/cmd/chroot.c
@@ -0,0 +1,62 @@
+// Collection of Unix tools, comparable to coreutils
+// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-License-Identifier: MPL-2.0
+
+#define _POSIX_C_SOURCE 200809L
+#define _DEFAULT_SOURCE // chroot
+#include <assert.h> // assert
+#include <errno.h> // errno
+#include <stdbool.h> // false
+#include <stdio.h> // fprintf, perror
+#include <stdlib.h> // getenv
+#include <unistd.h> // chroot, execl, execv
+
+int
+main(int argc, char *argv[])
+{
+ if(argc < 2)
+ {
+ fprintf(stderr, "chroot: Needs arguments\n");
+ fprintf(stderr, "Usage: chroot <newroot> [command [args ...]]\n");
+ return 125;
+ }
+
+ if(chroot(argv[1]) < 0)
+ {
+ perror("chroot");
+ return 125;
+ }
+
+ if(chdir("/") < 0)
+ {
+ perror("chdir");
+ return 125;
+ }
+
+ int ret = 0;
+ errno = 0;
+ if(argc == 2)
+ {
+ char *shell = getenv("SHELL");
+ if(shell == NULL) shell = "/bin/sh";
+
+ /* flawfinder: ignore. No restrictions on commands is intended */
+ ret = execlp(shell, shell, "-i", NULL);
+ }
+ else
+ {
+ argv += 2;
+ /* flawfinder: ignore. No restrictions on commands is intended */
+ ret = execvp(argv[0], argv);
+ }
+
+ if(ret != 0)
+ {
+ perror("chroot: exec");
+ if(errno == ENOENT) return 127;
+
+ return 126;
+ }
+
+ assert(false);
+}
diff --git a/coreutils.txt b/coreutils.txt
@@ -12,7 +12,7 @@ chcon: No, SELinux-specific, wtf
chgrp: Todo
chmod: Todo
chown: Todo
-chroot: Todo
+chroot: Done
cksum: No, POSIX
comm: Maybe, POSIX
cp: Todo