Message ID | 20240307135616.3953774-1-xin.wang2@amd.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | [v3] tools/9pfsd: Fix build error caused by strerror_r | expand |
On 07.03.24 14:56, Henry Wang wrote: > 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 > > Using strerror_r() without special casing different build environments > is impossible due to the different return types (int vs char *) > depending on the environment. As p9_error() is not on a performance > critical path, using strerror() with a mutex ought to be fine. So, > fix the build by using strerror() to replace strerror_r(). 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. > > Fixes: f4900d6d69b5 ("9pfsd: allow building with old glibc") > Signed-off-by: Henry Wang <xin.wang2@amd.com> > Reviewed-by: Jan Beulich <jbeulich@suse.com> Reviewed-by: Juergen Gross <jgross@suse.com> Juergen
On Thu, Mar 07, 2024 at 09:56:16PM +0800, Henry Wang wrote: > 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 > > Using strerror_r() without special casing different build environments > is impossible due to the different return types (int vs char *) > depending on the environment. As p9_error() is not on a performance > critical path, using strerror() with a mutex ought to be fine. So, > fix the build by using strerror() to replace strerror_r(). 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. > > Fixes: f4900d6d69b5 ("9pfsd: allow building with old glibc") > Signed-off-by: Henry Wang <xin.wang2@amd.com> > Reviewed-by: Jan Beulich <jbeulich@suse.com> Acked-by: Anthony PERARD <anthony.perard@citrix.com>
On 07.03.24 14:56, Henry Wang wrote: > 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 > > Using strerror_r() without special casing different build environments > is impossible due to the different return types (int vs char *) > depending on the environment. As p9_error() is not on a performance > critical path, using strerror() with a mutex ought to be fine. So, > fix the build by using strerror() to replace strerror_r(). 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. > > Fixes: f4900d6d69b5 ("9pfsd: allow building with old glibc") > Signed-off-by: Henry Wang <xin.wang2@amd.com> > Reviewed-by: Jan Beulich <jbeulich@suse.com> Reviewed-by: Juergen Gross <jgross@suse.com> Juergen
diff --git a/tools/9pfsd/io.c b/tools/9pfsd/io.c index adb887c7d9..df1be3df7d 100644 --- a/tools/9pfsd/io.c +++ b/tools/9pfsd/io.c @@ -677,11 +677,28 @@ static bool name_ok(const char *str) return true; } +/* Including the '\0' */ +#define MAX_ERRSTR_LEN 80 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 *str; + size_t len = 0; + + /* + * While strerror_r() exists, it comes in a POSIX and a GNU flavor. + * Let's try to avoid trying to be clever with determining which + * one it is that the underlying C library offers, when really we + * don't expect this function to be called very often. + */ + pthread_mutex_lock(&mutex); + str = strerror(err); + len = min(strlen(str), (size_t)(MAX_ERRSTR_LEN - 1)); + memcpy(ring->buffer, str, len); + ((char *)ring->buffer)[len] = '\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",