2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2006 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
18 * @brief code for installing and uninstalling signal handlers
19 * @author Christian Grothoff
23 #include "gnunet_util_lib.h"
25 #define LOG(kind,...) GNUNET_log_from (kind, "util-signal", __VA_ARGS__)
28 struct GNUNET_SIGNAL_Context
31 struct GNUNET_SIGNAL_Context *next;
33 struct GNUNET_SIGNAL_Context *prev;
37 GNUNET_SIGNAL_Handler method;
40 struct sigaction oldsig;
44 static struct GNUNET_SIGNAL_Context *sc_head;
46 static struct GNUNET_SIGNAL_Context *sc_tail;
50 GNUNET_SIGNAL_Handler w32_sigchld_handler = NULL;
53 struct GNUNET_SIGNAL_Context *
54 GNUNET_SIGNAL_handler_install (int signum, GNUNET_SIGNAL_Handler handler)
56 struct GNUNET_SIGNAL_Context *ret;
62 ret = GNUNET_new (struct GNUNET_SIGNAL_Context);
64 ret->method = handler;
66 memset (&sig, 0, sizeof (sig));
67 sig.sa_handler = (void *) handler;
68 sigemptyset (&sig.sa_mask);
70 sig.sa_flags = SA_INTERRUPT; /* SunOS */
72 sig.sa_flags = SA_RESTART;
74 sigaction (signum, &sig, &ret->oldsig);
76 if (signum == GNUNET_SIGCHLD)
77 w32_sigchld_handler = handler;
80 __p_sig_fn_t sigret = signal (signum, (__p_sig_fn_t) handler);
82 if (sigret == SIG_ERR)
84 LOG (GNUNET_ERROR_TYPE_WARNING, _("signal (%d, %p) returned %d.\n"),
85 signum, handler, sigret);
89 GNUNET_CONTAINER_DLL_insert_tail (sc_head, sc_tail, ret);
94 GNUNET_SIGNAL_handler_uninstall (struct GNUNET_SIGNAL_Context *ctx)
99 sigemptyset (&sig.sa_mask);
100 sigaction (ctx->sig, &ctx->oldsig, &sig);
102 GNUNET_CONTAINER_DLL_remove (sc_head, sc_tail, ctx);
108 * Raise the given signal by calling the installed signal handlers. This will
109 * not use the @em raise() system call but only calls the handlers registered
110 * through GNUNET_SIGNAL_handler_install().
112 * @param sig the signal to raise
115 GNUNET_SIGNAL_raise (const int sig)
117 struct GNUNET_SIGNAL_Context *ctx;
119 for (ctx = sc_head; NULL != ctx; ctx = ctx->next)
123 if (NULL == ctx->method)