📄 sigaction.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>sigaction</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_008_579"> </a>NAME</h4><blockquote>sigaction - examine and change signal action</blockquote><h4><a name = "tag_000_008_580"> </a>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="signal.h.html">signal.h</a>>int sigaction(int <i>sig</i>, const struct sigaction *<i>act</i>, struct sigaction *<i>oact</i>);</code></pre></blockquote><h4><a name = "tag_000_008_581"> </a>DESCRIPTION</h4><blockquote>The<i>sigaction()</i>function allows the calling process to examine and/or specify theaction to be associated with a specific signal.The argument<i>sig</i>specifies the signal; acceptable values are defined in<i><a href="signal.h.html"><signal.h></a></i>.<p>The structure<b>sigaction</b>,used to describe an action to be taken, is defined in the header<i><a href="signal.h.html"><signal.h></a></i>to include at least the following members:<p><table bordercolor=#000000 border=1 align=center><tr valign=top><th align=center><b>Member Type</b><th align=center><b>Member Name</b><th align=center><b>Description</b><tr valign=top><td align=left>void(*) (int)<td align=left>sa_handler<td align=left>SIG_DFL, SIG_IGN or pointer to a function.<tr valign=top><td align=left>sigset_t<td align=left>sa_mask<td align=left> Additional set of signals to be blocked during execution of signal-catching function. <tr valign=top><td align=left>int<td align=left>sa_flags<td align=left>Special flags to affect behaviour of signal.<tr valign=top><td align=left>void(*) (int, siginfo_t *, void *)<td align=left>sa_sigaction<td align=left>Signal-catching function.</table><p>If the argument<i>act</i>is not a null pointer, it points to a structure specifying the action to beassociated with the specified signal.If the argument<i>oact</i>is not a null pointer, the action previously associated with the signal isstored in the location pointed to by the argument<i>oact</i>.If the argument<i>act</i>is a null pointer, signal handling is unchanged; thus, the call can be usedto enquire about the current handling of a given signal.The<i>sa_handler</i>field of the<b>sigaction</b>structure identifies the action to be associated with thespecified signal.If the<i>sa_handler</i>field specifies a signal-catching function, the<i>sa_mask</i>field identifies a set of signals that will be added to theprocess' signal mask before the signal-catching function isinvoked.The SIGKILL and SIGSTOP signals will not be added to the signalmask using this mechanism; this restriction will be enforced bythe system without causing an error to be indicated.<p>If the SA_SIGINFO flag (see below) is cleared in the<i>sa_flags</i>field of the<b>sigaction</b>structure, the<i>sa_handler</i>field identifies the action to be associated with the specified signal.If the SA_SIGINFO flag is set in the<i>sa_flags</i>field, the<i>sa_sigaction</i>field specifies a signal-catching function.If the SA_SIGINFO bit is cleared and the<i>sa_handler</i>field specifies a signal-catching function, or if theSA_SIGINFO bit is set, the<i>sa_mask</i>field identifies a set of signalsthat will be added to the signal mask of the threadbefore the signal-catching function is invoked.<p>The<i>sa_flags</i>field can be used to modify the behaviour of the specifiedsignal.<p>The following flags, defined in the header<i><a href="signal.h.html"><signal.h></a></i>,can be set in<i>sa_flags</i>:<dl compact><dt>SA_NOCLDSTOP<dd>Do not generate SIGCHLD when children stop.<dt>ot generate SIGCHLD when children stop.<dd><dt>SA_ONSTACK<dd>If set and an alternate signal stack has been declared with<i><a href="sigaltstack.html">sigaltstack()</a></i>or<i><a href="sigstack.html">sigstack()</a></i>,the signal will be delivered to the calling process on that stack. Otherwise,the signal will be delivered on the current stack.<dt>SA_RESETHAND<dd>If set, the disposition of the signal will be reset to SIG_DFL and theSA_SIGINFO flag will be cleared on entry to thesignal handler.<dl><dt><b>Note:</b><dd>SIGILL and SIGTRAP cannot be automatically resetwhen delivered; the system silently enforces this restriction.</dl>Otherwise, the disposition of the signal will not be modified on entryto the signal handler.<p>In addition, if this flag is set,<i>sigaction()</i>behaves as if the SA_NODEFER flag were also set.<p><dt>SA_RESTART<dd>This flag affects the behaviour of interruptible functions; that is,those specified to fail with<i>errno</i>set to [EINTR].If set, and a function specified as interruptible is interrupted by thissignal, the function will restart and will not fail with [EINTR] unlessotherwise specified. If the flag is not set,interruptible functions interrupted by this signal will fail with<i>errno</i>set to [EINTR].<p><dt>SA_SIGINFO<dd>If cleared and the signal is caught, the signal-catching functionwill be entered as:<pre><code>void func(int <i>signo</i>);</code></pre>where <i>signo</i> is the only argument to the signal catching function. Inthis case the <b>sa_handler</b> member must be used to describe the signalcatching function and the application must not modify the <b>sa_sigaction</b>member.<p>If SA_SIGINFO is set and the signal is caught, the signal-catching functionwill be entered as:<pre><code>void func(int <i>signo</i>, siginfo_t *<i>info</i>, void *<i>context</i>);</code></pre>where two additional arguments are passed to the signal catching function.The second argument will point to an object of type<b>siginfo_t</b>explaining the reason why the signal was generated;the third argument can be cast to a pointer to an object of type<b>ucontext_t</b>to refer to the receiving process' context that was interrupted whenthe signal was delivered.In this case the <b>sa_sigaction</b> member must be used todescribe the signal catching function and the application must not modify the<b>sa_handler</b> member.<p>The <b>si_signo</b> member contains the system-generated signal number.<p>The <b>si_errno</b> member may contain implementation-dependent additionalerror information; if non-zero, it contains an error number identifying thecondition that caused the signal to be generated.<p>The <b>si_code</b> member contains a code identifying the cause of the signal. If thevalue of <b>si_code</b> is less than or equal to 0, then the signal wasgenerated by a process and <b>si_pid</b> and <b>si_uid</b> respectively indicatethe process ID and the real user ID of the sender. The <i><a href="signal.h.html"><signal.h></a></i>header description contains information about the signalspecific contents of the elements of the <b>siginfo_t</b>type.<p><dt>SA_NOCLDWAIT<dd>If set, and <i>sig</i> equals SIGCHLD,child processes of the calling processes will not be transformed into zombieprocesseswhen they terminate. If the calling process subsequently waits for itschildren, and the process has no unwaited for children that were transformedinto zombie processes, it will block until all of its children terminate, and<i><a href="wait.html">wait()</a></i>,<i><a href="wait3.html">wait3()</a></i>,<i><a href="waitid.html">waitid()</a></i>and<i><a href="waitpid.html">waitpid()</a></i>will fail and set<i>errno</i>to [ECHILD].Otherwise, terminating child processes will be transformed into zombieprocesses, unless SIGCHLD is set to SIG_IGN.<p><dt>SA_NODEFER<dd>If set and <i>sig</i> is caught,<i>sig</i> will not be added to the process' signal mask on entry to thesignal handler unless it is included in <b>sa_mask</b>. Otherwise, <i>sig</i>will always be added to the process' signal mask on entry to the signalhandler.<p></dl><p>If<i>sig</i>is SIGCHLD and the SA_NOCLDSTOP flag is not set in<i>sa_flags</i>,and the implementation supports the SIGCHLD signal, then aSIGCHLD signal will be generated for the calling process wheneverany of its child processes stop.If<i>sig</i>is SIGCHLD and the SA_NOCLDSTOP flag is set in<i>sa_flags</i>,then the implementation will not generate a SIGCHLD signal inthis way.<p>When a signal is caught by a signal-catching function installed by<i>sigaction()</i>,a new signal mask is calculated and installed for theduration of the signal-catching function (or until a call toeither<i><a href="sigprocmask.html">sigprocmask()</a></i>or<i><a href="sigsuspend.html">sigsuspend()</a></i>is made).This mask is formed by taking the union of the current signalmask and the value of the<i>sa_mask</i>for the signal being delivered unless SA_NODEFER or SA_RESETHAND is set,and then including the signal being delivered.If and when the user's signal handler returns normally, theoriginal signal mask is restored.<p>Once an action is installed for a specific signal, it remainsinstalled until another action is explicitly requested (byanother call to<i>sigaction()</i>),until the SA_RESETHAND flag causes resetting of the handler,or until one of the<i>exec</i>functions is called.<p>If the previous action for<i>sig</i>had been established by<i><a href="signal.html">signal()</a></i>,the values of the fields returned in the structure pointed to by<i>oact</i>are unspecified, and in particular<i>oact->sa_handler</i>is not necessarily the same value passed to<i><a href="signal.html">signal()</a></i>.However, if a pointer to the same structure or a copy thereof ispassed to a subsequent call to<i>sigaction()</i>via the<i>act</i>argument, handling of the signal will be as if the original call to<i><a href="signal.html">signal()</a></i>were repeated.<p>If<i>sigaction()</i>fails, no new signal handler is installed.<p>It is unspecified whether an attempt to set the action for a signalthat cannot be caught or ignored to SIG_DFL is ignored or causes anerror to be returned with<i>errno</i>set to [EINVAL].<p>If SA_SIGINFO is not set in<i>sa_flags</i>,then the disposition of subsequent occurrences of<i>sig</i>when it is already pending is implementation-dependent;the signal-catching function will be invoked with a single argument.If the implementation supports the Realtime Signals Extension option,and if SA_SIGINFO is set in<i>sa_flags,</i>then subsequent occurrences of<i>sig</i>generated by<i><a href="sigqueue.html">sigqueue()</a></i>or as a result of any signal-generating functionthat supports the specification of an application-defined value (when<i>sig</i>is already pending) will be queued in FIFO order until deliveredor accepted;the signal-catching function will be invoked with three arguments.The application specified value is passedto the signal-catching function as the<i>si_value</i>member of the<b>siginfo_t</b>structure.<h5><a name = "tag_000_008_581_001"> </a>Signal Generation and Delivery</h5><xref type="5" name="siggendel"></xref><p>A signal is said to be<i>generated</i>for (or sent to) a process or thread when the event that causes the signalfirst occurs.Examples of such events include detection of hardware faults,timer expiration,signals generated via the<b>sigevent</b>structureand terminal activity, as well as invocations of<i><a href="kill.html">kill()</a></i>and<i><a href="sigqueue.html">sigqueue()</a></i>functions.In some circumstances, the same event generates signals formultiple processes.<p>At the time of generation, a determination is made whetherthe signal has been generated for the processor for a specific thread within the process.Signals which are generated by some action attributable to aparticular thread, such as a hardware fault,are generated for the thread that caused the signal to begenerated.Signals that are generated in associationwith a process ID or process group IDor an asynchronous event such as terminal activityare generated for the process.<p>Each process has an action to be taken in response to each signaldefined by the system (see<xref href=sigact><a href="#tag_000_008_581_002">Signal Actions</a></xref>).A signal is said to be<i>delivered</i>to a process when the appropriate action for the process andsignal is taken.A signal is said to be<i>accepted</i>by a process when the signal is selected and returned by one of the<i><a href="sigwait.html">sigwait()</a></i>functions.<p>During the time between the generation of a signal and itsdelivery or acceptance, the signal is said to be<i>pending</i>.Ordinarily, this interval cannot be detected by an application.However, a signal can be<i>blocked</i>from delivery to a threadIf the action associated with a blocked signal is anything otherthan to ignore the signal, and if that signal is generated forthe thread the signal will remain pending until it is unblocked,it is accepted when it is selected and returned by a call to the<i><a href="sigwait.html">sigwait()</a></i>function, or the action associated with it is set to ignore the signal.Signals generated for the process will be delivered toexactly one of those threadswithin the process which is in a call to a<i><a href="sigwait.html">sigwait()</a></i>function selecting that signal or has not blocked delivery of thesignal.If there are no threads in a call to a<i><a href="sigwait.html">sigwait()</a></i>function selecting that signal, andif all threads within the process block delivery of the signal,the signal will remain pending on the process until a thread calls a<i><a href="sigwait.html">sigwait()</a></i>function selecting that signal,a thread unblocks delivery of the signal,or the action associated with the signal is set to ignore the signal.If the action associated with a blocked signal is to ignore thesignal and if that signal is generated for the process, it is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -