From 9d6976bf204f8cc2af3e879bd63b50a681175725 Mon Sep 17 00:00:00 2001 From: Ronald G Minnich Date: Sat, 23 Sep 2023 14:44:19 -0400 Subject: [PATCH] cpud: add an option to sleep before serving When cpud runs as init, and serves mDNS, it can start serving before mDNS will work. It is still not clear why this fails, but adding a 5 second sleep before serve() resolves it for now. Such a sleep is obviously undesirable, but the default is 0. To set the timeout when cpud runs as init, add -- --sleepBeforeServing=5s to the kernel command line. Signed-off-by: Ronald G Minnich --- cmds/cpud/main_linux.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cmds/cpud/main_linux.go b/cmds/cpud/main_linux.go index 46760956..ba404243 100644 --- a/cmds/cpud/main_linux.go +++ b/cmds/cpud/main_linux.go @@ -36,6 +36,11 @@ var ( registerAddr = flag.String("register", "", "address and port to register with after listen on cpu server port") registerTO = flag.Duration("registerTO", time.Duration(5*time.Second), "time.Duration for Dial address for registering") + // if we start up too quickly, mDNS won't work correctly. + // This sleep may be useful for other cases, so it is here, + // not specifically for mDNS uses. + sleepBeforeServing = flag.Duration("sleepBeforeServing", 0, "add a sleep before serving -- usually only needed if cpud runs as init with mDNS") + pid1 bool ) @@ -89,9 +94,10 @@ func main() { log.Fatal(err) } } - pid1 = os.Getpid() == 1 + pid := os.Getpid() + pid1 = pid == 1 *runAsInit = *runAsInit || pid1 - verbose("Args %v pid %d *runasinit %v *remote %v env %v", os.Args, os.Getpid(), *runAsInit, *remote, os.Environ()) + verbose("Args %v pid %d *runasinit %v *remote %v env %v", os.Args, pid, *runAsInit, *remote, os.Environ()) args := flag.Args() if *remote { verbose("args %q, port9p %v", args, *port9p) @@ -110,12 +116,14 @@ func main() { log.Fatalf("CPUD(remote): %v", err) } } else { - log.Printf("CPUD:running as a server (a.k.a. starter of cpud's for sessions)") + log.Printf("CPUD:PID(%d):running as a server (a.k.a. starter of cpud's for sessions)", pid) if *runAsInit { + log.Printf("CPUD:also running as init") if err := initsetup(); err != nil { log.Fatal(err) } } + time.Sleep(*sleepBeforeServing) if err := serve(os.Args[0]); err != nil { log.Fatal(err) }