Introduce shm_open() and shm_unlink()

This commit is contained in:
Justine Tunney 2023-10-31 23:57:52 -07:00
parent fadb64a2bf
commit 0b1acce680
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
9 changed files with 313 additions and 37 deletions

View file

@ -158,6 +158,8 @@ int setregid(unsigned, unsigned);
int setreuid(unsigned, unsigned);
int setsid(void);
int setuid(unsigned);
int shm_open(const char *, int, unsigned);
int shm_unlink(const char *);
int sigignore(int);
int siginterrupt(int, int);
int symlink(const char *, const char *);
@ -211,36 +213,37 @@ int madvise(void *, uint64_t, int);
#ifdef _COSMO_SOURCE
bool32 fdexists(int);
bool32 fileexists(const char *);
bool32 ischardev(int);
bool32 isdirectory(const char *);
bool32 isexecutable(const char *);
bool32 isregularfile(const char *);
bool32 issymlink(const char *);
bool32 ischardev(int);
char *commandv(const char *, char *, size_t);
int clone(void *, void *, size_t, int, void *, void *, void *, void *);
int fadvise(int, uint64_t, uint64_t, int);
int makedirs(const char *, unsigned);
int pivot_root(const char *, const char *);
int pledge(const char *, const char *);
int seccomp(unsigned, unsigned, void *);
int sys_iopl(int);
int sys_ioprio_get(int, int);
int sys_ioprio_set(int, int, int);
int sys_mlock(const void *, size_t);
int sys_mlock2(const void *, size_t, int);
int sys_mlockall(int);
int sys_personality(uint64_t);
int sys_munlock(const void *, size_t);
int sys_munlockall(void);
int sys_personality(uint64_t);
int sys_ptrace(int, ...);
int sys_sysctl(const int *, unsigned, void *, size_t *, void *, size_t);
int sys_ioprio_get(int, int);
int sys_ioprio_set(int, int, int);
int tmpfd(void);
int touch(const char *, unsigned);
int unveil(const char *, const char *);
long ptrace(int, ...);
int fadvise(int, uint64_t, uint64_t, int);
ssize_t copyfd(int, int, size_t);
ssize_t readansi(int, char *, size_t);
ssize_t tinyprint(int, const char *, ...) nullterminated();
void shm_path_np(const char *, char[hasatleast 78]);
#endif /* _COSMO_SOURCE */
int __wifstopped(int) pureconst;

View file

@ -91,11 +91,9 @@ textwindows int GetNtOpenFlags(int flags, int mode, uint32_t *out_perm,
// Be as generous as possible in sharing file access. Not doing this
// you'll quickly find yourself no longer able to administer Windows
if (flags & _O_EXCL) {
share = kNtFileShareExclusive;
} else {
share = kNtFileShareRead | kNtFileShareWrite | kNtFileShareDelete;
}
// and we need this to be the case even in O_EXCL mode otherwise the
// shm_open_test.c won't behave consistently on Windows like on UNIX
share = kNtFileShareRead | kNtFileShareWrite | kNtFileShareDelete;
// These POSIX to WIN32 mappings are relatively straightforward.
if (flags & _O_EXCL) {

45
libc/calls/shm_open.c Normal file
View file

@ -0,0 +1,45 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/blockcancel.internal.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/errfuns.h"
/**
* Opens POSIX named memory object.
*
* @param name should begin with a `/` and shouldn't contain subsequent
* slash characters, and furthermore shouldn't exceed NAME_MAX, for
* maximum portability; POSIX defines it as opening a multi-process
* object and leaves all other names implementation defined; and we
* choose (in this implementation) to define those names as meaning
* the same thing while not imposing any length limit; cosmo always
* feeds your name through BLAKE2B to create a `.sem` file existing
* under `/dev/shm` if available, otherwise it will go under `/tmp`
* @return open file descriptor, or -1 w/ errno
*/
int shm_open(const char *name, int oflag, unsigned mode) {
int fd;
char path[78];
shm_path_np(name, path);
BLOCK_CANCELATION;
fd = openat(AT_FDCWD, path, oflag, mode);
ALLOW_CANCELATION;
return fd;
}

View file

@ -20,36 +20,26 @@
#include "libc/dce.h"
#include "libc/str/blake2.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/thread.h"
/**
* Returns filesystem pathname of named semaphore.
*
* @param name is `name` of semaphore which should begin with slash
* @param buf is temporary storage with at least `size` bytes
* @param size is size of `buf` in bytes
* @param buf is temporary storage with at least 78 bytes
* @return pointer to file system path
* @raise ENAMETOOLONG if constructed path would exceed `size`
*/
const char *sem_path_np(const char *name, char *buf, size_t size) {
void shm_path_np(const char *name, char buf[hasatleast 78]) {
char *p;
unsigned n;
const char *path, *a;
const char *a;
uint8_t digest[BLAKE2B256_DIGEST_LENGTH];
a = "/tmp/", n = 5;
if (IsLinux() && isdirectory("/dev/shm")) {
a = "/dev/shm/", n = 9;
}
if (n + BLAKE2B256_DIGEST_LENGTH * 2 + 4 < size) {
BLAKE2B256(name, strlen(name), digest);
p = mempcpy(buf, a, n);
p = hexpcpy(p, digest, BLAKE2B256_DIGEST_LENGTH);
p = mempcpy(p, ".sem", 5);
path = buf;
} else {
enametoolong();
path = 0;
}
return path;
BLAKE2B256(name, strlen(name), digest);
p = mempcpy(buf, a, n);
p = hexpcpy(p, digest, BLAKE2B256_DIGEST_LENGTH);
mempcpy(p, ".sem", 5);
}

28
libc/calls/shm_unlink.c Normal file
View file

@ -0,0 +1,28 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
/**
* Deletes POSIX named memory object.
*/
int shm_unlink(const char *name) {
char path[78];
shm_path_np(name, path);
return unlink(path);
}

View file

@ -171,9 +171,8 @@ static struct Semaphore *sem_open_get(const sem_t *sem,
sem_t *sem_open(const char *name, int oflag, ...) {
sem_t *sem;
va_list va;
const char *path;
char path[78];
struct Semaphore *s;
char pathbuf[PATH_MAX];
unsigned mode = 0, value = 0;
va_start(va, oflag);
@ -206,9 +205,7 @@ sem_t *sem_open(const char *name, int oflag, ...) {
return SEM_FAILED;
}
}
if (!(path = sem_path_np(name, pathbuf, sizeof(pathbuf)))) {
return SEM_FAILED;
}
shm_path_np(name, path);
BLOCK_CANCELATION;
sem_open_init();
sem_open_lock();
@ -321,10 +318,9 @@ int sem_close(sem_t *sem) {
* @raise ENAMETOOLONG if too long
*/
int sem_unlink(const char *name) {
const char *path;
char path[78];
int rc, e = errno;
struct Semaphore *s;
char pathbuf[PATH_MAX];
#if 0
if (IsXnuSilicon()) {
@ -332,7 +328,7 @@ int sem_unlink(const char *name) {
}
#endif
if (!(path = sem_path_np(name, pathbuf, sizeof(pathbuf)))) return -1;
shm_path_np(name, path);
if ((rc = unlink(path)) == -1 && IsWindows() && errno == EACCES) {
sem_open_init();
sem_open_lock();

View file

@ -38,7 +38,6 @@ int sem_getvalue(sem_t *, int *);
sem_t *sem_open(const char *, int, ...);
int sem_close(sem_t *);
int sem_unlink(const char *);
const char *sem_path_np(const char *, char *, size_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */