enigma.cpp: add missing semicolon
[vuplus_dvbapp] / 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                 fprintf(f, "\t</enigma2>\n");
238
239                 fprintf(f, "\t<image>\n");
240                 std::string model = getFileContent("/proc/stb/info/model");
241                 if (model != "Error")
242                 {
243                         char modelname[STDBUFFER_SIZE];
244                         sprintf(modelname, "%s",model.c_str());
245                         if (strlen(modelname) && modelname[strlen(modelname)-1] == '\n')
246                                 modelname[strlen(modelname)-1] = 0;
247                         fprintf(f, "\t\t<dreamboxmodel>%s</dreamboxmodel>\n", modelname);
248                 }
249                 std::string kernel = getFileContent("/proc/cmdline");
250                 if (kernel != "Error")
251                 {
252                         char kernelcmd[STDBUFFER_SIZE];
253                         sprintf(kernelcmd, "%s",kernel.c_str());
254                         if (strlen(kernelcmd) && kernelcmd[strlen(kernelcmd)-1] == '\n')
255                                 kernelcmd[strlen(kernelcmd)-1] = 0;
256                         fprintf(f, "\t\t<kernelcmdline>%s</kernelcmdline>\n", kernelcmd);
257                 }
258                 std::string sendAnonCrashlog = getConfigFileValue("config.plugins.crashlogautosubmit.sendAnonCrashlog");
259                 if (sendAnonCrashlog == "False" || sendAnonCrashlog == "false") // defaults to true... default anonymized crashlogs
260                 {
261                         std::string ca = getFileContent("/proc/stb/info/ca");
262                         if (ca != "Error")
263                         {
264                                 char dreamboxca[STDBUFFER_SIZE];
265                                 sprintf(dreamboxca, "%s",ca.c_str());
266                                 if (strlen(dreamboxca) && dreamboxca[strlen(dreamboxca)-1] == '\n')
267                                         dreamboxca[strlen(dreamboxca)-1] = 0;
268                                 fprintf(f, "\t\t<dreamboxca>\n\t\t<![CDATA[\n%s\n\t\t]]>\n\t\t</dreamboxca>\n", dreamboxca);
269                         }
270                         std::string settings = getFileContent("/etc/enigma2/settings");
271                         if (settings != "Error")
272                         {
273                                 fprintf(f, "\t\t<enigma2settings>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</enigma2settings>\n", settings.c_str());
274                         }
275                 }
276                 std::string addNetwork = getConfigFileValue("config.plugins.crashlogautosubmit.addNetwork");
277                 if (addNetwork == "True" || addNetwork == "true")
278                 {
279                         std::string nwinterfaces = getFileContent("/etc/network/interfaces");
280                         if (nwinterfaces != "Error")
281                         {
282                                 fprintf(f, "\t\t<networkinterfaces>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</networkinterfaces>\n", nwinterfaces.c_str());
283                         }
284                         std::string dns = getFileContent("/etc/resolv.conf");
285                         if (dns != "Error")
286                         {
287                                 fprintf(f, "\t\t<dns>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</dns>\n", dns.c_str());
288                         }
289                         std::string defaultgw = getFileContent("/etc/default_gw");
290                         if (defaultgw != "Error")
291                         {
292                                 char gateway[STDBUFFER_SIZE];
293                                 sprintf(gateway, "%s",defaultgw.c_str());
294                                 if (strlen(gateway) && gateway[strlen(gateway)-1] == '\n')
295                                         gateway[strlen(gateway)-1] = 0;
296                                 fprintf(f, "\t\t<defaultgateway>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</defaultgateway>\n", gateway);
297                         }
298                 }
299                 std::string addWlan = getConfigFileValue("config.plugins.crashlogautosubmit.addWlan");
300                 if (addWlan == "True" || addWlan == "true")
301                 {
302                         std::string wpasupplicant = getFileContent("/etc/wpa_supplicant.conf");
303                         if (wpasupplicant != "Error")
304                         {
305                                 fprintf(f, "\t\t<wpasupplicant>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</wpasupplicant>\n", wpasupplicant.c_str());
306                         }
307                 }
308                 std::string imageversion = getFileContent("/etc/image-version");
309                 if (imageversion != "Error")
310                 {
311                         fprintf(f, "\t\t<imageversion>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</imageversion>\n", imageversion.c_str());
312                 }
313                 std::string imageissue = getFileContent("/etc/issue.net");
314                 if (imageissue != "Error")
315                 {
316                         fprintf(f, "\t\t<imageissue>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</imageissue>\n", imageissue.c_str());
317                 }
318                 fprintf(f, "\t</image>\n");
319
320                 fprintf(f, "\t<software>\n");
321                 std::string installedplugins = execCommand("ipkg list_installed | grep enigma2");
322                 fprintf(f, "\t\t<enigma2software>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</enigma2software>\n", installedplugins.c_str());
323                 std::string dreambox = execCommand("ipkg list_installed | grep dream");
324                 fprintf(f, "\t\t<dreamboxsoftware>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</dreamboxsoftware>\n", dreambox.c_str());
325                 std::string gstreamer = execCommand("ipkg list_installed | grep gst");
326                 fprintf(f, "\t\t<gstreamersoftware>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</gstreamersoftware>\n", gstreamer.c_str());
327                 fprintf(f, "\t</software>\n");
328
329                 fprintf(f, "\t<crashlogs>\n");
330                 std::string buffer = getLogBuffer();
331                 fprintf(f, "\t\t<enigma2crashlog>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</enigma2crashlog>\n", buffer.c_str());
332                 std::string pythonmd5 = execCommand("find /usr/lib/enigma2/python/ -name \"*.py\" | xargs md5sum");
333                 fprintf(f, "\t\t<pythonMD5sum>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</pythonMD5sum>\n", pythonmd5.c_str());
334                 fprintf(f, "\t</crashlogs>\n");
335
336                 fprintf(f, "\n</opendreambox>\n");
337                 fclose(f);
338                 
339         }
340         
341 #ifdef WITH_SDL
342         ePtr<gSDLDC> my_dc;
343         gSDLDC::getInstance(my_dc);
344 #else
345         ePtr<gFBDC> my_dc;
346         gFBDC::getInstance(my_dc);
347 #endif
348         
349         {
350                 gPainter p(my_dc);
351                 p.resetOffset();
352                 p.resetClip(eRect(ePoint(0, 0), my_dc->size()));
353 #ifdef ENIGMA2_CHECKOUT_TAG
354                 if (ENIGMA2_CHECKOUT_TAG[0] == 'T') /* tagged checkout (release) */
355                         p.setBackgroundColor(gRGB(0x0000C0));
356                 else if (ENIGMA2_CHECKOUT_TAG[0] == 'D') /* dated checkout (daily experimental build) */
357                 {
358                         srand(time(0));
359                         int r = rand();
360                         unsigned int col = 0;
361                         if (r & 1)
362                                 col |= 0x800000;
363                         if (r & 2)
364                                 col |= 0x008000;
365                         if (r & 4)
366                                 col |= 0x0000c0;
367                         p.setBackgroundColor(gRGB(col));
368                 }
369 #else
370                         p.setBackgroundColor(gRGB(0x008000));
371 #endif
372
373                 p.setForegroundColor(gRGB(0xFFFFFF));
374         
375                 ePtr<gFont> font = new gFont("Regular", 20);
376                 p.setFont(font);
377                 p.clear();
378         
379                 eRect usable_area = eRect(100, 70, my_dc->size().width() - 150, 100);
380                 
381                 char text[512];
382                 snprintf(text, 512, "We are really sorry. Your Dreambox encountered "
383                         "a software problem, and needs to be restarted. "
384                         "Please send the logfile created in /hdd/ to %s.\n"
385                         "Your Dreambox restarts in 10 seconds!\n"
386                         "Component: %s",
387                         crash_emailaddr, crash_component);
388         
389                 p.renderText(usable_area, text, gPainter::RT_WRAP|gPainter::RT_HALIGN_LEFT);
390         
391                 usable_area = eRect(100, 170, my_dc->size().width() - 180, my_dc->size().height() - 20);
392         
393                 int i;
394         
395                 size_t start = std::string::npos + 1;
396                 for (i=0; i<20; ++i)
397                 {
398                         start = lines.rfind('\n', start - 1);
399                         if (start == std::string::npos)
400                         {
401                                 start = 0;
402                                 break;
403                         }
404                 }
405         
406                 font = new gFont("Regular", 14);
407                 p.setFont(font);
408         
409                 p.renderText(usable_area, 
410                         lines.substr(start), gPainter::RT_HALIGN_LEFT);
411                 sleep(10);
412         }
413
414         raise(SIGKILL);
415 }
416
417 #if defined(__MIPSEL__)
418 void oops(const mcontext_t &context, int dumpcode)
419 {
420         eDebug("PC: %08lx", (unsigned long)context.pc);
421         int i;
422         for (i=0; i<32; ++i)
423         {
424                 eDebugNoNewLine(" %08x", (int)context.gregs[i]);
425                 if ((i&3) == 3)
426                         eDebug("");
427         }
428                 /* this is temporary debug stuff. */
429         if (dumpcode && ((unsigned long)context.pc) > 0x10000) /* not a zero pointer */
430         {
431                 eDebug("As a final action, i will try to dump a bit of code.");
432                 eDebug("I just hope that this won't crash.");
433                 int i;
434                 eDebugNoNewLine("%08lx:", (unsigned long)context.pc);
435                 for (i=0; i<0x20; ++i)
436                         eDebugNoNewLine(" %02x", ((unsigned char*)context.pc)[i]);
437                 eDebug(" (end)");
438         }
439 }
440 #else
441 #warning "no oops support!"
442 #define NO_OOPS_SUPPORT
443 #endif
444
445 void handleFatalSignal(int signum, siginfo_t *si, void *ctx)
446 {
447         ucontext_t *uc = (ucontext_t*)ctx;
448
449 #ifndef NO_OOPS_SUPPORT
450         oops(uc->uc_mcontext, signum == SIGSEGV || signum == SIGABRT);
451 #endif
452         eDebug("-------");
453         bsodFatal("enigma2, signal");
454 }
455
456 void bsodCatchSignals()
457 {
458         struct sigaction act;
459         act.sa_handler = SIG_DFL;
460         act.sa_sigaction = handleFatalSignal;
461         act.sa_flags = SA_RESTART | SA_SIGINFO;
462         if (sigemptyset(&act.sa_mask) == -1)
463                 perror("sigemptyset");
464         
465                 /* start handling segfaults etc. */
466         sigaction(SIGSEGV, &act, 0);
467         sigaction(SIGILL, &act, 0);
468         sigaction(SIGBUS, &act, 0);
469         sigaction(SIGABRT, &act, 0);
470 }
471
472 void bsodLogInit()
473 {
474         logOutput.connect(addToLogbuffer);
475 }