From b01aa72f176cde5f28e56f456e6efb01a7c9de8a Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 3 Aug 2023 10:32:52 +0200 Subject: [PATCH] libc+init+shmem-test: Add POSIX shared memory objects --- apps/init.cpp | 10 ++++++++++ apps/shmem-test.cpp | 23 ++++++++++++++++++----- libc/include/sys/mman.h | 6 ++++++ libc/src/sys/mman.cpp | 16 ++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/apps/init.cpp b/apps/init.cpp index 4df96dea..a18e395f 100644 --- a/apps/init.cpp +++ b/apps/init.cpp @@ -292,6 +292,15 @@ static void mount_tmpfs() if (chmod("/tmp", 01777) < 0) exit(255); } +static void mount_shmfs() +{ + if (mkdir("/dev/shm", 0755) < 0) exit(255); + + if (mount("/dev/shm", "tmpfs", "tmpfs") < 0) exit(255); + + if (chmod("/dev/shm", 01777) < 0) exit(255); +} + int main() { if (getpid() != 1) @@ -307,6 +316,7 @@ int main() stderr = fopen("/dev/console", "w"); mount_tmpfs(); + mount_shmfs(); umask(022); diff --git a/apps/shmem-test.cpp b/apps/shmem-test.cpp index c9c31f65..968b335b 100644 --- a/apps/shmem-test.cpp +++ b/apps/shmem-test.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,15 +6,27 @@ int main() { - void* address = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0); + int fd = shm_open("/shared", O_CREAT | O_RDWR, 0666); + if (fd < 0) + { + perror("shm_open"); + return 1; + } + + ftruncate(fd, PAGE_SIZE); + + void* address = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (address == MAP_FAILED) { perror("mmap"); return 1; } - int* ptr = (int*)address; - *ptr = 16; + char* ptr = (char*)address; + + printf("the value is %c\n", *ptr); + + *ptr = 'a'; pid_t child = fork(); if (child < 0) @@ -23,13 +36,13 @@ int main() } if (child == 0) { - *ptr = 32; + *ptr = 'e'; _exit(0); } waitpid(child, NULL, 0); - printf("the value is %d\n", *ptr); + printf("the value is %c\n", *ptr); return 0; } diff --git a/libc/include/sys/mman.h b/libc/include/sys/mman.h index c7433b19..1b605f79 100644 --- a/libc/include/sys/mman.h +++ b/libc/include/sys/mman.h @@ -28,6 +28,12 @@ extern "C" /* Write modified shared memory back to its associated file. */ int msync(void* addr, size_t len, int flags); + /* Create a new POSIX shared memory object. */ + int shm_open(const char* name, int oflag, mode_t mode); + + /* Delete a POSIX shared memory object from the file system. */ + int shm_unlink(const char* name); + #ifdef __cplusplus } #endif diff --git a/libc/src/sys/mman.cpp b/libc/src/sys/mman.cpp index 9fdc7a2d..6f321dc9 100644 --- a/libc/src/sys/mman.cpp +++ b/libc/src/sys/mman.cpp @@ -1,6 +1,8 @@ #include #include +#include #include +#include #include #include #include @@ -25,4 +27,18 @@ extern "C" long rc = syscall(SYS_msync, addr, len); __errno_return(rc, int); } + + int shm_open(const char* name, int oflag, mode_t mode) + { + char buf[BUFSIZ]; + snprintf(buf, sizeof(buf), "/dev/shm%s", name); + return open(buf, oflag | O_CLOEXEC, mode); + } + + int shm_unlink(const char* name) + { + char buf[BUFSIZ]; + snprintf(buf, sizeof(buf), "/dev/shm%s", name); + return unlink(buf); + } }