• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • util
kcrash.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the KDE Libraries
3  * Copyright (C) 2000 Timo Hummel <timo.hummel@sap.com>
4  * Tom Braun <braunt@fh-konstanz.de>
5  * Copyright 2009 KDE e.V.
6  * By Adriaan de Groot <groot@kde.org>
7  * Copyright (C) 2010 George Kiagiadakis <kiagiadakis.george@gmail.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB. If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 
25 #include "kcrash.h"
26 #include <kcmdlineargs.h>
27 #include <kstandarddirs.h>
28 #include <config-kstandarddirs.h>
29 
30 #include <config.h>
31 
32 #include <string.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 #include <sys/wait.h>
42 #include <sys/un.h>
43 #include <sys/socket.h>
44 #ifdef Q_OS_LINUX
45 #include <sys/prctl.h>
46 #endif
47 #include <errno.h>
48 
49 #include <qwindowdefs.h>
50 #include <kglobal.h>
51 #include <kcomponentdata.h>
52 #include <kaboutdata.h>
53 #include <kdebug.h>
54 #include <kapplication.h>
55 
56 #include <../kinit/klauncher_cmds.h>
57 
58 #include <QtCore/QFileInfo>
59 #include <QtCore/QDir>
60 
61 #if defined Q_WS_X11
62 #include <qx11info_x11.h>
63 #include <X11/Xlib.h>
64 #endif
65 
66 #ifdef Q_OS_SOLARIS
67 // Solaris has built-in, thread-safe, async-signal-safe, mechanisms
68 // to walk the stack in the case of a crash, as well as (optionally)
69 // to demangle C++ symbol names. In the case of a crash, dump a stack
70 // trace to stderr before starting drKonqui (because what drKonqui is
71 // going to do is -- through a complicated process -- print the
72 // exact same information, but less reliably).
73 #include <ucontext.h>
74 #endif
75 
76 #if defined(Q_OS_WIN)
77 # include <windows.h>
78 #endif
79 
80 static KCrash::HandlerType s_emergencySaveFunction = 0;
81 static KCrash::HandlerType s_crashHandler = 0;
82 static char *s_appName = 0;
83 static char *s_autoRestartCommand = 0;
84 static char *s_appPath = 0;
85 static int s_autoRestartArgc = 0;
86 static char **s_autoRestartCommandLine = 0;
87 static char *s_drkonqiPath = 0;
88 static KCrash::CrashFlags s_flags = 0;
89 static bool s_launchDrKonqi = false;
90 
91 namespace KCrash
92 {
93  void startProcess(int argc, const char *argv[], bool waitAndExit);
94 
95 #if defined(Q_OS_WIN)
96  LONG WINAPI win32UnhandledExceptionFilter(_EXCEPTION_POINTERS *exceptionInfo);
97 #endif
98 }
99 
100 void
101 KCrash::setEmergencySaveFunction (HandlerType saveFunction)
102 {
103  s_emergencySaveFunction = saveFunction;
104 
105  /*
106  * We need at least the default crash handler for
107  * emergencySaveFunction to be called
108  */
109  if (s_emergencySaveFunction && !s_crashHandler) {
110  setCrashHandler(defaultCrashHandler);
111  }
112 }
113 
114 KCrash::HandlerType
115 KCrash::emergencySaveFunction()
116 {
117  return s_emergencySaveFunction;
118 }
119 
120 // Set the default crash handler in 10 seconds
121 // This is used after an autorestart, the second instance of the application
122 // is started with --nocrashhandler (no drkonqi, more precisely), and we
123 // set the defaultCrashHandler (to handle autorestart) after 10s.
124 // The delay is to see if we stay up for more than 10s time, to avoid infinite
125 // respawning if the app crashes on startup.
126 class KCrashDelaySetHandler : public QObject
127 {
128 public:
129  KCrashDelaySetHandler() {
130  startTimer(10000); // 10 s
131  }
132 protected:
133  void timerEvent(QTimerEvent *event) {
134  if (!s_crashHandler) // not set meanwhile
135  KCrash::setCrashHandler(KCrash::defaultCrashHandler);
136  killTimer(event->timerId());
137  this->deleteLater();
138  }
139 };
140 
141 
142 
143 void
144 KCrash::setFlags(KCrash::CrashFlags flags)
145 {
146  s_flags = flags;
147  if (s_flags & AutoRestart) {
148  // We need at least the default crash handler for autorestart to work.
149  if (!s_crashHandler) {
150  KCmdLineArgs *args = KCmdLineArgs::parsedArgs("kde");
151  if (!args->isSet("crashhandler")) // --nocrashhandler was passed, probably due to a crash, delay restart handler
152  new KCrashDelaySetHandler;
153  else // probably because KDE_DEBUG=1. set restart handler immediately.
154  setCrashHandler(defaultCrashHandler);
155  }
156  }
157 }
158 
159 //### KDE5:Consider merging the setApplicationPath and setApplicationName methods into one.
160 void
161 KCrash::setApplicationPath(const QString& path)
162 {
163  s_appPath = qstrdup(QFile::encodeName(path).constData());
164 
165  //if the appName has also been specified, update s_autoRestartCommand to be in the form "absolutePath/appName"
166  if (s_appName) {
167  delete[] s_autoRestartCommand;
168  QFileInfo appExecutable(QDir(path), QFile::decodeName(s_appName));
169  QByteArray cmd = QFile::encodeName(appExecutable.absoluteFilePath());
170  s_autoRestartCommand = qstrdup(cmd.constData());
171  }
172 
173  QStringList args = KCmdLineArgs::allArguments();
174  args[0] = s_autoRestartCommand; // replace argv[0] with full path above
175  if (!args.contains("--nocrashhandler"))
176  args.insert(1, "--nocrashhandler");
177  delete[] s_autoRestartCommandLine;
178  s_autoRestartArgc = args.count();
179  s_autoRestartCommandLine = new char* [args.count() + 1];
180  for (int i = 0; i < args.count(); ++i) {
181  s_autoRestartCommandLine[i] = qstrdup(QFile::encodeName(args.at(i)).constData());
182  }
183  s_autoRestartCommandLine[args.count()] = 0;
184 }
185 
186 void
187 KCrash::setApplicationName(const QString& name)
188 {
189  s_appName = qstrdup(QFile::encodeName(name).constData());
190 
191  //update the autoRestartCommand
192  delete[] s_autoRestartCommand;
193  if (s_appPath) {
194  //if we have appPath, make autoRestartCommand be in the form "absolutePath/appName"...
195  QFileInfo appExecutable(QDir(QFile::decodeName(s_appPath)), name);
196  QByteArray cmd = QFile::encodeName(appExecutable.absoluteFilePath());
197  s_autoRestartCommand = qstrdup(cmd.constData());
198  } else {
199  //...else just use the appName for the autoRestartCommand
200  s_autoRestartCommand = qstrdup(s_appName);
201  }
202 }
203 
204 void KCrash::setDrKonqiEnabled(bool enabled)
205 {
206  s_launchDrKonqi = enabled;
207  if (s_launchDrKonqi && !s_drkonqiPath) {
208  s_drkonqiPath = qstrdup(QFile::encodeName(KStandardDirs::findExe("drkonqi")).constData());
209  if (!s_drkonqiPath) {
210  kError() << "Could not find drkonqi";
211  s_launchDrKonqi = false;
212  }
213  }
214 
215  //we need at least the default crash handler to launch drkonqi
216  if (s_launchDrKonqi && !s_crashHandler) {
217  setCrashHandler(defaultCrashHandler);
218  }
219 }
220 
221 bool KCrash::isDrKonqiEnabled()
222 {
223  return s_launchDrKonqi;
224 }
225 
226 void
227 KCrash::setCrashHandler (HandlerType handler)
228 {
229 #if defined(Q_OS_WIN)
230  static LPTOP_LEVEL_EXCEPTION_FILTER s_previousExceptionFilter = NULL;
231 
232  if (handler && !s_previousExceptionFilter) {
233  s_previousExceptionFilter = SetUnhandledExceptionFilter(KCrash::win32UnhandledExceptionFilter);
234  } else if (!handler && s_previousExceptionFilter) {
235  SetUnhandledExceptionFilter(s_previousExceptionFilter);
236  s_previousExceptionFilter = NULL;
237  }
238 #else
239  if (!handler)
240  handler = SIG_DFL;
241 
242  sigset_t mask;
243  sigemptyset(&mask);
244 
245 #ifdef SIGSEGV
246  signal (SIGSEGV, handler);
247  sigaddset(&mask, SIGSEGV);
248 #endif
249 #ifdef SIGBUS
250  signal (SIGBUS, handler);
251  sigaddset(&mask, SIGBUS);
252 #endif
253 #ifdef SIGFPE
254  signal (SIGFPE, handler);
255  sigaddset(&mask, SIGFPE);
256 #endif
257 #ifdef SIGILL
258  signal (SIGILL, handler);
259  sigaddset(&mask, SIGILL);
260 #endif
261 #ifdef SIGABRT
262  signal (SIGABRT, handler);
263  sigaddset(&mask, SIGABRT);
264 #endif
265 
266  sigprocmask(SIG_UNBLOCK, &mask, 0);
267 #endif
268 
269  s_crashHandler = handler;
270 }
271 
272 KCrash::HandlerType
273 KCrash::crashHandler()
274 {
275  return s_crashHandler;
276 }
277 
278 static void
279 closeAllFDs()
280 {
281  // Close all remaining file descriptors except for stdin/stdout/stderr
282  struct rlimit rlp;
283  getrlimit(RLIMIT_NOFILE, &rlp);
284  for (int i = 3; i < (int)rlp.rlim_cur; i++)
285  close(i);
286 }
287 
288 void
289 KCrash::defaultCrashHandler (int sig)
290 {
291  // WABA: Do NOT use kDebug() in this function because it is much too risky!
292  // Handle possible recursions
293  static int crashRecursionCounter = 0;
294  crashRecursionCounter++; // Nothing before this, please !
295 
296 #if !defined(Q_OS_WIN)
297  signal(SIGALRM, SIG_DFL);
298  alarm(3); // Kill me... (in case we deadlock in malloc)
299 #endif
300 
301 #ifdef Q_OS_SOLARIS
302  (void) printstack(2 /* stderr, assuming it's still open. */);
303 #endif
304 
305  if (crashRecursionCounter < 2) {
306  if (s_emergencySaveFunction) {
307  s_emergencySaveFunction (sig);
308  }
309  if ((s_flags & AutoRestart) && s_autoRestartCommand) {
310  sleep(1);
311  startProcess(s_autoRestartArgc, const_cast<const char**>(s_autoRestartCommandLine), false);
312  }
313  crashRecursionCounter++;
314  }
315 
316 #if !defined(Q_OS_WIN)
317  if (!(s_flags & KeepFDs))
318  closeAllFDs();
319 # if defined(Q_WS_X11)
320  else if (QX11Info::display())
321  close(ConnectionNumber(QX11Info::display()));
322 # endif
323 #endif
324 
325  if (crashRecursionCounter < 3)
326  {
327 #ifndef NDEBUG
328  fprintf(stderr, "KCrash: crashing... crashRecursionCounter = %d\n",
329  crashRecursionCounter);
330  fprintf(stderr, "KCrash: Application Name = %s path = %s pid = %lld\n",
331  s_appName ? s_appName : "<unknown>" ,
332  s_appPath ? s_appPath : "<unknown>", QCoreApplication::applicationPid());
333  fprintf(stderr, "KCrash: Arguments: ");
334  for (int i = 0; s_autoRestartCommandLine[i]; ++i) {
335  fprintf(stderr, "%s ", s_autoRestartCommandLine[i]);
336  }
337  fprintf(stderr, "\n");
338 #else
339  fprintf(stderr, "KCrash: Application '%s' crashing...\n",
340  s_appName ? s_appName : "<unknown>");
341 #endif
342 
343  if (!s_launchDrKonqi) {
344  setCrashHandler(0);
345 #if !defined(Q_OS_WIN)
346  raise(sig); // dump core, or whatever is the default action for this signal.
347 #endif
348  return;
349  }
350 
351  const char * argv[27]; // don't forget to update this
352  int i = 0;
353 
354  // argument 0 has to be drkonqi
355  argv[i++] = s_drkonqiPath;
356 
357 #if defined Q_WS_X11
358  // start up on the correct display
359  argv[i++] = "-display";
360  if ( QX11Info::display() )
361  argv[i++] = XDisplayString(QX11Info::display());
362  else
363  argv[i++] = getenv("DISPLAY");
364 #elif defined(Q_WS_QWS)
365  // start up on the correct display
366  argv[i++] = "-display";
367  argv[i++] = getenv("QWS_DISPLAY");
368 #endif
369 
370  argv[i++] = "--appname";
371  argv[i++] = s_appName ? s_appName : "<unknown>";
372 
373  if (KApplication::loadedByKdeinit)
374  argv[i++] = "--kdeinit";
375 
376  // only add apppath if it's not NULL
377  if (s_appPath && *s_appPath) {
378  argv[i++] = "--apppath";
379  argv[i++] = s_appPath;
380  }
381 
382  // signal number -- will never be NULL
383  char sigtxt[ 10 ];
384  sprintf( sigtxt, "%d", sig );
385  argv[i++] = "--signal";
386  argv[i++] = sigtxt;
387 
388  char pidtxt[ 20 ];
389  sprintf( pidtxt, "%lld", QCoreApplication::applicationPid());
390  argv[i++] = "--pid";
391  argv[i++] = pidtxt;
392 
393  const KComponentData componentData = KGlobal::mainComponent();
394  const KAboutData *about = componentData.isValid() ? componentData.aboutData() : 0;
395  if (about) {
396  if (about->internalVersion()) {
397  argv[i++] = "--appversion";
398  argv[i++] = about->internalVersion();
399  }
400 
401  if (about->internalProgramName()) {
402  argv[i++] = "--programname";
403  argv[i++] = about->internalProgramName();
404  }
405 
406  if (about->internalBugAddress()) {
407  argv[i++] = "--bugaddress";
408  argv[i++] = about->internalBugAddress();
409  }
410  }
411 
412  char sidtxt[256];
413  if ( kapp && !kapp->startupId().isNull()) {
414  argv[i++] = "--startupid";
415  strlcpy(sidtxt, kapp->startupId().constData(), sizeof(sidtxt));
416  argv[i++] = sidtxt;
417  }
418 
419  if ( s_flags & SaferDialog )
420  argv[i++] = "--safer";
421 
422  if ((s_flags & AutoRestart) && s_autoRestartCommand)
423  argv[i++] = "--restarted"; //tell drkonqi if the app has been restarted
424 
425 #if defined(Q_OS_WIN)
426  char threadId[8] = { 0 };
427  sprintf( threadId, "%d", GetCurrentThreadId() );
428  argv[i++] = "--thread";
429  argv[i++] = threadId;
430 #endif
431 
432  // NULL terminated list
433  argv[i] = NULL;
434 
435  startProcess(i, argv, true);
436  }
437 
438  if (crashRecursionCounter < 4)
439  {
440  fprintf(stderr, "Unable to start Dr. Konqi\n");
441  }
442 
443  _exit(255);
444 }
445 
446 #if defined(Q_OS_WIN)
447 
448 void KCrash::startProcess(int argc, const char *argv[], bool waitAndExit)
449 {
450  QString cmdLine;
451  for(int i=0; i<argc; ++i) {
452  cmdLine.append('\"');
453  cmdLine.append(QFile::decodeName(argv[i]));
454  cmdLine.append("\" ");
455  }
456 
457  PROCESS_INFORMATION procInfo;
458  STARTUPINFOW startupInfo = { sizeof( STARTUPINFO ), 0, 0, 0,
459  (ulong)CW_USEDEFAULT, (ulong)CW_USEDEFAULT,
460  (ulong)CW_USEDEFAULT, (ulong)CW_USEDEFAULT,
461  0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
462 
463  bool success = CreateProcess(0, (wchar_t*) cmdLine.utf16(), NULL, NULL,
464  false, CREATE_UNICODE_ENVIRONMENT, NULL, NULL,
465  &startupInfo, &procInfo);
466 
467  if (success && waitAndExit) {
468  // wait for child to exit
469  WaitForSingleObject(procInfo.hProcess, INFINITE);
470  _exit(253);
471  }
472 }
473 
474 //glue function for calling the unix signal handler from the windows unhandled exception filter
475 LONG WINAPI KCrash::win32UnhandledExceptionFilter(_EXCEPTION_POINTERS *exceptionInfo)
476 {
477  // kdbgwin needs the context inside exceptionInfo because if getting the context after the
478  // exception happened, it will walk down the stack and will stop at KiUserEventDispatch in
479  // ntdll.dll, which is supposed to dispatch the exception from kernel mode back to user mode
480  // so... let's create some shared memory
481  HANDLE hMapFile = NULL;
482  hMapFile = CreateFileMapping(
483  INVALID_HANDLE_VALUE,
484  NULL,
485  PAGE_READWRITE,
486  0,
487  sizeof(CONTEXT),
488  TEXT("Local\\KCrashShared"));
489 
490  LPCTSTR pBuf = NULL;
491  pBuf = (LPCTSTR) MapViewOfFile(
492  hMapFile,
493  FILE_MAP_ALL_ACCESS,
494  0,
495  0,
496  sizeof(CONTEXT));
497  CopyMemory((PVOID) pBuf, exceptionInfo->ContextRecord, sizeof(CONTEXT));
498 
499  if (s_crashHandler) {
500  s_crashHandler(exceptionInfo->ExceptionRecord->ExceptionCode);
501  }
502 
503  CloseHandle(hMapFile);
504  return EXCEPTION_EXECUTE_HANDLER; //allow windows to do the default action (terminate)
505 }
506 #else
507 
508 static bool startProcessInternal(int argc, const char *argv[], bool waitAndExit, bool directly);
509 static pid_t startFromKdeinit(int argc, const char *argv[]);
510 static pid_t startDirectly(const char *argv[]);
511 static int write_socket(int sock, char *buffer, int len);
512 static int read_socket(int sock, char *buffer, int len);
513 static int openSocket();
514 
515 void KCrash::startProcess(int argc, const char *argv[], bool waitAndExit)
516 {
517  bool startDirectly = true;
518 
519  // First try to start the app via kdeinit, if the AlwaysDirectly flag hasn't been specified.
520  // This is done because it is dangerous to use fork() in the crash handler
521  // (there can be functions registered to be performed before fork(), for example handling
522  // of malloc locking, which doesn't work when malloc crashes because of heap corruption).
523  if (!(s_flags & AlwaysDirectly)) {
524  startDirectly = !startProcessInternal(argc, argv, waitAndExit, false);
525  }
526 
527  // If we can't reach kdeinit, we can still at least try to fork()
528  if (startDirectly) {
529  startProcessInternal(argc, argv, waitAndExit, true);
530  }
531 }
532 
533 static bool startProcessInternal(int argc, const char *argv[], bool waitAndExit, bool directly)
534 {
535  fprintf(stderr, "KCrash: Attempting to start %s %s\n", argv[0], directly ? "directly" : "from kdeinit");
536 
537  pid_t pid = directly ? startDirectly(argv) : startFromKdeinit(argc, argv);
538 
539  if (pid > 0 && waitAndExit) {
540  // Seems we made it....
541  alarm(0); //stop the pending alarm that was set at the top of the defaultCrashHandler
542 
543  // Wait forever until the started process exits. This code path is executed
544  // when launching drkonqi. Note that drkonqi will stop this process in the meantime.
545  if (directly) {
546  //if the process was started directly, use waitpid(), as it's a child...
547  while(waitpid(-1, NULL, 0) != pid) {}
548  } else {
549 #ifdef Q_OS_LINUX
550  // Declare the process that will be debugging the crashed KDE app (#245529)
551 #ifndef PR_SET_PTRACER
552 # define PR_SET_PTRACER 0x59616d61
553 #endif
554  prctl(PR_SET_PTRACER, pid, 0, 0, 0);
555 #endif
556  //...else poll its status using kill()
557  while(kill(pid, 0) >= 0) {
558  sleep(1);
559  }
560  }
561  _exit(253);
562  }
563 
564  return (pid > 0); //return true on success
565 }
566 
567 static pid_t startFromKdeinit(int argc, const char *argv[])
568 {
569  int socket = openSocket();
570  if( socket < -1 )
571  return 0;
572  klauncher_header header;
573  header.cmd = LAUNCHER_EXEC_NEW;
574  const int BUFSIZE = 8192; // make sure this is big enough
575  char buffer[ BUFSIZE + 10 ];
576  int pos = 0;
577  long argcl = argc;
578  memcpy( buffer + pos, &argcl, sizeof( argcl ));
579  pos += sizeof( argcl );
580  for( int i = 0;
581  i < argc;
582  ++i )
583  {
584  int len = strlen( argv[ i ] ) + 1; // include terminating \0
585  if( pos + len >= BUFSIZE )
586  {
587  fprintf( stderr, "BUFSIZE in KCrash not big enough!\n" );
588  return 0;
589  }
590  memcpy( buffer + pos, argv[ i ], len );
591  pos += len;
592  }
593  long env = 0;
594  memcpy( buffer + pos, &env, sizeof( env ));
595  pos += sizeof( env );
596  long avoid_loops = 0;
597  memcpy( buffer + pos, &avoid_loops, sizeof( avoid_loops ));
598  pos += sizeof( avoid_loops );
599  header.arg_length = pos;
600  write_socket(socket, (char *) &header, sizeof(header));
601  write_socket(socket, buffer, pos);
602  if( read_socket( socket, (char *) &header, sizeof(header)) < 0
603  || header.cmd != LAUNCHER_OK )
604  {
605  return 0;
606  }
607  long pid;
608  read_socket(socket, (char *) &pid, sizeof(pid));
609  return static_cast<pid_t>(pid);
610 }
611 
612 static pid_t startDirectly(const char *argv[])
613 {
614  pid_t pid = fork();
615  switch (pid)
616  {
617  case -1:
618  fprintf( stderr, "KCrash failed to fork(), errno = %d\n", errno );
619  return 0;
620  case 0:
621  if (setgid(getgid()) < 0 || setuid(getuid()) < 0)
622  _exit(253); // This cannot happen. Theoretically.
623  closeAllFDs(); // We are in the child now. Close FDs unconditionally.
624  execvp(argv[0], const_cast< char** >(argv));
625  fprintf( stderr, "KCrash failed to exec(), errno = %d\n", errno );
626  _exit(253);
627  default:
628  return pid;
629  }
630 }
631 
632 // From now on this code is copy&pasted from kinit/wrapper.c :
633 
634 static char *getDisplay()
635 {
636  const char *display;
637  char *result;
638  char *screen;
639  char *colon;
640  char *i;
641 /*
642  don't test for a value from qglobal.h but instead distinguish
643  Qt/X11 from Qt/Embedded by the fact that Qt/E apps have -DQWS
644  on the commandline (which in qglobal.h however triggers Q_WS_QWS,
645  but we don't want to include that here) (Simon)
646 #ifdef Q_WS_X11
647  */
648 #ifdef NO_DISPLAY
649  display = "NODISPLAY";
650 #elif !defined(QWS)
651  display = getenv("DISPLAY");
652 #else
653  display = getenv("QWS_DISPLAY");
654 #endif
655  if (!display || !*display)
656  {
657  display = ":0";
658  }
659  result = (char*)malloc(strlen(display)+1);
660  if (result == NULL)
661  return NULL;
662 
663  strcpy(result, display);
664  screen = strrchr(result, '.');
665  colon = strrchr(result, ':');
666  if (screen && (screen > colon))
667  *screen = '\0';
668  while((i = strchr(result, ':')))
669  *i = '_';
670 #ifdef __APPLE__
671  while((i = strchr(result, '/')))
672  *i = '_';
673 #endif
674  return result;
675 }
676 
677 /*
678  * Write 'len' bytes from 'buffer' into 'sock'.
679  * returns 0 on success, -1 on failure.
680  */
681 static int write_socket(int sock, char *buffer, int len)
682 {
683  ssize_t result;
684  int bytes_left = len;
685  while ( bytes_left > 0)
686  {
687  result = write(sock, buffer, bytes_left);
688  if (result > 0)
689  {
690  buffer += result;
691  bytes_left -= result;
692  }
693  else if (result == 0)
694  return -1;
695  else if ((result == -1) && (errno != EINTR) && (errno != EAGAIN))
696  return -1;
697  }
698  return 0;
699 }
700 
701 /*
702  * Read 'len' bytes from 'sock' into 'buffer'.
703  * returns 0 on success, -1 on failure.
704  */
705 static int read_socket(int sock, char *buffer, int len)
706 {
707  ssize_t result;
708  int bytes_left = len;
709  while ( bytes_left > 0)
710  {
711  result = read(sock, buffer, bytes_left);
712  if (result > 0)
713  {
714  buffer += result;
715  bytes_left -= result;
716  }
717  else if (result == 0)
718  return -1;
719  else if ((result == -1) && (errno != EINTR) && (errno != EAGAIN))
720  return -1;
721  }
722  return 0;
723 }
724 
725 static int openSocket()
726 {
727  kde_socklen_t socklen;
728  int s;
729  struct sockaddr_un server;
730 #define MAX_SOCK_FILE 255
731  char sock_file[MAX_SOCK_FILE + 1];
732  const char *home_dir = getenv("HOME");
733  const char *kde_home = getenv("KDEHOME");
734  char *display;
735 
736  sock_file[0] = sock_file[MAX_SOCK_FILE] = 0;
737 
738  if (!kde_home || !kde_home[0])
739  {
740  kde_home = "~/" KDE_DEFAULT_HOME "/";
741  }
742 
743  if (kde_home[0] == '~')
744  {
745  if (!home_dir || !home_dir[0])
746  {
747  fprintf(stderr, "Warning: $HOME not set!\n");
748  return -1;
749  }
750  if (strlen(home_dir) > (MAX_SOCK_FILE-100))
751  {
752  fprintf(stderr, "Warning: Home directory path too long!\n");
753  return -1;
754  }
755  kde_home++;
756  strlcpy(sock_file, home_dir, MAX_SOCK_FILE);
757  }
758  strlcat(sock_file, kde_home, MAX_SOCK_FILE);
759 
761  if ( sock_file[strlen(sock_file)-1] == '/')
762  sock_file[strlen(sock_file)-1] = 0;
763 
764  strlcat(sock_file, "/socket-", MAX_SOCK_FILE);
765  if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
766  {
767  perror("Warning: Could not determine hostname: ");
768  return -1;
769  }
770  sock_file[sizeof(sock_file)-1] = '\0';
771 
772  /* append $DISPLAY */
773  display = getDisplay();
774  if (display == NULL)
775  {
776  fprintf(stderr, "Error: Could not determine display.\n");
777  return -1;
778  }
779 
780  if (strlen(sock_file)+strlen(display)+strlen("/kdeinit4_")+2 > MAX_SOCK_FILE)
781  {
782  fprintf(stderr, "Warning: Socket name will be too long.\n");
783  free(display);
784  return -1;
785  }
786  strcat(sock_file, "/kdeinit4_");
787  strcat(sock_file, display);
788  free(display);
789 
790  if (strlen(sock_file) >= sizeof(server.sun_path))
791  {
792  fprintf(stderr, "Warning: Path of socketfile exceeds UNIX_PATH_MAX.\n");
793  return -1;
794  }
795 
796  /*
797  * create the socket stream
798  */
799  s = socket(PF_UNIX, SOCK_STREAM, 0);
800  if (s < 0)
801  {
802  perror("Warning: socket() failed: ");
803  return -1;
804  }
805 
806  server.sun_family = AF_UNIX;
807  strcpy(server.sun_path, sock_file);
808  printf("sock_file=%s\n", sock_file);
809  socklen = sizeof(server);
810  if(connect(s, (struct sockaddr *)&server, socklen) == -1)
811  {
812  perror("Warning: connect() failed: ");
813  close(s);
814  return -1;
815  }
816  return s;
817 }
818 
819 #endif // Q_OS_UNIX
closeAllFDs
static void closeAllFDs()
Definition: kcrash.cpp:279
s_appName
static char * s_appName
Definition: kcrash.cpp:82
kapp
#define kapp
Definition: kapplication.h:56
KAboutData::internalVersion
const char * internalVersion() const
KCmdLineArgs::allArguments
static QStringList allArguments()
header
const char header[]
perror
QDebug perror(QDebug s, KDebugTag)
KCrash::HandlerType
void(* HandlerType)(int)
Typedef for a pointer to a crash handler function.
Definition: kcrash.h:63
kdebug.h
kapplication.h
mask
#define mask
KCmdLineArgs::parsedArgs
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
KCmdLineArgs
KCrash::startProcess
void startProcess(int argc, const char *argv[], bool waitAndExit)
Definition: kcrash.cpp:448
KAboutData::internalBugAddress
const char * internalBugAddress() const
KStandardAction::name
const char * name(StandardAction id)
This will return the internal name of a given standard action.
Definition: kstandardaction.cpp:223
KCrash::defaultCrashHandler
void defaultCrashHandler(int signal)
The default crash handler.
Definition: kcrash.cpp:289
KCrash::setApplicationName
void setApplicationName(const QString &name)
Sets the application name which should be passed to DrKonqi, our nice crash display application...
Definition: kcrash.cpp:187
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KComponentData::aboutData
const KAboutData * aboutData() const
QString
s_autoRestartCommandLine
static char ** s_autoRestartCommandLine
Definition: kcrash.cpp:86
QObject
s_drkonqiPath
static char * s_drkonqiPath
Definition: kcrash.cpp:87
KCmdLineArgs::isSet
bool isSet(const QByteArray &option) const
kglobal.h
s_autoRestartArgc
static int s_autoRestartArgc
Definition: kcrash.cpp:85
KCrash::setApplicationPath
void setApplicationPath(const QString &path)
Sets the application path which should be passed to DrKonqi, our nice crash display application...
Definition: kcrash.cpp:161
s_crashHandler
static KCrash::HandlerType s_crashHandler
Definition: kcrash.cpp:81
KCrash::setEmergencySaveFunction
void setEmergencySaveFunction(HandlerType saveFunction=0)
Installs a function which should try to save the application's data.
Definition: kcrash.cpp:101
kcmdlineargs.h
QStringList
KCrash::KeepFDs
don't close all file descriptors immediately
Definition: kcrash.h:108
KAboutData
KCrash::isDrKonqiEnabled
bool isDrKonqiEnabled()
Returns true if DrKonqi is set to be launched from the crash handler or false otherwise.
Definition: kcrash.cpp:221
kcrash.h
KCrash::emergencySaveFunction
HandlerType emergencySaveFunction()
Returns the currently set emergency save function.
Definition: kcrash.cpp:115
s_emergencySaveFunction
static KCrash::HandlerType s_emergencySaveFunction
Definition: kcrash.cpp:80
s_appPath
static char * s_appPath
Definition: kcrash.cpp:84
KCrash::AutoRestart
autorestart this application. Only sensible for KUniqueApplications.
Definition: kcrash.h:111
s_launchDrKonqi
static bool s_launchDrKonqi
Definition: kcrash.cpp:89
KCrash::AlwaysDirectly
never try to to start DrKonqi via kdeinit. Use fork() and exec() instead.
Definition: kcrash.h:110
KCrash::SaferDialog
start DrKonqi without arbitrary disk access
Definition: kcrash.h:109
kstandarddirs.h
s_autoRestartCommand
static char * s_autoRestartCommand
Definition: kcrash.cpp:83
KStandardDirs::findExe
static QString findExe(const QString &appname, const QString &pathstr=QString(), SearchOptions options=NoSearchOptions)
KComponentData::isValid
bool isValid() const
KGlobal::mainComponent
const KComponentData & mainComponent()
KCrash::crashHandler
HandlerType crashHandler()
Returns the installed crash handler.
Definition: kcrash.cpp:273
KCrash::setDrKonqiEnabled
void setDrKonqiEnabled(bool enabled)
Enables or disables launching DrKonqi from the crash handler.
Definition: kcrash.cpp:204
KAboutData::internalProgramName
const char * internalProgramName() const
KCrash::setFlags
void setFlags(CrashFlags flags)
Set options to determine how the default crash handler should behave.
kaboutdata.h
s_flags
static KCrash::CrashFlags s_flags
Definition: kcrash.cpp:88
KApplication::loadedByKdeinit
static bool loadedByKdeinit
Definition: kapplication.h:404
kcomponentdata.h
KComponentData
KStandardAction::close
KAction * close(const QObject *recvr, const char *slot, QObject *parent)
Close the current document.
Definition: kstandardaction.cpp:269
KCrash::setCrashHandler
void setCrashHandler(HandlerType handler=defaultCrashHandler)
Install a function to be called when a crash occurs.
Definition: kcrash.cpp:227
KCrash::win32UnhandledExceptionFilter
LONG WINAPI win32UnhandledExceptionFilter(_EXCEPTION_POINTERS *exceptionInfo)
Definition: kcrash.cpp:475
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal