gimpsignal.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 3 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, see
  16. * <http://www.gnu.org/licenses/>.
  17. *
  18. * $Revision$
  19. */
  20. #include "config.h"
  21. #define _GNU_SOURCE /* for the sigaction stuff */
  22. #include <glib.h>
  23. #include "gimpsignal.h"
  24. /**
  25. * SECTION: gimpsignal
  26. * @title: gimpsignal
  27. * @short_description: Portable signal handling.
  28. * @see_also: signal(2), signal(5 or 7), sigaction(2).
  29. *
  30. * Portable signal handling.
  31. **/
  32. /* Courtesy of Austin Donnelly 06-04-2000 to address bug #2742 */
  33. /**
  34. * gimp_signal_private:
  35. * @signum: Selects signal to be handled see man 5 signal (or man 7 signal)
  36. * @handler: Handler that maps to signum. Invoked by O/S.
  37. * Handler gets signal that caused invocation. Corresponds
  38. * to the @sa_handler field of the @sigaction struct.
  39. * @flags: Preferences. OR'ed SA_&lt;xxx&gt;. See man sigaction. Corresponds
  40. * to the @sa_flags field of the @sigaction struct.
  41. *
  42. * This function furnishes a workalike for signal(2) but
  43. * which internally invokes sigaction(2) after certain
  44. * sa_flags are set; these primarily to ensure restarting
  45. * of interrupted system calls. See sigaction(2) It is a
  46. * aid to transition and not new development: that effort
  47. * should employ sigaction directly. [gosgood 18.04.2000]
  48. *
  49. * Cause @handler to be run when @signum is delivered. We
  50. * use sigaction(2) rather than signal(2) so that we can control the
  51. * signal handler's environment completely via @flags: some signal(2)
  52. * implementations differ in their sematics, so we need to nail down
  53. * exactly what we want. [austin 06.04.2000]
  54. *
  55. * Returns: A reference to the signal handling function which was
  56. * active before the call to gimp_signal_private().
  57. */
  58. GimpSignalHandlerFunc
  59. gimp_signal_private (gint signum,
  60. GimpSignalHandlerFunc handler,
  61. gint flags)
  62. {
  63. #ifndef G_OS_WIN32
  64. gint ret;
  65. struct sigaction sa;
  66. struct sigaction osa;
  67. /* The sa_handler (mandated by POSIX.1) and sa_sigaction (a
  68. * common extension) are often implemented by the OS as members
  69. * of a union. This means you CAN NOT set both, you set one or
  70. * the other. Caveat programmer!
  71. */
  72. /* Passing gimp_signal_private a gimp_sighandler of NULL is not
  73. * an error, and generally results in the action for that signal
  74. * being set to SIG_DFL (default behavior). Many OSes define
  75. * SIG_DFL as (void (*)()0, so setting sa_handler to NULL is
  76. * the same thing as passing SIG_DFL to it.
  77. */
  78. sa.sa_handler = handler;
  79. /* Mask all signals while handler runs to avoid re-entrancy
  80. * problems.
  81. */
  82. sigfillset (&sa.sa_mask);
  83. sa.sa_flags = flags;
  84. ret = sigaction (signum, &sa, &osa);
  85. if (ret < 0)
  86. g_error ("unable to set handler for signal %d\n", signum);
  87. return (GimpSignalHandlerFunc) osa.sa_handler;
  88. #else
  89. return NULL; /* Or g_error()? Should all calls to
  90. * this function really be inside
  91. * #ifdef G_OS_UNIX?
  92. */
  93. #endif
  94. }