-
Notifications
You must be signed in to change notification settings - Fork 16
/
creporterutils.cpp
354 lines (294 loc) · 11.3 KB
/
creporterutils.cpp
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
/*
* This file is part of crash-reporter
*
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
*
* Contact: Ville Ilvonen <ville.p.ilvonen@nokia.com>
* Author: Riku Halonen <riku.halonen@nokia.com>
*
* Copyright (C) 2013 Jolla Ltd.
* Contact: Jakub Adam <jakub.adam@jollamobile.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include <sys/types.h> // for stat()
#include <sys/stat.h>
#ifndef CREPORTER_UNIT_TEST
#include <deviceinfo.h>
#endif
#include <QDebug>
#include <QFileInfo>
#include <QDir>
#include <QFile>
#include <QProcess>
#include <mce/dbus-names.h>
#include <mce/mode-names.h>
#include <notification.h>
#include "creporterutils.h"
#include "creporternamespace.h"
#include "../autouploader_interface.h" // generated
#include "../ssu_interface.h" // generated
#ifndef CREPORTER_UNIT_TEST
#include "creporterprivacysettingsmodel.h"
#endif
namespace CReporter {
namespace LoggingCategory {
Q_LOGGING_CATEGORY(cr, "creporter", QtInfoMsg)
}
}
using CReporter::LoggingCategory::cr;
const QString richCoreTmpNoteFile = "/tmp/rich-core-note.txt";
const QString coreSuffixRcore = "rcore";
const QString coreSuffixRcoreLzo = "rcore.lzo";
CReporterUtils::CReporterUtils() {}
bool CReporterUtils::validateCore(const QString &path)
{
return (path.endsWith(coreSuffixRcoreLzo, Qt::CaseInsensitive) ||
path.endsWith(coreSuffixRcore, Qt::CaseInsensitive));
}
char *CReporterUtils::qstringToChar(const QString &str)
{
return str.toLatin1().data();
}
bool CReporterUtils::isMounted(const QString &path)
{
#if defined(CREPORTER_SDK_HOST) || defined(CREPORTER_UNIT_TEST)
Q_UNUSED(path);
// Skip, if running in scratchbox.
qCDebug(cr) << "Scratchbox target -> skip check and return true";
return true;
#else
struct stat st;
memset(&st, 0, sizeof(st));
if (stat(CReporterUtils::qstringToChar(path), &st) == 0) {
qCDebug(cr) << "Path:" << path << "is mounted. Device ID:" << st.st_dev;
return true;
}
qCDebug(cr) << "Path:" << path << "not mounted.";
return false;
#endif // defined(CREPORTER_SDK_HOST) || defined(CREPORTER_UNIT_TEST)
}
bool CReporterUtils::removeFile(const QString &path)
{
QFileInfo fi(path);
qCDebug(cr) << "Removing file:" << fi.absoluteFilePath();
return QFile::remove(fi.absoluteFilePath());
}
QStringList CReporterUtils::parseCrashInfoFromFilename(const QString &filePath)
{
qCDebug(cr) << "Parse:" << filePath;
QFileInfo fi(filePath);
// Remove path and file suffix. i.e. /media/mmc1/application-hwid-11-2029.rcore.lzo =>
// application-hwid-11-2029
QString baseName = fi.baseName();
/*
* The basename format is: application_name-hwid-11-2029
*
* Parsing from start until dash '-', because it may appear as part of executable name.
* Instead, walk from end to beginning.
*/
// Find the last dash. Start from the last character.
int searchIndex = baseName.lastIndexOf("-");
// Extracts PID substring.
QString pid = baseName.right(baseName.size() - (searchIndex + 1));
qCDebug(cr) << "PID:" << pid;
// Remove dash and PID from the original basename.
baseName = baseName.remove(searchIndex, pid.size() + 1);
// Repeat above for SIGNUM, HWID and IMEI. Finally, string what is left in the basename,
// is the application name.
// Extract SIGNUM.
searchIndex = baseName.lastIndexOf("-");
QString signum = baseName.right(baseName.size() - (searchIndex + 1));
qCDebug(cr) << "SIGNUM:" << signum;
baseName = baseName.remove(searchIndex, signum.size() + 1);
// Extract HWID.
searchIndex = baseName.lastIndexOf("-");
QString hwid = baseName.right(baseName.size() - (searchIndex + 1));
qCDebug(cr) << "HWID:" << hwid;
baseName = baseName.remove(searchIndex, hwid.size() + 1);
qCDebug(cr) << "Application name:" << baseName;
// Append results to list. Index 0 = Application name ....
QStringList result;
result << baseName << hwid << signum << pid;
return result;
}
bool CReporterUtils::appendToLzo(const QString &text, const QString &filePath)
{
QFile tmpFile(richCoreTmpNoteFile);
// Create local temp file, where comments are written.
if (!tmpFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qCWarning(cr) << "Unable to open file:" << filePath;
return false;
}
QTextStream out(&tmpFile);
out << text;
tmpFile.close();
// Append to *.lzo.
QString cmd = QString("/usr/bin/lzop -c %1 >> %2").arg(richCoreTmpNoteFile).arg(filePath) ;
int res = system(qstringToChar(cmd));
qCDebug(cr) << "System returned:" << res;
// Remove temp file.
if (!tmpFile.remove()) {
qCWarning(cr) << "Unable to remove file:" << richCoreTmpNoteFile;
}
return true;
}
QString CReporterUtils::deviceUid()
{
#ifndef CREPORTER_UNIT_TEST
static OrgNemoSsuInterface *ssuProxy = 0;
if (!ssuProxy) {
ssuProxy = new OrgNemoSsuInterface("org.nemo.ssu", "/org/nemo/ssu",
QDBusConnection::systemBus(), qApp);
}
QDBusPendingReply<QString> reply = ssuProxy->deviceUid();
reply.waitForFinished();
if (reply.isError()) {
qCWarning(cr) << "DBus unavailable, UUID might be incorrect.";
return DeviceInfo(true).deviceUid();
} else {
return reply.value();
}
#else
return "1234";
#endif
}
QString CReporterUtils::deviceModel()
{
#ifndef CREPORTER_UNIT_TEST
return DeviceInfo(true).model();
#else
return "Device";
#endif
}
bool CReporterUtils::shouldSavePower()
{
#ifndef CREPORTER_UNIT_TEST
auto *settings = CReporterPrivacySettingsModel::instance();
QDBusInterface mce(QLatin1String(MCE_SERVICE),
QLatin1String(MCE_REQUEST_PATH),
QLatin1String(MCE_REQUEST_IF),
QDBusConnection::systemBus());
QDBusReply<QString> batteryStatus = mce.call(QLatin1String(MCE_BATTERY_STATUS_GET));
if (!batteryStatus.isValid()) {
qCWarning(cr) << "Failed to query battery status" << batteryStatus.error().message();
return false;
}
QDBusReply<int> batteryLevel = mce.call(QLatin1String(MCE_BATTERY_LEVEL_GET));
if (!batteryLevel.isValid()) {
qCWarning(cr) << "Failed to query battery level" << batteryLevel.error().message();
return false;
}
QDBusReply<QString> chargerState = mce.call(QLatin1String(MCE_CHARGER_STATE_GET));
if (!chargerState.isValid()) {
qCWarning(cr) << "Failed to query charger state" << chargerState.error().message();
return false;
}
qCDebug(cr) << "Battery status:" << batteryStatus
<< "level:" << batteryLevel
<< "charger-state:" << chargerState;
if (batteryStatus == QLatin1String(MCE_BATTERY_STATUS_UNKNOWN))
return false;
if (batteryStatus == QLatin1String(MCE_BATTERY_STATUS_FULL)) {
return false;
} else if (chargerState != QLatin1String(MCE_CHARGER_STATE_OFF)) {
return settings->restrictWhenLowBattery()
&& batteryStatus != QLatin1String(MCE_BATTERY_STATUS_OK);
} else {
return settings->restrictWhenDischarging()
&& batteryLevel < settings->dischargingThreshold();
}
#else
return false;
#endif
}
bool CReporterUtils::reportIncludesCrash(const QString &fileName)
{
return !(fileName.contains(CReporter::QuickFeedbackPrefix) ||
fileName.contains(CReporter::EndurancePackagePrefix) ||
fileName.contains(CReporter::PowerExcessPrefix) ||
fileName.contains(CReporter::OneshotFailurePrefix) ||
fileName.contains(CReporter::HWrebootPrefix) ||
fileName.contains(CReporter::HWSMPLPrefix) ||
fileName.contains(CReporter::OverheatShutdownPrefix) ||
fileName.contains(CReporter::JournalSpyPrefix));
}
bool CReporterUtils::notifyAutoUploader(const QStringList &filesToUpload,
bool obeyResourcesRestrictions)
{
qCDebug(cr) << "Requesting crash-reporter-autouploader to upload"
<< filesToUpload.size() << "files.";
ComNokiaCrashReporterAutoUploaderInterface proxy(CReporter::AutoUploaderServiceName,
CReporter::AutoUploaderObjectPath, QDBusConnection::sessionBus());
QDBusPendingReply <bool>reply =
proxy.uploadFiles(filesToUpload, obeyResourcesRestrictions);
// This blocks.
reply.waitForFinished();
if (reply.isError()) {
qCWarning(cr) << "D-Bus error occurred:" << reply.error().name() << reply.error().message();
return false;
}
return true;
}
QProcess *CReporterUtils::invokeLogCollection(const QString &label)
{
QScopedPointer<QProcess> richCoreHelper(new QProcess(qApp));
richCoreHelper->start("/usr/libexec/rich-core-helper",
QStringList() << label);
if (!richCoreHelper->waitForStarted()) {
qCWarning(cr) << "Problem invoking rich-core-dumper.";
return 0;
}
typedef void (QProcess::*FinishedSignal)(int, QProcess::ExitStatus);
connect(richCoreHelper.data(), (FinishedSignal) &QProcess::finished,
richCoreHelper.data(), &QProcess::deleteLater);
return richCoreHelper.take();
}
static void runServiceHelper(const QString &service, bool run)
{
QProcess::execute("/usr/libexec/crashreporter-servicehelper", QStringList() << service << (run ? "start" : "stop"));
}
void CReporterUtils::setEnduranceServiceState(bool run)
{
runServiceHelper(QStringLiteral("endurance"), run);
}
void CReporterUtils::setJournalSpyServiceState(bool run)
{
runServiceHelper(QStringLiteral("journalspy"), run);
}
void CReporterUtils::applyNotificationStyle(Notification *notification)
{
//% "Crash reporter"
notification->setAppName(qtTrId("crash_reporter-notify-app_name"));
notification->setAppIcon(QLatin1String("icon-m-crash-reporter"));
QVariant action = Notification::remoteAction(QLatin1String("default"), QString(),
QLatin1String("com.jolla.settings"),
QLatin1String("/com/jolla/settings/ui"),
QLatin1String("com.jolla.settings.ui"),
QLatin1String("showPage"),
QVariantList() << QLatin1String("system_settings/system/crash_reporter"));
notification->setRemoteActions(QVariantList() << action);
}
CReporterUtils *CReporterUtils::instance()
{
/* Will be set to zero when someone outside calls delete on the object. This
* happens when qmlRegisterSingletonType() is used with the instance. */
static QPointer<CReporterUtils> instance;
if (!instance) {
instance = new CReporterUtils;
}
return instance.data();
}