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;