Skip to content

Instantly share code, notes, and snippets.

@vtjnash
Created August 17, 2021 22:03
Show Gist options
  • Save vtjnash/679174286cf282c298e5d11752343b8a to your computer and use it in GitHub Desktop.
Save vtjnash/679174286cf282c298e5d11752343b8a to your computer and use it in GitHub Desktop.
SIGCHLD bug with macOS mach xnu posix_spawn
/* Copyright libuv contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/*
This test demonstrates a case in which the macOS xnu mach kernel will reliably
fail to deliver the SIGCHLD signal most of the time.
*/
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <spawn.h>
#include <errno.h>
#include "assert.h"
static volatile int exit_cb_called;
void sig_chld(int sig) {
const char* msg = "SIGCHLD\n";
write(2, msg, strlen(msg));
exit_cb_called++;
}
int main(void) {
pid_t pid;
char *args[4];
int status;
signal(SIGCHLD, sig_chld);
args[0] = "sh";
args[1] = "-c";
args[2] = "exit 12";
args[3] = NULL;
int r = posix_spawn(&pid, "/bin/sh", NULL, NULL, args, NULL);
assert(r == 0);
for (int i = 0; i < 1000; i++) {
pid_t pid;
int r = posix_spawn(&pid, "does not exist", NULL, NULL, args, NULL);
assert(r == ENOENT);
}
assert(sleep(2) >= 0);
assert(exit_cb_called);
printf("exit_cb_called: %d\n", exit_cb_called);
//do {
r = waitpid(pid, &status, 0);
//} while (r == -1 && errno == EINTR);
assert(r == pid);
r = WIFEXITED(status);
assert(r);
r = WEXITSTATUS(status);
assert(r == 12);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment