summaryrefslogtreecommitdiff
path: root/src/rcl-timedate.c
blob: 3478026d6242e94b88db94df76cb333553e1d510 (plain)
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
/*
 * Copyright (C) 2023 Andrey V.Kosteltsev <kx@radix.pro>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"

#include <string.h>
#include <stdlib.h>

#include <glib.h>
#include <glib/gi18n-lib.h>
#include <glib-object.h>
#include <locale.h>

#include <polkit/polkit.h>

#include "rcl-timedate.h"
#include "rcl-time-utils.h"
#include "rcl-ntpd-utils.h"
#include "rcl-zone-utils.h"

struct RclDaemonPrivate
{
  gboolean         debug;
  gchar           *timezone;
  gboolean         local_rtc;
  gboolean         can_ntp;
  gboolean         use_ntp;
  PolkitAuthority *auth;
};

G_DEFINE_TYPE_WITH_PRIVATE (RclDaemon, rcl_daemon, RCL_TYPE_TIMEDATE_DAEMON_SKELETON)

#define RCL_DAEMON_ACTION_DELAY  20 /* seconds */
#define RCL_INTERFACE_PREFIX     "org.freedesktop.timedate1."


/***************************************************************
  Polkit functions:
  ================
 */
static gboolean
_check_polkit_for_action( RclDaemon             *object,
                          GDBusMethodInvocation *invocation,
                          const gchar           *function )
{
  const gchar               *action = g_strjoin( "", RCL_INTERFACE_PREFIX, function, NULL );
  const gchar               *sender;
  GError                    *error;
  PolkitSubject             *subject;
  PolkitAuthorizationResult *result;

  error = NULL;

  /* Check that caller is privileged */
  sender = g_dbus_method_invocation_get_sender( invocation );
  subject = polkit_system_bus_name_new( sender );

  /********************************************************************************************
    flag = POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION causes a pop-up dialog box.
    We have not to use any pop-up windows.
   */
  result = polkit_authority_check_authorization_sync( object->priv->auth,
                                                      subject,
                                                      action,
                                                      NULL,
                                                      POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
                                                      NULL,
                                                      &error );
  g_object_unref( G_OBJECT(subject) );

  if( error )
  {
    g_dbus_method_invocation_return_gerror( invocation, error );
    g_error_free( error );
    g_free( (gpointer)action );

    return FALSE;
  }

  if( !polkit_authorization_result_get_is_authorized( result ) )
  {
    error = g_error_new( RCL_DAEMON_ERROR,
                         RCL_DAEMON_ERROR_NOT_PRIVILEGED,
                         "User is not privileged for action %s", action );
    g_dbus_method_invocation_return_gerror( invocation, error );

    g_error_free( error );
    g_object_unref( G_OBJECT(result) );
    g_free( (gpointer)action );

    return FALSE;
  }

  g_object_unref( G_OBJECT(result) );
  g_free( (gpointer)action );

  return TRUE;
}


/***************************************************************
  DBus Properties:
  ===============
 */
static gboolean get_ntpsynchronized( RclTimedateDaemon *object )
{
  gboolean ntp_synced = ntp_synchronized();

  rcl_timedate_daemon_set_ntpsynchronized( object, ntp_synced );

  return ntp_synced;
}

static guint64 get_rtctime_usec( RclTimedateDaemon *object )
{
  struct tm tm   = {};
  guint64   usec = 0;

  if( !clock_get_hwclock( &tm ) )
  {
    g_debug( "get-ntpsynchronized: error: Cannot get RTC clock" );

    rcl_timedate_daemon_set_rtctime_usec( object, (guint64)0 );

    return (guint64)0;
  }

  usec = (guint64)timegm( &tm ) * USEC_PER_SEC;

  rcl_timedate_daemon_set_rtctime_usec( object, usec );

  return usec;
}

static guint64 get_time_usec( RclTimedateDaemon *object )
{
  guint64  usec;

  usec = now( CLOCK_REALTIME );

  rcl_timedate_daemon_set_time_usec( object, usec );

  return usec;
}

void rcl_daemon_sync_dbus_properties( RclTimedateDaemon *object )
{

  /* Update NTPSynchronized */
  (void)get_ntpsynchronized( object );

  /* Update RTCTimeUSec */
  (void)get_rtctime_usec( object );

  /* Update TimeUSec */
  (void)get_time_usec( object );
}


/***************************************************************
  DBus Handlers:
  =============
 */
gboolean handle_set_timezone( RclTimedateDaemon     *object,
                              GDBusMethodInvocation *invocation,
                              const gchar           *timezone,
                              gboolean               interactive,
                              RclDaemon             *daemon )
{
  gboolean ret = TRUE;

  if( !timezone_is_valid( timezone ) )
  {
    g_dbus_method_invocation_return_error( invocation,
                                           RCL_DAEMON_ERROR,
                                           RCL_DAEMON_ERROR_INVALID_TIMEZONE_FILE,
                                           "Requested timezone '%s' is invalid", timezone );
    return TRUE;
  }

  if( g_strcmp0( (const char *)daemon->priv->timezone, (const char *)timezone ) == 0 )
    goto out;

  if( !_check_polkit_for_action( daemon, invocation, "set-timezone" ) )
  {
    g_debug( "set-timezone: error: '%s'", "User is not privileged" );
    return TRUE;
  }

  ret = set_system_timezone( timezone );
  if( ret )
  {
    if( daemon->priv->local_rtc )
    {
      struct timespec ts;
      struct tm       tm;

      /* Sync RTC from system clock, with the new delta */
      if( clock_gettime(CLOCK_REALTIME, &ts) != 0 )
      {
        g_debug( "set-timezone: error: Sync RTC from system clock: '%s'", "clock_gettime(): failed" );
        return TRUE;
      }
      if( !localtime_r( &ts.tv_sec, &tm ) )
      {
        g_debug( "set-timezone: error: Sync RTC from system clock: '%s'", "localtime_r(): failed" );
        return TRUE;
      }
      if( !clock_set_hwclock( &tm ) )
      {
        g_debug( "set-timezone: error: Sync RTC from system clock: '%s'", "Cannot update '/dev/rtc' (ignoring)" );
      }
    }
  }
  else
  {
    g_debug( "set-timezone: error: Cannot set system timezone '%s'", timezone );
    return TRUE;
  }

  g_free( (gpointer)daemon->priv->timezone );
  daemon->priv->timezone  = g_strdup( timezone );
  rcl_timedate_daemon_set_timezone( object, (const gchar *)daemon->priv->timezone );

out:
  g_debug( "set-timezone: SetTimezone to '%s' returns successful status (interactive=%s)",
                                          daemon->priv->timezone, (interactive) ? "true" : "false" );

  rcl_timedate_daemon_complete_set_timezone( object, invocation );

  return TRUE;
}


gboolean handle_set_local_rtc( RclTimedateDaemon     *object,
                               GDBusMethodInvocation *invocation,
                               gboolean               local_rtc,
                               gboolean               fix_system,
                               gboolean               interactive,
                               RclDaemon             *daemon )
{
  struct timespec  ts;
  gboolean         ret = TRUE;

  if( daemon->priv->local_rtc == local_rtc && !fix_system )
    goto out;

  if( !_check_polkit_for_action( daemon, invocation, "set-local-rtc" ) )
  {
    g_debug( "set-local-rtc: error: '%s'", "User is not privileged" );
    return TRUE;
  }

  if( daemon->priv->local_rtc != local_rtc )
  {
    daemon->priv->local_rtc = local_rtc;

    /* Write new configuration files */
    ret = write_data_local_rtc( daemon->priv->local_rtc );
    if( !ret )
    {
      g_dbus_method_invocation_return_error( invocation,
                                             RCL_DAEMON_ERROR,
                                             RCL_DAEMON_ERROR_GENERAL,
                                             "Cannot set LocalRTC" );
      return TRUE;
    }

  }

  /* Tell the kernel our timezone */
  ret = clock_set_timezone( NULL );
  if( !ret )
  {
    g_debug( "set-local-rtc: error: Cannot set timezone clock after SetLocal_RTC" );
    return TRUE;
  }

  /* Synchronize clocks */
  if( clock_gettime(CLOCK_REALTIME, &ts) != 0 )
  {
    g_debug( "set-local-rtc: error: Sync RTC from system clock after SetLocalRTC: '%s'", "clock_gettime(): failed" );
    return TRUE;
  }

  if( fix_system )
  {
    struct tm tm;

    /* Sync system clock from RTC; first, initialize the timezone fields of struct tm. */
    localtime_or_gmtime_r( &ts.tv_sec, &tm, !daemon->priv->local_rtc);

    /* Override the main fields of struct tm, but not the timezone fields */
    ret = clock_get_hwclock( &tm );
    if( !ret)
    {
      g_debug( "set-local-rtc: error: Failed to get hardware clock (ignoring)" );
    }
    else
    {
      /* And set the system clock with this */
      ts.tv_sec = mktime_or_timegm( &tm, !daemon->priv->local_rtc );

      if( clock_settime( CLOCK_REALTIME, &ts ) < 0 )
      {
        g_debug( "set-local-rtc: error: Failed to update system clock (ignoring)" );
      }
    }
  }
  else
  {
    struct tm tm;

    /* Sync RTC from system clock */
    localtime_or_gmtime_r( &ts.tv_sec, &tm, !daemon->priv->local_rtc );

    ret = clock_set_hwclock( &tm );
    if( !ret )
    {
      g_debug( "set-local-rtc: error: Failed to sync time to hardware clock (ignoring)" );
    }
  }

  g_debug( "set-local-rtc: RTC configured to %s time", (daemon->priv->local_rtc) ? "localtime" : "UTC" );


  rcl_timedate_daemon_set_local_rtc( object, daemon->priv->local_rtc );

out:
  g_debug( "set-local-rtc: SetLocalRTC to '%s' returns successful status (fix_sysrem=%s; interactive=%s)",
                                           (daemon->priv->local_rtc) ? "localtime" : "UTC",
                                           (fix_system)              ? "true"      : "false",
                                           (interactive)             ? "true"      : "false" );

  rcl_timedate_daemon_complete_set_local_rtc( object, invocation );

  return TRUE;
}


gboolean handle_set_ntp( RclTimedateDaemon     *object,
                         GDBusMethodInvocation *invocation,
                         gboolean               use_ntp,
                         gboolean               interactive,
                         RclDaemon             *daemon )
{
  /* check CanNTP (in case NTPD was uninstalled while timedated running) */
  if( !ntp_daemon_installed() )
  {
    daemon->priv->can_ntp = FALSE;
    rcl_timedate_daemon_set_can_ntp( object, daemon->priv->can_ntp );
  }

  if( !daemon->priv->can_ntp )
  {
    daemon->priv->use_ntp = FALSE;
    rcl_timedate_daemon_set_ntp( object, daemon->priv->use_ntp );
    return TRUE;
  }

  if( !_check_polkit_for_action( daemon, invocation, "set-ntp" ) )
  {
    g_debug( "set-ntp: error: '%s'", "User is not privileged" );
    return TRUE;
  }

  if( daemon->priv->use_ntp == use_ntp )
    goto out;

  if( use_ntp ) /* enable and start NTP daemon: */
  {
    if( ntp_daemon_enabled() )
    {
      if( ntp_daemon_status() )
      {
        g_debug( "set-ntp: The NTP Daemon already running" );
        daemon->priv->use_ntp = TRUE;
        /* SUCCESS */
      }
      else
      {
        if( !start_ntp_daemon() )
        {
          g_debug( "set-ntp: error: Cannot start NTPD daemon" );
          g_dbus_method_invocation_return_error( invocation,
                                                 RCL_DAEMON_ERROR,
                                                 RCL_DAEMON_ERROR_GENERAL,
                                                 "Cannot start NTP Daemon" );
          daemon->priv->use_ntp = FALSE;
          /* FAILURE */
          return TRUE;
        }
        else
        {
          g_debug( "set-ntp: The NTPD daemon started successful" );
          daemon->priv->use_ntp = TRUE;
          /* SUCCESS */
        }
      }
    }
    else
    {
      if( !enable_ntp_daemon() )
      {
        g_debug( "set-ntp: error: Cannot enable NTPD daemon" );
        g_dbus_method_invocation_return_error( invocation,
                                               RCL_DAEMON_ERROR,
                                               RCL_DAEMON_ERROR_GENERAL,
                                               "Cannot enable NTP Daemon" );
        daemon->priv->use_ntp = FALSE;
        /* FAILURE */
        return TRUE;
      }
      else
      {
        if( !start_ntp_daemon() )
        {
          g_debug( "set-ntp: error: Cannot start NTPD daemon" );
          g_dbus_method_invocation_return_error( invocation,
                                                 RCL_DAEMON_ERROR,
                                                 RCL_DAEMON_ERROR_GENERAL,
                                                 "Cannot start NTP Daemon" );
          daemon->priv->use_ntp = FALSE;
          /* FAILURE */
          return TRUE;
        }
        else
        {
          g_debug( "set-ntp: The NTPD daemon started successful" );
          daemon->priv->use_ntp = TRUE;
          /* SUCCESS */
        }
      }
    }
  }
  else /* stop and disable NTP daemon: */
  {
    if( ntp_daemon_enabled() )
    {
      if( ntp_daemon_status() )
      {
        /* daemon is running; stop it */
        if( !stop_ntp_daemon() )
        {
          g_debug( "set-ntp: error: Cannot stop NTPD daemon" );
          g_dbus_method_invocation_return_error( invocation,
                                                 RCL_DAEMON_ERROR,
                                                 RCL_DAEMON_ERROR_GENERAL,
                                                 "Cannot stop NTP Daemon" );
          daemon->priv->use_ntp = TRUE;
          /* FAILURE */
          return TRUE;
        }
        else
        {
          g_debug( "set-ntp: The NTPD daemon stopped successful" );
          if( !disable_ntp_daemon() )
          {
            g_debug( "set-ntp: Cannot disable NTPD daemon" );
            g_dbus_method_invocation_return_error( invocation,
                                                   RCL_DAEMON_ERROR,
                                                   RCL_DAEMON_ERROR_GENERAL,
                                                   "Cannot disable NTP Daemon" );
            daemon->priv->use_ntp = FALSE;
            /* daemon stopped but not disabled (will start after reboot) */
            /* SUCCESS */
          }
          else
          {
            g_debug( "set-ntp: The NTPD daemon disabled successful" );
            daemon->priv->use_ntp = FALSE;
            /* SUCCESS */
          }
        }
      }
      else
      {
        /* daemon is stopped; disable it */
        if( !disable_ntp_daemon() )
        {
          g_debug( "set-ntp: error: Cannot disable NTPD daemon" );
          g_dbus_method_invocation_return_error( invocation,
                                                 RCL_DAEMON_ERROR,
                                                 RCL_DAEMON_ERROR_GENERAL,
                                                 "Cannot disable NTP Daemon" );
          daemon->priv->use_ntp = FALSE;
          /* daemon stopped but not disabled (will start after reboot) */
          /* SUCCESS */
        }
        else
        {
          g_debug( "set-ntp: The NTPD daemon disabled successful" );
          daemon->priv->use_ntp = FALSE;
          /* SUCCESS */
        }
      }
    }
    else
    {
      g_debug( "set-ntp: The NTPD daemon already disabled" );
      daemon->priv->use_ntp = FALSE;
      /* SUCCESS */
    }
  }

  g_debug( "set-ntp: NTP configured to %s", (daemon->priv->use_ntp) ? "enabled" : "disabled" );

  rcl_timedate_daemon_set_ntp( object, daemon->priv->use_ntp );
  /* rcl_timedate_daemon_set_ntpsynchronized( object, daemon->priv->use_ntp ); */

out:
  g_debug( "set-ntp: SetNTP to '%s' returns successful status (interactive=%s)",
                                (daemon->priv->use_ntp) ? "true" : "false",
                                (interactive)           ? "true" : "false" );

  rcl_timedate_daemon_complete_set_ntp( object, invocation );

  return TRUE;
}


gboolean handle_set_time( RclTimedateDaemon     *object,
                          GDBusMethodInvocation *invocation,
                          gint64                 usec_utc,
                          gboolean               relative,
                          gboolean               interactive,
                          RclDaemon             *daemon )
{
  struct timespec  ts;
  struct tm        tm;
  guint64          start;

  if( ntp_daemon_installed() && ntp_daemon_enabled() && ntp_daemon_status() )
  {
    /* NTP Daemon is running */
    g_debug( "set-time: error: Automatic time synchronization is enabled" );
    g_dbus_method_invocation_return_error( invocation,
                                           RCL_DAEMON_ERROR,
                                           RCL_DAEMON_ERROR_GENERAL,
                                           "set-time: Automatic time synchronization is enabled" );
    return TRUE;
  }

  start = now( CLOCK_MONOTONIC );

  if( !relative && usec_utc <= 0 )
  {
    g_debug( "set-time: error: Invalid absolute time" );
    g_dbus_method_invocation_return_error( invocation,
                                           RCL_DAEMON_ERROR,
                                           RCL_DAEMON_ERROR_INVALID_ARGS,
                                           "set-time: Invalid absolute time" );
    return TRUE;
  }

  if( relative && usec_utc == 0 )
  {
    /* Nothing to do */
    goto out;
  }

  if( relative )
  {
    guint64  n, x;

    n = now( CLOCK_REALTIME );
    x = n + usec_utc;

    if( (usec_utc > 0 && x < n) ||
        (usec_utc < 0 && x > n)   )
    {
      g_debug( "set-time: error: Time value overflow" );
      g_dbus_method_invocation_return_error( invocation,
                                             RCL_DAEMON_ERROR,
                                             RCL_DAEMON_ERROR_INVALID_ARGS,
                                             "set-time: Time value overflow" );
      return TRUE;
    }
    timespec_store( &ts, x );
  }
  else
  {
    timespec_store( &ts, (guint64)usec_utc);
  }

  if( !_check_polkit_for_action( daemon, invocation, "set-time" ) )
  {
    g_debug( "set-time: error: '%s'", "User is not privileged" );
    return TRUE;
  }

  timespec_store( &ts, timespec_load( &ts ) + (now( CLOCK_MONOTONIC ) - start) );

  /* Set system clock */
  if( clock_settime( CLOCK_REALTIME, &ts ) < 0 )
  {
    g_debug( "set-time: error: Failed to set local time" );
    g_dbus_method_invocation_return_error( invocation,
                                           RCL_DAEMON_ERROR,
                                           RCL_DAEMON_ERROR_GENERAL,
                                           "set-time: Failed to set local time" );
    return TRUE;
  }

  /* Sync down to RTC */
  localtime_or_gmtime_r( &ts.tv_sec, &tm, !daemon->priv->local_rtc );

  if( !clock_set_hwclock( &tm ) )
  {
    g_debug( "set-time: error: Failed to update hardware clock (ignoring)" );
  }

  g_debug( "set-time: SetTime method returns successful status" );

  /* Update RTCTimeUSec (by the way) */
  (void)get_rtctime_usec( object );

  rcl_timedate_daemon_set_time_usec( object, timespec_load( &ts ) );

out:
  g_debug( "set-time: SetTime to %ld returns successful status(relative=%s; interactive=%s)",
                                 usec_utc,
                                 (relative)    ? "true" : "false",
                                 (interactive) ? "true" : "false" );

  rcl_timedate_daemon_complete_set_time( object, invocation );

  return TRUE;
}


gboolean handle_list_timezones( RclTimedateDaemon     *object,
                                GDBusMethodInvocation *invocation,
                                RclDaemon             *daemon )
{
  gboolean ret;
  const gchar *const *zones = { NULL };

  ret = get_timezones( &zones );
  if( !ret )
  {
    g_debug( "list-timezones: error: Failed to read list of time zones" );
    g_dbus_method_invocation_return_error( invocation,
                                           RCL_DAEMON_ERROR,
                                           RCL_DAEMON_ERROR_GENERAL,
                                           "list-timezones: Failed to read list of time zones" );
    return TRUE;
  }

  g_debug( "list-timezones: ListTimesones returns successful status" );

  rcl_timedate_daemon_complete_list_timezones( object, invocation, zones );

  timezones_free( &zones );

  return TRUE;
}



/***************************************************************
  Daemon functions:
  ================
 */

void
rcl_daemon_set_debug( RclDaemon *daemon,
                      gboolean  debug )
{
  daemon->priv->debug = debug;
}

gboolean
rcl_daemon_get_debug( RclDaemon *daemon )
{
  return daemon->priv->debug;
}


/***************************************************************
  rcl_daemon_register_timedate_daemon:
 */
static gboolean
rcl_daemon_register_timedate_daemon( RclDaemon       *daemon,
                                     GDBusConnection *connection )
{
  GError *error = NULL;

  daemon->priv->auth = polkit_authority_get_sync( NULL, &error );
  if( daemon->priv->auth == NULL )
  {
    if( error != NULL )
    {
      g_critical ("timedated: error: Cannot get system bus: %s", error->message );
      g_error_free( error );
    }
    return FALSE;
  }

  /* export our interface on the bus */
  g_dbus_interface_skeleton_export( G_DBUS_INTERFACE_SKELETON( daemon ),
                                    connection,
                                    "/org/freedesktop/timedate1",
                                    &error );

  if( error != NULL )
  {
    g_critical( "timedated: error: Cannot register the daemon on system bus: %s", error->message );
    g_error_free( error );
    return FALSE;
  }

  return TRUE;
}

/***************************************************************
  rcl_daemon_startup:
 */
gboolean
rcl_daemon_startup( RclDaemon       *daemon,
                    GDBusConnection *connection )
{
  gboolean ret;

  /* register on bus */
  ret = rcl_daemon_register_timedate_daemon( daemon, connection );
  if( !ret )
  {
    g_warning( "timedated: warning: Failed to register the daemon on bus" );
    goto out;
  }

  g_debug( "Daemon now started" );

out:
  return ret;
}

/***************************************************************
  rcl_daemon_shutdown:

  Stop the daemon, release all resources.
 */
void
rcl_daemon_shutdown( RclDaemon *daemon )
{
}


/***************************************************************
  rcl_daemon_init:
 */
static void
rcl_daemon_init( RclDaemon *daemon )
{
  gboolean rtc = TRUE;  /* default */
  gboolean ntp = FALSE; /* default */

  daemon->priv = rcl_daemon_get_instance_private( daemon );

  /**********************
    Get current Timezone:
   */
  if( !get_system_timezone( (gchar **)&daemon->priv->timezone ) )
  {
    daemon->priv->timezone  = g_strdup( "Europe/Moscow" );
  }

  /******************
    Init Properties:
   */
  /* Timezone: */
  rcl_timedate_daemon_set_daemon_version( RCL_TIMEDATE_DAEMON( daemon ), PACKAGE_VERSION );
  rcl_timedate_daemon_set_timezone( RCL_TIMEDATE_DAEMON( daemon ), (const gchar *)daemon->priv->timezone );

  /* LocalRTC: */
  (void)read_data_local_rtc( &rtc );
  daemon->priv->local_rtc = rtc;
  rcl_timedate_daemon_set_local_rtc( RCL_TIMEDATE_DAEMON( daemon ), daemon->priv->local_rtc );

  /* CanNTP: */
  ntp = ntp_daemon_installed();
  daemon->priv->can_ntp = ntp;
  rcl_timedate_daemon_set_can_ntp( RCL_TIMEDATE_DAEMON( daemon ), daemon->priv->can_ntp );

  /* NTP: */
  ntp = ( ntp_daemon_installed() && ntp_daemon_enabled() && ntp_daemon_status() );
  daemon->priv->use_ntp = ntp;
  rcl_timedate_daemon_set_ntp( RCL_TIMEDATE_DAEMON( daemon ), daemon->priv->use_ntp );

  /* NTPSynchronized: */
  rcl_timedate_daemon_set_ntpsynchronized( RCL_TIMEDATE_DAEMON( daemon ), ntp_synchronized() );


  /******************
    Handlers:
   */
  g_signal_connect( RCL_TIMEDATE_DAEMON( daemon ),
                    "handle-set-timezone",
                    G_CALLBACK( handle_set_timezone ),
                    daemon ); /* user_data */

  g_signal_connect( RCL_TIMEDATE_DAEMON( daemon ),
                    "handle-set-local-rtc",
                    G_CALLBACK( handle_set_local_rtc ),
                    daemon ); /* user_data */

  g_signal_connect( RCL_TIMEDATE_DAEMON( daemon ),
                    "handle-set-ntp",
                    G_CALLBACK( handle_set_ntp ),
                    daemon ); /* user_data */

  g_signal_connect( RCL_TIMEDATE_DAEMON( daemon ),
                    "handle-set-time",
                    G_CALLBACK( handle_set_time ),
                    daemon ); /* user_data */

  g_signal_connect( RCL_TIMEDATE_DAEMON( daemon ),
                    "handle-list-timezones",
                    G_CALLBACK( handle_list_timezones ),
                    daemon ); /* user_data */

}


static const GDBusErrorEntry rcl_daemon_error_entries[] = {
  { RCL_DAEMON_ERROR_GENERAL,               RCL_INTERFACE_PREFIX "GeneralError" },
  { RCL_DAEMON_ERROR_NOT_PRIVILEGED,        RCL_INTERFACE_PREFIX "NotPrivileged" },
  { RCL_DAEMON_ERROR_INVALID_TIMEZONE_FILE, RCL_INTERFACE_PREFIX "InvalidTimezoneFile" },
  { RCL_DAEMON_ERROR_INVALID_ARGS,          RCL_INTERFACE_PREFIX "InvalidArguments" },
  { RCL_DAEMON_ERROR_NOT_SUPPORTED,         RCL_INTERFACE_PREFIX "NotSupported" },
};

/***************************************************************
  rcl_daemon_error_quark:
 */
GQuark
rcl_daemon_error_quark( void )
{
  static volatile gsize quark_volatile = 0;

  g_dbus_error_register_error_domain( "rcl_timedated_error",
                                      &quark_volatile,
                                      rcl_daemon_error_entries,
                                      G_N_ELEMENTS( rcl_daemon_error_entries ) );
  return quark_volatile;
}


/***************************************************************
  rcl_daemon_finalize:
 */
static void
rcl_daemon_finalize( GObject *object )
{
  RclDaemon *daemon = RCL_DAEMON( object );

  g_free( daemon->priv->timezone );
  g_clear_object( &daemon->priv->auth );

  G_OBJECT_CLASS( rcl_daemon_parent_class)->finalize( object );
}

/***************************************************************
  rcl_daemon_class_init:
 */
static void
rcl_daemon_class_init( RclDaemonClass *klass )
{
  GObjectClass *object_class = G_OBJECT_CLASS( klass );
  object_class->finalize = rcl_daemon_finalize;
}

/***************************************************************
  rcl_daemon_new:
 */
RclDaemon *
rcl_daemon_new( void )
{
  return RCL_DAEMON( g_object_new( RCL_TYPE_DAEMON, NULL ) );
}