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

@ -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

@ -1,55 +0,0 @@
/*-*- 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 2022 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"
#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
* @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) {
char *p;
unsigned n;
const char *path, *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;
}

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) */