summaryrefslogtreecommitdiff
path: root/src/crashhandler.c
blob: cd3eb3129ef6a33780a0430227bc7c433bfc068c (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
/*
 *
 * Compiz 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 <compiz.h>

#include "crashhandler_options.h"

static CompDisplay *cDisplay;

static void
crash_handler (int sig)
{
	if (sig == SIGSEGV || sig == SIGFPE || sig == SIGILL || sig == SIGABRT)
	{
		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/compiz_crash-%i.out; rm -f /tmp/gdb.tmp; echo \"\n[CRASH_HANDLER]: \\\"/tmp/compiz_crash-%i.out\\\" created!\n\"",
				 programName, getpid (), getpid (), getpid ());
		system (cmd);

		if (crashhandlerGetStartWm (cDisplay))
		{
			if (fork () == 0)
			{
				setsid ();
				putenv (cDisplay->displayString);
				execl ("/bin/sh", "/bin/sh", "-c",
					   crashhandlerGetWmCmd (cDisplay), NULL);
				exit (0);
			}
		}

		exit (1);
	}
}

static void
crashhandlerDisplayOptionChanged (CompDisplay * d, CompOption * opt,
								  CrashhandlerDisplayOptions num)
{
	switch (num)
	{
	case CrashhandlerDisplayOptionEnabled:
		if (crashhandlerGetEnabled (d))
		{
			// 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);
		}
		break;
	default:
		break;
	}
}

static Bool
crashhandlerInitDisplay (CompPlugin * p, CompDisplay * d)
{
	cDisplay = d;

	if (crashhandlerGetEnabled (d))
	{
		// segmentation fault
		signal (SIGSEGV, crash_handler);
		// floating point exception
		signal (SIGFPE, crash_handler);
		// illegal instruction
		signal (SIGILL, crash_handler);
		// abort
		signal (SIGABRT, crash_handler);
	}

	crashhandlerSetEnabledNotify (d, crashhandlerDisplayOptionChanged);

	return TRUE;
}

static void
crashhandlerFiniDisplay (CompPlugin * p, CompDisplay * d)
{
	signal (SIGSEGV, SIG_DFL);
	signal (SIGFPE, SIG_DFL);
	signal (SIGILL, SIG_DFL);
	signal (SIGABRT, SIG_DFL);
}

static int
crashhandlerGetVersion (CompPlugin * plugin, int version)
{
	return ABIVERSION;
}

CompPluginVTable crashhandlerVTable = {
	"crashhandler",
	N_("Crash handler"),
	N_("Compiz crash handler plugin"),
	crashhandlerGetVersion,
	0,
	0,
	crashhandlerInitDisplay,
	crashhandlerFiniDisplay,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0
};

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