From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 0979FA00E6 for ; Mon, 2 Sep 2019 09:53:06 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id C2F691C2A0; Mon, 2 Sep 2019 09:53:05 +0200 (CEST) Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by dpdk.org (Postfix) with ESMTP id 0E4C61C28F for ; Mon, 2 Sep 2019 09:53:03 +0200 (CEST) Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id x827npn4015731 for ; Mon, 2 Sep 2019 00:53:02 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=pfpt0818; bh=DIvalJhiZ2VyFb/8EoPimNwO7UQalxIEUjTyGhqqm6w=; b=eOx9PL3lv3Db8GnTBWzoz2l3Lw6/pUoEFN/EG7ptPp/08dbmVt8xr6teSlRZTp1Jq7bF CegaKwY16Pl+vZ4ihaLwlR3jJEQEMZJDHBm0mj3/fZGRcznA9dWlFtDxyODn6uWyICT9 gxIc6EwTlUhIthIJuEhcY+yxkMk/+NvRjVwty5tP+TGU/R7cpMjHcgeylgyzEyLkXUzp cROz55KSof5aC+Bh0JdG1tC1Jzpro9bclcVsEKcXo+ICllqTLk9dUS1z2ypJcoVB85sk snBheOaTAatsOY8O9+MgZSZdoNTKGv4ov5Lc8htFxKFotk7N2JdyPlOUo8UprVZ8hIEX Pg== Received: from sc-exch02.marvell.com ([199.233.58.182]) by mx0b-0016f401.pphosted.com with ESMTP id 2uqrdm5ak7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 02 Sep 2019 00:53:02 -0700 Received: from SC-EXCH03.marvell.com (10.93.176.83) by SC-EXCH02.marvell.com (10.93.176.82) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Mon, 2 Sep 2019 00:52:59 -0700 Received: from maili.marvell.com (10.93.176.43) by SC-EXCH03.marvell.com (10.93.176.83) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Mon, 2 Sep 2019 00:52:59 -0700 Received: from kk-box-0.marvell.com (unknown [10.95.130.12]) by maili.marvell.com (Postfix) with ESMTP id 3B0583F7040; Mon, 2 Sep 2019 00:52:57 -0700 (PDT) From: To: CC: Krzysztof Kanas , Amit Gupta , Jerin Jacob Kollanukkaran Date: Mon, 2 Sep 2019 09:52:50 +0200 Message-ID: <20190902075251.2917-1-kkanas@marvell.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.70,1.0.8 definitions=2019-09-02_02:2019-08-29,2019-09-02 signatures=0 Subject: [dpdk-dev] [PATCH] test: fix process dup fd close 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 process_dup was intending to close it's own fd's but failed to do so Fixes: af75078fece3 ("first public release") Signed-off-by: Krzysztof Kanas Reviewed-on: https://sj1git1.cavium.com/14906 Reviewed-by: Amit Gupta Reviewed-by: Jerin Jacob Kollanukkaran Tested-by: Jerin Jacob Kollanukkaran --- app/test/process.h | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/app/test/process.h b/app/test/process.h index 128ce41219a1..2a6428f104e1 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,8 +41,12 @@ 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, fd, fdir, status; + struct dirent *dirent = NULL; + const char *procdir = "/proc/self/fd/"; char path[32]; + char *endptr; + DIR *dir = NULL; #ifdef RTE_LIBRTE_PDUMP pthread_t thread; #endif @@ -58,11 +63,36 @@ process_dup(const char *const argv[], int numargs, const char *env_value) /* 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) - close(fd); + 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 || dirent->d_name == endptr) { + printf("Error converint name fd %d %s:\n", + fd, dirent->d_name); + continue; + } + + if (fd == fdir || fd <= 2) + continue; + + close(fd); + } + printf("Running binary with argv[]:"); for (i = 0; i < num; i++) printf("'%s' ", argv_cpy[i]); -- 2.21.0