10.17 Abort function

Source: Internet
Author: User
Tags posix signal handler


We mentioned earlier that function abort can cause an abnormal termination of the program.

 
   
  
  1. #include <stdlib.h>
  2. void abort(void);
  3. This function never returns.

The function sends a signal SIGABRT to the calling process. (The process should not ignore this signal), ISO C indicates that calling function abort will send an unsuccessful termination notification to the host environment by calling function raise (SIGABRT). (ISO C states that calling abort would deliver an successful termination notification to the host environment by calling RA Ise (SIGABRT)).
ISO C requires that if the signal SIGABRT is captured and the signal processing function returns, Abort will still not return to its calling process. If the signal is captured, if the signal is captured, the only way the signal processing function does not return is to call the function exit,_exit,_exit,longjmp,siglongjmp. Posix.1 also points out that abort will rewrite the signal blocking or ignore processing.
The purpose of having the process capture the signal SIGABRT is to allow the process to perform cleanup before it terminates, and if the process does not terminate itself in the signal handler, abort terminates the process when the signal handler function returns.
ISO c changes whether the output stream is emptied and the temporary file is deleted to the implementation, which allows the posix.1 to implement the calling function Fclose to close the standard IO stream that has already been opened before the process terminates.

Earlier versions of System V generated the Sigiot signal in the Abort function, which could capture or ignore the signal and could be returned from the signal processing function, in which case abort would be returned to its calling process.
The 4.3BSD generates a Sigill signal, and before the signal is signaled, 4.3BSD blocks the signal and modifies its processing to SIG_DFL (terminating and saving the core file). This can prevent the process from ignoring or capturing the signal.
Historically, the implementation of the Abort function has been different for the processing of standard IO streams, in order to improve the defense of the program and to improve its portability, if we want the standard IO stream to be emptied, we need to execute before calling the function abort, and we will implement this in the Err_dump function.
Because the implementation of many UNIX system functions tmpfile call the unlink function immediately after the file is created, ISO C does not need to worry about the temporary file's reminders.

Example

Figure 10.25 shows an implementation of the Abort function specified by posix.1:

  
 
  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. void abort(void) /*POSIX-style abort() function */
  6. {
  7. sigset_t mask;
  8. struct sigaction action;
  9. /*Caller can‘t ignore SIGABRT, if so reset to defualt */
  10. sigaction(SIGABRT, NULL, &action);
  11. if(action.sa_handler == SIG_IGN)
  12. {
  13. action.sa_handler = SIG_DFL;
  14. sigaction(SIGABRT, &action, NULL);
  15. }
  16. if(action.sa_handler == SIG_DFL)
  17. {
  18. fflush(NULL); /* flush all open stdio streams */
  19. }
  20. /* Caller can‘t block SIGABRT; make sure it‘s unblocked. */
  21. sigfillset(&mask);
  22. sigdelset(&mask, SIGABRT); /*mask has only SIGABRT turned off .*/
  23. sigprocmask(SIG_SETMASK, &mask, NULL);
  24. kill(getpid(), SIGABRT); /*send the signal*/
  25. /*If we‘re here,process caught SIGABRT and returned */
  26. fflush(NULL); /*flush all open stdio streams.*/
  27. action.sa_handler = SIG_DFL;
  28. sigaction(SIGABRT, &action, NULL); /*reset to default*/
  29. sigprocmask(SIG_SETMASK, &mask, NULL); /*just in case ... */
  30. kill(getpid(), SIGABRT); /*and one more time*/
  31. exit(1); /*this should never be excuted*/
  32. }

The implementation of the abort function of Figure 10.25 posix.1

First check that the signal is not the default handler, flush all IO streams if it is, note that this differs from fclose because fflush just flush the file stream but does not close them, and when the process terminates, the system closes all open files. If the process captures the signal and returns to the Abort function, we need to flush the file stream again, because the process may again produce more output, and we do not need to deal with the fact that the process captures the signal and calls the function _exit or _ Exit. In this case, there is no flush buffer in memory that will be discarded, and we assume that the user does not want the contents of the buffer to be flush.
In 10.9 we have learned that the kill function generates a signal, and if the signal is not blocked (the program in Figure 10.25 we can guarantee that it is not blocked), then the signal will be sent to the process before the kill function is returned, because we have blocked all the signals except SIGABRT, So we know that if the kill call returns to abort, then the process has captured the signal and the signal processing function has returned.



From for notes (Wiz)

10.17 Abort function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.