summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2012-10-05 11:55:10 -0700
committerKeith Packard <keithp@keithp.com>2012-10-05 11:55:10 -0700
commitf24210f9735851f0184a861dec9f435db969fb13 (patch)
tree765cdb378ea123531998a2c60fd384f893fc67d4
parent3cea43ccbe0bbc16b139caa0586d32d75fed060c (diff)
Simplify the basic example
Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r--fdpass.c77
-rw-r--r--fdpass.h2
-rw-r--r--fdpassing.c58
3 files changed, 48 insertions, 89 deletions
diff --git a/fdpass.c b/fdpass.c
index ed67713..7ae33d7 100644
--- a/fdpass.c
+++ b/fdpass.c
@@ -17,56 +17,57 @@
#include "fdpass.h"
-int
+ssize_t
sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
{
ssize_t size;
- struct msghdr msg;
- struct iovec iov;
- union {
- struct cmsghdr cmsghdr;
- char control[CMSG_SPACE(sizeof (int))];
- } cmsgu;
- struct cmsghdr *cmsg;
-
- iov.iov_base = buf;
- iov.iov_len = bufsize;
- msg.msg_name = NULL;
- msg.msg_namelen = 0;
- msg.msg_iov = &iov;
- msg.msg_iovlen = 1;
if (fd) {
+ struct msghdr msg;
+ struct iovec iov;
+ union {
+ struct cmsghdr cmsghdr;
+ char control[CMSG_SPACE(sizeof (int))];
+ } cmsgu;
+ struct cmsghdr *cmsg;
+
+ iov.iov_base = buf;
+ iov.iov_len = bufsize;
+
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
msg.msg_control = cmsgu.control;
msg.msg_controllen = sizeof(cmsgu.control);
size = recvmsg (sock, &msg, 0);
- } else {
- size = read (sock, buf, bufsize);
- }
-
- if (size < 0) {
- perror("recvmsg");
- exit(1);
- }
-
-
- if (fd && (cmsg = CMSG_FIRSTHDR(&msg)) &&
- cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
- if (cmsg->cmsg_level != SOL_SOCKET) {
- fprintf (stderr, "invalid cmsg_level %d\n",
- cmsg->cmsg_level);
+ if (size < 0) {
+ perror ("recvmsg");
exit(1);
}
- if (cmsg->cmsg_type != SCM_RIGHTS) {
- fprintf (stderr, "invalid cmsg_type %d\n",
- cmsg->cmsg_type);
+ cmsg = CMSG_FIRSTHDR(&msg);
+ if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
+ if (cmsg->cmsg_level != SOL_SOCKET) {
+ fprintf (stderr, "invalid cmsg_level %d\n",
+ cmsg->cmsg_level);
+ exit(1);
+ }
+ if (cmsg->cmsg_type != SCM_RIGHTS) {
+ fprintf (stderr, "invalid cmsg_type %d\n",
+ cmsg->cmsg_type);
+ exit(1);
+ }
+
+ *fd = *((int *) CMSG_DATA(cmsg));
+ printf ("received fd %d\n", *fd);
+ } else
+ *fd = -1;
+ } else {
+ size = read (sock, buf, bufsize);
+ if (size < 0) {
+ perror("read");
exit(1);
}
-
- *fd = *((int *) CMSG_DATA(cmsg));
- printf ("received fd %d\n", *fd);
- } else if (fd) {
- *fd = -1;
}
return size;
}
diff --git a/fdpass.h b/fdpass.h
index e3cc915..236849a 100644
--- a/fdpass.h
+++ b/fdpass.h
@@ -26,7 +26,7 @@
#include <stdio.h>
#include <string.h>
-int
+ssize_t
sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd);
ssize_t
diff --git a/fdpassing.c b/fdpassing.c
index 53642ab..0710522 100644
--- a/fdpassing.c
+++ b/fdpassing.c
@@ -17,53 +17,17 @@
#include "fdpass.h"
-char *
-itoa(int i, char *buf)
-{
- buf += 10;
- *--buf = '\0';
- do {
- *--buf = '0' + i % 10;
- } while ((i /= 10));
- return buf;
-}
-
-void
-handler(int i)
-{
- char buf[12];
- char *b = itoa(i, buf);
-
- write (2, "signal ", 7);
- write (2, b, strlen(b));
- write (2, "\n", 1);
- _exit(1);
-}
-
void
child(int sock)
{
- int fd, *fdp;
+ int fd;
char buf[16];
ssize_t size;
- int i = 0;
- signal(SIGSEGV, handler);
- sleep(1);
- for (;;) {
- if (!(i & 1))
- fdp = NULL;
- else
- fdp = &fd;
- size = sock_fd_read(sock, buf, sizeof(buf), fdp);
- if (size == 0)
- break;
- printf ("read %d\n", size);
- if (fdp && fd != -1)
- write(fd, "hello, world\n", 13);
- i++;
- }
- printf ("child done\n");
+ size = sock_fd_read(sock, buf, sizeof(buf), &fd);
+ printf ("read %d\n", size);
+ if (fd != -1)
+ write(fd, "hello, world\n", 13);
}
void
@@ -73,15 +37,9 @@ parent(int sock)
int i;
int fd;
- for (i = 0; i < 8; i++) {
- if ((i & 1))
- fd = -1;
- else
- fd = 1;
- size = sock_fd_write(sock, "1", 1, fd);
- printf ("wrote %d\n", size);
- }
- close(sock);
+ fd = 1;
+ size = sock_fd_write(sock, "1", 1, 1);
+ printf ("wrote %d\n", size);
}
int