diff options
author | Alex Colomar <alx.manpages@gmail.com> | 2022-09-13 00:56:22 +0200 |
---|---|---|
committer | Alex Colomar <alx.manpages@gmail.com> | 2022-09-13 01:22:36 +0200 |
commit | 5a5208c119653f436529bc42d1c492d3a5e51ea9 (patch) | |
tree | 62e31f4d6ae24b7cd0fe26dfff90292e430d45a1 | |
parent | 8d3cce0abc4c32d248f9b4c503c5ce4b2833b602 (diff) |
Various pages: EXAMPLES: Use <err.h> functions
When reporting errors, prefer using conventional <err.h>
functions, rather than home-made macros. Home-made macros are
necessary for portable programs, so in documentation about
portable functions, keep using the macros. However, in the
documentation for functions that are only available in GNU/Linux
and/or BSD systems, prefer <err.h>, which is available in both
systems.
Do the same in example programs documenting portable functions but
in which we already require _GNU_SOURCE for some other reason.
Signed-off-by: Alex Colomar <alx.manpages@gmail.com>
-rw-r--r-- | man2/clone.2 | 16 | ||||
-rw-r--r-- | man2/eventfd.2 | 12 | ||||
-rw-r--r-- | man2/futex.2 | 12 | ||||
-rw-r--r-- | man2/getdents.2 | 8 | ||||
-rw-r--r-- | man2/getrlimit.2 | 8 | ||||
-rw-r--r-- | man2/kcmp.2 | 12 | ||||
-rw-r--r-- | man2/memfd_create.2 | 18 | ||||
-rw-r--r-- | man2/mount_setattr.2 | 19 | ||||
-rw-r--r-- | man2/open_by_handle_at.2 | 24 | ||||
-rw-r--r-- | man2/pivot_root.2 | 22 | ||||
-rw-r--r-- | man2/sched_setaffinity.2 | 10 | ||||
-rw-r--r-- | man2/seccomp_unotify.2 | 32 | ||||
-rw-r--r-- | man2/setns.2 | 10 | ||||
-rw-r--r-- | man2/signalfd.2 | 10 | ||||
-rw-r--r-- | man2/spu_run.2 | 10 | ||||
-rw-r--r-- | man2/timerfd_create.2 | 16 | ||||
-rw-r--r-- | man2/unshare.2 | 11 | ||||
-rw-r--r-- | man2/userfaultfd.2 | 23 | ||||
-rw-r--r-- | man3/fmemopen.3 | 10 | ||||
-rw-r--r-- | man3/malloc_info.3 | 12 | ||||
-rw-r--r-- | man3/pthread_attr_init.3 | 34 | ||||
-rw-r--r-- | man3/pthread_getattr_default_np.3 | 19 | ||||
-rw-r--r-- | man3/pthread_getattr_np.3 | 26 | ||||
-rw-r--r-- | man3/pthread_setaffinity_np.3 | 8 | ||||
-rw-r--r-- | man3/pthread_setname_np.3 | 15 | ||||
-rw-r--r-- | man7/pkeys.7 | 14 | ||||
-rw-r--r-- | man7/user_namespaces.7 | 15 |
27 files changed, 178 insertions, 248 deletions
diff --git a/man2/clone.2 b/man2/clone.2 index e3f8e6904..8b7938606 100644 --- a/man2/clone.2 +++ b/man2/clone.2 @@ -1827,6 +1827,7 @@ so we should include it for portability. .\" SRC BEGIN (clone.c) .EX #define _GNU_SOURCE +#include <err.h> #include <sched.h> #include <signal.h> #include <stdint.h> @@ -1838,9 +1839,6 @@ so we should include it for portability. #include <sys/wait.h> #include <unistd.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - static int /* Start function for cloned child */ childFunc(void *arg) { @@ -1849,12 +1847,12 @@ childFunc(void *arg) /* Change hostname in UTS namespace of child. */ if (sethostname(arg, strlen(arg)) == \-1) - errExit("sethostname"); + err(EXIT_FAILURE, "sethostname"); /* Retrieve and display hostname. */ if (uname(&uts) == \-1) - errExit("uname"); + err(EXIT_FAILURE, "uname"); printf("uts.nodename in child: %s\en", uts.nodename); /* Keep the namespace open for a while, by sleeping. @@ -1886,7 +1884,7 @@ main(int argc, char *argv[]) stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, \-1, 0); if (stack == MAP_FAILED) - errExit("mmap"); + err(EXIT_FAILURE, "mmap"); stackTop = stack + STACK_SIZE; /* Assume stack grows downward */ @@ -1895,7 +1893,7 @@ main(int argc, char *argv[]) pid = clone(childFunc, stackTop, CLONE_NEWUTS | SIGCHLD, argv[1]); if (pid == \-1) - errExit("clone"); + err(EXIT_FAILURE, "clone"); printf("clone() returned %jd\en", (intmax_t) pid); /* Parent falls through to here */ @@ -1906,11 +1904,11 @@ main(int argc, char *argv[]) different from hostname in child\(aqs UTS namespace. */ if (uname(&uts) == \-1) - errExit("uname"); + err(EXIT_FAILURE, "uname"); printf("uts.nodename in parent: %s\en", uts.nodename); if (waitpid(pid, NULL, 0) == \-1) /* Wait for child */ - errExit("waitpid"); + err(EXIT_FAILURE, "waitpid"); printf("child has terminated\en"); exit(EXIT_SUCCESS); diff --git a/man2/eventfd.2 b/man2/eventfd.2 index ad388746a..71517ad3a 100644 --- a/man2/eventfd.2 +++ b/man2/eventfd.2 @@ -380,15 +380,13 @@ Parent read 28 (0x1c) from efd \& .\" SRC BEGIN (eventfd.c) .EX +#include <err.h> #include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <sys/eventfd.h> #include <unistd.h> -#define handle_error(msg) \e - do { perror(msg); exit(EXIT_FAILURE); } while (0) - int main(int argc, char *argv[]) { @@ -403,7 +401,7 @@ main(int argc, char *argv[]) efd = eventfd(0, 0); if (efd == \-1) - handle_error("eventfd"); + err(EXIT_FAILURE, "eventfd"); switch (fork()) { case 0: @@ -413,7 +411,7 @@ main(int argc, char *argv[]) /* strtoull() allows various bases */ s = write(efd, &u, sizeof(uint64_t)); if (s != sizeof(uint64_t)) - handle_error("write"); + err(EXIT_FAILURE, "write"); } printf("Child completed write loop\en"); @@ -425,12 +423,12 @@ main(int argc, char *argv[]) printf("Parent about to read\en"); s = read(efd, &u, sizeof(uint64_t)); if (s != sizeof(uint64_t)) - handle_error("read"); + err(EXIT_FAILURE, "read"); printf("Parent read %"PRIu64" (%#"PRIx64") from efd\en", u, u); exit(EXIT_SUCCESS); case \-1: - handle_error("fork"); + err(EXIT_FAILURE, "fork"); } } .EE diff --git a/man2/futex.2 b/man2/futex.2 index ff75fce5c..1332ae852 100644 --- a/man2/futex.2 +++ b/man2/futex.2 @@ -1805,6 +1805,7 @@ Child (18535) 4 writing messages. */ #define _GNU_SOURCE +#include <err.h> #include <errno.h> #include <linux/futex.h> #include <stdatomic.h> @@ -1817,9 +1818,6 @@ Child (18535) 4 #include <sys/wait.h> #include <unistd.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - static uint32_t *futex1, *futex2, *iaddr; static int @@ -1857,7 +1855,7 @@ fwait(uint32_t *futexp) s = futex(futexp, FUTEX_WAIT, 0, NULL, NULL, 0); if (s == \-1 && errno != EAGAIN) - errExit("futex\-FUTEX_WAIT"); + err(EXIT_FAILURE, "futex\-FUTEX_WAIT"); } } @@ -1877,7 +1875,7 @@ fpost(uint32_t *futexp) if (atomic_compare_exchange_strong(futexp, &zero, 1)) { s = futex(futexp, FUTEX_WAKE, 1, NULL, NULL, 0); if (s == \-1) - errExit("futex\-FUTEX_WAKE"); + err(EXIT_FAILURE, "futex\-FUTEX_WAKE"); } } @@ -1899,7 +1897,7 @@ main(int argc, char *argv[]) iaddr = mmap(NULL, sizeof(*iaddr) * 2, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, \-1, 0); if (iaddr == MAP_FAILED) - errExit("mmap"); + err(EXIT_FAILURE, "mmap"); futex1 = &iaddr[0]; futex2 = &iaddr[1]; @@ -1912,7 +1910,7 @@ main(int argc, char *argv[]) childPid = fork(); if (childPid == \-1) - errExit("fork"); + err(EXIT_FAILURE, "fork"); if (childPid == 0) { /* Child */ for (int j = 0; j < nloops; j++) { diff --git a/man2/getdents.2 b/man2/getdents.2 index ae815ae15..f66238eb8 100644 --- a/man2/getdents.2 +++ b/man2/getdents.2 @@ -251,6 +251,7 @@ inode# file type d_reclen d_off d_name .\" SRC BEGIN (getdents.c) .EX #define _GNU_SOURCE +#include <err.h> #include <dirent.h> /* Defines DT_* constants */ #include <fcntl.h> #include <stdint.h> @@ -259,9 +260,6 @@ inode# file type d_reclen d_off d_name #include <sys/syscall.h> #include <unistd.h> -#define handle_error(msg) \e - do { perror(msg); exit(EXIT_FAILURE); } while (0) - struct linux_dirent { unsigned long d_ino; off_t d_off; @@ -282,12 +280,12 @@ main(int argc, char *argv[]) fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY); if (fd == \-1) - handle_error("open"); + err(EXIT_FAILURE, "open"); for (;;) { nread = syscall(SYS_getdents, fd, buf, BUF_SIZE); if (nread == \-1) - handle_error("getdents"); + err(EXIT_FAILURE, "getdents"); if (nread == 0) break; diff --git a/man2/getrlimit.2 b/man2/getrlimit.2 index 33175ce61..c1f3a0727 100644 --- a/man2/getrlimit.2 +++ b/man2/getrlimit.2 @@ -777,15 +777,13 @@ The program below demonstrates the use of .EX #define _GNU_SOURCE #define _FILE_OFFSET_BITS 64 +#include <err.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <sys/resource.h> #include <time.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - int main(int argc, char *argv[]) { @@ -812,14 +810,14 @@ main(int argc, char *argv[]) previous limit */ if (prlimit(pid, RLIMIT_CPU, newp, &old) == \-1) - errExit("prlimit\-1"); + err(EXIT_FAILURE, "prlimit\-1"); printf("Previous limits: soft=%jd; hard=%jd\en", (intmax_t) old.rlim_cur, (intmax_t) old.rlim_max); /* Retrieve and display new CPU time limit */ if (prlimit(pid, RLIMIT_CPU, NULL, &old) == \-1) - errExit("prlimit\-2"); + err(EXIT_FAILURE, "prlimit\-2"); printf("New limits: soft=%jd; hard=%jd\en", (intmax_t) old.rlim_cur, (intmax_t) old.rlim_max); diff --git a/man2/kcmp.2 b/man2/kcmp.2 index 886d5d66c..0c5b891f5 100644 --- a/man2/kcmp.2 +++ b/man2/kcmp.2 @@ -339,6 +339,7 @@ Child duplicated FD 3 to create FD 5 .\" SRC BEGIN (kcmp.c) .EX #define _GNU_SOURCE +#include <err.h> #include <fcntl.h> #include <linux/kcmp.h> #include <stdint.h> @@ -348,9 +349,6 @@ Child duplicated FD 3 to create FD 5 #include <sys/wait.h> #include <unistd.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - static int kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) @@ -376,14 +374,14 @@ main(void) fd1 = open(pathname, O_CREAT | O_RDWR, 0600); if (fd1 == \-1) - errExit("open"); + err(EXIT_FAILURE, "open"); printf("Parent PID is %jd\en", (intmax_t) getpid()); printf("Parent opened file on FD %d\en\en", fd1); switch (fork()) { case \-1: - errExit("fork"); + err(EXIT_FAILURE, "fork"); case 0: printf("PID of child of fork() is %jd\en", (intmax_t) getpid()); @@ -393,7 +391,7 @@ main(void) fd2 = open(pathname, O_CREAT | O_RDWR, 0600); if (fd2 == \-1) - errExit("open"); + err(EXIT_FAILURE, "open"); printf("Child opened file on FD %d\en", fd2); test_kcmp("Compare FDs from distinct open()s in same process:", @@ -401,7 +399,7 @@ main(void) fd3 = dup(fd1); if (fd3 == \-1) - errExit("dup"); + err(EXIT_FAILURE, "dup"); printf("Child duplicated FD %d to create FD %d\en", fd1, fd3); test_kcmp("Compare duplicated FDs in same process:", diff --git a/man2/memfd_create.2 b/man2/memfd_create.2 index 26129c18c..636fc1ffa 100644 --- a/man2/memfd_create.2 +++ b/man2/memfd_create.2 @@ -410,6 +410,7 @@ Existing seals: WRITE SHRINK .\" SRC BEGIN (t_memfd_create.c) .EX #define _GNU_SOURCE +#include <err.h> #include <fcntl.h> #include <stdint.h> #include <stdio.h> @@ -418,9 +419,6 @@ Existing seals: WRITE SHRINK #include <sys/mman.h> #include <unistd.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - int main(int argc, char *argv[]) { @@ -450,12 +448,12 @@ main(int argc, char *argv[]) fd = memfd_create(name, MFD_ALLOW_SEALING); if (fd == \-1) - errExit("memfd_create"); + err(EXIT_FAILURE, "memfd_create"); /* Size the file as specified on the command line. */ if (ftruncate(fd, len) == \-1) - errExit("truncate"); + err(EXIT_FAILURE, "truncate"); printf("PID: %jd; fd: %d; /proc/%jd/fd/%d\en", (intmax_t) getpid(), fd, (intmax_t) getpid(), fd); @@ -481,7 +479,7 @@ main(int argc, char *argv[]) seals |= F_SEAL_SEAL; if (fcntl(fd, F_ADD_SEALS, seals) == \-1) - errExit("fcntl"); + err(EXIT_FAILURE, "fcntl"); } /* Keep running, so that the file created by memfd_create() @@ -498,13 +496,11 @@ main(int argc, char *argv[]) .\" SRC BEGIN (t_get_seals.c) .EX #define _GNU_SOURCE +#include <err.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - int main(int argc, char *argv[]) { @@ -518,11 +514,11 @@ main(int argc, char *argv[]) fd = open(argv[1], O_RDWR); if (fd == \-1) - errExit("open"); + err(EXIT_FAILURE, "open"); seals = fcntl(fd, F_GET_SEALS); if (seals == \-1) - errExit("fcntl"); + err(EXIT_FAILURE, "fcntl"); printf("Existing seals:"); if (seals & F_SEAL_SEAL) diff --git a/man2/mount_setattr.2 b/man2/mount_setattr.2 index 160fd2526..e3287a88f 100644 --- a/man2/mount_setattr.2 +++ b/man2/mount_setattr.2 @@ -904,6 +904,7 @@ with a structure which has every byte nonzero * and set various properties on it. */ #define _GNU_SOURCE +#include <err.h> #include <errno.h> #include <fcntl.h> #include <getopt.h> @@ -949,12 +950,6 @@ static const struct option longopts[] = { { NULL, 0, NULL, 0 }, }; -#define exit_log(format, ...) do \e -{ \e - fprintf(stderr, format, ##__VA_ARGS__); \e - exit(EXIT_FAILURE); \e -} while (0) - int main(int argc, char *argv[]) { @@ -970,7 +965,7 @@ main(int argc, char *argv[]) case \(aqa\(aq: fd_userns = open(optarg, O_RDONLY | O_CLOEXEC); if (fd_userns == \-1) - exit_log("%m \- Failed top open %s\en", optarg); + err(EXIT_FAILURE, "open(%s)", optarg); break; case \(aqb\(aq: recursive = true; @@ -992,12 +987,12 @@ main(int argc, char *argv[]) attr\->attr_clr |= MOUNT_ATTR__ATIME; break; default: - exit_log("Invalid argument specified"); + errx(EXIT_FAILURE, "Invalid argument specified"); } } if ((argc \- optind) < 2) - exit_log("Missing source or target mount point\en"); + errx(EXIT_FAILURE, "Missing source or target mount point"); const char *source = argv[optind]; const char *target = argv[optind + 1]; @@ -1021,7 +1016,7 @@ main(int argc, char *argv[]) OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC | AT_EMPTY_PATH | (recursive ? AT_RECURSIVE : 0)); if (fd_tree == \-1) - exit_log("%m \- Failed to open %s\en", source); + err(EXIT_FAILURE, "open(%s)", source); if (fd_userns >= 0) { attr\->attr_set |= MOUNT_ATTR_IDMAP; @@ -1032,7 +1027,7 @@ main(int argc, char *argv[]) AT_EMPTY_PATH | (recursive ? AT_RECURSIVE : 0), attr, sizeof(struct mount_attr)); if (ret == \-1) - exit_log("%m \- Failed to change mount attributes\en"); + err(EXIT_FAILURE, "mount_setattr"); close(fd_userns); @@ -1042,7 +1037,7 @@ main(int argc, char *argv[]) ret = move_mount(fd_tree, "", \-1, target, MOVE_MOUNT_F_EMPTY_PATH); if (ret == \-1) - exit_log("%m \- Failed to attach mount to %s\en", target); + err(EXIT_FAILURE, "move_mount() to %s", target); close(fd_tree); diff --git a/man2/open_by_handle_at.2 b/man2/open_by_handle_at.2 index 351fda93f..de9c3b745 100644 --- a/man2/open_by_handle_at.2 +++ b/man2/open_by_handle_at.2 @@ -528,14 +528,12 @@ open_by_handle_at: Stale NFS file handle .\" SRC BEGIN (t_name_to_handle_at.c) .EX #define _GNU_SOURCE +#include <err.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - int main(int argc, char *argv[]) { @@ -555,7 +553,7 @@ main(int argc, char *argv[]) fhsize = sizeof(*fhp); fhp = malloc(fhsize); if (fhp == NULL) - errExit("malloc"); + err(EXIT_FAILURE, "malloc"); /* Make an initial call to name_to_handle_at() to discover the size required for file handle. */ @@ -576,12 +574,12 @@ main(int argc, char *argv[]) fhsize = sizeof(*fhp) + fhp\->handle_bytes; fhp = realloc(fhp, fhsize); /* Copies fhp\->handle_bytes */ if (fhp == NULL) - errExit("realloc"); + err(EXIT_FAILURE, "realloc"); /* Get file handle from pathname supplied on command line. */ if (name_to_handle_at(dirfd, pathname, fhp, &mount_id, flags) == \-1) - errExit("name_to_handle_at"); + err(EXIT_FAILURE, "name_to_handle_at"); /* Write mount ID, file handle size, and file handle to stdout, for later reuse by t_open_by_handle_at.c. */ @@ -601,6 +599,7 @@ main(int argc, char *argv[]) .\" SRC BEGIN (t_open_by_handle_at.c) .EX #define _GNU_SOURCE +#include <err.h> #include <fcntl.h> #include <limits.h> #include <stdio.h> @@ -608,9 +607,6 @@ main(int argc, char *argv[]) #include <string.h> #include <unistd.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - /* Scan /proc/self/mountinfo to find the line whose mount ID matches \(aqmount_id\(aq. (An easier way to do this is to install and use the \(aqlibmount\(aq library provided by the \(aqutil\-linux\(aq project.) @@ -629,7 +625,7 @@ open_mount_path_by_id(int mount_id) fp = fopen("/proc/self/mountinfo", "r"); if (fp == NULL) - errExit("fopen"); + err(EXIT_FAILURE, "fopen"); found = 0; linep = NULL; @@ -697,7 +693,7 @@ main(int argc, char *argv[]) fhp = malloc(sizeof(*fhp) + handle_bytes); if (fhp == NULL) - errExit("malloc"); + err(EXIT_FAILURE, "malloc"); fhp\->handle_bytes = handle_bytes; @@ -717,19 +713,19 @@ main(int argc, char *argv[]) mount_fd = open_mount_path_by_id(mount_id); if (mount_fd == \-1) - errExit("opening mount fd"); + err(EXIT_FAILURE, "opening mount fd"); /* Open file using handle and mount point. */ fd = open_by_handle_at(mount_fd, fhp, O_RDONLY); if (fd == \-1) - errExit("open_by_handle_at"); + err(EXIT_FAILURE, "open_by_handle_at"); /* Try reading a few bytes from the file. */ nread = read(fd, buf, sizeof(buf)); if (nread == \-1) - errExit("read"); + err(EXIT_FAILURE, "read"); printf("Read %zd bytes\en", nread); diff --git a/man2/pivot_root.2 b/man2/pivot_root.2 index ddd8ce547..914781810 100644 --- a/man2/pivot_root.2 +++ b/man2/pivot_root.2 @@ -304,6 +304,7 @@ hello world /* pivot_root_demo.c */ #define _GNU_SOURCE +#include <err.h> #include <limits.h> #include <sched.h> #include <signal.h> @@ -316,9 +317,6 @@ hello world #include <sys/wait.h> #include <unistd.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - static int pivot_root(const char *new_root, const char *put_old) { @@ -341,28 +339,28 @@ child(void *arg) events to the initial mount namespace. */ if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL) == \-1) - errExit("mount\-MS_PRIVATE"); + err(EXIT_FAILURE, "mount\-MS_PRIVATE"); /* Ensure that \(aqnew_root\(aq is a mount point. */ if (mount(new_root, new_root, NULL, MS_BIND, NULL) == \-1) - errExit("mount\-MS_BIND"); + err(EXIT_FAILURE, "mount\-MS_BIND"); /* Create directory to which old root will be pivoted. */ snprintf(path, sizeof(path), "%s/%s", new_root, put_old); if (mkdir(path, 0777) == \-1) - errExit("mkdir"); + err(EXIT_FAILURE, "mkdir"); /* And pivot the root filesystem. */ if (pivot_root(new_root, path) == \-1) - errExit("pivot_root"); + err(EXIT_FAILURE, "pivot_root"); /* Switch the current working directory to "/". */ if (chdir("/") == \-1) - errExit("chdir"); + err(EXIT_FAILURE, "chdir"); /* Unmount old root and remove mount point. */ @@ -374,7 +372,7 @@ child(void *arg) /* Execute the command specified in argv[1]... */ execv(args[1], &args[1]); - errExit("execv"); + err(EXIT_FAILURE, "execv"); } int @@ -385,16 +383,16 @@ main(int argc, char *argv[]) char *stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, \-1, 0); if (stack == MAP_FAILED) - errExit("mmap"); + err(EXIT_FAILURE, "mmap"); if (clone(child, stack + STACK_SIZE, CLONE_NEWNS | SIGCHLD, &argv[1]) == \-1) - errExit("clone"); + err(EXIT_FAILURE, "clone"); /* Parent falls through to here; wait for child. */ if (wait(NULL) == \-1) - errExit("wait"); + err(EXIT_FAILURE, "wait"); exit(EXIT_SUCCESS); } diff --git a/man2/sched_setaffinity.2 b/man2/sched_setaffinity.2 index cc7c4d2b5..553ccf36d 100644 --- a/man2/sched_setaffinity.2 +++ b/man2/sched_setaffinity.2 @@ -344,15 +344,13 @@ sys 12.07 .\" SRC BEGIN (sched_setaffinity.c) .EX #define _GNU_SOURCE +#include <err.h> #include <sched.h> #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <unistd.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - int main(int argc, char *argv[]) { @@ -374,13 +372,13 @@ main(int argc, char *argv[]) switch (fork()) { case \-1: /* Error */ - errExit("fork"); + err(EXIT_FAILURE, "fork"); case 0: /* Child */ CPU_SET(childCPU, &set); if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1) - errExit("sched_setaffinity"); + err(EXIT_FAILURE, "sched_setaffinity"); for (int j = 0; j < nloops; j++) getppid(); @@ -391,7 +389,7 @@ main(int argc, char *argv[]) CPU_SET(parentCPU, &set); if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1) - errExit("sched_setaffinity"); + err(EXIT_FAILURE, "sched_setaffinity"); for (int j = 0; j < nloops; j++) getppid(); diff --git a/man2/seccomp_unotify.2 b/man2/seccomp_unotify.2 index bfb968014..1379f11d9 100644 --- a/man2/seccomp_unotify.2 +++ b/man2/seccomp_unotify.2 @@ -1394,6 +1394,7 @@ T: terminating .\" SRC BEGIN (seccomp_unotify.c) .EX #define _GNU_SOURCE +#include <err.h> #include <errno.h> #include <fcntl.h> #include <limits.h> @@ -1418,9 +1419,6 @@ T: terminating #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - /* Send the file descriptor \(aqfd\(aq over the connected UNIX domain socket \(aqsockfd\(aq. Returns 0 on success, or \-1 on error. */ @@ -1608,7 +1606,7 @@ installNotifyFilter(void) int notifyFd = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_NEW_LISTENER, &prog); if (notifyFd == \-1) - errExit("seccomp\-install\-notify\-filter"); + err(EXIT_FAILURE, "seccomp\-install\-notify\-filter"); return notifyFd; } @@ -1619,9 +1617,9 @@ static void closeSocketPair(int sockPair[2]) { if (close(sockPair[0]) == \-1) - errExit("closeSocketPair\-close\-0"); + err(EXIT_FAILURE, "closeSocketPair\-close\-0"); if (close(sockPair[1]) == \-1) - errExit("closeSocketPair\-close\-1"); + err(EXIT_FAILURE, "closeSocketPair\-close\-1"); } /* Implementation of the target process; create a child process that: @@ -1644,7 +1642,7 @@ targetProcess(int sockPair[2], char *argv[]) targetPid = fork(); if (targetPid == \-1) - errExit("fork"); + err(EXIT_FAILURE, "fork"); if (targetPid > 0) /* In parent, return PID of child */ return targetPid; @@ -1656,7 +1654,7 @@ targetProcess(int sockPair[2], char *argv[]) /* Install seccomp filter(s) */ if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) - errExit("prctl"); + err(EXIT_FAILURE, "prctl"); notifyFd = installNotifyFilter(); @@ -1664,12 +1662,12 @@ targetProcess(int sockPair[2], char *argv[]) a UNIX domain socket */ if (sendfd(sockPair[0], notifyFd) == \-1) - errExit("sendfd"); + err(EXIT_FAILURE, "sendfd"); /* Notification and socket FDs are no longer needed in target */ if (close(notifyFd) == \-1) - errExit("close\-target\-notify\-fd"); + err(EXIT_FAILURE, "close\-target\-notify\-fd"); closeSocketPair(sockPair); @@ -1793,11 +1791,11 @@ allocSeccompNotifBuffers(struct seccomp_notif **req, buffers of those sizes. */ if (seccomp(SECCOMP_GET_NOTIF_SIZES, 0, sizes) == \-1) - errExit("seccomp\-SECCOMP_GET_NOTIF_SIZES"); + err(EXIT_FAILURE, "seccomp\-SECCOMP_GET_NOTIF_SIZES"); *req = malloc(sizes\->seccomp_notif); if (*req == NULL) - errExit("malloc\-seccomp_notif"); + err(EXIT_FAILURE, "malloc\-seccomp_notif"); /* When allocating the response buffer, we must allow for the fact that the user\-space binary may have been built with user\-space @@ -1814,7 +1812,7 @@ allocSeccompNotifBuffers(struct seccomp_notif **req, *resp = malloc(resp_size); if (resp == NULL) - errExit("malloc\-seccomp_notif_resp"); + err(EXIT_FAILURE, "malloc\-seccomp_notif_resp"); } @@ -1842,7 +1840,7 @@ handleNotifications(int notifyFd) if (ioctl(notifyFd, SECCOMP_IOCTL_NOTIF_RECV, req) == \-1) { if (errno == EINTR) continue; - errExit("\etS: ioctl\-SECCOMP_IOCTL_NOTIF_RECV"); + err(EXIT_FAILURE, "\etS: ioctl\-SECCOMP_IOCTL_NOTIF_RECV"); } printf("\etS: got notification (ID %#llx) for PID %d\en", @@ -1952,7 +1950,7 @@ supervisor(int sockPair[2]) notifyFd = recvfd(sockPair[1]); if (notifyFd == \-1) - errExit("recvfd"); + err(EXIT_FAILURE, "recvfd"); closeSocketPair(sockPair); /* We no longer need the socket pair */ @@ -1977,7 +1975,7 @@ main(int argc, char *argv[]) supervisor process. */ if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockPair) == \-1) - errExit("socketpair"); + err(EXIT_FAILURE, "socketpair"); /* Create a child process\-\-the "target"\-\-that installs seccomp filtering. The target process writes the seccomp notification @@ -1993,7 +1991,7 @@ main(int argc, char *argv[]) sa.sa_flags = 0; sigemptyset(&sa.sa_mask); if (sigaction(SIGCHLD, &sa, NULL) == \-1) - errExit("sigaction"); + err(EXIT_FAILURE, "sigaction"); supervisor(sockPair); diff --git a/man2/setns.2 b/man2/setns.2 index 3af43dd6a..9ff0372a2 100644 --- a/man2/setns.2 +++ b/man2/setns.2 @@ -380,15 +380,13 @@ bizarro .\" SRC BEGIN (setns.c) .EX #define _GNU_SOURCE +#include <err.h> #include <fcntl.h> #include <sched.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - int main(int argc, char *argv[]) { @@ -405,13 +403,13 @@ main(int argc, char *argv[]) fd = open(argv[1], O_RDONLY | O_CLOEXEC); if (fd == \-1) - errExit("open"); + err(EXIT_FAILURE, "open"); if (setns(fd, 0) == \-1) /* Join that namespace */ - errExit("setns"); + err(EXIT_FAILURE, "setns"); execvp(argv[2], &argv[2]); /* Execute a command in namespace */ - errExit("execvp"); + err(EXIT_FAILURE, "execvp"); } .EE .\" SRC END diff --git a/man2/signalfd.2 b/man2/signalfd.2 index bc7706fb8..c011404f9 100644 --- a/man2/signalfd.2 +++ b/man2/signalfd.2 @@ -462,15 +462,13 @@ $ \& .\" SRC BEGIN (signalfd.c) .EX +#include <err.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <sys/signalfd.h> #include <unistd.h> -#define handle_error(msg) \e - do { perror(msg); exit(EXIT_FAILURE); } while (0) - int main(void) { @@ -487,16 +485,16 @@ main(void) according to their default dispositions. */ if (sigprocmask(SIG_BLOCK, &mask, NULL) == \-1) - handle_error("sigprocmask"); + err(EXIT_FAILURE, "sigprocmask"); sfd = signalfd(\-1, &mask, 0); if (sfd == \-1) - handle_error("signalfd"); + err(EXIT_FAILURE, "signalfd"); for (;;) { s = read(sfd, &fdsi, sizeof(fdsi)); if (s != sizeof(fdsi)) - handle_error("read"); + err(EXIT_FAILURE, "read"); if (fdsi.ssi_signo == SIGINT) { printf("Got SIGINT\en"); diff --git a/man2/spu_run.2 b/man2/spu_run.2 index 7af2c0ff8..ec85e46c0 100644 --- a/man2/spu_run.2 +++ b/man2/spu_run.2 @@ -205,6 +205,7 @@ system call. .PP .\" SRC BEGIN (spu_run.c) .EX +#include <err.h> #include <fcntl.h> #include <stdint.h> #include <stdio.h> @@ -212,9 +213,6 @@ system call. #include <sys/types.h> #include <unistd.h> -#define handle_error(msg) \e - do { perror(msg); exit(EXIT_FAILURE); } while (0) - int main(void) { int context, fd, spu_status; @@ -222,7 +220,7 @@ int main(void) context = syscall(SYS_spu_create, "/spu/example\-context", 0, 0755); if (context == \-1) - handle_error("spu_create"); + err(EXIT_FAILURE, "spu_create"); /* * Write a \(aqstop 0x1234\(aq instruction to the SPU\(aqs @@ -232,7 +230,7 @@ int main(void) fd = open("/spu/example\-context/mem", O_RDWR); if (fd == \-1) - handle_error("open"); + err(EXIT_FAILURE, "open"); write(fd, &instruction, sizeof(instruction)); /* @@ -244,7 +242,7 @@ int main(void) spu_status = syscall(SYS_spu_run, context, &npc, NULL); if (spu_status == \-1) - handle_error("open"); + err(EXIT_FAILURE, "open"); /* * We should see a status code of 0x1234002: diff --git a/man2/timerfd_create.2 b/man2/timerfd_create.2 index 50c81c463..9850f14c9 100644 --- a/man2/timerfd_create.2 +++ b/man2/timerfd_create.2 @@ -595,6 +595,7 @@ a.out 3 1 100 .\"#define TFD_TIMER_ABSTIME (1 << 0) .\" .\"//////////////////////////////////////////////////////////// +#include <err.h> #include <inttypes.h> #include <stdio.h> #include <stdlib.h> @@ -602,9 +603,6 @@ a.out 3 1 100 #include <time.h> #include <unistd.h> -#define handle_error(msg) \e - do { perror(msg); exit(EXIT_FAILURE); } while (0) - static void print_elapsed_time(void) { @@ -616,11 +614,11 @@ print_elapsed_time(void) if (first_call) { first_call = 0; if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1) - handle_error("clock_gettime"); + err(EXIT_FAILURE, "clock_gettime"); } if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1) - handle_error("clock_gettime"); + err(EXIT_FAILURE, "clock_gettime"); secs = curr.tv_sec \- start.tv_sec; nsecs = curr.tv_nsec \- start.tv_nsec; @@ -647,7 +645,7 @@ main(int argc, char *argv[]) } if (clock_gettime(CLOCK_REALTIME, &now) == \-1) - handle_error("clock_gettime"); + err(EXIT_FAILURE, "clock_gettime"); /* Create a CLOCK_REALTIME absolute timer with initial expiration and interval as specified in command line. */ @@ -665,10 +663,10 @@ main(int argc, char *argv[]) fd = timerfd_create(CLOCK_REALTIME, 0); if (fd == \-1) - handle_error("timerfd_create"); + err(EXIT_FAILURE, "timerfd_create"); if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1) - handle_error("timerfd_settime"); + err(EXIT_FAILURE, "timerfd_settime"); print_elapsed_time(); printf("timer started\en"); @@ -676,7 +674,7 @@ main(int argc, char *argv[]) for (tot_exp = 0; tot_exp < max_exp;) { s = read(fd, &exp, sizeof(uint64_t)); if (s != sizeof(uint64_t)) - handle_error("read"); + err(EXIT_FAILURE, "read"); tot_exp += exp; print_elapsed_time(); diff --git a/man2/unshare.2 b/man2/unshare.2 index 79a960728..bb8797727 100644 --- a/man2/unshare.2 +++ b/man2/unshare.2 @@ -505,17 +505,12 @@ commands shows that the two shells are in different mount namespaces. namespaces and execute a command. */ #define _GNU_SOURCE +#include <err.h> #include <sched.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> -/* A simple error\-handling function: print an error message based - on the value in \(aqerrno\(aq and terminate the calling process. */ - -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - static void usage(char *pname) { @@ -557,10 +552,10 @@ main(int argc, char *argv[]) usage(argv[0]); if (unshare(flags) == \-1) - errExit("unshare"); + err(EXIT_FAILURE, "unshare"); execvp(argv[optind], &argv[optind]); - errExit("execvp"); + err(EXIT_FAILURE, "execvp"); } .EE .\" SRC END diff --git a/man2/userfaultfd.2 b/man2/userfaultfd.2 index a49bc984c..c91a83132 100644 --- a/man2/userfaultfd.2 +++ b/man2/userfaultfd.2 @@ -748,6 +748,7 @@ Read address 0x7fd30106ec0f in main(): C Licensed under the GNU General Public License version 2 or later. */ #define _GNU_SOURCE +#include <err.h> #include <errno.h> #include <fcntl.h> #include <inttypes.h> @@ -762,9 +763,6 @@ Read address 0x7fd30106ec0f in main(): C #include <sys/syscall.h> #include <unistd.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - static int page_size; static void * @@ -787,7 +785,7 @@ fault_handler_thread(void *arg) page = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, \-1, 0); if (page == MAP_FAILED) - errExit("mmap"); + err(EXIT_FAILURE, "mmap"); } /* Loop, handling incoming events on the userfaultfd @@ -801,7 +799,7 @@ fault_handler_thread(void *arg) pollfd.events = POLLIN; nready = poll(&pollfd, 1, \-1); if (nready == \-1) - errExit("poll"); + err(EXIT_FAILURE, "poll"); printf("\enfault_handler_thread():\en"); printf(" poll() returns: nready = %d; " @@ -818,7 +816,7 @@ fault_handler_thread(void *arg) } if (nread == \-1) - errExit("read"); + err(EXIT_FAILURE, "read"); /* We expect only one kind of event; verify that assumption. */ @@ -851,7 +849,7 @@ fault_handler_thread(void *arg) uffdio_copy.mode = 0; uffdio_copy.copy = 0; if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == \-1) - errExit("ioctl\-UFFDIO_COPY"); + err(EXIT_FAILURE, "ioctl\-UFFDIO_COPY"); printf(" (uffdio_copy.copy returned %"PRId64")\en", uffdio_copy.copy); @@ -883,12 +881,12 @@ main(int argc, char *argv[]) uffd = syscall(SYS_userfaultfd, O_CLOEXEC | O_NONBLOCK); if (uffd == \-1) - errExit("userfaultfd"); + err(EXIT_FAILURE, "userfaultfd"); uffdio_api.api = UFFD_API; uffdio_api.features = 0; if (ioctl(uffd, UFFDIO_API, &uffdio_api) == \-1) - errExit("ioctl\-UFFDIO_API"); + err(EXIT_FAILURE, "ioctl\-UFFDIO_API"); /* Create a private anonymous mapping. The memory will be demand\-zero paged\-\-that is, not yet allocated. When we @@ -898,7 +896,7 @@ main(int argc, char *argv[]) addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, \-1, 0); if (addr == MAP_FAILED) - errExit("mmap"); + err(EXIT_FAILURE, "mmap"); printf("Address returned by mmap() = %p\en", addr); @@ -910,14 +908,13 @@ main(int argc, char *argv[]) uffdio_register.range.len = len; uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING; if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == \-1) - errExit("ioctl\-UFFDIO_REGISTER"); + err(EXIT_FAILURE, "ioctl\-UFFDIO_REGISTER"); /* Create a thread that will process the userfaultfd events. */ s = pthread_create(&thr, NULL, fault_handler_thread, (void *) uffd); if (s != 0) { - errno = s; - errExit("pthread_create"); + errc(EXIT_FAILURE, s, "pthread_create"); } /* Main thread now touches memory in the mapping, touching diff --git a/man3/fmemopen.3 b/man3/fmemopen.3 index 4a3a16cf3..d275a92dd 100644 --- a/man3/fmemopen.3 +++ b/man3/fmemopen.3 @@ -303,13 +303,11 @@ size=11; ptr=1 529 1849 .\" SRC BEGIN (fmemopen.c) .EX #define _GNU_SOURCE +#include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#define handle_error(msg) \e - do { perror(msg); exit(EXIT_FAILURE); } while (0) - int main(int argc, char *argv[]) { @@ -325,11 +323,11 @@ main(int argc, char *argv[]) in = fmemopen(argv[1], strlen(argv[1]), "r"); if (in == NULL) - handle_error("fmemopen"); + err(EXIT_FAILURE, "fmemopen"); out = open_memstream(&ptr, &size); if (out == NULL) - handle_error("open_memstream"); + err(EXIT_FAILURE, "open_memstream"); for (;;) { s = fscanf(in, "%d", &v); @@ -338,7 +336,7 @@ main(int argc, char *argv[]) s = fprintf(out, "%d ", v * v); if (s == \-1) - handle_error("fprintf"); + err(EXIT_FAILURE, "fprintf"); } fclose(in); diff --git a/man3/malloc_info.3 b/man3/malloc_info.3 index 7c792de0e..dbeb0ee92 100644 --- a/man3/malloc_info.3 +++ b/man3/malloc_info.3 @@ -167,6 +167,7 @@ glibc 2.13 .SS Program source .\" SRC BEGIN (malloc_info.c) .EX +#include <err.h> #include <errno.h> #include <malloc.h> #include <pthread.h> @@ -176,9 +177,6 @@ glibc 2.13 static size_t blockSize; static int numThreads, numBlocks; -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - static void * thread_func(void *arg) { @@ -189,7 +187,7 @@ thread_func(void *arg) for (int j = 0; j < numBlocks; j++) if (malloc(blockSize * (2 + tn)) == NULL) - errExit("malloc\-thread"); + err(EXIT_FAILURE, "malloc\-thread"); sleep(100); /* Sleep until main thread terminates. */ return NULL; @@ -215,7 +213,7 @@ main(int argc, char *argv[]) thr = calloc(numThreads, sizeof(*thr)); if (thr == NULL) - errExit("calloc"); + err(EXIT_FAILURE, "calloc"); printf("============ Before allocating blocks ============\en"); malloc_info(0, stdout); @@ -226,7 +224,7 @@ main(int argc, char *argv[]) errno = pthread_create(&thr[tn], NULL, thread_func, (void *) tn); if (errno != 0) - errExit("pthread_create"); + err(EXIT_FAILURE, "pthread_create"); /* If we add a sleep interval after the start\-up of each thread, the threads likely won\(aqt contend for malloc @@ -241,7 +239,7 @@ main(int argc, char *argv[]) for (int j = 0; j < numBlocks; j++) if (malloc(blockSize) == NULL) - errExit("malloc"); + err(EXIT_FAILURE, "malloc"); sleep(2); /* Give all threads a chance to complete allocations. */ diff --git a/man3/pthread_attr_init.3 b/man3/pthread_attr_init.3 index aa3922682..30d5fd2e1 100644 --- a/man3/pthread_attr_init.3 +++ b/man3/pthread_attr_init.3 @@ -149,15 +149,13 @@ Thread attributes: .\" SRC BEGIN (pthread_attr_init.c) .EX #define _GNU_SOURCE /* To get pthread_getattr_np() declaration */ +#include <err.h> #include <errno.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#define handle_error_en(en, msg) \e - do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) - static void display_pthread_attr(pthread_attr_t *attr, char *prefix) { @@ -168,7 +166,7 @@ display_pthread_attr(pthread_attr_t *attr, char *prefix) s = pthread_attr_getdetachstate(attr, &i); if (s != 0) - handle_error_en(s, "pthread_attr_getdetachstate"); + errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate"); printf("%sDetach state = %s\en", prefix, (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" : (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" : @@ -176,7 +174,7 @@ display_pthread_attr(pthread_attr_t *attr, char *prefix) s = pthread_attr_getscope(attr, &i); if (s != 0) - handle_error_en(s, "pthread_attr_getscope"); + errc(EXIT_FAILURE, s, "pthread_attr_getscope"); printf("%sScope = %s\en", prefix, (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" : (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" : @@ -184,7 +182,7 @@ display_pthread_attr(pthread_attr_t *attr, char *prefix) s = pthread_attr_getinheritsched(attr, &i); if (s != 0) - handle_error_en(s, "pthread_attr_getinheritsched"); + errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched"); printf("%sInherit scheduler = %s\en", prefix, (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" : (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" : @@ -192,7 +190,7 @@ display_pthread_attr(pthread_attr_t *attr, char *prefix) s = pthread_attr_getschedpolicy(attr, &i); if (s != 0) - handle_error_en(s, "pthread_attr_getschedpolicy"); + errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy"); printf("%sScheduling policy = %s\en", prefix, (i == SCHED_OTHER) ? "SCHED_OTHER" : (i == SCHED_FIFO) ? "SCHED_FIFO" : @@ -201,17 +199,17 @@ display_pthread_attr(pthread_attr_t *attr, char *prefix) s = pthread_attr_getschedparam(attr, &sp); if (s != 0) - handle_error_en(s, "pthread_attr_getschedparam"); + errc(EXIT_FAILURE, s, "pthread_attr_getschedparam"); printf("%sScheduling priority = %d\en", prefix, sp.sched_priority); s = pthread_attr_getguardsize(attr, &v); if (s != 0) - handle_error_en(s, "pthread_attr_getguardsize"); + errc(EXIT_FAILURE, s, "pthread_attr_getguardsize"); printf("%sGuard size = %zu bytes\en", prefix, v); s = pthread_attr_getstack(attr, &stkaddr, &v); if (s != 0) - handle_error_en(s, "pthread_attr_getstack"); + errc(EXIT_FAILURE, s, "pthread_attr_getstack"); printf("%sStack address = %p\en", prefix, stkaddr); printf("%sStack size = %#zx bytes\en", prefix, v); } @@ -228,7 +226,7 @@ thread_start(void *arg) s = pthread_getattr_np(pthread_self(), &gattr); if (s != 0) - handle_error_en(s, "pthread_getattr_np"); + errc(EXIT_FAILURE, s, "pthread_getattr_np"); printf("Thread attributes:\en"); display_pthread_attr(&gattr, "\et"); @@ -258,37 +256,37 @@ main(int argc, char *argv[]) s = pthread_attr_init(&attr); if (s != 0) - handle_error_en(s, "pthread_attr_init"); + errc(EXIT_FAILURE, s, "pthread_attr_init"); s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (s != 0) - handle_error_en(s, "pthread_attr_setdetachstate"); + errc(EXIT_FAILURE, s, "pthread_attr_setdetachstate"); s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); if (s != 0) - handle_error_en(s, "pthread_attr_setinheritsched"); + errc(EXIT_FAILURE, s, "pthread_attr_setinheritsched"); stack_size = strtoul(argv[1], NULL, 0); s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size); if (s != 0) - handle_error_en(s, "posix_memalign"); + errc(EXIT_FAILURE, s, "posix_memalign"); printf("posix_memalign() allocated at %p\en", sp); s = pthread_attr_setstack(&attr, sp, stack_size); if (s != 0) - handle_error_en(s, "pthread_attr_setstack"); + errc(EXIT_FAILURE, s, "pthread_attr_setstack"); } s = pthread_create(&thr, attrp, &thread_start, NULL); if (s != 0) - handle_error_en(s, "pthread_create"); + errc(EXIT_FAILURE, s, "pthread_create"); if (attrp != NULL) { s = pthread_attr_destroy(attrp); if (s != 0) - handle_error_en(s, "pthread_attr_destroy"); + errc(EXIT_FAILURE, s, "pthread_attr_destroy"); } pause(); /* Terminates when other thread calls exit() */ diff --git a/man3/pthread_getattr_default_np.3 b/man3/pthread_getattr_default_np.3 index 0c46ee4d5..fb5db9cf4 100644 --- a/man3/pthread_getattr_default_np.3 +++ b/man3/pthread_getattr_default_np.3 @@ -103,15 +103,12 @@ Inherit scheduler: INHERIT .\" SRC BEGIN (pthread_getattr_default_np.c) .EX #define _GNU_SOURCE +#include <err.h> #include <errno.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> -#define errExitEN(en, msg) \e - do { errno = en; perror(msg); \e - exit(EXIT_FAILURE); } while (0) - static void display_pthread_attr(pthread_attr_t *attr) { @@ -125,17 +122,17 @@ display_pthread_attr(pthread_attr_t *attr) s = pthread_attr_getstacksize(attr, &stacksize); if (s != 0) - errExitEN(s, "pthread_attr_getstacksize"); + errc(EXIT_FAILURE, s, "pthread_attr_getstacksize"); printf("Stack size: %zd\en", stacksize); s = pthread_attr_getguardsize(attr, &guardsize); if (s != 0) - errExitEN(s, "pthread_attr_getguardsize"); + errc(EXIT_FAILURE, s, "pthread_attr_getguardsize"); printf("Guard size: %zd\en", guardsize); s = pthread_attr_getschedpolicy(attr, &policy); if (s != 0) - errExitEN(s, "pthread_attr_getschedpolicy"); + errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy"); printf("Scheduling policy: %s\en", (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : @@ -143,12 +140,12 @@ display_pthread_attr(pthread_attr_t *attr) s = pthread_attr_getschedparam(attr, &schedparam); if (s != 0) - errExitEN(s, "pthread_attr_getschedparam"); + errc(EXIT_FAILURE, s, "pthread_attr_getschedparam"); printf("Scheduling priority: %d\en", schedparam.sched_priority); s = pthread_attr_getdetachstate(attr, &detachstate); if (s != 0) - errExitEN(s, "pthread_attr_getdetachstate"); + errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate"); printf("Detach state: %s\en", (detachstate == PTHREAD_CREATE_DETACHED) ? "DETACHED" : (detachstate == PTHREAD_CREATE_JOINABLE) ? "JOINABLE" : @@ -156,7 +153,7 @@ display_pthread_attr(pthread_attr_t *attr) s = pthread_attr_getinheritsched(attr, &inheritsched); if (s != 0) - errExitEN(s, "pthread_attr_getinheritsched"); + errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched"); printf("Inherit scheduler: %s\en", (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" : (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" : @@ -171,7 +168,7 @@ main(void) s = pthread_getattr_default_np(&attr); if (s != 0) - errExitEN(s, "pthread_getattr_default_np"); + errc(EXIT_FAILURE, s, "pthread_getattr_default_np"); display_pthread_attr(&attr); diff --git a/man3/pthread_getattr_np.3 b/man3/pthread_getattr_np.3 index 468bcd381..33762f441 100644 --- a/man3/pthread_getattr_np.3 +++ b/man3/pthread_getattr_np.3 @@ -179,15 +179,13 @@ Attributes of created thread: .\" SRC BEGIN (pthread_getattr_np.c) .EX #define _GNU_SOURCE /* To get pthread_getattr_np() declaration */ +#include <err.h> #include <errno.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#define handle_error_en(en, msg) \e - do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) - static void display_stack_related_attributes(pthread_attr_t *attr, char *prefix) { @@ -197,12 +195,12 @@ display_stack_related_attributes(pthread_attr_t *attr, char *prefix) s = pthread_attr_getguardsize(attr, &guard_size); if (s != 0) - handle_error_en(s, "pthread_attr_getguardsize"); + errc(EXIT_FAILURE, s, "pthread_attr_getguardsize"); printf("%sGuard size = %zu bytes\en", prefix, guard_size); s = pthread_attr_getstack(attr, &stack_addr, &stack_size); if (s != 0) - handle_error_en(s, "pthread_attr_getstack"); + errc(EXIT_FAILURE, s, "pthread_attr_getstack"); printf("%sStack address = %p", prefix, stack_addr); if (stack_size > 0) printf(" (EOS = %p)", (char *) stack_addr + stack_size); @@ -219,13 +217,13 @@ display_thread_attributes(pthread_t thread, char *prefix) s = pthread_getattr_np(thread, &attr); if (s != 0) - handle_error_en(s, "pthread_getattr_np"); + errc(EXIT_FAILURE, s, "pthread_getattr_np"); display_stack_related_attributes(&attr, prefix); s = pthread_attr_destroy(&attr); if (s != 0) - handle_error_en(s, "pthread_attr_destroy"); + errc(EXIT_FAILURE, s, "pthread_attr_destroy"); } static void * /* Start function for thread we create */ @@ -281,31 +279,31 @@ get_thread_attributes_from_cl(int argc, char *argv[], s = pthread_attr_init(attrp); if (s != 0) - handle_error_en(s, "pthread_attr_init"); + errc(EXIT_FAILURE, s, "pthread_attr_init"); } if (stack_size >= 0) { if (!allocate_stack) { s = pthread_attr_setstacksize(attrp, stack_size); if (s != 0) - handle_error_en(s, "pthread_attr_setstacksize"); + errc(EXIT_FAILURE, s, "pthread_attr_setstacksize"); } else { s = posix_memalign(&stack_addr, sysconf(_SC_PAGESIZE), stack_size); if (s != 0) - handle_error_en(s, "posix_memalign"); + errc(EXIT_FAILURE, s, "posix_memalign"); printf("Allocated thread stack at %p\en\en", stack_addr); s = pthread_attr_setstack(attrp, stack_addr, stack_size); if (s != 0) - handle_error_en(s, "pthread_attr_setstacksize"); + errc(EXIT_FAILURE, s, "pthread_attr_setstacksize"); } } if (guard_size >= 0) { s = pthread_attr_setguardsize(attrp, guard_size); if (s != 0) - handle_error_en(s, "pthread_attr_setstacksize"); + errc(EXIT_FAILURE, s, "pthread_attr_setstacksize"); } return ret_attrp; @@ -330,12 +328,12 @@ main(int argc, char *argv[]) s = pthread_create(&thr, attrp, &thread_start, NULL); if (s != 0) - handle_error_en(s, "pthread_create"); + errc(EXIT_FAILURE, s, "pthread_create"); if (attrp != NULL) { s = pthread_attr_destroy(attrp); if (s != 0) - handle_error_en(s, "pthread_attr_destroy"); + errc(EXIT_FAILURE, 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 f6cbc9757..c86db65ba 100644 --- a/man3/pthread_setaffinity_np.3 +++ b/man3/pthread_setaffinity_np.3 @@ -158,14 +158,12 @@ to check the resulting CPU affinity mask of the thread. .\" SRC BEGIN (pthread_setaffinity_np.c) .EX #define _GNU_SOURCE +#include <err.h> #include <errno.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> -#define handle_error_en(en, msg) \e - do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) - int main(void) { @@ -183,13 +181,13 @@ main(void) s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset); if (s != 0) - handle_error_en(s, "pthread_setaffinity_np"); + errc(EXIT_FAILURE, s, "pthread_setaffinity_np"); /* Check the actual affinity mask assigned to the thread. */ s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset); if (s != 0) - handle_error_en(s, "pthread_getaffinity_np"); + errc(EXIT_FAILURE, s, "pthread_getaffinity_np"); printf("Set returned by pthread_getaffinity_np() contained:\en"); for (int j = 0; j < CPU_SETSIZE; j++) diff --git a/man3/pthread_setname_np.3 b/man3/pthread_setname_np.3 index 4fa0acdf4..d70cadbfb 100644 --- a/man3/pthread_setname_np.3 +++ b/man3/pthread_setname_np.3 @@ -141,6 +141,7 @@ THREADFOO .\" SRC BEGIN (pthread_setname_np.c) .EX #define _GNU_SOURCE +#include <err.h> #include <errno.h> #include <pthread.h> #include <stdio.h> @@ -150,10 +151,6 @@ THREADFOO #define NAMELEN 16 -#define errExitEN(en, msg) \e - do { errno = en; perror(msg); \e - exit(EXIT_FAILURE); } while (0) - static void * threadfunc(void *parm) { @@ -170,27 +167,27 @@ main(int argc, char *argv[]) rc = pthread_create(&thread, NULL, threadfunc, NULL); if (rc != 0) - errExitEN(rc, "pthread_create"); + errc(EXIT_FAILURE, rc, "pthread_create"); rc = pthread_getname_np(thread, thread_name, NAMELEN); if (rc != 0) - errExitEN(rc, "pthread_getname_np"); + errc(EXIT_FAILURE, rc, "pthread_getname_np"); printf("Created a thread. Default name is: %s\en", thread_name); rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO"); if (rc != 0) - errExitEN(rc, "pthread_setname_np"); + errc(EXIT_FAILURE, rc, "pthread_setname_np"); sleep(2); rc = pthread_getname_np(thread, thread_name, NAMELEN); if (rc != 0) - errExitEN(rc, "pthread_getname_np"); + errc(EXIT_FAILURE, rc, "pthread_getname_np"); printf("The thread name after setting it is %s.\en", thread_name); rc = pthread_join(thread, NULL); if (rc != 0) - errExitEN(rc, "pthread_join"); + errc(EXIT_FAILURE, rc, "pthread_join"); printf("Done\en"); exit(EXIT_SUCCESS); diff --git a/man7/pkeys.7 b/man7/pkeys.7 index 1d3006f74..4e4bef938 100644 --- a/man7/pkeys.7 +++ b/man7/pkeys.7 @@ -164,14 +164,12 @@ Segmentation fault (core dumped) \& .EX #define _GNU_SOURCE +#include <err.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - int main(void) { @@ -185,7 +183,7 @@ main(void) buffer = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, \-1, 0); if (buffer == MAP_FAILED) - errExit("mmap"); + err(EXIT_FAILURE, "mmap"); /* * Put some random data into the page (still OK to touch). @@ -198,7 +196,7 @@ main(void) */ pkey = pkey_alloc(0, 0); if (pkey == \-1) - errExit("pkey_alloc"); + err(EXIT_FAILURE, "pkey_alloc"); /* * Disable access to any memory with "pkey" set, @@ -206,7 +204,7 @@ main(void) */ status = pkey_set(pkey, PKEY_DISABLE_ACCESS); if (status) - errExit("pkey_set"); + err(EXIT_FAILURE, "pkey_set"); /* * Set the protection key on "buffer". @@ -216,7 +214,7 @@ main(void) status = pkey_mprotect(buffer, getpagesize(), PROT_READ | PROT_WRITE, pkey); if (status == \-1) - errExit("pkey_mprotect"); + err(EXIT_FAILURE, "pkey_mprotect"); printf("about to read buffer again...\en"); @@ -227,7 +225,7 @@ main(void) status = pkey_free(pkey); if (status == \-1) - errExit("pkey_free"); + err(EXIT_FAILURE, "pkey_free"); exit(EXIT_SUCCESS); } diff --git a/man7/user_namespaces.7 b/man7/user_namespaces.7 index e4e21cb75..74a066966 100644 --- a/man7/user_namespaces.7 +++ b/man7/user_namespaces.7 @@ -1143,6 +1143,7 @@ CapEff: 0000001fffffffff creating a user namespace. */ #define _GNU_SOURCE +#include <err.h> #include <sched.h> #include <unistd.h> #include <stdint.h> @@ -1155,12 +1156,6 @@ CapEff: 0000001fffffffff #include <limits.h> #include <errno.h> -/* A simple error\-handling function: print an error message based - on the value in \(aqerrno\(aq and terminate the calling process. */ - -#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e - } while (0) - struct child_args { char **argv; /* Command to be executed by child, with args */ int pipe_fd[2]; /* Pipe used to synchronize parent and child */ @@ -1315,7 +1310,7 @@ childFunc(void *arg) printf("About to exec %s\en", args\->argv[0]); execvp(args\->argv[0], args\->argv); - errExit("execvp"); + err(EXIT_FAILURE, "execvp"); } #define STACK_SIZE (1024 * 1024) @@ -1381,14 +1376,14 @@ main(int argc, char *argv[]) transformation of a process\(aqs capabilities during execve()). */ if (pipe(args.pipe_fd) == \-1) - errExit("pipe"); + err(EXIT_FAILURE, "pipe"); /* Create the child in new namespace(s). */ child_pid = clone(childFunc, child_stack + STACK_SIZE, flags | SIGCHLD, &args); if (child_pid == \-1) - errExit("clone"); + err(EXIT_FAILURE, "clone"); /* Parent falls through to here. */ @@ -1428,7 +1423,7 @@ main(int argc, char *argv[]) close(args.pipe_fd[1]); if (waitpid(child_pid, NULL, 0) == \-1) /* Wait for child */ - errExit("waitpid"); + err(EXIT_FAILURE, "waitpid"); if (verbose) printf("%s: terminating\en", argv[0]); |