From 940c8ce24412db4aa871c35905729c42ef2c9d31 Mon Sep 17 00:00:00 2001 From: Michael Kerrisk Date: Tue, 11 Nov 2008 08:35:25 -0500 Subject: [PATCH] getdents.2, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, pthread_setaffinity_np.3, pthread_setschedparam.3, pthread_tryjoin_np.3: Use consistent error-handling function names Many older pages use a handle_error() macro to do simple error handling from system and library function calls. Switch these pages to do similar. Signed-off-by: Michael Kerrisk --- man2/getdents.2 | 10 +++++----- man3/pthread_attr_init.3 | 36 +++++++++++++++++------------------ man3/pthread_create.3 | 25 ++++++++++++------------ man3/pthread_getattr_np.3 | 28 +++++++++++++-------------- man3/pthread_setaffinity_np.3 | 11 ++++++----- man3/pthread_setschedparam.3 | 32 ++++++++++++++----------------- man3/pthread_tryjoin_np.3 | 2 +- 7 files changed, 70 insertions(+), 74 deletions(-) diff --git a/man2/getdents.2 b/man2/getdents.2 index 0b309fc75..33148ea02 100644 --- a/man2/getdents.2 +++ b/man2/getdents.2 @@ -24,7 +24,7 @@ .\" Modified 22 July 1995 by Michael Chastain : .\" Derived from 'readdir.2'. .\" Modified Tue Oct 22 08:11:14 EDT 1996 by Eric S. Raymond -.TH GETDENTS 2 2008-10-28 "Linux" "Linux Programmer's Manual" +.TH GETDENTS 2 2008-11-11 "Linux" "Linux Programmer's Manual" .SH NAME getdents \- get directory entries .SH SYNOPSIS @@ -191,8 +191,8 @@ i-node# file type d_reclen d_off d_name #include #include -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\ - } while (0) +#define handle_error(msg) \\ + do { perror(msg); exit(EXIT_FAILURE); } while (0) struct linux_dirent { long d_ino; @@ -216,12 +216,12 @@ main(int argc, char *argv[]) fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY); if (fd == \-1) - errExit("open"); + handle_error("open"); for ( ; ; ) { nread = syscall(SYS_getdents, fd, buf, BUF_SIZE); if (nread == \-1) - errExit("getdents"); + handle_error("getdents"); if (nread == 0) break; diff --git a/man3/pthread_attr_init.3 b/man3/pthread_attr_init.3 index c08d75ec2..2b50211a2 100644 --- a/man3/pthread_attr_init.3 +++ b/man3/pthread_attr_init.3 @@ -21,7 +21,7 @@ .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" -.TH PTHREAD_ATTR_INIT 3 2008-10-24 "Linux" "Linux Programmer's Manual" +.TH PTHREAD_ATTR_INIT 3 2008-11-11 "Linux" "Linux Programmer's Manual" .SH NAME pthread_attr_init, pthread_attr_destroy \- initialize and destroy thread attributes object @@ -152,8 +152,8 @@ Thread attributes: #include #include -#define errExitEN(en, msg) { errno = en; perror(msg); \\ - exit(EXIT_FAILURE); } +#define handle_error_en(en, msg) \\ + do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) static void display_pthread_attr(pthread_attr_t *attr, char *prefix) @@ -165,7 +165,7 @@ display_pthread_attr(pthread_attr_t *attr, char *prefix) s = pthread_attr_getdetachstate(attr, &i); if (s != 0) - errExitEN(s, "pthread_attr_getdetachstate"); + handle_error_en(s, "pthread_attr_getdetachstate"); printf("%sDetach state = %s\\n", prefix, (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" : (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" : @@ -173,7 +173,7 @@ display_pthread_attr(pthread_attr_t *attr, char *prefix) s = pthread_attr_getscope(attr, &i); if (s != 0) - errExitEN(s, "pthread_attr_getscope"); + handle_error_en(s, "pthread_attr_getscope"); printf("%sScope = %s\\n", prefix, (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" : (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" : @@ -181,7 +181,7 @@ display_pthread_attr(pthread_attr_t *attr, char *prefix) s = pthread_attr_getinheritsched(attr, &i); if (s != 0) - errExitEN(s, "pthread_attr_getinheritsched"); + handle_error_en(s, "pthread_attr_getinheritsched"); printf("%sInherit scheduler = %s\\n", prefix, (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" : (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" : @@ -189,7 +189,7 @@ display_pthread_attr(pthread_attr_t *attr, char *prefix) s = pthread_attr_getschedpolicy(attr, &i); if (s != 0) - errExitEN(s, "pthread_attr_getschedpolicy"); + handle_error_en(s, "pthread_attr_getschedpolicy"); printf("%sScheduling policy = %s\\n", prefix, (i == SCHED_OTHER) ? "SCHED_OTHER" : (i == SCHED_FIFO) ? "SCHED_FIFO" : @@ -198,17 +198,17 @@ display_pthread_attr(pthread_attr_t *attr, char *prefix) s = pthread_attr_getschedparam(attr, &sp); if (s != 0) - errExitEN(s, "pthread_attr_getschedparam"); + handle_error_en(s, "pthread_attr_getschedparam"); printf("%sScheduling priority = %d\\n", prefix, sp.sched_priority); s = pthread_attr_getguardsize(attr, &v); if (s != 0) - errExitEN(s, "pthread_attr_getguardsize"); + handle_error_en(s, "pthread_attr_getguardsize"); printf("%sGuard size = %d bytes\\n", prefix, v); s = pthread_attr_getstack(attr, &stkaddr, &v); if (s != 0) - errExitEN(s, "pthread_attr_getstack"); + handle_error_en(s, "pthread_attr_getstack"); printf("%sStack address = %p\\n", prefix, stkaddr); printf("%sStack size = 0x%x bytes\\n", prefix, v); } @@ -225,7 +225,7 @@ thread_start(void *arg) s = pthread_getattr_np(pthread_self(), &gattr); if (s != 0) - errExitEN(s, "pthread_getattr_np"); + handle_error_en(s, "pthread_getattr_np"); printf("Thread attributes:\\n"); display_pthread_attr(&gattr, "\\t"); @@ -255,37 +255,37 @@ main(int argc, char *argv[]) s = pthread_attr_init(&attr); if (s != 0) - errExitEN(s, "pthread_attr_init"); + handle_error_en(s, "pthread_attr_init"); s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (s != 0) - errExitEN(s, "pthread_attr_setdetachstate"); + handle_error_en(s, "pthread_attr_setdetachstate"); s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); if (s != 0) - errExitEN(s, "pthread_attr_setinheritsched"); + handle_error_en(s, "pthread_attr_setinheritsched"); stack_size = strtoul(argv[1], NULL, 0); s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size); if (s != 0) - errExitEN(s, "posix_memalign"); + handle_error_en(s, "posix_memalign"); printf("posix_memalign() allocated at %p\\n", sp); s = pthread_attr_setstack(&attr, sp, stack_size); if (s != 0) - errExitEN(s, "pthread_attr_setstack"); + handle_error_en(s, "pthread_attr_setstack"); } s = pthread_create(&thr, attrp, &thread_start, NULL); if (s != 0) - errExitEN(s, "pthread_create"); + handle_error_en(s, "pthread_create"); if (attrp != NULL) { s = pthread_attr_destroy(attrp); if (s != 0) - errExitEN(s, "pthread_attr_destroy"); + handle_error_en(s, "pthread_attr_destroy"); } pause(); /* Terminates when other thread calls exit() */ diff --git a/man3/pthread_create.3 b/man3/pthread_create.3 index 738bd5562..83ad96a56 100644 --- a/man3/pthread_create.3 +++ b/man3/pthread_create.3 @@ -21,7 +21,7 @@ .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" -.TH PTHREAD_CREATE 3 2008-11-05 "Linux" "Linux Programmer's Manual" +.TH PTHREAD_CREATE 3 2008-11-11 "Linux" "Linux Programmer's Manual" .SH NAME pthread_create \- create a new thread .SH SYNOPSIS @@ -241,12 +241,11 @@ Joined with thread 3; returned value was SERVUS #include #include -/* Simple error handling functions */ +#define handle_error_en(en, msg) \\ + do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) -#define errExit(msg) { perror(msg); exit(EXIT_FAILURE); } - -#define errExitEN(en, msg) { errno = en; perror(msg); \\ - exit(EXIT_FAILURE); } +#define handle_error(msg) \\ + do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info { /* Used as argument to thread_start() */ pthread_t thread_id; /* ID returned by pthread_create */ @@ -268,7 +267,7 @@ thread_start(void *arg) uargv = strdup(tinfo\->argv_string); if (uargv == NULL) - errExit("strdup"); + handle_error("strdup"); for (p = uargv; *p != \(aq\\0\(aq; p++) *p = toupper(*p); @@ -307,19 +306,19 @@ main(int argc, char *argv[]) s = pthread_attr_init(&attr); if (s != 0) - errExitEN(s, "pthread_attr_init"); + handle_error_en(s, "pthread_attr_init"); if (stack_size > 0) { s = pthread_attr_setstacksize(&attr, stack_size); if (s != 0) - errExitEN(s, "pthread_attr_setstacksize"); + handle_error_en(s, "pthread_attr_setstacksize"); } /* Allocate memory for pthread_create() arguments */ tinfo = calloc(num_threads, sizeof(struct thread_info)); if (tinfo == NULL) - errExit("calloc"); + handle_error("calloc"); /* Create one thread for each command\-line argument */ @@ -333,7 +332,7 @@ main(int argc, char *argv[]) s = pthread_create(&tinfo[tnum].thread_id, &attr, &thread_start, &tinfo[tnum]); if (s != 0) - errExitEN(s, "pthread_create"); + handle_error_en(s, "pthread_create"); } /* Destroy the thread attributes object, since it is no @@ -341,14 +340,14 @@ main(int argc, char *argv[]) s = pthread_attr_destroy(&attr); if (s != 0) - errExitEN(s, "pthread_attr_destroy"); + handle_error_en(s, "pthread_attr_destroy"); /* Now join with each thread, and display its returned value */ for (tnum = 0; tnum < num_threads; tnum++) { s = pthread_join(tinfo[tnum].thread_id, &res); if (s != 0) - errExitEN(s, "pthread_join"); + handle_error_en(s, "pthread_join"); printf("Joined with thread %d; returned value was %s\\n", tinfo[tnum].thread_num, (char *) res); diff --git a/man3/pthread_getattr_np.3 b/man3/pthread_getattr_np.3 index 72524de1d..f57b30d5d 100644 --- a/man3/pthread_getattr_np.3 +++ b/man3/pthread_getattr_np.3 @@ -21,7 +21,7 @@ .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" -.TH PTHREAD_GETATTR_NP 3 2008-11-07 "Linux" "Linux Programmer's Manual" +.TH PTHREAD_GETATTR_NP 3 2008-11-11 "Linux" "Linux Programmer's Manual" .SH NAME pthread_getattr_np \- get attributes of created thread .SH SYNOPSIS @@ -183,8 +183,8 @@ Attributes of created thread: #include #include -#define errExitEN(en, msg) { errno = en; perror(msg); \\ - exit(EXIT_FAILURE); } +#define handle_error_en(en, msg) \\ + do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) static void display_stack_related_attributes(pthread_attr_t *attr, char *prefix) @@ -195,12 +195,12 @@ display_stack_related_attributes(pthread_attr_t *attr, char *prefix) s = pthread_attr_getguardsize(attr, &guard_size); if (s != 0) - errExitEN(s, "pthread_attr_getguardsize"); + handle_error_en(s, "pthread_attr_getguardsize"); printf("%sGuard size = %d bytes\\n", prefix, guard_size); s = pthread_attr_getstack(attr, &stack_addr, &stack_size); if (s != 0) - errExitEN(s, "pthread_attr_getstack"); + handle_error_en(s, "pthread_attr_getstack"); printf("%sStack address = %p", prefix, stack_addr); if (stack_size > 0) printf(" (EOS = %p)", (char *) stack_addr + stack_size); @@ -217,13 +217,13 @@ display_thread_attributes(pthread_t thread, char *prefix) s = pthread_getattr_np(thread, &attr); if (s != 0) - errExitEN(s, "pthread_getattr_np"); + handle_error_en(s, "pthread_getattr_np"); display_stack_related_attributes(&attr, prefix); s = pthread_attr_destroy(&attr); if (s != 0) - errExitEN(s, "pthread_attr_destroy"); + handle_error_en(s, "pthread_attr_destroy"); } static void * /* Start function for thread we create */ @@ -279,31 +279,31 @@ get_thread_attributes_from_cl(int argc, char *argv[], s = pthread_attr_init(attrp); if (s != 0) - errExitEN(s, "pthread_attr_init"); + handle_error_en(s, "pthread_attr_init"); } if (stack_size >= 0) { if (!allocate_stack) { s = pthread_attr_setstacksize(attrp, stack_size); if (s != 0) - errExitEN(s, "pthread_attr_setstacksize"); + handle_error_en(s, "pthread_attr_setstacksize"); } else { s = posix_memalign(&stack_addr, sysconf(_SC_PAGESIZE), stack_size); if (s != 0) - errExitEN(s, "posix_memalign"); + handle_error_en(s, "posix_memalign"); printf("Allocated thread stack at %p\\n\\n", stack_addr); s = pthread_attr_setstack(attrp, stack_addr, stack_size); if (s != 0) - errExitEN(s, "pthread_attr_setstacksize"); + handle_error_en(s, "pthread_attr_setstacksize"); } } if (guard_size >= 0) { s = pthread_attr_setguardsize(attrp, guard_size); if (s != 0) - errExitEN(s, "pthread_attr_setstacksize"); + handle_error_en(s, "pthread_attr_setstacksize"); } return ret_attrp; @@ -328,12 +328,12 @@ main(int argc, char *argv[]) s = pthread_create(&thr, attrp, &thread_start, NULL); if (s != 0) - errExitEN(s, "pthread_create"); + handle_error_en(s, "pthread_create"); if (attrp != NULL) { s = pthread_attr_destroy(attrp); if (s != 0) - errExitEN(s, "pthread_attr_destroy"); + handle_error_en(s, "pthread_attr_destroy"); } pause(); /* Terminates when other thread calls exit() */ diff --git a/man3/pthread_setaffinity_np.3 b/man3/pthread_setaffinity_np.3 index 4b4040bf9..e7baa922b 100644 --- a/man3/pthread_setaffinity_np.3 +++ b/man3/pthread_setaffinity_np.3 @@ -21,7 +21,7 @@ .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" -.TH PTHREAD_SETAFFINITY_NP 3 2008-11-07 "Linux" "Linux Programmer's Manual" +.TH PTHREAD_SETAFFINITY_NP 3 2008-11-11 "Linux" "Linux Programmer's Manual" .SH NAME pthread_setaffinity_np, pthread_getaffinity_np \- set/get CPU affinity of a thread @@ -144,8 +144,9 @@ to check the resulting CPU affinity mask of the thread. #include #include -#define errExitEN(en, msg) { errno = en; perror(msg); \\ - exit(EXIT_FAILURE); } +#define handle_error_en(en, msg) \\ + do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) + int main(int argc, char *argv[]) { @@ -163,13 +164,13 @@ main(int argc, char *argv[]) s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) - errExitEN(s, "pthread_setaffinity_np"); + handle_error_en(s, "pthread_setaffinity_np"); /* Check the actual affinity mask assigned to the thread */ s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) - errExitEN(s, "pthread_getaffinity_np"); + handle_error_en(s, "pthread_getaffinity_np"); printf("Set returned by pthread_getaffinity_np() contained:\\n"); for (j = 0; j < CPU_SETSIZE; j++) diff --git a/man3/pthread_setschedparam.3 b/man3/pthread_setschedparam.3 index e0d570a28..5ebb2fec2 100644 --- a/man3/pthread_setschedparam.3 +++ b/man3/pthread_setschedparam.3 @@ -21,7 +21,7 @@ .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" -.TH PTHREAD_SETSCHEDPARAM 3 2008-11-07 "Linux" "Linux Programmer's Manual" +.TH PTHREAD_SETSCHEDPARAM 3 2008-11-11 "Linux" "Linux Programmer's Manual" .SH NAME pthread_setschedparam, pthread_setschedparam \- set/get scheduling policy and parameters of a thread @@ -224,12 +224,8 @@ rather than the thread attributes object. #include #include -/* Simple error handling functions */ - -#define errExit(msg) { perror(msg); exit(EXIT_FAILURE); } - -#define errExitEN(en, msg) { errno = en; perror(msg); \\ - exit(EXIT_FAILURE); } +#define handle_error_en(en, msg) \\ + do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) static void usage(char *prog_name, char *msg) @@ -284,7 +280,7 @@ display_thread_sched_attr(char *msg) s = pthread_getschedparam(pthread_self(), &policy, ¶m); if (s != 0) - errExitEN(s, "pthread_getschedparam"); + handle_error_en(s, "pthread_getschedparam"); printf("%s\\n", msg); display_sched_attr(policy, ¶m); @@ -339,7 +335,7 @@ main(int argc, char *argv[]) s = pthread_setschedparam(pthread_self(), policy, ¶m); if (s != 0) - errExitEN(s, "pthread_setschedparam"); + handle_error_en(s, "pthread_setschedparam"); } display_thread_sched_attr("Scheduler settings of main thread"); @@ -352,7 +348,7 @@ main(int argc, char *argv[]) if (!use_null_attrib) { s = pthread_attr_init(&attr); if (s != 0) - errExitEN(s, "pthread_attr_init"); + handle_error_en(s, "pthread_attr_init"); attrp = &attr; } @@ -366,7 +362,7 @@ main(int argc, char *argv[]) s = pthread_attr_setinheritsched(&attr, inheritsched); if (s != 0) - errExitEN(s, "pthread_attr_setinheritsched"); + handle_error_en(s, "pthread_attr_setinheritsched"); } if (attr_sched_str != NULL) { @@ -377,10 +373,10 @@ main(int argc, char *argv[]) s = pthread_attr_setschedpolicy(&attr, policy); if (s != 0) - errExitEN(s, "pthread_attr_setschedpolicy"); + handle_error_en(s, "pthread_attr_setschedpolicy"); s = pthread_attr_setschedparam(&attr, ¶m); if (s != 0) - errExitEN(s, "pthread_attr_setschedparam"); + handle_error_en(s, "pthread_attr_setschedparam"); } /* If we initialized a thread attributes object, display @@ -389,10 +385,10 @@ main(int argc, char *argv[]) if (attrp != NULL) { s = pthread_attr_getschedparam(&attr, ¶m); if (s != 0) - errExitEN(s, "pthread_attr_getschedparam"); + handle_error_en(s, "pthread_attr_getschedparam"); s = pthread_attr_getschedpolicy(&attr, &policy); if (s != 0) - errExitEN(s, "pthread_attr_getschedpolicy"); + handle_error_en(s, "pthread_attr_getschedpolicy"); printf("Scheduler settings in \(aqattr\(aq\\n"); display_sched_attr(policy, ¶m); @@ -409,17 +405,17 @@ main(int argc, char *argv[]) s = pthread_create(&thread, attrp, &thread_start, NULL); if (s != 0) - errExitEN(s, "pthread_create"); + handle_error_en(s, "pthread_create"); /* Destroy unneeded thread attributes object */ s = pthread_attr_destroy(&attr); if (s != 0) - errExitEN(s, "pthread_attr_destroy"); + handle_error_en(s, "pthread_attr_destroy"); s = pthread_join(thread, NULL); if (s != 0) - errExitEN(s, "pthread_join"); + handle_error_en(s, "pthread_join"); exit(EXIT_SUCCESS); } /* main */ diff --git a/man3/pthread_tryjoin_np.3 b/man3/pthread_tryjoin_np.3 index 55fa90e9e..85a6f3460 100644 --- a/man3/pthread_tryjoin_np.3 +++ b/man3/pthread_tryjoin_np.3 @@ -21,7 +21,7 @@ .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" -.TH PTHREAD_TRYJOIN_NP 3 2008-11-07 "Linux" "Linux Programmer's Manual" +.TH PTHREAD_TRYJOIN_NP 3 2008-11-11 "Linux" "Linux Programmer's Manual" .SH NAME pthread_tryjoin_np, pthread_timedjoin_np \- try to join with a terminated thread