man/man3/pthread_attr_init.3: Replace errc
PATCH 1/1 man/man3/pthread_attr_init.3: Replace errc
From: "Dr. David Alan Gilbert" <[email protected]>
The pthread_attr_init.3 example uses 'errc' to exit on an error printing the error code. However, 'errc' is a BSDism that Linux doesn't (and has never?) have.
Replace it by 'errx' with a strerror() call.
--- man/man3/pthread_attr_init.3 | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
man/man3/pthread_attr_init.3 +16 -15
@@ -153,6 +153,7 @@ .SS Program source #include <pthread.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> \& static void
@@ -165,7 +166,7 @@ .SS Program source \& s = pthread_attr_getdetachstate(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate"); + errx(EXIT_FAILURE, "pthread_attr_getdetachstate: %s", strerror(s)); printf("%sDetach state = %s\[rs]n", prefix, (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" : (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
@@ -173,7 +174,7 @@ .SS Program source \& s = pthread_attr_getscope(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getscope"); + errx(EXIT_FAILURE, "pthread_attr_getscope: %s", strerror(s)); printf("%sScope = %s\[rs]n", prefix, (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" : (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
@@ -181,7 +182,7 @@ .SS Program source \& s = pthread_attr_getinheritsched(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched"); + errx(EXIT_FAILURE, "pthread_attr_getinheritsched: %s", strerror(s)); printf("%sInherit scheduler = %s\[rs]n", prefix, (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" : (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
@@ -189,7 +190,7 @@ .SS Program source \& s = pthread_attr_getschedpolicy(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy"); + errx(EXIT_FAILURE, "pthread_attr_getschedpolicy: %s", strerror(s)); printf("%sScheduling policy = %s\[rs]n", prefix, (i == SCHED_OTHER) ? "SCHED_OTHER" : (i == SCHED_FIFO) ? "SCHED_FIFO" :
@@ -198,17 +199,17 @@ .SS Program source \& s = pthread_attr_getschedparam(attr, &sp); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getschedparam"); + errx(EXIT_FAILURE, "pthread_attr_getschedparam: %s", strerror(s)); printf("%sScheduling priority = %d\[rs]n", prefix, sp.sched_priority); \& s = pthread_attr_getguardsize(attr, &v); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getguardsize"); + errx(EXIT_FAILURE, "pthread_attr_getguardsize: %s", strerror(s)); printf("%sGuard size = %zu bytes\[rs]n", prefix, v); \& s = pthread_attr_getstack(attr, &stkaddr, &v); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getstack"); + errx(EXIT_FAILURE, "pthread_attr_getstack: %s", strerror(s)); printf("%sStack address = %p\[rs]n", prefix, stkaddr); printf("%sStack size = %#zx bytes\[rs]n", prefix, v); }
@@ -225,7 +226,7 @@ .SS Program source \& s = pthread_getattr_np(pthread_self(), &gattr); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_getattr_np"); + errx(EXIT_FAILURE, "pthread_getattr_np: %s", strerror(s)); \& printf("Thread attributes:\[rs]n"); display_pthread_attr(&gattr, "\[rs]t");
@@ -255,37 +256,37 @@ .SS Program source \& s = pthread_attr_init(&attr); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_init"); + errx(EXIT_FAILURE, "pthread_attr_init: %s", strerror(s)); \& s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_setdetachstate"); + errx(EXIT_FAILURE, "pthread_attr_setdetachstate: %s", strerror(s)); \& s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_setinheritsched"); + errx(EXIT_FAILURE, "pthread_attr_setinheritsched: %s", strerror(s)); \& stack_size = strtoul(argv[1], NULL, 0); \& s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size); if (s != 0) - errc(EXIT_FAILURE, s, "posix_memalign"); + errx(EXIT_FAILURE, "posix_memalign: %s", strerror(s)); \& printf("posix_memalign() allocated at %p\[rs]n", sp); \& s = pthread_attr_setstack(&attr, sp, stack_size); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_setstack"); + errx(EXIT_FAILURE, "pthread_attr_setstack: %s", strerror(s)); } \& s = pthread_create(&thr, attrp, &thread_start, NULL); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_create"); + errx(EXIT_FAILURE, "pthread_create: %s", strerror(s)); \& if (attrp != NULL) { s = pthread_attr_destroy(attrp); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_destroy"); + errx(EXIT_FAILURE, "pthread_attr_destroy: %s", strerror(s)); } \& pause(); /* Terminates when other thread calls exit() */
Signature · 1 line
2.50.1
Alejandro Colomar
maintainerHi Dave,
Quoted 5 lines · On Wed, Aug 20, 2025 at 02:27:25AM +0100, [email protected] wrote:
From: "Dr. David Alan Gilbert" <[email protected]>
The pthread_attr_init.3 example uses 'errc' to exit on an error printing the error code. However, 'errc' is a BSDism that Linux doesn't (and has never?) have.
Libbsd provides a compatbility layer to provide errc(3bsd) on Linux (and many POSIX systems). libbsd is available in many distros (in Debian, you should install libbsd-dev). BTW, it would be nice if glibc decided to pick this API eventually.
Have a lovely day!
Alex
Quoted 133 lines
Replace it by 'errx' with a
strerror()call.Signed-off-by Dr. David Alan Gilbert <[email protected]>--- man/man3/pthread_attr_init.3 | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)man/man3/pthread_attr_init.3 +16
-15@@ -153,6 +153,7 @@ .SS Program source #include <pthread.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> \& static void@@ -165,7 +166,7 @@ .SS Program source \& s = pthread_attr_getdetachstate(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate"); + errx(EXIT_FAILURE, "pthread_attr_getdetachstate: %s", strerror(s)); printf("%sDetach state = %s\[rs]n", prefix, (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" : (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :@@ -173,7 +174,7 @@ .SS Program source \& s = pthread_attr_getscope(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getscope"); + errx(EXIT_FAILURE, "pthread_attr_getscope: %s", strerror(s)); printf("%sScope = %s\[rs]n", prefix, (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" : (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :@@ -181,7 +182,7 @@ .SS Program source \& s = pthread_attr_getinheritsched(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched"); + errx(EXIT_FAILURE, "pthread_attr_getinheritsched: %s", strerror(s)); printf("%sInherit scheduler = %s\[rs]n", prefix, (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" : (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :@@ -189,7 +190,7 @@ .SS Program source \& s = pthread_attr_getschedpolicy(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy"); + errx(EXIT_FAILURE, "pthread_attr_getschedpolicy: %s", strerror(s)); printf("%sScheduling policy = %s\[rs]n", prefix, (i == SCHED_OTHER) ? "SCHED_OTHER" : (i == SCHED_FIFO) ? "SCHED_FIFO" :@@ -198,17 +199,17 @@ .SS Program source \& s = pthread_attr_getschedparam(attr, &sp); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getschedparam"); + errx(EXIT_FAILURE, "pthread_attr_getschedparam: %s", strerror(s)); printf("%sScheduling priority = %d\[rs]n", prefix, sp.sched_priority); \& s = pthread_attr_getguardsize(attr, &v); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getguardsize"); + errx(EXIT_FAILURE, "pthread_attr_getguardsize: %s", strerror(s)); printf("%sGuard size = %zu bytes\[rs]n", prefix, v); \& s = pthread_attr_getstack(attr, &stkaddr, &v); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getstack"); + errx(EXIT_FAILURE, "pthread_attr_getstack: %s", strerror(s)); printf("%sStack address = %p\[rs]n", prefix, stkaddr); printf("%sStack size = %#zx bytes\[rs]n", prefix, v); }@@ -225,7 +226,7 @@ .SS Program source \& s = pthread_getattr_np(pthread_self(), &gattr); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_getattr_np"); + errx(EXIT_FAILURE, "pthread_getattr_np: %s", strerror(s)); \& printf("Thread attributes:\[rs]n"); display_pthread_attr(&gattr, "\[rs]t");@@ -255,37 +256,37 @@ .SS Program source \& s = pthread_attr_init(&attr); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_init"); + errx(EXIT_FAILURE, "pthread_attr_init: %s", strerror(s)); \& s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_setdetachstate"); + errx(EXIT_FAILURE, "pthread_attr_setdetachstate: %s", strerror(s)); \& s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_setinheritsched"); + errx(EXIT_FAILURE, "pthread_attr_setinheritsched: %s", strerror(s)); \& stack_size = strtoul(argv[1], NULL, 0); \& s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size); if (s != 0) - errc(EXIT_FAILURE, s, "posix_memalign"); + errx(EXIT_FAILURE, "posix_memalign: %s", strerror(s)); \& printf("posix_memalign() allocated at %p\[rs]n", sp); \& s = pthread_attr_setstack(&attr, sp, stack_size); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_setstack"); + errx(EXIT_FAILURE, "pthread_attr_setstack: %s", strerror(s)); } \& s = pthread_create(&thr, attrp, &thread_start, NULL); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_create"); + errx(EXIT_FAILURE, "pthread_create: %s", strerror(s)); \& if (attrp != NULL) { s = pthread_attr_destroy(attrp); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_destroy"); + errx(EXIT_FAILURE, "pthread_attr_destroy: %s", strerror(s)); } \& pause(); /* Terminates when other thread calls exit() */Signature · 1 line
2.50.1
Signature · 1 line
<https://www.alejandro-colomar.es/>
Quoted 1 line · * Alejandro Colomar ([email protected]) wrote:
Hi Dave,
Hi Alex,
Quoted 11 lines · On Wed, Aug 20, 2025 at 02:27:25AM +0100, [email protected] wrote:
Quoted 5 lines
From: "Dr. David Alan Gilbert" <[email protected]>
The pthread_attr_init.3 example uses 'errc' to exit on an error printing the error code. However, 'errc' is a BSDism that Linux doesn't (and has never?) have.
Libbsd provides a compatbility layer to provide errc(3bsd) on Linux (and many POSIX systems). libbsd is available in many distros (in Debian, you should install libbsd-dev). BTW, it would be nice if glibc decided to pick this API eventually.
To use that we'd still need to change to:
#include <bsd/err.h>
and tell people to link with -lbsd
TBH using BSD only things in a Linux manpage with a _GNU_SOURCE define seems odd. The err()/errc()/errx() family of calls seems to be pretty obscure, most [linux] people I checked with had never used them. But at least err/errx are in normal glibc.
Dave
Quoted 140 lines
Have a lovely day!
AlexQuoted 133 lines
Replace it by 'errx' with a
strerror()call.Signed-off-by Dr. David Alan Gilbert <[email protected]>--- man/man3/pthread_attr_init.3 | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)man/man3/pthread_attr_init.3 +16
-15@@ -153,6 +153,7 @@ .SS Program source #include <pthread.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> \& static void@@ -165,7 +166,7 @@ .SS Program source \& s = pthread_attr_getdetachstate(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate"); + errx(EXIT_FAILURE, "pthread_attr_getdetachstate: %s", strerror(s)); printf("%sDetach state = %s\[rs]n", prefix, (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" : (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :@@ -173,7 +174,7 @@ .SS Program source \& s = pthread_attr_getscope(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getscope"); + errx(EXIT_FAILURE, "pthread_attr_getscope: %s", strerror(s)); printf("%sScope = %s\[rs]n", prefix, (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" : (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :@@ -181,7 +182,7 @@ .SS Program source \& s = pthread_attr_getinheritsched(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched"); + errx(EXIT_FAILURE, "pthread_attr_getinheritsched: %s", strerror(s)); printf("%sInherit scheduler = %s\[rs]n", prefix, (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" : (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :@@ -189,7 +190,7 @@ .SS Program source \& s = pthread_attr_getschedpolicy(attr, &i); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy"); + errx(EXIT_FAILURE, "pthread_attr_getschedpolicy: %s", strerror(s)); printf("%sScheduling policy = %s\[rs]n", prefix, (i == SCHED_OTHER) ? "SCHED_OTHER" : (i == SCHED_FIFO) ? "SCHED_FIFO" :@@ -198,17 +199,17 @@ .SS Program source \& s = pthread_attr_getschedparam(attr, &sp); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getschedparam"); + errx(EXIT_FAILURE, "pthread_attr_getschedparam: %s", strerror(s)); printf("%sScheduling priority = %d\[rs]n", prefix, sp.sched_priority); \& s = pthread_attr_getguardsize(attr, &v); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getguardsize"); + errx(EXIT_FAILURE, "pthread_attr_getguardsize: %s", strerror(s)); printf("%sGuard size = %zu bytes\[rs]n", prefix, v); \& s = pthread_attr_getstack(attr, &stkaddr, &v); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_getstack"); + errx(EXIT_FAILURE, "pthread_attr_getstack: %s", strerror(s)); printf("%sStack address = %p\[rs]n", prefix, stkaddr); printf("%sStack size = %#zx bytes\[rs]n", prefix, v); }@@ -225,7 +226,7 @@ .SS Program source \& s = pthread_getattr_np(pthread_self(), &gattr); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_getattr_np"); + errx(EXIT_FAILURE, "pthread_getattr_np: %s", strerror(s)); \& printf("Thread attributes:\[rs]n"); display_pthread_attr(&gattr, "\[rs]t");@@ -255,37 +256,37 @@ .SS Program source \& s = pthread_attr_init(&attr); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_init"); + errx(EXIT_FAILURE, "pthread_attr_init: %s", strerror(s)); \& s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_setdetachstate"); + errx(EXIT_FAILURE, "pthread_attr_setdetachstate: %s", strerror(s)); \& s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_setinheritsched"); + errx(EXIT_FAILURE, "pthread_attr_setinheritsched: %s", strerror(s)); \& stack_size = strtoul(argv[1], NULL, 0); \& s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size); if (s != 0) - errc(EXIT_FAILURE, s, "posix_memalign"); + errx(EXIT_FAILURE, "posix_memalign: %s", strerror(s)); \& printf("posix_memalign() allocated at %p\[rs]n", sp); \& s = pthread_attr_setstack(&attr, sp, stack_size); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_setstack"); + errx(EXIT_FAILURE, "pthread_attr_setstack: %s", strerror(s)); } \& s = pthread_create(&thr, attrp, &thread_start, NULL); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_create"); + errx(EXIT_FAILURE, "pthread_create: %s", strerror(s)); \& if (attrp != NULL) { s = pthread_attr_destroy(attrp); if (s != 0) - errc(EXIT_FAILURE, s, "pthread_attr_destroy"); + errx(EXIT_FAILURE, "pthread_attr_destroy: %s", strerror(s)); } \& pause(); /* Terminates when other thread calls exit() */Signature · 1 line
2.50.1Signature · 1 line
<https://www.alejandro-colomar.es/>
Signature · 4 lines
-----Open up your eyes, open up your mind, open up your code ------- / Dr. David Alan Gilbert | Running GNU/Linux | Happy \ \ dave @ treblig.org | | In Hex / \ _________________________|_____ http://www.treblig.org |_______/
Alejandro Colomar
maintainerQuoted 4 lines · Dr. David Alan Gilbert
Quoted 1 line · * Alejandro Colomar ([email protected]) wrote:
Hi Dave,
Hi Alex,
Hi Dave,
Quoted 7 lines
Quoted 6 lines · On Wed, Aug 20, 2025 at 02:27:25AM +0100, [email protected] wrote:
Quoted 5 lines
From: "Dr. David Alan Gilbert" <[email protected]>
The pthread_attr_init.3 example uses 'errc' to exit on an error printing the error code. However, 'errc' is a BSDism that Linux doesn't (and has never?) have.
Quoted 7 lines
Quoted 4 lines
Libbsd provides a compatbility layer to provide errc(3bsd) on Linux (and many POSIX systems). libbsd is available in many distros (in Debian, you should install libbsd-dev). BTW, it would be nice if glibc decided to pick this API eventually.
To use that we'd still need to change to:
#include <bsd/err.h>
With pkgconf --cflags libbsd-overlay you get it as <err.h>.
alx@debian:~$ pkgconf --list-package-names | grep libbsd libbsd-overlay libbsd-ctor libbsd alx@debian:~$ pkgconf --cflags libbsd-overlay -isystem /usr/include/x86_64-linux-gnu/bsd -DLIBBSD_OVERLAY
It uses some #include_next magic for that.
We could include <bsd/err.h>, although then the examples would ironically not be portable to the BSDs. :-)
Quoted 1 line
and tell people to link with -lbsd
Yup, we could show the command for compiling the example, which needs pkgconf --cflags --libs libbsd-overlay.
Maybe... We could include <bsd/err.h>. BSD people will know to remove that, hopefully. And for the rest, compilation will be as simple as -lbsd, instead of the pkgconf(1) command.
Feel free to send patches in any of those directions for errc(3).
Quoted 3 lines
TBH using BSD only things in a Linux manpage with a
_GNU_SOURCEdefine seems odd. The err()/errc()/errx() family of calls seems to be pretty obscure, most [linux] people I checked with had never used them.
It's sad that these function aren't more known. They're quite handy.
Quoted 1 line
But at least err/errx are in normal glibc.
Yup, that's a good thing. I wish glibc added errc(3) too.
Cheers,
Alex
Signature · 1 line
<https://www.alejandro-colomar.es/>