1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  * Copyright (C) 2004-2021 Kim Woelders
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies of the Software, its documentation and marketing & publicity
14  * materials, and acknowledgment shall be given in the documentation, materials
15  * and software packages that this Software was used.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "config.h"
25 
26 #include <sys/wait.h>
27 #include <signal.h>
28 
29 #include "E.h"
30 #include "session.h"
31 
32 static void
SignalHandler(int sig)33 SignalHandler(int sig)
34 {
35    static int          loop_count = 0;
36    int                 status;
37 
38    Mode.wm.in_signal_handler = 1;
39 
40    if (EDebug(EDBUG_TYPE_SESSION))
41       Eprintf("%s: signal=%d\n", __func__, sig);
42 
43    switch (sig)
44      {
45      case SIGHUP:
46 	SessionExit(EEXIT_RESTART, NULL);
47 	break;
48 
49      default:
50      case SIGINT:
51      case SIGQUIT:
52      case SIGABRT:
53      case SIGTERM:
54 	SessionExit(EEXIT_EXIT, NULL);
55 	break;
56 
57      case SIGPIPE:
58      case SIGALRM:
59      case SIGUSR1:
60      case SIGUSR2:
61 	break;
62 
63      case SIGILL:
64 	Mode.wm.exit_now = 1;
65 	Alert(_("Enlightenment performed an Illegal Instruction.\n" "\n"
66 		"This most likely is due to you having installed an run a\n"
67 		"binary of Enlightenment that was compiled for a make or model\n"
68 		"of CPU not 100%% identical or compatible with yours. Please\n"
69 		"either obtain the correct package for your system, or\n"
70 		"re-compile Enlightenment and possibly any support libraries\n"
71 		"that you got in binary format to run Enlightenment.\n"));
72 	goto do_abort;
73 
74      case SIGFPE:
75 	Mode.wm.exit_now = 1;
76 	Alert(_("Enlightenment caused a Floating Point Exception.\n" "\n"
77 		"This means that Enlightenment or support library routines it calls\n"
78 		"have performed an illegal mathematical operation (most likely\n"
79 		"dividing a number by zero). This is most likely a bug. It is\n"
80 		"recommended to restart now. If you wish to help fix this please\n"
81 		"compile Enlightenment with debugging symbols in and run\n"
82 		"Enlightenment under gdb so you can backtrace for where it died and\n"
83 		"send in a useful bug report with backtrace information and variable\n"
84 		"dumps etc.\n"));
85 	goto do_abort;
86 
87      case SIGSEGV:
88 	Mode.wm.exit_now = 1;
89 	Alert(_("Enlightenment caused Segment Violation (Segfault)\n" "\n"
90 		"This means that Enlightenment or support library routines it calls\n"
91 		"have accessed areas of your system's memory that they are not\n"
92 		"allowed access to. This is most likely a bug. It is recommended to\n"
93 		"restart now. If you wish to help fix this please compile\n"
94 		"Enlightenment with debugging symbols in and run Enlightenment\n"
95 		"under gdb so you can backtrace for where it died and send in a\n"
96 		"useful bug report with backtrace information and variable\n"
97 		"dumps etc.\n"));
98 	goto do_abort;
99 
100      case SIGBUS:
101 	Mode.wm.exit_now = 1;
102 	Alert(_("Enlightenment caused Bus Error.\n" "\n"
103 		"It is suggested you check your hardware and OS installation.\n"
104 		"It is highly unusual to cause Bus Errors on operational\n"
105 		"hardware.\n"));
106 	goto do_abort;
107 
108       do_abort:
109 	if (loop_count > 0)
110 	   abort();
111 	loop_count++;
112 #if 0				/* Avoid X-ops? */
113 	disp = NULL;
114 #endif
115 	SessionExit(EEXIT_ERROR, NULL);
116 	abort();
117 	break;
118 
119      case SIGCHLD:
120 	while (waitpid(-1, &status, WNOHANG) > 0)
121 	   ;
122 	break;
123      }
124 
125    Mode.wm.in_signal_handler = 0;
126 }
127 
128 static void
doSignalsSetup(int setup)129 doSignalsSetup(int setup)
130 {
131    static const int    signals[] = {
132       SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, SIGSEGV, SIGPIPE,
133       SIGALRM, SIGTERM, SIGUSR1, SIGUSR2, SIGCHLD, SIGBUS
134    };
135    unsigned int        i, sig;
136    struct sigaction    sa;
137 
138    /*
139     * We may be here after a fork/exec within in a signal handler.
140     * Therefore, lets just clear the blocked signals.
141     */
142    sigemptyset(&sa.sa_mask);
143    sigprocmask(SIG_SETMASK, &sa.sa_mask, (sigset_t *) NULL);
144 
145    for (i = 0; i < E_ARRAY_SIZE(signals); i++)
146      {
147 	sig = signals[i];
148 	if (Mode.wm.coredump &&
149 	    (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || sig == SIGBUS))
150 	   continue;
151 
152 	if (setup)
153 	  {
154 	     sa.sa_handler = SignalHandler;
155 	     sa.sa_flags = (sig == SIGCHLD) ? SA_RESTART : 0;
156 	  }
157 	else
158 	  {
159 	     sa.sa_handler = SIG_DFL;
160 	     sa.sa_flags = 0;
161 	  }
162 	sigemptyset(&sa.sa_mask);
163 	sigaction(sig, &sa, (struct sigaction *)0);
164      }
165 }
166 
167 void
SignalsSetup(void)168 SignalsSetup(void)
169 {
170    /* This function will set up all the signal handlers for E */
171    doSignalsSetup(1);
172 }
173 
174 void
SignalsRestore(void)175 SignalsRestore(void)
176 {
177    /* This function will restore all the signal handlers for E */
178    doSignalsSetup(0);
179 }
180