mount and umount commands

This commit is contained in:
Mark
2020-01-15 22:36:46 +02:00
parent 2fef647007
commit 8b6d76abae
4 changed files with 120 additions and 16 deletions
+3 -1
View File
@@ -12,7 +12,9 @@ STAGE_BIN=$(STAGE)/init \
$(STAGE)/bin/ls \
$(STAGE)/bin/reboot \
$(STAGE)/bin/date \
$(STAGE)/bin/uname
$(STAGE)/bin/uname \
$(STAGE)/bin/mount \
$(STAGE)/bin/umount
usr_CFLAGS=-msse \
-msse2 \
+90
View File
@@ -0,0 +1,90 @@
#include <stdio.h>
static void usage(void) {
printf("Usage: mount [-t type] device target\n");
}
int main(int argc, char **argv) {
int res;
struct stat st;
const char *type = NULL;
const char *src = NULL;
const char *dst = NULL;
uid_t uid;
// Crude attempt at getopt()
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 't':
if (i == argc - 1) {
usage();
return -1;
}
type = argv[i + 1];
++i;
break;
default:
return -1;
}
continue;
}
if (!src) {
src = argv[i];
} else if (!dst) {
dst = argv[i];
} else {
usage();
return -1;
}
}
if (!dst) {
dst = src;
src = NULL;
}
if (!dst) {
usage();
return -1;
}
// Check that UID is 0
if ((uid = getuid()) != 0) {
printf("mount should be run as root\n");
return -1;
}
if (src) {
// Check that "src" is a block device
if ((res = stat(src, &st)) != 0) {
perror(src);
return -1;
}
if ((st.st_mode & S_IFMT) != S_IFBLK) {
printf("%s: Not a block device\n", src);
return -1;
}
}
// Check that "dst" is a directory
if ((res = stat(dst, &st)) != 0) {
perror(dst);
return -1;
}
if ((st.st_mode & S_IFMT) != S_IFDIR) {
printf("%s: Not a directory\n", dst);
return -1;
}
// Do the magic stuff
if ((res = mount(src, dst, type, 0, NULL)) != 0) {
perror("mount()");
return -1;
}
return 0;
}
+17
View File
@@ -0,0 +1,17 @@
#include <stdio.h>
int main(int argc, char **argv) {
int res;
if (argc != 2) {
printf("Usage: umount dir\n");
return -1;
}
if ((res = umount(argv[1])) != 0) {
perror(argv[1]);
return -1;
}
return 0;
}
+10 -15
View File
@@ -550,7 +550,11 @@ static int b_dir(const char *arg) {
}
static int b_abort(const char *arg) {
printf("Before SIGUSR1\n");
raise(SIGUSR1);
printf("After SIGUSR1\n");
abort();
printf("This code should not run\n");
return 0;
}
@@ -832,32 +836,23 @@ static int cmd_exec(const char *line) {
return -1;
}
static void sigusr1_handler(int signum) {
printf("SIGUSR1 received and handled\n");
}
int main(int argc, char **argv) {
if (getpid() != 1) {
printf("Won't work if PID is not 1\n");
return -1;
}
signal(SIGUSR1, sigusr1_handler);
char linebuf[512];
char c;
size_t l = 0;
int res;
// Try to mount devfs
const char *automount_dev = "/dev/sda1";
if ((res = mount(NULL, "/dev", "devfs", 0, NULL)) != 0) {
perror("Failed to mount devfs");
}
// Mount sda1 if present
if (access(automount_dev, F_OK) == 0) {
printf("%s exists, trying to mount at /mnt\n", automount_dev);
if ((res = mount(automount_dev, "/mnt", "ext2", 0, NULL)) != 0) {
perror(automount_dev);
}
}
//setuid(1000);
//setgid(1000);