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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
/*
* This file is part of the KDE Libraries
* Copyright (C) 2000 Timo Hummel <[email protected]>
* Tom Braun <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*
* This file is used to catch signals which would normally
* crash the application (like segmentation fault, floating
* point exception and such).
*/
#include "config.h"
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "kcrash.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <errno.h>
#include <tqwindowdefs.h>
#include <tdeglobal.h>
#include <kinstance.h>
#include <tdeaboutdata.h>
#include <kdebug.h>
#include <tdeapplication.h>
#include <dcopclient.h>
#include <../tdeinit/tdelauncher_cmds.h>
#if defined Q_WS_X11
#include <X11/Xlib.h>
#endif
TDECrash::HandlerType TDECrash::_emergencySaveFunction = 0;
TDECrash::HandlerType TDECrash::_crashHandler = 0;
const char *TDECrash::appName = 0;
const char *TDECrash::appPath = 0;
bool TDECrash::safer = false;
// This function sets the function which should be called when the
// application crashes and the
// application is asked to try to save its data.
void
TDECrash::setEmergencySaveFunction (HandlerType saveFunction)
{
_emergencySaveFunction = saveFunction;
/*
* We need at least the default crash handler for
* emergencySaveFunction to be called
*/
if (_emergencySaveFunction && !_crashHandler)
_crashHandler = defaultCrashHandler;
}
// This function sets the function which should be responsible for
// the application crash handling.
void
TDECrash::setCrashHandler (HandlerType handler)
{
#ifdef Q_OS_UNIX
if (!handler)
handler = SIG_DFL;
sigset_t mask;
sigemptyset(&mask);
#ifdef SIGSEGV
signal (SIGSEGV, handler);
sigaddset(&mask, SIGSEGV);
#endif
#ifdef SIGFPE
signal (SIGFPE, handler);
sigaddset(&mask, SIGFPE);
#endif
#ifdef SIGILL
signal (SIGILL, handler);
sigaddset(&mask, SIGILL);
#endif
#ifdef SIGABRT
signal (SIGABRT, handler);
sigaddset(&mask, SIGABRT);
#endif
sigprocmask(SIG_UNBLOCK, &mask, 0);
#endif //Q_OS_UNIX
_crashHandler = handler;
}
void
TDECrash::defaultCrashHandler (int sig)
{
#ifdef Q_OS_UNIX
// WABA: Do NOT use kdDebug() in this function because it is much too risky!
// Handle possible recursions
static int crashRecursionCounter = 0;
crashRecursionCounter++; // Nothing before this, please !
signal(SIGALRM, SIG_DFL);
alarm(3); // Kill me... (in case we deadlock in malloc)
if (crashRecursionCounter < 2) {
if (_emergencySaveFunction) {
_emergencySaveFunction (sig);
}
crashRecursionCounter++; //
}
// Close all remaining file descriptors except for stdin/stdout/stderr
struct rlimit rlp;
getrlimit(RLIMIT_NOFILE, &rlp);
for (int i = 3; i < (int)rlp.rlim_cur; i++)
close(i);
// this code is leaking, but this should not hurt cause we will do a
// exec() afterwards. exec() is supposed to clean up.
if (crashRecursionCounter < 3)
{
if (appName)
{
#ifndef NDEBUG
fprintf(stderr, "[kcrash] TDECrash: crashing... crashRecursionCounter = %d\n", crashRecursionCounter);
fprintf(stderr, "[kcrash] TDECrash: Application Name = %s path = %s pid = %d\n", appName ? appName : "<unknown>" , appPath ? appPath : "<unknown>", getpid());
#else
fprintf(stderr, "[kcrash] TDECrash: Application '%s' crashing...\n", appName ? appName : "<unknown>");
#endif
const char * argv[24]; // don't forget to update this
int i = 0;
// argument 0 has to be drkonqi
argv[i++] = "drkonqi";
#if defined Q_WS_X11
// start up on the correct display
argv[i++] = "-display";
if ( tqt_xdisplay() )
argv[i++] = XDisplayString(tqt_xdisplay());
else
argv[i++] = getenv("DISPLAY");
#elif defined(Q_WS_QWS)
// start up on the correct display
argv[i++] = "-display";
argv[i++] = getenv("QWS_DISPLAY");
#endif
// we have already tested this
argv[i++] = "--appname";
argv[i++] = appName;
if (TDEApplication::loadedByKdeinit)
argv[i++] = "--tdeinit";
// only add apppath if it's not NULL
if (appPath) {
argv[i++] = "--apppath";
argv[i++] = appPath;
}
// signal number -- will never be NULL
char sigtxt[ 10 ];
sprintf( sigtxt, "%d", sig );
argv[i++] = "--signal";
argv[i++] = sigtxt;
char pidtxt[ 10 ];
sprintf( pidtxt, "%d", getpid());
argv[i++] = "--pid";
argv[i++] = pidtxt;
const TDEInstance *instance = TDEGlobal::_instance;
const TDEAboutData *about = instance ? instance->aboutData() : 0;
if (about) {
if (about->internalVersion()) {
argv[i++] = "--appversion";
argv[i++] = about->internalVersion();
}
if (about->internalProgramName()) {
argv[i++] = "--programname";
argv[i++] = about->internalProgramName();
}
if (about->internalBugAddress()) {
argv[i++] = "--bugaddress";
argv[i++] = about->internalBugAddress();
}
}
if ( kapp && !kapp->startupId().isNull()) {
argv[i++] = "--startupid";
argv[i++] = kapp->startupId().data();
}
if ( safer )
argv[i++] = "--safer";
// NULL terminated list
argv[i] = NULL;
startDrKonqi( argv, i );
_exit(253);
}
else {
fprintf(stderr, "[kcrash] Unknown appname\n");
}
}
if (crashRecursionCounter < 4)
{
fprintf(stderr, "[kcrash] Unable to start Dr. Konqi\n");
}
#endif //Q_OS_UNIX
_exit(255);
}
#ifdef Q_OS_UNIX
// Since we can't fork() in the crashhandler, we cannot execute any external code
// (there can be functions registered to be performed before fork(), for example
// handling of malloc locking, which doesn't work when malloc crashes because of heap corruption).
static int write_socket(int sock, char *buffer, int len);
static int read_socket(int sock, char *buffer, int len);
static int openSocket();
void TDECrash::startDrKonqi( const char* argv[], int argc )
{
int socket = openSocket();
if( socket < -1 )
{
startDirectly( argv, argc );
return;
}
tdelauncher_header header;
header.cmd = LAUNCHER_EXEC_NEW;
const int BUFSIZE = 8192; // make sure this is big enough
char buffer[ BUFSIZE + 10 ];
int pos = 0;
long argcl = argc;
memcpy( buffer + pos, &argcl, sizeof( argcl ));
pos += sizeof( argcl );
for( int i = 0;
i < argc;
++i )
{
int len = strlen( argv[ i ] ) + 1; // include terminating \0
if( pos + len > BUFSIZE )
{
fprintf( stderr, "[kcrash] BUFSIZE in TDECrash not big enough!\n" );
startDirectly( argv, argc );
return;
}
memcpy( buffer + pos, argv[ i ], len );
pos += len;
}
long env = 0;
memcpy( buffer + pos, &env, sizeof( env ));
pos += sizeof( env );
long avoid_loops = 0;
memcpy( buffer + pos, &avoid_loops, sizeof( avoid_loops ));
pos += sizeof( avoid_loops );
header.arg_length = pos;
write_socket(socket, (char *) &header, sizeof(header));
write_socket(socket, buffer, pos);
if( read_socket( socket, (char *) &header, sizeof(header)) < 0
|| header.cmd != LAUNCHER_OK )
{
startDirectly( argv, argc );
return;
}
long pid;
read_socket(socket, buffer, header.arg_length);
pid = *((long *) buffer);
alarm(0); // Seems we made it....
for(;;)
{
if( kill( pid, 0 ) < 0 )
_exit(253);
sleep(1);
// the debugger should stop this process anyway
}
}
// If we can't reach tdeinit we can still at least try to fork()
void TDECrash::startDirectly( const char* argv[], int )
{
fprintf( stderr, "[kcrash] TDECrash cannot reach tdeinit, launching directly.\n" );
pid_t pid = fork();
if (pid <= 0)
{
if(!geteuid() && setgid(getgid()) < 0)
_exit(253);
if(!geteuid() && setuid(getuid()) < 0)
_exit(253);
execvp("drkonqi", const_cast< char** >( argv ));
_exit(errno);
}
else
{
alarm(0); // Seems we made it....
// wait for child to exit
waitpid(pid, NULL, 0);
_exit(253);
}
}
// From now on this code is copy&pasted from tdeinit/wrapper.c :
extern char **environ;
static char *getDisplay()
{
const char *display;
char *result;
char *screen;
char *colon;
char *i;
/*
don't test for a value from tqglobal.h but instead distinguish
Qt/X11 from Qt/Embedded by the fact that Qt/E apps have -DQWS
on the commandline (which in tqglobal.h however triggers Q_WS_QWS,
but we don't want to include that here) (Simon)
#ifdef Q_WS_X11
*/
#if !defined(QWS)
display = getenv("DISPLAY");
#else
display = getenv("QWS_DISPLAY");
#endif
if (!display || !*display)
{
display = ":0";
}
result = (char*)malloc(strlen(display)+1);
if (result == NULL)
return NULL;
strcpy(result, display);
screen = strrchr(result, '.');
colon = strrchr(result, ':');
if (screen && (screen > colon))
*screen = '\0';
while((i = strchr(result, ':')))
*i = '_';
return result;
}
/*
* Write 'len' bytes from 'buffer' into 'sock'.
* returns 0 on success, -1 on failure.
*/
static int write_socket(int sock, char *buffer, int len)
{
ssize_t result;
int bytes_left = len;
while ( bytes_left > 0)
{
result = write(sock, buffer, bytes_left);
if (result > 0)
{
buffer += result;
bytes_left -= result;
}
else if (result == 0)
return -1;
else if ((result == -1) && (errno != EINTR) && (errno != EAGAIN))
return -1;
}
return 0;
}
/*
* Read 'len' bytes from 'sock' into 'buffer'.
* returns 0 on success, -1 on failure.
*/
static int read_socket(int sock, char *buffer, int len)
{
ssize_t result;
int bytes_left = len;
while ( bytes_left > 0)
{
result = read(sock, buffer, bytes_left);
if (result > 0)
{
buffer += result;
bytes_left -= result;
}
else if (result == 0)
return -1;
else if ((result == -1) && (errno != EINTR) && (errno != EAGAIN))
return -1;
}
return 0;
}
static int openSocket()
{
kde_socklen_t socklen;
int s;
struct sockaddr_un server;
#define MAX_SOCK_FILE 255
char sock_file[MAX_SOCK_FILE + 1];
const char *home_dir = getenv("HOME");
const char *kde_home = getenv("TDEHOME");
char *display;
sock_file[0] = sock_file[MAX_SOCK_FILE] = 0;
if (!kde_home || !kde_home[0])
{
kde_home = "~/.trinity/";
}
if (kde_home[0] == '~')
{
if (!home_dir || !home_dir[0])
{
fprintf(stderr, "[kcrash] Warning: $HOME not set!\n");
return -1;
}
if (strlen(home_dir) > (MAX_SOCK_FILE-100))
{
fprintf(stderr, "[kcrash] Warning: Home directory path too long!\n");
return -1;
}
kde_home++;
strncpy(sock_file, home_dir, MAX_SOCK_FILE);
}
strncat(sock_file, kde_home, MAX_SOCK_FILE - strlen(sock_file));
/** Strip trailing '/' **/
if ( sock_file[strlen(sock_file)-1] == '/')
sock_file[strlen(sock_file)-1] = 0;
strncat(sock_file, "/socket-", MAX_SOCK_FILE - strlen(sock_file));
if( getenv("XAUTHLOCALHOSTNAME"))
strncat(sock_file, getenv("XAUTHLOCALHOSTNAME"), MAX_SOCK_FILE - strlen(sock_file) - 1);
else if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
{
perror("[kcrash] Warning: Could not determine hostname: ");
return -1;
}
sock_file[sizeof(sock_file)-1] = '\0';
/* append $DISPLAY */
display = getDisplay();
if (display == NULL)
{
fprintf(stderr, "[kcrash] Error: Could not determine display.\n");
return -1;
}
if (strlen(sock_file)+strlen(display)+strlen("/tdeinit_")+2 > MAX_SOCK_FILE)
{
fprintf(stderr, "[kcrash] Warning: Socket name will be too long.\n");
free(display);
return -1;
}
strcat(sock_file, "/tdeinit_");
strcat(sock_file, display);
free(display);
if (strlen(sock_file) >= sizeof(server.sun_path))
{
fprintf(stderr, "[kcrash] Warning: Path of socketfile exceeds UNIX_PATH_MAX.\n");
return -1;
}
/*
* create the socket stream
*/
s = socket(PF_UNIX, SOCK_STREAM, 0);
if (s < 0)
{
perror("[kcrash] Warning: socket creation failed: ");
return -1;
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, sock_file);
socklen = sizeof(server);
if(connect(s, (struct sockaddr *)&server, socklen) == -1)
{
perror("[kcrash] Warning: socket connection failed: ");
close(s);
return -1;
}
return s;
}
#endif // Q_OS_UNIX
|