summaryrefslogtreecommitdiffstats
path: root/src/input/privilegedHandlerForwarder.cpp
blob: 17c4ab18e32084333779a0f709db89d7467a50bf (plain) (blame)
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
/*
 # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
 #
 # This program is free software distributed under the GPL version 2.
 # See http://openslx.org/COPYING
 #
 # If you have any feedback please consult http://openslx.org/feedback and
 # send your suggestions, praise, or complaints to feedback@openslx.org
 #
 # General information about OpenSLX can be found at http://openslx.org/
 # --------------------------------------------------------------------------
 # inputHandlerChain.cpp:
 #  - Forward privileged input events to a special handler process - implementation
 # --------------------------------------------------------------------------
 */

#include <sys/socket.h>
#include <sys/un.h>
#include <cassert>
#include <cerrno>
#include <cstring>
#include <string>
#include <QSettings>
#include <QtDebug>
#include "pvsPrivInputSocket.h"
#include "privilegedHandlerForwarder.h"

using namespace std;

#ifndef UNIX_PATH_MAX
# define UNIX_PATH_MAX 108 /* according to unix(7) */
#endif

void PrivilegedHandlerForwarder::initialize()
{
	QSettings settings(QSettings::NativeFormat, QSettings::SystemScope, "openslx", "pvsinputd");

	QString defaultPath = "/tmp/pvsprivinputd.sock";
	QByteArray socketPath = settings.value("socketpath", defaultPath).toString().toLocal8Bit();

	_socket = pvsPrivInputMakeClientSocket();
	if(_socket < 0)
	{
		return;
	}
}

void PrivilegedHandlerForwarder::doHandle(InputEvent const& evt, InputEventContext const*)
{
	qDebug("Trying to handle %s in PrivilegedHandlerForwarder", evt.toString().toLocal8Bit().constData());
	if(_socket < 0)
	{
		initialize();
	}

	QByteArray data;
	QDataStream strm(&data, QIODevice::WriteOnly);
	strm.setByteOrder(QDataStream::BigEndian);
	strm << evt;

	assert(data.size() == 8);

	int delta = 0;
	int count = 0;
	do {
		delta = write(_socket, data.constData(), data.size());
		if(delta < 0)
		{
			qWarning("Error while communicating with pvsprivinputd: %s", strerror(errno));
			close(_socket);

			// Try again:
			initialize();
		}
		else if(delta != 8)
		{
			// This should normally not happen.
			qWarning("Could not send a complete packet. Only %d bytes sent", delta);
		}
		count++;
	} while(delta != 8 && count < 3);
}