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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/assert.h"
|
|
|
|
#include "libc/calls/calls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2022-06-15 23:19:50 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/intrin/cmpxchg.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/extend.internal.h"
|
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2022-06-15 23:19:50 +00:00
|
|
|
#include "libc/runtime/directmap.internal.h"
|
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/str/str.h"
|
2022-06-15 23:19:50 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
#include "libc/sysv/consts/sig.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2022-06-15 23:19:50 +00:00
|
|
|
static volatile size_t mapsize;
|
2022-05-21 01:51:41 +00:00
|
|
|
|
2022-04-15 06:39:48 +00:00
|
|
|
/**
|
|
|
|
* Grows file descriptor array memory if needed.
|
2022-06-15 23:19:50 +00:00
|
|
|
*
|
|
|
|
* @see libc/runtime/memtrack64.txt
|
|
|
|
* @see libc/runtime/memtrack32.txt
|
|
|
|
* @asyncsignalsafe
|
2022-04-15 06:39:48 +00:00
|
|
|
*/
|
2022-05-17 11:14:28 +00:00
|
|
|
int __ensurefds_unlocked(int fd) {
|
2022-09-09 23:54:28 +00:00
|
|
|
bool relocate;
|
2022-05-21 01:51:41 +00:00
|
|
|
if (fd < g_fds.n) return fd;
|
2022-09-09 23:54:28 +00:00
|
|
|
g_fds.n = fd + 1;
|
2022-10-06 11:55:26 +00:00
|
|
|
g_fds.e = _extend(g_fds.p, g_fds.n * sizeof(*g_fds.p), g_fds.e, MAP_PRIVATE,
|
2022-09-13 06:10:38 +00:00
|
|
|
kMemtrackFdsStart + kMemtrackFdsSize);
|
2022-05-17 11:14:28 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grows file descriptor array memory if needed.
|
2022-06-15 23:19:50 +00:00
|
|
|
* @asyncsignalsafe
|
|
|
|
* @threadsafe
|
2022-05-17 11:14:28 +00:00
|
|
|
*/
|
|
|
|
int __ensurefds(int fd) {
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_lock();
|
2022-05-17 11:14:28 +00:00
|
|
|
fd = __ensurefds_unlocked(fd);
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_unlock();
|
2022-04-15 06:39:48 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Finds open file descriptor slot.
|
2022-06-15 23:19:50 +00:00
|
|
|
* @asyncsignalsafe
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
2022-05-17 11:14:28 +00:00
|
|
|
int __reservefd_unlocked(int start) {
|
2021-02-03 14:22:51 +00:00
|
|
|
int fd;
|
2022-06-15 23:19:50 +00:00
|
|
|
for (;;) {
|
|
|
|
for (fd = MAX(start, g_fds.f); fd < g_fds.n; ++fd) {
|
|
|
|
if (!g_fds.p[fd].kind) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fd = __ensurefds_unlocked(fd);
|
|
|
|
bzero(g_fds.p + fd, sizeof(*g_fds.p));
|
|
|
|
if (_cmpxchg(&g_fds.p[fd].kind, kFdEmpty, kFdReserved)) {
|
|
|
|
_cmpxchg(&g_fds.f, fd, fd + 1);
|
|
|
|
return fd;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-17 11:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds open file descriptor slot.
|
2022-06-15 23:19:50 +00:00
|
|
|
* @asyncsignalsafe
|
|
|
|
* @threadsafe
|
2022-05-17 11:14:28 +00:00
|
|
|
*/
|
|
|
|
int __reservefd(int start) {
|
|
|
|
int fd;
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_lock();
|
2022-05-17 11:14:28 +00:00
|
|
|
fd = __reservefd_unlocked(start);
|
2022-06-09 03:01:28 +00:00
|
|
|
__fds_unlock();
|
2022-05-17 11:14:28 +00:00
|
|
|
return fd;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|