From patchwork Mon Jun 6 15:59:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 9158707 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5EF8460573 for ; Mon, 6 Jun 2016 16:01:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4FFE020700 for ; Mon, 6 Jun 2016 16:01:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 44ED826B41; Mon, 6 Jun 2016 16:01:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D502720700 for ; Mon, 6 Jun 2016 16:01:52 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b9wwN-0007Ix-VC; Mon, 06 Jun 2016 15:59:47 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b9wwL-0007IO-Of for xen-devel@lists.xenproject.org; Mon, 06 Jun 2016 15:59:45 +0000 Received: from [193.109.254.147] by server-7.bemta-14.messagelabs.com id 89/31-25102-1FD95575; Mon, 06 Jun 2016 15:59:45 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprAIsWRWlGSWpSXmKPExsXitHRDpO6HuaH hBqsui1p83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBn7em6yF7SrVew8t4exgXGnXBcjJ4eEgL/E 98VnWUBsNgFliZ+dvWwgtoiAnkTTgeeMIDazgJfEjxU97CC2sECQxNYdV1hBbBYBFYk/2xqZQ GxeAWeJ3tZ+FoiZchLnj/9kBrE5BVwkzvd9BesVAqqZe2selK0g0TH9GFSvoMTJmU9YIHZJSB x88QKolwNoDrfE3277CYx8s5BUzUJStYCRaRWjRnFqUVlqka6hqV5SUWZ6RkluYmaOrqGhiV5 uanFxYnpqTmJSsV5yfu4mRmBI1TMwMO5g/Hva8xCjJAeTkijvjv7QcCG+pPyUyozE4oz4otKc 1OJDjDIcHEoSvM1zgHKCRanpqRVpmTnA4IZJS3DwKInw3p8NlOYtLkjMLc5Mh0idYlSUEueNB OkTAElklObBtcEi6hKjrJQwLyMDA4MQT0FqUW5mCar8K0ZxDkYlYd5pIFN4MvNK4Ka/AlrMBL R42bVgkMUliQgpqQbGWZ1rXDfMTVze8n7lC5U8eZ7Ct/MmzThSvd/6xFLXFfb2Wex7zKUeVv1 axfx5h/LXWsXD92a1cL+sT6vnVXYO/Rk8fb0UpyjzrT+3F91qVWermDyNpXJBXdmc73/dy1lM DyhF/WCY/HDu3AlTnnDxVUgVavf4dSxz0t235oeH4Yd/WceUrjJ+V2Ipzkg01GIuKk4EAO6Vu GWjAgAA X-Env-Sender: prvs=9584f9c1d=wei.liu2@citrix.com X-Msg-Ref: server-12.tower-27.messagelabs.com!1465228782!46060827!1 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 8.46; banners=-,-,- X-VirusChecked: Checked Received: (qmail 53987 invoked from network); 6 Jun 2016 15:59:44 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-12.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 6 Jun 2016 15:59:44 -0000 X-IronPort-AV: E=Sophos;i="5.26,427,1459814400"; d="scan'208";a="358699059" From: Wei Liu To: Xen-devel Date: Mon, 6 Jun 2016 16:59:37 +0100 Message-ID: <1465228781-22754-3-git-send-email-wei.liu2@citrix.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1465228781-22754-1-git-send-email-wei.liu2@citrix.com> References: <1465228781-22754-1-git-send-email-wei.liu2@citrix.com> MIME-Version: 1.0 X-DLP: MIA1 Cc: Wei Liu , Ian Jackson Subject: [Xen-devel] [PATCH 2/6] xenconsoled: switch hypervisor log to use logfile abstraction X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP To minimise code churn, copy and paste a some existing functions and adapt them to write to logfile. The functions to deal with fd directly will go away eventually. Signed-off-by: Wei Liu --- tools/console/daemon/io.c | 95 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 71 insertions(+), 24 deletions(-) diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c index 7e6a886..228c4af 100644 --- a/tools/console/daemon/io.c +++ b/tools/console/daemon/io.c @@ -21,6 +21,7 @@ #include "utils.h" #include "io.h" +#include "logfile.h" #include #include #include @@ -71,7 +72,7 @@ extern int discard_overflowed_data; static int log_time_hv_needts = 1; static int log_time_guest_needts = 1; -static int log_hv_fd = -1; +static struct logfile *log_hv_file; static xengnttab_handle *xgt_handle = NULL; @@ -127,6 +128,15 @@ static int write_all(int fd, const char* buf, size_t len) return 0; } +static int write_logfile(struct logfile *logfile, const char *buf, + size_t len) +{ + ssize_t r = logfile_append(logfile, buf, len); + + if (r < 0) return -1; + return 0; +} + static int write_with_timestamp(int fd, const char *data, size_t sz, int *needts) { @@ -158,6 +168,38 @@ static int write_with_timestamp(int fd, const char *data, size_t sz, return 0; } +static int write_logfile_with_timestamp(struct logfile *logfile, + const char *data, size_t sz, + int *needts) +{ + char ts[32]; + time_t now = time(NULL); + const struct tm *tmnow = localtime(&now); + size_t tslen = strftime(ts, sizeof(ts), "[%Y-%m-%d %H:%M:%S] ", tmnow); + const char *last_byte = data + sz - 1; + + while (data <= last_byte) { + const char *nl = memchr(data, '\n', last_byte + 1 - data); + int found_nl = (nl != NULL); + if (!found_nl) + nl = last_byte; + + if ((*needts && logfile_append(logfile, ts, tslen)) + || logfile_append(logfile, data, nl + 1 - data)) + return -1; + + *needts = found_nl; + data = nl + 1; + if (found_nl) { + // If we printed a newline, strip all \r following it + while (data <= last_byte && *data == '\r') + data++; + } + } + + return 0; +} + static void buffer_append(struct domain *dom) { struct buffer *buffer = &dom->buffer; @@ -265,29 +307,33 @@ static bool domain_is_valid(int domid) return ret; } -static int create_hv_log(void) +static struct logfile *create_hv_log(void) { char logfile[PATH_MAX]; - int fd; + struct logfile *tmp; + snprintf(logfile, PATH_MAX-1, "%s/hypervisor.log", log_dir); logfile[PATH_MAX-1] = '\0'; - fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0644); - if (fd == -1) + tmp = logfile_new(logfile, 0644); + + if (!tmp) dolog(LOG_ERR, "Failed to open log %s: %d (%s)", logfile, errno, strerror(errno)); - if (fd != -1 && log_time_hv) { - if (write_with_timestamp(fd, "Logfile Opened\n", - strlen("Logfile Opened\n"), - &log_time_hv_needts) < 0) { + + if (tmp && log_time_hv) { + if (write_logfile_with_timestamp(tmp, "Logfile Opened\n", + strlen("Logfile Opened\n"), + &log_time_hv_needts) < 0) { dolog(LOG_ERR, "Failed to log opening timestamp " - "in %s: %d (%s)", logfile, errno, + "in %s: %d (%s)", tmp->basepath, errno, strerror(errno)); - close(fd); - return -1; + logfile_free(tmp); + return NULL; } } - return fd; + + return tmp; } static int create_domain_log(struct domain *dom) @@ -929,10 +975,11 @@ static void handle_hv_logs(xenevtchn_handle *xce_handle, bool force) break; if (log_time_hv) - logret = write_with_timestamp(log_hv_fd, buffer, size, - &log_time_hv_needts); + logret = write_logfile_with_timestamp(log_hv_file, + buffer, size, + &log_time_hv_needts); else - logret = write_all(log_hv_fd, buffer, size); + logret = write_logfile(log_hv_file, buffer, size); if (logret < 0) dolog(LOG_ERR, "Failed to write hypervisor log: " @@ -955,9 +1002,9 @@ static void handle_log_reload(void) } if (log_hv) { - if (log_hv_fd != -1) - close(log_hv_fd); - log_hv_fd = create_hv_log(); + if (log_hv_file) + logfile_free(log_hv_file); + log_hv_file = create_hv_log(); } } @@ -1017,8 +1064,8 @@ void handle_io(void) errno, strerror(errno)); goto out; } - log_hv_fd = create_hv_log(); - if (log_hv_fd == -1) + log_hv_file = create_hv_log(); + if (!log_hv_file) goto out; log_hv_evtchn = xenevtchn_bind_virq(xce_handle, VIRQ_CON_RING); if (log_hv_evtchn == -1) { @@ -1199,9 +1246,9 @@ void handle_io(void) current_array_size = 0; out: - if (log_hv_fd != -1) { - close(log_hv_fd); - log_hv_fd = -1; + if (log_hv_file) { + logfile_free(log_hv_file); + log_hv_file = NULL; } if (xce_handle != NULL) { xenevtchn_close(xce_handle);