aboutsummaryrefslogtreecommitdiffstats
path: root/man/man2
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2025-08-19 17:48:50 +0200
committerAlejandro Colomar <alx@kernel.org>2025-08-20 18:14:03 +0200
commit0e7a39804a3c017a209117fc2243c6cbb543dede (patch)
tree91e0b287d8b826d668c3d8118347f07cc8b8964a /man/man2
parente2d3f14fe40ad90a1fedf0fcd27e6cc896c49a7a (diff)
downloadman-pages-0e7a39804a3c.tar.gz
man/: EXAMPLES: Use err(3) and errc(3bsd) instead of similar macros
These functions are quite portable. And if one doesn't have them for some reason (but libbsd has been ported to many systems), one can write them easily as macros, anyway. Signed-off-by: Alejandro Colomar <alx@kernel.org>
Diffstat (limited to 'man/man2')
-rw-r--r--man/man2/bind.216
-rw-r--r--man/man2/mmap.212
-rw-r--r--man/man2/mprotect.212
-rw-r--r--man/man2/poll.214
-rw-r--r--man/man2/shmop.222
-rw-r--r--man/man2/timer_create.220
6 files changed, 42 insertions, 54 deletions
diff --git a/man/man2/bind.2 b/man/man2/bind.2
index 5d44c152a6..309f6d9d7c 100644
--- a/man/man2/bind.2
+++ b/man/man2/bind.2
@@ -209,6 +209,7 @@ domain, and accept connections:
.P
.\" SRC BEGIN (bind.c)
.EX
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -219,9 +220,6 @@ domain, and accept connections:
#define MY_SOCK_PATH "/somepath"
#define LISTEN_BACKLOG 50
\&
-#define handle_error(msg) \[rs]
- do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
int
main(void)
{
@@ -231,7 +229,7 @@ main(void)
\&
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == \-1)
- handle_error("socket");
+ err(EXIT_FAILURE, "socket");
\&
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sun_family = AF_UNIX;
@@ -240,10 +238,10 @@ main(void)
\&
if (bind(sfd, (struct sockaddr *) &my_addr,
sizeof(my_addr)) == \-1)
- handle_error("bind");
+ err(EXIT_FAILURE, "bind");
\&
if (listen(sfd, LISTEN_BACKLOG) == \-1)
- handle_error("listen");
+ err(EXIT_FAILURE, "listen");
\&
/* Now we can accept incoming connections one
at a time using accept(2). */
@@ -252,15 +250,15 @@ main(void)
cfd = accept(sfd, (struct sockaddr *) &peer_addr,
&peer_addr_size);
if (cfd == \-1)
- handle_error("accept");
+ err(EXIT_FAILURE, "accept");
\&
/* Code to deal with incoming connection(s)... */
\&
if (close(sfd) == \-1)
- handle_error("close");
+ err(EXIT_FAILURE, "close");
\&
if (unlink(MY_SOCK_PATH) == \-1)
- handle_error("unlink");
+ err(EXIT_FAILURE, "unlink");
}
.EE
.\" SRC END
diff --git a/man/man2/mmap.2 b/man/man2/mmap.2
index 28ad07198a..23ffcd758c 100644
--- a/man/man2/mmap.2
+++ b/man/man2/mmap.2
@@ -948,6 +948,7 @@ to output the desired bytes.
.SS Program source
.\" SRC BEGIN (mmap.c)
.EX
+#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -956,9 +957,6 @@ to output the desired bytes.
#include <sys/types.h>
#include <unistd.h>
\&
-#define handle_error(msg) \[rs]
- do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
int
main(int argc, char *argv[])
{
@@ -976,10 +974,10 @@ main(int argc, char *argv[])
\&
fd = open(argv[1], O_RDONLY);
if (fd == \-1)
- handle_error("open");
+ err(EXIT_FAILURE, "open");
\&
if (fstat(fd, &sb) == \-1) /* To obtain file size */
- handle_error("fstat");
+ err(EXIT_FAILURE, "fstat");
\&
offset = atoi(argv[2]);
pa_offset = offset & \[ti](sysconf(_SC_PAGE_SIZE) \- 1);
@@ -1003,12 +1001,12 @@ main(int argc, char *argv[])
addr = mmap(NULL, length + offset \- pa_offset, PROT_READ,
MAP_PRIVATE, fd, pa_offset);
if (addr == MAP_FAILED)
- handle_error("mmap");
+ err(EXIT_FAILURE, "mmap");
\&
s = write(STDOUT_FILENO, addr + offset \- pa_offset, length);
if (s != length) {
if (s == \-1)
- handle_error("write");
+ err(EXIT_FAILURE, "write");
\&
fprintf(stderr, "partial write");
exit(EXIT_FAILURE);
diff --git a/man/man2/mprotect.2 b/man/man2/mprotect.2
index 773d93dc9c..269ae204d6 100644
--- a/man/man2/mprotect.2
+++ b/man/man2/mprotect.2
@@ -293,6 +293,7 @@ Got SIGSEGV at address: 0x804e000
\&
.\" SRC BEGIN (mprotect.c)
.EX
+#include <err.h>
#include <malloc.h>
#include <signal.h>
#include <stdio.h>
@@ -300,9 +301,6 @@ Got SIGSEGV at address: 0x804e000
#include <sys/mman.h>
#include <unistd.h>
\&
-#define handle_error(msg) \[rs]
- do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
static char *buffer;
\&
static void
@@ -328,24 +326,24 @@ main(void)
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
if (sigaction(SIGSEGV, &sa, NULL) == \-1)
- handle_error("sigaction");
+ err(EXIT_FAILURE, "sigaction");
\&
pagesize = sysconf(_SC_PAGE_SIZE);
if (pagesize == \-1)
- handle_error("sysconf");
+ err(EXIT_FAILURE, "sysconf");
\&
/* Allocate a buffer aligned on a page boundary;
initial protection is PROT_READ | PROT_WRITE. */
\&
buffer = memalign(pagesize, 4 * pagesize);
if (buffer == NULL)
- handle_error("memalign");
+ err(EXIT_FAILURE, "memalign");
\&
printf("Start of region: %p\[rs]n", buffer);
\&
if (mprotect(buffer + pagesize * 2, pagesize,
PROT_READ) == \-1)
- handle_error("mprotect");
+ err(EXIT_FAILURE, "mprotect");
\&
for (char *p = buffer ; ; )
*(p++) = \[aq]a\[aq];
diff --git a/man/man2/poll.2 b/man/man2/poll.2
index 85856bb34d..e3e5119d2e 100644
--- a/man/man2/poll.2
+++ b/man/man2/poll.2
@@ -555,6 +555,7 @@ at which point the file descriptor was closed and the program terminated.
\&
Licensed under GNU General Public License v2 or later.
*/
+#include <err.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
@@ -562,9 +563,6 @@ at which point the file descriptor was closed and the program terminated.
#include <sys/types.h>
#include <unistd.h>
\&
-#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \[rs]
- } while (0)
-\&
int
main(int argc, char *argv[])
{
@@ -582,14 +580,14 @@ main(int argc, char *argv[])
num_open_fds = nfds = argc \- 1;
pfds = calloc(nfds, sizeof(struct pollfd));
if (pfds == NULL)
- errExit("malloc");
+ err(EXIT_FAILURE, "malloc");
\&
/* Open each file on command line, and add it to \[aq]pfds\[aq] array. */
\&
for (nfds_t j = 0; j < nfds; j++) {
pfds[j].fd = open(argv[j + 1], O_RDONLY);
if (pfds[j].fd == \-1)
- errExit("open");
+ err(EXIT_FAILURE, "open");
\&
printf("Opened \[rs]"%s\[rs]" on fd %d\[rs]n", argv[j + 1], pfds[j].fd);
\&
@@ -603,7 +601,7 @@ main(int argc, char *argv[])
printf("About to poll()\[rs]n");
ready = poll(pfds, nfds, \-1);
if (ready == \-1)
- errExit("poll");
+ err(EXIT_FAILURE, "poll");
\&
printf("Ready: %d\[rs]n", ready);
\&
@@ -619,13 +617,13 @@ main(int argc, char *argv[])
if (pfds[j].revents & POLLIN) {
s = read(pfds[j].fd, buf, sizeof(buf));
if (s == \-1)
- errExit("read");
+ err(EXIT_FAILURE, "read");
printf(" read %zd bytes: %.*s\[rs]n",
s, (int) s, buf);
} else { /* POLLERR | POLLHUP */
printf(" closing fd %d\[rs]n", pfds[j].fd);
if (close(pfds[j].fd) == \-1)
- errExit("close");
+ err(EXIT_FAILURE, "close");
num_open_fds\-\-;
}
}
diff --git a/man/man2/shmop.2 b/man/man2/shmop.2
index 12e7bd763f..660d9615ea 100644
--- a/man/man2/shmop.2
+++ b/man/man2/shmop.2
@@ -312,13 +312,11 @@ The following header file is included by the "reader" and "writer" programs:
#ifndef SVSHM_STRING_H
#define SVSHM_STRING_H
\&
+#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sem.h>
\&
-#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \[rs]
- } while (0)
-\&
union semun { /* Used in calls to semctl() */
int val;
struct semid_ds *buf;
@@ -372,23 +370,23 @@ main(void)
\&
shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600);
if (shmid == \-1)
- errExit("shmget");
+ err(EXIT_FAILURE, "shmget");
\&
semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
if (semid == \-1)
- errExit("semget");
+ err(EXIT_FAILURE, "semget");
\&
/* Attach shared memory into our address space. */
\&
addr = shmat(shmid, NULL, SHM_RDONLY);
if (addr == (void *) \-1)
- errExit("shmat");
+ err(EXIT_FAILURE, "shmat");
\&
/* Initialize semaphore 0 in set with value 1. */
\&
arg.val = 1;
if (semctl(semid, 0, SETVAL, arg) == \-1)
- errExit("semctl");
+ err(EXIT_FAILURE, "semctl");
\&
printf("shmid = %d; semid = %d\[rs]n", shmid, semid);
\&
@@ -399,7 +397,7 @@ main(void)
sop.sem_flg = 0;
\&
if (semop(semid, &sop, 1) == \-1)
- errExit("semop");
+ err(EXIT_FAILURE, "semop");
\&
/* Print the string from shared memory. */
\&
@@ -408,9 +406,9 @@ main(void)
/* Remove shared memory and semaphore set. */
\&
if (shmctl(shmid, IPC_RMID, NULL) == \-1)
- errExit("shmctl");
+ err(EXIT_FAILURE, "shmctl");
if (semctl(semid, 0, IPC_RMID, dummy) == \-1)
- errExit("semctl");
+ err(EXIT_FAILURE, "semctl");
\&
exit(EXIT_SUCCESS);
}
@@ -470,7 +468,7 @@ main(int argc, char *argv[])
\&
addr = shmat(shmid, NULL, 0);
if (addr == (void *) \-1)
- errExit("shmat");
+ err(EXIT_FAILURE, "shmat");
\&
memcpy(addr, argv[3], size);
\&
@@ -481,7 +479,7 @@ main(int argc, char *argv[])
sop.sem_flg = 0;
\&
if (semop(semid, &sop, 1) == \-1)
- errExit("semop");
+ err(EXIT_FAILURE, "semop");
\&
exit(EXIT_SUCCESS);
}
diff --git a/man/man2/timer_create.2 b/man/man2/timer_create.2
index 287d7bda4a..90ed35ef85 100644
--- a/man/man2/timer_create.2
+++ b/man/man2/timer_create.2
@@ -353,6 +353,7 @@ Caught signal 34
\&
.\" SRC BEGIN (timer_create.c)
.EX
+#include <err.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
@@ -363,9 +364,6 @@ Caught signal 34
#define CLOCKID CLOCK_REALTIME
#define SIG SIGRTMIN
\&
-#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \[rs]
- } while (0)
-\&
static void
print_siginfo(siginfo_t *si)
{
@@ -379,9 +377,9 @@ print_siginfo(siginfo_t *si)
\&
or = timer_getoverrun(*tidp);
if (or == \-1)
- errExit("timer_getoverrun");
- else
- printf(" overrun count = %d\[rs]n", or);
+ err(EXIT_FAILURE, "timer_getoverrun");
+\&
+ printf(" overrun count = %d\[rs]n", or);
}
\&
static void
@@ -421,7 +419,7 @@ main(int argc, char *argv[])
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
if (sigaction(SIG, &sa, NULL) == \-1)
- errExit("sigaction");
+ err(EXIT_FAILURE, "sigaction");
\&
/* Block timer signal temporarily. */
\&
@@ -429,7 +427,7 @@ main(int argc, char *argv[])
sigemptyset(&mask);
sigaddset(&mask, SIG);
if (sigprocmask(SIG_SETMASK, &mask, NULL) == \-1)
- errExit("sigprocmask");
+ err(EXIT_FAILURE, "sigprocmask");
\&
/* Create the timer. */
\&
@@ -437,7 +435,7 @@ main(int argc, char *argv[])
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
if (timer_create(CLOCKID, &sev, &timerid) == \-1)
- errExit("timer_create");
+ err(EXIT_FAILURE, "timer_create");
\&
printf("timer ID is %#jx\[rs]n", (uintmax_t) timerid);
\&
@@ -450,7 +448,7 @@ main(int argc, char *argv[])
its.it_interval.tv_nsec = its.it_value.tv_nsec;
\&
if (timer_settime(timerid, 0, &its, NULL) == \-1)
- errExit("timer_settime");
+ err(EXIT_FAILURE, "timer_settime");
\&
/* Sleep for a while; meanwhile, the timer may expire
multiple times. */
@@ -463,7 +461,7 @@ main(int argc, char *argv[])
\&
printf("Unblocking signal %d\[rs]n", SIG);
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == \-1)
- errExit("sigprocmask");
+ err(EXIT_FAILURE, "sigprocmask");
\&
exit(EXIT_SUCCESS);
}