From 669e9a9a3d80d4e60b96d676d0dae8a0be8bd252 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 9 Sep 2017 18:01:23 +0200 Subject: apt-daily: Wait for network before daily updates Introduce a new helper, apt-helper wait-online that uses NetworkManager and/or systemd-networkd to wait for them reporting online, with a time out of 30 seconds; and run that helper before running the daily update script. LP: #1699850 Gbp-Dch: Full --- cmdline/apt-helper.cc | 52 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) (limited to 'cmdline') diff --git a/cmdline/apt-helper.cc b/cmdline/apt-helper.cc index 59b72ec25..1f1285eb4 100644 --- a/cmdline/apt-helper.cc +++ b/cmdline/apt-helper.cc @@ -30,6 +30,8 @@ #include #include +#include +#include #include #include @@ -176,6 +178,44 @@ static bool DoCatFile(CommandLine &CmdL) /*{{{*/ return true; } /*}}}*/ + +static pid_t ExecuteProcess(const char *Args[]) /*{{{*/ +{ + pid_t pid = ExecFork(); + if (pid == 0) + { + execvp(Args[0], (char **)Args); + _exit(100); + } + return pid; +} + +static bool ServiceIsActive(const char *service) +{ + const char *argv[] = {"systemctl", "is-active", "-q", service, nullptr}; + pid_t pid = ExecuteProcess(argv); + return ExecWait(pid, "systemctl is-active", true); +} + +static bool DoWaitOnline(CommandLine &CmdL) +{ + static const char *WaitingTasks[][6] = { + {"systemd-networkd.service", "/lib/systemd/systemd-networkd-wait-online", "-q", "--timeout=30", nullptr}, + {"NetworkManager.service", "nm-online", "-q", "--timeout", "30", nullptr}}; + + for (const char **task : WaitingTasks) + { + if (ServiceIsActive(task[0])) + { + pid_t pid = ExecuteProcess(task + 1); + + ExecWait(pid, task[1]); + } + } + + return _error->PendingError() == false; +} + /*}}}*/ static bool ShowHelp(CommandLine &) /*{{{*/ { std::cout << @@ -191,12 +231,12 @@ static bool ShowHelp(CommandLine &) /*{{{*/ static std::vector GetCommands() /*{{{*/ { return { - {"download-file", &DoDownloadFile, _("download the given uri to the target-path")}, - {"srv-lookup", &DoSrvLookup, _("lookup a SRV record (e.g. _http._tcp.ftp.debian.org)")}, - {"cat-file", &DoCatFile, _("concatenate files, with automatic decompression")}, - {"auto-detect-proxy", &DoAutoDetectProxy, _("detect proxy using apt.conf")}, - {nullptr, nullptr, nullptr} - }; + {"download-file", &DoDownloadFile, _("download the given uri to the target-path")}, + {"srv-lookup", &DoSrvLookup, _("lookup a SRV record (e.g. _http._tcp.ftp.debian.org)")}, + {"cat-file", &DoCatFile, _("concatenate files, with automatic decompression")}, + {"auto-detect-proxy", &DoAutoDetectProxy, _("detect proxy using apt.conf")}, + {"wait-online", &DoWaitOnline, _("wait for system to be online")}, + {nullptr, nullptr, nullptr}}; } /*}}}*/ int main(int argc,const char *argv[]) /*{{{*/ -- cgit v1.2.3