-
Notifications
You must be signed in to change notification settings - Fork 9
/
mce-dbus.c
5472 lines (4541 loc) · 147 KB
/
mce-dbus.c
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
/**
* @file mce-dbus.c
* D-Bus handling code for the Mode Control Entity
* <p>
* Copyright (c) 2004 - 2009 Nokia Corporation and/or its subsidiary(-ies).
* Copyright (c) 2012 - 2023 Jolla Ltd.
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
* @author Ismo Laitinen <ismo.laitinen@nokia.com>
* @author Santtu Lakkala <ext-santtu.1.lakkala@nokia.com>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce 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 mce. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mce-dbus.h"
#include "mce.h"
#include "mce-log.h"
#include "mce-lib.h"
#include "mce-wakelock.h"
#include "systemui/dbus-names.h"
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <grp.h>
#include <pwd.h>
#include "dbus-gmain/dbus-gmain.h"
#include <mce/dbus-names.h>
#include <dsme/thermalmanager_dbus_if.h>
/* ========================================================================= *
* TYPES & CONSTANTS
* ========================================================================= */
/** How long to block late suspend when mce is sending dbus messages */
#define MCE_DBUS_SEND_SUSPEND_BLOCK_MS 1000
/** Placeholder value for invalid/unknown process id */
#define PEERINFO_NO_PID ((pid_t)-1)
/** Placeholder value for invalid/unknown user id */
#define PEERINFO_NO_UID ((uid_t)-1)
/** Placeholder value for invalid/unknown group id */
#define PEERINFO_NO_GID ((gid_t)-1)
/** Root user id */
#define PEERINFO_ROOT_UID ((uid_t)0)
/** Root group id */
#define PEERINFO_ROOT_GID ((gid_t)0)
/** D-Bus handler callback function */
typedef gboolean (*handler_callback_t)(DBusMessage *const msg);
/** D-Bus handler structure */
typedef struct
{
handler_callback_t callback; /**< Handler callback */
gchar *sender; /**< Service name to match */
gchar *interface; /**< The interface to listen on */
gchar *rules; /**< Additional matching rules */
gchar *name; /**< Method call or signal name */
gchar *args; /**< Introspect XML data */
int type; /**< DBUS_MESSAGE_TYPE */
bool privileged; /**< Allowed for privileged users only */
} handler_struct_t;
/** Possible values for "privileged" peer checks */
typedef enum
{
/** Client/service is not available / details are not known yet */
PRIVILEGED_UNKNOWN = -1,
/** Client/service is on D-Bus and it is not a privileged process */
PRIVILEGED_NO,
/** Client/service is on D-Bus and it is a privileged process */
PRIVILEGED_YES,
} privileged_t;
/** Notification details for client exit tracking */
typedef struct peerquit_t peerquit_t;
/** Callback function type for notifying client exits */
typedef gboolean (*peerquit_fn)(DBusMessage *const msg);
struct peerquit_t
{
peerinfo_t *pq_peerinfo;
peerquit_fn pq_callback;
};
/** Notification details for client state tracking */
typedef struct peernotify_t peernotify_t;
struct peernotify_t
{
peerinfo_t *pn_peerinfo;
peernotify_fn pn_callback;
gpointer pn_userdata;
GDestroyNotify pn_userfree;
guint pn_idle_id;
};
struct peerinfo_t
{
/** Availability / property query IPC state */
peerstate_t pi_state;
/** D-Bus name to track */
gchar *pi_name;
/** Unique bus name of the name owner */
gchar *pi_owner_name;
/** Signal match that has been sent to D-Bus daemon */
gchar *pi_rule;
/** Process id of sandbox xdg-dbus-proxy */
pid_t pi_proxy_pid;
/** Process id of the D-Bus name owner */
pid_t pi_owner_pid;
/** Cached effective user id of the D-Bus name owner */
uid_t pi_owner_uid;
/** Cached effective group id of the D-Bus name owner */
gid_t pi_owner_gid;
/** Cached command line of the Bus name owner */
gchar *pi_owner_cmd;
/** Optional datapipe to use for service availability signaling */
datapipe_t *pi_datapipe;
/** Client exit notifications for this D-Bus name */
GQueue pi_quit_callbacks; // -> peerquit_t *
/** Client stat notifications for this D-Bus name */
GQueue pi_notifications; // -> peernotify_t
/** Queue of privileged method calls waiting for peer details */
GQueue pi_priv_methods; // -> DBusMessage *
/** Pending org.freedesktop.DBus.GetNameOwner method call */
DBusPendingCall *pi_name_owner_pc;
/** Pending org.freedesktop.DBus.GetConnectionUnixProcessID method call */
DBusPendingCall *pi_name_pid_pc;
/** Pending org.sailfishos.sailjailed.Identify method call */
DBusPendingCall *pi_identify_pc;
/** Timer for delayed delete after hitting PEERSTATE_STOPPED */
guint pi_delete_id;
/** Fixed size buffer for providing peer details for debugging purposes */
char pi_repr[128];
};
/* ========================================================================= *
* FUNCTIONALITY
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* SUSPEND_PROOFING
* ------------------------------------------------------------------------- */
static void mdb_callgate_detach_cb (void *aptr);
static void mdb_callgate_attach (DBusPendingCall *pc);
void mce_dbus_pending_call_blocks_suspend (DBusPendingCall *pc);
/* ------------------------------------------------------------------------- *
* DEBUG_HELPERS
* ------------------------------------------------------------------------- */
static dbus_bool_t mce_dbus_message_repr_any (FILE *file, DBusMessageIter *iter);
char *mce_dbus_message_iter_repr (DBusMessageIter *iter);
char *mce_dbus_message_repr (DBusMessage *const msg);
/* ------------------------------------------------------------------------- *
* HANDLER_STRUCT_T
* ------------------------------------------------------------------------- */
static inline void handler_struct_set_sender (handler_struct_t *self, const char *val);
static inline void handler_struct_set_type (handler_struct_t *self, int val);
static inline void handler_struct_set_interface (handler_struct_t *self, const char *val);
static inline void handler_struct_set_args (handler_struct_t *self, const char *val);
static inline void handler_struct_set_name (handler_struct_t *self, const char *val);
static inline void handler_struct_set_rules (handler_struct_t *self, const char *val);
static inline void handler_struct_set_callback (handler_struct_t *self, handler_callback_t val);
static inline void handler_struct_set_privileged (handler_struct_t *self, bool val);
static void handler_struct_delete (handler_struct_t *self);
static handler_struct_t *handler_struct_create (void);
/* ------------------------------------------------------------------------- *
* PEERSTATE_T
* ------------------------------------------------------------------------- */
const char *peerstate_repr (peerstate_t state);
/* ------------------------------------------------------------------------- *
* PEERQUIT_T
* ------------------------------------------------------------------------- */
static peerquit_t *peerquit_create (peerinfo_t *parent, peerquit_fn callback);
static void peerquit_delete (peerquit_t *self);
static void peerquit_notify (peerquit_t *self, DBusMessage *msg);
/* ------------------------------------------------------------------------- *
* PEERNOTIFY_T
* ------------------------------------------------------------------------- */
static gboolean peernotify_idle_cb (gpointer aptr);
static void peernotify_schedule (peernotify_t *self);
static void peernotify_unschedule (peernotify_t *self);
static void peernotify_execute (peernotify_t *self);
static peernotify_t *peernotify_create (peerinfo_t *peerinfo, peernotify_fn callback, gpointer userdata, GDestroyNotify userfree);
static void peernotify_delete (peernotify_t *self);
/* ------------------------------------------------------------------------- *
* PEERINFO_T
* ------------------------------------------------------------------------- */
static gchar *peerinfo_guess_cmd (pid_t pid);
static inline void peerinfo_ctor (peerinfo_t *self, const char *name);
static inline void peerinfo_dtor (peerinfo_t *self);
peerinfo_t *peerinfo_create (const char *name);
void peerinfo_delete (peerinfo_t *self);
void peerinfo_delete_cb (void *self);
static void peerinfo_enter_state (peerinfo_t *self);
static void peerinfo_leave_state (peerinfo_t *self);
static void peerinfo_set_state (peerinfo_t *self, peerstate_t state);
peerstate_t peerinfo_get_state (const peerinfo_t *self);
static const char *peerinfo_repr (peerinfo_t *self);
const char *peerinfo_name (const peerinfo_t *self);
const char *peerinfo_get_owner_name (const peerinfo_t *self);
static bool peerinfo_set_owner_name (peerinfo_t *self, const char *name);
pid_t peerinfo_get_proxy_pid (const peerinfo_t *self);
static void peerinfo_set_proxy_pid (peerinfo_t *self, pid_t pid);
pid_t peerinfo_get_owner_pid (const peerinfo_t *self);
static void peerinfo_set_owner_pid (peerinfo_t *self, pid_t pid);
uid_t peerinfo_get_owner_uid (const peerinfo_t *self);
static void peerinfo_set_owner_uid (peerinfo_t *self, uid_t uid);
gid_t peerinfo_get_owner_gid (const peerinfo_t *self);
static void peerinfo_set_owner_gid (peerinfo_t *self, gid_t gid);
const char *peerinfo_get_owner_cmd (const peerinfo_t *self);
static void peerinfo_set_owner_cmd (peerinfo_t *self, const char *cmd);
static privileged_t peerinfo_get_privileged (const peerinfo_t *self, bool no_caching);
static void peerinfo_set_datapipe (peerinfo_t *self, datapipe_t *datapipe);
static void peerinfo_query_owner_ign (peerinfo_t *self);
static void peerinfo_query_owner_rsp (DBusPendingCall *pc, void *aptr);
static void peerinfo_query_owner_req (peerinfo_t *self);
static void peerinfo_query_pid_ign (peerinfo_t *self);
static void peerinfo_query_pid_rsp (DBusPendingCall *pc, void *aptr);
static void peerinfo_query_pid_req (peerinfo_t *self);
static void peerinfo_identify_ign (peerinfo_t *self);
static void peerinfo_identify_rsp (DBusPendingCall *pc, void *aptr);
static void peerinfo_identify_req (peerinfo_t *self);
static void peerinfo_query_delete_ign (peerinfo_t *self);
static gboolean peerinfo_query_delete_tmo (gpointer aptr);
static void peerinfo_query_delete_req (peerinfo_t *self);
static peerquit_t *peerinfo_add_quit_callback (peerinfo_t *self, peerquit_fn callback);
static void peerinfo_remove_quit_callback (peerinfo_t *self, peerquit_t *quit);
static void peerinfo_execute_quit_callbacks (peerinfo_t *self);
static void peerinfo_flush_quit_callbacks (peerinfo_t *self);
static GList *peerinfo_find_notify_slot (peerinfo_t *self, peernotify_fn callback, gpointer userdata);
static void peerinfo_flush_notify_callbacks (peerinfo_t *self);
static void peerinfo_execute_notify_callbacks (peerinfo_t *self);
static void peerinfo_add_notify_callback (peerinfo_t *self, peernotify_fn callback, gpointer userdata, GDestroyNotify userfree);
static void peerinfo_remove_notify_callback (peerinfo_t *self, peernotify_fn callback, gpointer userdata);
static void peerinfo_queue_method (peerinfo_t *self, DBusMessage *req);
static void peerinfo_flush_methods (peerinfo_t *self);
static void peerinfo_handle_methods (peerinfo_t *self);
/* ------------------------------------------------------------------------- *
* PEER_TRACKING
* ------------------------------------------------------------------------- */
static DBusHandlerResult mce_dbus_peerinfo_filter_cb (DBusConnection *con, DBusMessage *msg, void *user_data);
static void mce_dbus_init_peerinfo (void);
static void mce_dbus_quit_peerinfo (void);
peerinfo_t *mce_dbus_get_peerinfo (const char *name);
peerinfo_t *mce_dbus_add_peerinfo (const char *name);
void mce_dbus_update_peerinfo (const char *name, const char *owner);
void mce_dbus_del_peerinfo (const char *name);
const char *mce_dbus_get_peerdesc (const char *name);
/* ------------------------------------------------------------------------- *
* MESSAGE_SENDING
* ------------------------------------------------------------------------- */
DBusConnection *dbus_connection_get (void);
DBusMessage *dbus_new_signal (const gchar *const path, const gchar *const interface, const gchar *const name);
static DBusMessage *dbus_new_error (DBusMessage *req, const char *err, const char *fmt, ...);
DBusMessage *dbus_new_method_call (const gchar *const service, const gchar *const path, const gchar *const interface, const gchar *const name);
DBusMessage *dbus_new_method_reply (DBusMessage *const message);
gboolean dbus_send_message (DBusMessage *const msg);
static gboolean dbus_send_message_with_reply_handler (DBusMessage *const msg, DBusPendingCallNotifyFunction callback, int timeout, void *user_data, DBusFreeFunction user_free, DBusPendingCall **ppc);
static gboolean dbus_send_va (const char *service, const char *path, const char *interface, const char *name, DBusPendingCallNotifyFunction callback, int timeout, void *user_data, DBusFreeFunction user_free, DBusPendingCall **ppc, int first_arg_type, va_list va);
gboolean dbus_send_ex (const char *service, const char *path, const char *interface, const char *name, DBusPendingCallNotifyFunction callback, void *user_data, DBusFreeFunction user_free, DBusPendingCall **ppc, int first_arg_type, ...);
gboolean dbus_send_ex2 (const char *service, const char *path, const char *interface, const char *name, DBusPendingCallNotifyFunction callback, int timeout, void *user_data, DBusFreeFunction user_free, DBusPendingCall **ppc, int first_arg_type, ...);
gboolean dbus_send (const gchar *const service, const gchar *const path, const gchar *const interface, const gchar *const name, DBusPendingCallNotifyFunction callback, int first_arg_type, ...);
/* ------------------------------------------------------------------------- *
* METHOD_CALL_HANDLERS
* ------------------------------------------------------------------------- */
static gboolean version_get_dbus_cb (DBusMessage *const msg);
static gboolean suspend_stats_get_dbus_cb (DBusMessage *const req);
static gboolean verbosity_get_dbus_cb (DBusMessage *const req);
static gboolean config_get_dbus_cb (DBusMessage *const msg);
static gboolean verbosity_set_dbus_cb (DBusMessage *const req);
static gboolean config_get_all_dbus_cb (DBusMessage *const req);
static gboolean config_reset_dbus_cb (DBusMessage *const msg);
static gboolean config_set_dbus_cb (DBusMessage *const msg);
static gboolean introspect_dbus_cb (DBusMessage *const req);
/* ------------------------------------------------------------------------- *
* CONFIG_VALUES
* ------------------------------------------------------------------------- */
static const char **string_array_from_gconf_value (GConfValue *conf, int *pcount);
static dbus_int32_t *int_array_from_gconf_value (GConfValue *conf, int *pcount);
static dbus_bool_t *bool_array_from_gconf_value (GConfValue *conf, int *pcount);
static double *float_array_from_gconf_value (GConfValue *conf, int *pcount);
static const char *type_signature (GConfValueType type);
static const char *value_signature (GConfValue *conf);
static bool append_gconf_value_to_dbus_iterator (DBusMessageIter *iter, GConfValue *conf);
static bool append_gconf_entry_to_dbus_iterator (DBusMessageIter *iter, GConfEntry *entry);
static bool append_gconf_entries_to_dbus_iterator (DBusMessageIter *iter, GSList *entries);
static bool append_gconf_value_to_dbus_message (DBusMessage *reply, GConfValue *conf);
void mce_dbus_send_config_notification (GConfEntry *entry);
static void value_list_free (GSList *list);
static GSList *value_list_from_string_array (DBusMessageIter *iter);
static GSList *value_list_from_int_array (DBusMessageIter *iter);
static GSList *value_list_from_bool_array (DBusMessageIter *iter);
static GSList *value_list_from_float_array (DBusMessageIter *iter);
/* ------------------------------------------------------------------------- *
* MESSAGE_DISPATCH
* ------------------------------------------------------------------------- */
static gboolean check_rules (DBusMessage *const msg, const char *rules);
static gchar *mce_dbus_build_signal_match (const gchar *sender, const gchar *interface, const gchar *name, const gchar *rules);
static void mce_dbus_squeeze_slist (GSList **list);
static bool mce_dbus_match (const char *msg_val, const char *hnd_val);
static DBusHandlerResult msg_handler (DBusConnection *const connection, DBusMessage *const msg, gpointer const user_data);
static gconstpointer mce_dbus_handler_add_ex (const gchar *const sender, const gchar *const interface, const gchar *const name, const gchar *const args, const gchar *const rules, const guint type, gboolean (*callback)(DBusMessage *const msg), bool privileged);
static void mce_dbus_handler_remove (gconstpointer cookie);
static void mce_dbus_handler_remove_cb (gpointer handler, gpointer user_data);
void mce_dbus_handler_register (mce_dbus_handler_t *self);
void mce_dbus_handler_unregister (mce_dbus_handler_t *self);
void mce_dbus_handler_register_array (mce_dbus_handler_t *array);
void mce_dbus_handler_unregister_array (mce_dbus_handler_t *array);
/* ------------------------------------------------------------------------- *
* OWNER_MONITORING
* ------------------------------------------------------------------------- */
static peerquit_t *find_monitored_service (const gchar *service, GSList *monitor_list);
gssize mce_dbus_owner_monitor_add (const gchar *service, gboolean (*callback)(DBusMessage *const msg), GSList **monitor_list, gssize max_num);
gssize mce_dbus_owner_monitor_remove (const gchar *service, GSList **monitor_list);
void mce_dbus_owner_monitor_remove_all (GSList **monitor_list);
/* ------------------------------------------------------------------------- *
* SERVICE_MANAGEMENT
* ------------------------------------------------------------------------- */
static gboolean dbus_acquire_services (void);
static void dbus_quit_message_handler (void);
static gboolean dbus_init_message_handler (void);
/* ------------------------------------------------------------------------- *
* MESSAGE_ITER
* ------------------------------------------------------------------------- */
const char *mce_dbus_type_repr (int type);
bool mce_dbus_iter_at_end (DBusMessageIter *iter);
bool mce_dbus_iter_req_type (DBusMessageIter *iter, int want);
bool mce_dbus_iter_get_basic (DBusMessageIter *iter, void *pval, int type);
bool mce_dbus_iter_get_object (DBusMessageIter *iter, const char **pval);
bool mce_dbus_iter_get_string (DBusMessageIter *iter, const char **pval);
bool mce_dbus_iter_get_bool (DBusMessageIter *iter, bool *pval);
bool mce_dbus_iter_get_int32 (DBusMessageIter *iter, dbus_int32_t *pval);
bool mce_dbus_iter_get_uint32 (DBusMessageIter *iter, dbus_uint32_t *pval);
static bool mce_dbus_iter_get_container (DBusMessageIter *iter, DBusMessageIter *sub, int type);
bool mce_dbus_iter_get_array (DBusMessageIter *iter, DBusMessageIter *sub);
bool mce_dbus_iter_get_struct (DBusMessageIter *iter, DBusMessageIter *sub);
bool mce_dbus_iter_get_entry (DBusMessageIter *iter, DBusMessageIter *sub);
bool mce_dbus_iter_get_variant (DBusMessageIter *iter, DBusMessageIter *sub);
/* ------------------------------------------------------------------------- *
* PEER_IDENTITY
* ------------------------------------------------------------------------- */
const char *mce_dbus_get_name_owner_ident (const char *name);
const char *mce_dbus_get_message_sender_ident (DBusMessage *msg);
/* ------------------------------------------------------------------------- *
* INTROSPECT_SUPPORT
* ------------------------------------------------------------------------- */
static void introspect_add_methods (FILE *file, const char *interface);
static void introspect_add_signals (FILE *file, const char *interface);
static void introspect_add_defaults (FILE *file);
static void introspect_com_nokia_mce_request (FILE *file);
static void introspect_com_nokia_mce_signal (FILE *file);
static void introspect_com_nokia_mce (FILE *file);
static void introspect_com_nokia (FILE *file);
static void introspect_com (FILE *file);
static void introspect_root (FILE *file);
static bool introspectable_signal (const char *interface, const char *member);
/* ------------------------------------------------------------------------- *
* DBUS_NAME_OWNER_TRACKING
* ------------------------------------------------------------------------- */
const char *mce_dbus_nameowner_get (const char *name);
static char *mce_dbus_nameowner_watch (const char *name);
static void mce_dbus_nameowner_unwatch (char *rule);
/* ------------------------------------------------------------------------- *
* MODULE_INIT_QUIT
* ------------------------------------------------------------------------- */
DBusConnection *dbus_bus_get (DBusBusType type, DBusError *err);
DBusConnection *dbus_bus_get_private (DBusBusType type, DBusError *err);
static void mce_dbus_init_privileged_uid (void);
static void mce_dbus_init_privileged_gid (void);
gboolean mce_dbus_init (const gboolean systembus);
void mce_dbus_exit (void);
/* ========================================================================= *
* DATA
* ========================================================================= */
/** Pointer to the DBusConnection */
static DBusConnection *dbus_connection = NULL;
/** List of all D-Bus handlers */
static GSList *dbus_handlers = NULL; // -> handler_struct_t *
/** Cached UID for "privileged" user; assume root only */
static uid_t mce_dbus_privileged_uid = PEERINFO_ROOT_UID;
/** Cached GID for "privileged" group; assume root only */
static gid_t mce_dbus_privileged_gid = PEERINFO_ROOT_GID;
/* ========================================================================= *
* SUSPEND_PROOFING
* ========================================================================= */
/** Pending call callgate slot destry callback function
*
* Called when pending call ref count drops to zero.
*
* Releases the ultiplexed wakelock that has been attached
* to the pending call object via mdb_callgate_attach().
*
* @param aptr Name of the attached wakelock
*/
static void mdb_callgate_detach_cb(void *aptr)
{
char *name = aptr;
mce_log(LL_DEBUG, "detach %s", name);
mce_wakelock_release(name);
g_free(name);
}
/** Block suspend while mce is waiting for a reply to a method call
*
* Take advantage of the fact that custom data attached to a pending
* call object gets destroyed along with the object itself and create
* unique wakelock that is used for blocking the device from entering
* suspend until:
*
* a) the wait for pending call is canceled
* b) mce has received and processed the reply message
*
* @param pc Pending call object to suspend proof
*/
static void mdb_callgate_attach(DBusPendingCall *pc)
{
static dbus_int32_t slot = -1;
static unsigned uniq = 0;
gchar *name = 0;
if( !pc )
goto EXIT;
if( slot == -1 && !dbus_pending_call_allocate_data_slot(&slot) )
goto EXIT;
if( !(name = g_strdup_printf("dbus_call_%u", ++uniq)) )
goto EXIT;
mce_log(LL_DEBUG, "attach %s", name);
if( dbus_pending_call_set_data(pc, slot, name, mdb_callgate_detach_cb) ) {
mce_wakelock_obtain(name, -1);
name = 0;
}
EXIT:
g_free(name);
}
/** Public function for making dbus method calls suspend proof
*
* NOTE: We would not need this function if all of the mce code base
* would use dbus_send_ex() based method call handling.
*
* FIXME: Fix all code that uses dbus_connection_send_with_reply()
* so that dbus_send_ex() is used instead.
*
* @param pc Pending call object to protect
*/
void mce_dbus_pending_call_blocks_suspend(DBusPendingCall *pc)
{
mdb_callgate_attach(pc);
}
/* ========================================================================= *
* DEBUG_HELPERS
* ========================================================================= */
/** Emit one iterm from dbus message iterator to file
*
* @param file output file
* @param iter dbus message parse position
*
* @return TRUE if more items can be parsed, FALSE otherwise
*/
static dbus_bool_t
mce_dbus_message_repr_any(FILE *file, DBusMessageIter *iter)
{
dbus_any_t val = { .u64 = 0 };
DBusMessageIter sub;
switch( dbus_message_iter_get_arg_type(iter) ) {
case DBUS_TYPE_INVALID:
return FALSE;
default:
case DBUS_TYPE_UNIX_FD:
fprintf(file, " ???");
return FALSE;
case DBUS_TYPE_BYTE:
dbus_message_iter_get_basic(iter, &val.o);
fprintf(file, " byte:%d", val.o);
break;
case DBUS_TYPE_BOOLEAN:
dbus_message_iter_get_basic(iter, &val.b);
fprintf(file, " bool:%d", val.b);
break;
case DBUS_TYPE_INT16:
dbus_message_iter_get_basic(iter, &val.i16);
fprintf(file, " i16:%d", val.i16);
break;
case DBUS_TYPE_INT32:
dbus_message_iter_get_basic(iter, &val.i32);
fprintf(file, " i32:%d", val.i32);
break;
case DBUS_TYPE_INT64:
dbus_message_iter_get_basic(iter, &val.i64);
fprintf(file, " i64:%lld", (long long)val.i64);
break;
case DBUS_TYPE_UINT16:
dbus_message_iter_get_basic(iter, &val.u16);
fprintf(file, " u16:%u", val.u16);
break;
case DBUS_TYPE_UINT32:
dbus_message_iter_get_basic(iter, &val.u32);
fprintf(file, " u32:%u", val.u32);
break;
case DBUS_TYPE_UINT64:
dbus_message_iter_get_basic(iter, &val.u64);
fprintf(file, " u64:%llu", (unsigned long long)val.u64);
break;
case DBUS_TYPE_DOUBLE:
dbus_message_iter_get_basic(iter, &val.d);
fprintf(file, " dbl:%g", val.d);
break;
case DBUS_TYPE_STRING:
dbus_message_iter_get_basic(iter, &val.s);
fprintf(file, " str:\"%s\"", val.s);
break;
case DBUS_TYPE_OBJECT_PATH:
dbus_message_iter_get_basic(iter, &val.s);
fprintf(file, " obj:\"%s\"", val.s);
break;
case DBUS_TYPE_SIGNATURE:
dbus_message_iter_get_basic(iter, &val.s);
fprintf(file, " sgn:\"%s\"", val.s);
break;
case DBUS_TYPE_ARRAY:
dbus_message_iter_recurse(iter, &sub);
fprintf(file, " [");
while( mce_dbus_message_repr_any(file, &sub) ) {}
fprintf(file, " ]");
break;
case DBUS_TYPE_VARIANT:
dbus_message_iter_recurse(iter, &sub);
fprintf(file, " var");
mce_dbus_message_repr_any(file, &sub);
break;
case DBUS_TYPE_STRUCT:
dbus_message_iter_recurse(iter, &sub);
fprintf(file, " {");
while( mce_dbus_message_repr_any(file, &sub) ) {}
fprintf(file, " }");
break;
case DBUS_TYPE_DICT_ENTRY:
dbus_message_iter_recurse(iter, &sub);
fprintf(file, " key");
mce_dbus_message_repr_any(file, &sub);
fprintf(file, " val");
mce_dbus_message_repr_any(file, &sub);
break;
}
return dbus_message_iter_next(iter);
}
/** Convert dbus message read iterator to string
*
* Caller must release returned string with free().
*
* @param iter dbus message iterator
*
* @returns representation of the iterator, or NULL
*/
char *
mce_dbus_message_iter_repr(DBusMessageIter *iter)
{
size_t size = 0;
char *data = 0;
FILE *file = open_memstream(&data, &size);
if( !iter )
goto EXIT;
while( mce_dbus_message_repr_any(file, iter) ) {}
EXIT:
fclose(file);
return data;
}
/** Convert dbus message to string
*
* Caller must release returned string with free().
*
* @param msg dbus message
*
* @returns representation of the dbus message, or NULL
*/
char *
mce_dbus_message_repr(DBusMessage *const msg)
{
size_t size = 0;
char *data = 0;
FILE *file = open_memstream(&data, &size);
const char *iface = dbus_message_get_interface(msg);
const char *member = dbus_message_get_member(msg);
int type = dbus_message_get_type(msg);
const char *tname = dbus_message_type_to_string(type);
const char *sender = dbus_message_get_sender(msg);
fprintf(file, "%s", tname);
if( sender ) fprintf(file, " from %s", sender);
if( iface ) fprintf(file, " %s", iface);
if( member ) fprintf(file, " %s", member);
DBusMessageIter iter;
dbus_message_iter_init(msg, &iter);
if( dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INVALID )
goto EXIT;
fprintf(file, ":");
while( mce_dbus_message_repr_any(file, &iter) ) {}
EXIT:
fclose(file);
return data;
}
/* ========================================================================= *
* HANDLER_STRUCT_T
* ========================================================================= */
/** Set sender for D-Bus handler structure */
static inline void handler_struct_set_sender(handler_struct_t *self, const char *val)
{
g_free(self->sender), self->sender = val ? g_strdup(val) : 0;
}
/** Set handler type for D-Bus handler structure */
static inline void handler_struct_set_type(handler_struct_t *self, int val)
{
self->type = val;
}
/** Set interface name for D-Bus handler structure */
static inline void handler_struct_set_interface(handler_struct_t *self, const char *val)
{
g_free(self->interface), self->interface = val ? g_strdup(val) : 0;
}
/** Set introspect args for D-Bus handler structure */
static inline void handler_struct_set_args(handler_struct_t *self, const char *val)
{
g_free(self->args), self->args = val ? g_strdup(val) : 0;
}
/** Set member name for D-Bus handler structure */
static inline void handler_struct_set_name(handler_struct_t *self, const char *val)
{
g_free(self->name), self->name = val ? g_strdup(val) : 0;
}
/** Set custom rules for D-Bus handler structure */
static inline void handler_struct_set_rules(handler_struct_t *self, const char *val)
{
g_free(self->rules), self->rules = val ? g_strdup(val) : 0;
}
/** Set callback function for D-Bus handler structure */
static inline void handler_struct_set_callback(handler_struct_t *self,
handler_callback_t val)
{
self->callback = val;
}
/** Set privileged requirement for D-Bus handler structure */
static inline void handler_struct_set_privileged(handler_struct_t *self, bool val)
{
self->privileged = val;
}
/** Release D-Bus handler structure */
static void handler_struct_delete(handler_struct_t *self)
{
if( !self )
goto EXIT;
g_free(self->args);
g_free(self->name);
g_free(self->rules);
g_free(self->interface);
g_free(self->sender);
g_free(self);
EXIT:
return;
}
/** Allocate D-Bus handler structure */
static handler_struct_t *handler_struct_create(void)
{
handler_struct_t *self = g_malloc0(sizeof *self);
self->sender = 0;
self->interface = 0;
self->rules = 0;
self->name = 0;
self->args = 0;
self->type = DBUS_MESSAGE_TYPE_INVALID;
self->privileged = false;
return self;
}
/* ========================================================================= *
* PEERSTATE_T
* ========================================================================= */
const char *
peerstate_repr(peerstate_t state)
{
const char *repr = "PEERSTATE_INVALID";
switch( state ) {
case PEERSTATE_INITIAL: repr = "PEERSTATE_INITIAL"; break;
case PEERSTATE_QUERY_OWNER: repr = "PEERSTATE_QUERY_OWNER"; break;
case PEERSTATE_QUERY_PID: repr = "PEERSTATE_QUERY_PID"; break;
case PEERSTATE_IDENTIFY: repr = "PEERSTATE_IDENTIFY"; break;
case PEERSTATE_RUNNING: repr = "PEERSTATE_RUNNING"; break;
case PEERSTATE_STOPPED: repr = "PEERSTATE_STOPPED"; break;
default: break;
}
return repr;
}
/* ========================================================================= *
* PEERQUIT_T
* ========================================================================= */
static peerquit_t *
peerquit_create(peerinfo_t *parent, peerquit_fn callback)
{
peerquit_t *self = g_malloc0(sizeof *self);
self->pq_peerinfo = parent;
self->pq_callback = callback;
mce_log(LL_DEBUG, "create quit notify: %s -> %p",
peerinfo_name(self->pq_peerinfo),
self->pq_callback);
return self;
}
static void
peerquit_delete(peerquit_t *self)
{
if( self )
{
mce_log(LL_DEBUG, "delete quit notify: %s -> %p",
peerinfo_name(self->pq_peerinfo),
self->pq_callback);
g_free(self);
}
}
static void
peerquit_notify(peerquit_t *self, DBusMessage *msg)
{
mce_log(LL_DEBUG, "execute quit notify: %s -> %p",
peerinfo_name(self->pq_peerinfo),
self->pq_callback);
self->pq_callback(msg);
}
/* ========================================================================= *
* PEERNOTIFY_T
* ========================================================================= */
/** Callback for Notifying initial peer state
*
* @param aptr peernotify_t object as void pointer
*
* @return FALSE to stop idle callback from being repeated
*/
static gboolean
peernotify_idle_cb(gpointer aptr)
{
peernotify_t *self = aptr;
if( self->pn_idle_id ) {
self->pn_idle_id = 0;
peernotify_execute(self);
}
return FALSE;
}
/** Schedule initial state notification for freshly added tracker
*
* Is called as a consequence of peerinfo_add_notify_callback() call.
*
* @param self peernotify_t object
*/
static void
peernotify_schedule(peernotify_t *self)
{
if( !self->pn_idle_id ) {
self->pn_idle_id = g_idle_add(peernotify_idle_cb, self);
}
}
/** Cancel initial state notification for freshly added tracker
*
* Is called a part of notify object cleanup / if the tracking
* state change notification gets emitted before idle callback
* gets a chance to get dispatched.
*
* @param self peernotify_t object
*/
static void
peernotify_unschedule(peernotify_t *self)
{
if( self->pn_idle_id ) {
g_source_remove(self->pn_idle_id), self->pn_idle_id = 0;
}
}
/** Notify peer tracking state change
*
* @param self peernotify_t object
*/
static void
peernotify_execute(peernotify_t *self)
{
peernotify_unschedule(self);
if( self->pn_callback ) {
self->pn_callback(self->pn_peerinfo, self->pn_userdata);
}
}
/** Create peer state tracking object
*
* @param peerinfo D-Bus client being tracked
* @param callback Notification function to call on state change
* @param userdata A pointer that should be passed to notification callback
* @param userfree free() like function to call on userdata on cleanup
*
* @param self peernotify_t object
*/
static peernotify_t *
peernotify_create(peerinfo_t *peerinfo,
peernotify_fn callback,
gpointer userdata,
GDestroyNotify userfree)
{
peernotify_t *self = calloc(1, sizeof *self);
self->pn_peerinfo = peerinfo;
self->pn_callback = callback;
self->pn_userdata = userdata;
self->pn_userfree = userfree;
self->pn_idle_id = 0;
peernotify_schedule(self);
return self;
}
/** Delete peer state tracking object
*
* @param self peernotify_t object, or NULL
*/
static void
peernotify_delete(peernotify_t *self)
{
if( self != 0 )
{
peernotify_unschedule(self);
if( self->pn_userdata && self->pn_userfree )
self->pn_userfree(self->pn_userdata);
self->pn_peerinfo = 0;
self->pn_callback = 0;
self->pn_userdata = 0;
self->pn_userfree = 0;
free(self);
}
}
/* ========================================================================= *
* PEERINFO_T
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* misc helpers
* ------------------------------------------------------------------------- */
static gchar *
peerinfo_guess_cmd(pid_t pid)