CINXE.COM
signal-safety
<html> <head> <title>signal-safety</title> <META NAME="KEYWORDS" CONTENT="signal-safety"> </head> <body BGCOLOR="#ffffff" LINK="#0000ff" VLINK="#0000ff" ALINK="#0000ff" TEXT="#000000"> <center> <h1><b>signal-safety</b></h1></center> <PRE> <STRONG><A HREF="/man7/SIGNAL-SAFETY">SIGNAL-SAFETY(7)</A></STRONG> Linux Programmer's Manual <STRONG><A HREF="/man7/SIGNAL-SAFETY">SIGNAL-SAFETY(7)</A></STRONG> NAME signal-safety - async-signal-safe functions DESCRIPTION An async-signal-safe function is one that can be safely called from within a signal handler. Many functions are not async-signal-safe. In particular, nonreentrant functions are generally unsafe to call from a signal handler. The kinds of issues that render a function unsafe can be quickly under- stood when one considers the implementation of the stdio library, all of whose functions are not async-signal-safe. When performing buffered I/O on a file, the stdio functions must main- tain a statically allocated data buffer along with associated counters and indexes (or pointers) that record the amount of data and the cur- rent position in the buffer. Suppose that the main program is in the middle of a call to a stdio function such as <STRONG><A HREF="/man3/printf">printf(3)</A></STRONG> where the buffer and associated variables have been partially updated. If, at that mo- ment, the program is interrupted by a signal handler that also calls <STRONG><A HREF="/man3/printf">printf(3)</A></STRONG>, then the second call to <STRONG><A HREF="/man3/printf">printf(3)</A></STRONG> will operate on inconsis- tent data, with unpredictable results. To avoid problems with unsafe functions, there are two possible choices: 1. Ensure that (a) the signal handler calls only async-signal-safe functions, and (b) the signal handler itself is reentrant with re- spect to global variables in the main program. 2. Block signal delivery in the main program when calling functions that are unsafe or operating on global data that is also accessed by the signal handler. Generally, the second choice is difficult in programs of any complex- ity, so the first choice is taken. POSIX.1 specifies a set of functions that an implementation must make async-signal-safe. (An implementation may provide safe implementations of additional functions, but this is not required by the standard and other implementations may not provide the same guarantees.) In general, a function is async-signal-safe either because it is reen- trant or because it is atomic with respect to signals (i.e., its execu- tion can't be interrupted by a signal handler). The set of functions required to be async-signal-safe by POSIX.1 is shown in the following table. The functions not otherwise noted were required to be async-signal-safe in POSIX.1-2001; the table details changes in the subsequent standards. Function Notes <STRONG><A HREF="/man3/abort">abort(3)</A></STRONG> Added in POSIX.1-2003 <STRONG><A HREF="/man2/accept">accept(2)</A></STRONG> <STRONG><A HREF="/man2/access">access(2)</A></STRONG> <STRONG><A HREF="/man3/aio_error">aio_error(3)</A></STRONG> <STRONG><A HREF="/man3/aio_return">aio_return(3)</A></STRONG> <STRONG><A HREF="/man3/aio_suspend">aio_suspend(3)</A></STRONG> See notes below <STRONG><A HREF="/man2/alarm">alarm(2)</A></STRONG> <STRONG><A HREF="/man2/bind">bind(2)</A></STRONG> <STRONG><A HREF="/man3/cfgetispeed">cfgetispeed(3)</A></STRONG> <STRONG><A HREF="/man3/cfgetospeed">cfgetospeed(3)</A></STRONG> <STRONG><A HREF="/man3/cfsetispeed">cfsetispeed(3)</A></STRONG> <STRONG><A HREF="/man3/cfsetospeed">cfsetospeed(3)</A></STRONG> <STRONG><A HREF="/man2/chdir">chdir(2)</A></STRONG> <STRONG><A HREF="/man2/chmod">chmod(2)</A></STRONG> <STRONG><A HREF="/man2/chown">chown(2)</A></STRONG> <STRONG><A HREF="/man2/clock_gettime">clock_gettime(2)</A></STRONG> <STRONG><A HREF="/man2/close">close(2)</A></STRONG> <STRONG><A HREF="/man2/connect">connect(2)</A></STRONG> <STRONG><A HREF="/man2/creat">creat(2)</A></STRONG> <STRONG><A HREF="/man2/dup">dup(2)</A></STRONG> <STRONG><A HREF="/man2/dup2">dup2(2)</A></STRONG> <STRONG><A HREF="/man3/execl">execl(3)</A></STRONG> Added in POSIX.1-2008; see notes below <STRONG><A HREF="/man3/execle">execle(3)</A></STRONG> See notes below <STRONG><A HREF="/man3/execv">execv(3)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/execve">execve(2)</A></STRONG> <STRONG><A HREF="/man2/_exit">_exit(2)</A></STRONG> <STRONG><A HREF="/man2/_Exit">_Exit(2)</A></STRONG> <STRONG><A HREF="/man2/faccessat">faccessat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/fchdir">fchdir(2)</A></STRONG> Added in POSIX.1-2013 <STRONG><A HREF="/man2/fchmod">fchmod(2)</A></STRONG> <STRONG><A HREF="/man2/fchmodat">fchmodat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/fchown">fchown(2)</A></STRONG> <STRONG><A HREF="/man2/fchownat">fchownat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/fcntl">fcntl(2)</A></STRONG> <STRONG><A HREF="/man2/fdatasync">fdatasync(2)</A></STRONG> <STRONG><A HREF="/man3/fexecve">fexecve(3)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man3/ffs">ffs(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man2/fork">fork(2)</A></STRONG> See notes below <STRONG><A HREF="/man2/fstat">fstat(2)</A></STRONG> <STRONG><A HREF="/man2/fstatat">fstatat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/fsync">fsync(2)</A></STRONG> <STRONG><A HREF="/man2/ftruncate">ftruncate(2)</A></STRONG> <STRONG><A HREF="/man3/futimens">futimens(3)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/getegid">getegid(2)</A></STRONG> <STRONG><A HREF="/man2/geteuid">geteuid(2)</A></STRONG> <STRONG><A HREF="/man2/getgid">getgid(2)</A></STRONG> <STRONG><A HREF="/man2/getgroups">getgroups(2)</A></STRONG> <STRONG><A HREF="/man2/getpeername">getpeername(2)</A></STRONG> <STRONG><A HREF="/man2/getpgrp">getpgrp(2)</A></STRONG> <STRONG><A HREF="/man2/getpid">getpid(2)</A></STRONG> <STRONG><A HREF="/man2/getppid">getppid(2)</A></STRONG> <STRONG><A HREF="/man2/getsockname">getsockname(2)</A></STRONG> <STRONG><A HREF="/man2/getsockopt">getsockopt(2)</A></STRONG> <STRONG><A HREF="/man2/getuid">getuid(2)</A></STRONG> <STRONG><A HREF="/man3/htonl">htonl(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/htons">htons(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man2/kill">kill(2)</A></STRONG> <STRONG><A HREF="/man2/link">link(2)</A></STRONG> <STRONG><A HREF="/man2/linkat">linkat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/listen">listen(2)</A></STRONG> <STRONG><A HREF="/man3/longjmp">longjmp(3)</A></STRONG> Added in POSIX.1-2016; see notes below <STRONG><A HREF="/man2/lseek">lseek(2)</A></STRONG> <STRONG><A HREF="/man2/lstat">lstat(2)</A></STRONG> <STRONG><A HREF="/man3/memccpy">memccpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/memchr">memchr(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/memcmp">memcmp(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/memcpy">memcpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/memmove">memmove(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/memset">memset(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man2/mkdir">mkdir(2)</A></STRONG> <STRONG><A HREF="/man2/mkdirat">mkdirat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man3/mkfifo">mkfifo(3)</A></STRONG> <STRONG><A HREF="/man3/mkfifoat">mkfifoat(3)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/mknod">mknod(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/mknodat">mknodat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man3/ntohl">ntohl(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/ntohs">ntohs(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man2/open">open(2)</A></STRONG> <STRONG><A HREF="/man2/openat">openat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/pause">pause(2)</A></STRONG> <STRONG><A HREF="/man2/pipe">pipe(2)</A></STRONG> <STRONG><A HREF="/man2/poll">poll(2)</A></STRONG> <STRONG><A HREF="/man3/posix_trace_event">posix_trace_event(3)</A></STRONG> <STRONG><A HREF="/man2/pselect">pselect(2)</A></STRONG> <STRONG><A HREF="/man3/pthread_kill">pthread_kill(3)</A></STRONG> Added in POSIX.1-2013 <STRONG><A HREF="/man3/pthread_self">pthread_self(3)</A></STRONG> Added in POSIX.1-2013 <STRONG><A HREF="/man3/pthread_sigmask">pthread_sigmask(3)</A></STRONG> Added in POSIX.1-2013 <STRONG><A HREF="/man3/raise">raise(3)</A></STRONG> <STRONG><A HREF="/man2/read">read(2)</A></STRONG> <STRONG><A HREF="/man2/readlink">readlink(2)</A></STRONG> <STRONG><A HREF="/man2/readlinkat">readlinkat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/recv">recv(2)</A></STRONG> <STRONG><A HREF="/man2/recvfrom">recvfrom(2)</A></STRONG> <STRONG><A HREF="/man2/recvmsg">recvmsg(2)</A></STRONG> <STRONG><A HREF="/man2/rename">rename(2)</A></STRONG> <STRONG><A HREF="/man2/renameat">renameat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/rmdir">rmdir(2)</A></STRONG> <STRONG><A HREF="/man2/select">select(2)</A></STRONG> <STRONG><A HREF="/man3/sem_post">sem_post(3)</A></STRONG> <STRONG><A HREF="/man2/send">send(2)</A></STRONG> <STRONG><A HREF="/man2/sendmsg">sendmsg(2)</A></STRONG> <STRONG><A HREF="/man2/sendto">sendto(2)</A></STRONG> <STRONG><A HREF="/man2/setgid">setgid(2)</A></STRONG> <STRONG><A HREF="/man2/setpgid">setpgid(2)</A></STRONG> <STRONG><A HREF="/man2/setsid">setsid(2)</A></STRONG> <STRONG><A HREF="/man2/setsockopt">setsockopt(2)</A></STRONG> <STRONG><A HREF="/man2/setuid">setuid(2)</A></STRONG> <STRONG><A HREF="/man2/shutdown">shutdown(2)</A></STRONG> <STRONG><A HREF="/man2/sigaction">sigaction(2)</A></STRONG> <STRONG><A HREF="/man3/sigaddset">sigaddset(3)</A></STRONG> <STRONG><A HREF="/man3/sigdelset">sigdelset(3)</A></STRONG> <STRONG><A HREF="/man3/sigemptyset">sigemptyset(3)</A></STRONG> <STRONG><A HREF="/man3/sigfillset">sigfillset(3)</A></STRONG> <STRONG><A HREF="/man3/sigismember">sigismember(3)</A></STRONG> <STRONG><A HREF="/man3/siglongjmp">siglongjmp(3)</A></STRONG> Added in POSIX.1-2016; see notes below <STRONG><A HREF="/man2/signal">signal(2)</A></STRONG> <STRONG><A HREF="/man3/sigpause">sigpause(3)</A></STRONG> <STRONG><A HREF="/man2/sigpending">sigpending(2)</A></STRONG> <STRONG><A HREF="/man2/sigprocmask">sigprocmask(2)</A></STRONG> <STRONG><A HREF="/man2/sigqueue">sigqueue(2)</A></STRONG> <STRONG><A HREF="/man3/sigset">sigset(3)</A></STRONG> <STRONG><A HREF="/man2/sigsuspend">sigsuspend(2)</A></STRONG> <STRONG><A HREF="/man3/sleep">sleep(3)</A></STRONG> <STRONG><A HREF="/man3/sockatmark">sockatmark(3)</A></STRONG> Added in POSIX.1-2004 <STRONG><A HREF="/man2/socket">socket(2)</A></STRONG> <STRONG><A HREF="/man2/socketpair">socketpair(2)</A></STRONG> <STRONG><A HREF="/man2/stat">stat(2)</A></STRONG> <STRONG><A HREF="/man3/stpcpy">stpcpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/stpncpy">stpncpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strcat">strcat(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strchr">strchr(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strcmp">strcmp(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strcpy">strcpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strcspn">strcspn(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strlen">strlen(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strncat">strncat(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strncmp">strncmp(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strncpy">strncpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strnlen">strnlen(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strpbrk">strpbrk(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strrchr">strrchr(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strspn">strspn(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strstr">strstr(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/strtok_r">strtok_r(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man2/symlink">symlink(2)</A></STRONG> <STRONG><A HREF="/man2/symlinkat">symlinkat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man3/tcdrain">tcdrain(3)</A></STRONG> <STRONG><A HREF="/man3/tcflow">tcflow(3)</A></STRONG> <STRONG><A HREF="/man3/tcflush">tcflush(3)</A></STRONG> <STRONG><A HREF="/man3/tcgetattr">tcgetattr(3)</A></STRONG> <STRONG><A HREF="/man3/tcgetpgrp">tcgetpgrp(3)</A></STRONG> <STRONG><A HREF="/man3/tcsendbreak">tcsendbreak(3)</A></STRONG> <STRONG><A HREF="/man3/tcsetattr">tcsetattr(3)</A></STRONG> <STRONG><A HREF="/man3/tcsetpgrp">tcsetpgrp(3)</A></STRONG> <STRONG><A HREF="/man2/time">time(2)</A></STRONG> <STRONG><A HREF="/man2/timer_getoverrun">timer_getoverrun(2)</A></STRONG> <STRONG><A HREF="/man2/timer_gettime">timer_gettime(2)</A></STRONG> <STRONG><A HREF="/man2/timer_settime">timer_settime(2)</A></STRONG> <STRONG><A HREF="/man2/times">times(2)</A></STRONG> <STRONG><A HREF="/man2/umask">umask(2)</A></STRONG> <STRONG><A HREF="/man2/uname">uname(2)</A></STRONG> <STRONG><A HREF="/man2/unlink">unlink(2)</A></STRONG> <STRONG><A HREF="/man2/unlinkat">unlinkat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/utime">utime(2)</A></STRONG> <STRONG><A HREF="/man2/utimensat">utimensat(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/utimes">utimes(2)</A></STRONG> Added in POSIX.1-2008 <STRONG><A HREF="/man2/wait">wait(2)</A></STRONG> <STRONG><A HREF="/man2/waitpid">waitpid(2)</A></STRONG> <STRONG><A HREF="/man3/wcpcpy">wcpcpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcpncpy">wcpncpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcscat">wcscat(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcschr">wcschr(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcscmp">wcscmp(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcscpy">wcscpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcscspn">wcscspn(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcslen">wcslen(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcsncat">wcsncat(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcsncmp">wcsncmp(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcsncpy">wcsncpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcsnlen">wcsnlen(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcspbrk">wcspbrk(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcsrchr">wcsrchr(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcsspn">wcsspn(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcsstr">wcsstr(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wcstok">wcstok(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wmemchr">wmemchr(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wmemcmp">wmemcmp(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wmemcpy">wmemcpy(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wmemmove">wmemmove(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man3/wmemset">wmemset(3)</A></STRONG> Added in POSIX.1-2016 <STRONG><A HREF="/man2/write">write(2)</A></STRONG> Notes: * POSIX.1-2001 and POSIX.1-2004 required the functions <STRONG><A HREF="/man3/fpathconf">fpathconf(3)</A></STRONG>, <STRONG><A HREF="/man3/pathconf">pathconf(3)</A></STRONG>, and <STRONG><A HREF="/man3/sysconf">sysconf(3)</A></STRONG> to be async-signal-safe, but this re- quirement was removed in POSIX.1-2008. * If a signal handler interrupts the execution of an unsafe function, and the handler terminates via a call to <STRONG><A HREF="/man3/longjmp">longjmp(3)</A></STRONG> or <STRONG><A HREF="/man3/siglongjmp">siglongjmp(3)</A></STRONG> and the program subsequently calls an unsafe function, then the be- havior of the program is undefined. * POSIX.1-2003 clarified that if an application calls <STRONG><A HREF="/man2/fork">fork(2)</A></STRONG> from a signal handler and any of the fork handlers registered by <STRONG><A HREF="/man3/pthread_atfork">pthread_atfork(3)</A></STRONG> calls a function that is not async-signal-safe, the behavior is undefined. A future revision of the standard is likely to remove <STRONG><A HREF="/man2/fork">fork(2)</A></STRONG> from the list of async-signal-safe func- tions. * Asynchronous signal handlers that call functions which are cancella- tion points and nest over regions of deferred cancellation may trig- ger cancellation whose behavior is as if asynchronous cancellation had occurred and may cause application state to become inconsistent. Deviations in the GNU C library The following known deviations from the standard occur in the GNU C li- brary: * Before glibc 2.24, <STRONG><A HREF="/man3/execl">execl(3)</A></STRONG> and <STRONG><A HREF="/man3/execle">execle(3)</A></STRONG> employed <STRONG><A HREF="/man3/realloc">realloc(3)</A></STRONG> inter- nally and were consequently not async-signal-safe. This was fixed in glibc 2.24. * The glibc implementation of <STRONG><A HREF="/man3/aio_suspend">aio_suspend(3)</A></STRONG> is not async-signal-safe because it uses <STRONG><A HREF="/man3/pthread_mutex_lock">pthread_mutex_lock(3)</A></STRONG> internally. SEE ALSO <STRONG><A HREF="/man2/sigaction">sigaction(2)</A></STRONG>, <STRONG><A HREF="/man7/signal">signal(7)</A></STRONG>, <STRONG><A HREF="/man7/standards">standards(7)</A></STRONG> COLOPHON This page is part of release 5.05 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2019-10-10 <STRONG><A HREF="/man7/SIGNAL-SAFETY">SIGNAL-SAFETY(7)</A></STRONG></PRE> <center> <h6>Man Pages Copyright Respective Owners. Site Copyright (C) 1994 - 2025 <a href="http://www.he.net">Hurricane Electric</a>. All Rights Reserved.</h6></center> </body> </html>