From 018538fbf28a47e2b76353a37d5b3c5fe9ae3f60 Mon Sep 17 00:00:00 2001 From: JuniorJPDJ Date: Wed, 7 Jul 2021 05:18:10 +0200 Subject: [PATCH 1/5] cast time_t to intmax_t when printing This allows to preserve compatibility with 32-bit and 64-bit architectures. Fixes compilation problems like: format '%lu' expects argument of type 'long unsigned int', but argument 5 has type 'time_t' {aka 'long long int'} --- main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 475279aa..976615d7 100644 --- a/main.c +++ b/main.c @@ -657,7 +657,7 @@ int tcmur_get_time(struct tcmu_device *dev, struct timespec *time) ret = clock_gettime(CLOCK_MONOTONIC_COARSE, time); if (!ret) { - tcmu_dev_dbg(dev, "Current time %lu secs.\n", time->tv_sec); + tcmu_dev_dbg(dev, "Current time %"PRIdMAX" secs.\n", (intmax_t)time->tv_sec); return 0; } @@ -700,9 +700,9 @@ static bool get_next_cmd_timeout(struct tcmu_device *dev, tmo->tv_sec = 0; } - tcmu_dev_dbg(dev, "Next cmd id %hu timeout in %lu secs. Current time %lu. Start time %lu\n", - tcmur_cmd->lib_cmd->cmd_id, tmo->tv_sec, - curr_time->tv_sec, tcmur_cmd->start_time.tv_sec); + tcmu_dev_dbg(dev, "Next cmd id %hu timeout in %"PRIdMAX" secs. Current time %"PRIdMAX". Start time %"PRIdMAX"\n", + tcmur_cmd->lib_cmd->cmd_id, (intmax_t)tmo->tv_sec, + (intmax_t)curr_time->tv_sec, (intmax_t)tcmur_cmd->start_time.tv_sec); break; } pthread_spin_unlock(&rdev->lock); From ef4fe44bccc3ee2f5b52e148a37bf3affb1ce819 Mon Sep 17 00:00:00 2001 From: JuniorJPDJ Date: Wed, 7 Jul 2021 05:20:10 +0200 Subject: [PATCH 2/5] replace assert_perror with assert This permits to preserve musl libc compatibility and fixes compile time problems like: error: implicit declaration of function 'assert_perror'; --- api.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api.c b/api.c index 490e2577..90fe8de9 100644 --- a/api.c +++ b/api.c @@ -74,7 +74,7 @@ uint64_t tcmu_cdb_get_lba(uint8_t *cdb) case 16: return be64toh(*((u_int64_t *)&cdb[2])); default: - assert_perror(EINVAL); + assert(0); return 0; /* not reached */ } } @@ -91,7 +91,7 @@ uint32_t tcmu_cdb_get_xfer_length(uint8_t *cdb) case 16: return be32toh(*((u_int32_t *)&cdb[10])); default: - assert_perror(EINVAL); + assert(0); return 0; /* not reached */ } } From 015b9f4078616daee5ab3b5efb2088cce764537c Mon Sep 17 00:00:00 2001 From: JuniorJPDJ Date: Wed, 7 Jul 2021 05:25:37 +0200 Subject: [PATCH 3/5] add missing pthread.h include This fixes musl libc compatibility and fixes compilation time error like: error: unknown type name 'pthread_t' --- libtcmu_common.h | 1 + 1 file changed, 1 insertion(+) diff --git a/libtcmu_common.h b/libtcmu_common.h index 350693b4..19721b04 100644 --- a/libtcmu_common.h +++ b/libtcmu_common.h @@ -14,6 +14,7 @@ #define __LIBTCMU_COMMON_H #include +#include #ifdef __cplusplus extern "C" { From 4567bc772cdd6ee95a3f644b91948b8b698a9789 Mon Sep 17 00:00:00 2001 From: JuniorJPDJ Date: Wed, 7 Jul 2021 05:27:34 +0200 Subject: [PATCH 4/5] implicitly cast pthread_t to long unsigned int This fixes compilation time errors like: error: format '%lu' expects argument of type 'long unsigned int', but argument 5 has type 'pthread_t' {aka 'struct __pthread *'} --- libtcmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libtcmu.c b/libtcmu.c index c75bb108..14f73a93 100644 --- a/libtcmu.c +++ b/libtcmu.c @@ -860,7 +860,7 @@ void tcmu_set_thread_name(const char *prefix, struct tcmu_device *dev) if (!prefix) { tcmu_dev_err(dev, "Failed to set name for thread %lu\n", - pthread_self()); + (long unsigned int)pthread_self()); return; } From 207e0bd5e33661b2ddd280de7791d0f78c22c1ed Mon Sep 17 00:00:00 2001 From: JuniorJPDJ Date: Wed, 7 Jul 2021 05:33:14 +0200 Subject: [PATCH 5/5] remove pthread_getname_np calls This allows to preserve compatiblity with musl libc which lacks this function --- api.c | 2 ++ libtcmu.c | 6 +----- libtcmu_common.h | 2 ++ libtcmu_log.c | 6 +----- tcmur_work.c | 8 ++------ 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/api.c b/api.c index 90fe8de9..d55da0a8 100644 --- a/api.c +++ b/api.c @@ -28,6 +28,8 @@ #include "libtcmu_priv.h" #include "be_byteshift.h" +__thread int __tcmu_is_ework_thread = 0; + int tcmu_cdb_get_length(uint8_t *cdb) { uint8_t group_code = cdb[0] >> 5; diff --git a/libtcmu.c b/libtcmu.c index 14f73a93..11272183 100644 --- a/libtcmu.c +++ b/libtcmu.c @@ -843,17 +843,13 @@ const char *tcmu_dev_get_uio_name(struct tcmu_device *dev) void tcmu_set_thread_name(const char *prefix, struct tcmu_device *dev) { const char *uio = dev ? tcmu_dev_get_uio_name(dev) : NULL; - char cname[TCMU_THREAD_NAME_LEN]; char *pname; - if (pthread_getname_np(pthread_self(), cname, TCMU_THREAD_NAME_LEN)) - return; - /* * If we are trying to set the pthread name in the * event work thread, we must ignore it. */ - if (!strcmp(cname, "ework-thread")) { + if (__tcmu_is_ework_thread) { tcmu_dev_warn(dev, "Do not set name for event work thread in the callback fn\n"); return; } diff --git a/libtcmu_common.h b/libtcmu_common.h index 19721b04..2897e1f0 100644 --- a/libtcmu_common.h +++ b/libtcmu_common.h @@ -194,6 +194,8 @@ void __tcmu_sense_set_data(uint8_t *sense_buf, uint8_t key, uint16_t asc_ascq); */ void tcmu_thread_cancel(pthread_t thread); +extern __thread int __tcmu_is_ework_thread; + #ifdef __cplusplus } #endif diff --git a/libtcmu_log.c b/libtcmu_log.c index f7b6e2fa..fee27a46 100644 --- a/libtcmu_log.c +++ b/libtcmu_log.c @@ -403,7 +403,6 @@ static int output_to_fd(int pri, const char *timestamp, int fd = (intptr_t) data; char *buf, *msg; int count, ret, written = 0, r, pid = 0; - char pname[TCMU_THREAD_NAME_LEN]; if (fd == -1) return -1; @@ -412,13 +411,10 @@ static int output_to_fd(int pri, const char *timestamp, if (pid <= 0) return -1; - if (pthread_getname_np(pthread_self(), pname, TCMU_THREAD_NAME_LEN)) - return -1; - /* * format: timestamp pid [loglevel] msg */ - ret = asprintf(&msg, "%s %d:%s [%s] %s", timestamp, pid, pname, + ret = asprintf(&msg, "%s %d [%s] %s", timestamp, pid, loglevel_string(pri), str); if (ret < 0) return -1; diff --git a/tcmur_work.c b/tcmur_work.c index ee567e90..801e305f 100644 --- a/tcmur_work.c +++ b/tcmur_work.c @@ -41,16 +41,11 @@ struct tcmur_work *tcmur_create_work(void) static void __tcmur_flush_work(struct tcmur_work *work) { - char pname[TCMU_THREAD_NAME_LEN]; - - if (pthread_getname_np(pthread_self(), pname, TCMU_THREAD_NAME_LEN)) - return; - /* * The event work thread may need to do a handler reopen * call and try to flush itself. Just ignore. */ - if (!strcmp(pname, "ework-thread")) + if (__tcmu_is_ework_thread) return; /* @@ -79,6 +74,7 @@ static void *tcmur_work_fn(void *data) struct private *p = data; tcmu_set_thread_name("ework-thread", NULL); + __tcmu_is_ework_thread = 1; p->work_fn(p->data);