~paulliu/ubuntu/quantal/freerdp/fixext

« back to all changes in this revision

Viewing changes to libfreerdp-utils/signal.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2012-01-31 10:02:14 UTC
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: package-import@ubuntu.com-20120131100214-zvig71djj2sqgq22
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * FreeRDP: A Remote Desktop Protocol Client
 
3
 * Signal handling
 
4
 *
 
5
 * Copyright 2011 Shea Levy <shea@shealevy.com>
 
6
 *
 
7
 * Licensed under the Apache License, Version 2.0 (the "License");
 
8
 * you may not use this file except in compliance with the License.
 
9
 * You may obtain a copy of the License at
 
10
 *
 
11
 *     http://www.apache.org/licenses/LICENSE-2.0
 
12
 *
 
13
 * Unless required by applicable law or agreed to in writing, software
 
14
 * distributed under the License is distributed on an "AS IS" BASIS,
 
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
16
 * See the License for the specific language governing permissions and
 
17
 * limitations under the License.
 
18
 */
 
19
 
 
20
#include <stddef.h>
 
21
#include <freerdp/utils/signal.h>
 
22
#ifdef _WIN32
 
23
#include <errno.h>
 
24
int freerdp_handle_signals(void)
 
25
{
 
26
        errno = ENOSYS;
 
27
        return -1;
 
28
}
 
29
#else
 
30
volatile sig_atomic_t terminal_needs_reset = 0;
 
31
int terminal_fildes = 0;
 
32
struct termios orig_flags;
 
33
struct termios new_flags;
 
34
 
 
35
static void fatal_handler(int signum)
 
36
{
 
37
        struct sigaction default_sigaction;
 
38
        sigset_t this_mask;
 
39
 
 
40
        if (terminal_needs_reset)
 
41
                tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags);
 
42
 
 
43
        default_sigaction.sa_handler = SIG_DFL;
 
44
        sigfillset(&(default_sigaction.sa_mask));
 
45
        default_sigaction.sa_flags = 0;
 
46
 
 
47
        sigaction(signum, &default_sigaction, NULL);
 
48
 
 
49
        sigemptyset(&this_mask);
 
50
        sigaddset(&this_mask, signum);
 
51
        pthread_sigmask(SIG_UNBLOCK, &this_mask, NULL);
 
52
        raise(signum);
 
53
}
 
54
 
 
55
int freerdp_handle_signals(void)
 
56
{
 
57
        const int fatal_signals[] = {
 
58
                SIGABRT,
 
59
                SIGALRM,
 
60
                SIGBUS,
 
61
                SIGFPE,
 
62
                SIGHUP,
 
63
                SIGILL,
 
64
                SIGINT,
 
65
                SIGKILL,
 
66
                SIGPIPE,
 
67
                SIGQUIT,
 
68
                SIGSEGV,
 
69
                SIGSTOP,
 
70
                SIGTERM,
 
71
                SIGTSTP,
 
72
                SIGTTIN,
 
73
                SIGTTOU,
 
74
                SIGUSR1,
 
75
                SIGUSR2,
 
76
#ifdef SIGPOLL
 
77
                SIGPOLL,
 
78
#endif
 
79
#ifdef SIGPROF
 
80
                SIGPROF,
 
81
#endif
 
82
#ifdef SIGSYS
 
83
                SIGSYS,
 
84
#endif
 
85
                SIGTRAP,
 
86
#ifdef SIGVTALRM
 
87
                SIGVTALRM,
 
88
#endif
 
89
                SIGXCPU,
 
90
                SIGXFSZ
 
91
        };
 
92
        int signal_index;
 
93
        sigset_t orig_set;
 
94
        struct sigaction orig_sigaction, fatal_sigaction;
 
95
 
 
96
        sigfillset(&(fatal_sigaction.sa_mask));
 
97
        sigdelset(&(fatal_sigaction.sa_mask), SIGCONT);
 
98
        pthread_sigmask(SIG_BLOCK, &(fatal_sigaction.sa_mask), &orig_set);
 
99
 
 
100
        fatal_sigaction.sa_handler = fatal_handler;
 
101
        fatal_sigaction.sa_flags  = 0;
 
102
 
 
103
        for (signal_index = 0;
 
104
                signal_index < (sizeof fatal_signals / sizeof fatal_signals[0]);
 
105
                signal_index++)
 
106
                if (sigaction(fatal_signals[signal_index],
 
107
                        NULL, &orig_sigaction) == 0)
 
108
                        if (orig_sigaction.sa_handler != SIG_IGN)
 
109
                                sigaction(fatal_signals[signal_index],
 
110
                                        &fatal_sigaction, NULL); 
 
111
 
 
112
        pthread_sigmask(SIG_SETMASK, &orig_set, NULL);
 
113
        return 0;
 
114
}
 
115
#endif