libc+init+shmem-test: Add POSIX shared memory objects
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
d41fb85466
commit
b01aa72f17
@ -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);
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/wait.h>
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -1,6 +1,8 @@
|
||||
#include <bits/errno-return.h>
|
||||
#include <bits/mmap.h>
|
||||
#include <fcntl.h>
|
||||
#include <luna/Heap.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user