new file mode 100644
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/*
+ * Copyright (c) 2024 SUSE Software Solutions Germany GmbH
+ *
+ * Interfaces of libxenmanage.
+ *
+ * libxenmanage provides management functions for the host using stable
+ * hypercall interfaces.
+ */
+#ifndef XENMANAGE_H
+#define XENMANAGE_H
+
+#include <stdint.h>
+
+/* Avoid the need to #include <xentoollog.h> */
+struct xentoollog_logger;
+
+typedef struct xenmanage_handle xenmanage_handle;
+
+/*
+ * Open libxenmanage.
+ *
+ * Get a handle of the xenmanage library. The handle is required for all
+ * further operations of the library.
+ * Parameters:
+ * logger: Logging function to use. If NULL logging is done to stderr.
+ * open_flags: Only 0 supported.
+ * Return value: Handle or NULL if error.
+ */
+xenmanage_handle *xenmanage_open(struct xentoollog_logger *logger,
+ unsigned int open_flags);
+
+/*
+ * Close libxenmanage.
+ *
+ * Return a handle of the xenmanage library.
+ * Parameters:
+ * hdl: Handle obtained by xenmanage_open().
+ * Return value: always 0.
+ */
+int xenmanage_close(xenmanage_handle *hdl);
+
+#define XENMANAGE_GETDOMSTATE_STATE_EXIST 0x0001 /* Domain is existing. */
+#define XENMANAGE_GETDOMSTATE_STATE_SHUTDOWN 0x0002 /* Shutdown finished. */
+#define XENMANAGE_GETDOMSTATE_STATE_DYING 0x0004 /* Domain dying. */
+#define XENMANAGE_GETDOMSTATE_STATE_DEAD 0x0008 /* Domain dead. */
+
+/*
+ * Return state information of an existing domain.
+ *
+ * Returns the domain state and unique id of the given domain.
+ * Parameters:
+ * hdl: handle returned by xenmanage_open()
+ * domid: domain id of the domain to get the information for
+ * state: where to store the state (XENMANAGE_GETDOMSTATE_STATE_ flags,
+ * nothing stored if NULL)
+ * unique_id: where to store the unique id of the domain (nothing stored if
+ * NULL)
+ * Return value: 0 if information was stored, -1 else (errno is set)
+ */
+int xenmanage_get_domain_info(xenmanage_handle *hdl, unsigned int domid,
+ unsigned int *state, uint64_t *unique_id);
+
+/*
+ * Return information of a domain having changed state recently.
+ *
+ * Returns the domain id, state and unique id of a domain having changed
+ * state (any of the state bits was modified) since the last time information
+ * for that domain was returned by this function. Only usable by callers who
+ * have registered the VIRQ_DOM_EXC event (normally Xenstore).
+ * Parameters:
+ * hdl: handle returned by xenmanage_open()
+ * domid: where to store the domid of the domain (not NULL)
+ * state: where to store the state (XENMANAGE_GETDOMSTATE_STATE_ flags,
+ * nothing stored if NULL)
+ * unique_id: where to store the unique id of the domain (nothing stored if
+ * NULL)
+ * Return value: 0 if information was stored, -1 else (errno is set)
+ */
+int xenmanage_poll_changed_domain(xenmanage_handle *hdl, unsigned int *domid,
+ unsigned int *state, uint64_t *unique_id);
+#endif /* XENMANAGE_H */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
@@ -12,6 +12,7 @@ SUBDIRS-y += devicemodel
SUBDIRS-y += ctrl
SUBDIRS-y += guest
SUBDIRS-y += hypfs
+SUBDIRS-y += manage
SUBDIRS-y += store
SUBDIRS-y += stat
SUBDIRS-$(CONFIG_Linux) += vchan
new file mode 100644
@@ -0,0 +1,10 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+MAJOR = 1
+MINOR = 0
+version-script := libxenmanage.map
+
+include Makefile.common
+
+include $(XEN_ROOT)/tools/libs/libs.mk
new file mode 100644
@@ -0,0 +1,3 @@
+CFLAGS += -D__XEN_TOOLS__
+
+OBJS-y += core.o
new file mode 100644
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2024 SUSE Software Solutions Germany GmbH
+ *
+ * This library 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;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <xentoollog.h>
+#include <xenmanage.h>
+#include <xencall.h>
+#include <xentoolcore_internal.h>
+
+#include <xen/xen.h>
+#include <xen/domctl.h>
+
+struct xenmanage_handle {
+ xentoollog_logger *logger, *logger_tofree;
+ unsigned int flags;
+ xencall_handle *xcall;
+};
+
+xenmanage_handle *xenmanage_open(xentoollog_logger *logger,
+ unsigned open_flags)
+{
+ xenmanage_handle *hdl = calloc(1, sizeof(*hdl));
+ int saved_errno;
+
+ if ( !hdl )
+ return NULL;
+
+ if ( open_flags )
+ {
+ errno = EINVAL;
+ goto err;
+ }
+
+ hdl->flags = open_flags;
+ hdl->logger = logger;
+ hdl->logger_tofree = NULL;
+
+ if ( !hdl->logger )
+ {
+ hdl->logger = hdl->logger_tofree =
+ (xentoollog_logger *)
+ xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+ if ( !hdl->logger )
+ goto err;
+ }
+
+ hdl->xcall = xencall_open(hdl->logger, 0);
+ if ( !hdl->xcall )
+ goto err;
+
+ return hdl;
+
+err:
+ saved_errno = errno;
+ xenmanage_close(hdl);
+ errno = saved_errno;
+
+ return NULL;
+}
+
+int xenmanage_close(xenmanage_handle *hdl)
+{
+ if ( !hdl )
+ return 0;
+
+ xencall_close(hdl->xcall);
+ xtl_logger_destroy(hdl->logger_tofree);
+ free(hdl);
+ return 0;
+}
+
+static int xenmanage_do_domctl_get_domain_state(xenmanage_handle *hdl,
+ unsigned int domid_in,
+ unsigned int *domid_out,
+ unsigned int *state,
+ uint64_t *unique_id)
+{
+ struct xen_domctl *buf;
+ int saved_errno;
+ int ret;
+
+ buf = xencall_alloc_buffer(hdl->xcall, sizeof(*buf));
+ if ( !buf )
+ {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ memset(buf, 0, sizeof(*buf));
+
+ buf->cmd = XEN_DOMCTL_get_domain_state;
+ buf->domain = domid_in;
+
+ ret = xencall1(hdl->xcall, __HYPERVISOR_domctl, (unsigned long)buf);
+ saved_errno = errno;
+ if ( !ret )
+ {
+ struct xen_domctl_get_domain_state *st = &buf->u.get_domain_state;
+
+ if ( domid_out )
+ *domid_out = buf->domain;
+ if ( state )
+ {
+ *state = 0;
+ if ( st->state & XEN_DOMCTL_GETDOMSTATE_STATE_EXIST )
+ *state |= XENMANAGE_GETDOMSTATE_STATE_EXIST;
+ if ( st->state & XEN_DOMCTL_GETDOMSTATE_STATE_SHUTDOWN )
+ *state |= XENMANAGE_GETDOMSTATE_STATE_SHUTDOWN;
+ if ( st->state & XEN_DOMCTL_GETDOMSTATE_STATE_DYING )
+ *state |= XENMANAGE_GETDOMSTATE_STATE_DYING;
+ if ( st->state & XEN_DOMCTL_GETDOMSTATE_STATE_DEAD )
+ *state |= XENMANAGE_GETDOMSTATE_STATE_DEAD;
+ }
+ if ( unique_id )
+ *unique_id = st->unique_id;
+ }
+
+ xencall_free_buffer(hdl->xcall, buf);
+
+ errno = saved_errno;
+
+ return ret;
+}
+
+int xenmanage_get_domain_info(xenmanage_handle *hdl, unsigned int domid,
+ unsigned int *state, uint64_t *unique_id)
+{
+ if ( !hdl || domid >= DOMID_FIRST_RESERVED )
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ return xenmanage_do_domctl_get_domain_state(hdl, domid, NULL, state,
+ unique_id);
+}
+
+int xenmanage_poll_changed_domain(xenmanage_handle *hdl, unsigned int *domid,
+ unsigned int *state, uint64_t *unique_id)
+{
+ if ( !hdl || !domid )
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ return xenmanage_do_domctl_get_domain_state(hdl, DOMID_INVALID, domid,
+ state, unique_id);
+}
new file mode 100644
@@ -0,0 +1,8 @@
+VERS_1.0 {
+ global:
+ xenmanage_open;
+ xenmanage_close;
+ xenmanage_get_domain_info;
+ xenmanage_poll_changed_domain;
+ local: *; /* Do not expose anything by default */
+};
@@ -16,6 +16,8 @@ LIBS_LIBS += devicemodel
USELIBS_devicemodel := toollog toolcore call
LIBS_LIBS += hypfs
USELIBS_hypfs := toollog toolcore call
+LIBS_LIBS += manage
+USELIBS_manage := toollog toolcore call
LIBS_LIBS += ctrl
USELIBS_ctrl := toollog call evtchn gnttab foreignmemory devicemodel
LIBS_LIBS += guest