From patchwork Tue Nov 12 20:31:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Marchand X-Patchwork-Id: 62909 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2F841A0353; Tue, 12 Nov 2019 21:32:03 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id A41E0397D; Tue, 12 Nov 2019 21:32:01 +0100 (CET) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by dpdk.org (Postfix) with ESMTP id 18FD32C15 for ; Tue, 12 Nov 2019 21:32:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573590719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QDG237trJwoMSZ4sy3ST+pDShtME1ChapwQ/uTTDVpQ=; b=FRRPhkPhBh+IJaZ1lRnmnQrL3oG0fqxKYr7P8oA0S/z/CI0sZ5hpA97wjcI1NKqama3hnk DNzaEXfPf/iQmv78/PpSvOmVbZEyhlhWVt9y4+qPk2phQg3/JnH9rY+fjV20+6greQQV9f rrzyhSdLP4oYlVETekMnGQMI2sQX8n4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-191-HPt7vpGoOoC-SH8f58BgkQ-1; Tue, 12 Nov 2019 15:31:55 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D66DADBA6; Tue, 12 Nov 2019 20:31:54 +0000 (UTC) Received: from dmarchan.remote.csb (unknown [10.40.205.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id 690914D6F1; Tue, 12 Nov 2019 20:31:53 +0000 (UTC) From: David Marchand To: dev@dpdk.org Cc: kkanas@marvell.com, stable@dpdk.org Date: Tue, 12 Nov 2019 21:31:02 +0100 Message-Id: <1573590662-31370-1-git-send-email-david.marchand@redhat.com> In-Reply-To: <20190902075251.2917-1-kkanas@marvell.com> References: <20190902075251.2917-1-kkanas@marvell.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: HPt7vpGoOoC-SH8f58BgkQ-1 X-Mimecast-Spam-Score: 0 Subject: [dpdk-dev] [PATCH v4] test: optimise fd closing in forked test process X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" From: Krzysztof Kanas Caught while investigating timeouts on a ARM64 server. Stracing a test process running the eal_flags_autotest, we can see that the fork helper is checking all possible file descriptors from getdtablesize() to 2, and close the existing ones. We can do better by inspecting this forked process /proc/self/fd directory. Besides, checking file descriptors via /proc/self/fd only makes sense for Linux. This code was a noop on FreeBSD. Fixes: af75078fece3 ("first public release") Cc: stable@dpdk.org Signed-off-by: Krzysztof Kanas Signed-off-by: David Marchand Acked-by: Kevin Traynor Tested-by: Krzysztof Kanas --- Changelog since v3: - rewrote commit log message, - restricted the fix to Linux only, --- app/test/process.h | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/app/test/process.h b/app/test/process.h index 128ce41..191d279 100644 --- a/app/test/process.h +++ b/app/test/process.h @@ -11,6 +11,7 @@ #include /* NULL */ #include /* strerror */ #include /* readlink */ +#include #include #include /* strlcpy */ @@ -40,7 +41,7 @@ process_dup(const char *const argv[], int numargs, const char *env_value) { int num; char *argv_cpy[numargs + 1]; - int i, fd, status; + int i, status; char path[32]; #ifdef RTE_LIBRTE_PDUMP pthread_t thread; @@ -56,13 +57,50 @@ process_dup(const char *const argv[], int numargs, const char *env_value) argv_cpy[i] = NULL; num = numargs; - /* close all open file descriptors, check /proc/self/fd to only - * call close on open fds. Exclude fds 0, 1 and 2*/ - for (fd = getdtablesize(); fd > 2; fd-- ) { - snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd); - if (access(path, F_OK) == 0) +#ifdef RTE_EXEC_ENV_LINUX + { + const char *procdir = "/proc/" self "/fd/"; + struct dirent *dirent; + char *endptr; + int fd, fdir; + DIR *dir; + + /* close all open file descriptors, check /proc/self/fd + * to only call close on open fds. Exclude fds 0, 1 and + * 2 + */ + dir = opendir(procdir); + if (dir == NULL) { + rte_panic("Error opening %s: %s\n", procdir, + strerror(errno)); + } + + fdir = dirfd(dir); + if (fdir < 0) { + status = errno; + closedir(dir); + rte_panic("Error %d obtaining fd for dir %s: %s\n", + fdir, procdir, + strerror(status)); + } + + while ((dirent = readdir(dir)) != NULL) { + errno = 0; + fd = strtol(dirent->d_name, &endptr, 10); + if (errno != 0 || endptr[0] != '\0') { + printf("Error converting name fd %d %s:\n", + fd, dirent->d_name); + continue; + } + + if (fd == fdir || fd <= 2) + continue; + close(fd); + } + closedir(dir); } +#endif printf("Running binary with argv[]:"); for (i = 0; i < num; i++) printf("'%s' ", argv_cpy[i]);