1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-20 06:50:22 +03:00

util: add APIs for more systemd notifications

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>
This commit is contained in:
Daniel P. Berrangé 2025-01-06 18:58:05 +00:00
parent 354b455ec6
commit 785cd56e58
4 changed files with 52 additions and 6 deletions

View File

@ -3531,7 +3531,11 @@ virSystemdHasResolved;
virSystemdHasResolvedResetCachedValue;
virSystemdMakeScopeName;
virSystemdMakeSliceName;
virSystemdNotifyStartup;
virSystemdNotifyExtendTimeout;
virSystemdNotifyReady;
virSystemdNotifyReload;
virSystemdNotifyStatus;
virSystemdNotifyStopping;
virSystemdResolvedRegisterNameServer;
virSystemdTerminateMachine;

View 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) {

View 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)
{

View 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);