File: main.c

package info (click to toggle)
chrony 1.14-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,480 kB
  • ctags: 2,091
  • sloc: ansic: 15,266; sh: 194; makefile: 113
file content (198 lines) | stat: -rw-r--r-- 4,192 bytes parent folder | download
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
/*
  $Header: /cvs/src/chrony/main.c,v 1.23 1999/04/19 20:27:29 richard Exp $

  =======================================================================

  chronyd/chronyc - Programs for keeping computer clocks accurate.

  Copyright (C) 1997-1999 Richard P. Curnow
  All rights reserved.

  For conditions of use, refer to the file LICENCE.

  =======================================================================

  The main program
  */

#include "sysincl.h"

#include "main.h"
#include "sched.h"
#include "local.h"
#include "sys.h"
#include "ntp_io.h"
#include "ntp_sources.h"
#include "ntp_core.h"
#include "sources.h"
#include "sourcestats.h"
#include "reference.h"
#include "logging.h"
#include "conf.h"
#include "cmdmon.h"
#include "keys.h"
#include "acquire.h"
#include "manual.h"
#include "version.h"
#include "rtc.h"
#include "clientlog.h"

/* ================================================== */

static int reload = 0;

/* ================================================== */

volatile void
MAI_CleanupAndExit(void)
{
  if (CNF_GetDumpOnExit()) {
    SRC_DumpSources();
  }

  RTC_Finalise();
  MNL_Finalise();
  ACQ_Finalise();
  CAM_Finalise();
  KEY_Finalise();
  CLG_Finalise();
  NIO_Finalise();
  NSR_Finalise();
  NCR_Finalise();
  SRC_Finalise();
  SST_Finalise();
  REF_Finalise();
  SYS_Finalise();
  SCH_Finalise();
  LCL_Finalise();
  LOG_Finalise();

  exit(0);
}

/* ================================================== */

static void
signal_cleanup(int x)
{
  LOG(LOGS_WARN, LOGF_Main, "chronyd exiting on signal");
  MAI_CleanupAndExit();
}

/* ================================================== */

static void
post_acquire_hook(void *anything)
{

  CNF_AddSources();
  if (reload) {
    /* Note, we want reload to come well after the initialisation from
       the real time clock - this gives us a fighting chance that the
       system-clock scale for the reloaded samples still has a
       semblence of validity about it. */
    SRC_ReloadSources();
  }
  CNF_SetupAccessRestrictions();

  RTC_StartMeasurements();
}

/* ================================================== */

static void
post_init_rtc_hook(void *anything)
{
  CNF_ProcessInitStepSlew(post_acquire_hook, NULL);
}

/* ================================================== */

int main
(int argc, char **argv)
{
  char *conf_file = NULL;
  int debug = 0;
  int do_init_rtc = 0;

  LOG_Initialise();

  /* Parse command line options */
  while (++argv, (--argc)>0) {

    if (!strcmp("-f", *argv)) {
      ++argv, --argc;
      conf_file = *argv;
    } else if (!strcmp("-r", *argv)) {
      reload = 1;
    } else if (!strcmp("-s", *argv)) {
      do_init_rtc = 1;
    } else if (!strcmp("-v", *argv) || !strcmp("--version",*argv)) {
      /* This write to the terminal is OK, it comes before we turn into a daemon */
      printf("chronyd (chrony) version %s\n", PROGRAM_VERSION_STRING);
      exit(0);
    } else if (!strcmp("-d", *argv)) {
      debug = 1;
    } else {
      LOG(LOGS_WARN, LOGF_Main, "Unrecognized command line option [%s]\n", *argv);
    }
  }

#ifndef SYS_WINNT
  if (getuid() != 0) {
    /* This write to the terminal is OK, it comes before we turn into a daemon */
    fprintf(stderr,"Not superuser\n");
    exit(1);
  }

  /* Turn into a daemon */
  if (!debug) {
    LOG_GoDaemon();
  }
#endif

  CNF_ReadFile(conf_file);

  if (do_init_rtc) {
    RTC_TimePreInit();
  }

  LCL_Initialise();
  SCH_Initialise();
  SYS_Initialise();
  REF_Initialise();
  SST_Initialise();
  SRC_Initialise();
  NCR_Initialise();
  NSR_Initialise();
  NIO_Initialise();
  CLG_Initialise();
  KEY_Initialise();
  CAM_Initialise();
  ACQ_Initialise();
  MNL_Initialise();
  RTC_Initialise();

  if (do_init_rtc) {
    RTC_TimeInit(post_init_rtc_hook, NULL);
  } else {
    post_init_rtc_hook(NULL);
  }

  signal(SIGINT, signal_cleanup);
  signal(SIGTERM, signal_cleanup);
#if !defined(WINNT)
  signal(SIGQUIT, signal_cleanup);
  signal(SIGHUP, signal_cleanup);
#endif /* WINNT */

  /* The program normally runs under control of the main loop in
     the scheduler. */
  SCH_MainLoop();

  MAI_CleanupAndExit();

  return 0;
}

/* ================================================== */