diff --git a/src/process/posix/SDL_posixprocess.c b/src/process/posix/SDL_posixprocess.c index 23a286023..ea67d82a6 100644 --- a/src/process/posix/SDL_posixprocess.c +++ b/src/process/posix/SDL_posixprocess.c @@ -72,6 +72,28 @@ static bool SetupStream(SDL_Process *process, int fd, const char *mode, const ch return true; } +static void IgnoreSignal(int sig) +{ +#ifdef HAVE_SIGACTION + struct sigaction action; + + sigaction(SIGPIPE, NULL, &action); +#ifdef HAVE_SA_SIGACTION + if (action.sa_handler == SIG_DFL && (void (*)(int))action.sa_sigaction == SIG_DFL) { +#else + if (action.sa_handler == SIG_DFL) { +#endif + action.sa_handler = SIG_IGN; + sigaction(sig, &action, NULL); + } +#elif defined(HAVE_SIGNAL_H) + void (*ohandler)(int) = signal(sig, SIG_IGN); + if (ohandler != SIG_DFL && ohandler != SIG_IGN) { + signal(sig, ohandler); + } +#endif +} + static bool CreatePipe(int fds[2]) { if (pipe(fds) < 0) { @@ -82,6 +104,9 @@ static bool CreatePipe(int fds[2]) fcntl(fds[READ_END], F_SETFD, fcntl(fds[READ_END], F_GETFD) | FD_CLOEXEC); fcntl(fds[WRITE_END], F_SETFD, fcntl(fds[WRITE_END], F_GETFD) | FD_CLOEXEC); + // Make sure we don't crash if we write when the pipe is closed + IgnoreSignal(SIGPIPE); + return true; }