티스토리 뷰

DEV/C&C++

async signal safe ...

pu928 2019. 4. 2. 16:24
반응형

전혀 생각지도 못한 signal safe 문제..;;

 

 

 

SIGNAL-SAFETY(7) Linux Programmer's Manual SIGNAL-SAFETY(7)

NAME         top

signal-safety - async-signal-safe functions

DESCRIPTION         top

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 understood 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 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 stdio function such as printf(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 printf(3), then the second call to printf(3) will operate on inconsistent 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 respect 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 complexity, 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 reentrant or because it is atomic with respect to signals (i.e., its execution 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

abort(3) Added in POSIX.1-2003

accept(2)

access(2)

aio_error(3)

aio_return(3) aio_suspend(3) See notes below alarm(2) bind(2) cfgetispeed(3) cfgetospeed(3) cfsetispeed(3) cfsetospeed(3) chdir(2) chmod(2) chown(2) clock_gettime(2) close(2) connect(2) creat(2) dup(2) dup2(2) execl(3) Added in POSIX.1-2008; see notes below execle(3) See notes below execv(3) Added in POSIX.1-2008 execve(2) _exit(2) _Exit(2) faccessat(2) Added in POSIX.1-2008 fchdir(2) Added in POSIX.1-2013 fchmod(2) fchmodat(2) Added in POSIX.1-2008 fchown(2) fchownat(2) Added in POSIX.1-2008 fcntl(2) fdatasync(2) fexecve(3) Added in POSIX.1-2008 ffs(3) Added in POSIX.1-2016 fork(2) See notes below fstat(2) fstatat(2) Added in POSIX.1-2008 fsync(2) ftruncate(2) futimens(3) Added in POSIX.1-2008 getegid(2) geteuid(2) getgid(2) getgroups(2) getpeername(2) getpgrp(2) getpid(2) getppid(2) getsockname(2) getsockopt(2) getuid(2) htonl(3) Added in POSIX.1-2016 htons(3) Added in POSIX.1-2016 kill(2) link(2) linkat(2) Added in POSIX.1-2008 listen(2) longjmp(3) Added in POSIX.1-2016; see notes below lseek(2) lstat(2) memccpy(3) Added in POSIX.1-2016 memchr(3) Added in POSIX.1-2016 memcmp(3) Added in POSIX.1-2016 memcpy(3) Added in POSIX.1-2016 memmove(3) Added in POSIX.1-2016 memset(3) Added in POSIX.1-2016 mkdir(2) mkdirat(2) Added in POSIX.1-2008 mkfifo(3) mkfifoat(3) Added in POSIX.1-2008 mknod(2) Added in POSIX.1-2008 mknodat(2) Added in POSIX.1-2008 ntohl(3) Added in POSIX.1-2016 ntohs(3) Added in POSIX.1-2016 open(2) openat(2) Added in POSIX.1-2008 pause(2) pipe(2) poll(2) posix_trace_event(3) pselect(2) pthread_kill(3) Added in POSIX.1-2013 pthread_self(3) Added in POSIX.1-2013 pthread_sigmask(3) Added in POSIX.1-2013 raise(3) read(2) readlink(2) readlinkat(2) Added in POSIX.1-2008 recv(2) recvfrom(2) recvmsg(2) rename(2) renameat(2) Added in POSIX.1-2008 rmdir(2) select(2) sem_post(3) send(2) sendmsg(2) sendto(2) setgid(2) setpgid(2) setsid(2) setsockopt(2) setuid(2) shutdown(2) sigaction(2) sigaddset(3) sigdelset(3) sigemptyset(3) sigfillset(3) sigismember(3) siglongjmp(3) Added in POSIX.1-2016; see notes below signal(2) sigpause(3) sigpending(2) sigprocmask(2) sigqueue(2) sigset(3) sigsuspend(2) sleep(3) sockatmark(3) Added in POSIX.1-2004 socket(2) socketpair(2) stat(2) stpcpy(3) Added in POSIX.1-2016 stpncpy(3) Added in POSIX.1-2016 strcat(3) Added in POSIX.1-2016 strchr(3) Added in POSIX.1-2016 strcmp(3) Added in POSIX.1-2016 strcpy(3) Added in POSIX.1-2016 strcspn(3) Added in POSIX.1-2016 strlen(3) Added in POSIX.1-2016 strncat(3) Added in POSIX.1-2016 strncmp(3) Added in POSIX.1-2016 strncpy(3) Added in POSIX.1-2016 strnlen(3) Added in POSIX.1-2016 strpbrk(3) Added in POSIX.1-2016 strrchr(3) Added in POSIX.1-2016 strspn(3) Added in POSIX.1-2016 strstr(3) Added in POSIX.1-2016 strtok_r(3) Added in POSIX.1-2016 symlink(2) symlinkat(2) Added in POSIX.1-2008 tcdrain(3) tcflow(3) tcflush(3) tcgetattr(3) tcgetpgrp(3) tcsendbreak(3) tcsetattr(3) tcsetpgrp(3) time(2) timer_getoverrun(2) timer_gettime(2) timer_settime(2) times(2) umask(2) uname(2) unlink(2) unlinkat(2) Added in POSIX.1-2008 utime(2) utimensat(2) Added in POSIX.1-2008 utimes(2) Added in POSIX.1-2008 wait(2) waitpid(2) wcpcpy(3) Added in POSIX.1-2016 wcpncpy(3) Added in POSIX.1-2016 wcscat(3) Added in POSIX.1-2016 wcschr(3) Added in POSIX.1-2016 wcscmp(3) Added in POSIX.1-2016 wcscpy(3) Added in POSIX.1-2016 wcscspn(3) Added in POSIX.1-2016 wcslen(3) Added in POSIX.1-2016 wcsncat(3) Added in POSIX.1-2016 wcsncmp(3) Added in POSIX.1-2016 wcsncpy(3) Added in POSIX.1-2016 wcsnlen(3) Added in POSIX.1-2016 wcspbrk(3) Added in POSIX.1-2016 wcsrchr(3) Added in POSIX.1-2016 wcsspn(3) Added in POSIX.1-2016 wcsstr(3) Added in POSIX.1-2016 wcstok(3) Added in POSIX.1-2016 wmemchr(3) Added in POSIX.1-2016 wmemcmp(3) Added in POSIX.1-2016 wmemcpy(3) Added in POSIX.1-2016 wmemmove(3) Added in POSIX.1-2016 wmemset(3) Added in POSIX.1-2016 write(2)

 

 

Notes:

 

* POSIX.1-2001 and POSIX.1-2004 required the functions fpathconf(3), pathconf(3), and sysconf(3) to be async-signal-safe, but this requirement 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 longjmp(3) or siglongjmp(3) and the program subsequently calls an unsafe function, then the behavior of the program is undefined.

 

* POSIX.1-2003 clarified that if an application calls fork(2) from a signal handler and any of the fork handlers registered by pthread_atfork(3) calls a function that is not async-signal-safe, the behavior is undefined. A future revision of the standard is likely to remove fork(2) from the list of async-signal-safe functions.

 

Deviations in the GNU C library

 

The following known deviations from the standard occur in the GNU C library:

 

* Before glibc 2.24, execl(3) and execle(3) employed realloc(3) internally and were consequently not async-signal-safe. This was fixed in glibc 2.24.

 

* The glibc implementation of aio_suspend(3) is not async-signal- safe because it uses pthread_mutex_lock(3) internally.

SEE ALSO         top

sigaction(2), signal(7), standards(7)

COLOPHON         top

This page is part of release 5.00 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/.

 

 

 

 

http://man7.org/linux/man-pages/man7/signal-safety.7.html

 

------------------------- 구글 번역 --------------------------

비동기 신호 안전 함수는 신호 처리기 내에서 안전하게 호출 할 수있는 함수입니다. 많은 함수가 비동기 신호에 안전하지 않습니다.
 특히 비재 귀족 함수는 일반적으로 신호 처리기에서 호출하는 것이 안전하지 않습니다.

 함수를 안전하지 않게 만드는 종류의 문제는 stdio 라이브러리의 구현을 고려할 때 신속하게 이해할 수 있습니다. 모든 함수는 비동기 신호에 안전하지 않습니다.

 파일에 버퍼링 된 입출력을 수행 할 때 stdio 함수는 데이터의 양과 버퍼의 현재 위치를 기록하는 관련 카운터 및 색인 (또는 포인터)과 함께 정적으로 할당 된 데이터 버퍼를 유지 관리해야합니다. 주 프로그램이 버퍼 및 관련 변수가 부분적으로 업데이트 된 printf (3)와 같은 stdio 함수를 호출하는 중간에 있다고 가정합니다. 그 순간에 프로그램이 printf (3)를 호출하는 신호 처리기에 의해 인터럽트되면 printf (3)에 대한 두 번째 호출은 예측할 수없는 결과와 함께 일관성없는 데이터에서 작동합니다.

       안전하지 않은 기능에 대한 문제를 피하기 위해 가능한 두 가지가 있습니다
       선택 사항 :

       1. (a) 신호 처리기가 비동기 신호 안전 함수 만 호출하고, (b) 신호 처리기 자체가 주 프로그램의 전역 변수에 대해 재진입 가능하도록 보장하십시오.

       2. 안전하지 않거나 신호 처리기가 액세스하는 전역 데이터에서 작동하는 함수를 호출 할 때 주 프로그램에서 신호 전달을 차단합니다.

       일반적으로 두 번째 선택은 복잡한 프로그램에서 어렵 기 때문에 첫 번째 선택이 취해집니다.

       POSIX.1은 구현이 비동기 신호를 안전하게해야하는 함수 집합을 명시한다. (구현은 추가 기능의 안전한 구현을 제공 할 수 있지만 표준에서는 필수 사항이 아니며 다른 구현은 동일한 보증을 제공하지 않을 수도 있습니다.) 일반적으로 함수는 재진입 적이거나 또는 (즉, 시그널 핸들러에 의해 인터럽트 될 수 없다).


POSIX.1에 의해 비동기 신호 안전이 요구되는 함수 세트는 다음 표에 나와 있습니다. 달리 언급되지 않은 함수는 POSIX.1-2001에서 비동기 신호 안전성을 요구 받았다. 테이블 세부 정보는 이후 표준에서 변경됩니다.

 

 

 

 

 

 

 

 

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함