1a38107941725211e7c3f051f7a8f5e12199f03acmaeder/*
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke Unix SMB/CIFS implementation.
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens Elkner signal handling functions
9eb6a481980d81a55898ba418fba72fc3c09d8c8Dominik Luecke
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke Copyright (C) Andrew Tridgell 1998
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke This program is free software; you can redistribute it and/or modify
2eeec5240b424984e3ee26296da1eeab6c6d739eChristian Maeder it under the terms of the GNU General Public License as published by
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke the Free Software Foundation; either version 3 of the License, or
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke (at your option) any later version.
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke This program is distributed in the hope that it will be useful,
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke but WITHOUT ANY WARRANTY; without even the implied warranty of
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke GNU General Public License for more details.
da955132262baab309a50fdffe228c9efe68251dCui Jian
da955132262baab309a50fdffe228c9efe68251dCui Jian You should have received a copy of the GNU General Public License
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke along with this program. If not, see <http://www.gnu.org/licenses/>.
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke*/
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Luecke#include "util/util.h"
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Luecke#include <sys/types.h>
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke#include <sys/wait.h>
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke/**
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke * @file
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke * @brief Signal handling
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Luecke */
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke/**
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder Block sigs.
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder**/
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Lueckevoid BlockSignals(bool block, int signum)
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder{
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Luecke#ifdef HAVE_SIGPROCMASK
202df46772cac2ee2e8627ba196a5faebb6f9a05Christian Maeder sigset_t set;
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke sigemptyset(&set);
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke sigaddset(&set,signum);
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke sigprocmask(block?SIG_BLOCK:SIG_UNBLOCK,&set,NULL);
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder#elif defined(HAVE_SIGBLOCK)
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke if (block) {
fc5cd838fb20cabc8494e3fba1c76c434dc92f30Christian Maeder sigblock(sigmask(signum));
fc5cd838fb20cabc8494e3fba1c76c434dc92f30Christian Maeder } else {
fc5cd838fb20cabc8494e3fba1c76c434dc92f30Christian Maeder sigsetmask(siggetmask() & ~sigmask(signum));
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke }
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke#else
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke /* yikes! This platform can't block signals? */
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke static int done;
4df63f7187b1ba16cbe5c781db187a42f2f49579Dominik Luecke if (!done) {
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke DEBUG(SSSDBG_FATAL_FAILURE,"WARNING: No signal blocking available\n");
5ad5dffe06818a13e1632b1119fbca7881085fc1Dominik Luecke done=1;
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Luecke }
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder#endif
548f3850942936a8c6021185c8391dfcd3b03018Dominik Luecke}
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Luecke
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Luecke/**
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Luecke Catch a signal. This should implement the following semantics:
202df46772cac2ee2e8627ba196a5faebb6f9a05Christian Maeder
202df46772cac2ee2e8627ba196a5faebb6f9a05Christian Maeder 1) The handler remains installed after being called.
202df46772cac2ee2e8627ba196a5faebb6f9a05Christian Maeder 2) The signal should be blocked during handler execution.
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Luecke**/
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Luecke
b694e4b3f771a2f32042c9c505dd698bde969558Dominik Lueckevoid (*CatchSignal(int signum,void (*handler)(int )))(int)
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder{
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke#ifdef HAVE_SIGACTION
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke struct sigaction act;
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke struct sigaction oldact;
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke ZERO_STRUCT(act);
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke act.sa_handler = handler;
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke#ifdef SA_RESTART
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke /*
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke * We *want* SIGALRM to interrupt a system call.
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke */
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke if(signum != SIGALRM)
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke act.sa_flags = SA_RESTART;
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke#endif
6e2dba19f0c5ef183c1ac9cf1dff55b9626aeabdDominik Luecke sigemptyset(&act.sa_mask);
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder sigaddset(&act.sa_mask,signum);
sigaction(signum,&act,&oldact);
return oldact.sa_handler;
#else /* !HAVE_SIGACTION */
/* FIXME: need to handle sigvec and systems with broken signal() */
return signal(signum, handler);
#endif
}