Skip to content

Commit 8527462

Browse files
committed
Fix ECHILD with WNOHANG on Windows
This change along with a patch for rsync's safe_write() function that'll that'll soon be added to superconfigure, gets rsync working. There's one remaining issue (which isn't a blocker) which is how rsync logs an error about abnormal process termination since there's currently no way for us to send non-fatal signals between processes. rsync in cosmos is restored Fixes #1240
1 parent 8313dca commit 8527462

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

libc/proc/wait4-nt.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,20 @@ static textwindows int __proc_wait(int pid, int *wstatus, int options,
8888
__proc_lock();
8989
CheckForZombies:
9090
int rc = __proc_check(pid, wstatus, rusage);
91-
if (rc || (options & WNOHANG)) {
92-
__proc_unlock();
93-
return rc;
94-
}
9591

96-
// there's no zombies left
92+
// if there's no zombies left
9793
// check if there's any living processes
98-
if (dll_is_empty(__proc.list)) {
94+
if (!rc && dll_is_empty(__proc.list)) {
9995
__proc_unlock();
10096
return echild();
10197
}
10298

99+
// otherwise return zombie or zero
100+
if (rc || (options & WNOHANG)) {
101+
__proc_unlock();
102+
return rc;
103+
}
104+
103105
// get appropriate wait object
104106
// register ourself as waiting
105107
struct Proc *pr = 0;

test/posix/wait2x_test.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2024 Justine Alexandra Roberts Tunney
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for
4+
// any purpose with or without fee is hereby granted, provided that the
5+
// above copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
8+
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
9+
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
10+
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
11+
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
12+
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
13+
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14+
// PERFORMANCE OF THIS SOFTWARE.
15+
16+
#include <errno.h>
17+
#include <stdatomic.h>
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
#include <sys/mman.h>
21+
#include <sys/wait.h>
22+
#include <unistd.h>
23+
24+
int main(int argc, char *argv[]) {
25+
int ws, rc;
26+
27+
// create shared memory for synchronization
28+
atomic_int *ready =
29+
mmap(0, 4, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
30+
31+
if ((rc = waitpid(-1, &ws, WNOHANG)) != -1)
32+
return 1;
33+
if (errno != ECHILD)
34+
return 2;
35+
36+
// create process
37+
int pid = fork();
38+
if (pid == -1)
39+
return 3;
40+
if (!pid) {
41+
for (;;)
42+
if (*ready)
43+
break;
44+
_Exit(0);
45+
}
46+
47+
// wait on process
48+
if ((rc = waitpid(pid, &ws, WNOHANG)) == -1)
49+
return 4;
50+
if (rc != 0)
51+
return 5;
52+
if (ws)
53+
return 6;
54+
55+
// signal subprocess
56+
*ready = 1;
57+
58+
if ((rc = waitpid(pid, &ws, 0)) == -1)
59+
return 7;
60+
if (rc != pid)
61+
return 8;
62+
if (ws)
63+
return 9;
64+
65+
// wait again
66+
if ((rc = waitpid(pid, &ws, WNOHANG)) != -1)
67+
return 10;
68+
if (errno != ECHILD)
69+
return 11;
70+
}

0 commit comments

Comments
 (0)