summaryrefslogtreecommitdiffstats
path: root/lib/Plugins/Kerneloops.cpp
blob: b743a48a36a468978ab51d858a324e8319b63434 (plain)
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
/*
 * Copyright 2007, Intel Corporation
 * Copyright 2009, Red Hat Inc.
 *
 * This file is part of Abrt.
 *
 * This program file 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; version 2 of the License.
 *
 * 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 in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * Authors:
 *      Anton Arapov <anton@redhat.com>
 *      Arjan van de Ven <arjan@linux.intel.com>
 */

#include "Kerneloops.h"
#include "KerneloopsSysLog.h"
#include "DebugDump.h"
#include "PluginSettings.h"

#include <sstream>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <limits.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <asm/unistd.h>

#define MAX(A,B) ((A) > (B) ? (A) : (B))

CAnalyzerKerneloops::CAnalyzerKerneloops() :
	m_sSysLogFile("/var/log/messages")
{}

void CAnalyzerKerneloops::WriteSysLog(int m_nCount)
{
	if (m_nCount > 0) {
		openlog("abrt", 0, LOG_KERN);
		syslog(LOG_WARNING, "Kerneloops: Reported %i kernel oopses to Abrt", m_nCount);
		closelog();
	}
}

std::string CAnalyzerKerneloops::GetLocalUUID(const std::string& pDebugDumpDir)
{
	std::string m_sOops;
	std::stringstream m_sHash;
	CDebugDump m_pDebugDump;
	m_pDebugDump.Open(pDebugDumpDir);
	m_pDebugDump.LoadText(FILENAME_TEXTDATA1, m_sOops);

	/* An algorithm proposed by Donald E. Knuth in The Art Of Computer
	 * Programming Volume 3, under the topic of sorting and search
	 * chapter 6.4.
	 */
	unsigned int m_nHash = static_cast<unsigned int>(m_sOops.length());
	for(std::size_t i = 0; i < m_sOops.length(); i++)
	{
		m_nHash = ((m_nHash << 5) ^ (m_nHash >> 27)) ^ m_sOops[i];
	}
	m_sHash << (m_nHash & 0x7FFFFFFF);

	return m_sHash.str();
}

std::string CAnalyzerKerneloops::GetGlobalUUID(const std::string& pDebugDumpDir)
{
	return GetLocalUUID(pDebugDumpDir);
}

void CAnalyzerKerneloops::Report()
{
	CDebugDump m_pDebugDump;
	char m_sPath[PATH_MAX];
	std::list<COops> m_pOopsList;

	time_t t = time(NULL);
	if (((time_t) -1) == t)
	{
		throw std::string("CAnalyzerKerneloops::Report(): cannot get local time.");
	}

	m_pOopsList = m_pSysLog.GetOopsList();
	m_pSysLog.ClearOopsList();
	while (!m_pOopsList.empty())
	{
		snprintf(m_sPath, sizeof(m_sPath), "%s/kerneloops-%d-%d", DEBUG_DUMPS_DIR, t, m_pOopsList.size());

		COops m_pOops;
		m_pOops = m_pOopsList.back();

		try
		{
			m_pDebugDump.Create(m_sPath);
			m_pDebugDump.SaveText(FILENAME_ANALYZER, "Kerneloops");
			m_pDebugDump.SaveText(FILENAME_UID, "0");
			m_pDebugDump.SaveText(FILENAME_EXECUTABLE, "kernel");
			m_pDebugDump.SaveText(FILENAME_KERNEL, m_pOops.m_sVersion);
			m_pDebugDump.SaveText(FILENAME_PACKAGE, "not_applicable");
			m_pDebugDump.SaveText(FILENAME_TEXTDATA1, m_pOops.m_sData);
			m_pDebugDump.Close();
		}
		catch (std::string sError)
		{
			throw std::string("CAnalyzerKerneloops::Report(): ") + sError.c_str();
		}
		m_pOopsList.pop_back();
	}
}

void CAnalyzerKerneloops::Init()
{
	/* daemonize */
	pid_t pid = fork();
	if (pid < 0)
		throw std::string("CAnalyzerKerneloops::Init(): fork failed.");

	/* hack: release Init() */
	if (pid)
		return;

	sched_yield();

#ifdef PR_SET_TIMERSLACK
	/*
	 * Signal the kernel that we're not timing critical
	 */
	prctl(PR_SET_TIMERSLACK,1000*1000*1000, 0, 0, 0);
#endif

	/* we scan dmesg before /var/log/messages; dmesg is a more accurate source normally */
	ScanDmesg();
	/* during boot... don't go too fast and slow the system down */
	sleep(10);
	ScanSysLogFile(m_sSysLogFile.c_str(), 1);

	while(1) {
		sleep(10);
		ScanDmesg();
	}
}

void CAnalyzerKerneloops::ScanDmesg()
{
	int m_nFoundOopses;
	char *buffer;

	buffer = (char*)calloc(getpagesize()+1, 1);

	syscall(__NR_syslog, 3, buffer, getpagesize());
	m_nFoundOopses = m_pSysLog.ExtractOops(buffer, strlen(buffer), 0);
	free(buffer);

	if (m_nFoundOopses > 0)
		Report();
}

void CAnalyzerKerneloops::ScanSysLogFile(const char *filename, int issyslog)
{
	char *buffer;
	struct stat statb;
	FILE *file;
	int ret;
	int m_nFoundOopses;
	size_t buflen;

	memset(&statb, 0, sizeof(statb));

	ret = stat(filename, &statb);

	if (statb.st_size < 1 || ret != 0)
		return;

	/*
	 * in theory there's a race here, since someone could spew
	 * to /var/log/messages before we read it in... we try to
	 * deal with it by reading at most 1023 bytes extra. If there's
	 * more than that.. any oops will be in dmesg anyway.
	 * Do not try to allocate an absurt amount of memory; ignore
	 * older log messages because they are unlikely to have
	 * sufficiently recent data to be useful.  32MB is more
	 * than enough; it's not worth looping through more log
	 * if the log is larger than that.
	 */
	buflen = MAX(statb.st_size+1024, 32*1024*1024);
	buffer = (char*)calloc(buflen, 1);
	assert(buffer != NULL);

	file = fopen(filename, "rm");
	if (!file) {
		free(buffer);
		return;
	}
	fseek(file, -buflen, SEEK_END);
	ret = fread(buffer, 1, buflen-1, file);
	fclose(file);

	if (ret > 0)
		m_nFoundOopses = m_pSysLog.ExtractOops(buffer, buflen-1, issyslog);
	free(buffer);

	if (m_nFoundOopses > 0) {
		Report();
		WriteSysLog(m_nFoundOopses);
	}
}

void CAnalyzerKerneloops::LoadSettings(const std::string& pPath)
{
	map_settings_t settings;
	plugin_load_settings(pPath, settings);

	if (settings.find("SysLogFile")!= settings.end())
	{
		m_sSysLogFile = settings["SysLogFile"];
	}
}