From patchwork Mon Jun 6 15:59:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 9158701 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 5ECDE60573 for ; Mon, 6 Jun 2016 16:01:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F88620700 for ; Mon, 6 Jun 2016 16:01:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4398628327; Mon, 6 Jun 2016 16:01:49 +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 9C08520700 for ; Mon, 6 Jun 2016 16:01:48 +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 1b9wwO-0007Jc-Lq; Mon, 06 Jun 2016 15:59:48 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b9wwM-0007IZ-Ip for xen-devel@lists.xenproject.org; Mon, 06 Jun 2016 15:59:46 +0000 Received: from [193.109.254.147] by server-14.bemta-14.messagelabs.com id 2E/E3-05219-2FD95575; Mon, 06 Jun 2016 15:59:46 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupjkeJIrShJLcpLzFFi42JxWrrBXvfj3NB wg6/veSy+b5nM5MDocfjDFZYAxijWzLyk/IoE1oyeFZ4Fp5QqZv+bztLAeEa6i5GTQ0LAX+Lo +1vsIDabgLLEz85eNhBbREBPounAc0YQm1nAS+LHih6wGmEBX4nZfa/BbBYBFYmGl/eYQGxeA WeJ7l1PWSFmykmcP/6TGcTmFHCRON/3FaxeCKhm7q15ULaCRMf0Y1C9ghInZz5hgdglIXHwxQ ugXg6gOdwSf7vtJzDyzUJSNQtJ1QJGplWM6sWpRWWpRbpGeklFmekZJbmJmTm6hoYmermpxcW J6ak5iUnFesn5uZsYgeHEAAQ7GFvmOB9ilORgUhLl3dEfGi7El5SfUpmRWJwRX1Sak1p8iFGG g0NJgvfmHKCcYFFqempFWmYOMLBh0hIcPEoivMdA0rzFBYm5xZnpEKlTjIpS4ryHQBICIImM0 jy4Nlg0XWKUlRLmZQQ6RIinILUoN7MEVf4VozgHo5Iw7zSQKTyZeSVw018BLWYCWrzsWjDI4p JEhJRUA6PPYw+1U2den+bo72RpdteXTvke6Ni8+/I97XMbdx/dMv9H0aZ0keAXZUw68lxdRx8 rnRFy+a+pseAm6wqtrYcnXT9wZOkd/sKsea0nVR/Jysa8FTly9dtig58/nzlLJrLZbus9UNzA xawR/Sr+Qp6GEP+FwPfcWVONNt2zz3izQtk4pudSVJkSS3FGoqEWc1FxIgAlHrqioQIAAA== X-Env-Sender: prvs=9584f9c1d=wei.liu2@citrix.com X-Msg-Ref: server-11.tower-27.messagelabs.com!1465228782!37504805!3 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 8.46; banners=-,-,- X-VirusChecked: Checked Received: (qmail 2263 invoked from network); 6 Jun 2016 15:59:44 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-11.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="365531439" From: Wei Liu To: Xen-devel Date: Mon, 6 Jun 2016 16:59:38 +0100 Message-ID: <1465228781-22754-4-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 3/6] xenconsoled: switch guest 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 Note that this causes write_with_timestamp to have no caller. Mark it as unused for now to minimise code churn. Signed-off-by: Wei Liu Acked-by: Ian Jackson --- tools/console/daemon/io.c | 67 +++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c index 228c4af..c2f63e6 100644 --- a/tools/console/daemon/io.c +++ b/tools/console/daemon/io.c @@ -95,7 +95,7 @@ struct domain { int master_fd; int master_pollfd_idx; int slave_fd; - int log_fd; + struct logfile *log; bool is_dead; unsigned last_seen; struct buffer buffer; @@ -137,8 +137,9 @@ static int write_logfile(struct logfile *logfile, const char *buf, return 0; } -static int write_with_timestamp(int fd, const char *data, size_t sz, - int *needts) +static __attribute__((unused)) +int write_with_timestamp(int fd, const char *data, size_t sz, + int *needts) { char ts[32]; time_t now = time(NULL); @@ -235,16 +236,16 @@ static void buffer_append(struct domain *dom) * no one is listening on the console pty then it will fill up * and handle_tty_write will stop being called. */ - if (dom->log_fd != -1) { + if (dom->log) { int logret; if (log_time_guest) { - logret = write_with_timestamp( - dom->log_fd, + logret = write_logfile_with_timestamp( + dom->log, buffer->data + buffer->size - size, size, &log_time_guest_needts); } else { - logret = write_all( - dom->log_fd, + logret = write_logfile( + dom->log, buffer->data + buffer->size - size, size); } @@ -336,50 +337,53 @@ static struct logfile *create_hv_log(void) return tmp; } -static int create_domain_log(struct domain *dom) +static struct logfile *create_domain_log(struct domain *dom) { char logfile[PATH_MAX]; char *namepath, *data, *s; - int fd; unsigned int len; + struct logfile *tmp; 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; + return NULL; if (!len) { free(data); - return -1; + return NULL; } 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); - 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_guest) { - if (write_with_timestamp(fd, "Logfile Opened\n", - strlen("Logfile Opened\n"), - &log_time_guest_needts) < 0) { + + if (tmp && log_time_guest) { + if (write_logfile_with_timestamp(tmp, "Logfile Opened\n", + strlen("Logfile Opened\n"), + &log_time_guest_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 void domain_close_tty(struct domain *dom) @@ -665,8 +669,8 @@ static int domain_create_ring(struct domain *dom) } } - if (log_guest && (dom->log_fd == -1)) - dom->log_fd = create_domain_log(dom); + if (log_guest && !dom->log) + dom->log = create_domain_log(dom); out: return err; @@ -724,7 +728,6 @@ static struct domain *create_domain(int domid) dom->master_fd = -1; dom->master_pollfd_idx = -1; dom->slave_fd = -1; - dom->log_fd = -1; dom->xce_pollfd_idx = -1; dom->next_period = ((long long)ts.tv_sec * 1000) + (ts.tv_nsec / 1000000) + RATE_LIMIT_PERIOD; @@ -777,9 +780,9 @@ static void cleanup_domain(struct domain *d) { domain_close_tty(d); - if (d->log_fd != -1) { - close(d->log_fd); - d->log_fd = -1; + if (d->log) { + logfile_free(d->log); + d->log = NULL; } free(d->buffer.data); @@ -995,9 +998,9 @@ static void handle_log_reload(void) if (log_guest) { struct domain *d; for (d = dom_head; d; d = d->next) { - if (d->log_fd != -1) - close(d->log_fd); - d->log_fd = create_domain_log(d); + if (d->log) + logfile_free(d->log); + d->log = create_domain_log(d); } }