s/dir(/handle_error(/

This commit is contained in:
Michael Kerrisk 2007-11-19 03:25:26 +00:00
parent 4c20c40c7b
commit 6a578b8823
5 changed files with 31 additions and 26 deletions

View File

@ -272,7 +272,8 @@ domain, and accept connections:
#define MY_SOCK_PATH "/somepath"
#define LISTEN_BACKLOG 50
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error(msg) \\
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
@ -283,7 +284,7 @@ main(int argc, char *argv[])
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == \-1)
die("socket");
handle_error("socket");
memset(&my_addr, 0, sizeof(struct sockaddr_un));
/* Clear structure */
@ -293,10 +294,10 @@ main(int argc, char *argv[])
if (bind(sfd, (struct sockaddr *) &my_addr,
sizeof(struct sockaddr_un)) == \-1)
die("bind");
handle_error("bind");
if (listen(sfd, LISTEN_BACKLOG) == \-1)
die("listen");
handle_error("listen");
/* Now we can accept incoming connections one
at a time using accept(2) */
@ -305,7 +306,7 @@ main(int argc, char *argv[])
cfd = accept(sfd, (struct sockaddr *) &peer_addr,
&peer_addr_size);
if (cfd == \-1)
die("accept");
handle_error("accept");
/* Code to deal with incoming connection(s)... */

View File

@ -164,7 +164,8 @@ Got SIGSEGV at address: 0x804e000
#include <errno.h>
#include <sys/mman.h>
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error(msg) \\
do { perror(msg); exit(EXIT_FAILURE); } while (0)
char *buffer;
@ -187,24 +188,24 @@ main(int argc, char *argv[])
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
if (sigaction(SIGSEGV, &sa, NULL) == \-1)
die("sigaction");
handle_error("sigaction");
pagesize = sysconf(_SC_PAGE_SIZE);
if (pagesize == \-1)
die("sysconf");
handle_error("sysconf");
/* Allocate a buffer aligned on a page boundary;
initial protection is PROT_READ | PROT_WRITE */
buffer = memalign(pagesize, 4 * pagesize);
if (buffer == NULL)
die("memalign");
handle_error("memalign");
printf("Start of region: 0x%lx\\n", (long) buffer);
if (mprotect(buffer + pagesize * 2, pagesize,
PROT_NONE) == \-1)
die("mprotect");
handle_error("mprotect");
for (p = buffer ; ; )
*(p++) = 'a';

View File

@ -166,7 +166,8 @@ size=11; ptr=1 529 1849
#include <stdio.h>
#include <stdlib.h>
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error(msg) \\
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
@ -180,11 +181,11 @@ main(int argc, char *argv[])
in = fmemopen(argv[1], strlen(argv[1]), "r");
if (in == NULL)
die("fmemopen");
handle_error("fmemopen");
out = open_memstream(&ptr, &size);
if (out == NULL)
die("fmemopen");
handle_error("fmemopen");
for (;;) {
s = fscanf(in, "%d", &v);
@ -193,7 +194,7 @@ main(int argc, char *argv[])
s = fprintf(out, "%d ", v * v);
if (s == \-1)
die("fprintf");
handle_error("fprintf");
}
fclose(in);
fclose(out);

View File

@ -132,7 +132,8 @@ main: exiting
static ucontext_t uctx_main, uctx_func1, uctx_func2;
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error(msg) \\
do { perror(msg); exit(EXIT_FAILURE); } while (0)
static void
func1(void)
@ -140,7 +141,7 @@ func1(void)
printf("func1: started\\n");
printf("func1: swapcontext(&uctx_func1, &uctx_func2)\\n");
if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
die("swapcontext");
handle_error("swapcontext");
printf("func1: returning\\n");
}
@ -150,7 +151,7 @@ func2(void)
printf("func2: started\\n");
printf("func2: swapcontext(&uctx_func2, &uctx_func1)\\n");
if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
die("swapcontext");
handle_error("swapcontext");
printf("func2: returning\\n");
}
@ -161,14 +162,14 @@ main(int argc, char *argv[])
char func2_stack[16384];
if (getcontext(&uctx_func1) == \-1)
die("getcontext");
handle_error("getcontext");
uctx_func1.uc_stack.ss_sp = func1_stack;
uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
uctx_func1.uc_link = &uctx_main;
makecontext(&uctx_func1, func1, 0);
if (getcontext(&uctx_func2) == \-1)
die("getcontext");
handle_error("getcontext");
uctx_func2.uc_stack.ss_sp = func2_stack;
uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
/* Successor context is f1(), unless argc > 1 */
@ -177,7 +178,7 @@ main(int argc, char *argv[])
printf("main: swapcontext(&uctx_main, &uctx_func2)\\n");
if (swapcontext(&uctx_main, &uctx_func2) == \-1)
die("swapcontext");
handle_error("swapcontext");
printf("main: exiting\\n");
exit(EXIT_SUCCESS);

View File

@ -205,7 +205,8 @@ queue and then terminates the process.
#include <stdlib.h>
#include <unistd.h>
#define die(msg) { perror(msg); exit(EXIT_FAILURE); }
#define handle_error(msg) \\
do { perror(msg); exit(EXIT_FAILURE); } while (0)
static void /* Thread start function */
tfunc(union sigval sv)
@ -218,14 +219,14 @@ tfunc(union sigval sv)
/* Determine max. msg size; allocate buffer to receive msg */
if (mq_getattr(mqdes, &attr) == \-1)
die("mq_getattr");
handle_error("mq_getattr");
buf = malloc(attr.mq_msgsize);
if (buf == NULL)
die("malloc");
handle_error("malloc");
nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
if (nr == \-1)
die("mq_receive");
handle_error("mq_receive");
printf("Read %ld bytes from MQ\\n", (long) nr);
free(buf);
@ -242,14 +243,14 @@ main(int argc, char *argv[])
mqdes = mq_open(argv[1], O_RDONLY);
if (mqdes == (mqd_t) \-1)
die("mq_open");
handle_error("mq_open");
not.sigev_notify = SIGEV_THREAD;
not.sigev_notify_function = tfunc;
not.sigev_notify_attributes = NULL;
not.sigev_value.sival_ptr = &mqdes; /* Arg. to thread func. */
if (mq_notify(mqdes, &not) == \-1)
die("mq_notify");
handle_error("mq_notify");
pause(); /* Process will be terminated by thread function */
}