EINTR on 'wait'

From: Volodymyr Boyko
Date: Thu Jan 17 2019 - 15:55:40 EST


Hi,

According to http://pubs.opengroup.org/onlinepubs/009695399/functions/wait.html:
>
> The wait() function shall fail if:
> [ECHILD]
> The calling process has no existing unwaited-for child processes.
> [EINTR]
> The function was interrupted by a signal. The value of the location pointed to by stat_loc is undefined.
and man -e 7 signal:
>
> If a signal handler is invoked while a system call or library function call is blocked, then either:
>
> * the call is automatically restarted after the signal handler returns; or
>
> * the call fails with the error EINTR.

> If a blocked call to one of the following interfaces is interrupted by a signal handler, then the call will be automatically restarted after the signal handler returns if the
> SA_RESTART flag was used; otherwise the call will fail with the error EINTR:
>
> * wait(2), wait3(2), wait4(2), waitid(2), and waitpid(2).

So from my understanding of above, if we are blocked in 'wait' and
receiving a signal (SIGCHLD), the call should return with value -1 and
errno set to EINTR.
But the following snippet behaves differently on linux 4.15.0-43
and freebsd 11.2:
>
> #include <stdlib.h>
> #include <unistd.h>
> #include <signal.h>
> #include <stdio.h>
> #include <string.h>
> #include <errno.h>
> #include <sys/types.h>
> #include <sys/wait.h>
> static void sigchld_handler(int signo)
> {
> const char *const msg = "\nSIGCHLD";
> (void)signo;
> write(STDERR_FILENO, msg, strlen(msg));
> }
> int main(void)
> {
> pid_t wait_ret = 0, pid = 0;
> int status = 0, ret = -1;
>
>
>
> sigaction(SIGCHLD, &(struct sigaction){
> .sa_handler = sigchld_handler,
> }, NULL);
>
>
>
> if ((pid = fork()) < 0)
> {
> perror("\nfork: ");
> goto Exit;
> }
> if (!pid)
> {
> sleep(3);
> return 0;
> }
> if ((wait_ret = wait(&status)) < 0)
> {
> perror("\nwait: ");
> goto Exit;
> }
> fprintf(stderr, "\nwait done, pid %d", wait_ret);
> ret = 0;
> Exit:
> fprintf(stderr, "\n");
> return ret;
> }

freebsd and solaris do share my understanding:
>
> SIGCHLD
> wait: : Interrupted system call

but linux:
>
> SIGCHLD
> wait done, pid <child pid>

As to me, it deserves a deeper investigation (if it already is not
proved to be or not to be an issue - I haven't found any mention about
this yet, though dug only a little). if it's really Linux's
nonconformity to POSIX, it should be at least noted in 'wait' manuals.

Thanks.