summaryrefslogtreecommitdiff
path: root/beryl-plugins/src/crashhandler.c
blob: 2c4e89d0d1956a01c94ba9ac46ddaf3de7ef7c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
 *
 * Beryl crash handler plugin
 *
 * crashhandler.c
 *
 * Copyright : (C) 2006 by Dennis Kasprzyk
 * E-mail    : onestone@beryl-project.org
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 **/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

#include <beryl.h>

#define GET_CRASHHANDLER_DISPLAY(d)                                  \
    ((CrashhandlerDisplay *) (d)->privates[displayPrivateIndex].ptr)

#define CRASHHANDLER_DISPLAY(d)                      \
    CrashhandlerDisplay *cd = GET_CRASHHANDLER_DISPLAY (d)

#define CRASHHANDLER_DISPLAY_OPTION_ENABLED                 0
#define CRASHHANDLER_DISPLAY_OPTION_START_WM                1
#define CRASHHANDLER_DISPLAY_OPTION_WM                      2
#define CRASHHANDLER_DISPLAY_OPTION_NUM                     3

#define CRASHHANDLER_DISPLAY_OPTION_ENABLED_DEFAULT         TRUE
#define CRASHHANDLER_DISPLAY_OPTION_START_WM_DEFAULT        FALSE
#define CRASHHANDLER_DISPLAY_OPTION_WM_DEFAULT              ""

#define NUM_OPTIONS(s) (sizeof ((s)->opt) / sizeof (CompOption))

static int displayPrivateIndex = 0;
static CompDisplay *cDisplay;

typedef struct _CrashhandlerDisplay
{
	int screenPrivateIndex;
	CompOption opt[CRASHHANDLER_DISPLAY_OPTION_NUM];
} CrashhandlerDisplay;

static void crash_handler(int sig)
{
	if (sig == SIGSEGV || sig == SIGFPE || sig == SIGILL || sig == SIGABRT)
	{
		CRASHHANDLER_DISPLAY(cDisplay);
		static int count = 0;

		if (++count > 1)
			exit(1);
		// backtrace
		char cmd[1024];

		sprintf(cmd,
				"echo -e \"set prompt\nthread apply all bt full\necho \\\\\\n\necho \\\\\\n\nbt\nquit\" > /tmp/gdb.tmp; gdb -q %s %i < /tmp/gdb.tmp | grep -v \"No symbol table\" | tee /tmp/beryl_crash-%i.out; rm -f /tmp/gdb.tmp; echo \"\n[CRASH_HANDLER]: \\\"/tmp/beryl_crash-%i.out\\\" created!\n\"",
				getProgramName(), getpid(), getpid(), getpid());
		system(cmd);

		if (cd->opt[CRASHHANDLER_DISPLAY_OPTION_START_WM].value.b)
		{
			if (fork() == 0)
			{
				setsid();
				putenv(cDisplay->displayString);
				execl("/bin/sh", "/bin/sh", "-c",
					  cd->opt[CRASHHANDLER_DISPLAY_OPTION_WM].value.s, NULL);
				exit(0);
			}
		}

		exit(1);
	}
}

static Bool
crashhandlerSetDisplayOption(CompDisplay * display, char *name,
							 CompOptionValue * value)
{
	CompOption *o;
	int index;

	CRASHHANDLER_DISPLAY(display);

	o = compFindOption(cd->opt, NUM_OPTIONS(cd), name, &index);
	if (!o)
		return FALSE;

	switch (index)
	{
	case CRASHHANDLER_DISPLAY_OPTION_ENABLED:
		if (compSetBoolOption(o, value))
		{
			if (value->b)
			{
				// enable crash handler
				signal(SIGSEGV, crash_handler);
				signal(SIGFPE, crash_handler);
				signal(SIGILL, crash_handler);
				signal(SIGABRT, crash_handler);
			}
			else
			{
				// disable crash handler
				signal(SIGSEGV, SIG_DFL);
				signal(SIGFPE, SIG_DFL);
				signal(SIGILL, SIG_DFL);
				signal(SIGABRT, SIG_DFL);
			}
			return TRUE;
		}
		break;
	case CRASHHANDLER_DISPLAY_OPTION_START_WM:
		if (compSetBoolOption(o, value))
			return TRUE;
		break;
	case CRASHHANDLER_DISPLAY_OPTION_WM:
		if (compSetStringOption(o, value))
			return TRUE;
		break;
	default:
		break;
	}

	return FALSE;
}

static void crashhandlerDisplayInitOptions(CrashhandlerDisplay * cd)
{
	CompOption *o;

	o = &cd->opt[CRASHHANDLER_DISPLAY_OPTION_ENABLED];
	o->advanced = False;
	o->name = "enabled";
	o->group = N_("Misc. settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Enable Crash Handler");
	o->longDesc = N_("Activate Crash Handler.");
	o->type = CompOptionTypeBool;
	o->value.b = CRASHHANDLER_DISPLAY_OPTION_ENABLED_DEFAULT;

	o = &cd->opt[CRASHHANDLER_DISPLAY_OPTION_START_WM];
	o->advanced = False;
	o->name = "start_window_manager";
	o->group = N_("Misc. settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Start Other Window Manager");
	o->longDesc = N_("Start other window manager on Crash.");
	o->type = CompOptionTypeBool;
	o->value.b = CRASHHANDLER_DISPLAY_OPTION_START_WM_DEFAULT;

	o = &cd->opt[CRASHHANDLER_DISPLAY_OPTION_WM];
	o->advanced = False;
	o->name = "window_manager_command_line";
	o->group = N_("Misc. settings");
	o->subGroup = N_("");
	o->displayHints = "command;";
	o->shortDesc = N_("Window Manager Command Line");
	o->longDesc =
			N_("Window manager command line. DO NOT ENTER BERYL HERE!!!");
	o->type = CompOptionTypeString;
	o->value.s = strdup(CRASHHANDLER_DISPLAY_OPTION_WM_DEFAULT);
	o->rest.s.string = 0;
	o->rest.s.nString = 0;

}


static CompOption *crashhandlerGetDisplayOptions(CompDisplay * display,
												 int *count)
{
	if (display)
	{
		CRASHHANDLER_DISPLAY(display);

		*count = NUM_OPTIONS(cd);
		return cd->opt;
	}
	else
	{
		CrashhandlerDisplay *cd = malloc(sizeof(CrashhandlerDisplay));

		crashhandlerDisplayInitOptions(cd);
		*count = NUM_OPTIONS(cd);
		return cd->opt;
	}
}


static Bool crashhandlerInitDisplay(CompPlugin * p, CompDisplay * d)
{
	//Generate a bench display
	CrashhandlerDisplay *cd =
			(CrashhandlerDisplay *) malloc(sizeof(CrashhandlerDisplay));
	//Allocate a private index
	cd->screenPrivateIndex = allocateScreenPrivateIndex(d);
	//Check if its valid
	if (cd->screenPrivateIndex < 0)
	{
		//Its invalid so free memory and return
		free(cd);
		return FALSE;
	}
	crashhandlerDisplayInitOptions(cd);
	cDisplay = d;

	if (cd->opt[CRASHHANDLER_DISPLAY_OPTION_ENABLED].value.b)
	{
		// segmentation fault
		signal(SIGSEGV, crash_handler);
		// floating point exception
		signal(SIGFPE, crash_handler);
		// illegal instruction
		signal(SIGILL, crash_handler);
		// abort
		signal(SIGABRT, crash_handler);
	}


	//Record the display
	d->privates[displayPrivateIndex].ptr = cd;
	return TRUE;
}

static void crashhandlerFiniDisplay(CompPlugin * p, CompDisplay * d)
{
	signal(SIGSEGV, SIG_DFL);
	CRASHHANDLER_DISPLAY(d);
	//Free the private index
	freeScreenPrivateIndex(d, cd->screenPrivateIndex);
	//Free the pointer
	free(cd);
}



static Bool crashhandlerInit(CompPlugin * p)
{
	displayPrivateIndex = allocateDisplayPrivateIndex();

	if (displayPrivateIndex < 0)
		return FALSE;

	return TRUE;
}

static void crashhandlerFini(CompPlugin * p)
{
	if (displayPrivateIndex >= 0)
		freeDisplayPrivateIndex(displayPrivateIndex);
}


CompPluginVTable crashhandlerVTable = {
	"crashhandler",
	N_("Crash handler"),
	N_("Beryl crash handler plugin"),
	crashhandlerInit,
	crashhandlerFini,
	crashhandlerInitDisplay,
	crashhandlerFiniDisplay,
	0,
	0,
	0,
	0,
	crashhandlerGetDisplayOptions,
	crashhandlerSetDisplayOption,
	0,
	0,
	0,
	0,
	0,
	0,
	BERYL_ABI_INFO,
	"beryl-plugins",
	"devel",
	0,
	0,
	False,
};

CompPluginVTable *getCompPluginInfo(void)
{
	return &crashhandlerVTable;
}