commit: 09a8aedbd5f3ca21ebd271b4b90118a14f9e8d94
parent 33df680666854a4cddcf6e2f6822ef31e33bec30
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Mon, 20 May 2024 22:06:29 +0200
mount-stub.c: Add
Diffstat:
3 files changed, 89 insertions(+), 1 deletion(-)
diff --git a/init.sh b/init.sh
@@ -26,7 +26,7 @@ build_awk() {
}
build_stubs() {
- for i in ls grep cp getty
+ for i in ls grep cp getty mount
do
$CC $CFLAGS -o "/bin/$i" "/${i}-stub.c" || die "Failed compiling $i stub"
done
diff --git a/make-root.sh b/make-root.sh
@@ -40,6 +40,7 @@ local_files="
grep-stub.c
cp-stub.c
getty-stub.c
+ mount-stub.c
bootstrap-bash.sh
bootstrap-make.sh
bootstrap-xz.sh
diff --git a/mount-stub.c b/mount-stub.c
@@ -0,0 +1,87 @@
+// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-License-Identifier: MPL-2.0
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <getopt.h> // getopt_long
+#include <stdio.h> // fprintf
+#include <string.h> // strerror
+#include <sys/mount.h> // mount
+
+static void
+usage()
+{
+ fprintf(stderr, "\
+Usage: mount [-t fstype] device mountpoint\n\
+ mount [--bind|--rbind|--move] source destination\n\
+");
+}
+
+int
+main(int argc, char *argv[])
+{
+ char *fstype = NULL;
+ int mnt_flags = 0;
+ int opt_bind_move = 0;
+ struct option longopts[] = {
+ /* options exclusive to another, so mnt_flags can be set directly */
+ {"bind", no_argument, &opt_bind_move, MS_BIND},
+ {"rbind", no_argument, &opt_bind_move, MS_BIND | MS_REC},
+ {"move", no_argument, &opt_bind_move, MS_MOVE},
+ };
+
+ int c = -1;
+ while((c = getopt_long(argc, argv, ":t:", longopts, NULL)) != -1)
+ {
+ switch(c)
+ {
+ case 0:
+ break;
+ case 't':
+ fstype = optarg;
+ break;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if(opt_bind_move != 0)
+ {
+ if(argc != 2)
+ {
+ fprintf(stderr, "mount: Expected 2 arguments, got %d\n", argc);
+ usage();
+ return 1;
+ }
+ char *src = argv[0];
+ char *dest = argv[1];
+
+ int ret = mount(src, dest, NULL, opt_bind_move, NULL);
+ if(ret != 0)
+ {
+ fprintf(stderr, "mount: Failed mounting %s to %s: %s\n", src, dest, strerror(errno));
+ return 1;
+ }
+
+ return 0;
+ }
+
+ if(argc != 2)
+ {
+ fprintf(stderr, "mount: Expected 2 arguments, got %d\n", argc);
+ usage();
+ return 1;
+ }
+ char *src = argv[0];
+ char *dest = argv[1];
+
+ int ret = mount(src, dest, fstype, mnt_flags, NULL);
+ if(ret != 0)
+ {
+ fprintf(stderr, "mount: Failed mounting %s to %s: %s\n", src, dest, strerror(errno));
+ return 1;
+ }
+
+ return 0;
+}