Paul Eggert wrote:
> In looking into this sigprocmask business some more, I discovered a 
> problem that can lead to subtle race condition bugs on macOS. Gnulib 
> assumes that in a multithreaded process sigprocmask behaves like 
> pthread_sigmask (aside from minor API changes) if both functions exist. 
> Although that's typically true, it's false on Darwin-based systems[1] 
> such as macOS.

Indeed. Looking at the other *BSD platforms, it appears that only macOS
is affected:

* macOS
  xnu/bsd/kern/kern_sig.c
  __pthread_sigmask manipulates the thread's uu_sigmask.
  sigprocmask manipulates all threads' uu_sigmask and the process' p_sigmask.
* FreeBSD
  src/sys/kern/kern_sig.c
  sys_sigprocmask invokes kern_sigprocmask, which operates on a single thread.
* NetBSD
  src/sys/kern/sys_sig.c
  sigprocmask1 operates on a single thread (struct lwp).
* OpenBSD
  src/lib/libc/sys/pthread_sigmask.c
  pthread_sigmask just invokes sigprocmask.

But since POSIX [1] says:
  "The sigprocmask() function shall be equivalent to pthread_sigmask(),
   except that its behavior is unspecified if called from a multi-threaded
   process, and ..."

I agree with you that Gnulib should favour calling pthread_sigmask over
sigprocmask.

> So I propose altering Gnulib to do things as POSIX suggests

I agree with that.

> adjusting linking instructions as needed.

Here, I think instead of requiring linking with '-lpthread' from more
modules, it is better to remove that linking requirement from the
'pthread_sigmask' module. Recall that this linking requirement exists
only on glibc ≤ 2.31, NetBSD, OpenBSD ≤ 5.8, AIX. For glibc, NetBSD,
OpenBSD, we know that pthread_sigmask and sigprocmask are equivalent.
Only for AIX, this can't be done [2][3][4].

> In the meantime I installed the attached 
> patch to document the issue, and to add FIXMEs where it seems that 
> sigprocmask should be replaced by pthread_sigmask.

Thanks!

Bruno

[1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/sigprocmask.html
[2] 
https://www.ibm.com/docs/en/aix/7.2.0?topic=s-sigprocmask-sigsetmask-sigblock-subroutine
[3] 
https://www.ibm.com/docs/en/aix/7.2.0?topic=s-sigthreadmask-subroutine#sigthreadmask
[4] https://www.ibm.com/docs/en/aix/7.2.0?topic=p-pthread-sigmask-subroutine




Reply via email to