mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-05-15 00:50:25 +02:00
all: rework routers ntp waiting mechanism
Currently, on routers that require NTP waiting, ctrld makes the cleanup process, and restart dnsmasq for restoring default DNS config, so ntpd can query the NTP servers. It did work, but the code will depends on router platforms. Instead, we can spawn a plain DNS listener before PreRun on routers, this listener will serve NTP dns queries and once ntp is configured, the listener is terminated and ctrld will start serving using its configured upstreams. While at it, also fix the userHomeDir function on freshtomato, which must return the binary directory for routers that requires JFFS.
This commit is contained in:
committed by
Cuong Manh Le
parent
2d950eecdf
commit
1cd54a48e9
@@ -2,16 +2,11 @@ package router
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"tailscale.com/logtail/backoff"
|
||||
)
|
||||
|
||||
func setupMerlin() error {
|
||||
@@ -92,43 +87,3 @@ func merlinParsePostConf(buf []byte) []byte {
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
func merlinPreStart() (err error) {
|
||||
pidFile := "/tmp/ctrld.pid"
|
||||
|
||||
// Remove pid file and trigger dnsmasq restart, so NTP can resolve
|
||||
// server name and perform time synchronization.
|
||||
pid, err := os.ReadFile(pidFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("PreStart: os.Readfile: %w", err)
|
||||
}
|
||||
if err := os.Remove(pidFile); err != nil {
|
||||
return fmt.Errorf("PreStart: os.Remove: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
if werr := os.WriteFile(pidFile, pid, 0600); werr != nil {
|
||||
err = errors.Join(err, werr)
|
||||
return
|
||||
}
|
||||
if rerr := restartDNSMasq(); rerr != nil {
|
||||
err = errors.Join(err, rerr)
|
||||
return
|
||||
}
|
||||
}()
|
||||
if err := restartDNSMasq(); err != nil {
|
||||
return fmt.Errorf("PreStart: restartDNSMasqFn: %w", err)
|
||||
}
|
||||
|
||||
// Wait until `ntp_ready=1` set.
|
||||
b := backoff.NewBackoff("PreStart", func(format string, args ...any) {}, 10*time.Second)
|
||||
for {
|
||||
out, err := nvram("get", "ntp_ready")
|
||||
if err != nil {
|
||||
return fmt.Errorf("PreStart: nvram: %w", err)
|
||||
}
|
||||
if out == "1" {
|
||||
return nil
|
||||
}
|
||||
b.BackOff(context.Background(), errors.New("ntp not ready"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,18 @@ package router
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/kardianos/service"
|
||||
"tailscale.com/logtail/backoff"
|
||||
|
||||
"github.com/Control-D-Inc/ctrld"
|
||||
)
|
||||
@@ -106,14 +110,23 @@ func ConfigureService(sc *service.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PreStart blocks until the router is ready for running ctrld.
|
||||
func PreStart() (err error) {
|
||||
// PreRun blocks until the router is ready for running ctrld.
|
||||
func PreRun() (err error) {
|
||||
// On some routers, NTP may out of sync, so waiting for it to be ready.
|
||||
switch Name() {
|
||||
case Merlin:
|
||||
return merlinPreStart()
|
||||
case Tomato:
|
||||
return tomatoPreStart()
|
||||
case Merlin, Tomato:
|
||||
// Wait until `ntp_ready=1` set.
|
||||
b := backoff.NewBackoff("PreStart", func(format string, args ...any) {}, 10*time.Second)
|
||||
for {
|
||||
out, err := nvram("get", "ntp_ready")
|
||||
if err != nil {
|
||||
return fmt.Errorf("PreStart: nvram: %w", err)
|
||||
}
|
||||
if out == "1" {
|
||||
return nil
|
||||
}
|
||||
b.BackOff(context.Background(), errors.New("ntp not ready"))
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"tailscale.com/logtail/backoff"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -72,27 +67,6 @@ func cleanupTomato() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func tomatoPreStart() (err error) {
|
||||
// cleanup to trigger dnsmasq restart, so NTP can resolve
|
||||
// server name and perform time synchronization.
|
||||
if err = cleanupTomato(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Wait until `ntp_ready=1` set.
|
||||
b := backoff.NewBackoff("PreStart", func(format string, args ...any) {}, 10*time.Second)
|
||||
for {
|
||||
out, err := nvram("get", "ntp_ready")
|
||||
if err != nil {
|
||||
return fmt.Errorf("PreStart: nvram: %w", err)
|
||||
}
|
||||
if out == "1" {
|
||||
return nil
|
||||
}
|
||||
b.BackOff(context.Background(), errors.New("ntp not ready"))
|
||||
}
|
||||
}
|
||||
|
||||
func tomatoRestartService(name string) error {
|
||||
return tomatoRestartServiceWithKill(name, false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user