home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / unix / unixlib_1 / !UnixLib37_src_signal_c_setup < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-09  |  3.0 KB  |  104 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/signal/c/RCS/setup,v $
  4.  * $Date: 1996/11/06 22:01:42 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: setup,v $
  10.  * Revision 1.2  1996/11/06 22:01:42  unixlib
  11.  * Yet more changes by NB, PB and SC.
  12.  *
  13.  * Revision 1.1  1996/10/30 22:04:51  unixlib
  14.  * Initial revision
  15.  *
  16.  ***************************************************************************/
  17.  
  18. static const char rcs_id[] = "$Id: setup,v 1.2 1996/11/06 22:01:42 unixlib Rel $";
  19.  
  20. /* signal.c.setup: Written by Nick Burrett, 1 September 1996.  */
  21.  
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #include <unixlib/sigstate.h>
  25.  
  26. /* #define DEBUG */
  27.  
  28. /* This function chooses a suitable execution environment
  29.    for the signal handler and calls the appropriate function
  30.    to perform the job.
  31.  
  32.    Return 0 on sucess, 1 on fail.  */
  33.  
  34. int
  35. __unixlib_setup_sighandler (struct unixlib_sigstate *ss,
  36.                 sighandler_t handler,
  37.                 int signo, int sigcode, int flags)
  38. {
  39.   int system_stack;
  40.  
  41.   sigcode = sigcode;
  42. #ifdef DEBUG
  43.   printf ("Entered unixlib_setup_sighandler: signo = %d, handler = %x\n",
  44.         signo, (int)handler);
  45. #endif
  46.   system_stack = ((ss->signalstack.ss_flags & SA_DISABLE)
  47.               || ss->signalstack.ss_size == 0
  48.               || ss->signalstack.ss_sp == 0) ? 1 : 0;
  49.  
  50.   /* The user has requested to execute the signal on a sigstack
  51.      but it can only be executed on the system stack. Return failure.  */
  52.   if ((flags & SA_ONSTACK) && system_stack)
  53.     {
  54. #ifdef DEBUG
  55.       printf ("unixlib_setup_sighandler: bad system stack\n");
  56.       printf ("   ss_size = %d, ss_sp = %d, ss_flags = %d\n",
  57.                 (int)ss->signalstack.ss_size,
  58.                 (int)ss->signalstack.ss_sp,
  59.                 (int)ss->signalstack.ss_flags);
  60. #endif
  61.       return 1;
  62.     }
  63.  
  64.   /* If the SA_DISABLE flag is set, we will not execute
  65.      this signal on a signal stack.
  66.  
  67.      Look out for problems where the stack pointer is zero or the
  68.      stack size is zero.  */
  69.   if (system_stack)
  70.     {
  71. #ifdef DEBUG
  72.       printf (" unixlib_setup_sighandler: executing off normal stack\n");
  73. #endif
  74.       __unixlib_exec_sig (handler, signo);
  75.       return 0;
  76.     }
  77.  
  78.   /* Specify that the signal stack is currently in use.  */
  79.   ss->signalstack.ss_flags |= SA_ONSTACK;
  80.  
  81.   /* If ss_size == -1, then this sigaltstack was the result of
  82.      a BSD sigstack conversion. Run the BSD alternative signal
  83.      executor.  */
  84.   if (ss->signalstack.ss_size == -1)
  85.     {
  86. #ifdef DEBUG
  87.       printf (" unixlib_setup_sighandler: executing off BSD stack\n");
  88. #endif
  89.        __unixlib_exec_sigstack_bsd (ss->signalstack.ss_sp, handler, signo);
  90.     }
  91.   else
  92.     {
  93. #ifdef DEBUG
  94.       printf (" unixlib_setup_sighandler: executing off POSIX stack\n");
  95. #endif
  96.        __unixlib_exec_sigstack (ss->signalstack.ss_sp, ss->signalstack.ss_size,
  97.                       handler, signo);
  98.     }
  99.   /* Say that the signal stack is no longer in use.  */
  100.   ss->signalstack.ss_flags &= ~SA_ONSTACK;
  101.  
  102.   return 0;
  103. }
  104.