]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: add APIs for more systemd notifications
authorDaniel P. Berrangé <berrange@redhat.com>
Mon, 6 Jan 2025 18:58:05 +0000 (18:58 +0000)
committerDaniel P. Berrangé <berrange@redhat.com>
Wed, 12 Feb 2025 18:05:37 +0000 (18:05 +0000)
We have a way to notify systemd when we're done starting the daemon.

Systemd supports many more notifications, however, and many of them
are quite relevant to our needs:

  https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html

This renames the existing notification API to better reflect its
semantics, and adds new APIs for reporting

 * Initiation of config file reload
 * Initiation of daemon shutdown process
 * Adhoc progress status messages
 * Request to extend service shutdown timeout

Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
src/libvirt_private.syms
src/rpc/virnetdaemon.c
src/util/virsystemd.c
src/util/virsystemd.h

index 6888f6b5991132ffbfdf21d37ea1dd9adb1ffe2c..4eca252b2a1ecf8113a4111fe45a64e288b402ec 100644 (file)
@@ -3531,7 +3531,11 @@ virSystemdHasResolved;
 virSystemdHasResolvedResetCachedValue;
 virSystemdMakeScopeName;
 virSystemdMakeSliceName;
-virSystemdNotifyStartup;
+virSystemdNotifyExtendTimeout;
+virSystemdNotifyReady;
+virSystemdNotifyReload;
+virSystemdNotifyStatus;
+virSystemdNotifyStopping;
 virSystemdResolvedRegisterNameServer;
 virSystemdTerminateMachine;
 
index f647cf60bada3af6813ca1b17d5a13eaf6704f11..e6bdfe0f88ccfb2d4b818cfa00827a1ffbf66bff 100644 (file)
@@ -753,7 +753,7 @@ virNetDaemonRun(virNetDaemon *dmn)
 
     /* We are accepting connections now. Notify systemd
      * so it can start dependent services. */
-    virSystemdNotifyStartup();
+    virSystemdNotifyReady();
 
     VIR_DEBUG("dmn=%p quit=%d", dmn, dmn->quit);
     while (!dmn->finished) {
index d46e5f74fcab837c357756de78fe06c2858bb173..57fe409c570223fcc44410cccab27ff30cdbca7b 100644 (file)
@@ -624,12 +624,11 @@ int virSystemdTerminateMachine(const char *name)
     return 0;
 }
 
-void
-virSystemdNotifyStartup(void)
+static void
+virSystemdNotify(const char *msg)
 {
 #ifndef WIN32
     const char *path;
-    const char *msg = "READY=1";
     int fd;
     struct sockaddr_un un = {
         .sun_family = AF_UNIX,
@@ -644,6 +643,8 @@ virSystemdNotifyStartup(void)
         .msg_iovlen = 1,
     };
 
+    VIR_DEBUG("Notify '%s'", msg);
+
     if (!(path = getenv("NOTIFY_SOCKET"))) {
         VIR_DEBUG("Skipping systemd notify, not requested");
         return;
@@ -674,6 +675,43 @@ virSystemdNotifyStartup(void)
 #endif /* !WIN32 */
 }
 
+void virSystemdNotifyReady(void)
+{
+    virSystemdNotify("READY=1");
+}
+
+void virSystemdNotifyReload(void)
+{
+    gint64 now = g_get_monotonic_time();
+    g_autofree char *msg = g_strdup_printf(
+        "RELOADING=1\nMONOTONIC_USEC=%lld", (long long int)now);
+    virSystemdNotify(msg);
+}
+
+void virSystemdNotifyStopping(void)
+{
+    virSystemdNotify("STOPPING=1");
+}
+
+void virSystemdNotifyExtendTimeout(int secs)
+{
+    g_autofree char *msg = g_strdup_printf("EXTEND_TIMEOUT_USEC=%llu",
+                                           secs * 1000ull * 1000ull);
+    virSystemdNotify(msg);
+}
+
+void virSystemdNotifyStatus(const char *fmt, ...)
+{
+    g_autofree char *msg1 = NULL;
+    g_autofree char *msg2 = NULL;
+    va_list ap;
+    va_start(ap, fmt);
+    msg1 = g_strdup_vprintf(fmt, ap);
+    va_end(ap);
+    msg2 = g_strdup_printf("STATUS=%s", msg1);
+    virSystemdNotify(msg2);
+}
+
 static int
 virSystemdPMSupportTarget(const char *methodName, bool *result)
 {
index b7fdaf67df58473e4f026b80439794f94bf7fcab..98460dbc3a45eeeb4f08080208990715f8537b15 100644 (file)
@@ -44,7 +44,11 @@ int virSystemdCreateMachine(const char *name,
 
 int virSystemdTerminateMachine(const char *name);
 
-void virSystemdNotifyStartup(void);
+void virSystemdNotifyReady(void);
+void virSystemdNotifyReload(void);
+void virSystemdNotifyStopping(void);
+void virSystemdNotifyExtendTimeout(int secs);
+void virSystemdNotifyStatus(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
 
 int virSystemdHasMachined(void);