mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-28 00:02:28 +00:00
Linux: Add cachestat, fchmodat2 syscalls (#958)
This commit is contained in:
parent
69faf1b403
commit
cc5c5319bf
8 changed files with 287 additions and 1 deletions
121
test/libc/calls/cachestat_test.c
Normal file
121
test/libc/calls/cachestat_test.c
Normal file
|
@ -0,0 +1,121 @@
|
|||
/*-*- 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 Nhat Pham <nphamcs@gmail.com> │
|
||||
│ Copyright 2023 Stephen Gregoratto <dev@sgregoratto.me> │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/cachestat.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/statfs.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/gc.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/sysconf.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/sysv/consts/auxv.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
static size_t pagesize;
|
||||
|
||||
bool HasCachestatSupport(void) {
|
||||
return IsLinux() && cachestat(-1, 0, 0, 0) == -1 && errno == EBADF;
|
||||
}
|
||||
|
||||
void SetUpOnce(void) {
|
||||
if (!HasCachestatSupport()) {
|
||||
kprintf("warning: cachestat not supported on this systemL %m\n");
|
||||
exit(0);
|
||||
}
|
||||
testlib_enable_tmp_setup_teardown();
|
||||
pagesize = (size_t)getauxval(AT_PAGESZ);
|
||||
// ASSERT_SYS(0, 0, pledge("stdio rpath wpath cpath", 0));
|
||||
}
|
||||
|
||||
TEST(cachestat, testCachestatOnDevices) {
|
||||
const char *const files[] = {
|
||||
"/dev/zero", "/dev/null", "/dev/urandom", "/proc/version", "/proc",
|
||||
};
|
||||
struct cachestat_range range = {0, 4 * pagesize};
|
||||
struct cachestat cs;
|
||||
for (size_t i = 0; i < ARRAYLEN(files); i++) {
|
||||
ASSERT_SYS(0, 3, open(files[i], O_RDONLY));
|
||||
ASSERT_SYS(0, 0, cachestat(3, &range, &cs, 0));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(cachestat, testCachestatAfterWrite) {
|
||||
size_t size = 4 * pagesize;
|
||||
char *data = gc(xmalloc(size));
|
||||
ASSERT_SYS(0, size, getrandom(data, size, 0));
|
||||
// TODO: handle EINTR like xbarf
|
||||
ASSERT_SYS(0, 3, open("tmpfilecachestat", O_CREAT | O_RDWR, 0600));
|
||||
ASSERT_SYS(0, size, write(3, data, size));
|
||||
struct cachestat_range range = {0, size};
|
||||
struct cachestat cs;
|
||||
ASSERT_SYS(0, 0, cachestat(3, &range, &cs, 0));
|
||||
ASSERT_EQ(4, cs.nr_cache + cs.nr_evicted,
|
||||
"total number of evicted pages is off.");
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
#define TMPFS_MAGIC 0x01021994
|
||||
TEST(cachestat, testCachestatSyncNoDirty) {
|
||||
size_t size = 4 * pagesize;
|
||||
char *data = gc(xmalloc(size));
|
||||
ASSERT_SYS(0, size, getrandom(data, size, 0));
|
||||
// TODO: handle EINTR like xbarf
|
||||
ASSERT_SYS(0, 3, open("tmpfilecachestat", O_CREAT | O_RDWR, 0600));
|
||||
ASSERT_SYS(0, size, write(3, data, size));
|
||||
struct cachestat_range range = {0, size};
|
||||
struct cachestat cs;
|
||||
ASSERT_SYS(0, 0, cachestat(3, &range, &cs, 0));
|
||||
ASSERT_EQ(4, cs.nr_cache + cs.nr_evicted,
|
||||
"total number of evicted pages is off.");
|
||||
struct statfs statfs;
|
||||
ASSERT_SYS(0, 0, fstatfs(3, &statfs));
|
||||
if (statfs.f_type == TMPFS_MAGIC) goto done;
|
||||
ASSERT_SYS(0, 0, fsync(3));
|
||||
ASSERT_SYS(0, 0, cachestat(3, &range, &cs, 0));
|
||||
EXPECT_EQ(0, cs.nr_dirty,
|
||||
"dirty pages should be zero after fsync, got %llu\n", cs.nr_dirty);
|
||||
done:
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(cachestat, testCachestatShmem) {
|
||||
size_t filesize = 512 * 2 * pagesize; // 2 2MB huge pages.
|
||||
size_t compute_len = 512 * pagesize;
|
||||
unsigned long num_pages = compute_len / pagesize;
|
||||
char *data = gc(xmalloc(filesize));
|
||||
ASSERT_SYS(0, filesize, getrandom(data, filesize, 0));
|
||||
ASSERT_SYS(0, 3, shm_open("tmpshmcstat", O_CREAT | O_RDWR, 0600));
|
||||
ASSERT_SYS(0, 0, ftruncate(3, filesize));
|
||||
ASSERT_SYS(0, filesize, write(3, data, filesize));
|
||||
struct cachestat_range range = {pagesize, compute_len};
|
||||
struct cachestat cs;
|
||||
ASSERT_SYS(0, 0, cachestat(3, &range, &cs, 0));
|
||||
ASSERT_EQ(num_pages, cs.nr_cache + cs.nr_evicted,
|
||||
"total number of cached and evicted pages is off.\n");
|
||||
ASSERT_SYS(0, 0, shm_unlink("tmpshmcstat"));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
68
test/libc/calls/fchmodat_test.c
Normal file
68
test/libc/calls/fchmodat_test.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*-*- 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 Stephen Gregoratto │
|
||||
│ │
|
||||
│ 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/errno.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/sysv/consts/s.h"
|
||||
// #include "libc/mem/gc.internal.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/at.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
void SetUpOnce(void) {
|
||||
testlib_enable_tmp_setup_teardown();
|
||||
// ASSERT_SYS(0, 0, pledge("stdio rpath wpath cpath", 0));
|
||||
}
|
||||
|
||||
static void ExpectMode(const char *filename, uint32_t mode) {
|
||||
struct stat st;
|
||||
ASSERT_SYS(0, 0, fstatat(AT_FDCWD, filename, &st, AT_SYMLINK_NOFOLLOW));
|
||||
ASSERT_TRUE((st.st_mode & 0777) == mode);
|
||||
}
|
||||
|
||||
TEST(fchmodat, testFchmodat) {
|
||||
ASSERT_SYS(0, 3,
|
||||
open("regfile", O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0644));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
ASSERT_SYS(0, 0, symlink("regfile", "symlink"));
|
||||
ExpectMode("regfile", 0644);
|
||||
struct stat st;
|
||||
ASSERT_SYS(0, 0, fstatat(AT_FDCWD, "symlink", &st, AT_SYMLINK_NOFOLLOW));
|
||||
uint32_t sym_mode = st.st_mode & 0777;
|
||||
ASSERT_SYS(0, 0, fchmodat(AT_FDCWD, "regfile", 0640, 0));
|
||||
ExpectMode("regfile", 0640);
|
||||
ASSERT_SYS(0, 0, fchmodat(AT_FDCWD, "regfile", 0600, AT_SYMLINK_NOFOLLOW));
|
||||
ExpectMode("regfile", 0600);
|
||||
ASSERT_SYS(0, 0, fchmodat(AT_FDCWD, "symlink", 0640, 0));
|
||||
ExpectMode("regfile", 0640);
|
||||
ExpectMode("symlink", sym_mode);
|
||||
int rc = fchmodat(AT_FDCWD, "symlink", 0600, AT_SYMLINK_NOFOLLOW);
|
||||
if (rc == -1) {
|
||||
ASSERT_TRUE(errno == ENOTSUP);
|
||||
errno = 0;
|
||||
} else {
|
||||
ExpectMode("symlink", 0600);
|
||||
}
|
||||
ExpectMode("regfile", 0640);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue