diff options
author | Hans-Joerg Hoexer <hshoexer@cvs.openbsd.org> | 2008-03-15 16:49:16 +0000 |
---|---|---|
committer | Hans-Joerg Hoexer <hshoexer@cvs.openbsd.org> | 2008-03-15 16:49:16 +0000 |
commit | a430e982ee4fd56dfbbe64a54b55de2cffa8e1fd (patch) | |
tree | 98b34effb5a86fb4347dee8f47a4156ef531be76 | |
parent | 4d6a330d6ef239447a33e87153f32763cce18806 (diff) |
Repair usage of CMSG_SPACE and CMSG_LEN. While there, send three fds
instead of just two as this decreases the propability that things just
work although the sizes are wrong (ie. 8 aligns correctly on both 32 and 64 bit
platforms even with wrong usage of CMSG_{LEN,SPACE} whereas 12 doesn't).
-rw-r--r-- | regress/sys/kern/unfdpass/expected | 1 | ||||
-rw-r--r-- | regress/sys/kern/unfdpass/unfdpass.c | 20 |
2 files changed, 11 insertions, 10 deletions
diff --git a/regress/sys/kern/unfdpass/expected b/regress/sys/kern/unfdpass/expected index 8f0a0870d07..15fb28eeae2 100644 --- a/regress/sys/kern/unfdpass/expected +++ b/regress/sys/kern/unfdpass/expected @@ -1,2 +1,3 @@ This is file 1. This is file 2. +This is file 3. diff --git a/regress/sys/kern/unfdpass/unfdpass.c b/regress/sys/kern/unfdpass/unfdpass.c index b695b227446..80e6b0ec8fd 100644 --- a/regress/sys/kern/unfdpass/unfdpass.c +++ b/regress/sys/kern/unfdpass/unfdpass.c @@ -1,4 +1,4 @@ -/* $OpenBSD: unfdpass.c,v 1.13 2008/03/13 01:49:53 deraadt Exp $ */ +/* $OpenBSD: unfdpass.c,v 1.14 2008/03/15 16:49:15 hshoexer Exp $ */ /* $NetBSD: unfdpass.c,v 1.3 1998/06/24 23:51:30 thorpej Exp $ */ /*- @@ -76,7 +76,7 @@ main(int argc, char *argv[]) pid_t pid; union { struct cmsghdr hdr; - char buf[CMSG_SPACE(sizeof(int) * 2)]; + char buf[CMSG_SPACE(sizeof(int) * 3)]; } cmsgbuf; int pflag; extern char *__progname; @@ -96,7 +96,7 @@ main(int argc, char *argv[]) /* * Create the test files. */ - for (i = 0; i < 2; i++) { + for (i = 0; i < 3; i++) { (void) snprintf(fname, sizeof fname, "file%d", i + 1); if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) err(1, "open %s", fname); @@ -173,7 +173,7 @@ main(int argc, char *argv[]) */ (void) memset(&msg, 0, sizeof(msg)); msg.msg_control = &cmsgbuf.buf; - msg.msg_controllen = sizeof(cmsgbuf.buf); + msg.msg_controllen = CMSG_LEN(sizeof(int) * 3); if (recvmsg(sock, &msg, 0) < 0) err(1, "recvmsg"); @@ -194,7 +194,7 @@ main(int argc, char *argv[]) switch (cmp->cmsg_type) { case SCM_RIGHTS: - if (cmp->cmsg_len != CMSG_LEN(sizeof(int) * 2)) + if (cmp->cmsg_len != CMSG_LEN(sizeof(int) * 3)) errx(1, "bad fd control message length %d", cmp->cmsg_len); @@ -213,7 +213,7 @@ main(int argc, char *argv[]) if (files == NULL) warnx("didn't get fd control message"); else { - for (i = 0; i < 2; i++) { + for (i = 0; i < 3; i++) { (void) memset(buf, 0, sizeof(buf)); if (read(files[i], buf, sizeof(buf)) <= 0) err(1, "read file %d (%d)", i + 1, files[i]); @@ -248,7 +248,7 @@ child(int sock) struct sockaddr_un sun; union { struct cmsghdr hdr; - char buf[CMSG_SPACE(sizeof(int) * 2)]; + char buf[CMSG_SPACE(sizeof(int) * 3)]; } cmsgbuf; int *files; @@ -270,10 +270,10 @@ child(int sock) (void) memset(&msg, 0, sizeof(msg)); msg.msg_control = &cmsgbuf.buf; - msg.msg_controllen = sizeof(cmsgbuf.buf); + msg.msg_controllen = CMSG_LEN(sizeof(int) * 3); cmp = CMSG_FIRSTHDR(&msg); - cmp->cmsg_len = CMSG_LEN(sizeof(int) * 2); + cmp->cmsg_len = CMSG_LEN(sizeof(int) * 3); cmp->cmsg_level = SOL_SOCKET; cmp->cmsg_type = SCM_RIGHTS; @@ -281,7 +281,7 @@ child(int sock) * Open the files again, and pass them to the child over the socket. */ files = (int *)CMSG_DATA(cmp); - for (i = 0; i < 2; i++) { + for (i = 0; i < 3; i++) { (void) snprintf(fname, sizeof fname, "file%d", i + 1); if ((fd = open(fname, O_RDONLY, 0666)) == -1) err(1, "child open %s", fname); |