From d142c8df78d029d9714e5e7fb367b05696dcb302 Mon Sep 17 00:00:00 2001 From: mglae Date: Wed, 10 Jun 2020 23:46:03 +0200 Subject: [PATCH] wait-time-sync: new package to reliable detect ntp synchronized system time --- packages/sysutils/wait-time-sync/package.mk | 15 ++++++++++ .../sysutils/wait-time-sync/sources/Makefile | 12 ++++++++ .../wait-time-sync/sources/wait-time-sync.c | 23 +++++++++++++++ .../system.d/wait-time-sync.service | 28 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 packages/sysutils/wait-time-sync/package.mk create mode 100644 packages/sysutils/wait-time-sync/sources/Makefile create mode 100644 packages/sysutils/wait-time-sync/sources/wait-time-sync.c create mode 100644 packages/sysutils/wait-time-sync/system.d/wait-time-sync.service diff --git a/packages/sysutils/wait-time-sync/package.mk b/packages/sysutils/wait-time-sync/package.mk new file mode 100644 index 0000000000..b45468f08b --- /dev/null +++ b/packages/sysutils/wait-time-sync/package.mk @@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2020-present Team LibreELEC (https://libreelec.tv) + +PKG_NAME="wait-time-sync" +PKG_VERSION="1.0" +PKG_LICENSE="GPL" +PKG_SITE="" +PKG_URL="" +PKG_DEPENDS_TARGET="toolchain" +PKG_LONGDESC="A simple tool and systemd service to wait until NTP time is synced" + + +post_install() { + enable_service wait-time-sync.service +} diff --git a/packages/sysutils/wait-time-sync/sources/Makefile b/packages/sysutils/wait-time-sync/sources/Makefile new file mode 100644 index 0000000000..1014a4452e --- /dev/null +++ b/packages/sysutils/wait-time-sync/sources/Makefile @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2020-present Team LibreELEC (https://libreelec.tv) + +.PHONY: all +all: wait-time-sync + +PREFIX = /usr + +.PHONY: install +install: wait-time-sync + install -d $(DESTDIR)$(PREFIX)/bin + install $< $(DESTDIR)$(PREFIX)/bin diff --git a/packages/sysutils/wait-time-sync/sources/wait-time-sync.c b/packages/sysutils/wait-time-sync/sources/wait-time-sync.c new file mode 100644 index 0000000000..52cd5d780a --- /dev/null +++ b/packages/sysutils/wait-time-sync/sources/wait-time-sync.c @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2020-present Team LibreELEC (https://libreelec.tv) */ + +#include +#include +#include + +int main() +{ + int rc; + + for (;;) + { + struct timex tx = {}; + + rc = adjtimex(&tx); + if (rc != TIME_ERROR) + break; + usleep(1000000U/3); + } + + return rc == -1 ? errno : 0; +} diff --git a/packages/sysutils/wait-time-sync/system.d/wait-time-sync.service b/packages/sysutils/wait-time-sync/system.d/wait-time-sync.service new file mode 100644 index 0000000000..2a875353ea --- /dev/null +++ b/packages/sysutils/wait-time-sync/system.d/wait-time-sync.service @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: LGPL-2.1+ +# +# This file was part of systemd. +# Modified by Team LibreELEC (https://libreelec.tv) +# +# systemd is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. + +[Unit] +Description=Wait For Kernel Time Synchronized + +ConditionVirtualization=!container + +DefaultDependencies=no +Before=time-sync.target shutdown.target +Wants=time-sync.target +Conflicts=shutdown.target + +[Service] +Type=oneshot +ExecStart=/usr/bin/wait-time-sync +TimeoutStartSec=infinity +RemainAfterExit=yes + +[Install] +WantedBy=sysinit.target