2002-03-30 07:10:18 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
2000-04-22 00:42:11 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
2000-04-22 00:42:11 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2002-03-30 07:10:18 +08:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-04-22 00:42:11 +08:00
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
2018-07-12 05:27:07 +08:00
|
|
|
* <https://www.gnu.org/licenses/>.
|
2000-04-22 00:42:11 +08:00
|
|
|
*
|
|
|
|
* $Revision$
|
2002-03-30 07:10:18 +08:00
|
|
|
*/
|
2000-04-22 00:42:11 +08:00
|
|
|
|
2006-05-20 15:18:25 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-06-17 00:21:35 +08:00
|
|
|
#define _GNU_SOURCE /* for the sigaction stuff */
|
2006-05-20 15:18:25 +08:00
|
|
|
|
2000-05-31 07:38:46 +08:00
|
|
|
#include <glib.h>
|
|
|
|
|
2000-04-22 00:42:11 +08:00
|
|
|
#include "gimpsignal.h"
|
|
|
|
|
2000-04-22 02:37:25 +08:00
|
|
|
|
2010-06-30 01:13:40 +08:00
|
|
|
/**
|
|
|
|
* SECTION: gimpsignal
|
|
|
|
* @title: gimpsignal
|
|
|
|
* @short_description: Portable signal handling.
|
|
|
|
* @see_also: signal(2), signal(5 or 7), sigaction(2).
|
|
|
|
*
|
|
|
|
* Portable signal handling.
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
2000-04-22 02:37:25 +08:00
|
|
|
/* Courtesy of Austin Donnelly 06-04-2000 to address bug #2742 */
|
|
|
|
|
2003-12-05 23:55:15 +08:00
|
|
|
/**
|
2019-08-08 05:32:15 +08:00
|
|
|
* gimp_signal_private: (skip)
|
2000-05-02 00:53:14 +08:00
|
|
|
* @signum: Selects signal to be handled see man 5 signal (or man 7 signal)
|
2003-12-05 23:55:15 +08:00
|
|
|
* @handler: Handler that maps to signum. Invoked by O/S.
|
2000-05-02 00:53:14 +08:00
|
|
|
* Handler gets signal that caused invocation. Corresponds
|
|
|
|
* to the @sa_handler field of the @sigaction struct.
|
2003-02-07 02:51:44 +08:00
|
|
|
* @flags: Preferences. OR'ed SA_<xxx>. See man sigaction. Corresponds
|
2000-05-02 00:53:14 +08:00
|
|
|
* to the @sa_flags field of the @sigaction struct.
|
2000-04-22 00:42:11 +08:00
|
|
|
*
|
|
|
|
* This function furnishes a workalike for signal(2) but
|
|
|
|
* which internally invokes sigaction(2) after certain
|
|
|
|
* sa_flags are set; these primarily to ensure restarting
|
2003-12-05 23:55:15 +08:00
|
|
|
* of interrupted system calls. See sigaction(2) It is a
|
|
|
|
* aid to transition and not new development: that effort
|
|
|
|
* should employ sigaction directly. [gosgood 18.04.2000]
|
2000-04-22 00:42:11 +08:00
|
|
|
*
|
2000-05-02 00:53:14 +08:00
|
|
|
* Cause @handler to be run when @signum is delivered. We
|
2000-04-22 00:42:11 +08:00
|
|
|
* use sigaction(2) rather than signal(2) so that we can control the
|
2000-05-02 00:53:14 +08:00
|
|
|
* signal handler's environment completely via @flags: some signal(2)
|
2018-04-19 02:57:03 +08:00
|
|
|
* implementations differ in their semantics, so we need to nail down
|
2000-04-22 02:37:25 +08:00
|
|
|
* exactly what we want. [austin 06.04.2000]
|
2000-04-22 00:42:11 +08:00
|
|
|
*
|
2000-05-02 00:53:14 +08:00
|
|
|
* Returns: A reference to the signal handling function which was
|
|
|
|
* active before the call to gimp_signal_private().
|
2000-04-22 00:42:11 +08:00
|
|
|
*/
|
2000-05-02 00:53:14 +08:00
|
|
|
GimpSignalHandlerFunc
|
|
|
|
gimp_signal_private (gint signum,
|
2006-04-12 18:53:28 +08:00
|
|
|
GimpSignalHandlerFunc handler,
|
|
|
|
gint flags)
|
2000-04-22 00:42:11 +08:00
|
|
|
{
|
2002-03-30 07:10:18 +08:00
|
|
|
#ifndef G_OS_WIN32
|
2000-05-01 22:57:54 +08:00
|
|
|
gint ret;
|
2000-04-22 00:42:11 +08:00
|
|
|
struct sigaction sa;
|
|
|
|
struct sigaction osa;
|
|
|
|
|
2000-05-01 22:57:54 +08:00
|
|
|
/* The sa_handler (mandated by POSIX.1) and sa_sigaction (a
|
|
|
|
* common extension) are often implemented by the OS as members
|
|
|
|
* of a union. This means you CAN NOT set both, you set one or
|
|
|
|
* the other. Caveat programmer!
|
|
|
|
*/
|
2000-04-22 00:42:11 +08:00
|
|
|
|
2000-05-01 22:57:54 +08:00
|
|
|
/* Passing gimp_signal_private a gimp_sighandler of NULL is not
|
|
|
|
* an error, and generally results in the action for that signal
|
|
|
|
* being set to SIG_DFL (default behavior). Many OSes define
|
|
|
|
* SIG_DFL as (void (*)()0, so setting sa_handler to NULL is
|
|
|
|
* the same thing as passing SIG_DFL to it.
|
|
|
|
*/
|
2000-05-02 00:53:14 +08:00
|
|
|
sa.sa_handler = handler;
|
2000-04-22 00:42:11 +08:00
|
|
|
|
2000-05-01 22:57:54 +08:00
|
|
|
/* Mask all signals while handler runs to avoid re-entrancy
|
|
|
|
* problems.
|
|
|
|
*/
|
2000-04-22 00:42:11 +08:00
|
|
|
sigfillset (&sa.sa_mask);
|
|
|
|
|
2000-05-02 00:53:14 +08:00
|
|
|
sa.sa_flags = flags;
|
2000-04-22 00:42:11 +08:00
|
|
|
|
|
|
|
ret = sigaction (signum, &sa, &osa);
|
2000-05-01 22:57:54 +08:00
|
|
|
|
2000-04-22 00:42:11 +08:00
|
|
|
if (ret < 0)
|
2000-04-22 02:37:25 +08:00
|
|
|
g_error ("unable to set handler for signal %d\n", signum);
|
2000-05-01 22:57:54 +08:00
|
|
|
|
2000-05-02 00:53:14 +08:00
|
|
|
return (GimpSignalHandlerFunc) osa.sa_handler;
|
2002-03-30 07:10:18 +08:00
|
|
|
#else
|
2006-05-20 15:18:25 +08:00
|
|
|
return NULL; /* Or g_error()? Should all calls to
|
2006-04-12 18:53:28 +08:00
|
|
|
* this function really be inside
|
|
|
|
* #ifdef G_OS_UNIX?
|
|
|
|
*/
|
2002-03-30 07:10:18 +08:00
|
|
|
#endif
|
2000-04-22 00:42:11 +08:00
|
|
|
}
|