[XEN PATCH] tools/xenconsoled: add --syslog option

Changlei Li posted 1 patch 2 hours ago
CHANGELOG.md                |   2 +
tools/console/daemon/io.c   | 195 +++++++++++++++++++++++++++++++-----
tools/console/daemon/main.c |  26 ++++-
3 files changed, 196 insertions(+), 27 deletions(-)
[XEN PATCH] tools/xenconsoled: add --syslog option
Posted by Changlei Li 2 hours ago
The hypervisor and guest console logs can only be written to files
today, so they are not part of the host's syslog stream and are not
forwarded with it.

Add a --syslog=none|guest|hv|all option, with the same values as --log.
Each line of console output is sent to syslog at LOG_INFO, facility
LOG_DAEMON. Hypervisor lines use the ident "hypervisor". Guest lines use
"guest-<name>" ("guest-<name>-vuart0" for the vUART), which matches the
guest log file name. xenconsoled keeps its own "xenconsoled" ident for
its messages, so the sources can be told apart without relying on
levels.

syslog(3) keeps one ident, option set and log mask per process. For
each console line, the ident, options and mask are switched and then
restored, so xenconsoled's own logging is unchanged. xenconsoled is
single-threaded, so no other message can be logged in between.

Each console has its own line buffer, and so does the hypervisor log.
A line is sent when its newline arrives, so a line written in several
parts becomes one syslog message. A line longer than 960 bytes is split.
A partial line is sent when a guest console is cleaned up, or when
xenconsoled leaves its main loop. It is lost when xenconsoled is killed,
for example by SIGTERM. A trailing '\r' is removed, as guest consoles
usually end lines with "\r\n". Guest output is passed to its buffer in
buffer_append(), where it is written to the guest log file, so overflow
discard does not affect it.

The guest name comes from xenstore. In the ident, ASCII characters other
than A-Z, a-z, 0-9, '.', '_' and '-' are replaced with '_', so the name
cannot break syslog tag parsing. Non-ASCII (UTF-8) bytes are kept, so
names in other scripts stay readable and distinct. The ident is limited
to 127 bytes, cut on a UTF-8 character boundary, and keeps the vUART
suffix. The xenstore read of the name is shared with the guest log file
code.

--log keeps controlling file output and the two options are independent,
so --log=none --syslog=hv sends the hypervisor log to syslog only.
--timestamp and --log-dir only apply to file output; --replace-escape
applies to both. An unknown --syslog value is an error.

Signed-off-by: Changlei Li <changlei.li@citrix.com>
---
 CHANGELOG.md                |   2 +
 tools/console/daemon/io.c   | 195 +++++++++++++++++++++++++++++++-----
 tools/console/daemon/main.c |  26 ++++-
 3 files changed, 196 insertions(+), 27 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index f967437eda..851c1ad6f6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
      afterwards.
 
 ### Added
+ - xenconsoled: new --syslog=none|guest|hv|all option to send console output
+   to syslog.
 
 ### Removed
  - On Arm:
diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c
index b6c46d11de..1090336bbe 100644
--- a/tools/console/daemon/io.c
+++ b/tools/console/daemon/io.c
@@ -63,16 +63,29 @@
 extern int log_reload;
 extern int log_guest;
 extern int log_hv;
+extern int syslog_guest;
+extern int syslog_hv;
 extern int log_time_hv;
 extern int log_time_guest;
 extern char *log_dir;
 extern int discard_overflowed_data;
 extern int replace_escape;
+extern int syslog_option;
 
 static int log_time_hv_needts = 1;
 static int log_time_guest_needts = 1;
 static int log_hv_fd = -1;
 
+struct syslog_line {
+	/* glibc keeps the whole ident; musl keeps at most 31 bytes. */
+	char ident[128];
+	size_t len;
+	/* No limit in glibc; fits musl's 1024-byte buffer with the header. */
+	char data[960];
+};
+
+static struct syslog_line hv_syslog = { .ident = "hypervisor" };
+
 static xengnttab_handle *xgt_handle = NULL;
 static xenforeignmemory_handle *xfm_handle;
 
@@ -94,6 +107,7 @@ struct console {
 	int master_pollfd_idx;
 	int slave_fd;
 	int log_fd;
+	struct syslog_line *syslog;
 	struct buffer buffer;
 	char *xspath;
 	const char *log_suffix;
@@ -296,6 +310,53 @@ static int write_with_timestamp(int fd, const char *data, size_t sz,
 	return 0;
 }
 
+static void syslog_line_flush(struct syslog_line *line)
+{
+	int mask;
+
+	/* Guest consoles usually end lines with "\r\n". */
+	while (line->len && line->data[line->len - 1] == '\r')
+		line->len--;
+	if (!line->len)
+		return;
+
+	/*
+	 * Own tag, no flags and own mask, restored afterwards; safe as we are
+	 * single-threaded. No LOG_CONS/LOG_PERROR: the line must not loop
+	 * back into the console ring or be duplicated on stderr.
+	 */
+	mask = setlogmask(LOG_MASK(LOG_INFO));
+	openlog(line->ident, 0, LOG_DAEMON);
+	syslog(LOG_INFO, "%.*s", (int)line->len, line->data);
+	openlog("xenconsoled", syslog_option, LOG_DAEMON);
+	setlogmask(mask);
+	line->len = 0;
+}
+
+static void syslog_line_write(struct syslog_line *line, const char *data,
+			      size_t sz)
+{
+	size_t i;
+
+	for (i = 0; i < sz; i++) {
+		char c = data[i];
+
+		if (c == '\n') {
+			syslog_line_flush(line);
+			continue;
+		}
+		/* Strip \r after a newline, like write_with_timestamp(). */
+		if (c == '\r' && !line->len)
+			continue;
+		if (c == '\033' && replace_escape)
+			c = '.';
+
+		line->data[line->len++] = c;
+		if (line->len == sizeof(line->data))
+			syslog_line_flush(line);
+	}
+}
+
 static inline bool buffer_available(struct console *con)
 {
 	if (discard_overflowed_data ||
@@ -361,6 +422,10 @@ static void buffer_append(struct console *con)
 			      dom->domid, errno, strerror(errno));
 	}
 
+	if (con->syslog)
+		syslog_line_write(con->syslog,
+				  buffer->data + buffer->size - size, size);
+
 	if (discard_overflowed_data && buffer->max_capacity &&
 	    buffer->size > 5 * buffer->max_capacity / 4) {
 		if (buffer->consumed > buffer->max_capacity / 4) {
@@ -433,30 +498,37 @@ static int create_hv_log(void)
 	return fd;
 }
 
-static int create_console_log(struct console *con)
+static char *read_domain_name(struct domain *dom)
 {
-	char logfile[PATH_MAX];
 	char *namepath, *data, *s;
-	int fd;
 	unsigned int len;
-	struct domain *dom = con->d;
 
 	namepath = xs_get_domain_path(xs, dom->domid);
 	s = realloc(namepath, strlen(namepath) + 6);
 	if (s == NULL) {
 		free(namepath);
-		return -1;
+		return NULL;
 	}
 	namepath = s;
 	strcat(namepath, "/name");
 	data = xs_read(xs, XBT_NULL, namepath, &len);
 	free(namepath);
-	if (!data)
-		return -1;
-	if (!len) {
+	if (data && !len) {
 		free(data);
-		return -1;
+		return NULL;
 	}
+	return data;
+}
+
+static int create_console_log(struct console *con)
+{
+	char logfile[PATH_MAX];
+	char *data;
+	int fd;
+
+	data = read_domain_name(con->d);
+	if (!data)
+		return -1;
 
 	snprintf(logfile, PATH_MAX-1, "%s/guest-%s%s.log",
 		 log_dir, data, con->log_suffix);
@@ -482,6 +554,45 @@ static int create_console_log(struct console *con)
 	return fd;
 }
 
+static struct syslog_line *create_console_syslog(struct console *con)
+{
+	struct syslog_line *line;
+	char *name, *p;
+	size_t len, name_max;
+
+	name = read_domain_name(con->d);
+	if (!name)
+		return NULL;
+
+	line = calloc(1, sizeof(*line));
+	if (line == NULL) {
+		dolog(LOG_ERR, "Memory allocation failed");
+		exit(ENOMEM);
+	}
+
+	/* Keep the suffix and whole UTF-8 characters if the name is cut. */
+	name_max = sizeof(line->ident) - strlen("guest-") -
+		   strlen(con->log_suffix) - 1;
+	len = strlen(name);
+	if (len > name_max) {
+		len = name_max;
+		while (len && ((unsigned char)name[len] & 0xc0) == 0x80)
+			len--;
+	}
+	snprintf(line->ident, sizeof(line->ident), "guest-%.*s%s",
+		 (int)len, name, con->log_suffix);
+	free(name);
+
+	/* Replace ASCII that could break tag parsing; keep UTF-8 bytes. */
+	for (p = line->ident; *p; p++)
+		if (!((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') ||
+		      (*p >= '0' && *p <= '9') || *p == '.' || *p == '_' ||
+		      *p == '-' || (unsigned char)*p >= 0x80))
+			*p = '_';
+
+	return line;
+}
+
 static void console_close_tty(struct console *con)
 {
 	if (con->master_fd != -1) {
@@ -780,6 +891,9 @@ static int console_create_ring(struct console *con)
 	if (log_guest && (con->log_fd == -1))
 		con->log_fd = create_console_log(con);
 
+	if (syslog_guest && !con->syslog)
+		con->syslog = create_console_syslog(con);
+
  out:
 	/* Mark the console connected. */
 	if (!err && con->interface)
@@ -827,6 +941,7 @@ static int console_init(struct console *con, struct domain *dom, void **data)
 	con->master_pollfd_idx = -1;
 	con->slave_fd = -1;
 	con->log_fd = -1;
+	con->syslog = NULL;
 	con->ring_ref = -1;
 	con->local_port = -1;
 	con->remote_port = -1;
@@ -916,6 +1031,12 @@ static void remove_domain(struct domain *dom)
 	}
 }
 
+static void console_flush_syslog(struct console *con)
+{
+	if (con->syslog)
+		syslog_line_flush(con->syslog);
+}
+
 static void console_cleanup(struct console *con)
 {
 	if (con->log_fd != -1) {
@@ -923,6 +1044,12 @@ static void console_cleanup(struct console *con)
 		con->log_fd = -1;
 	}
 
+	if (con->syslog) {
+		syslog_line_flush(con->syslog);
+		free(con->syslog);
+		con->syslog = NULL;
+	}
+
 	free(con->buffer.data);
 	con->buffer.data = NULL;
 
@@ -1175,22 +1302,29 @@ static void handle_hv_logs(xenevtchn_handle *xce_handle, bool force)
 
 	do
 	{
-		int logret;
-
 		size = sizeof(buffer);
 		if (xc_readconsolering(xc, bufptr, &size, 0, 1, &index) != 0 ||
 		    size == 0)
 			break;
 
-		if (log_time_hv)
-			logret = write_with_timestamp(log_hv_fd, buffer, size,
-						      &log_time_hv_needts);
-		else
-			logret = write_all(log_hv_fd, buffer, size);
+		if (syslog_hv)
+			syslog_line_write(&hv_syslog, buffer, size);
 
-		if (logret < 0)
-			dolog(LOG_ERR, "Failed to write hypervisor log: "
-				       "%d (%s)", errno, strerror(errno));
+		if (log_hv) {
+			int logret;
+
+			if (log_time_hv)
+				logret = write_with_timestamp(
+					log_hv_fd, buffer, size,
+					&log_time_hv_needts);
+			else
+				logret = write_all(log_hv_fd, buffer, size);
+
+			if (logret < 0)
+				dolog(LOG_ERR,
+				      "Failed to write hypervisor log: %d (%s)",
+				      errno, strerror(errno));
+		}
 	} while (size == sizeof(buffer));
 
 	if (port != -1)
@@ -1324,17 +1458,20 @@ void handle_io(void)
 	int xce_pollfd_idx = -1;
 	int xs_pollfd_idx = -1;
 	xenevtchn_handle *xce_handle = NULL;
+	bool read_hv = log_hv || syslog_hv;
 
-	if (log_hv) {
+	if (read_hv) {
 		xce_handle = xenevtchn_open(NULL, 0);
 		if (xce_handle == NULL) {
 			dolog(LOG_ERR, "Failed to open xce handle: %d (%s)",
 			      errno, strerror(errno));
 			goto out;
 		}
-		log_hv_fd = create_hv_log();
-		if (log_hv_fd == -1)
-			goto out;
+		if (log_hv) {
+			log_hv_fd = create_hv_log();
+			if (log_hv_fd == -1)
+				goto out;
+		}
 		log_hv_evtchn = xenevtchn_bind_virq(xce_handle, VIRQ_CON_RING);
 		if (log_hv_evtchn == -1) {
 			dolog(LOG_ERR, "Failed to bind to VIRQ_CON_RING: "
@@ -1371,7 +1508,7 @@ void handle_io(void)
 
 		xs_pollfd_idx = set_fds(xs_fileno(xs), POLLIN|POLLPRI);
 
-		if (log_hv)
+		if (read_hv)
 			xce_pollfd_idx = set_fds(xenevtchn_fd(xce_handle),
 						 POLLIN|POLLPRI);
 
@@ -1421,7 +1558,7 @@ void handle_io(void)
 			break;
 		}
 
-		if (log_hv && xce_pollfd_idx != -1) {
+		if (read_hv && xce_pollfd_idx != -1) {
 			if (fds[xce_pollfd_idx].revents & ~(POLLIN|POLLOUT|POLLPRI)) {
 				dolog(LOG_ERR,
 				      "Failure in poll xce_handle: %d (%s)",
@@ -1472,6 +1609,14 @@ void handle_io(void)
 		close(log_hv_fd);
 		log_hv_fd = -1;
 	}
+	if (syslog_hv)
+		syslog_line_flush(&hv_syslog);
+	if (syslog_guest) {
+		struct domain *d;
+
+		for (d = dom_head; d; d = d->next)
+			console_iter_void_arg1(d, console_flush_syslog);
+	}
 	if (xce_handle != NULL) {
 		xenevtchn_close(xce_handle);
 		xce_handle = NULL;
diff --git a/tools/console/daemon/main.c b/tools/console/daemon/main.c
index 798077ac2f..56b2bb79d8 100644
--- a/tools/console/daemon/main.c
+++ b/tools/console/daemon/main.c
@@ -35,11 +35,14 @@
 int log_reload = 0;
 int log_guest = 0;
 int log_hv = 0;
+int syslog_guest = 0;
+int syslog_hv = 0;
 int log_time_hv = 0;
 int log_time_guest = 0;
 char *log_dir = NULL;
 int discard_overflowed_data = 1;
 int replace_escape = 0;
+int syslog_option = LOG_CONS;
 
 static void handle_hup(int sig)
 {
@@ -48,8 +51,9 @@ static void handle_hup(int sig)
 
 static void usage(char *name)
 {
-	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH] [-t, --timestamp=none|guest|hv|all] [-o, --overflow-data=discard|keep] [--replace-escape]\n", name);
+	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH] [-t, --timestamp=none|guest|hv|all] [-o, --overflow-data=discard|keep] [--replace-escape] [--syslog=none|guest|hv|all]\n", name);
 	printf("  --replace-escape  - replace ESC character with dot when writing console log\n");
+	printf("  --syslog          - send console output to syslog (independent of --log)\n");
 }
 
 static void version(char *name)
@@ -106,11 +110,11 @@ int main(int argc, char **argv)
 		{ "timestamp", 1, 0, 't' },
 		{ "overflow-data", 1, 0, 'o'},
 		{ "replace-escape", 0, 0, 'e'},
+		{ "syslog", 1, 0, 'S'},
 		{ 0 },
 	};
 	bool is_interactive = false;
 	int ch;
-	int syslog_option = LOG_CONS;
 	int syslog_mask = LOG_MASK(LOG_WARNING)|LOG_MASK(LOG_ERR)|LOG_MASK(LOG_CRIT)|\
 		          LOG_MASK(LOG_ALERT)|LOG_MASK(LOG_EMERG);
 	int opt_ind = 0;
@@ -173,6 +177,24 @@ int main(int argc, char **argv)
 		case 'e':
 			replace_escape = 1;
 			break;
+		case 'S':
+			if (!strcmp(optarg, "all")) {
+				syslog_hv = 1;
+				syslog_guest = 1;
+			} else if (!strcmp(optarg, "hv")) {
+				syslog_hv = 1;
+			} else if (!strcmp(optarg, "guest")) {
+				syslog_guest = 1;
+			} else if (!strcmp(optarg, "none")) {
+				syslog_hv = 0;
+				syslog_guest = 0;
+			} else {
+				fprintf(stderr, "Invalid --syslog value '%s'\n",
+					optarg);
+				usage(argv[0]);
+				exit(EINVAL);
+			}
+			break;
 		case '?':
 			fprintf(stderr,
 				"Try `%s --help' for more information\n",
-- 
2.52.0
Re: [XEN PATCH] tools/xenconsoled: add --syslog option
Posted by Changlei Li (李长磊) 2 hours ago
Adding the maintainers and reviewers to Cc, which I missed on the first send.
________________________________
From: Changlei Li (李长磊) <changlei.li@citrix.com>
Sent: Thursday, November 26, 2026 4:27 PM
To: Changlei Li (李长磊) <changlei.li@citrix.com>; xen-devel@lists.xenproject.org <xen-devel@lists.xenproject.org>
Subject: [XEN PATCH] tools/xenconsoled: add --syslog option

The hypervisor and guest console logs can only be written to files
today, so they are not part of the host's syslog stream and are not
forwarded with it.

Add a --syslog=none|guest|hv|all option, with the same values as --log.
Each line of console output is sent to syslog at LOG_INFO, facility
LOG_DAEMON. Hypervisor lines use the ident "hypervisor". Guest lines use
"guest-<name>" ("guest-<name>-vuart0" for the vUART), which matches the
guest log file name. xenconsoled keeps its own "xenconsoled" ident for
its messages, so the sources can be told apart without relying on
levels.

syslog(3) keeps one ident, option set and log mask per process. For
each console line, the ident, options and mask are switched and then
restored, so xenconsoled's own logging is unchanged. xenconsoled is
single-threaded, so no other message can be logged in between.

Each console has its own line buffer, and so does the hypervisor log.
A line is sent when its newline arrives, so a line written in several
parts becomes one syslog message. A line longer than 960 bytes is split.
A partial line is sent when a guest console is cleaned up, or when
xenconsoled leaves its main loop. It is lost when xenconsoled is killed,
for example by SIGTERM. A trailing '\r' is removed, as guest consoles
usually end lines with "\r\n". Guest output is passed to its buffer in
buffer_append(), where it is written to the guest log file, so overflow
discard does not affect it.

The guest name comes from xenstore. In the ident, ASCII characters other
than A-Z, a-z, 0-9, '.', '_' and '-' are replaced with '_', so the name
cannot break syslog tag parsing. Non-ASCII (UTF-8) bytes are kept, so
names in other scripts stay readable and distinct. The ident is limited
to 127 bytes, cut on a UTF-8 character boundary, and keeps the vUART
suffix. The xenstore read of the name is shared with the guest log file
code.

--log keeps controlling file output and the two options are independent,
so --log=none --syslog=hv sends the hypervisor log to syslog only.
--timestamp and --log-dir only apply to file output; --replace-escape
applies to both. An unknown --syslog value is an error.

Signed-off-by: Changlei Li <changlei.li@citrix.com>
---
 CHANGELOG.md                |   2 +
 tools/console/daemon/io.c   | 195 +++++++++++++++++++++++++++++++-----
 tools/console/daemon/main.c |  26 ++++-
 3 files changed, 196 insertions(+), 27 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index f967437eda..851c1ad6f6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
      afterwards.

 ### Added
+ - xenconsoled: new --syslog=none|guest|hv|all option to send console output
+   to syslog.

 ### Removed
  - On Arm:
diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c
index b6c46d11de..1090336bbe 100644
--- a/tools/console/daemon/io.c
+++ b/tools/console/daemon/io.c
@@ -63,16 +63,29 @@
 extern int log_reload;
 extern int log_guest;
 extern int log_hv;
+extern int syslog_guest;
+extern int syslog_hv;
 extern int log_time_hv;
 extern int log_time_guest;
 extern char *log_dir;
 extern int discard_overflowed_data;
 extern int replace_escape;
+extern int syslog_option;

 static int log_time_hv_needts = 1;
 static int log_time_guest_needts = 1;
 static int log_hv_fd = -1;

+struct syslog_line {
+       /* glibc keeps the whole ident; musl keeps at most 31 bytes. */
+       char ident[128];
+       size_t len;
+       /* No limit in glibc; fits musl's 1024-byte buffer with the header. */
+       char data[960];
+};
+
+static struct syslog_line hv_syslog = { .ident = "hypervisor" };
+
 static xengnttab_handle *xgt_handle = NULL;
 static xenforeignmemory_handle *xfm_handle;

@@ -94,6 +107,7 @@ struct console {
         int master_pollfd_idx;
         int slave_fd;
         int log_fd;
+       struct syslog_line *syslog;
         struct buffer buffer;
         char *xspath;
         const char *log_suffix;
@@ -296,6 +310,53 @@ static int write_with_timestamp(int fd, const char *data, size_t sz,
         return 0;
 }

+static void syslog_line_flush(struct syslog_line *line)
+{
+       int mask;
+
+       /* Guest consoles usually end lines with "\r\n". */
+       while (line->len && line->data[line->len - 1] == '\r')
+               line->len--;
+       if (!line->len)
+               return;
+
+       /*
+        * Own tag, no flags and own mask, restored afterwards; safe as we are
+        * single-threaded. No LOG_CONS/LOG_PERROR: the line must not loop
+        * back into the console ring or be duplicated on stderr.
+        */
+       mask = setlogmask(LOG_MASK(LOG_INFO));
+       openlog(line->ident, 0, LOG_DAEMON);
+       syslog(LOG_INFO, "%.*s", (int)line->len, line->data);
+       openlog("xenconsoled", syslog_option, LOG_DAEMON);
+       setlogmask(mask);
+       line->len = 0;
+}
+
+static void syslog_line_write(struct syslog_line *line, const char *data,
+                             size_t sz)
+{
+       size_t i;
+
+       for (i = 0; i < sz; i++) {
+               char c = data[i];
+
+               if (c == '\n') {
+                       syslog_line_flush(line);
+                       continue;
+               }
+               /* Strip \r after a newline, like write_with_timestamp(). */
+               if (c == '\r' && !line->len)
+                       continue;
+               if (c == '\033' && replace_escape)
+                       c = '.';
+
+               line->data[line->len++] = c;
+               if (line->len == sizeof(line->data))
+                       syslog_line_flush(line);
+       }
+}
+
 static inline bool buffer_available(struct console *con)
 {
         if (discard_overflowed_data ||
@@ -361,6 +422,10 @@ static void buffer_append(struct console *con)
                               dom->domid, errno, strerror(errno));
         }

+       if (con->syslog)
+               syslog_line_write(con->syslog,
+                                 buffer->data + buffer->size - size, size);
+
         if (discard_overflowed_data && buffer->max_capacity &&
             buffer->size > 5 * buffer->max_capacity / 4) {
                 if (buffer->consumed > buffer->max_capacity / 4) {
@@ -433,30 +498,37 @@ static int create_hv_log(void)
         return fd;
 }

-static int create_console_log(struct console *con)
+static char *read_domain_name(struct domain *dom)
 {
-       char logfile[PATH_MAX];
         char *namepath, *data, *s;
-       int fd;
         unsigned int len;
-       struct domain *dom = con->d;

         namepath = xs_get_domain_path(xs, dom->domid);
         s = realloc(namepath, strlen(namepath) + 6);
         if (s == NULL) {
                 free(namepath);
-               return -1;
+               return NULL;
         }
         namepath = s;
         strcat(namepath, "/name");
         data = xs_read(xs, XBT_NULL, namepath, &len);
         free(namepath);
-       if (!data)
-               return -1;
-       if (!len) {
+       if (data && !len) {
                 free(data);
-               return -1;
+               return NULL;
         }
+       return data;
+}
+
+static int create_console_log(struct console *con)
+{
+       char logfile[PATH_MAX];
+       char *data;
+       int fd;
+
+       data = read_domain_name(con->d);
+       if (!data)
+               return -1;

         snprintf(logfile, PATH_MAX-1, "%s/guest-%s%s.log",
                  log_dir, data, con->log_suffix);
@@ -482,6 +554,45 @@ static int create_console_log(struct console *con)
         return fd;
 }

+static struct syslog_line *create_console_syslog(struct console *con)
+{
+       struct syslog_line *line;
+       char *name, *p;
+       size_t len, name_max;
+
+       name = read_domain_name(con->d);
+       if (!name)
+               return NULL;
+
+       line = calloc(1, sizeof(*line));
+       if (line == NULL) {
+               dolog(LOG_ERR, "Memory allocation failed");
+               exit(ENOMEM);
+       }
+
+       /* Keep the suffix and whole UTF-8 characters if the name is cut. */
+       name_max = sizeof(line->ident) - strlen("guest-") -
+                  strlen(con->log_suffix) - 1;
+       len = strlen(name);
+       if (len > name_max) {
+               len = name_max;
+               while (len && ((unsigned char)name[len] & 0xc0) == 0x80)
+                       len--;
+       }
+       snprintf(line->ident, sizeof(line->ident), "guest-%.*s%s",
+                (int)len, name, con->log_suffix);
+       free(name);
+
+       /* Replace ASCII that could break tag parsing; keep UTF-8 bytes. */
+       for (p = line->ident; *p; p++)
+               if (!((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') ||
+                     (*p >= '0' && *p <= '9') || *p == '.' || *p == '_' ||
+                     *p == '-' || (unsigned char)*p >= 0x80))
+                       *p = '_';
+
+       return line;
+}
+
 static void console_close_tty(struct console *con)
 {
         if (con->master_fd != -1) {
@@ -780,6 +891,9 @@ static int console_create_ring(struct console *con)
         if (log_guest && (con->log_fd == -1))
                 con->log_fd = create_console_log(con);

+       if (syslog_guest && !con->syslog)
+               con->syslog = create_console_syslog(con);
+
  out:
         /* Mark the console connected. */
         if (!err && con->interface)
@@ -827,6 +941,7 @@ static int console_init(struct console *con, struct domain *dom, void **data)
         con->master_pollfd_idx = -1;
         con->slave_fd = -1;
         con->log_fd = -1;
+       con->syslog = NULL;
         con->ring_ref = -1;
         con->local_port = -1;
         con->remote_port = -1;
@@ -916,6 +1031,12 @@ static void remove_domain(struct domain *dom)
         }
 }

+static void console_flush_syslog(struct console *con)
+{
+       if (con->syslog)
+               syslog_line_flush(con->syslog);
+}
+
 static void console_cleanup(struct console *con)
 {
         if (con->log_fd != -1) {
@@ -923,6 +1044,12 @@ static void console_cleanup(struct console *con)
                 con->log_fd = -1;
         }

+       if (con->syslog) {
+               syslog_line_flush(con->syslog);
+               free(con->syslog);
+               con->syslog = NULL;
+       }
+
         free(con->buffer.data);
         con->buffer.data = NULL;

@@ -1175,22 +1302,29 @@ static void handle_hv_logs(xenevtchn_handle *xce_handle, bool force)

         do
         {
-               int logret;
-
                 size = sizeof(buffer);
                 if (xc_readconsolering(xc, bufptr, &size, 0, 1, &index) != 0 ||
                     size == 0)
                         break;

-               if (log_time_hv)
-                       logret = write_with_timestamp(log_hv_fd, buffer, size,
-                                                     &log_time_hv_needts);
-               else
-                       logret = write_all(log_hv_fd, buffer, size);
+               if (syslog_hv)
+                       syslog_line_write(&hv_syslog, buffer, size);

-               if (logret < 0)
-                       dolog(LOG_ERR, "Failed to write hypervisor log: "
-                                      "%d (%s)", errno, strerror(errno));
+               if (log_hv) {
+                       int logret;
+
+                       if (log_time_hv)
+                               logret = write_with_timestamp(
+                                       log_hv_fd, buffer, size,
+                                       &log_time_hv_needts);
+                       else
+                               logret = write_all(log_hv_fd, buffer, size);
+
+                       if (logret < 0)
+                               dolog(LOG_ERR,
+                                     "Failed to write hypervisor log: %d (%s)",
+                                     errno, strerror(errno));
+               }
         } while (size == sizeof(buffer));

         if (port != -1)
@@ -1324,17 +1458,20 @@ void handle_io(void)
         int xce_pollfd_idx = -1;
         int xs_pollfd_idx = -1;
         xenevtchn_handle *xce_handle = NULL;
+       bool read_hv = log_hv || syslog_hv;

-       if (log_hv) {
+       if (read_hv) {
                 xce_handle = xenevtchn_open(NULL, 0);
                 if (xce_handle == NULL) {
                         dolog(LOG_ERR, "Failed to open xce handle: %d (%s)",
                               errno, strerror(errno));
                         goto out;
                 }
-               log_hv_fd = create_hv_log();
-               if (log_hv_fd == -1)
-                       goto out;
+               if (log_hv) {
+                       log_hv_fd = create_hv_log();
+                       if (log_hv_fd == -1)
+                               goto out;
+               }
                 log_hv_evtchn = xenevtchn_bind_virq(xce_handle, VIRQ_CON_RING);
                 if (log_hv_evtchn == -1) {
                         dolog(LOG_ERR, "Failed to bind to VIRQ_CON_RING: "
@@ -1371,7 +1508,7 @@ void handle_io(void)

                 xs_pollfd_idx = set_fds(xs_fileno(xs), POLLIN|POLLPRI);

-               if (log_hv)
+               if (read_hv)
                         xce_pollfd_idx = set_fds(xenevtchn_fd(xce_handle),
                                                  POLLIN|POLLPRI);

@@ -1421,7 +1558,7 @@ void handle_io(void)
                         break;
                 }

-               if (log_hv && xce_pollfd_idx != -1) {
+               if (read_hv && xce_pollfd_idx != -1) {
                         if (fds[xce_pollfd_idx].revents & ~(POLLIN|POLLOUT|POLLPRI)) {
                                 dolog(LOG_ERR,
                                       "Failure in poll xce_handle: %d (%s)",
@@ -1472,6 +1609,14 @@ void handle_io(void)
                 close(log_hv_fd);
                 log_hv_fd = -1;
         }
+       if (syslog_hv)
+               syslog_line_flush(&hv_syslog);
+       if (syslog_guest) {
+               struct domain *d;
+
+               for (d = dom_head; d; d = d->next)
+                       console_iter_void_arg1(d, console_flush_syslog);
+       }
         if (xce_handle != NULL) {
                 xenevtchn_close(xce_handle);
                 xce_handle = NULL;
diff --git a/tools/console/daemon/main.c b/tools/console/daemon/main.c
index 798077ac2f..56b2bb79d8 100644
--- a/tools/console/daemon/main.c
+++ b/tools/console/daemon/main.c
@@ -35,11 +35,14 @@
 int log_reload = 0;
 int log_guest = 0;
 int log_hv = 0;
+int syslog_guest = 0;
+int syslog_hv = 0;
 int log_time_hv = 0;
 int log_time_guest = 0;
 char *log_dir = NULL;
 int discard_overflowed_data = 1;
 int replace_escape = 0;
+int syslog_option = LOG_CONS;

 static void handle_hup(int sig)
 {
@@ -48,8 +51,9 @@ static void handle_hup(int sig)

 static void usage(char *name)
 {
-       printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH] [-t, --timestamp=none|guest|hv|all] [-o, --overflow-data=discard|keep] [--replace-escape]\n", name);
+       printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH] [-t, --timestamp=none|guest|hv|all] [-o, --overflow-data=discard|keep] [--replace-escape] [--syslog=none|guest|hv|all]\n", name);
         printf("  --replace-escape  - replace ESC character with dot when writing console log\n");
+       printf("  --syslog          - send console output to syslog (independent of --log)\n");
 }

 static void version(char *name)
@@ -106,11 +110,11 @@ int main(int argc, char **argv)
                 { "timestamp", 1, 0, 't' },
                 { "overflow-data", 1, 0, 'o'},
                 { "replace-escape", 0, 0, 'e'},
+               { "syslog", 1, 0, 'S'},
                 { 0 },
         };
         bool is_interactive = false;
         int ch;
-       int syslog_option = LOG_CONS;
         int syslog_mask = LOG_MASK(LOG_WARNING)|LOG_MASK(LOG_ERR)|LOG_MASK(LOG_CRIT)|\
                           LOG_MASK(LOG_ALERT)|LOG_MASK(LOG_EMERG);
         int opt_ind = 0;
@@ -173,6 +177,24 @@ int main(int argc, char **argv)
                 case 'e':
                         replace_escape = 1;
                         break;
+               case 'S':
+                       if (!strcmp(optarg, "all")) {
+                               syslog_hv = 1;
+                               syslog_guest = 1;
+                       } else if (!strcmp(optarg, "hv")) {
+                               syslog_hv = 1;
+                       } else if (!strcmp(optarg, "guest")) {
+                               syslog_guest = 1;
+                       } else if (!strcmp(optarg, "none")) {
+                               syslog_hv = 0;
+                               syslog_guest = 0;
+                       } else {
+                               fprintf(stderr, "Invalid --syslog value '%s'\n",
+                                       optarg);
+                               usage(argv[0]);
+                               exit(EINVAL);
+                       }
+                       break;
                 case '?':
                         fprintf(stderr,
                                 "Try `%s --help' for more information\n",
--
2.52.0