Skip to content

Latest commit

 

History

History
1017 lines (818 loc) · 27.7 KB

server.c

File metadata and controls

1017 lines (818 loc) · 27.7 KB
 
Jan 26, 2011
Jan 26, 2011
1
Jan 28, 2011
Jan 28, 2011
2
3
4
5
/******************************************************************************
** This file is part of profile-qt
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
Oct 5, 2020
Oct 5, 2020
6
** Copyright (C) 2020 Jolla Ltd.
Jan 28, 2011
Jan 28, 2011
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
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**
** Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer. Redistributions in
** binary form must reproduce the above copyright notice, this list of
** conditions and the following disclaimer in the documentation and/or
** other materials provided with the distribution.
**
** Neither the name of Nokia Corporation nor the names of its contributors
** may be used to endorse or promote products derived from this software
** without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
Jan 26, 2011
Jan 26, 2011
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "profiled_config.h"
#include "logging.h"
#include "mainloop.h"
#include "server.h"
#include "database.h"
#include "codec.h"
#include "profile_dbus.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
Oct 5, 2020
Oct 5, 2020
50
#include "dbus-gmain/dbus-gmain.h"
Jan 26, 2011
Jan 26, 2011
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
#ifndef DBUS_ERROR_INIT
# define DBUS_ERROR_INIT { NULL, NULL, TRUE, 0, 0, 0, 0, NULL }
#endif
enum
{
/* Avoiding bursts of change signals: delay broadcast by
* BROADCAST_DELAY_MIN ms after each change to profile data,
* but no longer than BROADCAST_DELAY_MAX from the first
* change */
// broadcast 100ms after last change
BROADCAST_DELAY_MIN = 100, /* [ms] */
// send change broadcast in one second even if changes keep coming in
BROADCAST_DELAY_MAX = 1000, /* [ms] */
};
/* ========================================================================= *
* PROFILE DBUS SERVER FUNCTIONS
* ========================================================================= */
static DBusConnection *server_bus = NULL;
/* ------------------------------------------------------------------------- *
* server_make_reply -- create method call responce message
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_make_reply(DBusMessage *msg, int type, ...)
{
DBusMessage *rsp = 0;
va_list va;
va_start(va, type);
if( (rsp = dbus_message_new_method_return(msg)) != 0 )
{
if( !dbus_message_append_args_valist(rsp, type, va) )
{
dbus_message_unref(rsp), rsp = 0;
}
}
va_end(va);
return rsp;
}
static
DBusMessage *
introspect(DBusMessage *msg)
{
const char *xml =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node>\n"
" <interface name=\"com.nokia.profiled\">\n"
" <signal name=\"profile_changed\">\n"
" <arg type=\"b\" direction=\"out\"/>\n"
" <arg type=\"b\" direction=\"out\"/>\n"
" <arg type=\"s\" direction=\"out\"/>\n"
" <arg type=\"a(sss)\" direction=\"out\"/>\n"
" </signal>\n"
" </interface>\n"
"</node>";
log_info("%s -> reply: '%s'\n", __FUNCTION__, xml);
return server_make_reply(msg, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID);
}
/* ------------------------------------------------------------------------- *
* server_get_profiles -- handle PROFILED_GET_PROFILES method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_get_profiles(DBusMessage *msg)
{
DBusMessage *rsp = 0;
int len = 0;
char **vec = database_get_profiles(&len);
// QUARANTINE debugf("@ server_get_profiles() -> %p %d\n", vec, len);
rsp = server_make_reply(msg,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &vec, len,
DBUS_TYPE_INVALID);
database_free_profiles(vec);
log_info("%s -> reply: %d names\n", __FUNCTION__, len);
return rsp;
}
/* ------------------------------------------------------------------------- *
* server_get_profile -- handle PROFILED_GET_PROFILE method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_get_profile(DBusMessage *msg)
{
const char *prof = database_get_profile();
log_info("%s -> reply: '%s'\n", __FUNCTION__, prof);
return server_make_reply(msg, DBUS_TYPE_STRING, &prof, DBUS_TYPE_INVALID);
}
/* ------------------------------------------------------------------------- *
* server_set_profile -- handle PROFILED_SET_PROFILE method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_set_profile(DBusMessage *msg)
{
DBusMessage *rsp = 0;
char *prof = 0;
dbus_bool_t res = 0;
DBusError err = DBUS_ERROR_INIT;
if( dbus_message_get_args(msg, &err,
DBUS_TYPE_STRING, &prof,
DBUS_TYPE_INVALID) )
{
res = (database_set_profile(prof) == 0);
}
else
{
log_err("%s: %s: %s\n",
dbus_message_get_member(msg),
err.name, err.message);
}
rsp = server_make_reply(msg, DBUS_TYPE_BOOLEAN, &res, DBUS_TYPE_INVALID);
dbus_error_free(&err);
log_info("%s -> reply: %s\n", __FUNCTION__, res ? "True" : "False");
return rsp;
}
/* ------------------------------------------------------------------------- *
* server_has_profile -- handle PROFILED_HAS_PROFILE method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_has_profile(DBusMessage *msg)
{
DBusMessage *rsp = 0;
char *prof = 0;
dbus_bool_t res = 0;
DBusError err = DBUS_ERROR_INIT;
if( dbus_message_get_args(msg, &err,
DBUS_TYPE_STRING, &prof,
DBUS_TYPE_INVALID) )
{
res = (database_has_profile(prof) != 0);
}
else
{
log_err("%s: %s: %s\n",
dbus_message_get_member(msg),
err.name, err.message);
}
rsp = server_make_reply(msg, DBUS_TYPE_BOOLEAN, &res, DBUS_TYPE_INVALID);
dbus_error_free(&err);
log_info("%s -> reply: %s\n", __FUNCTION__, res ? "True" : "False");
return rsp;
}
/* ------------------------------------------------------------------------- *
* server_get_keys -- handle PROFILED_GET_KEYS method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_get_keys(DBusMessage *msg)
{
char **vec = 0;
int len = 0;
DBusMessage *rsp = 0;
vec = database_get_keys(&len);
rsp = server_make_reply(msg,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &vec, len,
DBUS_TYPE_INVALID);
database_free_keys(vec);
log_info("%s -> reply: %d names\n", __FUNCTION__, len);
return rsp;
}
/* ------------------------------------------------------------------------- *
* server_get_values -- handle PROFILED_GET_VALUES method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_get_values(DBusMessage *msg)
{
char *prof = 0;
profileval_t *vec = 0;
int len = 0;
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
if( dbus_message_get_args(msg, &err,
DBUS_TYPE_STRING, &prof,
DBUS_TYPE_INVALID) )
{
vec = database_get_values(prof, &len);
rsp = server_make_reply(msg, DBUS_TYPE_INVALID);
DBusMessageIter iter, item;
dbus_message_iter_init_append(rsp, &iter);
static const char sgn[] =
DBUS_STRUCT_BEGIN_CHAR_AS_STRING
DBUS_TYPE_STRING_AS_STRING
DBUS_TYPE_STRING_AS_STRING
DBUS_TYPE_STRING_AS_STRING
DBUS_STRUCT_END_CHAR_AS_STRING;
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, sgn, &item);
for( int i = 0; i < len; ++i )
{
const char *key = vec[i].pv_key;
const char *val = vec[i].pv_val;
const char *type = vec[i].pv_type;
encode_triplet(&item, &key, &val, &type);
}
dbus_message_iter_close_container(&iter, &item);
}
else
{
log_err("%s: %s: %s\n",
dbus_message_get_member(msg),
err.name, err.message);
}
database_free_values(vec);
log_info("%s -> reply: %d values\n", __FUNCTION__, len);
return rsp;
}
/* ------------------------------------------------------------------------- *
* server_set_value -- handle PROFILED_SET_VALUE method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_set_value(DBusMessage *msg)
{
DBusMessage *rsp = 0;
char *prof = 0;
char *key = 0;
char *val = 0;
dbus_bool_t res = 0;
DBusError err = DBUS_ERROR_INIT;
if( dbus_message_get_args(msg, &err,
DBUS_TYPE_STRING, &prof,
DBUS_TYPE_STRING, &key,
DBUS_TYPE_STRING, &val,
DBUS_TYPE_INVALID) )
{
res = (database_set_value(prof, key, val) == 0 );
}
else
{
log_err("%s: %s: %s\n",
dbus_message_get_member(msg),
err.name, err.message);
}
rsp = server_make_reply(msg, DBUS_TYPE_BOOLEAN, &res, DBUS_TYPE_INVALID);
dbus_error_free(&err);
log_info("%s -> reply: %s\n", __FUNCTION__, res ? "True" : "False");
return rsp;
}
/* ------------------------------------------------------------------------- *
* server_has_value -- handle PROFILED_HAS_VALUE method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_has_value(DBusMessage *msg)
{
DBusMessage *rsp = 0;
char *key = 0;
dbus_bool_t res = 0;
DBusError err = DBUS_ERROR_INIT;
if( dbus_message_get_args(msg, &err,
DBUS_TYPE_STRING, &key,
DBUS_TYPE_INVALID) )
{
res = (database_has_value(key) != 0);
}
else
{
log_err("%s: %s: %s\n",
dbus_message_get_member(msg),
err.name, err.message);
}
rsp = server_make_reply(msg, DBUS_TYPE_BOOLEAN, &res, DBUS_TYPE_INVALID);
dbus_error_free(&err);
log_info("%s -> reply: %s\n", __FUNCTION__, res ? "True" : "False");
return rsp;
}
/* ------------------------------------------------------------------------- *
* server_is_writable -- handle PROFILED_IS_WRITABLE method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_is_writable(DBusMessage *msg)
{
DBusMessage *rsp = 0;
char *key = 0;
dbus_bool_t res = 0;
DBusError err = DBUS_ERROR_INIT;
if( dbus_message_get_args(msg, &err,
DBUS_TYPE_STRING, &key,
DBUS_TYPE_INVALID) )
{
res = (database_is_writable(key) != 0);
}
else
{
log_err("%s: %s: %s\n",
dbus_message_get_member(msg),
err.name, err.message);
}
rsp = server_make_reply(msg, DBUS_TYPE_BOOLEAN, &res, DBUS_TYPE_INVALID);
dbus_error_free(&err);
log_info("%s -> reply: %s\n", __FUNCTION__, res ? "True" : "False");
return rsp;
}
/* ------------------------------------------------------------------------- *
* server_get_value -- handle PROFILED_GET_VALUE method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_get_value(DBusMessage *msg)
{
DBusMessage *rsp = 0;
char *prof = 0;
char *key = 0;
const char *val = 0;
DBusError err = DBUS_ERROR_INIT;
if( dbus_message_get_args(msg, &err,
DBUS_TYPE_STRING, &prof,
DBUS_TYPE_STRING, &key,
DBUS_TYPE_INVALID) )
{
val = database_get_value(prof, key, "");
}
else
{
log_err("%s: %s: %s\n",
dbus_message_get_member(msg),
err.name, err.message);
}
rsp = server_make_reply(msg, DBUS_TYPE_STRING, &val, DBUS_TYPE_INVALID);
dbus_error_free(&err);
log_info("%s -> reply: '%s'\n", __FUNCTION__, val);
return rsp;
}
/* ------------------------------------------------------------------------- *
* server_get_type -- handle PROFILED_GET_TYPE method call
* ------------------------------------------------------------------------- */
static
DBusMessage *
server_get_type(DBusMessage *msg)
{
DBusMessage *rsp = 0;
char *key = 0;
const char *val = 0;
DBusError err = DBUS_ERROR_INIT;
if( dbus_message_get_args(msg, &err,
DBUS_TYPE_STRING, &key,
DBUS_TYPE_INVALID) )
{
val = database_get_type(key, "");
}
else
{
log_err("%s: %s: %s\n",
dbus_message_get_member(msg),
err.name, err.message);
}
rsp = server_make_reply(msg, DBUS_TYPE_STRING, &val, DBUS_TYPE_INVALID);
dbus_error_free(&err);
log_info("%s -> reply: '%s'\n", __FUNCTION__, val);
return rsp;
}
/* ------------------------------------------------------------------------- *
* server_filter -- handle requests coming via dbus
* ------------------------------------------------------------------------- */
static
DBusHandlerResult
server_filter(DBusConnection *conn,
DBusMessage *msg,
void *user_data)
{
Oct 4, 2013
Oct 4, 2013
502
503
(void)user_data;
Jan 26, 2011
Jan 26, 2011
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
DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
const char *interface = dbus_message_get_interface(msg);
const char *member = dbus_message_get_member(msg);
const char *object = dbus_message_get_path(msg);
int type = dbus_message_get_type(msg);
DBusMessage *rsp = 0;
// QUARANTINE log_debug("@%s(%s, %s, %s)", __FUNCTION__, object, interface, member);
if( !interface || !member || !object )
{
goto cleanup;
}
if( dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected") )
{
/* Make a orderly shutdown if we get disconnected from
* session bus, so that we do not leave behind defunct
* profiledaemon process that might overwrite data saved
* by another profile daemon connected to functioning
* session bus. */
log_warning("disconnected from session bus - terminating\n");
mainloop_stop();
goto cleanup;
}
if (!strcmp(interface, "org.freedesktop.DBus.Introspectable")
&& !strcmp(member, "Introspect")
&& type == DBUS_MESSAGE_TYPE_METHOD_CALL) {
result = DBUS_HANDLER_RESULT_HANDLED;
log_info("introspect");
rsp = introspect(msg);
if(rsp == 0)
rsp = dbus_message_new_error(msg, DBUS_ERROR_FAILED, member);
if(rsp != 0)
dbus_connection_send(conn, rsp, 0);
goto cleanup;
}
if( !strcmp(interface, PROFILED_INTERFACE) && !strcmp(object, PROFILED_PATH) )
{
/* - - - - - - - - - - - - - - - - - - - *
* as far as dbus message filtering is
* considered handling of PROFILED_INTERFACE
* should happen right here
* - - - - - - - - - - - - - - - - - - - */
result = DBUS_HANDLER_RESULT_HANDLED;
/* - - - - - - - - - - - - - - - - - - - *
* handle method_call messages
* - - - - - - - - - - - - - - - - - - - */
if( type == DBUS_MESSAGE_TYPE_METHOD_CALL )
{
static const struct
{
const char *member;
DBusMessage *(*func)(DBusMessage *);
} lut[] =
{
{PROFILED_GET_PROFILES, server_get_profiles},
{PROFILED_GET_PROFILE, server_get_profile},
{PROFILED_SET_PROFILE, server_set_profile},
{PROFILED_HAS_PROFILE, server_has_profile},
{PROFILED_GET_KEYS, server_get_keys},
{PROFILED_GET_VALUES, server_get_values},
{PROFILED_GET_TYPE, server_get_type},
{PROFILED_GET_VALUE, server_get_value},
{PROFILED_SET_VALUE, server_set_value},
{PROFILED_HAS_VALUE, server_has_value},
{PROFILED_IS_WRITABLE, server_is_writable},
{0,0}
};
for( int i = 0; ; ++i )
{
if( lut[i].member == 0 )
{
log_err("unknown method call: %s\n", member);
rsp = dbus_message_new_error(msg, DBUS_ERROR_UNKNOWN_METHOD, member);
break;
}
if( !strcmp(lut[i].member, member) )
{
log_info("handling method call: %s\n", member);
rsp = lut[i].func(msg);
break;
}
}
}
/* - - - - - - - - - - - - - - - - - - - *
* if no responce message was created
* above and the peer expects reply,
* create a generic error message
* - - - - - - - - - - - - - - - - - - - */
if( rsp == 0 && !dbus_message_get_no_reply(msg) )
{
rsp = dbus_message_new_error(msg, DBUS_ERROR_FAILED, member);
}
/* - - - - - - - - - - - - - - - - - - - *
* send responce if we have something
* to send
* - - - - - - - - - - - - - - - - - - - */
if( rsp != 0 )
{
dbus_connection_send(conn, rsp, 0);
//dbus_connection_flush(conn);
}
}
cleanup:
if( rsp != 0 )
{
dbus_message_unref(rsp);
}
return result;
}
static
int
server_append_values_to_iter(DBusMessageIter *iter,
profileval_t *vec, int cnt)
{
int res = -1;
DBusMessageIter item, memb;
static const char sgn[] =
DBUS_STRUCT_BEGIN_CHAR_AS_STRING
DBUS_TYPE_STRING_AS_STRING
DBUS_TYPE_STRING_AS_STRING
DBUS_TYPE_STRING_AS_STRING
DBUS_STRUCT_END_CHAR_AS_STRING;
// TODO: need to check success / failure
dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, sgn, &item);
for( int i = 0; i < cnt; ++i )
{
const char *key = vec[i].pv_key;
const char *val = vec[i].pv_val;
const char *type = vec[i].pv_type;
dbus_message_iter_open_container(&item, DBUS_TYPE_STRUCT, 0, &memb);
dbus_message_iter_append_basic(&memb, DBUS_TYPE_STRING, &key);
dbus_message_iter_append_basic(&memb, DBUS_TYPE_STRING, &val);
dbus_message_iter_append_basic(&memb, DBUS_TYPE_STRING, &type);
dbus_message_iter_close_container(&item, &memb);
}
dbus_message_iter_close_container(iter, &item);
res = 0;
//cleanup:
return res;
}
/* ------------------------------------------------------------------------- *
* server_change_broadcast -- send profile change signals to dbus
* ------------------------------------------------------------------------- */
static
int
server_change_broadcast(int changed, int active, const char *profile)
{
// QUARANTINE debugf("@ %s\n", __FUNCTION__);
int cnt = 0;
profileval_t *set = database_get_changed_values(profile, &cnt);
DBusMessage *msg = 0;
DBusMessageIter iter;
log_notice("%s: changed=%d, active=%d, profile=%s, values=%d\n",
__FUNCTION__, changed, active, profile, cnt);
msg = dbus_message_new_signal(PROFILED_PATH,
PROFILED_INTERFACE,
PROFILED_CHANGED);
dbus_bool_t cflag = (changed != 0);
dbus_bool_t aflag = (active != 0);
dbus_message_append_args(msg,
DBUS_TYPE_BOOLEAN, &cflag,
DBUS_TYPE_BOOLEAN, &aflag,
DBUS_TYPE_STRING, &profile,
DBUS_TYPE_INVALID);
dbus_message_iter_init_append(msg, &iter);
server_append_values_to_iter(&iter, set, cnt);
database_free_changed_values(set);
dbus_connection_send(server_bus, msg, 0);
dbus_connection_flush(server_bus);
dbus_message_unref(msg);
return 0;
}
/* ------------------------------------------------------------------------- *
* server_changes_max_id -- timeout id for unhandled profile data changes
* ------------------------------------------------------------------------- */
static guint server_changes_min_id = 0;
static guint server_changes_max_id = 0;
static int server_changes_broadcast_cancel(void)
{
int cancelled = 0;
if( server_changes_min_id != 0 )
{
g_source_remove(server_changes_min_id);
server_changes_min_id = 0;
cancelled = 1;
}
if( server_changes_max_id != 0 )
{
g_source_remove(server_changes_max_id);
server_changes_max_id = 0;
cancelled = 1;
}
return cancelled;
}
/* ------------------------------------------------------------------------- *
* server_changes_broadcast_cb -- broadcasts changes signals after timeout
* ------------------------------------------------------------------------- */
static
gboolean
server_changes_broadcast_cb(gpointer data)
{
Oct 4, 2013
Oct 4, 2013
752
753
(void)data;
Jan 26, 2011
Jan 26, 2011
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
// QUARANTINE debugf("@ %s\n", __FUNCTION__);
const char *current = database_get_profile();
const char *previous = database_get_previous();
int changed = strcmp(current, previous);
// QUARANTINE debugf("prev=%s, curr=%s, diff=%d\n",
// QUARANTINE previous,current,changed);
int count = 0;
char **profiles = database_get_changed_profiles(&count);
// QUARANTINE debugf("\tp=%p, n=%d\n", profiles, count);
for( int i = 0; profiles[i]; ++i )
{
const char *profile = profiles[i];
int active = !strcmp(profile, current);
if( changed && active )
{
continue;
}
// QUARANTINE debugf("\tPROF %s\n", profile);
server_change_broadcast(0, active, profiles[i]);
}
if( changed )
{
server_change_broadcast(1, 1, current);
}
database_free_changed_profiles(profiles);
database_clear_changes();
server_changes_broadcast_cancel();
return FALSE;
}
static void server_changes_broadcast_request(void)
{
if( BROADCAST_DELAY_MAX > 0 )
{
if( server_changes_max_id == 0 )
{
server_changes_max_id = g_timeout_add(BROADCAST_DELAY_MAX,
server_changes_broadcast_cb, 0);
}
}
if( server_changes_min_id != 0 )
{
g_source_remove(server_changes_min_id);
server_changes_min_id = 0;
}
if( BROADCAST_DELAY_MIN > 0 )
{
server_changes_min_id = g_timeout_add(BROADCAST_DELAY_MIN,
server_changes_broadcast_cb, 0);
}
}
/* ------------------------------------------------------------------------- *
* server_changes_handler_cb -- get called when profile data changes
* ------------------------------------------------------------------------- */
static
void
server_changes_handler_cb(void)
{
// QUARANTINE debugf("@ %s\n", __FUNCTION__);
server_changes_broadcast_request();
}
/* ------------------------------------------------------------------------- *
* server_changes_save -- skip change broadcast, only save changes
* ------------------------------------------------------------------------- */
static
void
server_changes_save(void)
{
if( server_changes_broadcast_cancel() )
{
database_clear_changes();
}
}
/* ------------------------------------------------------------------------- *
* If database engine notices that somebody (i.e. osso-backup doing restore)
* has touched the data files it disables data saving and makes a
* restart request.
* ------------------------------------------------------------------------- */
static guint server_restart_id = 0;
static
gboolean
server_restart_idle_cb(gpointer data)
{
Oct 4, 2013
Oct 4, 2013
858
859
(void)data;
Jan 26, 2011
Jan 26, 2011
860
861
862
863
864
865
866
867
868
869
870
871
872
// QUARANTINE log_debug("@%s", __FUNCTION__);
server_restart_id = 0;
// terminate - we will be restarted by dbus when our interface
// is used by somebody
mainloop_stop();
return FALSE;
}
static
gboolean
server_restart_delay_cb(gpointer data)
{
Oct 4, 2013
Oct 4, 2013
873
874
(void)data;
Jan 26, 2011
Jan 26, 2011
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
// QUARANTINE log_debug("@%s", __FUNCTION__);
// wait for idle
server_restart_id = g_idle_add(server_restart_idle_cb, 0);
return FALSE;
}
static
void
server_restart_cb(void)
{
// QUARANTINE log_debug("@%s", __FUNCTION__);
if( server_restart_id == 0 )
{
// wait couple of secs
server_restart_id = g_timeout_add(5 * 1000, server_restart_delay_cb, 0);
}
}
/* ------------------------------------------------------------------------- *
* server_init
* ------------------------------------------------------------------------- */
int
server_init(void)
{
// QUARANTINE log_debug("@%s()", __FUNCTION__);
int res = -1;
DBusError err = DBUS_ERROR_INIT;
/* - - - - - - - - - - - - - - - - - - - *
* set data changed notification callback
* - - - - - - - - - - - - - - - - - - - */
database_set_changed_cb(server_changes_handler_cb);
/* - - - - - - - - - - - - - - - - - - - *
* set callback that will be called if
* database notices that the data files
* have been modified by somebody else
* (like osso-backup during restore)
* - - - - - - - - - - - - - - - - - - - */
database_set_restart_request_cb(server_restart_cb);
/* - - - - - - - - - - - - - - - - - - - *
* connect to dbus
* - - - - - - - - - - - - - - - - - - - */
#ifdef USE_SYSTEM_BUS
if( (server_bus = dbus_bus_get(DBUS_BUS_SYSTEM, &err)) == 0 )
{
//CLEANUP fatalf("%s: %s\n", "dbus_bus_get", err.message);
goto cleanup;
}
#else
if( (server_bus = dbus_bus_get(DBUS_BUS_SESSION, &err)) == 0 )
{
//CLEANUP fatalf("%s: %s\n", "dbus_bus_get", err.message);
goto cleanup;
}
#endif
/* - - - - - - - - - - - - - - - - - - - *
* register message filter
* - - - - - - - - - - - - - - - - - - - */
if( !dbus_connection_add_filter(server_bus, server_filter, 0, 0) )
{
//CLEANUP fatalf("%s: %s\n", "dbus_connection_add_filter", "FAILED");
goto cleanup;
}
/* - - - - - - - - - - - - - - - - - - - *
* request service name
* - - - - - - - - - - - - - - - - - - - */
int ret = dbus_bus_request_name(server_bus, PROFILED_SERVICE,
DBUS_NAME_FLAG_DO_NOT_QUEUE,
//DBUS_NAME_FLAG_REPLACE_EXISTING,
&err);
if( ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER )
{
if( dbus_error_is_set(&err) )
{
log_err("%s: %s: %s\n", "dbus_bus_request_name", err.name, err.message);
}
else
{
log_err("%s: %s\n", "dbus_bus_request_name", "not primary owner of connection\n");
}
goto cleanup;
}
/* - - - - - - - - - - - - - - - - - - - *
* attach connection to mainloop
* - - - - - - - - - - - - - - - - - - - */
Oct 5, 2020
Oct 5, 2020
974
dbus_gmain_set_up_connection(server_bus, NULL);
Jan 26, 2011
Jan 26, 2011
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
dbus_connection_set_exit_on_disconnect(server_bus, 0);
/* - - - - - - - - - - - - - - - - - - - *
* success
* - - - - - - - - - - - - - - - - - - - */
res = 0;
/* - - - - - - - - - - - - - - - - - - - *
* cleanup & return
* - - - - - - - - - - - - - - - - - - - */
cleanup:
dbus_error_free(&err);
return res;
}
/* ------------------------------------------------------------------------- *
* server_quit
* ------------------------------------------------------------------------- */
void
server_quit(void)
{
// QUARANTINE log_debug("@%s()", __FUNCTION__);