diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-11-08 12:59:16 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-11-08 12:59:16 +0100 |
commit | 3c8064a7872b990c6b48455ed6509d161adf4721 (patch) | |
tree | 497c35347f921211d3248222931892b4acf9bdff /Userland/sh.cpp | |
parent | fdbd9f1e272b97d7d28f9f610be8fbf0bdbd98d9 (diff) | |
download | serenity-3c8064a7872b990c6b48455ed6509d161adf4721.zip |
Support basic mmap'ing of a file!
All right, we can now mmap() a file and it gets magically paged in from fs
in response to an NP page fault. This is really cool :^)
I need to refactor this to support sharing of read-only file-backed pages,
but it's cool to just have something working.
Diffstat (limited to 'Userland/sh.cpp')
-rw-r--r-- | Userland/sh.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Userland/sh.cpp b/Userland/sh.cpp index 9a4706dc52..e20dbb9d1a 100644 --- a/Userland/sh.cpp +++ b/Userland/sh.cpp @@ -6,6 +6,7 @@ #include <LibC/stdlib.h> #include <LibC/utsname.h> #include <LibC/pwd.h> +#include <sys/mman.h> #include <signal.h> #include <AK/FileSystemPath.h> @@ -112,6 +113,32 @@ static int sh_wt(int, const char**) return 0; } +static int sh_mf(int, const char**) +{ + int rc; + int fd = open("/Banner.txt", O_RDONLY); + if (fd < 0) { + perror("open(/Banner.txt)"); + return 1; + } + printf("opened /Banner.txt, calling mmap...\n"); + byte* data = (byte*)mmap(nullptr, getpagesize(), PROT_READ, MAP_PRIVATE, fd, 0); + if (data == MAP_FAILED) { + perror("mmap()"); + goto close_it; + } + printf("mapped file @ %p\n", data); + printf("contents: %b %b %b %b\n", data[0], data[1], data[2], data[3]); + + rc = munmap(data, getpagesize()); + printf("munmap() returned %d\n", rc); + +close_it: + rc = close(fd); + printf("close() returned %d\n", rc); + return 0; +} + static int sh_exit(int, const char**) { printf("Good-bye!\n"); @@ -190,6 +217,10 @@ static bool handle_builtin(int argc, const char** argv, int& retval) retval = sh_wt(argc, argv); return true; } + if (!strcmp(argv[0], "mf")) { + retval = sh_mf(argc, argv); + return true; + } if (!strcmp(argv[0], "fork")) { retval = sh_fork(argc, argv); return true; |