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 905CFA04B3; Fri, 8 Nov 2019 11:23:51 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 6FA621C18E; Fri, 8 Nov 2019 11:21:58 +0100 (CET) Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by dpdk.org (Postfix) with ESMTP id B486A1C036 for ; Fri, 8 Nov 2019 11:21:52 +0100 (CET) Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id xA8ALG65000689; Fri, 8 Nov 2019 02:21:51 -0800 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=/mgMmurjUoCM0T8alMNKUeuswL70Izc1VMe24W/aGdc=; b=pr7/V3oYFNNKXDZanberiAdCIUxVuL0VUDr0TX1yZzX2LMU1TnTUUZTAZRSTD/l5XYPp kW9V1xrJdt6q/1+8Th8ttk5AE3H5VhIIKLL0Mnkt3+ZwYTXgV+oPxCdnP15WK1WSXe0T UauDdXjs6QzCwShvKGVotEMNf+cWdozRsQIvDb5uAN43o3qznBwseVBv8zA6DBKyCPRg Y//E+4hwfbB46Rgk+ii5Zb1EAslEjeXvLmLlEf1KQBGAdUyPGiyfPK8m9P/hVZTa+NCR zw2slXJwsnZw9VIVqAd/E9jzcsdmqN4epKOMiCvZ7OsNF2n00J5tRoX1VfQgIFOJVQqU 2w== Received: from sc-exch02.marvell.com ([199.233.58.182]) by mx0a-0016f401.pphosted.com with ESMTP id 2w511615dw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Fri, 08 Nov 2019 02:21:51 -0800 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; Fri, 8 Nov 2019 02:21:50 -0800 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; Fri, 8 Nov 2019 02:21:50 -0800 Received: from kk-box-0.marvell.com (unknown [10.95.130.41]) by maili.marvell.com (Postfix) with ESMTP id 4B1723F7040; Fri, 8 Nov 2019 02:21:49 -0800 (PST) From: To: , , CC: Krzysztof Kanas Date: Fri, 8 Nov 2019 11:21:34 +0100 Message-ID: <20191108102135.7249-3-kkanas@marvell.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191108102135.7249-1-kkanas@marvell.com> References: <20191108102135.7249-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.95,18.0.572 definitions=2019-11-08_02:2019-11-07,2019-11-08 signatures=0 Subject: [dpdk-dev] [PATCH v3 2/3] test: move close files to separate function 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 As iterating over opened files is different on Linux and FreeBSD move to separate function. Signed-off-by: Krzysztof Kanas --- app/test/process.h | 89 ++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/app/test/process.h b/app/test/process.h index 298ba288087a..34afac5a1f39 100644 --- a/app/test/process.h +++ b/app/test/process.h @@ -30,6 +30,54 @@ extern void *send_pkts(void *empty); extern uint16_t flag_for_send_pkts; #endif + +/* close all open file descriptors, check /proc/self/fd to only + * call close on open fds. Exclude fds 0, 1 and 2 + */ +#ifdef RTE_EXEC_ENV_LINUX +static inline void +close_files(void) +{ + const char *procdir = "/proc/self/fd/"; + struct dirent *dirent; + int fd, fdir, status; + char *endptr; + DIR *dir; + + 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) { + if (!strcmp(".", dirent->d_name) || + !strcmp("..", dirent->d_name)) + continue; + + errno = 0; + fd = strtol(dirent->d_name, &endptr, 10); + if (errno != 0 || endptr[0] != '\0') { + printf("Error converint name fd %d %s:\n", + fd, dirent->d_name); + continue; + } + if (fd == fdir || fd <= 2) + continue; + + close(fd); + } + closedir(dir); +} +#endif /* * launches a second copy of the test process using the given argv parameters, * which should include argv[0] as the process name. To identify in the @@ -41,12 +89,8 @@ process_dup(const char *const argv[], int numargs, const char *env_value) { int num; char *argv_cpy[numargs + 1]; - int i, fd, fdir, status; - struct dirent *dirent; - const char *procdir = "/proc/self/fd/"; + int i, status; char path[32]; - char *endptr; - DIR *dir; #ifdef RTE_LIBRTE_PDUMP pthread_t thread; #endif @@ -61,40 +105,7 @@ 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*/ - 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) { - if (!strcmp(".", dirent->d_name) || - !strcmp("..", dirent->d_name)) - continue; - - errno = 0; - fd = strtol(dirent->d_name, &endptr, 10); - if (errno != 0 || endptr[0] != '\0') { - printf("Error converint name fd %d %s:\n", - fd, dirent->d_name); - continue; - } - if (fd == fdir || fd <= 2) - continue; - - close(fd); - } - closedir(dir); + close_files(); printf("Running binary with argv[]:"); for (i = 0; i < num; i++) -- 2.21.0