mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 03:38:31 +00:00
Introduce interprocess signaling on Windows
This change gets rsync working without any warning or errors. On Windows we now create a bunch of C:\var\sig\x\y.pid shared memory files, so sigs can be delivered between processes. WinMain() creates this file when the process starts. If the program links signaling system calls then we make a thread at startup too, which allows asynchronous delivery each quantum and cancelation points can spot these signals potentially faster on wait See #1240
This commit is contained in:
parent
8527462b95
commit
0d74673213
22 changed files with 302 additions and 62 deletions
36
libc/intrin/itoa16.c
Normal file
36
libc/intrin/itoa16.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2024 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/fmt/internal.h"
|
||||
|
||||
textwindows char16_t *__itoa16(char16_t p[21], uint64_t x) {
|
||||
char t;
|
||||
size_t a, b, i = 0;
|
||||
do {
|
||||
p[i++] = x % 10 + '0';
|
||||
x = x / 10;
|
||||
} while (x > 0);
|
||||
if (i) {
|
||||
for (a = 0, b = i - 1; a < b; ++a, --b) {
|
||||
t = p[a];
|
||||
p[a] = p[b];
|
||||
p[b] = t;
|
||||
}
|
||||
}
|
||||
return p + i;
|
||||
}
|
|
@ -17,11 +17,25 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/fmt/internal.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/nt/createfile.h"
|
||||
#include "libc/nt/enum/accessmask.h"
|
||||
#include "libc/nt/enum/creationdisposition.h"
|
||||
#include "libc/nt/enum/fileflagandattributes.h"
|
||||
#include "libc/nt/enum/filemapflags.h"
|
||||
#include "libc/nt/enum/filemovemethod.h"
|
||||
#include "libc/nt/enum/filesharemode.h"
|
||||
#include "libc/nt/enum/pageflags.h"
|
||||
#include "libc/nt/files.h"
|
||||
#include "libc/nt/memory.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/thread/tls.h"
|
||||
|
||||
struct Signals __sig;
|
||||
|
@ -52,3 +66,65 @@ void __sig_unblock(sigset_t m) {
|
|||
sys_sigprocmask(SIG_SETMASK, &m, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __x86_64__
|
||||
|
||||
textwindows char16_t *__sig_process_path(char16_t *path, uint32_t pid) {
|
||||
char16_t *p = path;
|
||||
*p++ = 'C';
|
||||
*p++ = ':';
|
||||
*p++ = '\\';
|
||||
*p++ = 'v';
|
||||
*p++ = 'a';
|
||||
*p++ = 'r';
|
||||
*p = 0;
|
||||
CreateDirectory(path, 0);
|
||||
*p++ = '\\';
|
||||
*p++ = 's';
|
||||
*p++ = 'i';
|
||||
*p++ = 'g';
|
||||
*p = 0;
|
||||
CreateDirectory(path, 0);
|
||||
*p++ = '\\';
|
||||
p = __itoa16(p, (pid & 0x000fff00) >> 8);
|
||||
*p = 0;
|
||||
CreateDirectory(path, 0);
|
||||
*p++ = '\\';
|
||||
p = __itoa16(p, pid);
|
||||
*p++ = '.';
|
||||
*p++ = 'p';
|
||||
*p++ = 'i';
|
||||
*p++ = 'd';
|
||||
*p = 0;
|
||||
return path;
|
||||
}
|
||||
|
||||
textwindows static atomic_ulong *__sig_map_process_impl(int pid) {
|
||||
char16_t path[128];
|
||||
intptr_t hand = CreateFile(__sig_process_path(path, pid),
|
||||
kNtGenericRead | kNtGenericWrite,
|
||||
kNtFileShareRead | kNtFileShareWrite, 0,
|
||||
kNtOpenAlways, kNtFileAttributeNormal, 0);
|
||||
if (hand == -1)
|
||||
return 0;
|
||||
SetFilePointer(hand, 8, 0, kNtFileBegin);
|
||||
SetEndOfFile(hand);
|
||||
intptr_t map = CreateFileMapping(hand, 0, kNtPageReadwrite, 0, 8, 0);
|
||||
if (!map) {
|
||||
CloseHandle(hand);
|
||||
return 0;
|
||||
}
|
||||
atomic_ulong *sigs = MapViewOfFileEx(map, kNtFileMapWrite, 0, 0, 8, 0);
|
||||
CloseHandle(map);
|
||||
CloseHandle(hand);
|
||||
return sigs;
|
||||
}
|
||||
|
||||
textwindows atomic_ulong *__sig_map_process(int pid) {
|
||||
int e = errno;
|
||||
atomic_ulong *res = __sig_map_process_impl(pid);
|
||||
errno = e;
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
|
@ -34,7 +35,7 @@ textwindows int __sig_mask(int how, const sigset_t *neu, sigset_t *old) {
|
|||
}
|
||||
|
||||
// get address of sigset to modify
|
||||
_Atomic(uint64_t) *mask = &__get_tls()->tib_sigmask;
|
||||
atomic_ulong *mask = &__get_tls()->tib_sigmask;
|
||||
|
||||
// handle read-only case
|
||||
sigset_t oldmask;
|
||||
|
|
|
@ -16,8 +16,16 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/nt/files.h"
|
||||
#include "libc/nt/memory.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/thunk/msabi.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
__msabi extern typeof(TerminateProcess) *const __imp_TerminateProcess;
|
||||
|
||||
|
@ -25,8 +33,20 @@ __msabi extern typeof(TerminateProcess) *const __imp_TerminateProcess;
|
|||
* Terminates the calling process and all of its threads.
|
||||
*/
|
||||
textwindows dontinstrument void TerminateThisProcess(uint32_t dwWaitStatus) {
|
||||
|
||||
// delete sig file
|
||||
char16_t path[128];
|
||||
atomic_ulong *real;
|
||||
atomic_ulong fake = 0;
|
||||
real = __sig.process;
|
||||
__sig.process = &fake;
|
||||
UnmapViewOfFile(real);
|
||||
DeleteFile(__sig_process_path(path, __pid));
|
||||
|
||||
// "When a process terminates itself, TerminateProcess stops execution
|
||||
// of the calling thread and does not return." -Quoth MSDN
|
||||
__imp_TerminateProcess(-1, dwWaitStatus);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue