2011-09-28 06:32:19 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-09-28 06:32:19 +04:00
|
|
|
* Signal handling
|
|
|
|
*
|
|
|
|
* Copyright 2011 Shea Levy <shea@shealevy.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-09-28 09:15:42 +04:00
|
|
|
#include <stddef.h>
|
2012-11-22 06:22:06 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2011-09-28 06:32:19 +04:00
|
|
|
#include <freerdp/utils/signal.h>
|
2014-08-07 18:51:24 +04:00
|
|
|
#include <freerdp/utils/debug.h>
|
2012-10-14 04:31:01 +04:00
|
|
|
|
2011-09-28 10:48:18 +04:00
|
|
|
#ifdef _WIN32
|
2012-10-14 04:31:01 +04:00
|
|
|
|
2011-09-28 10:48:18 +04:00
|
|
|
#include <errno.h>
|
2012-10-14 04:31:01 +04:00
|
|
|
|
2011-09-28 10:48:18 +04:00
|
|
|
int freerdp_handle_signals(void)
|
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-14 04:31:01 +04:00
|
|
|
|
2013-01-25 20:50:40 +04:00
|
|
|
#elif !defined(ANDROID)
|
2012-10-14 04:31:01 +04:00
|
|
|
|
2011-09-28 06:32:19 +04:00
|
|
|
volatile sig_atomic_t terminal_needs_reset = 0;
|
|
|
|
int terminal_fildes = 0;
|
|
|
|
struct termios orig_flags;
|
|
|
|
struct termios new_flags;
|
|
|
|
|
2011-09-28 09:15:42 +04:00
|
|
|
static void fatal_handler(int signum)
|
|
|
|
{
|
|
|
|
struct sigaction default_sigaction;
|
2011-09-28 10:09:52 +04:00
|
|
|
sigset_t this_mask;
|
2011-09-28 09:15:42 +04:00
|
|
|
|
2014-08-07 18:51:24 +04:00
|
|
|
DEBUG_MSG("fatal_handler: signum=%d\n", signum);
|
2014-01-20 20:20:50 +04:00
|
|
|
|
2011-09-28 09:15:42 +04:00
|
|
|
if (terminal_needs_reset)
|
|
|
|
tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags);
|
|
|
|
|
|
|
|
default_sigaction.sa_handler = SIG_DFL;
|
|
|
|
sigfillset(&(default_sigaction.sa_mask));
|
|
|
|
default_sigaction.sa_flags = 0;
|
|
|
|
|
|
|
|
sigaction(signum, &default_sigaction, NULL);
|
2011-09-28 10:09:52 +04:00
|
|
|
|
|
|
|
sigemptyset(&this_mask);
|
|
|
|
sigaddset(&this_mask, signum);
|
|
|
|
pthread_sigmask(SIG_UNBLOCK, &this_mask, NULL);
|
2011-09-28 09:15:42 +04:00
|
|
|
raise(signum);
|
|
|
|
}
|
|
|
|
|
2013-02-21 19:46:11 +04:00
|
|
|
const int fatal_signals[] =
|
2011-09-28 06:32:19 +04:00
|
|
|
{
|
2013-02-21 19:46:11 +04:00
|
|
|
SIGABRT,
|
|
|
|
SIGALRM,
|
|
|
|
SIGBUS,
|
|
|
|
SIGFPE,
|
|
|
|
SIGHUP,
|
|
|
|
SIGILL,
|
|
|
|
SIGINT,
|
|
|
|
SIGKILL,
|
|
|
|
SIGQUIT,
|
|
|
|
SIGSEGV,
|
|
|
|
SIGSTOP,
|
|
|
|
SIGTERM,
|
|
|
|
SIGTSTP,
|
|
|
|
SIGTTIN,
|
|
|
|
SIGTTOU,
|
|
|
|
SIGUSR1,
|
|
|
|
SIGUSR2,
|
2011-09-28 09:21:38 +04:00
|
|
|
#ifdef SIGPOLL
|
2013-02-21 19:46:11 +04:00
|
|
|
SIGPOLL,
|
2011-09-28 09:21:38 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPROF
|
2013-02-21 19:46:11 +04:00
|
|
|
SIGPROF,
|
2011-09-28 09:21:38 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SIGSYS
|
2013-02-21 19:46:11 +04:00
|
|
|
SIGSYS,
|
2011-09-28 09:21:38 +04:00
|
|
|
#endif
|
2013-02-21 19:46:11 +04:00
|
|
|
SIGTRAP,
|
2011-09-28 09:21:38 +04:00
|
|
|
#ifdef SIGVTALRM
|
2013-02-21 19:46:11 +04:00
|
|
|
SIGVTALRM,
|
2011-09-28 09:21:38 +04:00
|
|
|
#endif
|
2013-02-21 19:46:11 +04:00
|
|
|
SIGXCPU,
|
|
|
|
SIGXFSZ
|
|
|
|
};
|
|
|
|
|
|
|
|
int freerdp_handle_signals(void)
|
|
|
|
{
|
2011-09-28 09:15:42 +04:00
|
|
|
int signal_index;
|
|
|
|
sigset_t orig_set;
|
2013-02-21 19:46:11 +04:00
|
|
|
struct sigaction orig_sigaction;
|
|
|
|
struct sigaction fatal_sigaction;
|
2011-09-28 09:15:42 +04:00
|
|
|
|
|
|
|
sigfillset(&(fatal_sigaction.sa_mask));
|
2011-09-28 10:40:35 +04:00
|
|
|
sigdelset(&(fatal_sigaction.sa_mask), SIGCONT);
|
2011-09-28 09:15:42 +04:00
|
|
|
pthread_sigmask(SIG_BLOCK, &(fatal_sigaction.sa_mask), &orig_set);
|
|
|
|
|
|
|
|
fatal_sigaction.sa_handler = fatal_handler;
|
|
|
|
fatal_sigaction.sa_flags = 0;
|
|
|
|
|
2013-02-21 19:46:11 +04:00
|
|
|
for (signal_index = 0; signal_index < ARRAYSIZE(fatal_signals); signal_index++)
|
|
|
|
{
|
|
|
|
if (sigaction(fatal_signals[signal_index], NULL, &orig_sigaction) == 0)
|
|
|
|
{
|
2011-09-28 09:15:42 +04:00
|
|
|
if (orig_sigaction.sa_handler != SIG_IGN)
|
2013-02-21 19:46:11 +04:00
|
|
|
{
|
|
|
|
sigaction(fatal_signals[signal_index], &fatal_sigaction, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 09:15:42 +04:00
|
|
|
|
|
|
|
pthread_sigmask(SIG_SETMASK, &orig_set, NULL);
|
2013-02-21 19:46:11 +04:00
|
|
|
|
2014-01-20 20:20:50 +04:00
|
|
|
/* Ignore SIGPIPE signal. */
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
2011-09-28 10:48:18 +04:00
|
|
|
return 0;
|
2011-09-28 06:32:19 +04:00
|
|
|
}
|
2012-10-14 04:31:01 +04:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int freerdp_handle_signals(void)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-09-28 10:48:18 +04:00
|
|
|
#endif
|