2021-02-26 02:30:17 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2020-06-15 14:18:57 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2021-02-26 02:30:17 +00:00
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
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-11-06 07:29:47 +00:00
|
|
|
#include "ape/relocations.h"
|
2022-10-14 16:52:35 +00:00
|
|
|
#include "ape/sections.internal.h"
|
2022-11-06 07:29:47 +00:00
|
|
|
#include "libc/assert.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2021-02-26 02:30:17 +00:00
|
|
|
#include "libc/calls/metalfile.internal.h"
|
2022-11-06 07:29:47 +00:00
|
|
|
#include "libc/intrin/directmap.internal.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2022-11-06 07:29:47 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2021-02-26 02:30:17 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2022-11-06 07:29:47 +00:00
|
|
|
#include "libc/runtime/pc.internal.h"
|
2021-02-26 02:30:17 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/str/str.h"
|
2022-11-06 07:29:47 +00:00
|
|
|
#include "libc/sysv/consts/at.h"
|
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
2021-02-26 02:30:17 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2023-08-12 06:14:02 +00:00
|
|
|
#include "libc/runtime/zipos.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2023-05-09 08:56:56 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2021-02-26 02:30:17 +00:00
|
|
|
int sys_openat_metal(int dirfd, const char *file, int flags, unsigned mode) {
|
|
|
|
int fd;
|
|
|
|
struct MetalFile *state;
|
2022-11-06 07:29:47 +00:00
|
|
|
if (dirfd != AT_FDCWD || strcmp(file, APE_COM_NAME)) return enoent();
|
|
|
|
if (flags != O_RDONLY) return eacces();
|
2023-04-27 03:45:01 +00:00
|
|
|
if (!_weaken(__ape_com_base) || !_weaken(__ape_com_size)) return eopnotsupp();
|
2022-04-15 06:39:48 +00:00
|
|
|
if ((fd = __reservefd(-1)) == -1) return -1;
|
2022-11-06 07:29:47 +00:00
|
|
|
if (!_weaken(calloc) || !_weaken(free)) {
|
|
|
|
struct DirectMap dm;
|
|
|
|
dm = sys_mmap_metal(NULL, ROUNDUP(sizeof(struct MetalFile), 4096),
|
2023-04-27 03:45:01 +00:00
|
|
|
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1,
|
|
|
|
0);
|
2022-11-06 07:29:47 +00:00
|
|
|
state = dm.addr;
|
|
|
|
if (state == (void *)-1) return -1;
|
|
|
|
} else {
|
|
|
|
state = _weaken(calloc)(1, sizeof(struct MetalFile));
|
|
|
|
if (!state) return -1;
|
|
|
|
}
|
|
|
|
state->base = (char *)__ape_com_base;
|
|
|
|
state->size = __ape_com_size;
|
2021-02-26 02:30:17 +00:00
|
|
|
g_fds.p[fd].kind = kFdFile;
|
|
|
|
g_fds.p[fd].flags = flags;
|
2022-04-15 06:39:48 +00:00
|
|
|
g_fds.p[fd].mode = mode;
|
2021-02-26 02:30:17 +00:00
|
|
|
g_fds.p[fd].handle = (intptr_t)state;
|
|
|
|
return fd;
|
|
|
|
}
|
2023-05-09 08:56:56 +00:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|