File: halstorageaccess.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (527 lines) | stat: -rw-r--r-- 17,070 bytes parent folder | download
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
525
526
527
/*
    Copyright 2006 Kevin Ottens <ervin@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#include "halstorageaccess.h"

#include "../../genericinterface.h"
#include "halfstabhandling.h"

#include <QApplication>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDBusVariant>
#include <QDebug>
#include <QLocale>
#include <QProcess>
#include <QTimer>
#include <QWidget>

#include <stdlib.h>
#include <unistd.h>

#ifdef Q_OS_FREEBSD
#include <langinfo.h>
#endif

using namespace Solid::Backends::Hal;

StorageAccess::StorageAccess(HalDevice* device)
	: DeviceInterface(device), m_setupInProgress(false), m_teardownInProgress(false), m_ejectInProgress(false),
	  m_passphraseRequested(false)
{
	connect(device, SIGNAL(propertyChanged(QMap<QString, int>)),
	        this, SLOT(slotPropertyChanged(QMap<QString, int>)));
	// Delay connecting to DBus signals to avoid the related time penalty
	// in hot paths such as predicate matching
	QTimer::singleShot(0, this, SLOT(connectDBusSignals()));
}

StorageAccess::~StorageAccess()
{
}

void StorageAccess::connectDBusSignals()
{
	m_device->registerAction("setup", this,
	                         SLOT(slotSetupRequested()),
	                         SLOT(slotSetupDone(int, QString)));

	m_device->registerAction("teardown", this,
	                         SLOT(slotTeardownRequested()),
	                         SLOT(slotTeardownDone(int, QString)));

	m_device->registerAction("eject", this,
	                         SLOT(slotEjectRequested()),
	                         SLOT(slotEjectDone(int, QString)));
}

void StorageAccess::slotSetupDone(int error, const QString& errorString)
{
	m_setupInProgress = false;
	emit setupDone(static_cast<Solid::ErrorType>(error), errorString, m_device->udi());
}

void StorageAccess::slotTeardownDone(int error, const QString& errorString)
{
	m_teardownInProgress = false;
	emit teardownDone(static_cast<Solid::ErrorType>(error), errorString, m_device->udi());
}

void StorageAccess::slotEjectDone(int error, const QString& errorString)
{
	m_ejectInProgress = false;
	emit ejectDone(static_cast<Solid::ErrorType>(error), errorString, m_device->udi());
}

bool StorageAccess::isAccessible() const
{
	if (m_device->prop("info.interfaces").toStringList().contains("org.freedesktop.Hal.Device.Volume.Crypto")) {

		// Might be a bit slow, but I see no cleaner way to do this with HAL...
		QDBusInterface manager("org.freedesktop.Hal",
		                       "/org/freedesktop/Hal/Manager",
		                       "org.freedesktop.Hal.Manager",
		                       QDBusConnection::systemBus());

		QDBusReply<QStringList> reply = manager.call("FindDeviceStringMatch",
		                                             "volume.crypto_luks.clear.backing_volume",
		                                             m_device->udi());

		QStringList list = reply;

		return reply.isValid() && !list.isEmpty();
	}
	else {
		return m_device->prop("volume.is_mounted").toBool();
	}
}

QString StorageAccess::filePath() const
{
	QString result = m_device->prop("volume.mount_point").toString();

	if (result.isEmpty()) {
		QStringList mountpoints = FstabHandling::possibleMountPoints(m_device->prop("block.device").toString());
		if (mountpoints.size() == 1) {
			result = mountpoints.first();
		}
	}

	return result;
}

bool StorageAccess::isIgnored() const
{
	HalDevice lock("/org/freedesktop/Hal/devices/computer");
	bool isLocked = lock.prop("info.named_locks.Global.org.freedesktop.Hal.Device.Storage.locked").toBool();

	if (m_device->prop("volume.ignore").toBool() || isLocked) {
		return true;
	}

	const QString mount_point = StorageAccess(m_device).filePath();
	const bool mounted = m_device->prop("volume.is_mounted").toBool();
	if (!mounted) {
		return false;
	}
	else if (mount_point.startsWith(QLatin1String("/media/")) || mount_point.startsWith(QLatin1String("/mnt/"))) {
		return false;
	}

	/* Now be a bit more aggressive on what we want to ignore,
     * the user generally need to check only what's removable or in /media
     * the volumes mounted to make the system (/, /boot, /var, etc.)
     * are useless to him.
     */
	Solid::Device drive(m_device->prop("block.storage_device").toString());

	const bool removable = drive.as<Solid::GenericInterface>()->property("storage.removable").toBool();
	const bool hotpluggable = drive.as<Solid::GenericInterface>()->property("storage.hotpluggable").toBool();

	return !removable && !hotpluggable;
}

bool StorageAccess::setup()
{
	if (m_teardownInProgress || m_setupInProgress || isAccessible()) {
		return false;
	}
	m_setupInProgress = true;
	m_device->broadcastActionRequested("setup");

	if (m_device->prop("info.interfaces").toStringList().contains("org.freedesktop.Hal.Device.Volume.Crypto")) {
		return requestPassphrase();
	}
	else if (FstabHandling::isInFstab(m_device->prop("block.device").toString())) {
		return callSystemMount();
	}
	else {
		return callHalVolumeMount();
	}
}

bool StorageAccess::teardown()
{
	if (m_teardownInProgress || m_setupInProgress || !isAccessible()) {
		return false;
	}
	m_teardownInProgress = true;
	m_device->broadcastActionRequested("teardown");

	if (m_device->prop("info.interfaces").toStringList().contains("org.freedesktop.Hal.Device.Volume.Crypto")) {
		return callCryptoTeardown();
	}
	else if (FstabHandling::isInFstab(m_device->prop("block.device").toString())) {
		return callSystemUnmount();
	}
	else {
		return callHalVolumeUnmount();
	}
}

void StorageAccess::slotPropertyChanged(const QMap<QString, int>& changes)
{
	if (changes.contains("volume.is_mounted")) {
		emit accessibilityChanged(isAccessible(), m_device->udi());
	}
}

void StorageAccess::slotDBusReply(const QDBusMessage& /*reply*/)
{
	if (m_setupInProgress) {
		m_setupInProgress = false;
		m_device->broadcastActionDone("setup");
	}
	else if (m_teardownInProgress) {
		m_teardownInProgress = false;
		m_device->broadcastActionDone("teardown");

		HalDevice drive(m_device->prop("block.storage_device").toString());
		if (drive.prop("storage.drive_type").toString() != "cdrom"
		    && drive.prop("storage.requires_eject").toBool()) {

			QString devnode = m_device->prop("block.device").toString();

#if defined(Q_OS_OPENBSD)
			QString program = "cdio";
			QStringList args;
			args << "-f" << devnode << "eject";
#elif defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
			devnode.remove("/dev/").replace("([0-9]).", "\\1");
			QString program = "cdcontrol";
			QStringList args;
			args << "-f" << devnode << "eject";
#else
			QString program = "eject";
			QStringList args;
			args << devnode;
#endif

			m_ejectInProgress = true;
			m_device->broadcastActionRequested("eject");
			m_process = FstabHandling::callSystemCommand("eject", args,
			                                             this, SLOT(slotProcessFinished(int, QProcess::ExitStatus)));
		}
	}
	else if (m_ejectInProgress) {
		m_ejectInProgress = false;
		m_device->broadcastActionDone("eject");
	}
}

void StorageAccess::slotDBusError(const QDBusError& error)
{
	// TODO: Better error reporting here
	if (m_setupInProgress) {
		m_setupInProgress = false;
		m_device->broadcastActionDone("setup", Solid::UnauthorizedOperation,
		                              QString(error.name() + ": " + error.message()));
	}
	else if (m_teardownInProgress) {
		m_teardownInProgress = false;
		m_device->broadcastActionDone("teardown", Solid::UnauthorizedOperation,
		                              QString(error.name() + ": " + error.message()));
	}
	else if (m_ejectInProgress) {
		m_ejectInProgress = false;
		m_device->broadcastActionDone("eject", Solid::UnauthorizedOperation,
		                              QString(error.name() + ": " + error.message()));
	}
}

void Solid::Backends::Hal::StorageAccess::slotProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
	Q_UNUSED(exitStatus);
	if (m_setupInProgress) {
		m_setupInProgress = false;

		if (exitCode == 0) {
			m_device->broadcastActionDone("setup");
		}
		else {
			m_device->broadcastActionDone("setup", Solid::UnauthorizedOperation,
			                              m_process->readAllStandardError());
		}
	}
	else if (m_teardownInProgress) {
		m_teardownInProgress = false;
		if (exitCode == 0) {
			m_device->broadcastActionDone("teardown");
		}
		else {
			m_device->broadcastActionDone("teardown", Solid::UnauthorizedOperation,
			                              m_process->readAllStandardError());
		}
	}
	else if (m_ejectInProgress) {
		if (exitCode == 0) {
			m_ejectInProgress = false;
			m_device->broadcastActionDone("eject");
		}
		else {
			callHalVolumeEject();
		}
	}

	delete m_process;
}

void StorageAccess::slotSetupRequested()
{
	m_setupInProgress = true;
	emit setupRequested(m_device->udi());
}

void StorageAccess::slotTeardownRequested()
{
	m_teardownInProgress = true;
	emit teardownRequested(m_device->udi());
}

void StorageAccess::slotEjectRequested()
{
	m_ejectInProgress = true;
}

QString generateReturnObjectPath()
{
	static int number = 1;

	return "/org/kde/solid/HalStorageAccess_" + QString::number(number++);
}

bool StorageAccess::requestPassphrase()
{
	QString udi = m_device->udi();
	QString returnService = QDBusConnection::sessionBus().baseService();
	m_lastReturnObject = generateReturnObjectPath();

	QDBusConnection::sessionBus().registerObject(m_lastReturnObject, this,
	                                             QDBusConnection::ExportScriptableSlots);

	QWidget* activeWindow = QApplication::activeWindow();
	uint wId = 0;
	if (activeWindow != nullptr) {
		wId = (uint)activeWindow->winId();
	}

	QString appId = QCoreApplication::applicationName();

	QDBusInterface soliduiserver("org.kde.kded", "/modules/soliduiserver", "org.kde.SolidUiServer");
	QDBusReply<void> reply = soliduiserver.call("showPassphraseDialog", udi,
	                                            returnService, m_lastReturnObject,
	                                            wId, appId);
	m_passphraseRequested = reply.isValid();
	if (!m_passphraseRequested) {
		qWarning() << "Failed to call the SolidUiServer, D-Bus said:" << reply.error();
	}
	return m_passphraseRequested;
}

void StorageAccess::passphraseReply(const QString& passphrase)
{
	if (m_passphraseRequested) {
		QDBusConnection::sessionBus().unregisterObject(m_lastReturnObject);
		m_passphraseRequested = false;
		if (!passphrase.isEmpty()) {
			callCryptoSetup(passphrase);
		}
		else {
			m_setupInProgress = false;
			m_device->broadcastActionDone("setup");
		}
	}
}

bool StorageAccess::callHalVolumeMount()
{
	QDBusConnection c = QDBusConnection::systemBus();
	QString udi = m_device->udi();
	QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.Hal", udi,
	                                                  "org.freedesktop.Hal.Device.Volume",
	                                                  "Mount");

	// HAL 0.5.12 supports using alternative drivers for the same filesystem.
	// This is mainly used to integrate the ntfs-3g driver.
	// Unfortunately, the primary driver gets used unless we
	// specify some other driver (fstype) to the Mount method.
	// TODO: Allow the user to choose the driver.

	QString fstype = m_device->prop("volume.fstype").toString();
	QStringList halOptions = m_device->prop("volume.mount.valid_options").toStringList();

	QString alternativePreferred = m_device->prop("volume.fstype.alternative.preferred").toString();
	if (!alternativePreferred.isEmpty()) {
		QStringList alternativeFstypes = m_device->prop("volume.fstype.alternative").toStringList();
		if (alternativeFstypes.contains(alternativePreferred)) {
			fstype = alternativePreferred;
			halOptions = m_device->prop("volume.mount." + fstype + ".valid_options").toStringList();
		}
	}

	QStringList options;

#ifdef Q_OS_FREEBSD
	QString uid = "-u=";
#else
	QString uid = "uid=";
#endif
	if (halOptions.contains(uid)) {
		options << uid + QString::number(::getuid());
	}

#ifdef Q_OS_FREEBSD
	char* cType;
	if (fstype == "vfat" && halOptions.contains("-L=")) {
		if ((cType = getenv("LC_ALL")) || (cType = getenv("LC_CTYPE")) || (cType = getenv("LANG")))
			options << "-L=" + QString(cType);
	}
	else if ((fstype.startsWith(QLatin1String("ntfs")) || fstype == "iso9660" || fstype == "udf") && halOptions.contains("-C=")) {
		if ((cType = getenv("LC_ALL")) || (cType = getenv("LC_CTYPE")) || (cType = getenv("LANG")))
			options << "-C=" + QString(nl_langinfo(CODESET));
	}
#else
	if (fstype == "vfat" || fstype == "ntfs" || fstype == "iso9660" || fstype == "udf") {
		if (halOptions.contains("utf8"))
			options << "utf8";
		else if (halOptions.contains("iocharset="))
			options << "iocharset=utf8";
		if (halOptions.contains("shortname="))
			options << "shortname=mixed";
		if (halOptions.contains("flush"))
			options << "flush";
	}
	// pass our locale to the ntfs-3g driver so it can translate local characters
	else if (halOptions.contains("locale=")) {
		// have to obtain LC_CTYPE as returned by the `locale` command
		// check in the same order as `locale` does
		char* cType;
		if ((cType = getenv("LC_ALL")) || (cType = getenv("LC_CTYPE")) || (cType = getenv("LANG"))) {
			options << "locale=" + QString(cType);
		}
	}
#endif

	msg << "" << fstype << options;

	return c.callWithCallback(msg, this,
	                          SLOT(slotDBusReply(QDBusMessage)),
	                          SLOT(slotDBusError(QDBusError)));
}

bool StorageAccess::callHalVolumeUnmount()
{
	QDBusConnection c = QDBusConnection::systemBus();
	QString udi = m_device->udi();
	QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.Hal", udi,
	                                                  "org.freedesktop.Hal.Device.Volume",
	                                                  "Unmount");

	msg << QStringList();

	return c.callWithCallback(msg, this,
	                          SLOT(slotDBusReply(QDBusMessage)),
	                          SLOT(slotDBusError(QDBusError)));
}

bool StorageAccess::callHalVolumeEject()
{
	QString udi = m_device->udi();
	QString interface = "org.freedesktop.Hal.Device.Volume";

	QDBusConnection c = QDBusConnection::systemBus();
	QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.Hal", udi,
	                                                  interface, "Eject");

	msg << QStringList();

	return c.callWithCallback(msg, this,
	                          SLOT(slotDBusReply(QDBusMessage)),
	                          SLOT(slotDBusError(QDBusError)));
}

bool Solid::Backends::Hal::StorageAccess::callSystemMount()
{
	const QString device = m_device->prop("block.device").toString();
	m_process = FstabHandling::callSystemCommand("mount", device,
	                                             this, SLOT(slotProcessFinished(int, QProcess::ExitStatus)));

	return m_process != nullptr;
}

bool Solid::Backends::Hal::StorageAccess::callSystemUnmount()
{
	const QString device = m_device->prop("block.device").toString();
	m_process = FstabHandling::callSystemCommand("umount", device,
	                                             this, SLOT(slotProcessFinished(int, QProcess::ExitStatus)));

	return m_process != nullptr;
}

void StorageAccess::callCryptoSetup(const QString& passphrase)
{
	QDBusConnection c = QDBusConnection::systemBus();
	QString udi = m_device->udi();
	QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.Hal", udi,
	                                                  "org.freedesktop.Hal.Device.Volume.Crypto",
	                                                  "Setup");

	msg << passphrase;

	c.callWithCallback(msg, this,
	                   SLOT(slotDBusReply(QDBusMessage)),
	                   SLOT(slotDBusError(QDBusError)));
}

bool StorageAccess::callCryptoTeardown()
{
	QDBusConnection c = QDBusConnection::systemBus();
	QString udi = m_device->udi();
	QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.Hal", udi,
	                                                  "org.freedesktop.Hal.Device.Volume.Crypto",
	                                                  "Teardown");

	return c.callWithCallback(msg, this,
	                          SLOT(slotDBusReply(QDBusMessage)),
	                          SLOT(slotDBusError(QDBusError)));
}

#include "backends/hal/moc_halstorageaccess.cpp"