cosmopolitan/libc/calls/openanon.c

100 lines
4.2 KiB
C
Raw Normal View History

2020-06-15 14:18:57 +00:00
/*-*- 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 2020 Justine Alexandra Roberts Tunney
2020-12-28 01:18:44 +00:00
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.
2020-06-15 14:18:57 +00:00
2020-12-28 01:18:44 +00:00
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.
2020-06-15 14:18:57 +00:00
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/dce.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/isslash.internal.h"
#include "libc/fmt/itoa.h"
2020-06-15 14:18:57 +00:00
#include "libc/nt/createfile.h"
#include "libc/nt/enum/accessmask.h"
2020-06-15 14:18:57 +00:00
#include "libc/nt/enum/creationdisposition.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/filesharemode.h"
2020-06-15 14:18:57 +00:00
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/o.h"
static struct OpenAnon { int count; } g_openanon;
static void openanon_genpath(const char *name, struct OpenAnon *state,
2020-06-15 14:18:57 +00:00
char pathbuf[hasatleast PATH_MAX]) {
char c;
size_t i;
char *p, *pe;
p = stpcpy(pathbuf, kTmpPath);
pe = pathbuf + PATH_MAX - 8 - 10 - 1 - 10 - 1;
if (!name) name = "openanon";
for (i = 0; p < pe; ++i) {
if (!(c = name[i])) break;
if (isslash(c)) c = '_';
*p++ = c;
}
*p++ = '.';
p += uint64toarray_radix10(getpid(), p);
*p++ = '.';
p += uint64toarray_radix10(++state->count, p);
*p = '\0';
assert(p < pe);
}
static int openanon_impl(const char *name, unsigned flags,
2020-06-15 14:18:57 +00:00
struct OpenAnon *state,
char pathbuf[hasatleast PATH_MAX]) {
int fd;
openanon_genpath(name, state, pathbuf);
flags |= O_RDWR | O_CREAT | O_EXCL | O_TRUNC;
2020-06-15 14:18:57 +00:00
if (!IsWindows()) {
if ((fd = sys_openat(AT_FDCWD, pathbuf, flags, 0600)) != -1) {
2020-06-15 14:18:57 +00:00
unlink(pathbuf);
}
return fd;
} else {
if ((fd = __reservefd()) == -1) return -1;
if ((g_fds.p[fd].handle = CreateFileA(
2020-06-15 14:18:57 +00:00
pathbuf, kNtGenericRead | kNtGenericWrite, kNtFileShareExclusive,
&kNtIsInheritable, kNtCreateAlways,
2020-06-15 14:18:57 +00:00
(kNtFileAttributeNotContentIndexed | kNtFileAttributeNormal |
kNtFileAttributeTemporary | kNtFileFlagDeleteOnClose),
0)) != -1) {
g_fds.p[fd].kind = kFdFile;
g_fds.p[fd].flags = flags;
2020-06-15 14:18:57 +00:00
return fd;
} else {
__releasefd(fd);
return __winerr();
2020-06-15 14:18:57 +00:00
}
}
}
/**
* Creates anonymous file.
*
* @param name is purely informative
* @param flags can have O_CLOEXEC
* @return fd of file with no name, needing close(), or -1 w/ errno
* @see memfd_create() if disk-paranoid
* @see mkostempsm() for named files
*/
int openanon(char *name, unsigned flags) {
char pathbuf[PATH_MAX];
return openanon_impl(name, flags, &g_openanon, pathbuf);
2020-06-15 14:18:57 +00:00
}