File: DBusMainLoop.cc

package info (click to toggle)
snapper 0.10.6-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,072 kB
  • sloc: cpp: 24,846; ansic: 1,466; sh: 1,410; makefile: 514; python: 127; ruby: 90
file content (350 lines) | stat: -rw-r--r-- 8,324 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
/*
 * Copyright (c) [2012-2015] Novell, Inc.
 *
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License 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 General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, contact Novell, Inc.
 *
 * To contact Novell about this file by physical or electronic mail, you may
 * find current contact information at www.novell.com.
 */


#include <unistd.h>
#include <poll.h>

#include "DBusMainLoop.h"


namespace DBus
{

    MainLoop::Watch::Watch(DBusWatch* dbus_watch)
	: dbus_watch(dbus_watch), enabled(false), fd(-1), events(0)
    {
	enabled = dbus_watch_get_enabled(dbus_watch);
	fd = dbus_watch_get_unix_fd(dbus_watch);

	unsigned int flags = dbus_watch_get_flags(dbus_watch);
	if (flags & DBUS_WATCH_READABLE)
	    events |= POLLIN;
	if (flags & DBUS_WATCH_WRITABLE)
	    events |= POLLOUT;
    }


    MainLoop::Timeout::Timeout(DBusTimeout* dbus_timeout)
	: dbus_timeout(dbus_timeout), enabled(false), interval(0)
    {
	enabled = dbus_timeout_get_enabled(dbus_timeout);
	interval = dbus_timeout_get_interval(dbus_timeout);
    }


    MainLoop::MainLoop(DBusBusType type)
	: Connection(type), idle_timeout(-1)
    {
	if (pipe(wakeup_pipe) != 0)
	    SN_THROW(FatalException());

	if (!dbus_connection_set_watch_functions(conn, add_watch, remove_watch, toggled_watch,
						 this, NULL))
	    SN_THROW(FatalException());

	if (!dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout,
						   toggled_timeout, this, NULL))
	    SN_THROW(FatalException());

	dbus_connection_set_wakeup_main_function(conn, wakeup_main, this, NULL);
    }


    MainLoop::~MainLoop()
    {
	close(wakeup_pipe[0]);
	close(wakeup_pipe[1]);
    }


    void
    DBus::MainLoop::run()
    {
	reset_idle_count();

	while (true)
	{
	    vector<struct pollfd> pollfds;

	    {
		struct pollfd tmp;
		tmp.fd = wakeup_pipe[0];
		tmp.events = POLLIN;
		tmp.revents = 0;
		pollfds.push_back(tmp);
	    }

	    for (vector<Watch>::const_iterator it = watches.begin(); it != watches.end(); ++it)
	    {
		if (it->enabled)
		{
		    struct pollfd tmp;
		    tmp.fd = it->fd;
		    tmp.events = it->events;
		    tmp.revents = 0;
		    pollfds.push_back(tmp);
		}
	    }

	    milliseconds timeout = periodic_timeout();

	    if (idle_timeout.count() >= 0)
	    {
		steady_clock::duration time_left = idle_for() + idle_timeout;
		if (timeout > time_left || timeout.count() < 0)
		    timeout = duration_cast<milliseconds>(time_left);
	    }

	    int r = poll(&pollfds[0], pollfds.size(), timeout.count());
	    if (r == -1)
		SN_THROW(FatalException());

	    periodic();

	    for (vector<struct pollfd>::const_iterator it1 = pollfds.begin(); it1 != pollfds.end(); ++it1)
	    {
		if (it1->fd == wakeup_pipe[0])
		{
		    if (it1->revents & POLLIN)
		    {
			char arbitrary;
			read(wakeup_pipe[0], &arbitrary, 1);
		    }
		}
		else
		{
		    unsigned int flags = 0;

		    if (it1->revents & POLLIN)
			flags |= DBUS_WATCH_READABLE;

		    if (it1->revents & POLLOUT)
			flags |= DBUS_WATCH_WRITABLE;

		    if (flags != 0)
		    {
			// Do not iterate over watches here since calling dbus_watch_handle() can
			// trigger a remove_watch() callback, thus invalidating the iterator.
			// Instead always search for the watch.

			vector<Watch>::const_iterator it2 = find_enabled_watch(it1->fd, it1->events);
			if (it2 != watches.end())
			{
			    boost::lock_guard<boost::mutex> lock(mutex);
			    dbus_watch_handle(it2->dbus_watch, flags);
			}
		    }
		}
	    }

	    DBusMessage* tmp = pop_message();
	    if (tmp)
	    {
		DBus::Message msg(tmp, false);
		dispatch_incoming(msg);
	    }

	    if (idle_timeout.count() >= 0)
	    {
		steady_clock::duration time_left = idle_for() + idle_timeout;
		if (time_left.count() <= 0)
		    break;
	    }
	}
    }


    void
    MainLoop::set_idle_timeout(milliseconds idle_timeout)
    {
	MainLoop::idle_timeout = idle_timeout;
    }


    void
    MainLoop::reset_idle_count()
    {
	last_action = steady_clock::now();
    }


    milliseconds
    MainLoop::idle_for() const
    {
	return duration_cast<milliseconds>(last_action - steady_clock::now());
    }


    vector<MainLoop::Watch>::iterator
    MainLoop::find_watch(DBusWatch* dbus_watch)
    {
	for (vector<Watch>::iterator it = watches.begin(); it != watches.end(); ++it)
	    if (it->dbus_watch == dbus_watch)
		return it;

	SN_THROW(FatalException());
	__builtin_unreachable();
    }


    vector<MainLoop::Watch>::iterator
    MainLoop::find_enabled_watch(int fd, short events)
    {
	for (vector<Watch>::iterator it = watches.begin(); it != watches.end(); ++it)
	    if (it->enabled && it->fd == fd && it->events == events)
		return it;

	return watches.end();
    }


    vector<MainLoop::Timeout>::iterator
    MainLoop::find_timeout(DBusTimeout* dbus_timeout)
    {
	for (vector<Timeout>::iterator it = timeouts.begin(); it != timeouts.end(); ++it)
	    if (it->dbus_timeout == dbus_timeout)
		return it;

	SN_THROW(FatalException());
	__builtin_unreachable();
    }


    dbus_bool_t
    MainLoop::add_watch(DBusWatch* dbus_watch, void* data)
    {
	Watch tmp(dbus_watch);
	MainLoop* s = static_cast<MainLoop*>(data);
	s->watches.push_back(tmp);
	return true;
    }


    void
    MainLoop::remove_watch(DBusWatch* dbus_watch, void* data)
    {
	MainLoop* s = static_cast<MainLoop*>(data);
	vector<Watch>::iterator it = s->find_watch(dbus_watch);
	s->watches.erase(it);
    }


    void
    MainLoop::toggled_watch(DBusWatch* dbus_watch, void* data)
    {
	MainLoop* s = static_cast<MainLoop*>(data);
	vector<Watch>::iterator it = s->find_watch(dbus_watch);
	it->enabled = dbus_watch_get_enabled(dbus_watch);
    }


    dbus_bool_t
    MainLoop::add_timeout(DBusTimeout* dbus_timeout, void* data)
    {
	Timeout tmp(dbus_timeout);
	MainLoop* s = static_cast<MainLoop*>(data);
	s->timeouts.push_back(tmp);
	return true;
    }


    void
    MainLoop::remove_timeout(DBusTimeout* dbus_timeout, void* data)
    {
	MainLoop* s = static_cast<MainLoop*>(data);
	vector<Timeout>::iterator it = s->find_timeout(dbus_timeout);
	s->timeouts.erase(it);
    }


    void
    MainLoop::toggled_timeout(DBusTimeout* dbus_timeout, void* data)
    {
	MainLoop* s = static_cast<MainLoop*>(data);
	vector<Timeout>::iterator it = s->find_timeout(dbus_timeout);
	it->enabled = dbus_timeout_get_enabled(dbus_timeout);
    }


    void
    MainLoop::wakeup_main(void* data)
    {
	MainLoop* s = static_cast<MainLoop*>(data);
	const char arbitrary = 42;
	write(s->wakeup_pipe[1], &arbitrary, 1);
    }


    void
    DBus::MainLoop::add_client_match(const string& name)
    {
	// Filtering for the sender doesn't work for me. So also check the
	// sender later when handling the signal.
	add_match("type='signal', sender='" DBUS_SERVICE_DBUS "', path='" DBUS_PATH_DBUS "', "
		  "interface='" DBUS_INTERFACE_DBUS "', member='NameOwnerChanged', "
		  "arg0='" + name + "'");
    }


    void
    DBus::MainLoop::remove_client_match(const string& name)
    {
	// Filtering for the sender doesn't work for me. So also check the
	// sender later when handling the signal.
	remove_match("type='signal', sender='" DBUS_SERVICE_DBUS "', path='" DBUS_PATH_DBUS "', "
		     "interface='" DBUS_INTERFACE_DBUS "', member='NameOwnerChanged', "
		     "arg0='" + name + "'");
    }


    void
    DBus::MainLoop::dispatch_incoming(Message& msg)
    {
	switch (msg.get_type())
	{
	    case DBUS_MESSAGE_TYPE_METHOD_CALL:
	    {
		method_call(msg);
	    }
	    break;

	    case DBUS_MESSAGE_TYPE_SIGNAL:
	    {
		signal(msg);

		if (msg.get_sender() == DBUS_SERVICE_DBUS &&
		    msg.is_signal(DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
		{
		    string name, old_owner, new_owner;

		    DBus::Unmarshaller unmarshaller(msg);
		    unmarshaller >> name >> old_owner >> new_owner;

		    if (name == old_owner && new_owner.empty())
			client_disconnected(name);
		}
	    }
	    break;
	}
    }

}