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 EF6A645C76; Mon, 4 Nov 2024 12:11:18 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2F40D40E38; Mon, 4 Nov 2024 12:10:49 +0100 (CET) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 4F9BB40E15 for ; Mon, 4 Nov 2024 12:10:41 +0100 (CET) Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Xhpc62n00z10PY2; Mon, 4 Nov 2024 19:08:22 +0800 (CST) Received: from dggpeml500011.china.huawei.com (unknown [7.185.36.84]) by mail.maildlp.com (Postfix) with ESMTPS id 0D9621402C8; Mon, 4 Nov 2024 19:10:40 +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:39 +0800 From: Dengdui Huang To: CC: , , , , , , , , , , Subject: [PATCH v5 10/52] vhost: replace strerror with reentrant version Date: Mon, 4 Nov 2024 19:09:55 +0800 Message-ID: <20241104111037.3632161-11-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 --- lib/vhost/fd_man.c | 14 ++++-- lib/vhost/socket.c | 34 ++++++++++--- lib/vhost/vduse.c | 105 +++++++++++++++++++++++++++++++---------- lib/vhost/vhost_user.c | 25 +++++++--- 4 files changed, 138 insertions(+), 40 deletions(-) diff --git a/lib/vhost/fd_man.c b/lib/vhost/fd_man.c index 9bc7e50b93..db913f8c1c 100644 --- a/lib/vhost/fd_man.c +++ b/lib/vhost/fd_man.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "fd_man.h" @@ -212,6 +213,7 @@ fdset_find_entry_locked(struct fdset *pfdset, int fd) int fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat) { + char errmsg[RTE_STRERR_BUFSIZE]; struct epoll_event ev; struct fdentry *pfdentry; int ret = 0; @@ -237,8 +239,10 @@ fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat) ret = epoll_ctl(pfdset->epfd, EPOLL_CTL_ADD, fd, &ev); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_FDMAN_LOG(ERR, "could not add %d fd to %d epfd: %s", - fd, pfdset->epfd, strerror(errno)); + fd, pfdset->epfd, errmsg); goto out_remove; } @@ -256,13 +260,17 @@ fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat) static void fdset_del_locked(struct fdset *pfdset, struct fdentry *pfdentry) { + char errmsg[RTE_STRERR_BUFSIZE]; + if (epoll_ctl(pfdset->epfd, EPOLL_CTL_DEL, pfdentry->fd, NULL) == -1) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); if (errno == EBADF) /* File might have already been closed. */ VHOST_FDMAN_LOG(DEBUG, "could not remove %d fd from %d epfd: %s", - pfdentry->fd, pfdset->epfd, strerror(errno)); + pfdentry->fd, pfdset->epfd, errmsg); else VHOST_FDMAN_LOG(ERR, "could not remove %d fd from %d epfd: %s", - pfdentry->fd, pfdset->epfd, strerror(errno)); + pfdentry->fd, pfdset->epfd, errmsg); } fdset_remove_entry(pfdset, pfdentry); diff --git a/lib/vhost/socket.c b/lib/vhost/socket.c index d29d15494c..96a9b84194 100644 --- a/lib/vhost/socket.c +++ b/lib/vhost/socket.c @@ -16,6 +16,7 @@ #include #include +#include #include "fd_man.h" #include "vduse.h" @@ -105,6 +106,7 @@ read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int m struct iovec iov; struct msghdr msgh; char control[CMSG_SPACE(max_fds * sizeof(int))]; + char errmsg[RTE_STRERR_BUFSIZE]; struct cmsghdr *cmsg; int got_fds = 0; int ret; @@ -122,9 +124,12 @@ read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int m ret = recvmsg(sockfd, &msgh, 0); if (ret <= 0) { - if (ret) + if (ret) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(ifname, ERR, "recvmsg failed on fd %d (%s)", - sockfd, strerror(errno)); + sockfd, errmsg); + } return ret; } @@ -161,6 +166,7 @@ send_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int f struct msghdr msgh; size_t fdsize = fd_num * sizeof(int); char control[CMSG_SPACE(fdsize)]; + char errmsg[RTE_STRERR_BUFSIZE]; struct cmsghdr *cmsg; int ret; @@ -194,8 +200,10 @@ send_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int f } while (ret < 0 && errno == EINTR); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(ifname, ERR, "sendmsg error on fd %d (%s)", - sockfd, strerror(errno)); + sockfd, errmsg); return ret; } @@ -341,6 +349,7 @@ static int create_unix_socket(struct vhost_user_socket *vsocket) { int fd; + char errmsg[RTE_STRERR_BUFSIZE]; struct sockaddr_un *un = &vsocket->un; fd = socket(AF_UNIX, SOCK_STREAM, 0); @@ -350,9 +359,11 @@ create_unix_socket(struct vhost_user_socket *vsocket) vsocket->is_server ? "server" : "client", fd); if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(vsocket->path, ERR, "vhost-user: can't set nonblocking mode for socket, fd: %d (%s)", - fd, strerror(errno)); + fd, errmsg); close(fd); return -1; } @@ -371,6 +382,7 @@ vhost_user_start_server(struct vhost_user_socket *vsocket) { int ret; int fd = vsocket->socket_fd; + char errmsg[RTE_STRERR_BUFSIZE]; const char *path = vsocket->path; /* @@ -385,8 +397,10 @@ vhost_user_start_server(struct vhost_user_socket *vsocket) */ ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un)); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(path, ERR, "failed to bind: %s; remove it and try again", - strerror(errno)); + errmsg); goto err; } VHOST_CONFIG_LOG(path, INFO, "binding succeeded"); @@ -430,6 +444,7 @@ static rte_thread_t reconn_tid; static int vhost_user_connect_nonblock(char *path, int fd, struct sockaddr *un, size_t sz) { + char errmsg[RTE_STRERR_BUFSIZE]; int ret, flags; ret = connect(fd, un, sz); @@ -438,8 +453,10 @@ vhost_user_connect_nonblock(char *path, int fd, struct sockaddr *un, size_t sz) flags = fcntl(fd, F_GETFL, 0); if (flags < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(path, ERR, "can't get flags for connfd %d (%s)", - fd, strerror(errno)); + fd, errmsg); return -2; } if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) { @@ -523,6 +540,7 @@ vhost_user_start_client(struct vhost_user_socket *vsocket) { int ret; int fd = vsocket->socket_fd; + char errmsg[RTE_STRERR_BUFSIZE]; const char *path = vsocket->path; struct vhost_user_reconnect *reconn; @@ -534,7 +552,9 @@ vhost_user_start_client(struct vhost_user_socket *vsocket) return 0; } - VHOST_CONFIG_LOG(path, WARNING, "failed to connect: %s", strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + VHOST_CONFIG_LOG(path, WARNING, "failed to connect: %s", errmsg); if (ret == -2 || !vsocket->reconnect) { close(fd); diff --git a/lib/vhost/vduse.c b/lib/vhost/vduse.c index eaf3146b95..58bf065e66 100644 --- a/lib/vhost/vduse.c +++ b/lib/vhost/vduse.c @@ -17,6 +17,7 @@ #include #include +#include #include "fd_man.h" #include "iotlb.h" @@ -119,14 +120,17 @@ static struct vhost_backend_ops vduse_backend_ops = { static void vduse_control_queue_event(int fd, void *arg, int *remove __rte_unused) { + char errmsg[RTE_STRERR_BUFSIZE]; struct virtio_net *dev = arg; uint64_t buf; int ret; ret = read(fd, &buf, sizeof(buf)); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to read control queue event: %s", - strerror(errno)); + errmsg); return; } @@ -142,13 +146,16 @@ vduse_vring_setup(struct virtio_net *dev, unsigned int index, bool reconnect) struct vhost_vring_addr *ra = &vq->ring_addrs; struct vduse_vq_info vq_info; struct vduse_vq_eventfd vq_efd; + char errmsg[RTE_STRERR_BUFSIZE]; int ret; vq_info.index = index; ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_GET_INFO, &vq_info); if (ret) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to get VQ %u info: %s", - index, strerror(errno)); + index, errmsg); return; } @@ -178,8 +185,10 @@ vduse_vring_setup(struct virtio_net *dev, unsigned int index, bool reconnect) VHOST_CONFIG_LOG(dev->ifname, INFO, "\tready: %u", vq_info.ready); vq->kickfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (vq->kickfd < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to init kickfd for VQ %u: %s", - index, strerror(errno)); + index, errmsg); vq->kickfd = VIRTIO_INVALID_EVENTFD; return; } @@ -210,8 +219,10 @@ vduse_vring_setup(struct virtio_net *dev, unsigned int index, bool reconnect) ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP_KICKFD, &vq_efd); if (ret) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to setup kickfd for VQ %u: %s", - index, strerror(errno)); + index, errmsg); close(vq->kickfd); vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD; return; @@ -220,9 +231,11 @@ vduse_vring_setup(struct virtio_net *dev, unsigned int index, bool reconnect) if (vq == dev->cvq) { ret = fdset_add(vduse.fdset, vq->kickfd, vduse_control_queue_event, NULL, dev); if (ret) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to setup kickfd handler for VQ %u: %s", - index, strerror(errno)); + index, errmsg); vq_efd.fd = VDUSE_EVENTFD_DEASSIGN; ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP_KICKFD, &vq_efd); close(vq->kickfd); @@ -237,6 +250,7 @@ static void vduse_vring_cleanup(struct virtio_net *dev, unsigned int index) { struct vhost_virtqueue *vq = dev->virtqueue[index]; + char errmsg[RTE_STRERR_BUFSIZE]; struct vduse_vq_eventfd vq_efd; int ret; @@ -247,9 +261,12 @@ vduse_vring_cleanup(struct virtio_net *dev, unsigned int index) vq_efd.fd = VDUSE_EVENTFD_DEASSIGN; ret = ioctl(dev->vduse_dev_fd, VDUSE_VQ_SETUP_KICKFD, &vq_efd); - if (ret) + if (ret) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to cleanup kickfd for VQ %u: %s", - index, strerror(errno)); + index, errmsg); + } close(vq->kickfd); vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD; @@ -274,6 +291,7 @@ vduse_vring_cleanup(struct virtio_net *dev, unsigned int index) static void vduse_device_start(struct virtio_net *dev, bool reconnect) { + char errmsg[RTE_STRERR_BUFSIZE]; unsigned int i, ret; VHOST_CONFIG_LOG(dev->ifname, INFO, "Starting device..."); @@ -287,8 +305,10 @@ vduse_device_start(struct virtio_net *dev, bool reconnect) ret = ioctl(dev->vduse_dev_fd, VDUSE_DEV_GET_FEATURES, &dev->features); if (ret) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to get features: %s", - strerror(errno)); + errmsg); return; } @@ -357,14 +377,17 @@ vduse_events_handler(int fd, void *arg, int *remove __rte_unused) struct vduse_dev_response resp; struct vhost_virtqueue *vq; uint8_t old_status = dev->status; + char errmsg[RTE_STRERR_BUFSIZE]; int ret; memset(&resp, 0, sizeof(resp)); ret = read(fd, &req, sizeof(req)); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to read request: %s", - strerror(errno)); + errmsg); return; } else if (ret < (int)sizeof(req)) { VHOST_CONFIG_LOG(dev->ifname, ERR, "Incomplete to read request %d", ret); @@ -406,8 +429,10 @@ vduse_events_handler(int fd, void *arg, int *remove __rte_unused) ret = write(dev->vduse_dev_fd, &resp, sizeof(resp)); if (ret != sizeof(resp)) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to write response %s", - strerror(errno)); + errmsg); return; } @@ -428,6 +453,7 @@ static bool vduse_reconnect_path_set; static int vduse_reconnect_path_init(void) { + char errmsg[RTE_STRERR_BUFSIZE]; const char *directory; int ret; @@ -457,8 +483,10 @@ vduse_reconnect_path_init(void) ret = mkdir(vduse_reconnect_dir, 0700); if (ret < 0 && errno != EEXIST) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG("vduse", ERR, "Error creating '%s': %s", - vduse_reconnect_dir, strerror(errno)); + vduse_reconnect_dir, errmsg); return -1; } @@ -473,6 +501,7 @@ vduse_reconnect_path_init(void) static int vduse_reconnect_log_map(struct virtio_net *dev, bool create) { + char errmsg[RTE_STRERR_BUFSIZE]; char reco_file[PATH_MAX]; int fd, ret; const char *name = dev->ifname + strlen("/dev/vduse/"); @@ -495,16 +524,20 @@ vduse_reconnect_log_map(struct virtio_net *dev, bool create) VHOST_CONFIG_LOG(dev->ifname, ERR, "Reconnect file %s exists but not the device", reco_file); } else { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to open reconnect file %s (%s)", - reco_file, strerror(errno)); + reco_file, errmsg); } return -1; } ret = ftruncate(fd, sizeof(*dev->reconnect_log)); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to truncate reconnect file %s (%s)", - reco_file, strerror(errno)); + reco_file, errmsg); goto out_close; } } else { @@ -512,9 +545,12 @@ vduse_reconnect_log_map(struct virtio_net *dev, bool create) if (fd < 0) { if (errno == ENOENT) VHOST_CONFIG_LOG(dev->ifname, ERR, "Missing reconnect file (%s)", reco_file); - else + else { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to open reconnect file %s (%s)", - reco_file, strerror(errno)); + reco_file, errmsg); + } return -1; } } @@ -522,8 +558,10 @@ vduse_reconnect_log_map(struct virtio_net *dev, bool create) dev->reconnect_log = mmap(NULL, sizeof(*dev->reconnect_log), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (dev->reconnect_log == MAP_FAILED) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to mmap reconnect file %s (%s)", - reco_file, strerror(errno)); + reco_file, errmsg); ret = -1; goto out_close; } @@ -576,6 +614,7 @@ vduse_reconnect_handler(int fd, void *arg, int *remove) static int vduse_reconnect_start_device(struct virtio_net *dev) { + char errmsg[RTE_STRERR_BUFSIZE]; int fd, ret; /* @@ -584,8 +623,10 @@ vduse_reconnect_start_device(struct virtio_net *dev) */ fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (fd < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to create reconnect efd: %s", - strerror(errno)); + errmsg); ret = -1; goto out_err; } @@ -624,6 +665,7 @@ vduse_device_create(const char *path, bool compliant_ol_flags) uint64_t features; const char *name = path + strlen("/dev/vduse/"); bool reconnect = false; + char errmsg[RTE_STRERR_BUFSIZE]; if (vduse.fdset == NULL) { vduse.fdset = fdset_init("vduse-evt"); @@ -635,14 +677,18 @@ vduse_device_create(const char *path, bool compliant_ol_flags) control_fd = open(VDUSE_CTRL_PATH, O_RDWR); if (control_fd < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(name, ERR, "Failed to open %s: %s", - VDUSE_CTRL_PATH, strerror(errno)); + VDUSE_CTRL_PATH, errmsg); return -1; } if (ioctl(control_fd, VDUSE_SET_API_VERSION, &ver)) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(name, ERR, "Failed to set API version: %" PRIu64 ": %s", - ver, strerror(errno)); + ver, errmsg); ret = -1; goto out_ctrl_close; } @@ -698,29 +744,37 @@ vduse_device_create(const char *path, bool compliant_ol_flags) free(dev_config); dev_config = NULL; if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(name, ERR, "Failed to create VDUSE device: %s", - strerror(errno)); + errmsg); goto out_ctrl_close; } dev_fd = open(path, O_RDWR); if (dev_fd < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(name, ERR, "Failed to open newly created device %s: %s", - path, strerror(errno)); + path, errmsg); ret = -1; goto out_ctrl_close; } } else { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(name, ERR, "Failed to open device %s: %s", - path, strerror(errno)); + path, errmsg); ret = -1; goto out_ctrl_close; } ret = fcntl(dev_fd, F_SETFL, O_NONBLOCK); if (ret < 0) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(name, ERR, "Failed to set chardev as non-blocking: %s", - strerror(errno)); + errmsg); goto out_dev_close; } @@ -820,6 +874,7 @@ int vduse_device_destroy(const char *path) { const char *name = path + strlen("/dev/vduse/"); + char errmsg[RTE_STRERR_BUFSIZE]; struct virtio_net *dev; int vid, ret; @@ -853,8 +908,10 @@ vduse_device_destroy(const char *path) ret = ioctl(dev->vduse_ctrl_fd, VDUSE_DESTROY_DEV, name); if (ret) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(name, ERR, "Failed to destroy VDUSE device: %s", - strerror(errno)); + errmsg); } else { /* * VDUSE device was no more attached to the vDPA bus, diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c index 6d92ad904e..6cde684a6e 100644 --- a/lib/vhost/vhost_user.c +++ b/lib/vhost/vhost_user.c @@ -829,10 +829,13 @@ mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64 void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz); uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz); size_t len = end - (uintptr_t)start; + char errmsg[RTE_STRERR_BUFSIZE]; if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, INFO, - "could not set coredump preference (%s).", strerror(errno)); + "could not set coredump preference (%s).", errmsg); } #endif } @@ -1192,6 +1195,7 @@ vhost_user_postcopy_region_register(struct virtio_net *dev, struct rte_vhost_mem_region *reg) { struct uffdio_register reg_struct; + char errmsg[RTE_STRERR_BUFSIZE]; /* * Let's register all the mmapped area to ensure @@ -1203,6 +1207,8 @@ vhost_user_postcopy_region_register(struct virtio_net *dev, if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER, ®_struct)) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to register ufd for region " "%" PRIx64 " - %" PRIx64 " (ufd = %d) %s", @@ -1210,7 +1216,7 @@ vhost_user_postcopy_region_register(struct virtio_net *dev, (uint64_t)reg_struct.range.start + (uint64_t)reg_struct.range.len - 1, dev->postcopy_ufd, - strerror(errno)); + errmsg); return -1; } @@ -1292,6 +1298,7 @@ vhost_user_mmap_region(struct virtio_net *dev, struct rte_vhost_mem_region *region, uint64_t mmap_offset) { + char errmsg[RTE_STRERR_BUFSIZE]; void *mmap_addr; uint64_t mmap_size; uint64_t alignment; @@ -1339,7 +1346,9 @@ vhost_user_mmap_region(struct virtio_net *dev, MAP_SHARED | populate, region->fd, 0); if (mmap_addr == MAP_FAILED) { - VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap failed (%s).", strerror(errno)); + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); + VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap failed (%s).", errmsg); return -1; } @@ -2770,21 +2779,25 @@ vhost_user_set_postcopy_advise(struct virtio_net **pdev, struct virtio_net *dev = *pdev; #ifdef RTE_LIBRTE_VHOST_POSTCOPY struct uffdio_api api_struct; + char errmsg[RTE_STRERR_BUFSIZE]; dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); if (dev->postcopy_ufd == -1) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, - "userfaultfd not available: %s", - strerror(errno)); + "userfaultfd not available: %s", errmsg); return RTE_VHOST_MSG_RESULT_ERR; } api_struct.api = UFFD_API; api_struct.features = 0; if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) { + if (strerror_r(errno, errmsg, sizeof(errmsg)) != 0) + snprintf(errmsg, sizeof(errmsg), "Unknown error %d", errno); VHOST_CONFIG_LOG(dev->ifname, ERR, "UFFDIO_API ioctl failure: %s", - strerror(errno)); + errmsg); close(dev->postcopy_ufd); dev->postcopy_ufd = -1; return RTE_VHOST_MSG_RESULT_ERR; -- 2.33.0