From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 31EE745C76; Mon, 4 Nov 2024 12:11:53 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 00F1440E3B; Mon, 4 Nov 2024 12:10:55 +0100 (CET) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 1D79A40E26 for ; Mon, 4 Nov 2024 12:10:42 +0100 (CET) Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Xhpc72jYLz10PYt; Mon, 4 Nov 2024 19:08:23 +0800 (CST) Received: from dggpeml500011.china.huawei.com (unknown [7.185.36.84]) by mail.maildlp.com (Postfix) with ESMTPS id 0F1E81402C8; Mon, 4 Nov 2024 19:10:41 +0800 (CST) Received: from localhost.huawei.com (10.50.165.33) by dggpeml500011.china.huawei.com (7.185.36.84) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Mon, 4 Nov 2024 19:10:40 +0800 From: Dengdui Huang To: CC: , , , , , , , , , , Subject: [PATCH v5 16/52] bus/vmbus: replace strerror with reentrant version Date: Mon, 4 Nov 2024 19:10:01 +0800 Message-ID: <20241104111037.3632161-17-huangdengdui@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20241104111037.3632161-1-huangdengdui@huawei.com> References: <20231114082539.1858594-1-huangdengdui@huawei.com> <20241104111037.3632161-1-huangdengdui@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.50.165.33] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500011.china.huawei.com (7.185.36.84) X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org The function strerror() is insecure in a multi-thread environment. This patch uses strerror_r() to replace it. Signed-off-by: Dengdui Huang Acked-by: Chengwen Feng Acked-by: Morten Brørup Acked-by: Huisong Li --- drivers/bus/vmbus/linux/vmbus_bus.c | 16 ++++++-- drivers/bus/vmbus/linux/vmbus_uio.c | 55 +++++++++++++++++++--------- drivers/bus/vmbus/vmbus_common.c | 13 +++++-- drivers/bus/vmbus/vmbus_common_uio.c | 7 +++- 4 files changed, 65 insertions(+), 26 deletions(-) diff --git a/drivers/bus/vmbus/linux/vmbus_bus.c b/drivers/bus/vmbus/linux/vmbus_bus.c index 01d8111b85..feb80518bf 100644 --- a/drivers/bus/vmbus/linux/vmbus_bus.c +++ b/drivers/bus/vmbus/linux/vmbus_bus.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "eal_filesystem.h" #include "private.h" @@ -43,14 +44,17 @@ extern struct rte_vmbus_bus rte_vmbus_bus; static int parse_sysfs_uuid(const char *filename, rte_uuid_t uu) { + char errmsg[RTE_STRERR_BUFSIZE]; char buf[BUFSIZ]; char *cp, *in = buf; FILE *f; f = fopen(filename, "r"); if (f == NULL) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VMBUS_LOG(ERR, "cannot open sysfs value %s: %s", - filename, strerror(errno)); + filename, errmsg); return -1; } @@ -86,13 +90,16 @@ parse_sysfs_uuid(const char *filename, rte_uuid_t uu) static int get_sysfs_string(const char *filename, char *buf, size_t buflen) { + char errmsg[RTE_STRERR_BUFSIZE]; char *cp; FILE *f; f = fopen(filename, "r"); if (f == NULL) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VMBUS_LOG(ERR, "cannot open sysfs value %s:%s", - filename, strerror(errno)); + filename, errmsg); return -1; } @@ -341,6 +348,7 @@ vmbus_scan_one(const char *name) int rte_vmbus_scan(void) { + char errmsg[RTE_STRERR_BUFSIZE]; struct dirent *e; DIR *dir; @@ -349,8 +357,10 @@ rte_vmbus_scan(void) if (errno == ENOENT) return 0; + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VMBUS_LOG(ERR, "opendir %s failed: %s", - SYSFS_VMBUS_DEVICES, strerror(errno)); + SYSFS_VMBUS_DEVICES, errmsg); return -1; } diff --git a/drivers/bus/vmbus/linux/vmbus_uio.c b/drivers/bus/vmbus/linux/vmbus_uio.c index 26edef342d..3e7bc618e4 100644 --- a/drivers/bus/vmbus/linux/vmbus_uio.c +++ b/drivers/bus/vmbus/linux/vmbus_uio.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "private.h" @@ -29,17 +30,21 @@ static void *vmbus_map_addr; /* Control interrupts */ void vmbus_uio_irq_control(struct rte_vmbus_device *dev, int32_t onoff) { + char errmsg[RTE_STRERR_BUFSIZE]; if ((rte_intr_fd_get(dev->intr_handle) < 0) || write(rte_intr_fd_get(dev->intr_handle), &onoff, sizeof(onoff)) < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VMBUS_LOG(ERR, "cannot write to %d:%s", rte_intr_fd_get(dev->intr_handle), - strerror(errno)); + errmsg); } } int vmbus_uio_irq_read(struct rte_vmbus_device *dev) { + char errmsg[RTE_STRERR_BUFSIZE]; int32_t count; int cc; @@ -50,8 +55,9 @@ int vmbus_uio_irq_read(struct rte_vmbus_device *dev) sizeof(count)); if (cc < (int)sizeof(count)) { if (cc < 0) { - VMBUS_LOG(ERR, "IRQ read failed %s", - strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + VMBUS_LOG(ERR, "IRQ read failed %s", errmsg); return -errno; } VMBUS_LOG(ERR, "can't read IRQ count"); @@ -83,6 +89,7 @@ int vmbus_uio_alloc_resource(struct rte_vmbus_device *dev, struct mapped_vmbus_resource **uio_res) { + char errmsg[RTE_STRERR_BUFSIZE]; char devname[PATH_MAX]; /* contains the /dev/uioX */ int fd; @@ -90,8 +97,9 @@ vmbus_uio_alloc_resource(struct rte_vmbus_device *dev, snprintf(devname, sizeof(devname), "/dev/uio%u", dev->uio_num); fd = open(devname, O_RDWR); if (fd < 0) { - VMBUS_LOG(ERR, "Cannot open %s: %s", - devname, strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + VMBUS_LOG(ERR, "Cannot open %s: %s", devname, errmsg); goto error; } @@ -150,6 +158,7 @@ vmbus_uio_map_resource_by_index(struct rte_vmbus_device *dev, int idx, { size_t size = dev->resource[idx].len; struct vmbus_map *maps = uio_res->maps; + char errmsg[RTE_STRERR_BUFSIZE]; void *mapaddr; off_t offset; int fd; @@ -157,8 +166,9 @@ vmbus_uio_map_resource_by_index(struct rte_vmbus_device *dev, int idx, /* devname for mmap */ fd = open(uio_res->path, O_RDWR); if (fd < 0) { - VMBUS_LOG(ERR, "Cannot open %s: %s", - uio_res->path, strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + VMBUS_LOG(ERR, "Cannot open %s: %s", uio_res->path, errmsg); return -1; } @@ -211,6 +221,7 @@ static int vmbus_uio_map_subchan(const struct rte_vmbus_device *dev, const struct vmbus_channel *chan, void **ring_buf, uint32_t *ring_size) { + char errmsg[RTE_STRERR_BUFSIZE]; char ring_path[PATH_MAX]; size_t file_size; struct stat sb; @@ -255,14 +266,16 @@ static int vmbus_uio_map_subchan(const struct rte_vmbus_device *dev, fd = open(ring_path, O_RDWR); if (fd < 0) { - VMBUS_LOG(ERR, "Cannot open %s: %s", - ring_path, strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + VMBUS_LOG(ERR, "Cannot open %s: %s", ring_path, errmsg); return -errno; } if (fstat(fd, &sb) < 0) { - VMBUS_LOG(ERR, "Cannot state %s: %s", - ring_path, strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + VMBUS_LOG(ERR, "Cannot state %s: %s", ring_path, errmsg); close(fd); return -errno; } @@ -334,6 +347,7 @@ int vmbus_uio_map_rings(struct vmbus_channel *chan) static int vmbus_uio_sysfs_read(const char *dir, const char *name, unsigned long *val, unsigned long max_range) { + char errmsg[RTE_STRERR_BUFSIZE]; char path[PATH_MAX]; FILE *f; int ret; @@ -341,8 +355,9 @@ static int vmbus_uio_sysfs_read(const char *dir, const char *name, snprintf(path, sizeof(path), "%s/%s", dir, name); f = fopen(path, "r"); if (!f) { - VMBUS_LOG(ERR, "can't open %s:%s", - path, strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + VMBUS_LOG(ERR, "can't open %s:%s", path, errmsg); return -errno; } @@ -393,6 +408,7 @@ int vmbus_uio_get_subchan(struct vmbus_channel *primary, { const struct rte_vmbus_device *dev = primary->device; char chan_path[PATH_MAX], subchan_path[PATH_MAX]; + char errmsg[RTE_STRERR_BUFSIZE]; struct dirent *ent; DIR *chan_dir; int err; @@ -403,8 +419,9 @@ int vmbus_uio_get_subchan(struct vmbus_channel *primary, chan_dir = opendir(chan_path); if (!chan_dir) { - VMBUS_LOG(ERR, "cannot open %s: %s", - chan_path, strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + VMBUS_LOG(ERR, "cannot open %s: %s", chan_path, errmsg); return -errno; } @@ -440,8 +457,10 @@ int vmbus_uio_get_subchan(struct vmbus_channel *primary, err = vmbus_uio_sysfs_read(subchan_path, "subchannel_id", &subid, UINT16_MAX); if (err) { + if (strerror_r(-err, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -err); VMBUS_LOG(NOTICE, "no subchannel_id in %s:%s", - subchan_path, strerror(-err)); + subchan_path, errmsg); goto fail; } @@ -451,8 +470,10 @@ int vmbus_uio_get_subchan(struct vmbus_channel *primary, err = vmbus_uio_sysfs_read(subchan_path, "monitor_id", &monid, UINT8_MAX); if (err) { + if (strerror_r(-err, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -err); VMBUS_LOG(NOTICE, "no monitor_id in %s:%s", - subchan_path, strerror(-err)); + subchan_path, errmsg); goto fail; } diff --git a/drivers/bus/vmbus/vmbus_common.c b/drivers/bus/vmbus/vmbus_common.c index 8a965d10d9..017ead03e5 100644 --- a/drivers/bus/vmbus/vmbus_common.c +++ b/drivers/bus/vmbus/vmbus_common.c @@ -29,16 +29,18 @@ void * vmbus_map_resource(void *requested_addr, int fd, off_t offset, size_t size, int flags) { + char errmsg[RTE_STRERR_BUFSIZE]; void *mapaddr; /* Map the memory resource of device */ mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE, MAP_SHARED | flags, fd, offset); if (mapaddr == MAP_FAILED) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VMBUS_LOG(ERR, "mmap(%d, %p, %zu, %ld) failed: %s", - fd, requested_addr, size, (long)offset, - strerror(errno)); + fd, requested_addr, size, (long)offset, errmsg); } else { VMBUS_LOG(DEBUG, " VMBUS memory mapped at %p", mapaddr); @@ -50,14 +52,17 @@ vmbus_map_resource(void *requested_addr, int fd, off_t offset, size_t size, void vmbus_unmap_resource(void *requested_addr, size_t size) { + char errmsg[RTE_STRERR_BUFSIZE]; + if (requested_addr == NULL) return; /* Unmap the VMBUS memory resource of device */ if (munmap(requested_addr, size)) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VMBUS_LOG(ERR, "munmap(%p, 0x%lx) failed: %s", - requested_addr, (unsigned long)size, - strerror(errno)); + requested_addr, (unsigned long)size, errmsg); } else { VMBUS_LOG(DEBUG, " VMBUS memory unmapped at %p", requested_addr); diff --git a/drivers/bus/vmbus/vmbus_common_uio.c b/drivers/bus/vmbus/vmbus_common_uio.c index 4d4613513c..d5f187c459 100644 --- a/drivers/bus/vmbus/vmbus_common_uio.c +++ b/drivers/bus/vmbus/vmbus_common_uio.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "private.h" @@ -43,6 +44,7 @@ static int vmbus_uio_map_secondary(struct rte_vmbus_device *dev) { struct mapped_vmbus_resource *uio_res; + char errmsg[RTE_STRERR_BUFSIZE]; struct vmbus_channel *chan; int fd, i; @@ -55,8 +57,9 @@ vmbus_uio_map_secondary(struct rte_vmbus_device *dev) /* open /dev/uioX */ fd = open(uio_res->path, O_RDWR); if (fd < 0) { - VMBUS_LOG(ERR, "Cannot open %s: %s", - uio_res->path, strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + VMBUS_LOG(ERR, "Cannot open %s: %s", uio_res->path, errmsg); return -1; } -- 2.33.0