xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Jean Guyader <jean.guyader@gmail.com>
To: xen-devel@lists.xen.org
Subject: [xenconsoled] Add syslog (v2)
Date: Thu, 24 May 2012 09:33:10 +0100	[thread overview]
Message-ID: <CAEBdQ90uE_J7mnSVAOGpyazkSL6D08a43QVyEFhqeDzEp8UORA@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 335 bytes --]

Changes in v2:
  - Create a new function to clear \r\n and write to syslog
  - Remove tmp buffer

Introduce a new option (-s/--syslog) to make xenconsoled log into syslog.
The default behaviour remains the same (log into plain files)

From: Eric Chanudet <eric.chanudet@citrix.com>
Signed-off-by: Jean Guyader <jean.guyader@gmail.com>

[-- Attachment #1.2: Type: text/html, Size: 546 bytes --]

[-- Attachment #2: xenconsoled-syslog.patch --]
[-- Type: text/x-patch, Size: 5677 bytes --]

diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c
index b6d41de..82ad2bb 100644
--- a/tools/console/daemon/io.c
+++ b/tools/console/daemon/io.c
@@ -62,6 +62,7 @@ extern int log_time_hv;
 extern int log_time_guest;
 extern char *log_dir;
 extern int discard_overflowed_data;
+extern int use_syslog;
 
 static int log_time_hv_needts = 1;
 static int log_time_guest_needts = 1;
@@ -80,6 +81,7 @@ struct buffer {
 
 struct domain {
 	int domid;
+	char *name;
 	int master_fd;
 	int slave_fd;
 	int log_fd;
@@ -113,6 +115,36 @@ static int write_all(int fd, const char* buf, size_t len)
 	return 0;
 }
 
+static void write_syslog(struct domain *dom, const char *data, size_t sz)
+{
+	char *buff = NULL;
+        const char *start, *p;
+        int i = 0;
+
+	start = p = data;
+        for (i = 0; i < sz; i++, p++)
+        {
+            if (*p == '\r' || *p == '\n')
+            {
+                buff = realloc(buff, p - start + 1);
+
+                memcpy(buff, start, p - start);
+                buff[p - start] = 0;
+
+                if (buff[0] == 0 || buff[0] == '\n')
+                    goto next;
+
+                if (dom == NULL) {
+                    syslog(LOG_INFO, "hypervisor: %s", buff);
+                } else {
+                    syslog(LOG_INFO, "guest-%s (%i): %s", dom->name, dom->domid, buff);
+                }
+next:
+                start = p + 1;
+            }
+        }
+}
+
 static int write_with_timestamp(int fd, const char *data, size_t sz,
 				int *needts)
 {
@@ -152,8 +184,8 @@ static void buffer_append(struct domain *dom)
 
 	cons = intf->out_cons;
 	prod = intf->out_prod;
-	xen_mb();
 
+	xen_mb();
 	size = prod - cons;
 	if ((size == 0) || (size > sizeof(intf->out)))
 		return;
@@ -196,6 +228,8 @@ static void buffer_append(struct domain *dom)
 			dolog(LOG_ERR, "Write to log failed "
 			      "on domain %d: %d (%s)\n",
 			      dom->domid, errno, strerror(errno));
+	} else if (use_syslog) {
+		write_syslog(dom, buffer->data + buffer->size - size, size);
 	}
 
 	if (discard_overflowed_data && buffer->max_capacity &&
@@ -293,14 +327,14 @@ static int create_domain_log(struct domain *dom)
 	data = xs_read(xs, XBT_NULL, namepath, &len);
 	free(namepath);
 	if (!data)
-		return -1;
+		goto out;
 	if (!len) {
 		free(data);
-		return -1;
+		goto out;
 	}
 
+	dom->name = data;
 	snprintf(logfile, PATH_MAX-1, "%s/guest-%s.log", log_dir, data);
-	free(data);
 	logfile[PATH_MAX-1] = '\0';
 
 	fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0644);
@@ -318,6 +352,9 @@ static int create_domain_log(struct domain *dom)
 		}
 	}
 	return fd;
+out:
+        dom->name = strdup("unamed");
+        return -1;
 }
 
 static void domain_close_tty(struct domain *dom)
@@ -656,6 +693,7 @@ static struct domain *create_domain(int domid)
 	dom->remote_port = -1;
 	dom->interface = NULL;
 	dom->xce_handle = NULL;
+	dom->name = NULL;
 
 	if (!watch_domain(dom, true))
 		goto out;
@@ -712,6 +750,11 @@ static void cleanup_domain(struct domain *d)
 	free(d->conspath);
 	d->conspath = NULL;
 
+        if (d->name) {
+	    free(d->name);
+	    d->name = NULL;
+        }
+
 	remove_domain(d);
 }
 
@@ -888,17 +931,23 @@ static void handle_hv_logs(void)
 		return;
 
 	if (xc_readconsolering(xch, bufptr, &size, 0, 1, &index) == 0 && size > 0) {
-		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 (log_hv_fd != -1) {
+			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));
+		}
 
-		if (logret < 0)
-			dolog(LOG_ERR, "Failed to write hypervisor log: "
-				       "%d (%s)", errno, strerror(errno));
-	}
+		if (use_syslog)
+			write_syslog(NULL, buffer, size);
+
+        }
 
 	(void)xc_evtchn_unmask(xce_handle, port);
 }
@@ -940,8 +989,6 @@ void handle_io(void)
 			goto out;
 		}
 		log_hv_fd = create_hv_log();
-		if (log_hv_fd == -1)
-			goto out;
 		log_hv_evtchn = xc_evtchn_bind_virq(xce_handle, VIRQ_CON_RING);
 		if (log_hv_evtchn == -1) {
 			dolog(LOG_ERR, "Failed to bind to VIRQ_CON_RING: "
diff --git a/tools/console/daemon/main.c b/tools/console/daemon/main.c
index 789baa6..2c7e267 100644
--- a/tools/console/daemon/main.c
+++ b/tools/console/daemon/main.c
@@ -39,6 +39,7 @@ int log_time_hv = 0;
 int log_time_guest = 0;
 char *log_dir = NULL;
 int discard_overflowed_data = 1;
+int use_syslog = 0;
 
 static void handle_hup(int sig)
 {
@@ -47,7 +48,7 @@ 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]\n", name);
+	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--syslog] [--log-dir=DIR] [--pid-file=PATH] [-t, --timestamp=none|guest|hv|all] [-o, --overflow-data=discard|keep]\n", name);
 }
 
 static void version(char *name)
@@ -68,6 +69,7 @@ int main(int argc, char **argv)
 		{ "pid-file", 1, 0, 'p' },
 		{ "timestamp", 1, 0, 't' },
 		{ "overflow-data", 1, 0, 'o'},
+		{ "syslog", 0, 0, 's' },
 		{ 0 },
 	};
 	bool is_interactive = false;
@@ -106,6 +108,9 @@ int main(int argc, char **argv)
 			      log_guest = 1;
 			}
 			break;
+		case 's':
+                        use_syslog = 1;
+                        break;
 		case 'r':
 		        log_dir = strdup(optarg);
 			break;

[-- Attachment #3: Type: text/plain, Size: 126 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

                 reply	other threads:[~2012-05-24  8:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAEBdQ90uE_J7mnSVAOGpyazkSL6D08a43QVyEFhqeDzEp8UORA@mail.gmail.com \
    --to=jean.guyader@gmail.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).