Skip to contentSkip to conversation

Fix portability issues on MacOS

PATCH In review Theodore Ts'o 2 participants 10 messages Apr 3 Library code

lore
Share & tools
lore
#0 lore raw

cover Fix portability issues on MacOS

The recent fuse2fs changes introduced some portability issues; fix them so that e2fsprogs can build on MacOS and create a fuse2fs binary that works on MacOS 26.3.1 using MacFuse.

Note: the f_opt_extent test is failing on Github Actions when testing on MacOS. All of the tests are passing clean up my MacOS laptop. So I'm not sure where the github action failure is coming from.

Theodore Ts'o (3):
libsupport: fix portability issues with the bthread.c
libsupport: add a portable get_thread_id() function
fuse2fs: fix build failure on systems which don't define EUCLEAN

 configure               | 18 ++++++++++++++++++
 configure.ac            |  3 +++
 lib/config.h.in         | 30 +++++++++++++++++++++---------
 lib/support/Makefile.in | 13 +++++++++++--
 lib/support/bthread.c   | 12 ++++++++++--
 lib/support/thread.c    | 36 ++++++++++++++++++++++++++++++++++++
 lib/support/thread.h    |  5 +++++
 misc/fuse2fs.c          |  5 ++++-
 8 files changed, 108 insertions(+), 14 deletions(-)
 create mode 100644 lib/support/thread.c
 create mode 100644 lib/support/thread.h
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/3 libsupport: fix portability issues with the bthread.c

The function pthread_setname_np() is non-portable; that's what the "np" means. In particular, on Mac systems, the function takes only a single argument, while on most other systems which have the function, it takes two arguments.

Also fix a problem where a 1-bit signed integer can only accept values of 0 or -1. Change it to be a 1-bit unsigned integer, which can accept values of 0 or 1. Clang will issue a warning if 1-bit signed integer are used incorrectly, and fail with -Werror.

---
 configure             |  6 ++++++
 configure.ac          |  1 +
 lib/config.h.in       | 24 +++++++++++++++---------
 lib/support/bthread.c | 12 ++++++++++--
 4 files changed, 32 insertions(+), 11 deletions(-)
configure +6 -0
index 4da5439fe..b9a82dcec 100755
@@ -13887,6 +13887,12 @@ if test "x$ac_cv_func_pwrite64" = xyes
 then :
   printf "%s\n" "#define HAVE_PWRITE64 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "pthread_setname_np" "ac_cv_func_pthread_setname_np"
+if test "x$ac_cv_func_pthread_setname_np" = xyes
+then :
+  printf "%s\n" "#define HAVE_PTHREAD_SETNAME_NP 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "qsort_r" "ac_cv_func_qsort_r"
 if test "x$ac_cv_func_qsort_r" = xyes
configure.ac +1 -0
index ecef9df39..2473879fd 100644
@@ -1269,6 +1269,7 @@ AC_CHECK_FUNCS(m4_flatten([
 	pwrite
 	pread64
 	pwrite64
+	pthread_setname_np
 	qsort_r
 	secure_getenv
 	setmntent
lib/config.h.in +15 -9
index 8ea7ec2b1..c6cbced5f 100644
@@ -46,9 +46,6 @@
 /* Define to the version of FUSE to use */
 #undef FUSE_USE_VERSION
 
-/* Define to 1 if fuse supports cache_readdir */
-#undef HAVE_FUSE_CACHE_READDIR
-
 /* Define to 1 if you have the 'add_key' function. */
 #undef HAVE_ADD_KEY
 
@@ -73,9 +70,6 @@
 /* Define to 1 if you have the BSD-style 'qsort_r' function. */
 #undef HAVE_BSD_QSORT_R
 
-/* Define to 1 if PR_SET_IO_FLUSHER is present */
-#undef HAVE_PR_SET_IO_FLUSHER
-
 /* Define to 1 if you have the Mac OS X function
    CFLocaleCopyPreferredLanguages in the CoreFoundation framework. */
 #undef HAVE_CFLOCALECOPYPREFERREDLANGUAGES
@@ -87,6 +81,9 @@
 /* Define to 1 if you have the 'chflags' function. */
 #undef HAVE_CHFLAGS
 
+/* Define to 1 if CLOCK_MONOTONIC is present */
+#undef HAVE_CLOCK_MONOTONIC
+
 /* Define if the GNU dcgettext() function is already present or preinstalled.
    */
 #undef HAVE_DCGETTEXT
@@ -136,9 +133,15 @@
 /* Define to 1 if you have the 'fsync' function. */
 #undef HAVE_FSYNC
 
+/* Define to 1 if FS_IOC_READ_VERITY_METADATA ioctl is available */
+#undef HAVE_FS_IOC_READ_VERITY_METADATA
+
 /* Define to 1 if you have the 'ftruncate64' function. */
 #undef HAVE_FTRUNCATE64
 
+/* Define to 1 if fuse supports cache_readdir */
+#undef HAVE_FUSE_CACHE_READDIR
+
 /* Define to 1 if you have the <fuse.h> header file. */
 #undef HAVE_FUSE_H
 
@@ -313,6 +316,9 @@
 /* Define to 1 if you have the 'pread64' function. */
 #undef HAVE_PREAD64
 
+/* Define to 1 if PR_SET_IO_FLUSHER is present */
+#undef HAVE_PR_SET_IO_FLUSHER
+
 /* Define if you have POSIX threads libraries and header files. */
 #undef HAVE_PTHREAD
 
@@ -322,6 +328,9 @@
 /* Have PTHREAD_PRIO_INHERIT. */
 #undef HAVE_PTHREAD_PRIO_INHERIT
 
+/* Define to 1 if you have the 'pthread_setname_np' function. */
+#undef HAVE_PTHREAD_SETNAME_NP
+
 /* Define to 1 if you have the 'pwrite' function. */
 #undef HAVE_PWRITE
 
@@ -699,7 +708,4 @@
 /* Define to 1 on platforms where this makes time_t a 64-bit type. */
 #undef __MINGW_USE_VC2005_COMPAT
 
-/* Define to 1 if CLOCK_MONOTONIC is present */
-#undef HAVE_CLOCK_MONOTONIC
-
 #include <dirpaths.h>
lib/support/bthread.c +10 -2
index 936ca0f0f..87eeb1b3d 100644
@@ -9,6 +9,7 @@
  * %End-Header%
  */
 #include "config.h"
+#ifdef HAVE_PTHREAD
 #include <stdlib.h>
 #include <errno.h>
 #include <pthread.h>
@@ -33,7 +34,7 @@ struct bthread {
 	bthread_fn_t fn;
 	void *data;
 	unsigned int period; /* seconds */
-	int can_join:1;
+	unsigned int can_join:1;
 };
 
 /* Wait for a signal or for the periodic interval */
@@ -101,7 +102,13 @@ int bthread_create(const char *name,  bthread_fn_t fn, void *data,
 	if (error)
 		goto out_cond;
 
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#ifdef __APPLE__
+	pthread_setname_np(name);
+#else
 	pthread_setname_np(bt->thread, name);
+#endif
+#endif
 
 	*btp = bt;
 	return 0;
@@ -178,7 +185,7 @@ int bthread_cancel(struct bthread *bt)
 /* Ask the thread to cancel itself and wait for it */
 void bthread_stop(struct bthread *bt)
 {
-	int need_join = 0;
+	unsigned int need_join = 0;
 
 	pthread_mutex_lock(&bt->lock);
 	switch (bt->state) {
@@ -199,3 +206,4 @@ void bthread_stop(struct bthread *bt)
 	if (need_join)
 		pthread_join(bt->thread, NULL);
 }
+#endif /* HAVE_PTHREAD */
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH 1/3] libsupport: fix portability issues with the bthread.c

Quoted 52 lines · Theodore Ts'o

The function pthread_setname_np() is non-portable; that's what the "np" means. In particular, on Mac systems, the function takes only a single argument, while on most other systems which have the function, it takes two arguments.

Also fix a problem where a 1-bit signed integer can only accept values of 0 or -1. Change it to be a 1-bit unsigned integer, which can accept values of 0 or 1. Clang will issue a warning if 1-bit signed integer are used incorrectly, and fail with -Werror.

---
 configure             |  6 ++++++
 configure.ac          |  1 +
 lib/config.h.in       | 24 +++++++++++++++---------
 lib/support/bthread.c | 12 ++++++++++--
 4 files changed, 32 insertions(+), 11 deletions(-)
configure +6 -0
index 4da5439fe..b9a82dcec 100755
@@ -13887,6 +13887,12 @@ if test "x$ac_cv_func_pwrite64" = xyes
 then :
   printf "%s\n" "#define HAVE_PWRITE64 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "pthread_setname_np" "ac_cv_func_pthread_setname_np"
+if test "x$ac_cv_func_pthread_setname_np" = xyes
+then :
+  printf "%s\n" "#define HAVE_PTHREAD_SETNAME_NP 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "qsort_r" "ac_cv_func_qsort_r"
 if test "x$ac_cv_func_qsort_r" = xyes
configure.ac +1 -0
index ecef9df39..2473879fd 100644
@@ -1269,6 +1269,7 @@ AC_CHECK_FUNCS(m4_flatten([
 	pwrite
 	pread64
 	pwrite64
+	pthread_setname_np
 	qsort_r
 	secure_getenv
 	setmntent
lib/config.h.in +0 -0
index 8ea7ec2b1..c6cbced5f 100644

lib/config.h.in line 50

Quoted 5 lines
lib/config.h.in +0 -2
@@ -46,9 +46,6 @@
 /* Define to the version of FUSE to use */
 #undef FUSE_USE_VERSION
 
-/* Define to 1 if fuse supports cache_readdir */
-#undef HAVE_FUSE_CACHE_READDIR

Huh, there's a lot of churn in this file. Do you have a magic script somewhere that regenerates config.h.in?

I don't see any problems with this file's changes, but I could also not scatter junk everywhere :)

Quoted 73 lines
lib/config.h.in +15 -7
-
 /* Define to 1 if you have the 'add_key' function. */
 #undef HAVE_ADD_KEY
 
@@ -73,9 +70,6 @@
 /* Define to 1 if you have the BSD-style 'qsort_r' function. */
 #undef HAVE_BSD_QSORT_R
 
-/* Define to 1 if PR_SET_IO_FLUSHER is present */
-#undef HAVE_PR_SET_IO_FLUSHER
-
 /* Define to 1 if you have the Mac OS X function
    CFLocaleCopyPreferredLanguages in the CoreFoundation framework. */
 #undef HAVE_CFLOCALECOPYPREFERREDLANGUAGES
@@ -87,6 +81,9 @@
 /* Define to 1 if you have the 'chflags' function. */
 #undef HAVE_CHFLAGS
 
+/* Define to 1 if CLOCK_MONOTONIC is present */
+#undef HAVE_CLOCK_MONOTONIC
+
 /* Define if the GNU dcgettext() function is already present or preinstalled.
    */
 #undef HAVE_DCGETTEXT
@@ -136,9 +133,15 @@
 /* Define to 1 if you have the 'fsync' function. */
 #undef HAVE_FSYNC
 
+/* Define to 1 if FS_IOC_READ_VERITY_METADATA ioctl is available */
+#undef HAVE_FS_IOC_READ_VERITY_METADATA
+
 /* Define to 1 if you have the 'ftruncate64' function. */
 #undef HAVE_FTRUNCATE64
 
+/* Define to 1 if fuse supports cache_readdir */
+#undef HAVE_FUSE_CACHE_READDIR
+
 /* Define to 1 if you have the <fuse.h> header file. */
 #undef HAVE_FUSE_H
 
@@ -313,6 +316,9 @@
 /* Define to 1 if you have the 'pread64' function. */
 #undef HAVE_PREAD64
 
+/* Define to 1 if PR_SET_IO_FLUSHER is present */
+#undef HAVE_PR_SET_IO_FLUSHER
+
 /* Define if you have POSIX threads libraries and header files. */
 #undef HAVE_PTHREAD
 
@@ -322,6 +328,9 @@
 /* Have PTHREAD_PRIO_INHERIT. */
 #undef HAVE_PTHREAD_PRIO_INHERIT
 
+/* Define to 1 if you have the 'pthread_setname_np' function. */
+#undef HAVE_PTHREAD_SETNAME_NP
+
 /* Define to 1 if you have the 'pwrite' function. */
 #undef HAVE_PWRITE
 
@@ -699,7 +708,4 @@
 /* Define to 1 on platforms where this makes time_t a 64-bit type. */
 #undef __MINGW_USE_VC2005_COMPAT
 
-/* Define to 1 if CLOCK_MONOTONIC is present */
-#undef HAVE_CLOCK_MONOTONIC
-
 #include <dirpaths.h>
lib/support/bthread.c +0 -0
index 936ca0f0f..87eeb1b3d 100644

lib/support/bthread.c line 12

Quoted 4 lines
lib/support/bthread.c +1 -0
@@ -9,6 +9,7 @@
  * %End-Header%
  */
 #include "config.h"
+#ifdef HAVE_PTHREAD

Hmmm if we don't have pthreads, then shouldn't bthread.h also exclude all the function declarations ifndef HAVE_PTHREAD?

lib/support/bthread.c struct bthread { · line 37

Quoted 9 lines
lib/support/bthread.c +1 -1
 #include <stdlib.h>
 #include <errno.h>
 #include <pthread.h>
@@ -33,7 +34,7 @@ struct bthread {
 	bthread_fn_t fn;
 	void *data;
 	unsigned int period; /* seconds */
-	int can_join:1;
+	unsigned int can_join:1;

That works, though I suppose you could change it to bool.

--D

Quoted 34 lines
lib/support/bthread.c +8 -1
 };
 
 /* Wait for a signal or for the periodic interval */
@@ -101,7 +102,13 @@ int bthread_create(const char *name,  bthread_fn_t fn, void *data,
 	if (error)
 		goto out_cond;
 
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#ifdef __APPLE__
+	pthread_setname_np(name);
+#else
 	pthread_setname_np(bt->thread, name);
+#endif
+#endif
 
 	*btp = bt;
 	return 0;
@@ -178,7 +185,7 @@ int bthread_cancel(struct bthread *bt)
 /* Ask the thread to cancel itself and wait for it */
 void bthread_stop(struct bthread *bt)
 {
-	int need_join = 0;
+	unsigned int need_join = 0;
 
 	pthread_mutex_lock(&bt->lock);
 	switch (bt->state) {
@@ -199,3 +206,4 @@ void bthread_stop(struct bthread *bt)
 	if (need_join)
 		pthread_join(bt->thread, NULL);
 }
+#endif /* HAVE_PTHREAD */
Signature · 1 line
2.51.0
#3 lore raw

Re: [PATCH 1/3] libsupport: fix portability issues with the bthread.c

Quoted 5 lines · Darrick J. Wong

lib/config.h.in line 50

Quoted 2 lines
lib/config.h.in +0 -2
-/* Define to 1 if fuse supports cache_readdir */
-#undef HAVE_FUSE_CACHE_READDIR

Huh, there's a lot of churn in this file. Do you have a magic script somewhere that regenerates config.h.in?

It's "autoheader". Whenver you add or remove tests to configure.ac, you need to run "autoconf ; autoheader". If you don't run "autoheader", then even if the autoconf's feature test enables some new feature test, say HAVE_PR_SET_IO_FLUSHER, the feature won't actually be enabled in the #ifdef.

Cheers,

- Ted

#4 lore raw

Re: [PATCH 1/3] libsupport: fix portability issues with the bthread.c

Quoted 7 lines · Theodore Tso
Quoted 5 lines · Darrick J. Wong

lib/config.h.in line 50

Quoted 2 lines
lib/config.h.in +0 -2
-/* Define to 1 if fuse supports cache_readdir */
-#undef HAVE_FUSE_CACHE_READDIR

Huh, there's a lot of churn in this file. Do you have a magic script somewhere that regenerates config.h.in?

Quoted 5 lines

It's "autoheader". Whenver you add or remove tests to configure.ac, you need to run "autoconf ; autoheader". If you don't run "autoheader", then even if the autoconf's feature test enables some new feature test, say HAVE_PR_SET_IO_FLUSHER, the feature won't actually be enabled in the #ifdef.

Aha! Thanks for that tip; I'll go fix my dev branch.

--D

Quoted 3 lines

Cheers,

- Ted

#5 lore raw

Re: [PATCH 1/3] libsupport: fix portability issues with the bthread.c

Quoted 7 lines · Darrick J. Wong
Quoted 5 lines

It's "autoheader". Whenver you add or remove tests to configure.ac, you need to run "autoconf ; autoheader". If you don't run "autoheader", then even if the autoconf's feature test enables some new feature test, say HAVE_PR_SET_IO_FLUSHER, the feature won't actually be enabled in the #ifdef.

Aha! Thanks for that tip; I'll go fix my dev branch.

Just to give more context, there are two ways that configure scripts can pass the feature definitions to programs. One is via the command line of the compiler. The problem with that is if you have dozens of options like -DHAVE_PR_SET_IO_FLUSHER for every single compile, it bloats the MAKELOG files and makes it hard to read, and eventually you can run into the command-line length limits.

The other way is via a config.h file which is then #included in each source file. But for *that* to work, the config.h.in file needs to have a template for echo variable that can be #defined. And that's what autoheader takes care of for you. Simpler projects that don't use a config.h file and pass everything on the compiler's command line don't need to use autoheader.

For a demonstration, try editing lib/config.h.in to remove the lines:

-/* Define to 1 if PR_SET_IO_FLUSHER is present */ -#undef HAVE_PR_SET_IO_FLUSHER

Then "rm lib/config.h" and run "./config.status", and examine lib/config.h. You will see that HAVE_PR_SET_IO_FLUSHER is no longer defined in the config.h file. Then run "autoheader", and then re-run "./config.status", and you will see that HAVE_PR_SET_IO_FLUSHER is properly defined.

Cheers,

- Ted

#6 lore raw

PATCH 2/3 libsupport: add a portable get_thread_id() function

In reply to Theodore Ts'o

The gettid() system call is only available on Linux. So create a new function, get_thread_id() which implements a number of different ways of providing a thread id as an integer.

Use get_thread_id() instead of gettid() in fuse2fs.

---
 configure               | 12 ++++++++++++
 configure.ac            |  2 ++
 lib/config.h.in         |  6 ++++++
 lib/support/Makefile.in | 13 +++++++++++--
 lib/support/thread.c    | 36 ++++++++++++++++++++++++++++++++++++
 lib/support/thread.h    |  5 +++++
 misc/fuse2fs.c          |  3 ++-
 7 files changed, 74 insertions(+), 3 deletions(-)
 create mode 100644 lib/support/thread.c
 create mode 100644 lib/support/thread.h
configure +12 -0
index b9a82dcec..b04b31aff 100755
@@ -13749,6 +13749,12 @@ if test "x$ac_cv_func_getrusage" = xyes
 then :
   printf "%s\n" "#define HAVE_GETRUSAGE 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "gettid" "ac_cv_func_gettid"
+if test "x$ac_cv_func_gettid" = xyes
+then :
+  printf "%s\n" "#define HAVE_GETTID 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "jrand48" "ac_cv_func_jrand48"
 if test "x$ac_cv_func_jrand48" = xyes
@@ -13893,6 +13899,12 @@ if test "x$ac_cv_func_pthread_setname_np" = xyes
 then :
   printf "%s\n" "#define HAVE_PTHREAD_SETNAME_NP 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "pthread_threadid_np" "ac_cv_func_pthread_threadid_np"
+if test "x$ac_cv_func_pthread_threadid_np" = xyes
+then :
+  printf "%s\n" "#define HAVE_PTHREAD_THREADID_NP 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "qsort_r" "ac_cv_func_qsort_r"
 if test "x$ac_cv_func_qsort_r" = xyes
configure.ac +2 -0
index 2473879fd..4921f81f7 100644
@@ -1246,6 +1246,7 @@ AC_CHECK_FUNCS(m4_flatten([
 	getrandom
 	getrlimit
 	getrusage
+	gettid
 	jrand48
 	keyctl
 	llistxattr
@@ -1270,6 +1271,7 @@ AC_CHECK_FUNCS(m4_flatten([
 	pread64
 	pwrite64
 	pthread_setname_np
+	pthread_threadid_np
 	qsort_r
 	secure_getenv
 	setmntent
lib/config.h.in +6 -0
index c6cbced5f..f129abfe7 100644
@@ -181,6 +181,9 @@
 /* Define if the GNU gettext() function is already present or preinstalled. */
 #undef HAVE_GETTEXT
 
+/* Define to 1 if you have the 'gettid' function. */
+#undef HAVE_GETTID
+
 /* Define to 1 if you have the GNU-style 'qsort_r' function. */
 #undef HAVE_GNU_QSORT_R
 
@@ -331,6 +334,9 @@
 /* Define to 1 if you have the 'pthread_setname_np' function. */
 #undef HAVE_PTHREAD_SETNAME_NP
 
+/* Define to 1 if you have the 'pthread_threadid_np' function. */
+#undef HAVE_PTHREAD_THREADID_NP
+
 /* Define to 1 if you have the 'pwrite' function. */
 #undef HAVE_PWRITE
 
lib/support/Makefile.in +11 -2
index 6383816fd..9aac9cf00 100644
@@ -25,6 +25,7 @@ OBJS=		bthread.o \
 		quotaio.o \
 		quotaio_v2.o \
 		quotaio_tree.o \
+		thread.o \
 		dict.o \
 		devname.o
 
@@ -41,6 +42,7 @@ SRCS=		$(srcdir)/argv_parse.c \
 		$(srcdir)/quotaio.c \
 		$(srcdir)/quotaio_tree.c \
 		$(srcdir)/quotaio_v2.c \
+		$(srcdir)/thread.c \
 		$(srcdir)/dict.c \
 		$(srcdir)/devname.c
 
@@ -81,10 +83,15 @@ test_cstring: $(srcdir)/cstring.c
 	$(Q) $(CC) -o test_cstring -DDEBUG_PROGRAM $(srcdir)/cstring.c \
 		$(ALL_CFLAGS)
 
+test_thread: $(srcdir)/thread.c
+	$(E) " CC $@"
+	$(Q) $(CC) -o test_thread -DDEBUG_PROGRAM $(srcdir)/thread.c \
+		$(ALL_CFLAGS)
+
 clean::
 	$(RM) -f \#* *.s *.o *.a *~ *.bak core profiled/* \
 		../libsupport.a ../libsupport_p.a $(SMANPAGES) \
-		prof_err.c prof_err.h test_profile test_cstring
+		prof_err.c prof_err.h test_profile test_cstring test_thread
 
 #fullcheck check:: tst_uuid
 #	LD_LIBRARY_PATH=$(LIB) DYLD_LIBRARY_PATH=$(LIB) ./tst_uuid
@@ -111,7 +118,7 @@ $(OBJS):
 argv_parse.o: $(srcdir)/argv_parse.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/argv_parse.h
 bthread.o: $(srcdir)/bthread.c $(top_builddir)/lib/config.h \
- $(srcdir)/bthread.h
+ $(top_builddir)/lib/dirpaths.h $(srcdir)/bthread.h
 cstring.o: $(srcdir)/cstring.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/cstring.h
 mkquota.o: $(srcdir)/mkquota.c $(top_builddir)/lib/config.h \
@@ -183,6 +190,8 @@ quotaio_v2.o: $(srcdir)/quotaio_v2.c $(top_builddir)/lib/config.h \
  $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \
  $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/dqblk_v2.h \
  $(srcdir)/quotaio_tree.h
+thread.o: $(srcdir)/thread.c $(top_builddir)/lib/config.h \
+ $(top_builddir)/lib/dirpaths.h $(srcdir)/thread.h
 dict.o: $(srcdir)/dict.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/dict.h
 devname.o: $(srcdir)/devname.c $(top_builddir)/lib/config.h \
lib/support/thread.c new file +36 -0
new file mode 100644
index 000000000..a9a10940c
@@ -0,0 +1,36 @@
+/*
+ * thread.c - utility functions for Posix threads
+ */
+
+#include "config.h"
+#ifdef HAVE_PTHREAD
+#include <pthread.h>
+#endif
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "support/thread.h"
+
+uint64_t get_thread_id(void)
+{
+#if defined(HAVE_GETTID)
+	return gettid();
+#elif defined(HAVE_PTHREAD_THREADID_NP)
+	uint64_t tid;
+
+	if (pthread_threadid_np(NULL, &tid))
+		return tid;
+#elif defined(HAVE_PTHREAD)
+	return (__u64)(uintptr_t) pthread_self();
+#endif
+	return getpid();
+}
+
+#ifdef DEBUG_PROGRAM
+int main(int argc, char **argv)
+{
+	printf("Thread id: %llu\n", get_thread_id());
+	return 0;
+}
+#endif
lib/support/thread.h new file +5 -0
new file mode 100644
index 000000000..9a7f5c9db
@@ -0,0 +1,5 @@
+/*
+ * thread.h -- header file for thread utilities
+ */
+
+uint64_t get_thread_id(void);
misc/fuse2fs.c +2 -1
index 0b43ec0fb..dfbc98636 100644
@@ -48,6 +48,7 @@
 #include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fsP.h"
 #include "support/bthread.h"
+#include "support/thread.h"
 #if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
 # define FUSE_PLATFORM_OPTS	""
 #else
@@ -148,7 +149,7 @@ static inline uint64_t round_down(uint64_t b, unsigned int align)
 
 #define dbg_printf(fuse2fs, format, ...) \
 	while ((fuse2fs)->debug) { \
-		printf("FUSE2FS (%s): tid=%d " format, (fuse2fs)->shortdev, gettid(), ##__VA_ARGS__); \
+		printf("FUSE2FS (%s): tid=%llu " format, (fuse2fs)->shortdev, get_thread_id(), ##__VA_ARGS__); \
 		fflush(stdout); \
 		break; \
 	}
Signature · 1 line
2.51.0
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] libsupport: add a portable get_thread_id() function

Quoted 154 lines · Theodore Ts'o

The gettid() system call is only available on Linux. So create a new function, get_thread_id() which implements a number of different ways of providing a thread id as an integer.

Use get_thread_id() instead of gettid() in fuse2fs.

---
 configure               | 12 ++++++++++++
 configure.ac            |  2 ++
 lib/config.h.in         |  6 ++++++
 lib/support/Makefile.in | 13 +++++++++++--
 lib/support/thread.c    | 36 ++++++++++++++++++++++++++++++++++++
 lib/support/thread.h    |  5 +++++
 misc/fuse2fs.c          |  3 ++-
 7 files changed, 74 insertions(+), 3 deletions(-)
 create mode 100644 lib/support/thread.c
 create mode 100644 lib/support/thread.h
configure +12 -0
index b9a82dcec..b04b31aff 100755
@@ -13749,6 +13749,12 @@ if test "x$ac_cv_func_getrusage" = xyes
 then :
   printf "%s\n" "#define HAVE_GETRUSAGE 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "gettid" "ac_cv_func_gettid"
+if test "x$ac_cv_func_gettid" = xyes
+then :
+  printf "%s\n" "#define HAVE_GETTID 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "jrand48" "ac_cv_func_jrand48"
 if test "x$ac_cv_func_jrand48" = xyes
@@ -13893,6 +13899,12 @@ if test "x$ac_cv_func_pthread_setname_np" = xyes
 then :
   printf "%s\n" "#define HAVE_PTHREAD_SETNAME_NP 1" >>confdefs.h
 
+fi
+ac_fn_c_check_func "$LINENO" "pthread_threadid_np" "ac_cv_func_pthread_threadid_np"
+if test "x$ac_cv_func_pthread_threadid_np" = xyes
+then :
+  printf "%s\n" "#define HAVE_PTHREAD_THREADID_NP 1" >>confdefs.h
+
 fi
 ac_fn_c_check_func "$LINENO" "qsort_r" "ac_cv_func_qsort_r"
 if test "x$ac_cv_func_qsort_r" = xyes
configure.ac +2 -0
index 2473879fd..4921f81f7 100644
@@ -1246,6 +1246,7 @@ AC_CHECK_FUNCS(m4_flatten([
 	getrandom
 	getrlimit
 	getrusage
+	gettid
 	jrand48
 	keyctl
 	llistxattr
@@ -1270,6 +1271,7 @@ AC_CHECK_FUNCS(m4_flatten([
 	pread64
 	pwrite64
 	pthread_setname_np
+	pthread_threadid_np
 	qsort_r
 	secure_getenv
 	setmntent
lib/config.h.in +6 -0
index c6cbced5f..f129abfe7 100644
@@ -181,6 +181,9 @@
 /* Define if the GNU gettext() function is already present or preinstalled. */
 #undef HAVE_GETTEXT
 
+/* Define to 1 if you have the 'gettid' function. */
+#undef HAVE_GETTID
+
 /* Define to 1 if you have the GNU-style 'qsort_r' function. */
 #undef HAVE_GNU_QSORT_R
 
@@ -331,6 +334,9 @@
 /* Define to 1 if you have the 'pthread_setname_np' function. */
 #undef HAVE_PTHREAD_SETNAME_NP
 
+/* Define to 1 if you have the 'pthread_threadid_np' function. */
+#undef HAVE_PTHREAD_THREADID_NP
+
 /* Define to 1 if you have the 'pwrite' function. */
 #undef HAVE_PWRITE
 
lib/support/Makefile.in +11 -2
index 6383816fd..9aac9cf00 100644
@@ -25,6 +25,7 @@ OBJS=		bthread.o \
 		quotaio.o \
 		quotaio_v2.o \
 		quotaio_tree.o \
+		thread.o \
 		dict.o \
 		devname.o
 
@@ -41,6 +42,7 @@ SRCS=		$(srcdir)/argv_parse.c \
 		$(srcdir)/quotaio.c \
 		$(srcdir)/quotaio_tree.c \
 		$(srcdir)/quotaio_v2.c \
+		$(srcdir)/thread.c \
 		$(srcdir)/dict.c \
 		$(srcdir)/devname.c
 
@@ -81,10 +83,15 @@ test_cstring: $(srcdir)/cstring.c
 	$(Q) $(CC) -o test_cstring -DDEBUG_PROGRAM $(srcdir)/cstring.c \
 		$(ALL_CFLAGS)
 
+test_thread: $(srcdir)/thread.c
+	$(E) " CC $@"
+	$(Q) $(CC) -o test_thread -DDEBUG_PROGRAM $(srcdir)/thread.c \
+		$(ALL_CFLAGS)
+
 clean::
 	$(RM) -f \#* *.s *.o *.a *~ *.bak core profiled/* \
 		../libsupport.a ../libsupport_p.a $(SMANPAGES) \
-		prof_err.c prof_err.h test_profile test_cstring
+		prof_err.c prof_err.h test_profile test_cstring test_thread
 
 #fullcheck check:: tst_uuid
 #	LD_LIBRARY_PATH=$(LIB) DYLD_LIBRARY_PATH=$(LIB) ./tst_uuid
@@ -111,7 +118,7 @@ $(OBJS):
 argv_parse.o: $(srcdir)/argv_parse.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/argv_parse.h
 bthread.o: $(srcdir)/bthread.c $(top_builddir)/lib/config.h \
- $(srcdir)/bthread.h
+ $(top_builddir)/lib/dirpaths.h $(srcdir)/bthread.h
 cstring.o: $(srcdir)/cstring.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/cstring.h
 mkquota.o: $(srcdir)/mkquota.c $(top_builddir)/lib/config.h \
@@ -183,6 +190,8 @@ quotaio_v2.o: $(srcdir)/quotaio_v2.c $(top_builddir)/lib/config.h \
  $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \
  $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/dqblk_v2.h \
  $(srcdir)/quotaio_tree.h
+thread.o: $(srcdir)/thread.c $(top_builddir)/lib/config.h \
+ $(top_builddir)/lib/dirpaths.h $(srcdir)/thread.h
 dict.o: $(srcdir)/dict.c $(top_builddir)/lib/config.h \
  $(top_builddir)/lib/dirpaths.h $(srcdir)/dict.h
 devname.o: $(srcdir)/devname.c $(top_builddir)/lib/config.h \
lib/support/thread.c new file +0 -0
new file mode 100644
index 000000000..a9a10940c

lib/support/thread.c line 2

Quoted 2 lines
lib/support/thread.c new file +2 -0
@@ -0,0 +1,36 @@
+/*
+ * thread.c - utility functions for Posix threads

Should these new files have an explicit license specification?

Other than that,

--D

Quoted 69 lines
lib/support/thread.c new file +34 -0
+ */
+
+#include "config.h"
+#ifdef HAVE_PTHREAD
+#include <pthread.h>
+#endif
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "support/thread.h"
+
+uint64_t get_thread_id(void)
+{
+#if defined(HAVE_GETTID)
+	return gettid();
+#elif defined(HAVE_PTHREAD_THREADID_NP)
+	uint64_t tid;
+
+	if (pthread_threadid_np(NULL, &tid))
+		return tid;
+#elif defined(HAVE_PTHREAD)
+	return (__u64)(uintptr_t) pthread_self();
+#endif
+	return getpid();
+}
+
+#ifdef DEBUG_PROGRAM
+int main(int argc, char **argv)
+{
+	printf("Thread id: %llu\n", get_thread_id());
+	return 0;
+}
+#endif
lib/support/thread.h new file +5 -0
new file mode 100644
index 000000000..9a7f5c9db
@@ -0,0 +1,5 @@
+/*
+ * thread.h -- header file for thread utilities
+ */
+
+uint64_t get_thread_id(void);
misc/fuse2fs.c +2 -1
index 0b43ec0fb..dfbc98636 100644
@@ -48,6 +48,7 @@
 #include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fsP.h"
 #include "support/bthread.h"
+#include "support/thread.h"
 #if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
 # define FUSE_PLATFORM_OPTS	""
 #else
@@ -148,7 +149,7 @@ static inline uint64_t round_down(uint64_t b, unsigned int align)
 
 #define dbg_printf(fuse2fs, format, ...) \
 	while ((fuse2fs)->debug) { \
-		printf("FUSE2FS (%s): tid=%d " format, (fuse2fs)->shortdev, gettid(), ##__VA_ARGS__); \
+		printf("FUSE2FS (%s): tid=%llu " format, (fuse2fs)->shortdev, get_thread_id(), ##__VA_ARGS__); \
 		fflush(stdout); \
 		break; \
 	}
Signature · 1 line
2.51.0
#8 lore raw

PATCH 3/3 fuse2fs: fix build failure on systems which don't define EUCLEAN

In reply to Theodore Ts'o

MacOS doesn't have EUCLEAN, so we use EIO as the closest error code. But then we need to avoid a compile error caused by a duplicate case labels of EUCLEAN and EIO.

---
 misc/fuse2fs.c | 2 ++
 1 file changed, 2 insertions(+)
misc/fuse2fs.c +2 -0
index dfbc98636..94e289fab 100644
@@ -5870,7 +5870,9 @@ static int __translate_error(ext2_filsys fs, ext2_ino_t ino, errcode_t err,
 #ifdef EILSEQ
 	case EILSEQ:
 #endif
+#if EUCLEAN != EIO
 	case EUCLEAN:
+#endif
 		/* these errnos usually denote corruption or persistence fail */
 		is_err = 1;
 		ret = -err;
Signature · 1 line
2.51.0
Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH 3/3] fuse2fs: fix build failure on systems which don't define EUCLEAN

Quoted 5 lines · Theodore Ts'o

MacOS doesn't have EUCLEAN, so we use EIO as the closest error code. But then we need to avoid a compile error caused by a duplicate case labels of EUCLEAN and EIO.

Looks good to me,

--D

Quoted 21 lines
---
 misc/fuse2fs.c | 2 ++
 1 file changed, 2 insertions(+)
misc/fuse2fs.c +2 -0
index dfbc98636..94e289fab 100644
@@ -5870,7 +5870,9 @@ static int __translate_error(ext2_filsys fs, ext2_ino_t ino, errcode_t err,
 #ifdef EILSEQ
 	case EILSEQ:
 #endif
+#if EUCLEAN != EIO
 	case EUCLEAN:
+#endif
 		/* these errnos usually denote corruption or persistence fail */
 		is_err = 1;
 		ret = -err;
Signature · 1 line
2.51.0