home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / k / ksh48.zip / sh / sigact.c < prev    next >
C/C++ Source or Header  |  1992-05-28  |  6KB  |  308 lines

  1. /* NAME:
  2.  *      sigact.c - fake sigaction(2)
  3.  *
  4.  * SYNOPSIS:
  5.  *      #include <signal.h>
  6.  *      #ifndef  SA_NOCLDSTOP
  7.  *      # include "sigact.h"
  8.  *      #endif
  9.  * 
  10.  *      int sigaction(int sig, struct sigaction *act, 
  11.  *                      struct sigaction *oact);
  12.  *      int sigaddset(sigset_t *mask, int sig);
  13.  *      int sigdelset(sigset_t *mask, int sig);
  14.  *      int sigemptyset(sigset_t *mask);
  15.  *      int sigfillset(sigset_t *mask);
  16.  *      int sigismember(sigset_t *mask, int sig);
  17.  *      int sigpending(sigset_t *set);
  18.  *      int sigprocmask(int how, sigset_t *set, sigset_t *oldset);
  19.  *      int sigsuspend(sigset_t *mask);
  20.  *
  21.  * DESCRIPTION:
  22.  *      This is a fake sigaction implementation.  It uses 
  23.  *      sigset(2) if available, otherwise it just uses 
  24.  *      signal(2).  If it thinks sigaction(2) really exists it 
  25.  *      compiles to almost nothing.
  26.  *      
  27.  *      The need for all this is the problems caused by mixing 
  28.  *      signal handling routines in the one process.  This 
  29.  *      module allows for a consistent POSIX compliant interface 
  30.  *      to whatever is available.
  31.  *
  32.  * RETURN VALUE:
  33.  *      0==success, -1==failure
  34.  *
  35.  * FILES:
  36.  *      None.
  37.  *
  38.  * SEE ALSO:
  39.  *      
  40.  *
  41.  * BUGS:
  42.  *      Since we fake most of this, don't expect fancy usage to 
  43.  *      work.
  44.  *      
  45.  * COPYRIGHT:
  46.  *      @(#)Copyright (c) 1992 Simon J. Gerraty
  47.  *
  48.  *      This is free software.  It comes with NO WARRANTY.
  49.  *      Permission to use, modify and distribute this source code 
  50.  *      is granted subject to the following conditions.
  51.  *      1/ that that the above copyright notice and this notice 
  52.  *      are preserved in all copies and that due credit be given 
  53.  *      to the author.  
  54.  *      2/ that any changes to this code are clearly commented 
  55.  *      as such so that the author does get blamed for bugs 
  56.  *      other than his own.
  57.  *      
  58.  *      Please send copies of changes and bug-fixes to:
  59.  *      sjg@zen.void.oz.au
  60.  *
  61.  */
  62. #ifndef lint
  63. static char  *RCSid = "$Id: sigact.c,v 1.5 1992/05/03 08:29:10 sjg Exp $";
  64. #endif
  65. /*
  66.  * $Log: sigact.c,v $
  67.  * Revision 1.5  1992/05/03  08:29:10  sjg
  68.  * Update for Patch05
  69.  *
  70.  * Revision 1.4  1992/04/29  06:29:13  sjg
  71.  * avoid use of #pragma
  72.  *
  73.  * Revision 1.3  1992/04/26  11:24:43  sjg
  74.  * USE_SIGSET corrected in sigsuspend().
  75.  *
  76.  * Revision 1.2  1992/04/24  15:04:11  sjg
  77.  * now compiles with cc
  78.  *
  79.  * Revision 1.1  1992/04/24  12:03:58  sjg
  80.  * Initial revision
  81.  *
  82.  */
  83.  
  84. #include <signal.h>
  85.  
  86. /*
  87.  * some systems have a faulty sigaction() implementation!
  88.  * Allow us to bypass it.
  89.  */
  90. #if !defined(SA_NOCLDSTOP) || defined(USE_SIGNAL) || defined(USE_SIGSET) || defined(USE_SIGMASK)
  91.  
  92. /*
  93.  * if we haven't been told,
  94.  * try and guess what we should implement with.
  95.  */
  96. #if !defined(USE_SIGSET) && !defined(USE_SIGMASK) && !defined(USE_SIGNAL)
  97. # if defined(sigmask) || defined(BSD) || defined(_BSD) && !defined(BSD41)
  98. #   define USE_SIGMASK
  99. # else
  100. #   ifndef NO_SIGSET
  101. #     define USE_SIGSET
  102. #   else
  103. #     define USE_SIGNAL
  104. #   endif
  105. # endif
  106. #endif
  107. /*
  108.  * if we still don't know, we're in trouble
  109.  */
  110. #if !defined(USE_SIGSET) && !defined(USE_SIGMASK) && !defined(USE_SIGNAL)
  111. error must know what to implement with
  112. #endif
  113.  
  114. #include "sigact.h"
  115.  
  116.  
  117.  
  118. int
  119. sigaction(sig, act, oact)
  120.   int sig;
  121.   struct sigaction *act, *oact;
  122. {
  123.   void (*oldh)();
  124.  
  125.   if (act)
  126.   {
  127. #ifdef USE_SIGSET
  128.     oldh = sigset(sig, act->sa_handler);
  129. #else
  130.     oldh = signal(sig, act->sa_handler);
  131. #endif
  132.   }
  133.   else
  134.   {
  135.     if (oact)
  136.     {      
  137. #ifdef USE_SIGSET
  138.       oldh = sigset(sig, SIG_IGN);
  139. #else
  140.       oldh = signal(sig, SIG_IGN);
  141. #endif
  142.       if (oldh != SIG_IGN)
  143.       {
  144. #ifdef USE_SIGSET
  145.     (void) sigset(sig, oldh);
  146. #else
  147.     (void) signal(sig, oldh);
  148. #endif
  149.       }
  150.     }
  151.   }
  152.   if (oact)
  153.   {
  154.     oact->sa_handler = oldh;
  155.   }
  156.   return 0;                /* hey we're faking it */
  157. }
  158.  
  159. int
  160. sigaddset(mask, sig)
  161.   sigset_t *mask;
  162.   int sig;
  163. {
  164.   *mask |= sigmask(sig);
  165.   return 0;
  166. }
  167.  
  168.  
  169. int
  170. sigdelset(mask, sig)
  171.   sigset_t *mask;
  172.   int sig;
  173. {
  174.   *mask &= ~(sigmask(sig));
  175.   return 0;
  176. }
  177.  
  178.  
  179. int
  180. sigemptyset(mask)
  181.   sigset_t *mask;
  182. {
  183.   *mask = 0;
  184.   return 0;
  185. }
  186.  
  187.  
  188. int
  189. sigfillset(mask)
  190.   sigset_t *mask;
  191. {
  192.   *mask = ~0;
  193.   return 0;
  194. }
  195.  
  196.  
  197. int
  198. sigismember(mask, sig)
  199.   sigset_t *mask;
  200.   int sig;
  201. {
  202.   return ((*mask) & sigmask(sig));
  203. }
  204.  
  205.  
  206. int
  207. sigpending(set)
  208.   sigset_t *set;
  209. {
  210.   return 0;
  211. }
  212.  
  213.  
  214. int
  215. sigprocmask(how, set, oldset)
  216.   int how;
  217.   sigset_t *set, *oldset;
  218. {
  219. #ifdef USE_SIGSET
  220.   register int i;
  221. #endif
  222.   sigset_t sm;
  223.  
  224. #ifdef USE_SIGMASK
  225.   sm = sigblock(0);
  226. #else
  227.   sm = 0;
  228. #endif
  229.   
  230.   if (oldset)
  231.     *oldset = sm;    /* dangerous ? */
  232.   if (set)
  233.   {
  234.     switch (how)
  235.     {
  236.     case SIG_BLOCK:
  237.       sm |= *set;
  238.       break;
  239.     case SIG_UNBLOCK:
  240.       sm &= ~(*set);
  241.       break;
  242.     case SIG_SETMASK:
  243.       sm = *set;
  244.       break;
  245.     }
  246. #ifdef USE_SIGMASK
  247.     (void) sigsetmask(sm);
  248. #else
  249. # ifdef USE_SIGSET
  250.     for (i = 1; i < NSIG; i++)
  251.     {
  252.       if (how == SIG_UNBLOCK)
  253.       {
  254.     if (*set & sigmask(i))
  255.       sigrelse(i);
  256.       }
  257.       else
  258.     if (sm & sigmask(i))
  259.     {
  260.       sighold(i);
  261.     }
  262.     }
  263. # endif
  264. #endif
  265.   }
  266.   return 0;
  267. }
  268.  
  269.  
  270. int
  271. sigsuspend(mask)
  272.   sigset_t *mask;
  273. {
  274. #ifdef USE_SIGSET
  275.   int sig = SIGCHLD;            /* our default */
  276.   
  277.   /*
  278.    * add as many tests as you think sensible, but
  279.    * SIGALRM, and SIGCHLD are probably the most
  280.    * common.
  281.    */
  282.   if (*mask & sigmask(SIGALRM))
  283.     sig = SIGALRM;
  284.   else
  285.     if (*mask & sigmask(SIGPIPE))
  286.       sig = SIGPIPE;
  287.   sigpause(sig);
  288. #else
  289. # ifdef USE_SIGMASK
  290.   sigpause(mask);
  291. # else
  292.   pause();
  293. # endif
  294. #endif
  295.   return 0;
  296. }
  297.  
  298. #endif /* ! SA_NOCLDSTOP */
  299.  
  300.  
  301. /* This lot (for GNU-Emacs) goes at the end of the file. */
  302. /* 
  303.  * Local Variables:
  304.  * version-control:t
  305.  * comment-column:40
  306.  * End:
  307.  */
  308.