Merge commit 'origin/bug_600_softwaremanager_small_fixes' into experimental
[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/base/nconfig.h>
8 #include <lib/gdi/grc.h>
9 #include <lib/gdi/gfbdc.h>
10 #ifdef WITH_SDL
11 #include <lib/gdi/sdl.h>
12 #endif
13
14 #include "version.h"
15
16 /************************************************/
17
18 #define CRASH_EMAILADDR "crashlog@dream-multimedia-tv.de"
19 #define STDBUFFER_SIZE 512
20 #define RINGBUFFER_SIZE 16384
21 static char ringbuffer[RINGBUFFER_SIZE];
22 static int ringbuffer_head;
23
24 static void addToLogbuffer(const char *data, int len)
25 {
26         while (len)
27         {
28                 int remaining = RINGBUFFER_SIZE - ringbuffer_head;
29         
30                 if (remaining > len)
31                         remaining = len;
32         
33                 memcpy(ringbuffer + ringbuffer_head, data, remaining);
34                 len -= remaining;
35                 data += remaining;
36                 ringbuffer_head += remaining;
37                 if (ringbuffer_head >= RINGBUFFER_SIZE)
38                         ringbuffer_head = 0;
39         }
40 }
41
42 static std::string getLogBuffer()
43 {
44         int begin = ringbuffer_head;
45         while (ringbuffer[begin] == 0)
46         {
47                 ++begin;
48                 if (begin == RINGBUFFER_SIZE)
49                         begin = 0;
50                 if (begin == ringbuffer_head)
51                         return "";
52         }
53         if (begin < ringbuffer_head)
54                 return std::string(ringbuffer + begin, ringbuffer_head - begin);
55         else
56         {
57                 return std::string(ringbuffer + begin, RINGBUFFER_SIZE - begin) + std::string(ringbuffer, ringbuffer_head);
58         }
59 }
60
61 static void addToLogbuffer(int level, const std::string &log)
62 {
63         addToLogbuffer(log.c_str(), log.size());
64 }
65
66 static std::string getConfigFileValue(const char *entry)
67 {
68         std::string configfile = "/etc/enigma2/settings";
69         std::string configvalue;
70         if (entry)
71         {
72                 ePythonConfigQuery::getConfigValue(entry, configvalue);
73                 if (configvalue != "") //we get at least the default value if python is still alive
74                 {
75                         return configvalue;
76                 }
77                 else // get value from enigma2 settings file
78                 {
79                         FILE *f = fopen(configfile.c_str(), "r");
80                         if (!f)
81                         {
82                                 return "Error";
83                         }
84                         while (1)
85                         {
86                                 char line[1024];
87                                 if (!fgets(line, 1024, f))
88                                         break;
89                                 if (!strncmp(line, entry, strlen(entry) ))
90                                 {
91                                         if (strlen(line) && line[strlen(line)-1] == '\r')
92                                                 line[strlen(line)-1] = 0;
93                                         if (strlen(line) && line[strlen(line)-1] == '\n')
94                                                 line[strlen(line)-1] = 0;
95                                         std::string tmp = line;
96                                         int posEqual = tmp.find("=", 0);
97                                         configvalue = tmp.substr(posEqual+1);
98                                 }
99                         }
100                         fclose(f);
101                         return configvalue;
102                 }
103         }
104 }
105
106 static std::string getFileContent(const char *file)
107 {
108         std::string filecontent;
109
110         if (file)
111         {
112                 FILE *f = fopen(file, "r");
113                 if (!f)
114                 {
115                         return "Error";
116                 }
117                 while (1)
118                 {
119                         char line[1024];
120                         if (!fgets(line, 1024, f))
121                                 break;
122                         std::string tmp = line;
123                         std::string password;
124                         int pwdpos = tmp.find(".password=", 0);
125                         if( pwdpos != std::string::npos)
126                         {
127                                 filecontent += tmp.substr(0,pwdpos +10);
128                                 for ( int pos = pwdpos +10; pos < tmp.length()-1; ++pos )
129                                 {
130                                         filecontent += "X";
131                                 }
132                                 filecontent += "\n";
133                         }
134                         else {
135                                 filecontent += line;
136                         }
137                 }
138                 fclose(f);
139         }
140         return filecontent;
141 }
142
143 static std::string execCommand(char* cmd) {
144         FILE* pipe = popen(cmd, "r");
145         if (!pipe)
146                 return "Error";
147         char buffer[STDBUFFER_SIZE];
148         std::string result = "";
149         while(!feof(pipe))
150         {
151                 if(!fgets(buffer,STDBUFFER_SIZE, pipe))
152                         break;
153                 result += buffer;
154         }
155         pclose(pipe);
156         return result;
157 }
158
159 extern std::string execCommand();
160 extern std::string getConfigFileValue();
161 extern std::string getFileContent();
162 extern std::string getLogBuffer();
163
164 #define INFOFILE "/maintainer.info"
165
166 void bsodFatal(const char *component)
167 {
168         char logfile[128];
169         sprintf(logfile, "/media/hdd/enigma2_crash_%u.log", (unsigned int)time(0));
170         FILE *f = fopen(logfile, "wb");
171         
172         std::string lines = getLogBuffer();
173         
174                 /* find python-tracebacks, and extract "  File "-strings */
175         size_t start = 0;
176         
177         char crash_emailaddr[256] = CRASH_EMAILADDR;
178         char crash_component[256] = "enigma2";
179
180         if (component)
181                 snprintf(crash_component, 256, component);
182         else
183         {
184                 while ((start = lines.find("\n  File \"", start)) != std::string::npos)
185                 {
186                         start += 9;
187                         size_t end = lines.find("\"", start);
188                         if (end == std::string::npos)
189                                 break;
190                         end = lines.rfind("/", end);
191                                 /* skip a potential prefix to the path */
192                         unsigned int path_prefix = lines.find("/usr/", start);
193                         if (path_prefix != std::string::npos && path_prefix < end)
194                                 start = path_prefix;
195
196                         if (end == std::string::npos)
197                                 break;
198                         if (end - start >= (256 - strlen(INFOFILE)))
199                                 continue;
200                         char filename[256];
201                         snprintf(filename, 256, "%s%s", lines.substr(start, end - start).c_str(), INFOFILE);
202                         FILE *cf = fopen(filename, "r");
203                         if (cf)
204                         {
205                                 fgets(crash_emailaddr, sizeof crash_emailaddr, cf);
206                                 if (*crash_emailaddr && crash_emailaddr[strlen(crash_emailaddr)-1] == '\n')
207                                         crash_emailaddr[strlen(crash_emailaddr)-1] = 0;
208
209                                 fgets(crash_component, sizeof crash_component, cf);
210                                 if (*crash_component && crash_component[strlen(crash_component)-1] == '\n')
211                                         crash_component[strlen(crash_component)-1] = 0;
212                                 fclose(cf);
213                         }
214                 }
215         }
216
217         if (f)
218         {
219                 time_t t = time(0);
220                 char crashtime[STDBUFFER_SIZE];
221                 sprintf(crashtime, "%s",ctime(&t));
222                 if (strlen(crashtime) && crashtime[strlen(crashtime)-1] == '\n')
223                                 crashtime[strlen(crashtime)-1] = 0;
224                 fprintf(f, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n<opendreambox>\n");
225                 fprintf(f, "\t<enigma2>\n");
226                 fprintf(f, "\t\t<crashdate>%s</crashdate>\n", crashtime);
227 #ifdef ENIGMA2_CHECKOUT_TAG
228                 fprintf(f, "\t\t<checkouttag>" ENIGMA2_CHECKOUT_TAG "</checkouttag>\n");
229 #else
230                 fprintf(f, "\t\t<compiledate>" __DATE__ "</compiledate>\n");
231 #endif
232 #ifdef ENIGMA2_CHECKOUT_ROOT
233                 fprintf(f, "\t\t<checkoutroot>" ENIGMA2_CHECKOUT_ROOT "</checkoutroot>\n");
234 #endif
235                 fprintf(f, "\t\t<contactemail>%s</contactemail>\n", crash_emailaddr);
236                 fprintf(f, "\t\t<!-- Please email this crashlog to above address -->\n");
237                 std::string activeSkin = getConfigFileValue("config.skin.primary_skin");
238                 if (activeSkin != "Error")
239                 {
240                         if (activeSkin == "")
241                                 activeSkin = "Default Skin";
242                         fprintf(f, "\t\t<skin>%s</skin>\n", activeSkin.c_str());
243                 }
244                 fprintf(f, "\t</enigma2>\n");
245
246                 fprintf(f, "\t<image>\n");
247                 std::string model = getFileContent("/proc/stb/info/model");
248                 if (model != "Error")
249                 {
250                         char modelname[STDBUFFER_SIZE];
251                         sprintf(modelname, "%s",model.c_str());
252                         if (strlen(modelname) && modelname[strlen(modelname)-1] == '\n')
253                                 modelname[strlen(modelname)-1] = 0;
254                         fprintf(f, "\t\t<dreamboxmodel>%s</dreamboxmodel>\n", modelname);
255                 }
256                 std::string kernel = getFileContent("/proc/cmdline");
257                 if (kernel != "Error")
258                 {
259                         char kernelcmd[STDBUFFER_SIZE];
260                         sprintf(kernelcmd, "%s",kernel.c_str());
261                         if (strlen(kernelcmd) && kernelcmd[strlen(kernelcmd)-1] == '\n')
262                                 kernelcmd[strlen(kernelcmd)-1] = 0;
263                         fprintf(f, "\t\t<kernelcmdline>%s</kernelcmdline>\n", kernelcmd);
264                 }
265                 std::string sendAnonCrashlog = getConfigFileValue("config.plugins.crashlogautosubmit.sendAnonCrashlog");
266                 if (sendAnonCrashlog == "False" || sendAnonCrashlog == "false") // defaults to true... default anonymized crashlogs
267                 {
268                         std::string ca = getFileContent("/proc/stb/info/ca");
269                         if (ca != "Error")
270                         {
271                                 char dreamboxca[STDBUFFER_SIZE];
272                                 sprintf(dreamboxca, "%s",ca.c_str());
273                                 if (strlen(dreamboxca) && dreamboxca[strlen(dreamboxca)-1] == '\n')
274                                         dreamboxca[strlen(dreamboxca)-1] = 0;
275                                 fprintf(f, "\t\t<dreamboxca>\n\t\t<![CDATA[\n%s\n\t\t]]>\n\t\t</dreamboxca>\n", dreamboxca);
276                         }
277                         std::string settings = getFileContent("/etc/enigma2/settings");
278                         if (settings != "Error")
279                         {
280                                 fprintf(f, "\t\t<enigma2settings>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</enigma2settings>\n", settings.c_str());
281                         }
282                 }
283                 std::string addNetwork = getConfigFileValue("config.plugins.crashlogautosubmit.addNetwork");
284                 if (addNetwork == "True" || addNetwork == "true")
285                 {
286                         std::string nwinterfaces = getFileContent("/etc/network/interfaces");
287                         if (nwinterfaces != "Error")
288                         {
289                                 fprintf(f, "\t\t<networkinterfaces>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</networkinterfaces>\n", nwinterfaces.c_str());
290                         }
291                         std::string dns = getFileContent("/etc/resolv.conf");
292                         if (dns != "Error")
293                         {
294                                 fprintf(f, "\t\t<dns>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</dns>\n", dns.c_str());
295                         }
296                         std::string defaultgw = getFileContent("/etc/default_gw");
297                         if (defaultgw != "Error")
298                         {
299                                 char gateway[STDBUFFER_SIZE];
300                                 sprintf(gateway, "%s",defaultgw.c_str());
301                                 if (strlen(gateway) && gateway[strlen(gateway)-1] == '\n')
302                                         gateway[strlen(gateway)-1] = 0;
303                                 fprintf(f, "\t\t<defaultgateway>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</defaultgateway>\n", gateway);
304                         }
305                 }
306                 std::string addWlan = getConfigFileValue("config.plugins.crashlogautosubmit.addWlan");
307                 if (addWlan == "True" || addWlan == "true")
308                 {
309                         std::string wpasupplicant = getFileContent("/etc/wpa_supplicant.conf");
310                         if (wpasupplicant != "Error")
311                         {
312                                 fprintf(f, "\t\t<wpasupplicant>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</wpasupplicant>\n", wpasupplicant.c_str());
313                         }
314                 }
315                 std::string imageversion = getFileContent("/etc/image-version");
316                 if (imageversion != "Error")
317                 {
318                         fprintf(f, "\t\t<imageversion>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</imageversion>\n", imageversion.c_str());
319                 }
320                 std::string imageissue = getFileContent("/etc/issue.net");
321                 if (imageissue != "Error")
322                 {
323                         fprintf(f, "\t\t<imageissue>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</imageissue>\n", imageissue.c_str());
324                 }
325                 fprintf(f, "\t</image>\n");
326
327                 fprintf(f, "\t<software>\n");
328                 std::string installedplugins = execCommand("ipkg list_installed | grep enigma2");
329                 fprintf(f, "\t\t<enigma2software>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</enigma2software>\n", installedplugins.c_str());
330                 std::string dreambox = execCommand("ipkg list_installed | grep dream");
331                 fprintf(f, "\t\t<dreamboxsoftware>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</dreamboxsoftware>\n", dreambox.c_str());
332                 std::string gstreamer = execCommand("ipkg list_installed | grep gst");
333                 fprintf(f, "\t\t<gstreamersoftware>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</gstreamersoftware>\n", gstreamer.c_str());
334                 fprintf(f, "\t</software>\n");
335
336                 fprintf(f, "\t<crashlogs>\n");
337                 std::string buffer = getLogBuffer();
338                 fprintf(f, "\t\t<enigma2crashlog>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</enigma2crashlog>\n", buffer.c_str());
339                 std::string pythonmd5 = execCommand("find /usr/lib/enigma2/python/ -name \"*.py\" | xargs md5sum");
340                 fprintf(f, "\t\t<pythonMD5sum>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</pythonMD5sum>\n", pythonmd5.c_str());
341                 fprintf(f, "\t</crashlogs>\n");
342
343                 fprintf(f, "\n</opendreambox>\n");
344                 fclose(f);
345                 
346         }
347         
348 #ifdef WITH_SDL
349         ePtr<gSDLDC> my_dc;
350         gSDLDC::getInstance(my_dc);
351 #else
352         ePtr<gFBDC> my_dc;
353         gFBDC::getInstance(my_dc);
354 #endif
355         
356         {
357                 gPainter p(my_dc);
358                 p.resetOffset();
359                 p.resetClip(eRect(ePoint(0, 0), my_dc->size()));
360 #ifdef ENIGMA2_CHECKOUT_TAG
361                 if (ENIGMA2_CHECKOUT_TAG[0] == 'T') /* tagged checkout (release) */
362                         p.setBackgroundColor(gRGB(0x0000C0));
363                 else if (ENIGMA2_CHECKOUT_TAG[0] == 'D') /* dated checkout (daily experimental build) */
364                 {
365                         srand(time(0));
366                         int r = rand();
367                         unsigned int col = 0;
368                         if (r & 1)
369                                 col |= 0x800000;
370                         if (r & 2)
371                                 col |= 0x008000;
372                         if (r & 4)
373                                 col |= 0x0000c0;
374                         p.setBackgroundColor(gRGB(col));
375                 }
376 #else
377                         p.setBackgroundColor(gRGB(0x008000));
378 #endif
379
380                 p.setForegroundColor(gRGB(0xFFFFFF));
381         
382                 ePtr<gFont> font = new gFont("Regular", 20);
383                 p.setFont(font);
384                 p.clear();
385         
386                 eRect usable_area = eRect(100, 70, my_dc->size().width() - 150, 100);
387                 
388                 char text[512];
389                 snprintf(text, 512, "We are really sorry. Your Dreambox encountered "
390                         "a software problem, and needs to be restarted. "
391                         "Please send the logfile created in /hdd/ to %s.\n"
392                         "Your Dreambox restarts in 10 seconds!\n"
393                         "Component: %s",
394                         crash_emailaddr, crash_component);
395         
396                 p.renderText(usable_area, text, gPainter::RT_WRAP|gPainter::RT_HALIGN_LEFT);
397         
398                 usable_area = eRect(100, 170, my_dc->size().width() - 180, my_dc->size().height() - 20);
399         
400                 int i;
401         
402                 size_t start = std::string::npos + 1;
403                 for (i=0; i<20; ++i)
404                 {
405                         start = lines.rfind('\n', start - 1);
406                         if (start == std::string::npos)
407                         {
408                                 start = 0;
409                                 break;
410                         }
411                 }
412         
413                 font = new gFont("Regular", 14);
414                 p.setFont(font);
415         
416                 p.renderText(usable_area, 
417                         lines.substr(start), gPainter::RT_HALIGN_LEFT);
418                 sleep(10);
419         }
420
421         raise(SIGKILL);
422 }
423
424 #if defined(__MIPSEL__)
425 void oops(const mcontext_t &context, int dumpcode)
426 {
427         eDebug("PC: %08lx", (unsigned long)context.pc);
428         int i;
429         for (i=0; i<32; ++i)
430         {
431                 eDebugNoNewLine(" %08x", (int)context.gregs[i]);
432                 if ((i&3) == 3)
433                         eDebug("");
434         }
435                 /* this is temporary debug stuff. */
436         if (dumpcode && ((unsigned long)context.pc) > 0x10000) /* not a zero pointer */
437         {
438                 eDebug("As a final action, i will try to dump a bit of code.");
439                 eDebug("I just hope that this won't crash.");
440                 int i;
441                 eDebugNoNewLine("%08lx:", (unsigned long)context.pc);
442                 for (i=0; i<0x20; ++i)
443                         eDebugNoNewLine(" %02x", ((unsigned char*)context.pc)[i]);
444                 eDebug(" (end)");
445         }
446 }
447 #else
448 #warning "no oops support!"
449 #define NO_OOPS_SUPPORT
450 #endif
451
452 void handleFatalSignal(int signum, siginfo_t *si, void *ctx)
453 {
454         ucontext_t *uc = (ucontext_t*)ctx;
455
456 #ifndef NO_OOPS_SUPPORT
457         oops(uc->uc_mcontext, signum == SIGSEGV || signum == SIGABRT);
458 #endif
459         eDebug("-------");
460         bsodFatal("enigma2, signal");
461 }
462
463 void bsodCatchSignals()
464 {
465         struct sigaction act;
466         act.sa_handler = SIG_DFL;
467         act.sa_sigaction = handleFatalSignal;
468         act.sa_flags = SA_RESTART | SA_SIGINFO;
469         if (sigemptyset(&act.sa_mask) == -1)
470                 perror("sigemptyset");
471         
472                 /* start handling segfaults etc. */
473         sigaction(SIGSEGV, &act, 0);
474         sigaction(SIGILL, &act, 0);
475         sigaction(SIGBUS, &act, 0);
476         sigaction(SIGABRT, &act, 0);
477 }
478
479 void bsodLogInit()
480 {
481         logOutput.connect(addToLogbuffer);
482 }