Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 10.6 Reentrant Functions 331


abort faccessat linkat select socketpair
accept fchmod listen sem_post stat
access fchmodat lseek send symlink
aio_error fchown lstat sendmsg symlinkat
aio_return fchownat mkdir sendto tcdrain
aio_suspend fcntl mkdirat setgid tcflow
alarm fdatasync mkfifo setpgid tcflush
bind fexecve mkfifoat setsid tcgetattr
cfgetispeed fork mknod setsockopt tcgetpgrp
cfgetospeed fstat mknodat setuid tcsendbreak
cfsetispeed fstatat open shutdown tcsetattr
cfsetospeed fsync openat sigaction tcsetpgrp
chdir ftruncate pause sigaddset time
chmod futimens pipe sigdelset timer_getoverrun
chown getegid poll sigemptyset timer_gettime
clock_gettime geteuid posix_trace_event sigfillset timer_settime
close getgid pselect sigismember times
connect getgroups raise signal umask
creat getpeername read sigpause uname
dup getpgrp readlink sigpending unlink
dup2 getpid readlinkat sigprocmask unlinkat
execl getppid recv sigqueue utime
execle getsockname recvfrom sigset utimensat
execv getsockopt recvmsg sigsuspend utimes
execve getuid rename sleep wait
_Exit kill renameat sockatmark waitpid
_exit link rmdir socket write

Figure 10.4Reentrant functions that may be called from a signal handler

stored inmain.Therefore, as a general rule, when calling the functions listed in
Figure10.4 from a signal handler, we should save and restoreerrno.(Be awarethat a
commonly caught signal isSIGCHLD,and its signal handler usually calls one of the
waitfunctions. All thewaitfunctions can changeerrno.)
Note thatlongjmp(Section 7.10) andsiglongjmp(Section 10.15) aremissing
from Figure10.4, because the signal may have occurred while the main routine was
updating a data structure in a nonreentrant way.This data structurecould be left half
updated if we callsiglongjmpinstead of returning from the signal handler.If it is
going to do such things as update global data structures, as we describe here, while
catching signals that causesigsetjmpto be executed, an application needs to block the
signals while updating the data structures.

Example


Figure10.5 shows a program that calls the nonreentrant functiongetpwnamfrom a
signal handler that is called every second. We describe the alarm function in
Section 10.10.We use it here to generate aSIGALRMsignal every second.
Free download pdf