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 15FC4A00E6 for ; Mon, 2 Sep 2019 11:49:52 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 3DBF31C1C7; Mon, 2 Sep 2019 11:49:51 +0200 (CEST) Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by dpdk.org (Postfix) with ESMTP id 833DC1BFBE for ; Mon, 2 Sep 2019 11:49:49 +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 x829gZD9012316 for ; Mon, 2 Sep 2019 02:49:48 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=pfpt0818; bh=aYbFES9wc5qPlHVM+lhSMDWCT2Ava9nqM5PYlq437yE=; b=ndZ/jxIvQOqGo4HU5ZMaj80kbQ//SgPWl3zMgxXFQj9M17faMw7YrwrK4WIYsIpr0DQa ONx+vwDGkHef9IhHXC3w2eQHdU4N1xvtuPbgpYZ/liPkGVREEXh5Nwgr5ms2l+rBQozI /Qjzgv5wHPwjvBRay4sVK3bGEf43DCKZNsPCOo5vnWW2SliVnQbQTnSLmeANuCTEIV8D NGnHGgrRHYHZ5MzPaVj0GcbisR4Tn6VK9UoWQ0MeA3/XVI6InyFuB0u8Q8aytTcSELil kKoaj+dLmzVUjmGV5d/hYq0G0yQcNJDAUUKGZ+0UFq62mYLfA3y5hZuaOeNZGudPB8Lt aw== Received: from sc-exch03.marvell.com ([199.233.58.183]) by mx0b-0016f401.pphosted.com with ESMTP id 2uqrdm5kxk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 02 Sep 2019 02:49:48 -0700 Received: from SC-EXCH03.marvell.com (10.93.176.83) by SC-EXCH03.marvell.com (10.93.176.83) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Mon, 2 Sep 2019 02:49:46 -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 02:49:46 -0700 Received: from kk-box-0.marvell.com (unknown [10.95.130.12]) by maili.marvell.com (Postfix) with ESMTP id 064023F7041; Mon, 2 Sep 2019 02:49:44 -0700 (PDT) From: To: CC: Krzysztof Kanas Date: Mon, 2 Sep 2019 11:49:39 +0200 Message-ID: <20190902094939.20482-1-kkanas@marvell.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190902075251.2917-1-kkanas@marvell.com> References: <20190902075251.2917-1-kkanas@marvell.com> 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_03:2019-08-29,2019-09-02 signatures=0 Subject: [dpdk-dev] [PATCH v2] 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 --- v2: * remove unnecessary commit msg information 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