This repository was archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathsol-bus.c
More file actions
1016 lines (787 loc) · 24.9 KB
/
sol-bus.c
File metadata and controls
1016 lines (787 loc) · 24.9 KB
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the Soletta Project
*
* Copyright (C) 2015 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-event.h>
#include "sol-bus.h"
#include "sol-mainloop.h"
#include "sol-platform-impl.h"
#include "sol-util-internal.h"
#include "sol-vector.h"
#define SERVICE_NAME_OWNER_MATCH "type='signal'," \
"sender='org.freedesktop.DBus'," \
"path='/org/freedesktop/DBus'," \
"interface='org.freedesktop.DBus'," \
"member='NameOwnerChanged'," \
"arg0='%s'"
#define INTERFACES_ADDED_MATCH "type='signal'," \
"sender='%s'," \
"interface='org.freedesktop.DBus.ObjectManager'," \
"member='InterfacesAdded'"
struct property_table {
const struct sol_bus_properties *properties;
const void *data;
void (*changed)(void *data, const char *path, uint64_t mask);
sd_bus_slot *getall_slot;
char *iface;
char *path;
};
struct ctx {
struct sol_mainloop_source *mainloop_source;
sd_bus *bus;
sd_event_source *ping;
struct sol_ptr_vector clients;
bool exiting;
};
struct sol_bus_client {
sd_bus *bus;
char *service;
struct sol_ptr_vector property_tables;
const struct sol_bus_interfaces *interfaces;
const void *interfaces_data;
sd_bus_slot *name_changed;
sd_bus_slot *managed_objects;
sd_bus_slot *interfaces_added;
sd_bus_slot *properties_changed;
sd_bus_slot *name_owner_slot;
void (*connect)(void *data, const char *unique);
const void *connect_data;
void (*disconnect)(void *data);
const void *disconnect_data;
};
static struct ctx _ctx;
struct source_ctx {
struct sd_event *event;
struct sol_fd *fd_handler;
};
static bool
source_prepare(void *data)
{
struct source_ctx *s = data;
return sd_event_prepare(s->event) > 0;
}
static bool
source_check(void *data)
{
struct source_ctx *s = data;
return sd_event_wait(s->event, 0) > 0;
}
static void
source_dispatch(void *data)
{
struct source_ctx *s = data;
sd_event_dispatch(s->event);
}
static void
source_dispose(void *data)
{
struct source_ctx *s = data;
sd_event_unref(s->event);
sol_fd_del(s->fd_handler);
free(s);
}
static const struct sol_mainloop_source_type source_type = {
SOL_SET_API_VERSION(.api_version = SOL_MAINLOOP_SOURCE_TYPE_API_VERSION, )
.prepare = source_prepare,
.check = source_check,
.dispatch = source_dispatch,
.dispose = source_dispose,
};
static bool
on_sd_event_fd(void *data, int fd, uint32_t active_flags)
{
/* just used to wake up main loop */
return true;
}
static struct sol_mainloop_source *
event_create_source(sd_event *event)
{
struct sol_mainloop_source *source;
struct source_ctx *ctx;
ctx = malloc(sizeof(*ctx));
SOL_NULL_CHECK(ctx, NULL);
ctx->event = sd_event_ref(event);
ctx->fd_handler = sol_fd_add(sd_event_get_fd(event),
SOL_FD_FLAGS_IN | SOL_FD_FLAGS_HUP | SOL_FD_FLAGS_ERR,
on_sd_event_fd, ctx);
SOL_NULL_CHECK_GOTO(ctx->fd_handler, error_fd);
source = sol_mainloop_add_source(&source_type, ctx);
SOL_NULL_CHECK_GOTO(source, error_source);
return source;
error_source:
sol_fd_del(ctx->fd_handler);
error_fd:
sd_event_unref(ctx->event);
free(ctx);
return NULL;
}
static int
_event_mainloop_running(sd_event_source *event_source, void *userdata)
{
struct ctx *ctx = userdata;
SOL_DBG("systemd's mainloop running");
sd_event_source_unref(ctx->ping);
ctx->ping = NULL;
return 0;
}
static int
event_attach_mainloop(void)
{
int r;
sd_event *e = NULL;
if (_ctx.mainloop_source)
return 0;
r = sd_event_default(&e);
if (r < 0)
return r;
_ctx.mainloop_source = event_create_source(e);
if (!_ctx.mainloop_source)
goto fail;
sd_event_add_defer(e, &_ctx.ping, _event_mainloop_running, &_ctx);
return 0;
fail:
sd_event_unref(e);
return -ENOMEM;
}
static int
_match_disconnected(sd_bus_message *m, void *userdata,
sd_bus_error *error)
{
struct ctx *ctx = userdata;
if (!ctx->exiting) {
SOL_WRN("D-Bus connection terminated: %s. Exiting.",
error && error->message ? error->message : "(unknown reason)");
sol_quit();
}
return 0;
}
static int
connect_bus(void)
{
int r;
sd_bus *bus = NULL;
struct source_ctx *s;
r = sd_bus_default_system(&bus);
SOL_INT_CHECK(r, < 0, r);
s = sol_mainloop_source_get_data(_ctx.mainloop_source);
r = sd_bus_attach_event(bus, s->event,
SD_EVENT_PRIORITY_NORMAL);
SOL_INT_CHECK_GOTO(r, < 0, fail);
r = sd_bus_add_match(bus, NULL,
"type='signal',"
"sender='org.freedesktop.DBus.Local',"
"interface='org.freedesktop.DBus.Local',"
"member='Disconnected'",
_match_disconnected, &_ctx);
SOL_INT_CHECK_GOTO(r, < 0, fail);
_ctx.bus = bus;
return 0;
fail:
sd_bus_unref(bus);
return r;
}
/* We attach libsystemd's mainloop to Soletta's on the first time we need to
* connect to the bus. Any fail on getting connected to the bus means the
* mainloop terminates.
*/
SOL_API sd_bus *
sol_bus_get(int (*bus_initialized)(sd_bus *bus))
{
int r;
if (!_ctx.bus) {
if (!_ctx.mainloop_source) {
r = event_attach_mainloop();
SOL_INT_CHECK_GOTO(r, < 0, fail);
}
r = connect_bus();
SOL_INT_CHECK_GOTO(r, < 0, fail);
sol_ptr_vector_init(&_ctx.clients);
}
if (bus_initialized) {
r = bus_initialized(_ctx.bus);
SOL_INT_CHECK_GOTO(r, < 0, exit);
}
return _ctx.bus;
fail:
SOL_WRN("D-Bus requested but connection could not be made");
sol_quit();
exit:
return NULL;
}
static void
destroy_property_table(struct property_table *table)
{
sd_bus_slot_unref(table->getall_slot);
free(table->path);
free(table->iface);
free(table);
}
static void
destroy_client(struct sol_bus_client *client)
{
struct property_table *t;
uint16_t i;
SOL_PTR_VECTOR_FOREACH_IDX (&client->property_tables, t, i) {
destroy_property_table(t);
}
sol_ptr_vector_clear(&client->property_tables);
client->name_changed = sd_bus_slot_unref(client->name_changed);
client->managed_objects = sd_bus_slot_unref(client->managed_objects);
client->interfaces_added = sd_bus_slot_unref(client->interfaces_added);
client->bus = sd_bus_unref(client->bus);
free(client->service);
free(client);
}
SOL_API void
sol_bus_close(void)
{
_ctx.exiting = true;
if (_ctx.bus) {
struct sol_bus_client *c;
uint16_t i;
SOL_PTR_VECTOR_FOREACH_IDX (&_ctx.clients, c, i) {
destroy_client(c);
}
sol_ptr_vector_clear(&_ctx.clients);
sd_bus_flush(_ctx.bus);
sd_bus_close(_ctx.bus);
sd_bus_unref(_ctx.bus);
_ctx.bus = NULL;
}
if (_ctx.mainloop_source) {
struct source_ctx *s;
sd_event_source_unref(_ctx.ping);
s = sol_mainloop_source_get_data(_ctx.mainloop_source);
sd_event_unref(s->event);
sol_mainloop_del_source(_ctx.mainloop_source);
_ctx.mainloop_source = NULL;
}
}
SOL_API struct sol_bus_client *
sol_bus_client_new(sd_bus *bus, const char *service)
{
struct sol_bus_client *client;
size_t len;
int r;
SOL_NULL_CHECK(bus, NULL);
SOL_NULL_CHECK(service, NULL);
/* D-Bus specifies that the maximum service name length is 255 bytes */
len = strlen(service);
SOL_INT_CHECK(len, >= 255, NULL);
client = calloc(1, sizeof(struct sol_bus_client));
SOL_NULL_CHECK(client, NULL);
client->bus = sd_bus_ref(bus);
client->service = strdup(service);
SOL_NULL_CHECK_GOTO(client->service, fail);
sol_ptr_vector_init(&client->property_tables);
r = sol_ptr_vector_append(&_ctx.clients, client);
SOL_INT_CHECK_GOTO(r, < 0, fail);
return client;
fail:
destroy_client(client);
return NULL;
}
SOL_API void
sol_bus_client_free(struct sol_bus_client *client)
{
if (!client)
return;
sol_ptr_vector_remove(&_ctx.clients, client);
destroy_client(client);
}
SOL_API const char *
sol_bus_client_get_service(struct sol_bus_client *client)
{
SOL_NULL_CHECK(client, NULL);
return client->service;
}
SOL_API sd_bus *
sol_bus_client_get_bus(struct sol_bus_client *client)
{
SOL_NULL_CHECK(client, NULL);
return client->bus;
}
static int
_message_map_all_properties(sd_bus_message *m,
const struct property_table *t, sd_bus_error *ret_error)
{
uint64_t mask = 0;
int r;
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
SOL_INT_CHECK(r, < 0, r);
do {
const struct sol_bus_properties *iter;
const char *member, *contents;
char type;
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv");
if (r <= 0) {
r = 0;
break;
}
r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
SOL_INT_CHECK_GOTO(r, < 0, end);
for (iter = t->properties; iter->member; iter++) {
if (streq(iter->member, member))
break;
}
r = sd_bus_message_peek_type(m, &type, &contents);
SOL_INT_CHECK_GOTO(r, < 0, end);
if (iter->member) {
bool changed;
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
SOL_INT_CHECK_GOTO(r, < 0, end);
changed = iter->set((void *)t->data, t->path, m);
if (changed)
mask |= 1 << (iter - t->properties);
r = sd_bus_message_exit_container(m);
SOL_INT_CHECK_GOTO(r, < 0, end);
} else {
r = sd_bus_message_skip(m, "v");
SOL_INT_CHECK_GOTO(r, < 0, end);
}
r = sd_bus_message_exit_container(m);
SOL_INT_CHECK_GOTO(r, < 0, end);
} while (1);
end:
if (mask > 0)
t->changed((void *)t->data, t->path, mask);
if (r == 0)
r = sd_bus_message_exit_container(m);
return r;
}
static const struct property_table *
find_property_table(struct sol_bus_client *client,
const char *iface, const char *path)
{
struct property_table *t;
uint16_t i;
SOL_PTR_VECTOR_FOREACH_IDX (&client->property_tables, t, i) {
if (streq(t->iface, iface) && streq(t->path, path))
return t;
}
return NULL;
}
static int
_match_properties_changed(sd_bus_message *m, void *userdata,
sd_bus_error *ret_error)
{
struct sol_bus_client *client = userdata;
const struct property_table *t;
const char *path;
const char *iface;
int r;
path = sd_bus_message_get_path(m);
SOL_NULL_CHECK(path, -EINVAL);
r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &iface);
SOL_INT_CHECK(r, < 0, r);
t = find_property_table(client, iface, path);
if (!t) {
SOL_DBG("Property table not found for iface %s path %s", iface, path);
return 0;
}
/* Ignore PropertiesChanged signals until the GetAll() method returns */
if (t->getall_slot)
return 0;
r = _message_map_all_properties(m, t, ret_error);
SOL_INT_CHECK(r, < 0, r);
/* Ignore invalidated properties */
return 0;
}
static int
_getall_properties(sd_bus_message *reply, void *userdata,
sd_bus_error *ret_error)
{
struct property_table *t = userdata;
t->getall_slot = sd_bus_slot_unref(t->getall_slot);
if (sol_bus_log_callback(reply, userdata, ret_error) < 0)
return 0;
return _message_map_all_properties(reply, t, ret_error);
}
SOL_API int
sol_bus_map_cached_properties(struct sol_bus_client *client,
const char *path, const char *iface,
const struct sol_bus_properties property_table[],
void (*changed)(void *data, const char *path, uint64_t mask),
const void *data)
{
sd_bus_message *m = NULL;
const struct sol_bus_properties *iter_desc;
struct property_table *t;
char matchstr[512];
int r;
SOL_NULL_CHECK(client, -EINVAL);
/* Make sure uint64_t is sufficient to notify state changes - we only
* support at most 64 properties */
for (iter_desc = property_table; iter_desc->member != NULL;)
iter_desc++;
SOL_INT_CHECK(iter_desc - property_table, >= (int)sizeof(uint64_t) * CHAR_BIT, -ENOBUFS);
t = calloc(1, sizeof(*t));
SOL_NULL_CHECK(t, -ENOMEM);
t->iface = strdup(iface);
if (SOL_UNLIKELY(!t->iface)) {
r = -errno;
SOL_WRN("t->iface == NULL");
goto fail;
}
t->path = strdup(path);
if (SOL_UNLIKELY(!t->path)) {
r = -errno;
SOL_WRN("t->path == NULL");
goto fail;
}
t->properties = property_table;
t->data = data;
t->changed = changed;
if (!client->properties_changed) {
r = snprintf(matchstr, sizeof(matchstr),
"type='signal',"
"sender='%s',"
"interface='org.freedesktop.DBus.Properties',"
"member='PropertiesChanged'",
client->service);
SOL_INT_CHECK_GOTO(r, < 0, fail);
r = sd_bus_add_match(client->bus, &client->properties_changed, matchstr,
_match_properties_changed, client);
SOL_INT_CHECK_GOTO(r, < 0, fail);
}
r = sol_ptr_vector_append(&client->property_tables, t);
SOL_INT_CHECK_GOTO(r, < 0, fail);
if (client->managed_objects)
return 0;
r = sd_bus_message_new_method_call(client->bus, &m, client->service, path,
"org.freedesktop.DBus.Properties",
"GetAll");
SOL_INT_CHECK_GOTO(r, < 0, fail_getall);
r = sd_bus_message_append(m, "s", iface);
SOL_INT_CHECK_GOTO(r, < 0, fail_getall);
r = sd_bus_call_async(client->bus, &t->getall_slot, m,
_getall_properties, t, 0);
SOL_INT_CHECK_GOTO(r, < 0, fail_getall);
sd_bus_message_unref(m);
return 0;
fail_getall:
sd_bus_message_unref(m);
sol_ptr_vector_del(&client->property_tables,
sol_ptr_vector_get_len(&client->property_tables) - 1);
fail:
destroy_property_table(t);
return r;
}
SOL_API int
sol_bus_unmap_cached_properties(struct sol_bus_client *client,
const struct sol_bus_properties property_table[],
const void *data)
{
struct property_table *t, *found = NULL;
uint16_t i;
SOL_PTR_VECTOR_FOREACH_IDX (&client->property_tables, t, i) {
if (t->properties == property_table && t->data == data) {
found = t;
break;
}
}
SOL_NULL_CHECK(found, -ENOENT);
sol_ptr_vector_del(&client->property_tables, i);
destroy_property_table(found);
return 0;
}
static int
name_owner_changed(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
{
struct sol_bus_client *client = userdata;
const char *name, *old, *new;
if (sd_bus_message_read(m, "sss", &name, &old, &new) < 0)
return 0;
if (new && *new && client->connect) {
/* Assuming that when a name is replaced, calling 'connected()' is
* the right thing to do.
*/
client->connect((void *)client->connect_data, new);
return 0;
}
if (client->disconnect)
client->disconnect((void *)client->disconnect_data);
return 0;
}
static const struct sol_bus_interfaces *
find_interface(const struct sol_bus_client *client, const char *iface)
{
const struct sol_bus_interfaces *s;
for (s = client->interfaces; s && s->name; s++) {
if (streq(iface, s->name))
return s;
}
return NULL;
}
static bool
filter_device_properties(sd_bus_message *m, const char *iface, const char *path,
struct sol_bus_client *client, sd_bus_error *ret_error)
{
const struct property_table *t;
t = find_property_table(client, iface, path);
if (!t) {
sd_bus_message_skip(m, "a{sv}");
return false;
}
_message_map_all_properties(m, t, ret_error);
return true;
}
static void
filter_interfaces(struct sol_bus_client *client, sd_bus_message *m, sd_bus_error *ret_error)
{
const char *path;
int r;
r = sd_bus_message_read(m, "o", &path);
SOL_INT_CHECK(r, < 0);
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sa{sv}}");
SOL_INT_CHECK(r, < 0);
do {
const struct sol_bus_interfaces *s;
const char *iface;
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sa{sv}");
if (r == -ENXIO)
break;
SOL_INT_CHECK(r, < 0);
r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &iface);
SOL_INT_CHECK(r, < 0);
s = find_interface(client, iface);
if (s && s->appeared)
s->appeared((void *)client->interfaces_data, path);
filter_device_properties(m, iface, path, client, ret_error);
r = sd_bus_message_exit_container(m);
SOL_INT_CHECK(r, < 0);
} while (1);
}
static int
interfaces_added_cb(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
{
struct sol_bus_client *client = userdata;
if (sol_bus_log_callback(m, userdata, ret_error))
return -EINVAL;
filter_interfaces(client, m, ret_error);
return 0;
}
static int
managed_objects_cb(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
{
struct sol_bus_client *client = userdata;
int err;
client->managed_objects = sd_bus_slot_unref(client->managed_objects);
if (sol_bus_log_callback(m, userdata, ret_error)) {
err = -EINVAL;
goto end;
}
if (sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{oa{sa{sv}}}") < 0) {
err = -EINVAL;
goto end;
}
while (sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "oa{sa{sv}}") > 0) {
filter_interfaces(client, m, ret_error);
if (sd_bus_message_exit_container(m) < 0) {
err = -EINVAL;
goto end;
}
}
if (sd_bus_message_exit_container(m) < 0) {
err = -EINVAL;
goto end;
}
err = 0;
end:
return err;
}
SOL_API int
sol_bus_watch_interfaces(struct sol_bus_client *client,
const struct sol_bus_interfaces interfaces[],
const void *data)
{
sd_bus_message *m = NULL;
const struct sol_bus_interfaces *iter;
char matchstr[512];
int r;
/* One set of 'interfaces' per client. Too limited? */
if (client->interfaces)
return -EALREADY;
for (iter = interfaces; iter->name;)
iter++;
SOL_INT_CHECK(iter - interfaces, >= (int)sizeof(uint64_t) * CHAR_BIT, -ENOBUFS);
r = snprintf(matchstr, sizeof(matchstr), INTERFACES_ADDED_MATCH, client->service);
SOL_INT_CHECK(r, < 0, -ENOMEM);
client->interfaces = interfaces;
client->interfaces_data = data;
if (client->interfaces_added)
return 0;
r = sd_bus_add_match(client->bus, &client->interfaces_added,
matchstr, interfaces_added_cb, client);
SOL_INT_CHECK(r, < 0, -ENOMEM);
if (client->managed_objects)
return 0;
r = sd_bus_message_new_method_call(client->bus, &m, client->service, "/",
"org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
SOL_INT_CHECK_GOTO(r, < 0, error_message);
r = sd_bus_call_async(client->bus, &client->managed_objects,
m, managed_objects_cb, client, 0);
SOL_INT_CHECK_GOTO(r, < 0, error_call);
sd_bus_message_unref(m);
return 0;
error_call:
sd_bus_message_unref(m);
error_message:
client->interfaces_added = sd_bus_slot_unref(client->interfaces_added);
return -EINVAL;
}
SOL_API int
sol_bus_remove_interfaces_watch(struct sol_bus_client *client,
const struct sol_bus_interfaces interfaces[],
const void *data)
{
if (client->interfaces != interfaces || client->interfaces_data != data)
return -ENODATA;
client->interfaces = NULL;
client->interfaces_data = NULL;
return 0;
}
static sd_bus_slot *
add_name_owner_watch(struct sol_bus_client *client,
sd_bus_message_handler_t cb, void *userdata)
{
sd_bus_slot *slot = NULL;
char matchstr[512];
int r;
r = snprintf(matchstr, sizeof(matchstr), SERVICE_NAME_OWNER_MATCH, client->service);
SOL_INT_CHECK(r, < 0, NULL);
r = sd_bus_add_match(client->bus, &slot, matchstr, cb, userdata);
SOL_INT_CHECK(r, < 0, NULL);
return slot;
}
static int
get_name_owner_reply_cb(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
{
struct sol_bus_client *client = userdata;
const char *unique;
int r;
client->name_owner_slot = sd_bus_slot_unref(client->name_owner_slot);
/* Do not error because the service may not exist yet. */
if (sd_bus_message_is_method_error(m, NULL)) {
const sd_bus_error *error = sd_bus_message_get_error(m);
SOL_DBG("Failed method call: %s: %s", error->name, error->message);
return 0;
}
r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &unique);
SOL_INT_CHECK(r, < 0, -EINVAL);
if (client->connect)
client->connect((void *)client->connect_data, unique);
return 0;
}
SOL_API int
sol_bus_client_set_connect_handler(struct sol_bus_client *client,
void (*connect)(void *data, const char *unique),
const void *data)
{
SOL_NULL_CHECK(client, -EINVAL);
client->connect = connect;
client->connect_data = data;
if (client->name_changed)
return 0;
client->name_changed = add_name_owner_watch(client, name_owner_changed, client);
SOL_NULL_CHECK(client->name_changed, -ENOMEM);
/* In case the name is already present in the bus. */
sd_bus_call_method_async(sol_bus_client_get_bus(client),
&client->name_owner_slot, "org.freedesktop.DBus",
"/", "org.freedesktop.DBus", "GetNameOwner",
get_name_owner_reply_cb, client, "s", sol_bus_client_get_service(client));
return 0;
}
SOL_API int
sol_bus_client_set_disconnect_handler(struct sol_bus_client *client,
void (*disconnect)(void *data),
const void *data)
{
SOL_NULL_CHECK(client, -EINVAL);
client->disconnect = disconnect;
client->disconnect_data = data;
if (client->name_changed)
return 0;
client->name_changed = add_name_owner_watch(client, name_owner_changed, client);
SOL_NULL_CHECK(client->name_changed, -ENOMEM);
return 0;
}
SOL_API int
sol_bus_log_callback(sd_bus_message *reply, void *userdata,
sd_bus_error *ret_error)
{
const sd_bus_error *error;
if (!sd_bus_message_is_method_error(reply, NULL))
return 0;
error = sd_bus_message_get_error(reply);
SOL_WRN("Failed method call: %s: %s", error->name, error->message);
return -1;
}
SOL_API int
sol_bus_parse_dict(sd_bus_message *m, const struct sol_bus_dict_entry *dict)
{
int r;
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
SOL_INT_CHECK(r, < 0, r);
do {
const struct sol_bus_dict_entry *entry;
const char *member, *contents;
char type;
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv");
if (r <= 0) {
r = 0;
break;
}
r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
SOL_INT_CHECK_GOTO(r, < 0, end);
r = sd_bus_message_peek_type(m, &type, &contents);
SOL_INT_CHECK_GOTO(r, < 0, end);
for (entry = dict; entry && entry->name; entry++) {
if (streq(entry->name, member))
break;
}
if (entry->name) {
size_t len;
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
SOL_INT_CHECK_GOTO(r, < 0, end);
len = strlen(contents);
if (entry->type == 'v' && entry->parse_variant)
r = entry->parse_variant(m, entry->value);
else if (len == 1 && entry->type == contents[0])
r = sd_bus_message_read_basic(m, entry->type, entry->value);
else {
SOL_WRN("Invalid type in message '%s', expecting '%c'",
contents, entry->type);
r = -EINVAL;
}