aboutsummaryrefslogtreecommitdiffstats
path: root/layout/rootfs/etc/s6-rc/source-base/init-clock/up
blob: 1c7f02c95a59dbdd1c7d334b17427fcb294ef030 (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
#!/command/execlineb -P

# Get the local country code as well as a few other codes
# for countries all around the world.

pipeline
{
  s6-setuidgid nobody
  foreground
  {
    s6-envdir /etc/s6-linux-init/env-stage2
    importas -u COUNTRY_CODE COUNTRY_CODE
    s6-echo ${COUNTRY_CODE}
  }
  forx CODE { uk de ru cn kr jp us ca br au il za }
    importas -u CODE CODE
    s6-echo ${CODE}
}


# Suppress duplicates (e.g. if the local country code is in the list).

pipeline
{
  s6-setuidgid nobody
  s6-sort -u
}


# Get the IP addresses for some NTP servers in those countries.
# Limit the DNS queries to 30 seconds: if the DNS is slower than
# that, chances are the server isn't good for us anyway.
# All the queries are parallelized, so it's fast.

pipeline
{
  s6-setuidgid nobody
  forstdin -p -d"\n" CODE
    importas -u CODE CODE
    forx -p I { 0 1 }
      importas -u I I
      redirfd -w 2 /dev/null
      s6-dnsip4 -t 30000 -- ${I}.${CODE}.pool.ntp.org
}


# Ask for a NTP exchange with the servers we get. Accept anything.
# If the exchange can't be completed in 5 seconds, kill it.
# All the queries are parallelized.

pipeline
{
  s6-setuidgid ntp
  forstdin -p -d"\n" NTPSERVERIP
    importas -u NTPSERVERIP NTPSERVERIP
    redirfd -w 2 /dev/null
    s6-sntpclock -f -T 5000 ${NTPSERVERIP}
}


# Take the first answer we get.

pipeline
{
  s6-setuidgid nobody
  s6-head -Sc 12
}

# Update the system clock. Accept anything.

s6-clockadd -f