[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2] tools/9pfsd: Fix build error caused by strerror_r()


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Henry Wang <xin.wang2@xxxxxxx>
  • Date: Thu, 7 Mar 2024 18:38:47 +0800
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=0lDKtyh31u8dwYxymmmUfs05dLNVR57uq36d4Emq/4c=; b=UKimA3iljT+dE17uTdFw0/IJglP1Q0h4nzcl03SoneClpp3zdgkznIJBj09ViCRVLwyOWJlyyy2Xj1Jal9NEAr+q5C36go5RNvnG3/0B1QWkH/64Qf3O3ZwzGWPxiz9IFg8wF5KfKizD8KYUDiV9QZUq7jNZJzTZizDtADJhXKS3ESqy3gxxilwpWU9y4cvsze8D8UdmT6QUf3uLEJaHtIHmr/dzZz8aP1LBxwW235BROMOKrVfQcLUdwBLzaahmGjSRxaJ7J32QEifDvApsRxkBHWDvrupfib5BbfV4LZghofIBqikhHCe4pBk1UX0HCCB6B4G8P5cCCNjrius0Fg==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bBS64xvIJFiZ/RmlsJu2t/bhmHzhPYvs9uksPESOdxDIUe5SW8NgACsrscNkgezUqddbKfQKW0CRld3QQdQnm2k+HxTPRgcjZL3H3y7MzxgCPfB9A8XaCFwK/VapU62dVpLhH//uhDfzgfqOg0wzS/fKh4vyccun1IKLiPbC2K4CyrAcEoVv5KkRYO1aus3z/5IFMUkBhlRH4lqTpp/t/BfOjoEP24UzJ8SVsc5oo148mGSY7IkUefGSjQgLcZ4kxHeTT67NNnPBnVbDi0P9ayUB/JWMRGQGs0UeEJghlR7TegU4XRqvgxLK3Z+dzekQejkvqzSipH9sqErJD0y4Rw==
  • Cc: Henry Wang <xin.wang2@xxxxxxx>, Wei Liu <wl@xxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Juergen Gross <jgross@xxxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>
  • Delivery-date: Thu, 07 Mar 2024 10:39:06 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

Below error can be seen when doing Yocto build of the toolstack:

| io.c: In function 'p9_error':
| io.c:684:5: error: ignoring return value of 'strerror_r' declared
  with attribute 'warn_unused_result' [-Werror=unused-result]
|   684 |     strerror_r(err, ring->buffer, ring->ring_size);
|       |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| cc1: all warnings being treated as errors

Fix the build by using strerror() to replace strerror_r(). Since
strerror() is thread-unsafe, use a separate local mutex to protect
the action. The steps would then become: Acquire the mutex first,
invoke strerror(), copy the string from strerror() to the designated
buffer and then drop the mutex.

Signed-off-by: Henry Wang <xin.wang2@xxxxxxx>
---
 tools/9pfsd/io.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/tools/9pfsd/io.c b/tools/9pfsd/io.c
index adb887c7d9..2b80c9528d 100644
--- a/tools/9pfsd/io.c
+++ b/tools/9pfsd/io.c
@@ -680,8 +680,18 @@ static bool name_ok(const char *str)
 static void p9_error(struct ring *ring, uint16_t tag, uint32_t err)
 {
     unsigned int erroff;
+    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+    char *strerror_str;
+    RING_IDX strerror_len = 0, copy_len = 0;
+
+    pthread_mutex_lock(&mutex);
+    strerror_str = strerror(err);
+    strerror_len = strlen(strerror_str) + 1;
+    copy_len = min(strerror_len, ring->ring_size);
+    memcpy(ring->buffer, strerror_str, copy_len);
+    ((char *)(ring->buffer))[copy_len - 1] = '\0';
+    pthread_mutex_unlock(&mutex);
 
-    strerror_r(err, ring->buffer, ring->ring_size);
     erroff = add_string(ring, ring->buffer, strlen(ring->buffer));
     fill_buffer(ring, P9_CMD_ERROR, tag, "SU",
                 erroff != ~0 ? ring->str + erroff : "cannot allocate memory",
-- 
2.34.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.