diff mbox series

[v2,02/29] tools: add a new xen logging daemon

Message ID 20231110160804.29021-3-jgross@suse.com (mailing list archive)
State Superseded
Headers show
Series tools: enable xenstore-stubdom to use 9pfs | expand

Commit Message

Jürgen Groß Nov. 10, 2023, 4:07 p.m. UTC
Add "xen-9pfsd", a new logging daemon meant to support infrastructure
domains (e.g. xenstore-stubdom) to access files in dom0.

For now only add the code needed for starting the daemon and
registering it with Xenstore via a new "libxl/xen-9pfs/state" node by
writing the "running" state to it.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
V2:
- rename from xenlogd to xen-9pfsd (Andrew Cooper)
- use a backend domain local Xenstore node (Jason Andryuk)
- use "volatile" for stop_me (Andrew Cooper)
---
 tools/Makefile              |   1 +
 tools/xen-9pfsd/.gitignore  |   1 +
 tools/xen-9pfsd/Makefile    |  38 ++++++++++
 tools/xen-9pfsd/xen-9pfsd.c | 145 ++++++++++++++++++++++++++++++++++++
 4 files changed, 185 insertions(+)
 create mode 100644 tools/xen-9pfsd/.gitignore
 create mode 100644 tools/xen-9pfsd/Makefile
 create mode 100644 tools/xen-9pfsd/xen-9pfsd.c

Comments

Andrew Cooper Nov. 10, 2023, 4:13 p.m. UTC | #1
Subject wants a log->9pfsd adjustment too

On 10/11/2023 4:07 pm, Juergen Gross wrote:
> diff --git a/tools/xen-9pfsd/xen-9pfsd.c b/tools/xen-9pfsd/xen-9pfsd.c
> new file mode 100644
> index 0000000000..c365b35fe5
> --- /dev/null
> +++ b/tools/xen-9pfsd/xen-9pfsd.c
> @@ -0,0 +1,145 @@
> +/* SPDX-License-Identifier: GPL-2.0 */

Sorry I didn't notice this before.  This is a deprecated SDPX tag now,
and the preferred form is with an explicit -only or -or-later suffix.

I presume you mean -only in this case?

(Happy to fix both up on commit if there are no other issues.)

~Andrew
Jürgen Groß Nov. 10, 2023, 4:14 p.m. UTC | #2
On 10.11.23 17:13, Andrew Cooper wrote:
> Subject wants a log->9pfsd adjustment too
> 
> On 10/11/2023 4:07 pm, Juergen Gross wrote:
>> diff --git a/tools/xen-9pfsd/xen-9pfsd.c b/tools/xen-9pfsd/xen-9pfsd.c
>> new file mode 100644
>> index 0000000000..c365b35fe5
>> --- /dev/null
>> +++ b/tools/xen-9pfsd/xen-9pfsd.c
>> @@ -0,0 +1,145 @@
>> +/* SPDX-License-Identifier: GPL-2.0 */
> 
> Sorry I didn't notice this before.  This is a deprecated SDPX tag now,
> and the preferred form is with an explicit -only or -or-later suffix.
> 
> I presume you mean -only in this case?

Yes.

> 
> (Happy to fix both up on commit if there are no other issues.)

Thanks,


Juergen
Jason Andryuk Nov. 13, 2023, 5:36 p.m. UTC | #3
On Fri, Nov 10, 2023 at 11:08 AM Juergen Gross <jgross@suse.com> wrote:
>
> Add "xen-9pfsd", a new logging daemon meant to support infrastructure
> domains (e.g. xenstore-stubdom) to access files in dom0.
>
> For now only add the code needed for starting the daemon and
> registering it with Xenstore via a new "libxl/xen-9pfs/state" node by
> writing the "running" state to it.
>
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---

> --- /dev/null
> +++ b/tools/xen-9pfsd/xen-9pfsd.c
> @@ -0,0 +1,145 @@

> + * The backend device string is "xen_9pfs", the tag used for mounting the
> + * 9pfs device is "Xen".

'_' is much less common in xenstore node names than '-'.  Is there a
particular reason you chose '_'?  I generally prefer '-', but IIRC the
libxl idl can't handle '-'.  Did you hit that?

Reviewed-by: Jason Andryuk <jandryuk@gmail.com>
Jürgen Groß Nov. 14, 2023, 6:21 a.m. UTC | #4
On 13.11.23 18:36, Jason Andryuk wrote:
> On Fri, Nov 10, 2023 at 11:08 AM Juergen Gross <jgross@suse.com> wrote:
>>
>> Add "xen-9pfsd", a new logging daemon meant to support infrastructure
>> domains (e.g. xenstore-stubdom) to access files in dom0.
>>
>> For now only add the code needed for starting the daemon and
>> registering it with Xenstore via a new "libxl/xen-9pfs/state" node by
>> writing the "running" state to it.
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
> 
>> --- /dev/null
>> +++ b/tools/xen-9pfsd/xen-9pfsd.c
>> @@ -0,0 +1,145 @@
> 
>> + * The backend device string is "xen_9pfs", the tag used for mounting the
>> + * 9pfs device is "Xen".
> 
> '_' is much less common in xenstore node names than '-'.  Is there a
> particular reason you chose '_'?  I generally prefer '-', but IIRC the
> libxl idl can't handle '-'.  Did you hit that?

Yes.

> 
> Reviewed-by: Jason Andryuk <jandryuk@gmail.com>

Thanks,

Juergen
diff mbox series

Patch

diff --git a/tools/Makefile b/tools/Makefile
index 3a510663a0..f8faa3a902 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -32,6 +32,7 @@  SUBDIRS-y += xenpmd
 SUBDIRS-$(CONFIG_GOLANG) += golang
 SUBDIRS-y += xl
 SUBDIRS-y += helpers
+SUBDIRS-y += xen-9pfsd
 SUBDIRS-$(CONFIG_X86) += xenpaging
 SUBDIRS-$(CONFIG_X86) += debugger
 SUBDIRS-$(CONFIG_TESTS) += tests
diff --git a/tools/xen-9pfsd/.gitignore b/tools/xen-9pfsd/.gitignore
new file mode 100644
index 0000000000..d0c2d223ef
--- /dev/null
+++ b/tools/xen-9pfsd/.gitignore
@@ -0,0 +1 @@ 
+/xen-9pfsd
diff --git a/tools/xen-9pfsd/Makefile b/tools/xen-9pfsd/Makefile
new file mode 100644
index 0000000000..4e35202f08
--- /dev/null
+++ b/tools/xen-9pfsd/Makefile
@@ -0,0 +1,38 @@ 
+#
+# tools/xen-9pfsd/Makefile
+#
+
+XEN_ROOT = $(CURDIR)/../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+CFLAGS += $(PTHREAD_CFLAGS)
+LDFLAGS += $(PTHREAD_LDFLAGS)
+
+TARGETS := xen-9pfsd
+
+XEN-9PFSD_OBJS = xen-9pfsd.o
+$(XEN-9PFSD_OBJS): CFLAGS += $(CFLAGS_libxenstore)
+$(XEN-9PFSD_OBJS): CFLAGS += $(CFLAGS_libxenevtchn)
+$(XEN-9PFSD_OBJS): CFLAGS += $(CFLAGS_libxengnttab)
+xen-9pfsd: LDLIBS += $(call xenlibs-ldlibs,store evtchn gnttab)
+
+.PHONY: all
+all: $(TARGETS)
+
+xen-9pfsd: $(XEN-9PFSD_OBJS)
+	$(CC) $(LDFLAGS) -o $@ $(XEN-9PFSD_OBJS) $(LDLIBS) $(APPEND_LDFLAGS)
+
+.PHONY: install
+install: all
+	$(INSTALL_DIR) $(DESTDIR)$(LIBEXEC_BIN)
+	for i in $(TARGETS); do $(INSTALL_PROG) $$i $(DESTDIR)$(LIBEXEC_BIN); done
+
+.PHONY: uninstall
+uninstall:
+	for i in $(TARGETS); do rm -f $(DESTDIR)$(LIBEXEC_BIN)/$$i; done
+
+.PHONY: clean
+clean:
+	$(RM) *.o $(TARGETS) $(DEPS_RM)
+
+distclean: clean
diff --git a/tools/xen-9pfsd/xen-9pfsd.c b/tools/xen-9pfsd/xen-9pfsd.c
new file mode 100644
index 0000000000..c365b35fe5
--- /dev/null
+++ b/tools/xen-9pfsd/xen-9pfsd.c
@@ -0,0 +1,145 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * xen-9pfsd - Xen 9pfs daemon
+ *
+ * Copyright (C) 2023 Juergen Gross <jgross@suse.com>
+ *
+ * Daemon to enable guests to access a directory of the dom0 file system.
+ * Access is made via the 9pfs protocol (xen-9pfsd acts as a PV 9pfs backend).
+ *
+ * Usage: xen-9pfsd
+ *
+ * xen-9pfsd does NOT support writing any links (neither soft links nor hard
+ * links), and it is accepting only canonicalized file paths in order to
+ * avoid the possibility to "escape" from the guest specific directory.
+ *
+ * The backend device string is "xen_9pfs", the tag used for mounting the
+ * 9pfs device is "Xen".
+ *
+ * As an additional security measure the maximum file space used by the guest
+ * can be limited by the backend Xenstore node "max-size" specifying the size
+ * in MBytes. This size includes the size of the root directory of the guest.
+ */
+
+#include <err.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <xenevtchn.h>
+#include <xengnttab.h>
+#include <xenstore.h>
+
+static volatile bool stop_me;
+static bool daemon_running;
+static struct xs_handle *xs;
+static xengnttab_handle *xg;
+static xenevtchn_handle *xe;
+
+static void handle_stop(int sig)
+{
+    stop_me = true;
+}
+
+static void close_all(void)
+{
+    if ( daemon_running )
+        xs_rm(xs, XBT_NULL, "libxl/xen-9pfs");
+    if ( xe )
+        xenevtchn_close(xe);
+    if ( xg )
+        xengnttab_close(xg);
+    if ( xs )
+        xs_close(xs);
+    closelog();
+}
+
+static void do_err(const char *msg)
+{
+    syslog(LOG_ALERT, "%s, errno = %d", msg, errno);
+    close_all();
+    exit(1);
+}
+
+static void xen_connect(void)
+{
+    xs_transaction_t t;
+    char *val;
+    unsigned int len;
+
+    xs = xs_open(0);
+    if ( xs == NULL )
+        do_err("xs_open() failed");
+
+    xg = xengnttab_open(NULL, 0);
+    if ( xg == NULL )
+        do_err("xengnttab_open() failed");
+
+    xe = xenevtchn_open(NULL, 0);
+    if ( xe == NULL )
+        do_err("xenevtchn_open() failed");
+
+    while ( true )
+    {
+        t = xs_transaction_start(xs);
+        if ( t == XBT_NULL )
+            do_err("xs_transaction_start() failed");
+
+        val = xs_read(xs, t, "libxl/xen-9pfs/state", &len);
+        if ( val )
+        {
+            free(val);
+            xs_transaction_end(xs, t, true);
+            do_err("daemon already running");
+        }
+
+        if ( !xs_write(xs, t, "libxl/xen-9pfs/state", "running",
+                       strlen("running")) )
+        {
+            xs_transaction_end(xs, t, true);
+            do_err("xs_write() failed writing state");
+        }
+
+        if ( xs_transaction_end(xs, t, false) )
+            break;
+        if ( errno != EAGAIN )
+            do_err("xs_transaction_end() failed");
+    }
+
+    daemon_running = true;
+}
+
+int main(int argc, char *argv[])
+{
+    struct sigaction act = { .sa_handler = handle_stop, };
+    int syslog_mask = LOG_MASK(LOG_WARNING) | LOG_MASK(LOG_ERR) |
+                      LOG_MASK(LOG_CRIT) | LOG_MASK(LOG_ALERT) |
+                      LOG_MASK(LOG_EMERG);
+
+    umask(027);
+    if ( getenv("XEN_9PFSD_VERBOSE") )
+        syslog_mask |= LOG_MASK(LOG_NOTICE) | LOG_MASK(LOG_INFO);
+    openlog("xen-9pfsd", LOG_CONS, LOG_DAEMON);
+    setlogmask(syslog_mask);
+
+    sigemptyset(&act.sa_mask);
+    sigaction(SIGHUP, &act, NULL);
+
+    xen_connect();
+
+    while ( !stop_me )
+    {
+        sleep(60);
+    }
+
+    close_all();
+
+    return 0;
+}