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
|
//- writer
internal void dbus_w_init(DBusWriter *w, u8 *buffer, u32 capacity)
{
w->data = buffer;
w->size = 0;
w->capacity = capacity;
w->overflow = false;
}
internal void dbus_w_bytes(DBusWriter *w, const void *bytes, u32 size)
{
if(w->size + size > w->capacity) {
w->overflow = true;
return;
}
MemCpy(w->data + w->size, bytes, size);
w->size += size;
}
internal void dbus_w_align(DBusWriter *w, u32 alignment)
{
static const u8 zeros[8] = {};
u32 padding = (alignment - (w->size & (alignment - 1))) & (alignment - 1);
dbus_w_bytes(w, zeros, padding);
}
internal void dbus_w_u8(DBusWriter *w, u8 value) { dbus_w_bytes(w, &value, 1); }
internal void dbus_w_u32(DBusWriter *w, u32 value) { dbus_w_align(w, 4); dbus_w_bytes(w, &value, 4); }
internal void dbus_w_i32(DBusWriter *w, s32 value) { dbus_w_align(w, 4); dbus_w_bytes(w, &value, 4); }
internal void dbus_w_i64(DBusWriter *w, s64 value) { dbus_w_align(w, 8); dbus_w_bytes(w, &value, 8); }
internal void dbus_w_f64(DBusWriter *w, f64 value) { dbus_w_align(w, 8); dbus_w_bytes(w, &value, 8); }
internal void dbus_w_bool(DBusWriter *w, b32 value) { dbus_w_u32(w, value ? 1 : 0); }
internal void dbus_w_string(DBusWriter *w, String8 value)
{
dbus_w_u32(w, (u32)value.size);
dbus_w_bytes(w, value.data, (u32)value.size);
dbus_w_u8(w, 0);
}
internal void dbus_w_signature(DBusWriter *w, const char *signature)
{
u32 size = (u32)strlen(signature);
dbus_w_u8(w, (u8)size);
dbus_w_bytes(w, signature, size);
dbus_w_u8(w, 0);
}
// NOTE: an array is its byte length, padding up to the first element's alignment (not counted),
// then the elements. the mark remembers where the length goes.
internal u32 dbus_w_array_begin(DBusWriter *w, u32 element_alignment)
{
dbus_w_u32(w, 0);
u32 length_at = w->size - 4;
dbus_w_align(w, element_alignment);
return (length_at << 4) | (element_alignment & 0xF) ; // alignment is 1..8
}
internal void dbus_w_array_end(DBusWriter *w, u32 mark)
{
if(w->overflow) return;
u32 length_at = mark >> 4;
u32 alignment = mark & 0xF;
u32 start = (length_at + 4 + alignment - 1) & ~(alignment - 1);
u32 length = w->size - start;
MemCpy(w->data + length_at, &length, 4);
}
//- reader
internal void dbus_r_align(DBusReader *r, u32 alignment)
{
r->at = (r->at + alignment - 1) & ~(alignment - 1);
if(r->at > r->size) r->bad = true;
}
internal b32 dbus_r_has(DBusReader *r, u32 size)
{
if(r->bad || r->at + size > r->size) {
r->bad = true;
return false;
}
return true;
}
internal u8 dbus_r_u8(DBusReader *r)
{
if(!dbus_r_has(r, 1)) return 0;
return r->data[r->at++];
}
internal u32 dbus_r_u32(DBusReader *r)
{
dbus_r_align(r, 4);
u32 value = 0;
if(dbus_r_has(r, 4)) MemCpy(&value, r->data + r->at, 4), r->at += 4;
return value;
}
internal s64 dbus_r_i64(DBusReader *r)
{
dbus_r_align(r, 8);
s64 value = 0;
if(dbus_r_has(r, 8)) MemCpy(&value, r->data + r->at, 8), r->at += 8;
return value;
}
internal f64 dbus_r_f64(DBusReader *r)
{
dbus_r_align(r, 8);
f64 value = 0;
if(dbus_r_has(r, 8)) MemCpy(&value, r->data + r->at, 8), r->at += 8;
return value;
}
internal String8 dbus_r_string(DBusReader *r)
{
u32 size = dbus_r_u32(r);
if(!dbus_r_has(r, size + 1)) return str8_zero();
String8 value = ToString8Size(r->data + r->at, size);
r->at += size + 1;
return value;
}
internal String8 dbus_r_signature(DBusReader *r)
{
u32 size = dbus_r_u8(r);
if(!dbus_r_has(r, size + 1)) return str8_zero();
String8 value = ToString8Size(r->data + r->at, size);
r->at += size + 1;
return value;
}
internal DBusReader dbus_body(DBusMessage *message)
{
DBusReader r = {};
r.data = message->body;
r.size = message->body_size;
return r;
}
// NOTE: header fields are a(yv): code byte, then a variant. we only need the string-ish ones
// and the reply serial; unknown codes are skipped by their signature.
internal b32 dbus_parse(u8 *data, u32 size, DBusMessage *message)
{
MemSetZero(message, sizeof(*message));
if(size < 16 || data[0] != 'l' || data[3] != 1) return false;
message->type = data[1];
message->flags = data[2];
u32 body_size, fields_size;
MemCpy(&body_size, data + 4, 4);
MemCpy(&message->serial, data + 8, 4);
MemCpy(&fields_size, data + 12, 4);
DBusReader r = {};
r.data = data;
r.size = 16 + fields_size;
r.at = 16;
while(!r.bad && r.at < r.size) {
dbus_r_align(&r, 8);
if(r.at >= r.size) break;
u8 code = dbus_r_u8(&r);
String8 signature = dbus_r_signature(&r);
if(signature.size != 1) return false;
char type = (char)signature.data[0];
if(type == 'u') {
u32 value = dbus_r_u32(&r);
if(code == 5) message->reply_serial = value;
} else if(type == 's' || type == 'o') {
String8 value = dbus_r_string(&r);
if(code == 1) message->path = value;
if(code == 2) message->interface = value;
if(code == 3) message->member = value;
if(code == 4) message->error_name = value;
if(code == 6) message->destination = value;
if(code == 7) message->sender = value;
} else if(type == 'g') {
String8 value = dbus_r_signature(&r);
if(code == 8) message->signature = value;
} else {
return false; // no other header field types exist
}
}
u32 body_at = (16 + fields_size + 7) & ~7u;
if(r.bad || body_at + body_size > size) return false;
message->body = data + body_at;
message->body_size = body_size;
return true;
}
//- connection
internal b32 dbus_socket_address(struct sockaddr_un *address, socklen_t *length)
{
MemSetZero(address, sizeof(*address));
address->sun_family = AF_UNIX;
// NOTE: "unix:path=/run/user/1000/bus,guid=..." (possibly several, ';' separated). without
// the variable (no session manager exported it) the bus is at $XDG_RUNTIME_DIR/bus
const char *env = getenv("DBUS_SESSION_BUS_ADDRESS");
char path[108] = {};
b32 abstract = false;
if(env && env[0]) {
for(const char *entry = env; entry && *entry; entry = strchr(entry, ';') ? strchr(entry, ';') + 1 : NULL) {
if(strncmp(entry, "unix:", 5) != 0) continue;
const char *key = strstr(entry, "path=");
const char *abs = strstr(entry, "abstract=");
const char *end = strchr(entry, ';');
if(key && (!end || key < end)) key += 5;
else if(abs && (!end || abs < end)) key = abs + 9, abstract = true;
else continue;
u32 size = 0;
while(key[size] && key[size] != ',' && key[size] != ';' && size + 1 < sizeof(path)) ++size;
MemCpy(path, key, size);
break;
}
}
if(!path[0]) {
const char *runtime = getenv("XDG_RUNTIME_DIR");
if(!runtime) return false;
snprintf(path, sizeof(path), "%s/bus", runtime);
}
u32 size = (u32)strlen(path);
if(abstract) {
MemCpy(address->sun_path + 1, path, size); // leading 0 byte = abstract namespace
*length = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + 1 + size);
} else {
MemCpy(address->sun_path, path, size + 1);
*length = sizeof(*address);
}
return true;
}
internal b32 dbus_read_line(int fd, char *out, u32 size)
{
u32 at = 0;
while(at + 1 < size) {
char c;
if(read(fd, &c, 1) != 1) return false;
if(c == '\n') break;
out[at++] = c;
}
out[at] = 0;
return true;
}
internal void dbus_close(DBusConnection *bus)
{
if(bus->fd >= 0) close(bus->fd);
bus->fd = -1;
bus->in_size = 0;
}
internal b32 dbus_connect_session(DBusConnection *bus)
{
bus->fd = -1;
bus->serial = 0;
bus->in_size = 0;
struct sockaddr_un address;
socklen_t length = 0;
if(!dbus_socket_address(&address, &length)) return false;
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if(fd < 0) return false;
// NOTE: auth and Hello happen once at startup, blocking, with a timeout so a wedged bus can't
// hang the player
struct timeval timeout = {1, 0};
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
if(connect(fd, (struct sockaddr *)&address, length) != 0) {
close(fd);
return false;
}
//- auth: a nul byte, then EXTERNAL with our uid as hex encoded decimal text
char uid[16], hex[40], line[256];
snprintf(uid, sizeof(uid), "%u", (u32)getuid());
for(u32 index = 0; uid[index]; ++index) snprintf(hex + index * 2, 3, "%02x", (u8)uid[index]);
int size = snprintf(line, sizeof(line), "%cAUTH EXTERNAL %s\r\n", 0, hex);
if(write(fd, line, (size_t)size) != size || !dbus_read_line(fd, line, sizeof(line)) || strncmp(line, "OK ", 3) != 0 ||
write(fd, "BEGIN\r\n", 7) != 7) {
close(fd);
return false;
}
bus->fd = fd;
DBusMessage reply;
if(!dbus_call(bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "Hello", "", NULL, 0, &reply, 1000000000ull)) {
dbus_close(bus);
return false;
}
DBusReader r = dbus_body(&reply);
String8 name = dbus_r_string(&r);
snprintf(bus->unique_name, sizeof(bus->unique_name), Str8Fmt, Str8Arg(name));
return true;
}
internal void dbus_w_field_string(DBusWriter *w, u8 code, char type, const char *value)
{
dbus_w_align(w, 8);
dbus_w_u8(w, code);
char signature[2] = {type, 0};
dbus_w_signature(w, signature);
if(type == 'g') dbus_w_signature(w, value);
else dbus_w_cstring(w, value);
}
internal u32 dbus_send(DBusConnection *bus, u8 type, u8 flags, const char *path, const char *interface, const char *member,
const char *destination, u32 reply_serial, const char *error_name,
const char *body_signature, u8 *body, u32 body_size)
{
if(bus->fd < 0) return 0;
DBusWriter w;
dbus_w_init(&w, bus->out, sizeof(bus->out));
u32 serial = ++bus->serial;
dbus_w_u8(&w, 'l');
dbus_w_u8(&w, type);
dbus_w_u8(&w, flags);
dbus_w_u8(&w, 1);
dbus_w_u32(&w, body_size);
dbus_w_u32(&w, serial);
u32 fields = dbus_w_array_begin(&w, 8);
if(path) dbus_w_field_string(&w, 1, 'o', path);
if(interface) dbus_w_field_string(&w, 2, 's', interface);
if(member) dbus_w_field_string(&w, 3, 's', member);
if(error_name) dbus_w_field_string(&w, 4, 's', error_name);
if(reply_serial) {
dbus_w_align(&w, 8);
dbus_w_u8(&w, 5);
dbus_w_signature(&w, "u");
dbus_w_u32(&w, reply_serial);
}
if(destination) dbus_w_field_string(&w, 6, 's', destination);
if(body_signature && body_signature[0]) dbus_w_field_string(&w, 8, 'g', body_signature);
dbus_w_array_end(&w, fields);
dbus_w_align(&w, 8);
dbus_w_bytes(&w, body, body_size);
if(w.overflow) return 0;
// NOTE: blocking send: messages are tiny and the bus drains its sockets constantly
u32 sent = 0;
while(sent < w.size) {
ssize_t n = send(bus->fd, w.data + sent, w.size - sent, MSG_NOSIGNAL);
if(n < 0 && errno == EINTR) continue;
if(n <= 0) {
dbus_close(bus);
return 0;
}
sent += (u32)n;
}
return serial;
}
internal void dbus_reply(DBusConnection *bus, DBusMessage *call, const char *signature, u8 *body, u32 body_size)
{
if(call->flags & DBUS_FLAG_NO_REPLY_EXPECTED) return;
char destination[256];
snprintf(destination, sizeof(destination), Str8Fmt, Str8Arg(call->sender));
dbus_send(bus, DBUS_METHOD_RETURN, DBUS_FLAG_NO_REPLY_EXPECTED, NULL, NULL, NULL, call->sender.size ? destination : NULL,
call->serial, NULL, signature, body, body_size);
}
internal void dbus_reply_error(DBusConnection *bus, DBusMessage *call, const char *error_name, const char *text)
{
if(call->flags & DBUS_FLAG_NO_REPLY_EXPECTED) return;
u8 body[512];
DBusWriter w;
dbus_w_init(&w, body, sizeof(body));
dbus_w_cstring(&w, text);
char destination[256];
snprintf(destination, sizeof(destination), Str8Fmt, Str8Arg(call->sender));
dbus_send(bus, DBUS_ERROR, DBUS_FLAG_NO_REPLY_EXPECTED, NULL, NULL, NULL, call->sender.size ? destination : NULL,
call->serial, error_name, "s", body, w.size);
}
// NOTE: takes complete messages off the front of the input buffer. returns the size of the one
// at the front, 0 when it isn't complete yet, ~0u when the stream is garbage.
internal u32 dbus_message_size(u8 *data, u32 size)
{
if(size < 16) return 0;
if(data[0] != 'l') return ~0u; // big endian peers: not supported, and not expected here
u32 body_size, fields_size;
MemCpy(&body_size, data + 4, 4);
MemCpy(&fields_size, data + 12, 4);
u64 total = (u64)((16 + fields_size + 7) & ~7u) + body_size;
if(total > DBUS_MESSAGE_MAX) return ~0u;
return (total <= size) ? (u32)total : 0;
}
// NOTE: 1 = read something, 0 = nothing there (non blocking), -1 = the connection is gone
internal s32 dbus_fill(DBusConnection *bus, b32 wait)
{
if(bus->in_size >= sizeof(bus->in)) return -1;
ssize_t got = recv(bus->fd, bus->in + bus->in_size, sizeof(bus->in) - bus->in_size, wait ? 0 : MSG_DONTWAIT);
if(got > 0) {
bus->in_size += (u32)got;
return 1;
}
if(got < 0 && errno == EINTR) return 0;
if(got < 0 && errno == EAGAIN && !wait) return 0;
return -1;
}
internal void dbus_consume(DBusConnection *bus, u32 size)
{
MemMove(bus->in, bus->in + size, bus->in_size - size);
bus->in_size -= size;
}
internal b32 dbus_call(DBusConnection *bus, const char *destination, const char *path, const char *interface, const char *member,
const char *signature, u8 *body, u32 body_size, DBusMessage *reply, u64 timeout_ns)
{
u32 serial = dbus_send(bus, DBUS_METHOD_CALL, 0, path, interface, member, destination, 0, NULL, signature, body, body_size);
if(!serial) return false;
u64 deadline = time_now_ns() + timeout_ns;
while(time_now_ns() < deadline) {
u32 size = dbus_message_size(bus->in, bus->in_size);
if(size == ~0u) return false;
if(size == 0) {
if(dbus_fill(bus, true) < 0) return false;
continue;
}
// NOTE: the reply stays at the front of the buffer for the caller to read. anything
// before it (NameAcquired and friends at startup) is dropped.
if(dbus_parse(bus->in, size, reply) && reply->reply_serial == serial) {
b32 ok = (reply->type == DBUS_METHOD_RETURN);
// copy out of the receive buffer before consuming it
static u8 kept[DBUS_MESSAGE_MAX];
MemCpy(kept, bus->in, size);
dbus_consume(bus, size);
dbus_parse(kept, size, reply);
return ok;
}
dbus_consume(bus, size);
}
return false;
}
internal b32 dbus_poll(DBusConnection *bus, DBusHandler *handler, void *user)
{
if(bus->fd < 0) return false;
for(;;) {
s32 filled = dbus_fill(bus, false);
if(filled < 0) {
dbus_close(bus);
return false;
}
for(;;) {
u32 size = dbus_message_size(bus->in, bus->in_size);
if(size == ~0u) {
dbus_close(bus);
return false;
}
if(size == 0) break;
DBusMessage message;
if(dbus_parse(bus->in, size, &message)) handler(user, bus, &message);
if(bus->fd < 0) return false;
dbus_consume(bus, size);
}
if(filled == 0) return true; // drained the socket
}
}
|