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
|
/*
CCpp.cpp - the hook for C/C++ crashing program
Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com)
Copyright (C) 2009 RedHat inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "DebugDump.h"
#include "ABRTException.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <syslog.h>
#include <string>
#define FILENAME_EXECUTABLE "executable"
#define FILENAME_CMDLINE "cmdline"
#define FILENAME_COREDUMP "coredump"
static void write_success_log(const char* pid)
{
openlog("abrt", 0, LOG_DAEMON);
syslog(LOG_WARNING, "CCpp Language Hook: Crashed pid: %s", pid);
closelog();
}
static void write_faliure_log(const char* msg)
{
openlog("abrt", 0, LOG_DAEMON);
syslog(LOG_WARNING, "CCpp Language Hook: Exception occur: %s", msg);
closelog();
}
char* get_executable(const char* pid)
{
char path[PATH_MAX];
char executable[PATH_MAX];
int len;
snprintf(path, sizeof(path), "/proc/%s/exe", pid);
if ((len = readlink(path, executable, PATH_MAX)) != -1)
{
executable[len] = '\0';
return strdup(executable);
}
return NULL;
}
// taken from kernel
#define COMMAND_LINE_SIZE 2048
char* get_cmdline(const char* pid)
{
char path[PATH_MAX];
char cmdline[COMMAND_LINE_SIZE];
snprintf(path, sizeof(path), "/proc/%s/cmdline", pid);
FILE* fp = fopen(path, "r");
int ch;
int ii = 0;
while ((ch = fgetc(fp)) != EOF)
{
if (ch == 0)
{
cmdline[ii] = ' ';
}
else if (isspace(ch) || (isascii(ch) && !iscntrl(ch)))
{
cmdline[ii] = ch;
}
ii++;
}
cmdline[ii] = '\0';
fclose(fp);
return strdup(cmdline);
}
int main(int argc, char** argv)
{
const char* program_name = argv[0];
if (argc < 3)
{
fprintf(stderr, "Usage: %s: <dddir> <pid> <signal> <uid>\n",
program_name);
return -1;
}
const char* dddir = argv[1];
const char* pid = argv[2];
const char* signal = argv[3];
const char* uid = argv[4];
if (strcmp(signal, "3") != 0 && // SIGQUIT
strcmp(signal, "4") != 0 && // SIGILL
strcmp(signal, "6") != 0 && // SIGABRT
strcmp(signal, "8") != 0 && // SIGFPE
strcmp(signal, "11") != 0) // SIGSEGV
{
return 0;
}
try
{
FILE* fp;
CDebugDump dd;
int byte;
char path[PATH_MAX];
char* executable = NULL;
char* cmdline = NULL;
executable = get_executable(pid);
cmdline = get_cmdline(pid);
if (executable == NULL ||
cmdline == NULL)
{
free(executable);
free(cmdline);
throw CABRTException(EXCEP_FATAL, "Can not get proc info.");
}
snprintf(path, sizeof(path), "%s/ccpp-%ld-%s", dddir, time(NULL), pid);
dd.Create(path);
dd.SaveText(FILENAME_ANALYZER, "CCpp");
dd.SaveText(FILENAME_EXECUTABLE, executable);
dd.SaveText(FILENAME_UID, uid);
dd.SaveText(FILENAME_CMDLINE, cmdline);
snprintf(path + strlen(path), sizeof(path), "/%s", FILENAME_COREDUMP);
if ((fp = fopen(path, "w")) == NULL)
{
dd.Delete();
dd.Close();
throw CABRTException(EXCEP_FATAL, std::string("Can not open the file ") + path);
}
// TODO: rewrite this
while ((byte = getc(stdin)) != EOF)
{
if (putc(byte, fp) == EOF)
{
fclose(fp);
dd.Delete();
dd.Close();
throw CABRTException(EXCEP_FATAL, "Can not write to the file %s.");
}
}
free(executable);
free(cmdline);
fclose(fp);
dd.Close();
write_success_log(pid);
}
catch (std::exception& e)
{
fprintf(stderr, "%s: %s\n", program_name, e.what());
write_faliure_log(e.what());
return -2;
}
return 0;
}
|