Small changes in EXAMPLE program.

This commit is contained in:
Michael Kerrisk 2007-06-02 08:31:25 +00:00
parent 544195a1d6
commit d3b5ab8237
3 changed files with 29 additions and 44 deletions

View File

@ -264,6 +264,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)
int
main(int argc, char *argv[])
{
@ -272,10 +274,8 @@ main(int argc, char *argv[])
socklen_t peer_addr_size;
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
if (sfd == -1)
die("socket");
memset(&my_addr, 0, sizeof(struct sockaddr_un));
/* Clear structure */
@ -284,25 +284,20 @@ main(int argc, char *argv[])
sizeof(my_addr.sun_path) - 1);
if (bind(sfd, (struct sockaddr *) &my_addr,
sizeof(struct sockaddr_un)) == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
sizeof(struct sockaddr_un)) == -1)
die("bind");
if (listen(sfd, LISTEN_BACKLOG) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
if (listen(sfd, LISTEN_BACKLOG) == -1)
die("listen");
/* Now we can accept incoming connections one
at a time using accept(2) */
peer_addr_size = sizeof(struct sockaddr_un);
cfd = accept(sfd, (struct sockaddr *) &peer_addr,
&peer_addr_size) == -1) {
perror("accept");
exit(EXIT_FAILURE);
}
&peer_addr_size)
if (cfd == -1)
die("accept");
}
.fi
.in -0.25in

View File

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

View File

@ -166,6 +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)
int
main(int argc, char *argv[])
{
@ -177,16 +179,12 @@ main(int argc, char *argv[])
assert(argc == 2);
in = fmemopen(argv[1], strlen(argv[1]), "r");
if (in == NULL) {
perror("fmemopen");
exit(EXIT_FAILURE);
}
if (in == NULL)
die("fmemopen");
out = open_memstream(&ptr, &size);
if (out == NULL) {
perror("fmemopen");
exit(EXIT_FAILURE);
}
if (out == NULL)
die("fmemopen");
for (;;) {
s = fscanf(in, "%d", &v);
@ -194,10 +192,8 @@ main(int argc, char *argv[])
break;
s = fprintf(out, "%d ", v * v);
if (s == -1) {
perror("fprintf");
exit(EXIT_FAILURE);
}
if (s == -1)
die("fprintf");
}
fclose(in);
fclose(out);