home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / libgimp / gimpsignal.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-06  |  3.6 KB  |  91 lines

  1. /* LIBGIMP - The GIMP Library                                                   
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball                
  3.  *
  4.  * This library is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Lesser General Public
  6.  * License as published by the Free Software Foundation; either
  7.  * version 2 of the License, or (at your option) any later version.             
  8.  *                                                                              
  9.  * This library is distributed in the hope that it will be useful,              
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of               
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU            
  12.  * Library General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Lesser General Public
  15.  * License along with this library; if not, write to the
  16.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.  * Boston, MA 02111-1307, USA.
  18.  *
  19.  * $Revision: 1.7 $
  20.  */                                                                             
  21.  
  22. #include <glib.h>
  23.  
  24. #include "gimpsignal.h"
  25.  
  26.  
  27. /* Courtesy of Austin Donnelly 06-04-2000 to address bug #2742 */
  28.  
  29. /** 
  30.  * gimp_signal_private: 
  31.  * @signum: Selects signal to be handled see man 5 signal (or man 7 signal)
  32.  * @handler: Handler that maps to signum. Invoked by O/S. 
  33.  *           Handler gets signal that caused invocation. Corresponds
  34.  *           to the @sa_handler field of the @sigaction struct.
  35.  * @flags: Preferences. OR'ed SA_<xxx>. See man sigaction. Corresponds
  36.  *         to the @sa_flags field of the @sigaction struct.
  37.  *
  38.  * This function furnishes a workalike for signal(2) but
  39.  * which internally invokes sigaction(2) after certain
  40.  * sa_flags are set; these primarily to ensure restarting
  41.  * of interrupted system calls. See sigaction(2)  It is a 
  42.  * aid to transition and not new development: that effort 
  43.  * should employ sigaction directly. [gosgood 18.04.2000] 
  44.  *
  45.  * Cause @handler to be run when @signum is delivered.  We
  46.  * use sigaction(2) rather than signal(2) so that we can control the
  47.  * signal handler's environment completely via @flags: some signal(2)
  48.  * implementations differ in their sematics, so we need to nail down
  49.  * exactly what we want. [austin 06.04.2000]
  50.  *
  51.  * Returns: A reference to the signal handling function which was
  52.  *          active before the call to gimp_signal_private().
  53.  */
  54. GimpSignalHandlerFunc
  55. gimp_signal_private (gint                   signum,
  56.              GimpSignalHandlerFunc  handler,
  57.              gint                   flags)
  58. {
  59.   gint ret;
  60.   struct sigaction sa;
  61.   struct sigaction osa;
  62.  
  63.   /*  The sa_handler (mandated by POSIX.1) and sa_sigaction (a
  64.    *  common extension) are often implemented by the OS as members
  65.    *  of a union.  This means you CAN NOT set both, you set one or
  66.    *  the other.  Caveat programmer!
  67.    */
  68.  
  69.   /*  Passing gimp_signal_private a gimp_sighandler of NULL is not
  70.    *  an error, and generally results in the action for that signal
  71.    *  being set to SIG_DFL (default behavior).  Many OSes define
  72.    *  SIG_DFL as (void (*)()0, so setting sa_handler to NULL is
  73.    *  the same thing as passing SIG_DFL to it.
  74.    */
  75.   sa.sa_handler = handler;
  76.  
  77.   /*  Mask all signals while handler runs to avoid re-entrancy
  78.    *  problems.
  79.    */
  80.   sigfillset (&sa.sa_mask);
  81.  
  82.   sa.sa_flags = flags;
  83.  
  84.   ret = sigaction (signum, &sa, &osa);
  85.  
  86.   if (ret < 0)
  87.     g_error ("unable to set handler for signal %d\n", signum);
  88.  
  89.   return (GimpSignalHandlerFunc) osa.sa_handler;
  90. }
  91.