{"id":4115,"date":"2022-12-20T17:39:26","date_gmt":"2022-12-20T20:39:26","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/signal-safety-man7\/"},"modified":"2022-12-20T17:39:26","modified_gmt":"2022-12-20T20:39:26","slug":"signal-safety-man7","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/signal-safety-man7\/","title":{"rendered":"SIGNAL-SAFETY (man7)"},"content":{"rendered":"<h1 align=\"center\">SIGNAL-SAFETY<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">signal-safety \u2212 async-signal-safe functions<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">An <i>async-signal-safe<\/i> function is one that can be safely called from within a signal handler. Many functions are <i>not<\/i> async-signal-safe. In particular, nonreentrant functions are generally unsafe to call from a signal handler.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The kinds of issues that render a function unsafe can be quickly understood when one considers the implementation of the <i>stdio<\/i> library, all of whose functions are not async-signal-safe.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When performing buffered I\/O on a file, the <i>stdio<\/i> functions must maintain a statically allocated data buffer along with associated counters and indexes (or pointers) that record the amount of data and the current position in the buffer. Suppose that the main program is in the middle of a call to a <i>stdio<\/i> function such as <b>printf<\/b>(3) where the buffer and associated variables have been partially updated. If, at that moment, the program is interrupted by a signal handler that also calls <b>printf<\/b>(3), then the second call to <b>printf<\/b>(3) will operate on inconsistent data, with unpredictable results.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">To avoid problems with unsafe functions, there are two possible choices:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>1.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>Ensure that (a) the signal handler calls only async-signal-safe functions, and (b) the signal handler itself is reentrant with respect to global variables in the main program.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"3%\">\n<p>2.<\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"85%\">\n<p>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.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">Generally, the second choice is difficult in programs of any complexity, so the first choice is taken.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In general, a function is async-signal-safe either because it is reentrant or because it is atomic with respect to signals (i.e., its execution can\u2019t be interrupted by a signal handler).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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.<\/p>\n<p align=\"center\" style=\"margin-top: 1em\"><img decoding=\"async\" src=\"grohtml-962901.png\" alt=\"Image grohtml-962901.png\"><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Notes:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p style=\"margin-top: 1em\">POSIX.1-2001 and POSIX.1-2001 TC2 required the functions <b>fpathconf<\/b>(3), <b>pathconf<\/b>(3), and <b>sysconf<\/b>(3) to be async-signal-safe, but this requirement was removed in POSIX.1-2008.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>If a signal handler interrupts the execution of an unsafe function, and the handler terminates via a call to <b>longjmp<\/b>(3) or <b>siglongjmp<\/b>(3) and the program subsequently calls an unsafe function, then the behavior of the program is undefined.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>POSIX.1-2001 TC1 clarified that if an application calls <b>fork<\/b>(2) from a signal handler and any of the fork handlers registered by <b>pthread_atfork<\/b>(3) calls a function that is not async-signal-safe, the behavior is undefined. A future revision of the standard is likely to remove <b>fork<\/b>(2) from the list of async-signal-safe functions.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>Asynchronous signal handlers that call functions which are cancellation points and nest over regions of deferred cancellation may trigger cancellation whose behavior is as if asynchronous cancellation had occurred and may cause application state to become inconsistent.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>errno<\/b> <br \/> Fetching and setting the value of <i>errno<\/i> is async-signal-safe provided that the signal handler saves <i>errno<\/i> on entry and restores its value before returning.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Deviations in the GNU C library<\/b> <br \/> The following known deviations from the standard occur in the GNU C library:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p style=\"margin-top: 1em\">Before glibc 2.24, <b>execl<\/b>(3) and <b>execle<\/b>(3) employed <b>realloc<\/b>(3) internally and were consequently not async-signal-safe. This was fixed in glibc 2.24.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>*<\/p>\n<\/td>\n<td width=\"3%\"><\/td>\n<td width=\"85%\">\n<p>The glibc implementation of <b>aio_suspend<\/b>(3) is not async-signal-safe because it uses <b>pthread_mutex_lock<\/b>(3) internally.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>sigaction<\/b>(2), <b>signal<\/b>(7), <b>standards<\/b>(7)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> 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\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  signal-safety \u2212 async-signal-safe functions <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[971],"tags":[973,972,1194],"class_list":["post-4115","post","type-post","status-publish","format-standard","hentry","category-7-miscelanea","tag-973","tag-man7","tag-signal-safety"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=4115"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4115\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=4115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=4115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=4115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}