mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-14 15:09:09 +00:00
Get more Python tests passing (#141)
This commit is contained in:
parent
916f19eea1
commit
59e1c245d1
141 changed files with 3536 additions and 1203 deletions
|
@ -117,6 +117,7 @@ int fsync(int);
|
|||
int ftruncate(int, int64_t);
|
||||
int getdomainname(char *, size_t);
|
||||
int gethostname(char *, size_t);
|
||||
int getpgid(int);
|
||||
int getpid(void);
|
||||
int getppid(void);
|
||||
int getpriority(int, unsigned);
|
||||
|
@ -151,8 +152,8 @@ int posix_fadvise(int, uint64_t, uint64_t, int);
|
|||
int posix_madvise(void *, uint64_t, int);
|
||||
int prctl();
|
||||
int raise(int);
|
||||
int reboot(int);
|
||||
int readlink(const char *, char *, size_t);
|
||||
int reboot(int);
|
||||
int remove(const char *);
|
||||
int rename(const char *, const char *);
|
||||
int renameat(int, const char *, int, const char *);
|
||||
|
|
|
@ -26,8 +26,9 @@ static bool AccessCommand(char path[hasatleast PATH_MAX], const char *name,
|
|||
size_t namelen, size_t pathlen) {
|
||||
if (pathlen + 1 + namelen + 1 + 4 + 1 > PATH_MAX) return -1;
|
||||
if (pathlen && (path[pathlen - 1] != '/' && path[pathlen - 1] != '\\')) {
|
||||
path[pathlen] =
|
||||
!IsWindows() ? '/' : memchr(path, '\\', pathlen) ? '\\' : '/';
|
||||
path[pathlen] = !IsWindows() ? '/'
|
||||
: memchr(path, '\\', pathlen) ? '\\'
|
||||
: '/';
|
||||
pathlen++;
|
||||
}
|
||||
memcpy(path + pathlen, name, namelen + 1);
|
||||
|
@ -93,7 +94,9 @@ char *commandv(const char *name, char pathbuf[hasatleast PATH_MAX]) {
|
|||
(AccessCommand(pathbuf, name, namelen,
|
||||
stpcpy(pathbuf, kNtSystemDirectory) - pathbuf) ||
|
||||
AccessCommand(pathbuf, name, namelen,
|
||||
stpcpy(pathbuf, kNtWindowsDirectory) - pathbuf))) ||
|
||||
stpcpy(pathbuf, kNtWindowsDirectory) - pathbuf) ||
|
||||
AccessCommand(pathbuf, name, namelen,
|
||||
stpcpy(pathbuf, ".") - pathbuf))) ||
|
||||
SearchPath(pathbuf, name, namelen)) {
|
||||
errno = olderr;
|
||||
return pathbuf;
|
||||
|
|
23
libc/calls/getconsolectrlevent.h
Normal file
23
libc/calls/getconsolectrlevent.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_CALLS_GETCONSOLECTRLEVENT_H_
|
||||
#define COSMOPOLITAN_LIBC_CALLS_GETCONSOLECTRLEVENT_H_
|
||||
#include "libc/nt/enum/ctrlevent.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
static inline int GetConsoleCtrlEvent(int sig) {
|
||||
switch (sig) {
|
||||
case SIGINT:
|
||||
return kNtCtrlCEvent;
|
||||
case SIGHUP:
|
||||
return kNtCtrlCloseEvent;
|
||||
case SIGQUIT:
|
||||
return kNtCtrlBreakEvent;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_CALLS_GETCONSOLECTRLEVENT_H_ */
|
32
libc/calls/getpgid.c
Normal file
32
libc/calls/getpgid.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*-*- 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 2021 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/internal.h"
|
||||
#include "libc/dce.h"
|
||||
|
||||
/**
|
||||
* Returns process group id.
|
||||
*/
|
||||
int getpgid(int pid) {
|
||||
if (!IsWindows()) {
|
||||
return sys_getpgid(pid);
|
||||
} else {
|
||||
return getpid();
|
||||
}
|
||||
}
|
|
@ -38,8 +38,8 @@ textwindows int sys_getrusage_nt(int who, struct rusage *usage) {
|
|||
if ((who == RUSAGE_SELF ? GetProcessTimes : GetThreadTimes)(
|
||||
(who == RUSAGE_SELF ? GetCurrentProcess : GetCurrentThread)(),
|
||||
&CreationFileTime, &ExitFileTime, &KernelFileTime, &UserFileTime)) {
|
||||
FileTimeToTimeVal(&usage->ru_utime, UserFileTime);
|
||||
FileTimeToTimeVal(&usage->ru_stime, KernelFileTime);
|
||||
usage->ru_utime = FileTimeToTimeVal(UserFileTime);
|
||||
usage->ru_stime = FileTimeToTimeVal(KernelFileTime);
|
||||
return 0;
|
||||
} else {
|
||||
return __winerr();
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
int sys_gettimeofday_nt(struct timeval *tv, struct timezone *tz) {
|
||||
struct NtFileTime ft;
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
FileTimeToTimeVal(tv, ft);
|
||||
*tv = FileTimeToTimeVal(ft);
|
||||
if (tz) memset(tz, 0, sizeof(*tz));
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -142,6 +142,7 @@ i32 sys_ftruncate(i32, i64, i64) hidden;
|
|||
i32 sys_futimes(i32, const struct timeval *) hidden;
|
||||
i32 sys_futimesat(i32, const char *, const struct timeval *) hidden;
|
||||
i32 sys_getitimer(i32, struct itimerval *) hidden;
|
||||
i32 sys_getpgid(i32) hidden;
|
||||
i32 sys_getppid(void) hidden;
|
||||
i32 sys_getpriority(i32, u32) hidden;
|
||||
i32 sys_getrlimit(i32, struct rlimit *) hidden;
|
||||
|
|
|
@ -16,41 +16,37 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/getconsolectrlevent.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/console.h"
|
||||
#include "libc/nt/enum/ctrlevent.h"
|
||||
#include "libc/nt/enum/processaccess.h"
|
||||
#include "libc/nt/process.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
textwindows int sys_kill_nt(int pid, int sig) {
|
||||
int target;
|
||||
uint32_t event;
|
||||
if (!pid) return raise(sig);
|
||||
if ((pid > 0 && __isfdkind(pid, kFdProcess)) ||
|
||||
(pid < 0 && __isfdkind(-pid, kFdProcess))) {
|
||||
target = GetProcessId(g_fds.p[ABS(pid)].handle);
|
||||
} else {
|
||||
target = pid;
|
||||
}
|
||||
if (target == GetCurrentProcessId()) {
|
||||
return raise(sig);
|
||||
} else {
|
||||
switch (sig) {
|
||||
case SIGINT:
|
||||
event = kNtCtrlCEvent;
|
||||
case SIGHUP:
|
||||
event = kNtCtrlCloseEvent;
|
||||
case SIGQUIT:
|
||||
event = kNtCtrlBreakEvent;
|
||||
default:
|
||||
return einval();
|
||||
}
|
||||
if (GenerateConsoleCtrlEvent(event, target)) {
|
||||
return 0;
|
||||
bool ok;
|
||||
int event;
|
||||
int64_t handle;
|
||||
if (pid) {
|
||||
pid = ABS(pid);
|
||||
if ((event = GetConsoleCtrlEvent(sig)) != -1) {
|
||||
ok = !!GenerateConsoleCtrlEvent(
|
||||
event, __isfdkind(pid, kFdProcess) ? GetProcessId(g_fds.p[pid].handle)
|
||||
: pid);
|
||||
} else if (__isfdkind(pid, kFdProcess)) {
|
||||
ok = !!TerminateProcess(g_fds.p[pid].handle, 128 + sig);
|
||||
} else if ((handle = OpenProcess(kNtProcessAllAccess, false, pid))) {
|
||||
ok = !!TerminateProcess(handle, 128 + sig);
|
||||
CloseHandle(handle);
|
||||
} else {
|
||||
return __winerr();
|
||||
ok = false;
|
||||
}
|
||||
return ok ? 0 : __winerr();
|
||||
} else {
|
||||
return raise(sig);
|
||||
}
|
||||
}
|
||||
|
|
56
libc/calls/makedev.h
Normal file
56
libc/calls/makedev.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_CALLS_MAKEDEV_H_
|
||||
#define COSMOPOLITAN_LIBC_CALLS_MAKEDEV_H_
|
||||
#include "libc/dce.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
||||
static inline uint64_t major(uint64_t x) {
|
||||
if (IsXnu()) {
|
||||
return (x >> 24) & 0xff;
|
||||
} else if (IsNetbsd()) {
|
||||
return (x & 0x000fff00) >> 8;
|
||||
} else if (IsOpenbsd()) {
|
||||
return (x >> 8) & 0xff;
|
||||
} else if (IsFreebsd()) {
|
||||
return ((x >> 32) & 0xffffff00) | ((x >> 8) & 0x000000ff);
|
||||
} else {
|
||||
return ((x >> 32) & 0xfffff000) | ((x >> 8) & 0x00000fff);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint64_t minor(uint64_t x) {
|
||||
if (IsXnu()) {
|
||||
return x & 0x00ffffff;
|
||||
} else if (IsNetbsd()) {
|
||||
return (x & 0x000000ff) | (x & 0xfff00000) >> 12;
|
||||
} else if (IsOpenbsd()) {
|
||||
return (x & 0x000000ff) | (x & 0x0ffff000) >> 8;
|
||||
} else if (IsFreebsd()) {
|
||||
return ((x >> 24) & 0x0000ff00) | (x & 0xffff00ff);
|
||||
} else {
|
||||
return ((x >> 12) & 0xffffff00) | (x & 0x000000ff);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint64_t makedev(uint64_t x, uint64_t y) {
|
||||
if (IsXnu()) {
|
||||
return x << 24 | y;
|
||||
} else if (IsNetbsd()) {
|
||||
return ((x << 8) & 0x000fff00) | ((y << 12) & 0xfff00000u) |
|
||||
(y & 0x000000ff);
|
||||
} else if (IsOpenbsd()) {
|
||||
return (x & 0xff) << 8 | (y & 0xff) | (y & 0xffff00) << 8;
|
||||
} else if (IsFreebsd()) {
|
||||
return (x & 0xffffff00) << 32 | (x & 0x000000ff) << 8 |
|
||||
(y & 0x0000ff00) << 24 | (y & 0xffff00ff);
|
||||
} else {
|
||||
return (x & 0xfffff000) << 32 | (x & 0x00000fff) << 8 |
|
||||
(y & 0xffffff00) << 12 | (y & 0x000000ff);
|
||||
}
|
||||
}
|
||||
|
||||
#define major(x) major(x)
|
||||
#define minor(x) minor(x)
|
||||
#define makedev(x, y) makedev(x, y)
|
||||
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_CALLS_MAKEDEV_H_ */
|
|
@ -17,25 +17,12 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/getconsolectrlevent.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/nt/console.h"
|
||||
#include "libc/nt/enum/ctrlevent.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
|
||||
static textwindows uint32_t GetCtrlEvent(int sig) {
|
||||
switch (sig) {
|
||||
case SIGINT:
|
||||
return kNtCtrlCEvent;
|
||||
case SIGHUP:
|
||||
return kNtCtrlCloseEvent;
|
||||
case SIGQUIT:
|
||||
return kNtCtrlBreakEvent;
|
||||
default:
|
||||
ExitProcess(128 + sig);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends signal to this process.
|
||||
*
|
||||
|
@ -44,6 +31,7 @@ static textwindows uint32_t GetCtrlEvent(int sig) {
|
|||
* @asyncsignalsafe
|
||||
*/
|
||||
int raise(int sig) {
|
||||
int event;
|
||||
if (sig == SIGTRAP) {
|
||||
DebugBreak();
|
||||
return 0;
|
||||
|
@ -55,11 +43,13 @@ int raise(int sig) {
|
|||
}
|
||||
if (!IsWindows()) {
|
||||
return sys_kill(getpid(), sig, 1);
|
||||
} else {
|
||||
if (GenerateConsoleCtrlEvent(GetCtrlEvent(sig), 0)) {
|
||||
} else if ((event = GetConsoleCtrlEvent(sig))) {
|
||||
if (GenerateConsoleCtrlEvent(event, 0)) {
|
||||
return 0;
|
||||
} else {
|
||||
return __winerr();
|
||||
}
|
||||
} else {
|
||||
ExitProcess(128 + sig);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,13 +18,25 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/nt/enum/accessmask.h"
|
||||
#include "libc/nt/enum/tokeninformationclass.h"
|
||||
#include "libc/nt/errors.h"
|
||||
#include "libc/nt/files.h"
|
||||
#include "libc/nt/privilege.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/luid.h"
|
||||
#include "libc/nt/struct/tokenprivileges.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
static bool g_can_symlink;
|
||||
|
||||
textwindows int sys_symlinkat_nt(const char *target, int newdirfd,
|
||||
const char *linkpath) {
|
||||
const char *linkpath) {
|
||||
uint32_t flags;
|
||||
char16_t target16[PATH_MAX];
|
||||
char16_t linkpath16[PATH_MAX];
|
||||
if (!g_can_symlink) return eacces();
|
||||
flags = isdirectory(target) ? kNtSymbolicLinkFlagDirectory : 0;
|
||||
if (__mkntpathat(newdirfd, linkpath, 0, linkpath16) == -1) return -1;
|
||||
if (__mkntpath(target, target16) == -1) return -1;
|
||||
|
@ -34,3 +46,24 @@ textwindows int sys_symlinkat_nt(const char *target, int newdirfd,
|
|||
return __winerr();
|
||||
}
|
||||
}
|
||||
|
||||
static textstartup bool EnableSymlink(void) {
|
||||
int64_t tok;
|
||||
struct NtLuid id;
|
||||
struct NtTokenPrivileges tp;
|
||||
if (!OpenProcessToken(GetCurrentProcess(), kNtTokenAllAccess, &tok)) return 0;
|
||||
if (!LookupPrivilegeValue(0, u"SeCreateSymbolicLinkPrivilege", &id)) return 0;
|
||||
tp.PrivilegeCount = 1;
|
||||
tp.Privileges[0].Luid = id;
|
||||
tp.Privileges[0].Attributes = kNtSePrivilegeEnabled;
|
||||
if (!AdjustTokenPrivileges(tok, 0, &tp, sizeof(tp), 0, 0)) return 0;
|
||||
return GetLastError() != kNtErrorNotAllAssigned;
|
||||
}
|
||||
|
||||
static textstartup void g_can_symlink_init() {
|
||||
g_can_symlink = EnableSymlink();
|
||||
}
|
||||
|
||||
const void *const g_can_symlink_ctor[] initarray = {
|
||||
g_can_symlink_init,
|
||||
};
|
||||
|
|
|
@ -74,10 +74,17 @@ textwindows int sys_wait4_nt(int pid, int *opt_out_wstatus, int options,
|
|||
}
|
||||
if (opt_out_rusage) {
|
||||
memset(opt_out_rusage, 0, sizeof(*opt_out_rusage));
|
||||
GetProcessTimes(GetCurrentProcess(), &createfiletime, &exitfiletime,
|
||||
&kernelfiletime, &userfiletime);
|
||||
FileTimeToTimeVal(&opt_out_rusage->ru_utime, userfiletime);
|
||||
FileTimeToTimeVal(&opt_out_rusage->ru_stime, kernelfiletime);
|
||||
if (GetProcessTimes(g_fds.p[pids[i]].handle, &createfiletime,
|
||||
&exitfiletime, &kernelfiletime, &userfiletime)) {
|
||||
opt_out_rusage->ru_utime.tv_sec =
|
||||
ReadFileTime(userfiletime) / HECTONANOSECONDS;
|
||||
opt_out_rusage->ru_utime.tv_usec =
|
||||
ReadFileTime(userfiletime) % HECTONANOSECONDS;
|
||||
opt_out_rusage->ru_stime.tv_sec =
|
||||
ReadFileTime(kernelfiletime) / HECTONANOSECONDS;
|
||||
opt_out_rusage->ru_stime.tv_usec =
|
||||
ReadFileTime(kernelfiletime) % HECTONANOSECONDS;
|
||||
}
|
||||
}
|
||||
CloseHandle(g_fds.p[pids[i]].handle);
|
||||
g_fds.p[pids[i]].kind = kFdEmpty;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue