Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751481AbbEZG6o (ORCPT ); Tue, 26 May 2015 02:58:44 -0400 Received: from [133.145.228.5] ([133.145.228.5]:41504 "EHLO mail4.hitachi.co.jp" rhost-flags-FAIL-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1751378AbbEZG6f (ORCPT ); Tue, 26 May 2015 02:58:35 -0400 X-AuditID: 85900ec0-9cdc8b9000001a57-a4-55641978a916 Subject: [PATCH trace-cmd V6 3/7] trace-cmd/msg: Use poll(2) to wait for a message From: Masami Hiramatsu To: Steven Rostedt Cc: Yoshihiro YUNOMAE , Aaron Fabbri , linux-kernel@vger.kernel.org, cti.systems-productivity-manager.ts@hitachi.com, Divya Vyas , Hidehiro Kawai , yoshihiro.yunomae@aktsk.jp Date: Tue, 26 May 2015 15:55:28 +0900 Message-ID: <20150526065528.16023.377.stgit@localhost.localdomain> In-Reply-To: <20150526065522.16023.30813.stgit@localhost.localdomain> References: <20150526065522.16023.30813.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Brightmail-Tracker: AAAAAA== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2651 Lines: 99 From: Yoshihiro YUNOMAE Use poll(2) to wait for a message. If a client/server cannot send a message for any reasons, the current server/client will wait in a blocking read operation. So, we use poll(2) for avoiding remaining in a blocking state. Signed-off-by: Yoshihiro YUNOMAE Signed-off-by: Masami Hiramatsu --- Changes in V4: Change the argument of tracecmd_msg_recv_wait() Fix some typos --- trace-msg.c | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/trace-msg.c b/trace-msg.c index 5669dee..e3d4f3f 100644 --- a/trace-msg.c +++ b/trace-msg.c @@ -395,6 +395,27 @@ error: return -ENOMSG; } +#define MSG_WAIT_MSEC 5000 + +/* + * A return value of 0 indicates time-out + */ +static int tracecmd_msg_recv_wait(int fd, struct tracecmd_msg *msg) +{ + struct pollfd pfd; + int ret; + + pfd.fd = fd; + pfd.events = POLLIN; + ret = poll(&pfd, 1, MSG_WAIT_MSEC); + if (ret < 0) + return -errno; + else if (ret == 0) + return -ETIMEDOUT; + + return tracecmd_msg_recv(fd, msg); +} + static void *tracecmd_msg_buf_access(struct tracecmd_msg *msg, int offset) { return (void *)msg + offset; @@ -405,9 +426,12 @@ static int tracecmd_msg_wait_for_msg(int fd, struct tracecmd_msg *msg) u32 cmd; int ret; - ret = tracecmd_msg_recv(fd, msg); - if (ret < 0) + ret = tracecmd_msg_recv_wait(fd, msg); + if (ret < 0) { + if (ret == -ETIMEDOUT) + warning("Connection timed out\n"); return ret; + } cmd = ntohl(msg->cmd); if (cmd == MSG_CLOSE) @@ -487,9 +511,12 @@ int tracecmd_msg_initial_setting(int fd, int *cpus, int *pagesize) u32 cmd; msg = (struct tracecmd_msg *)buf; - ret = tracecmd_msg_recv(fd, msg); - if (ret < 0) + ret = tracecmd_msg_recv_wait(fd, msg); + if (ret < 0) { + if (ret == -ETIMEDOUT) + warning("Connection timed out\n"); return ret; + } cmd = ntohl(msg->cmd); if (cmd != MSG_TINIT) { @@ -627,9 +654,12 @@ int tracecmd_msg_collect_metadata(int ifd, int ofd) msg = (struct tracecmd_msg *)buf; do { - ret = tracecmd_msg_recv(ifd, msg); + ret = tracecmd_msg_recv_wait(ifd, msg); if (ret < 0) { - warning("reading client"); + if (ret == -ETIMEDOUT) + warning("Connection timed out\n"); + else + warning("reading client"); return ret; } -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/