forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timemaster.c
1186 lines (990 loc) · 27.3 KB
/
timemaster.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 timemaster.c
* @brief Program to run NTP with PTP as reference clocks.
* @note Copyright (C) 2014 Miroslav Lichvar <[email protected]>
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <linux/net_tstamp.h>
#include <signal.h>
#include <spawn.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "print.h"
#include "sk.h"
#include "util.h"
#include "version.h"
#define DEFAULT_RUNDIR "/var/run/timemaster"
#define DEFAULT_NTP_PROGRAM CHRONYD
#define DEFAULT_NTP_MINPOLL 6
#define DEFAULT_NTP_MAXPOLL 10
#define DEFAULT_PTP_DELAY 1e-4
#define DEFAULT_PTP_NTP_POLL 2
#define DEFAULT_PTP_PHC2SYS_POLL 0
#define DEFAULT_CHRONYD_SETTINGS \
"makestep 1 3"
#define DEFAULT_NTPD_SETTINGS \
"restrict default nomodify notrap nopeer noquery", \
"restrict 127.0.0.1", \
"restrict ::1"
#define DEFAULT_PTP4L_OPTIONS "-l", "5"
#define DEFAULT_PHC2SYS_OPTIONS "-l", "5"
enum source_type {
NTP_SERVER,
PTP_DOMAIN,
};
enum ntp_program {
CHRONYD,
NTPD,
};
struct ntp_server {
char *address;
int minpoll;
int maxpoll;
int iburst;
};
struct ptp_domain {
int domain;
int ntp_poll;
int phc2sys_poll;
double delay;
char **interfaces;
char **ptp4l_settings;
};
struct source {
enum source_type type;
union {
struct ntp_server ntp;
struct ptp_domain ptp;
};
};
struct program_config {
char *path;
char **options;
char **settings;
};
struct timemaster_config {
struct source **sources;
enum ntp_program ntp_program;
char *rundir;
struct program_config chronyd;
struct program_config ntpd;
struct program_config phc2sys;
struct program_config ptp4l;
};
struct config_file {
char *path;
char *content;
};
struct script {
struct config_file **configs;
char ***commands;
};
static void free_parray(void **a)
{
void **p;
for (p = a; *p; p++)
free(*p);
free(a);
}
static void extend_string_array(char ***a, char **strings)
{
char **s;
for (s = strings; *s; s++)
parray_append((void ***)a, xstrdup(*s));
}
static void extend_config_string(char **s, char **lines)
{
for (; *lines; lines++)
string_appendf(s, "%s\n", *lines);
}
static int parse_bool(char *s, int *b)
{
if (get_ranged_int(s, b, 0, 1) != PARSED_OK)
return 1;
return 0;
}
static int parse_int(char *s, int *i)
{
if (get_ranged_int(s, i, INT_MIN, INT_MAX) != PARSED_OK)
return 1;
return 0;
}
static int parse_double(char *s, double *d)
{
if (get_ranged_double(s, d, INT_MIN, INT_MAX) != PARSED_OK)
return 1;
return 0;
}
static char *parse_word(char *s)
{
while (*s && !isspace(*s))
s++;
while (*s && isspace(*s))
*(s++) = '\0';
return s;
}
static void parse_words(char *s, char ***a)
{
char *w;
if (**a) {
free_parray((void **)(*a));
*a = (char **)parray_new();
}
while (*s) {
w = s;
s = parse_word(s);
parray_append((void ***)a, xstrdup(w));
}
}
static void replace_string(char *s, char **str)
{
if (*str)
free(*str);
*str = xstrdup(s);
}
static char *parse_section_name(char *s)
{
char *s1, *s2;
s1 = s + 1;
for (s2 = s1; *s2 && *s2 != ']'; s2++)
;
*s2 = '\0';
return xstrdup(s1);
}
static void parse_setting(char *s, char **name, char **value)
{
*name = s;
for (*value = s; **value && !isspace(**value); (*value)++)
;
for (; **value && !isspace(**value); (*value)++)
;
for (; **value && isspace(**value); (*value)++)
**value = '\0';
}
static void source_destroy(struct source *source)
{
switch (source->type) {
case NTP_SERVER:
free(source->ntp.address);
break;
case PTP_DOMAIN:
free_parray((void **)source->ptp.interfaces);
free_parray((void **)source->ptp.ptp4l_settings);
break;
}
free(source);
}
static struct source *source_ntp_parse(char *parameter, char **settings)
{
char *name, *value;
struct ntp_server ntp_server;
struct source *source;
int r = 0;
if (!*parameter) {
pr_err("missing address for ntp_server");
return NULL;
}
ntp_server.address = parameter;
ntp_server.minpoll = DEFAULT_NTP_MINPOLL;
ntp_server.maxpoll = DEFAULT_NTP_MAXPOLL;
ntp_server.iburst = 0;
for (; *settings; settings++) {
parse_setting(*settings, &name, &value);
if (!strcasecmp(name, "minpoll")) {
r = parse_int(value, &ntp_server.minpoll);
} else if (!strcasecmp(name, "maxpoll")) {
r = parse_int(value, &ntp_server.maxpoll);
} else if (!strcasecmp(name, "iburst")) {
r = parse_bool(value, &ntp_server.iburst);
} else {
pr_err("unknown ntp_server setting %s", name);
return NULL;
}
if (r) {
pr_err("invalid value %s for %s", value, name);
return NULL;
}
}
source = xmalloc(sizeof(*source));
source->type = NTP_SERVER;
source->ntp = ntp_server;
source->ntp.address = xstrdup(source->ntp.address);
return source;
}
static struct source *source_ptp_parse(char *parameter, char **settings)
{
char *name, *value;
struct source *source;
int r = 0;
source = xmalloc(sizeof(*source));
source->type = PTP_DOMAIN;
source->ptp.delay = DEFAULT_PTP_DELAY;
source->ptp.ntp_poll = DEFAULT_PTP_NTP_POLL;
source->ptp.phc2sys_poll = DEFAULT_PTP_PHC2SYS_POLL;
source->ptp.interfaces = (char **)parray_new();
source->ptp.ptp4l_settings = (char **)parray_new();
if (parse_int(parameter, &source->ptp.domain)) {
pr_err("invalid ptp_domain number %s", parameter);
goto failed;
}
for (; *settings; settings++) {
parse_setting(*settings, &name, &value);
if (!strcasecmp(name, "delay")) {
r = parse_double(value, &source->ptp.delay);
} else if (!strcasecmp(name, "ntp_poll")) {
r = parse_int(value, &source->ptp.ntp_poll);
} else if (!strcasecmp(name, "phc2sys_poll")) {
r = parse_int(value, &source->ptp.phc2sys_poll);
} else if (!strcasecmp(name, "ptp4l_option")) {
parray_append((void ***)&source->ptp.ptp4l_settings,
xstrdup(value));
} else if (!strcasecmp(name, "interfaces")) {
parse_words(value, &source->ptp.interfaces);
} else {
pr_err("unknown ptp_domain setting %s", name);
goto failed;
}
if (r) {
pr_err("invalid value %s for %s", value, name);
goto failed;
}
}
if (!*source->ptp.interfaces) {
pr_err("no interfaces specified for ptp_domain %d",
source->ptp.domain);
goto failed;
}
return source;
failed:
source_destroy(source);
return NULL;
}
static int parse_program_settings(char **settings,
struct program_config *config)
{
char *name, *value;
for (; *settings; settings++) {
parse_setting(*settings, &name, &value);
if (!strcasecmp(name, "path")) {
replace_string(value, &config->path);
} else if (!strcasecmp(name, "options")) {
parse_words(value, &config->options);
} else {
pr_err("unknown program setting %s", name);
return 1;
}
}
return 0;
}
static int parse_timemaster_settings(char **settings,
struct timemaster_config *config)
{
char *name, *value;
for (; *settings; settings++) {
parse_setting(*settings, &name, &value);
if (!strcasecmp(name, "ntp_program")) {
if (!strcasecmp(value, "chronyd")) {
config->ntp_program = CHRONYD;
} else if (!strcasecmp(value, "ntpd")) {
config->ntp_program = NTPD;
} else {
pr_err("unknown ntp program %s", value);
return 1;
}
} else if (!strcasecmp(name, "rundir")) {
replace_string(value, &config->rundir);
} else {
pr_err("unknown timemaster setting %s", name);
return 1;
}
}
return 0;
}
static int parse_section(char **settings, char *name,
struct timemaster_config *config)
{
struct source *source = NULL;
char ***settings_dst = NULL;
char *parameter = parse_word(name);
if (!strcasecmp(name, "ntp_server")) {
source = source_ntp_parse(parameter, settings);
if (!source)
return 1;
} else if (!strcasecmp(name, "ptp_domain")) {
source = source_ptp_parse(parameter, settings);
if (!source)
return 1;
} else if (!strcasecmp(name, "chrony.conf")) {
settings_dst = &config->chronyd.settings;
} else if (!strcasecmp(name, "ntp.conf")) {
settings_dst = &config->ntpd.settings;
} else if (!strcasecmp(name, "ptp4l.conf")) {
settings_dst = &config->ptp4l.settings;
} else if (!strcasecmp(name, "chronyd")) {
if (parse_program_settings(settings, &config->chronyd))
return 1;
} else if (!strcasecmp(name, "ntpd")) {
if (parse_program_settings(settings, &config->ntpd))
return 1;
} else if (!strcasecmp(name, "phc2sys")) {
if (parse_program_settings(settings, &config->phc2sys))
return 1;
} else if (!strcasecmp(name, "ptp4l")) {
if (parse_program_settings(settings, &config->ptp4l))
return 1;
} else if (!strcasecmp(name, "timemaster")) {
if (parse_timemaster_settings(settings, config))
return 1;
} else {
pr_err("unknown section %s", name);
return 1;
}
if (source)
parray_append((void ***)&config->sources, source);
if (settings_dst) {
free_parray((void **)*settings_dst);
*settings_dst = (char **)parray_new();
extend_string_array(settings_dst, settings);
}
return 0;
}
static void init_program_config(struct program_config *config,
const char *name, ...)
{
const char *s;
va_list ap;
config->path = xstrdup(name);
config->settings = (char **)parray_new();
config->options = (char **)parray_new();
va_start(ap, name);
/* add default options and settings */
while ((s = va_arg(ap, const char *)))
parray_append((void ***)&config->options, xstrdup(s));
while ((s = va_arg(ap, const char *)))
parray_append((void ***)&config->settings, xstrdup(s));
va_end(ap);
}
static void free_program_config(struct program_config *config)
{
free(config->path);
free_parray((void **)config->settings);
free_parray((void **)config->options);
}
static void config_destroy(struct timemaster_config *config)
{
struct source **sources;
for (sources = config->sources; *sources; sources++)
source_destroy(*sources);
free(config->sources);
free_program_config(&config->chronyd);
free_program_config(&config->ntpd);
free_program_config(&config->phc2sys);
free_program_config(&config->ptp4l);
free(config->rundir);
free(config);
}
static struct timemaster_config *config_parse(char *path)
{
struct timemaster_config *config = xcalloc(1, sizeof(*config));
FILE *f;
char buf[4096], *line, *section_name = NULL;
char **section_lines = NULL;
int ret = 0;
config->sources = (struct source **)parray_new();
config->ntp_program = DEFAULT_NTP_PROGRAM;
config->rundir = xstrdup(DEFAULT_RUNDIR);
init_program_config(&config->chronyd, "chronyd",
NULL, DEFAULT_CHRONYD_SETTINGS, NULL);
init_program_config(&config->ntpd, "ntpd",
NULL, DEFAULT_NTPD_SETTINGS, NULL);
init_program_config(&config->phc2sys, "phc2sys",
DEFAULT_PHC2SYS_OPTIONS, NULL, NULL);
init_program_config(&config->ptp4l, "ptp4l",
DEFAULT_PTP4L_OPTIONS, NULL, NULL);
f = fopen(path, "r");
if (!f) {
pr_err("failed to open %s: %m", path);
free(config);
return NULL;
}
while (fgets(buf, sizeof(buf), f)) {
/* remove trailing and leading whitespace */
for (line = buf + strlen(buf) - 1;
line >= buf && isspace(*line); line--)
*line = '\0';
for (line = buf; *line && isspace(*line); line++)
;
/* skip comments and empty lines */
if (!*line || *line == '#')
continue;
if (*line == '[') {
/* parse previous section before starting another */
if (section_name) {
if (parse_section(section_lines, section_name,
config)) {
ret = 1;
break;
}
free_parray((void **)section_lines);
free(section_name);
}
section_name = parse_section_name(line);
section_lines = (char **)parray_new();
continue;
}
if (!section_lines) {
pr_err("settings outside section");
ret = 1;
break;
}
parray_append((void ***)§ion_lines, xstrdup(line));
}
if (!ret && section_name &&
parse_section(section_lines, section_name, config)) {
ret = 1;
}
fclose(f);
if (section_name)
free(section_name);
if (section_lines)
free_parray((void **)section_lines);
if (ret) {
config_destroy(config);
return NULL;
}
return config;
}
static char **get_ptp4l_command(struct program_config *config,
struct config_file *file, char **interfaces,
int hw_ts)
{
char **command = (char **)parray_new();
parray_append((void ***)&command, xstrdup(config->path));
extend_string_array(&command, config->options);
parray_extend((void ***)&command,
xstrdup("-f"), xstrdup(file->path),
xstrdup(hw_ts ? "-H" : "-S"), NULL);
for (; *interfaces; interfaces++)
parray_extend((void ***)&command,
xstrdup("-i"), xstrdup(*interfaces), NULL);
return command;
}
static char **get_phc2sys_command(struct program_config *config, int domain,
int poll, int shm_segment, char *uds_path)
{
char **command = (char **)parray_new();
parray_append((void ***)&command, xstrdup(config->path));
extend_string_array(&command, config->options);
parray_extend((void ***)&command,
xstrdup("-a"), xstrdup("-r"),
xstrdup("-R"), string_newf("%.2f", poll > 0 ?
1.0 / (1 << poll) : 1 << -poll),
xstrdup("-z"), xstrdup(uds_path),
xstrdup("-n"), string_newf("%d", domain),
xstrdup("-E"), xstrdup("ntpshm"),
xstrdup("-M"), string_newf("%d", shm_segment), NULL);
return command;
}
static char *get_refid(char *prefix, unsigned int number)
{
if (number < 10)
return string_newf("%.3s%u", prefix, number);
else if (number < 100)
return string_newf("%.2s%u", prefix, number);
else if (number < 1000)
return string_newf("%.1s%u", prefix, number);
return NULL;
};
static void add_shm_source(int shm_segment, int poll, int dpoll, double delay,
char *prefix, struct timemaster_config *config,
char **ntp_config)
{
char *refid = get_refid(prefix, shm_segment);
switch (config->ntp_program) {
case CHRONYD:
string_appendf(ntp_config,
"refclock SHM %d poll %d dpoll %d "
"refid %s precision 1.0e-9 delay %.1e\n",
shm_segment, poll, dpoll, refid, delay);
break;
case NTPD:
string_appendf(ntp_config,
"server 127.127.28.%d minpoll %d maxpoll %d "
"mode 1\n"
"fudge 127.127.28.%d refid %s\n",
shm_segment, poll, poll, shm_segment, refid);
break;
}
free(refid);
}
static int add_ntp_source(struct ntp_server *source, char **ntp_config)
{
pr_debug("adding NTP server %s", source->address);
string_appendf(ntp_config, "server %s minpoll %d maxpoll %d%s\n",
source->address, source->minpoll, source->maxpoll,
source->iburst ? " iburst" : "");
return 0;
}
static int add_ptp_source(struct ptp_domain *source,
struct timemaster_config *config, int *shm_segment,
int ***allocated_phcs, char **ntp_config,
struct script *script)
{
struct config_file *config_file;
char **command, *uds_path, **interfaces;
int i, j, num_interfaces, *phc, *phcs, hw_ts;
struct sk_ts_info ts_info;
pr_debug("adding PTP domain %d", source->domain);
hw_ts = SOF_TIMESTAMPING_TX_HARDWARE | SOF_TIMESTAMPING_RX_HARDWARE |
SOF_TIMESTAMPING_RAW_HARDWARE;
for (num_interfaces = 0;
source->interfaces[num_interfaces]; num_interfaces++)
;
if (!num_interfaces)
return 0;
/* get PHCs used by specified interfaces */
phcs = xmalloc(num_interfaces * sizeof(int));
for (i = 0; i < num_interfaces; i++) {
phcs[i] = -1;
/* check if the interface has a usable PHC */
if (sk_get_ts_info(source->interfaces[i], &ts_info)) {
pr_err("failed to get time stamping info for %s",
source->interfaces[i]);
free(phcs);
return 1;
}
if (!ts_info.valid ||
((ts_info.so_timestamping & hw_ts) != hw_ts)) {
pr_debug("interface %s: no PHC", source->interfaces[i]);
continue;
}
pr_debug("interface %s: PHC %d", source->interfaces[i],
ts_info.phc_index);
/* and the PHC isn't already used in another source */
for (j = 0; (*allocated_phcs)[j]; j++) {
if (*(*allocated_phcs)[j] == ts_info.phc_index) {
pr_debug("PHC %d already allocated",
ts_info.phc_index);
break;
}
}
if (!(*allocated_phcs)[j])
phcs[i] = ts_info.phc_index;
}
for (i = 0; i < num_interfaces; i++) {
/* skip if already used by ptp4l in this domain */
if (phcs[i] == -2)
continue;
interfaces = (char **)parray_new();
parray_append((void ***)&interfaces, source->interfaces[i]);
/* merge all interfaces sharing PHC to one ptp4l command */
if (phcs[i] >= 0) {
for (j = i + 1; j < num_interfaces; j++) {
if (phcs[i] == phcs[j]) {
parray_append((void ***)&interfaces,
source->interfaces[j]);
/* mark the interface as used */
phcs[j] = -2;
}
}
/* don't use this PHC in other sources */
phc = xmalloc(sizeof(int));
*phc = phcs[i];
parray_append((void ***)allocated_phcs, phc);
}
uds_path = string_newf("%s/ptp4l.%d.socket",
config->rundir, *shm_segment);
config_file = xmalloc(sizeof(*config_file));
config_file->path = string_newf("%s/ptp4l.%d.conf",
config->rundir, *shm_segment);
config_file->content = xstrdup("[global]\n");
extend_config_string(&config_file->content,
config->ptp4l.settings);
extend_config_string(&config_file->content,
source->ptp4l_settings);
string_appendf(&config_file->content,
"slaveOnly 1\n"
"domainNumber %d\n"
"uds_address %s\n",
source->domain, uds_path);
if (phcs[i] >= 0) {
/* HW time stamping */
command = get_ptp4l_command(&config->ptp4l, config_file,
interfaces, 1);
parray_append((void ***)&script->commands, command);
command = get_phc2sys_command(&config->phc2sys,
source->domain,
source->phc2sys_poll,
*shm_segment, uds_path);
parray_append((void ***)&script->commands, command);
} else {
/* SW time stamping */
command = get_ptp4l_command(&config->ptp4l, config_file,
interfaces, 0);
parray_append((void ***)&script->commands, command);
string_appendf(&config_file->content,
"clock_servo ntpshm\n"
"ntpshm_segment %d\n", *shm_segment);
}
parray_append((void ***)&script->configs, config_file);
add_shm_source(*shm_segment, source->ntp_poll,
source->phc2sys_poll, source->delay, "PTP",
config, ntp_config);
(*shm_segment)++;
free(uds_path);
free(interfaces);
}
free(phcs);
return 0;
}
static char **get_chronyd_command(struct program_config *config,
struct config_file *file)
{
char **command = (char **)parray_new();
parray_append((void ***)&command, xstrdup(config->path));
extend_string_array(&command, config->options);
parray_extend((void ***)&command, xstrdup("-n"),
xstrdup("-f"), xstrdup(file->path), NULL);
return command;
}
static char **get_ntpd_command(struct program_config *config,
struct config_file *file)
{
char **command = (char **)parray_new();
parray_append((void ***)&command, xstrdup(config->path));
extend_string_array(&command, config->options);
parray_extend((void ***)&command, xstrdup("-n"),
xstrdup("-c"), xstrdup(file->path), NULL);
return command;
}
static struct config_file *add_ntp_program(struct timemaster_config *config,
struct script *script)
{
struct config_file *ntp_config = xmalloc(sizeof(*ntp_config));
char **command = NULL;
ntp_config->content = xstrdup("");
switch (config->ntp_program) {
case CHRONYD:
extend_config_string(&ntp_config->content,
config->chronyd.settings);
ntp_config->path = string_newf("%s/chrony.conf",
config->rundir);
command = get_chronyd_command(&config->chronyd, ntp_config);
break;
case NTPD:
extend_config_string(&ntp_config->content,
config->ntpd.settings);
ntp_config->path = string_newf("%s/ntp.conf", config->rundir);
command = get_ntpd_command(&config->ntpd, ntp_config);
break;
}
parray_append((void ***)&script->configs, ntp_config);
parray_append((void ***)&script->commands, command);
return ntp_config;
}
static void script_destroy(struct script *script)
{
char ***commands, **command;
struct config_file *config, **configs;
for (configs = script->configs; *configs; configs++) {
config = *configs;
free(config->path);
free(config->content);
free(config);
}
free(script->configs);
for (commands = script->commands; *commands; commands++) {
for (command = *commands; *command; command++)
free(*command);
free(*commands);
}
free(script->commands);
free(script);
}
static struct script *script_create(struct timemaster_config *config)
{
struct script *script = xmalloc(sizeof(*script));
struct source *source, **sources;
struct config_file *ntp_config = NULL;
int **allocated_phcs = (int **)parray_new();
int ret = 0, shm_segment = 0;
script->configs = (struct config_file **)parray_new();
script->commands = (char ***)parray_new();
ntp_config = add_ntp_program(config, script);
for (sources = config->sources; (source = *sources); sources++) {
switch (source->type) {
case NTP_SERVER:
if (add_ntp_source(&source->ntp, &ntp_config->content))
ret = 1;
break;
case PTP_DOMAIN:
if (add_ptp_source(&source->ptp, config, &shm_segment,
&allocated_phcs,
&ntp_config->content, script))
ret = 1;
break;
}
}
free_parray((void **)allocated_phcs);
if (ret) {
script_destroy(script);
return NULL;
}
return script;
}
static pid_t start_program(char **command, sigset_t *mask)
{
char **arg, *s;
pid_t pid;
#ifdef HAVE_POSIX_SPAWN
posix_spawnattr_t attr;
if (posix_spawnattr_init(&attr)) {
pr_err("failed to init spawn attributes: %m");
return 0;
}
if (posix_spawnattr_setsigmask(&attr, mask) ||
posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK) ||
posix_spawnp(&pid, command[0], NULL, &attr, command, environ)) {
pr_err("failed to spawn %s: %m", command[0]);
posix_spawnattr_destroy(&attr);
return 0;
}
posix_spawnattr_destroy(&attr);
#else
pid = fork();
if (pid < 0) {
pr_err("fork() failed: %m");
return 0;
}
if (!pid) {
/* restore the signal mask */
if (sigprocmask(SIG_SETMASK, mask, NULL) < 0) {
pr_err("sigprocmask() failed: %m");
exit(100);
}
execvp(command[0], (char **)command);
pr_err("failed to execute %s: %m", command[0]);
exit(101);
}
#endif
for (s = xstrdup(""), arg = command; *arg; arg++)
string_appendf(&s, "%s ", *arg);
pr_info("process %d started: %s", pid, s);
free(s);
return pid;
}
static int create_config_files(struct config_file **configs)
{
struct config_file *config;
FILE *file;
char *tmp, *dir;
struct stat st;
for (; (config = *configs); configs++) {
tmp = xstrdup(config->path);
dir = dirname(tmp);
if (stat(dir, &st) < 0 && errno == ENOENT &&
mkdir(dir, 0755) < 0) {
pr_err("failed to create %s: %m", dir);
free(tmp);
return 1;
}
free(tmp);
pr_debug("creating %s", config->path);
file = fopen(config->path, "w");
if (!file) {
pr_err("failed to open %s: %m", config->path);
return 1;
}
if (fwrite(config->content,
strlen(config->content), 1, file) != 1) {
pr_err("failed to write to %s", config->path);
fclose(file);
return 1;
}
fclose(file);
}
return 0;
}
static int remove_config_files(struct config_file **configs)
{
struct config_file *config;
for (; (config = *configs); configs++) {
pr_debug("removing %s", config->path);