remove unneeded (double) close
[enigma2.git] / main / bsod.cpp
1 #include <string.h>
2 #include <signal.h>
3 #include <asm/ptrace.h>
4
5 #include <lib/base/eerror.h>
6 #include <lib/base/smartptr.h>
7 #include <lib/gdi/grc.h>
8 #include <lib/gdi/gfbdc.h>
9
10 #include "version.h"
11
12 /************************************************/
13
14 #define CRASH_EMAILADDR "crashlog@dream-multimedia-tv.de"
15
16 #define RINGBUFFER_SIZE 16384
17 static char ringbuffer[RINGBUFFER_SIZE];
18 static int ringbuffer_head;
19
20 static void addToLogbuffer(const char *data, int len)
21 {
22         while (len)
23         {
24                 int remaining = RINGBUFFER_SIZE - ringbuffer_head;
25         
26                 if (remaining > len)
27                         remaining = len;
28         
29                 memcpy(ringbuffer + ringbuffer_head, data, remaining);
30                 len -= remaining;
31                 data += remaining;
32                 ringbuffer_head += remaining;
33                 if (ringbuffer_head >= RINGBUFFER_SIZE)
34                         ringbuffer_head = 0;
35         }
36 }
37
38 static std::string getLogBuffer()
39 {
40         int begin = ringbuffer_head;
41         while (ringbuffer[begin] == 0)
42         {
43                 ++begin;
44                 if (begin == RINGBUFFER_SIZE)
45                         begin = 0;
46                 if (begin == ringbuffer_head)
47                         return "";
48         }
49         if (begin < ringbuffer_head)
50                 return std::string(ringbuffer + begin, ringbuffer_head - begin);
51         else
52         {
53                 return std::string(ringbuffer + begin, RINGBUFFER_SIZE - begin) + std::string(ringbuffer, ringbuffer_head);
54         }
55 }
56
57 static void addToLogbuffer(int level, const std::string &log)
58 {
59         addToLogbuffer(log.c_str(), log.size());
60 }
61
62
63 extern std::string getLogBuffer();
64
65 void bsodFatal()
66 {
67         char logfile[128];
68         sprintf(logfile, "/media/hdd/enigma2_crash_%u.log", (unsigned int)time(0));
69         FILE *f = fopen(logfile, "wb");
70         
71         std::string lines = getLogBuffer();
72
73         if (f)
74         {
75                 time_t t = time(0);
76                 fprintf(f, "enigma2 crashed on %s", ctime(&t));
77 #ifdef ENIGMA2_CHECKOUT_TAG
78                 fprintf(f, "enigma2 CVS TAG: " ENIGMA2_CHECKOUT_TAG "\n");
79 #else
80                 fprintf(f, "enigma2 compiled on " __DATE__ "\n");
81 #endif
82 #ifdef ENIGMA2_CHECKOUT_ROOT
83                 fprintf(f, "enigma2 checked out from " ENIGMA2_CHECKOUT_ROOT "\n");
84 #endif
85                 fprintf(f, "please email this file to " CRASH_EMAILADDR "\n");
86                 std::string buffer = getLogBuffer();
87                 fwrite(buffer.c_str(), buffer.size(), 1, f);
88                 fclose(f);
89         }
90         
91         ePtr<gFBDC> my_dc;
92         gFBDC::getInstance(my_dc);
93         
94         {
95                 gPainter p(my_dc);
96                 p.resetOffset();
97                 p.resetClip(eRect(ePoint(0, 0), my_dc->size()));
98                 p.setBackgroundColor(gRGB(0x0000C0));
99                 p.setForegroundColor(gRGB(0xFFFFFF));
100         
101                 ePtr<gFont> font = new gFont("Regular", 20);
102                 p.setFont(font);
103                 p.clear();
104         
105                 eRect usable_area = eRect(100, 70, my_dc->size().width() - 150, 100);
106         
107                 p.renderText(usable_area, 
108                         "We are really sorry. Something happened "
109                         "which should not have happened, and "
110                         "resulted in a crash. If you want to help "
111                         "us in improving this situation, please send "
112                         "the logfile created in /hdd/ to " CRASH_EMAILADDR "."
113                         "Your receiver restarts in 10 seconds !", gPainter::RT_WRAP|gPainter::RT_HALIGN_LEFT);
114         
115                 usable_area = eRect(100, 170, my_dc->size().width() - 180, my_dc->size().height() - 20);
116         
117                 int i;
118         
119                 size_t start = std::string::npos + 1;
120                 for (i=0; i<20; ++i)
121                 {
122                         start = lines.rfind('\n', start - 1);
123                         if (start == std::string::npos)
124                         {
125                                 start = 0;
126                                 break;
127                         }
128                 }
129         
130                 font = new gFont("Regular", 14);
131                 p.setFont(font);
132         
133                 p.renderText(usable_area, 
134                         lines.substr(start), gPainter::RT_HALIGN_LEFT);
135                 p.flush();
136                 sleep(10);
137         }
138
139         raise(SIGKILL);
140 }
141
142 #if defined(__MIPSEL__)
143 void oops(const mcontext_t &context, int dumpcode)
144 {
145         eDebug("PC: %08lx, vaddr: %08lx", (unsigned long)context.pc, (unsigned long)context.badvaddr);
146 }
147 #else
148 #warning "no oops support!"
149 #error bla
150 #define NO_OOPS_SUPPORT
151 #endif
152
153 void handleFatalSignal(int signum, siginfo_t *si, void *ctx)
154 {
155         ucontext_t *uc = (ucontext_t*)ctx;
156         eDebug("KILLED BY signal %d", signum);
157 #ifndef NO_OOPS_SUPPORT
158         oops(uc->uc_mcontext, signum == SIGSEGV);
159 #endif
160         eDebug("-------");
161         bsodFatal();
162 }
163
164 void bsodCatchSignals()
165 {
166         struct sigaction act;
167         act.sa_handler = SIG_DFL;
168         act.sa_sigaction = handleFatalSignal;
169         act.sa_flags = SA_RESTART | SA_SIGINFO;
170         if (sigemptyset(&act.sa_mask) == -1)
171                 perror("sigemptyset");
172         
173                 /* start handling segfaults etc. */
174         sigaction(SIGSEGV, &act, 0);
175         sigaction(SIGILL, &act, 0);
176         sigaction(SIGBUS, &act, 0);
177 }
178
179 void bsodLogInit()
180 {
181         logOutput.connect(addToLogbuffer);
182 }