PTHREAD_SIGMASK(3P)

πŸ’– PTHREAD_SIGMASK(3P) πŸ’–

okay so bestie, we're in the POSIX Programmer's Manual rn, and this one is all about pthread_sigmask, sigprocmask β€” the blocked-signals girlies. let's get into it 🫢


✨ PROLOG ✨

so like, this manual page is part of the POSIX Programmer's Manual. heads up though: the Linux implementation of this interface may differ (so go check the corresponding Linux manual page for the deets on Linux behavior), or the interface might not even be implemented on Linux at all. 😭


🌸 NAME 🌸

pthread_sigmask, sigprocmask β€” examine and change blocked signals. yes, they're a duo. iconic behavior.


✨ SYNOPSIS ✨

#include <signal.h>

int pthread_sigmask(int how, const sigset_t *restrict set,
    sigset_t *restrict oset);
int sigprocmask(int how, const sigset_t *restrict set,
    sigset_t *restrict oset);

πŸ’… DESCRIPTION πŸ’…

so pthread_sigmask() shall examine or change (or both, she's flexible πŸ’…) the calling thread's signal mask, no matter how many threads are vibing in the process. literally the whole point. The function shall be equivalent to sigprocmask(), just without that restriction that the call has to be made in a single-threaded process. main character energy, honestly.

In a single-threaded process, the sigprocmask() function shall examine or change (or both) the signal mask of the calling thread.

If the argument set is not a null pointer, it points to a set of signals to be used to change the currently blocked set.

The argument how indicates the way in which the set is changed, and the application shall ensure it consists of one of the following values:

  • SIG_BLOCK β€” The resulting set shall be the union of the current set and the signal set pointed to by set.
  • SIG_SETMASK β€” The resulting set shall be the signal set pointed to by set.
  • SIG_UNBLOCK β€” The resulting set shall be the intersection of the current set and the complement of the signal set pointed to by set.

If the argument oset is not a null pointer, the previous mask shall be stored in the location pointed to by oset. And if set is a null pointer, the value of the argument how is not significant and the thread's signal mask shall be unchanged β€” so basically you can use the call to snoop on currently blocked signals. πŸ‘€

If there are any pending unblocked signals after the call to sigprocmask(), at least one of those signals shall be delivered before the call to sigprocmask() returns.

it is not possible to block those signals which cannot be ignored. This shall be enforced by the system without causing an error to be indicated. so like, don't fight it, she just won't let you. 🚫

If any of the SIGFPE, SIGILL, SIGSEGV, or SIGBUS signals are generated while they are blocked, the result is undefined, unless the signal was generated by the action of another process, or by one of the functions kill(), pthread_kill(), raise(), or sigqueue(). so yeah, undefined behavior, be careful bestie. πŸ’€

If sigprocmask() fails, the thread's signal mask shall not be changed.

The use of the sigprocmask() function is unspecified in a multi-threaded process. 🫣


⭐ RETURN VALUE ⭐

Upon successful completion pthread_sigmask() shall return 0; otherwise, it shall return the corresponding error number.

Upon successful completion, sigprocmask() shall return 0; otherwise, -1 shall be returned, errno shall be set to indicate the error, and the signal mask of the process shall be unchanged.


πŸ”₯ ERRORS πŸ”₯

The pthread_sigmask() and sigprocmask() functions shall fail if:

  • EINVAL β€” The value of the how argument is not equal to one of the defined values.

The pthread_sigmask() function shall not return an error code of [EINTR].

The following sections are informative. (aka the bonus content, keep scrolling πŸ’–)


πŸŽ€ EXAMPLES πŸŽ€

Signaling in a Multi-Threaded Process 🧡

This example shows the use of pthread_sigmask() in order to deal with signals in a multi-threaded process. It provides a fairly general framework that could be easily adapted/extended. slay.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
...

static sigset_t   signal_mask;  /* signals to block         */

int main (int argc, char *argv[])
{
    pthread_t  sig_thr_id;      /* signal handler thread ID */
    int        rc;              /* return code              */

    sigemptyset (&signal_mask);
    sigaddset (&signal_mask, SIGINT);
    sigaddset (&signal_mask, SIGTERM);
    rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL);
    if (rc != 0) {
        /* handle error */
        ...
    }
    /* any newly created threads inherit the signal mask */

    rc = pthread_create (&sig_thr_id, NULL, signal_thread, NULL);
    if (rc != 0) {
        /* handle error */
        ...
    }

    /* APPLICATION CODE */
    ...
}

void *signal_thread (void *arg)
{
    int       sig_caught;    /* signal caught       */
    int       rc;            /* returned code       */

    rc = sigwait (&signal_mask, &sig_caught);
    if (rc != 0) {
        /* handle error */
    }
    switch (sig_caught)
    {
    case SIGINT:     /* process SIGINT  */
        ...
        break;
    case SIGTERM:    /* process SIGTERM */
        ...
        break;
    default:         /* should normally not happen */
        fprintf (stderr, "\nUnexpected signal %d\n", sig_caught);
        break;
    }
}

🌈 APPLICATION USAGE 🌈

None. (empty. a clean slate. we love a minimalist queen ✨)


πŸ’œ RATIONALE πŸ’œ

When a thread's signal mask is changed in a signal-catching function that is installed by sigaction(), the restoration of the signal mask on return from the signal-catching function overrides that change (see sigaction()). and if the signal-catching function was installed with signal(), it is unspecified whether this occurs. πŸ™ˆ

See kill() for a discussion of the requirement on delivery of signals.


πŸ¦‹ FUTURE DIRECTIONS πŸ¦‹

None. (the future is, apparently, more of the same 🫢)


πŸ‘‘ SEE ALSO πŸ‘‘

exec, kill(), sigaction(), sigaddset(), sigdelset(), sigemptyset(), sigfillset(), sigismember(), sigpending(), sigqueue(), sigsuspend()

The Base Definitions volume of POSIX.1‐2017, <signal.h>


Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page bestie β€” brought to you by IEEE/The Open Group, 2017, page PTHREAD_SIGMASK(3P). go block some signals responsibly. periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from