diff options
author | Julian Andres Klode <jak@debian.org> | 2017-09-09 18:01:23 +0200 |
---|---|---|
committer | Julian Andres Klode <jak@debian.org> | 2017-09-09 18:36:10 +0200 |
commit | 669e9a9a3d80d4e60b96d676d0dae8a0be8bd252 (patch) | |
tree | 26496f1b188b67a02be1154038b6487b27269f9d | |
parent | 923522367a28be7235bf6459abfeae89fafffcf1 (diff) |
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
-rw-r--r-- | cmdline/apt-helper.cc | 52 | ||||
-rw-r--r-- | debian/apt-daily.service | 1 |
2 files changed, 47 insertions, 6 deletions
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 <vector> #include <stdlib.h> +#include <sys/types.h> +#include <sys/wait.h> #include <unistd.h> #include <apti18n.h> @@ -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<aptDispatchWithHelp> 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[]) /*{{{*/ diff --git a/debian/apt-daily.service b/debian/apt-daily.service index 4a946afdd..d98f88ffe 100644 --- a/debian/apt-daily.service +++ b/debian/apt-daily.service @@ -5,5 +5,6 @@ ConditionACPower=true [Service] Type=oneshot +ExecStartPre=-/usr/lib/apt/apt-helper wait-online ExecStart=/usr/lib/apt/apt.systemd.daily update |