summaryrefslogtreecommitdiffstats
path: root/lib/Plugins/KerneloopsReporter.cpp
blob: 4dced9e364d9b40175cfcc979340f7f6b468a29b (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
/*
 * 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 "abrtlib.h"
#include "KerneloopsReporter.h"
#include "CommLayerInner.h"
#include <curl/curl.h>
#include "ABRTException.h"

#define FILENAME_KERNELOOPS "kerneloops"

/* helpers */
static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
{
	size *= nmemb;
/*
	char *c, *c1, *c2;

	log("received: '%*.*s'\n", (int)size, (int)size, (char*)ptr);
	c = (char*)xzalloc(size + 1);
	memcpy(c, ptr, size);
	c1 = strstr(c, "201 ");
	if (c1) {
		c1 += 4;
		c2 = strchr(c1, '\n');
		if (c2)
			*c2 = 0;
	}
	free(c);
*/

	return size;
}

/* Send oops data to kerneloops.org-style site, using HTTP POST */
/* Returns 0 on success */
static int http_post_to_kerneloops_site(const char *url, const char *oopsdata)
{
	CURLcode ret;
	CURL *handle;
	struct curl_httppost *post = NULL;
	struct curl_httppost *last = NULL;

	handle = curl_easy_init();
	curl_easy_setopt(handle, CURLOPT_URL, url);

	curl_formadd(&post, &last,
		CURLFORM_COPYNAME, "oopsdata",
		CURLFORM_COPYCONTENTS, oopsdata,
		CURLFORM_END);
	curl_formadd(&post, &last,
		CURLFORM_COPYNAME, "pass_on_allowed",
		CURLFORM_COPYCONTENTS, "yes",
		CURLFORM_END);

	curl_easy_setopt(handle, CURLOPT_HTTPPOST, post);
	curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writefunction);

	ret = curl_easy_perform(handle);

	curl_formfree(post);
	curl_easy_cleanup(handle);

	return ret != 0;
}


/* class CKerneloopsReporter */
CKerneloopsReporter::CKerneloopsReporter() :
	m_sSubmitURL("http://submit.kerneloops.org/submitoops.php")
{}

std::string CKerneloopsReporter::Report(const map_crash_report_t& pCrashReport, const std::string& pArgs)
{
	int ret = -1;
	map_crash_report_t::const_iterator it;

	update_client(_("Creating and submitting a report..."));

	it = pCrashReport.begin();
	it = pCrashReport.find(FILENAME_KERNELOOPS);
	if (it != pCrashReport.end()) {
		ret = http_post_to_kerneloops_site(
			m_sSubmitURL.c_str(),
			it->second[CD_CONTENT].c_str()
		);
	}

	if (ret)
	{
		/* FIXME: be more informative */
	    throw CABRTException(EXCEP_PLUGIN, std::string("CKerneloopsReporter::Report(): Report has not been sent..."));
	}
	return "Kernel oops report was uploaded to :" + m_sSubmitURL;
}

void CKerneloopsReporter::SetSettings(const map_plugin_settings_t& pSettings)
{
	if (pSettings.find("SubmitURL") != pSettings.end())
	{
		m_sSubmitURL = pSettings.find("SubmitURL")->second;
	}
}

map_plugin_settings_t CKerneloopsReporter::GetSettings()
{
	map_plugin_settings_t ret;

	ret["SubmitURL"] = m_sSubmitURL;

	return ret;
}

PLUGIN_INFO(REPORTER,
            CKerneloopsReporter,
            "KerneloopsReporter",
            "0.0.1",
            "Sends the Kerneloops crash information to Kerneloopsoops.org",
            "anton@redhat.com",
            "http://people.redhat.com/aarapov",
            PLUGINS_LIB_DIR"/KerneloopsReporter.GTKBuilder");