mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-22 02:20:31 +00:00
Clean up some code
This commit is contained in:
parent
ec3275179f
commit
285c565051
33 changed files with 122 additions and 382 deletions
|
@ -16,6 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/calls/struct/timespec.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/wintime.internal.h"
|
||||
|
@ -26,11 +27,16 @@
|
|||
textwindows int sys_clock_gettime_nt(int clock, struct timespec *ts) {
|
||||
struct NtFileTime ft;
|
||||
if (clock == CLOCK_REALTIME) {
|
||||
if (!ts) return 0;
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
*ts = FileTimeToTimeSpec(ft);
|
||||
return 0;
|
||||
} else if (clock == CLOCK_MONOTONIC) {
|
||||
if (!ts) return 0;
|
||||
return sys_clock_gettime_mono(ts);
|
||||
} else if (clock == CLOCK_BOOTTIME) {
|
||||
if (ts) *ts = timespec_frommillis(GetTickCount64());
|
||||
return 0;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
|
|
@ -16,12 +16,18 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/timespec.internal.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/calls/struct/timeval.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
#define CTL_KERN 1
|
||||
#define KERN_BOOTTIME 21
|
||||
|
||||
int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
|
||||
long ax, dx;
|
||||
if (clock == CLOCK_REALTIME) {
|
||||
|
@ -41,18 +47,28 @@ int sys_clock_gettime_xnu(int clock, struct timespec *ts) {
|
|||
// 2. old xnu returns *ts in rax:rdx regs
|
||||
//
|
||||
// we assume this system call always succeeds
|
||||
asm volatile("syscall"
|
||||
: "=a"(ax), "=d"(dx)
|
||||
: "0"(0x2000000 | 116), "D"(ts), "S"(0), "1"(0)
|
||||
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
||||
if (ax) {
|
||||
ts->tv_sec = ax;
|
||||
ts->tv_nsec = dx;
|
||||
if (ts) {
|
||||
asm volatile("syscall"
|
||||
: "=a"(ax), "=d"(dx)
|
||||
: "0"(0x2000000 | 116), "D"(ts), "S"(0), "1"(0)
|
||||
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
||||
if (ax) {
|
||||
ts->tv_sec = ax;
|
||||
ts->tv_nsec = dx;
|
||||
}
|
||||
ts->tv_nsec *= 1000;
|
||||
}
|
||||
ts->tv_nsec *= 1000;
|
||||
return 0;
|
||||
} else if (clock == CLOCK_MONOTONIC) {
|
||||
if (!ts) return 0;
|
||||
return sys_clock_gettime_mono(ts);
|
||||
} else if (clock == CLOCK_BOOTTIME) {
|
||||
struct timeval x;
|
||||
size_t n = sizeof(x);
|
||||
int mib[] = {CTL_KERN, KERN_BOOTTIME};
|
||||
if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return -1;
|
||||
if (ts) *ts = timeval_totimespec(timeval_sub(timeval_real(), x));
|
||||
return 0;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
|
|
@ -32,6 +32,8 @@ textwindows int sys_close_nt(int fd, int fildes) {
|
|||
if (fd + 0u >= g_fds.n) return ebadf();
|
||||
struct Fd *f = g_fds.p + fd;
|
||||
switch (f->kind) {
|
||||
case kFdEmpty:
|
||||
return ebadf();
|
||||
case kFdFile:
|
||||
void sys_fcntl_nt_lock_cleanup(int);
|
||||
if (_weaken(sys_fcntl_nt_lock_cleanup)) {
|
||||
|
|
62
libc/calls/mknod.c
Normal file
62
libc/calls/mknod.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ 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/calls/syscall-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/sysv/consts/at.h"
|
||||
#include "libc/sysv/consts/s.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
/**
|
||||
* Creates filesystem inode.
|
||||
*
|
||||
* @param mode is octal mode, e.g. 0600; needs to be or'd with one of:
|
||||
* S_IFDIR: directory
|
||||
* S_IFIFO: named pipe
|
||||
* S_IFREG: regular file
|
||||
* S_IFSOCK: named socket
|
||||
* S_IFBLK: block device (root has authorization)
|
||||
* S_IFCHR: character device (root has authorization)
|
||||
* @param dev it's complicated
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
* @asyncsignalsafe
|
||||
*/
|
||||
int mknod(const char *path, uint32_t mode, uint64_t dev) {
|
||||
int e, rc;
|
||||
if (IsAsan() && !__asan_is_valid_str(path)) return efault();
|
||||
if (mode & S_IFREG) return creat(path, mode & ~S_IFREG);
|
||||
if (mode & S_IFDIR) return mkdir(path, mode & ~S_IFDIR);
|
||||
if (mode & S_IFIFO) return enosys(); // no named pipes!
|
||||
if (!IsWindows()) {
|
||||
// TODO(jart): Whys there code out there w/ S_xxx passed via dev?
|
||||
e = errno;
|
||||
rc = sys_mknod(path, mode, dev);
|
||||
if (rc == -1 && rc == ENOSYS) {
|
||||
errno = e;
|
||||
rc = sys_mknodat(AT_FDCWD, path, mode, dev);
|
||||
}
|
||||
} else {
|
||||
rc = enosys();
|
||||
}
|
||||
STRACE("mknod(%#s, %#o, %#lx) → %d% m", path, mode, dev, rc);
|
||||
return rc;
|
||||
}
|
|
@ -21,6 +21,7 @@
|
|||
#include "libc/nt/accounting.h"
|
||||
#include "libc/nt/struct/memorystatusex.h"
|
||||
#include "libc/nt/struct/systeminfo.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/nt/systeminfo.h"
|
||||
|
||||
textwindows int sys_sysinfo_nt(struct sysinfo *info) {
|
||||
|
@ -29,10 +30,11 @@ textwindows int sys_sysinfo_nt(struct sysinfo *info) {
|
|||
GetSystemInfo(&sysinfo);
|
||||
memstat.dwLength = sizeof(struct NtMemoryStatusEx);
|
||||
if (GlobalMemoryStatusEx(&memstat)) {
|
||||
info->mem_unit = 1;
|
||||
info->totalram = memstat.ullTotalPhys;
|
||||
info->freeram = memstat.ullAvailPhys;
|
||||
info->procs = sysinfo.dwNumberOfProcessors;
|
||||
info->mem_unit = 1;
|
||||
info->uptime = GetTickCount64() / 1000;
|
||||
return 0;
|
||||
} else {
|
||||
return __winerr();
|
||||
|
|
|
@ -29,10 +29,17 @@
|
|||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
#define CTL_KERN 1
|
||||
#define CTL_VM 2
|
||||
#define CTL_HW 6
|
||||
#define VM_LOADAVG 2
|
||||
#define KERN_BOOTTIME 21
|
||||
#define HW_PHYSMEM (IsXnu() ? 24 : 5)
|
||||
|
||||
struct loadavg {
|
||||
uint32_t ldavg[3];
|
||||
int64_t fscale;
|
||||
};
|
||||
|
||||
static int64_t GetUptime(void) {
|
||||
if (IsNetbsd()) return 0; // TODO(jart): Why?
|
||||
struct timeval x;
|
||||
|
@ -50,7 +57,20 @@ static int64_t GetPhysmem(void) {
|
|||
return x;
|
||||
}
|
||||
|
||||
static void GetLoads(uint64_t loads[3]) {
|
||||
size_t size;
|
||||
struct loadavg loadinfo;
|
||||
int mib[2] = {CTL_VM, VM_LOADAVG};
|
||||
size = sizeof(loadinfo);
|
||||
if (sys_sysctl(mib, 2, &loadinfo, &size, 0, 0) != -1) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
loads[i] = (double)loadinfo.ldavg[i] / loadinfo.fscale * 65536;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int sys_sysinfo_bsd(struct sysinfo *info) {
|
||||
GetLoads(info->loads);
|
||||
info->uptime = GetUptime();
|
||||
info->totalram = GetPhysmem();
|
||||
info->bufferram = GetPhysmem();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue