summaryrefslogtreecommitdiff
path: root/regress/sys
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>1998-06-28 00:11:40 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>1998-06-28 00:11:40 +0000
commitf57803fbc2591b7a2c6f6d9a6e5c8d95715db32e (patch)
tree250996107dab79d32e7d9ac484df40a66e5f8ceb /regress/sys
parentd5dc99e7d17ad41e61ca93d506d30a6335610e17 (diff)
copy from netbsd...
Diffstat (limited to 'regress/sys')
-rw-r--r--regress/sys/Makefile2
-rw-r--r--regress/sys/arch/m68k/060sp/Makefile24
-rw-r--r--regress/sys/arch/m68k/060sp/dotest.c42
-rw-r--r--regress/sys/arch/m68k/060sp/dotestsubs.S27
-rw-r--r--regress/sys/arch/m68k/060sp/goodoutput15
-rw-r--r--regress/sys/arch/m68k/Makefile5
-rw-r--r--regress/sys/arch/m68k/emuspeed/Makefile14
-rw-r--r--regress/sys/arch/m68k/emuspeed/compare15
-rw-r--r--regress/sys/arch/m68k/emuspeed/div64.s50
-rw-r--r--regress/sys/arch/m68k/emuspeed/emuspeed.c82
-rw-r--r--regress/sys/arch/m68k/emuspeed/mul64.s50
-rw-r--r--regress/sys/arch/m68k/emuspeed/speed.h17
-rw-r--r--regress/sys/arch/m68k/emuspeed/test.s26
-rw-r--r--regress/sys/kern/unfdpass/Makefile16
-rw-r--r--regress/sys/kern/unfdpass/expected3
-rw-r--r--regress/sys/kern/unfdpass/unfdpass.c297
16 files changed, 684 insertions, 1 deletions
diff --git a/regress/sys/Makefile b/regress/sys/Makefile
index 36bdb5d0d46..58815eb3910 100644
--- a/regress/sys/Makefile
+++ b/regress/sys/Makefile
@@ -1,6 +1,6 @@
# $NetBSD: Makefile,v 1.4 1995/04/20 22:41:08 cgd Exp $
-SUBDIR+= kern
+SUBDIR+= kern unfdpass
.if exists(arch/${MACHINE})
SUBDIR+= arch/${MACHINE}
.endif
diff --git a/regress/sys/arch/m68k/060sp/Makefile b/regress/sys/arch/m68k/060sp/Makefile
new file mode 100644
index 00000000000..fb3bcb6f437
--- /dev/null
+++ b/regress/sys/arch/m68k/060sp/Makefile
@@ -0,0 +1,24 @@
+# $NetBSD: Makefile,v 1.3 1998/06/16 19:06:31 is Exp $
+
+NOMAN=notyet
+PROG=dotest
+SRCS=dotest.c dotestsubs.S
+AFLAGS+=-I.
+CPPFLAGS+=-I.
+SPDIR=${.CURDIR}/../../../../../sys/arch/m68k/060sp
+CLEANFILES+=itest.S ftest.S
+
+.SUFFIXES: .S .sa
+
+.sa.S: ; sh ${SPDIR}/asm2gas ${.IMPSRC} >${.TARGET}
+
+.PATH: ${SPDIR}
+
+depend dotestsubs.o: itest.S ftest.S
+
+regress: ${PROG}
+ @/usr/sbin/sysctl hw.model | grep -v -q 68060 || \
+ ./${PROG} | tee /dev/tty | cmp -s - ${.CURDIR}/goodoutput
+
+
+.include <bsd.prog.mk>
diff --git a/regress/sys/arch/m68k/060sp/dotest.c b/regress/sys/arch/m68k/060sp/dotest.c
new file mode 100644
index 00000000000..ac1076dd2f0
--- /dev/null
+++ b/regress/sys/arch/m68k/060sp/dotest.c
@@ -0,0 +1,42 @@
+/* $NetBSD: dotest.c,v 1.2 1998/01/09 08:03:54 perry Exp $ */
+
+#include <stdio.h>
+
+void print_str(const char *s);
+void print_num(const int);
+void itest(void);
+void ftest1(void);
+void ftest2(void);
+void ftest3(void);
+
+void
+print_str(s)
+ const char *s;
+{
+ printf("%s", s);
+ fflush(stdout);
+}
+
+void
+print_num(i)
+ int i;
+{
+ printf("%d", i);
+ fflush(stdout);
+}
+
+int
+main()
+{
+ itest();
+ ftest1();
+ ftest2();
+#if 0
+ /*
+ * We would need a special kernel, that clears the exception condition
+ * and does RTE, to run this.
+ */
+ ftest3();
+#endif
+ exit (0);
+}
diff --git a/regress/sys/arch/m68k/060sp/dotestsubs.S b/regress/sys/arch/m68k/060sp/dotestsubs.S
new file mode 100644
index 00000000000..32074c233ee
--- /dev/null
+++ b/regress/sys/arch/m68k/060sp/dotestsubs.S
@@ -0,0 +1,27 @@
+ .globl _print_str,_print_num
+
+Lprint_str: jmp _print_str
+Lprint_num: jmp _print_num
+
+_060FPSP_TEST:
+ .long Lprint_str - _060FPSP_TEST
+ .long Lprint_num - _060FPSP_TEST
+ .space 120
+#include "ftest.S"
+
+_060ISP_TEST:
+ .long Lprint_str - _060ISP_TEST
+ .long Lprint_num - _060ISP_TEST
+ .space 120
+#include "itest.S"
+
+ .globl _itest,_ftest1,_ftest2,_ftest3
+_itest:
+ bra _060ISP_TEST+128+0
+_ftest1:
+ bra _060FPSP_TEST+128+0
+_ftest2:
+ bra _060FPSP_TEST+128+8
+_ftest3:
+ bra _060FPSP_TEST+128+16
+
diff --git a/regress/sys/arch/m68k/060sp/goodoutput b/regress/sys/arch/m68k/060sp/goodoutput
new file mode 100644
index 00000000000..1a26b76568a
--- /dev/null
+++ b/regress/sys/arch/m68k/060sp/goodoutput
@@ -0,0 +1,15 @@
+Testing 68060 ISP started:
+ 64-bit multiply...passed
+ 64-bit divide...passed
+ cmp2,chk2...passed
+ movep...passed
+ Effective addresses...passed
+ cas...passed
+ cas2...passed
+Testing 68060 FPSP started:
+ Unimplemented <ea>...passed
+ Unimplemented data type/format...passed
+ Non-maskable overflow...passed
+ Non-maskable underflow...passed
+Testing 68060 FPSP unimplemented instruction started:
+ Unimplemented FP instructions...passed
diff --git a/regress/sys/arch/m68k/Makefile b/regress/sys/arch/m68k/Makefile
new file mode 100644
index 00000000000..bf862fd4438
--- /dev/null
+++ b/regress/sys/arch/m68k/Makefile
@@ -0,0 +1,5 @@
+# $NetBSD: Makefile,v 1.2 1998/01/09 08:03:53 perry Exp $
+#
+SUBDIR+=060sp emuspeed
+
+.include <bsd.subdir.mk>
diff --git a/regress/sys/arch/m68k/emuspeed/Makefile b/regress/sys/arch/m68k/emuspeed/Makefile
new file mode 100644
index 00000000000..c96d564339a
--- /dev/null
+++ b/regress/sys/arch/m68k/emuspeed/Makefile
@@ -0,0 +1,14 @@
+# $NetBSD: Makefile,v 1.2 1998/01/09 08:03:55 perry Exp $
+#
+PROG=emuspeed
+NOMAN=none
+
+SRCS=emuspeed.c test.s mul64.s div64.s # movepto.c movepfrom.c
+
+/* don't install: */
+proginstall:
+
+.include <bsd.prog.mk>
+
+regress: all
+ @./emuspeed
diff --git a/regress/sys/arch/m68k/emuspeed/compare b/regress/sys/arch/m68k/emuspeed/compare
new file mode 100644
index 00000000000..5e24743fa4c
--- /dev/null
+++ b/regress/sys/arch/m68k/emuspeed/compare
@@ -0,0 +1,15 @@
+Speed of instructions which are emulated on some cpus:
+
+Illegal [unimplemented] (test: unimplemented)
+mulsl sp@(8),Da 18181800/s (test: should be native)
+
+mulsl Dn,Da:Db 185300/s emulated on 68060
+mulul Dn,Da:Db 184300/s "
+mulsl sp@(8),Da:Db 99600/s "
+mulul sp@(8),Da:Db 98900/s "
+
+divsl Da:Db,Dn 162400/s "
+divul Da:Db,Dn 111500/s "
+divsl Da:Db,sp@(8) 58900/s "
+divul Da:Db,sp@(8) 59700/s "
+
diff --git a/regress/sys/arch/m68k/emuspeed/div64.s b/regress/sys/arch/m68k/emuspeed/div64.s
new file mode 100644
index 00000000000..b75235a492f
--- /dev/null
+++ b/regress/sys/arch/m68k/emuspeed/div64.s
@@ -0,0 +1,50 @@
+/*
+ * stack:
+ * + 8: count
+ * + 4: retads
+ * + 0: d2
+ */
+
+ .globl _div64ureg
+_div64ureg:
+ movl d2,sp@-
+ movl sp@(8),d2
+L1:
+ divul d2,d1:d0
+ subql #1,d2
+ jne L1
+ movl sp@+,d2
+ rts
+
+ .globl _div64sreg
+_div64sreg:
+ movl d2,sp@-
+ movl sp@(8),d2
+L2:
+ divsl d2,d1:d0
+ subql #1,d2
+ jne L2
+ movl sp@+,d2
+ rts
+
+ .globl _div64umem
+_div64umem:
+ movl d2,sp@-
+ movl sp@(8),d2
+L3:
+ divul sp@(8),d1:d0
+ subql #1,d2
+ jne L3
+ movl sp@+,d2
+ rts
+
+ .globl _div64smem
+_div64smem:
+ movl d2,sp@-
+ movl sp@(8),d2
+L4:
+ divsl sp@(8),d1:d0
+ subql #1,d2
+ jne L4
+ movl sp@+,d2
+ rts
diff --git a/regress/sys/arch/m68k/emuspeed/emuspeed.c b/regress/sys/arch/m68k/emuspeed/emuspeed.c
new file mode 100644
index 00000000000..092ec595be7
--- /dev/null
+++ b/regress/sys/arch/m68k/emuspeed/emuspeed.c
@@ -0,0 +1,82 @@
+/* $NetBSD: emuspeed.c,v 1.3 1998/06/15 14:43:25 is Exp $ */
+
+#include <setjmp.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "speed.h"
+
+#define PRECISION 500
+
+const struct test {
+ char *name;
+ void (*func)__P((int));
+ char *comment;
+} testlist[] = {
+ {"Illegal", illegal, "(test: unimplemented)"},
+ {"mulsl Da,Db", mul32sreg, "(test: should be native)"},
+ {"mulsl sp@(8),Da", mul32smem, "(test: should be native)\n"},
+
+ {"mulsl Dn,Da:Db", mul64sreg, "emulated on 68060"},
+ {"mulul Dn,Da:Db", mul64ureg, "\t\""},
+ {"mulsl sp@(8),Da:Db", mul64smem, "\t\""},
+ {"mulul sp@(8),Da:Db", mul64umem, "\t\"\n"},
+
+ {"divsl Da:Db,Dn", div64sreg, "\t\""},
+ {"divul Da:Db,Dn", div64ureg, "\t\""},
+ {"divsl Da:Db,sp@(8)", div64smem, "\t\""},
+ {"divul Da:Db,sp@(8)", div64umem, "\t\"\n"},
+
+ {NULL, NULL, NULL}
+};
+
+jmp_buf jbuf;
+void illhand (int);
+
+int
+main(argc, argv)
+ int argc;
+ char *argv[];
+{
+ const struct test *t;
+ clock_t start, stop;
+ int count;
+
+
+ if (signal(SIGILL, &illhand))
+ warn("%s: can't install illegal instruction handler.",
+ argv[0]);
+
+ printf("Speed of instructions which are emulated on some cpus:\n\n");
+ (void)sleep(1);
+ for (t=testlist; t->name; t++) {
+ printf("%-20s", t->name);
+ fflush(stdout);
+
+ if (setjmp(jbuf)) {
+ printf("%15s %s\n", "[unimplemented]", t->comment);
+ continue;
+ }
+
+ count = 1000;
+ do {
+ count *= 2;
+ start = clock();
+ t->func(count);
+ stop = clock();
+ } while ((stop - start) < PRECISION);
+ printf("%13d/s %s\n",
+ CLOCKS_PER_SEC*(count /(stop - start)),
+ t->comment);
+ }
+ exit (0);
+}
+
+void
+illhand(int i)
+{
+ longjmp(jbuf, 1);
+}
diff --git a/regress/sys/arch/m68k/emuspeed/mul64.s b/regress/sys/arch/m68k/emuspeed/mul64.s
new file mode 100644
index 00000000000..1586b423b58
--- /dev/null
+++ b/regress/sys/arch/m68k/emuspeed/mul64.s
@@ -0,0 +1,50 @@
+/*
+ * stack:
+ * + 8: count
+ * + 4: retads
+ * + 0: d2
+ */
+
+ .globl _mul64ureg
+_mul64ureg:
+ movl d2,sp@-
+ movl sp@(8),d2
+L1:
+ mulul d2,d1:d0
+ subql #1,d2
+ jne L1
+ movl sp@+,d2
+ rts
+
+ .globl _mul64sreg
+_mul64sreg:
+ movl d2,sp@-
+ movl sp@(8),d2
+L2:
+ mulsl d2,d1:d0
+ subql #1,d2
+ jne L2
+ movl sp@+,d2
+ rts
+
+ .globl _mul64umem
+_mul64umem:
+ movl d2,sp@-
+ movl sp@(8),d2
+L3:
+ mulul sp@(8),d1:d0
+ subql #1,d2
+ jne L3
+ movl sp@+,d2
+ rts
+
+ .globl _mul64smem
+_mul64smem:
+ movl d2,sp@-
+ movl sp@(8),d2
+L4:
+ mulsl sp@(8),d1:d0
+ subql #1,d2
+ jne L4
+ movl sp@+,d2
+ rts
diff --git a/regress/sys/arch/m68k/emuspeed/speed.h b/regress/sys/arch/m68k/emuspeed/speed.h
new file mode 100644
index 00000000000..205b143d0f5
--- /dev/null
+++ b/regress/sys/arch/m68k/emuspeed/speed.h
@@ -0,0 +1,17 @@
+/* $NetBSD: speed.h,v 1.2 1998/01/09 08:03:57 perry Exp $ */
+
+#include <sys/cdefs.h>
+
+void illegal __P((int));
+void mul32smem __P((int));
+void mul32sreg __P((int));
+
+void mul64sreg __P((int));
+void mul64ureg __P((int));
+void mul64smem __P((int));
+void mul64umem __P((int));
+
+void div64umem __P((int));
+void div64smem __P((int));
+void div64ureg __P((int));
+void div64sreg __P((int));
diff --git a/regress/sys/arch/m68k/emuspeed/test.s b/regress/sys/arch/m68k/emuspeed/test.s
new file mode 100644
index 00000000000..7c91f4f58f5
--- /dev/null
+++ b/regress/sys/arch/m68k/emuspeed/test.s
@@ -0,0 +1,26 @@
+ .globl _mul32smem
+_mul32smem:
+ movl d2,sp@-
+ movl sp@(8),d2
+L1:
+ mulsl sp@(8),d1
+ subql #1,d2
+ jne L1
+ movl sp@+,d2
+ rts
+
+ .globl _mul32sreg
+_mul32sreg:
+ movl d2,sp@-
+ movl sp@(8),d2
+L2:
+ mulsl d0,d1
+ subql #1,d2
+ jne L2
+ movl sp@+,d2
+ rts
+
+ .globl _illegal
+_illegal:
+ illegal
+ rts
diff --git a/regress/sys/kern/unfdpass/Makefile b/regress/sys/kern/unfdpass/Makefile
new file mode 100644
index 00000000000..2f4651ff3f3
--- /dev/null
+++ b/regress/sys/kern/unfdpass/Makefile
@@ -0,0 +1,16 @@
+# $NetBSD: Makefile,v 1.3 1998/03/02 21:57:38 cgd Exp $
+
+PROG= unfdpass
+NOMAN= noman, no way, man
+
+CLEANFILES+=file1 file2 output test-sock
+
+regress:
+ @./unfdpass > output
+ @if ! cmp -s ${.CURDIR}/expected output; then \
+ echo "FAILED"; \
+ else \
+ echo "PASSED"; \
+ fi
+
+.include <bsd.prog.mk>
diff --git a/regress/sys/kern/unfdpass/expected b/regress/sys/kern/unfdpass/expected
new file mode 100644
index 00000000000..c7a7622045d
--- /dev/null
+++ b/regress/sys/kern/unfdpass/expected
@@ -0,0 +1,3 @@
+This is file 1.
+This is file 2.
+Credentials match.
diff --git a/regress/sys/kern/unfdpass/unfdpass.c b/regress/sys/kern/unfdpass/unfdpass.c
new file mode 100644
index 00000000000..c946eea23b0
--- /dev/null
+++ b/regress/sys/kern/unfdpass/unfdpass.c
@@ -0,0 +1,297 @@
+/* $NetBSD: unfdpass.c,v 1.3 1998/06/24 23:51:30 thorpej Exp $ */
+
+/*-
+ * Copyright (c) 1998 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
+ * NASA Ames Research Center.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the NetBSD
+ * Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Test passing of file descriptors and credentials over Unix domain sockets.
+ */
+
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <sys/un.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#define SOCK_NAME "test-sock"
+
+int main __P((int, char *[]));
+void child __P((void));
+void catch_sigchld __P((int));
+
+struct fdcmessage {
+ struct cmsghdr cm;
+ int files[2];
+};
+
+struct crcmessage {
+ struct cmsghdr cm;
+ char creds[SOCKCREDSIZE(NGROUPS)];
+};
+
+/* ARGSUSED */
+int
+main(argc, argv)
+ int argc;
+ char *argv[];
+{
+ struct msghdr msg;
+ int listensock, sock, fd, i, status;
+ char fname[16], buf[64];
+ struct cmsghdr *cmp;
+ struct {
+ struct fdcmessage fdcm;
+ struct crcmessage crcm;
+ } message;
+ int *files = NULL;
+ struct sockcred *sc = NULL;
+ struct sockaddr_un sun, csun;
+ int csunlen;
+ fd_set oob;
+ pid_t pid;
+
+ /*
+ * Create the test files.
+ */
+ for (i = 0; i < 2; i++) {
+ (void) sprintf(fname, "file%d", i + 1);
+ if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
+ err(1, "open %s", fname);
+ (void) sprintf(buf, "This is file %d.\n", i + 1);
+ if (write(fd, buf, strlen(buf)) != strlen(buf))
+ err(1, "write %s", fname);
+ (void) close(fd);
+ }
+
+ /*
+ * Create the listen socket.
+ */
+ if ((listensock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
+ err(1, "socket");
+
+ (void) unlink(SOCK_NAME);
+ (void) memset(&sun, 0, sizeof(sun));
+ sun.sun_family = AF_LOCAL;
+ (void) strcpy(sun.sun_path, SOCK_NAME);
+ sun.sun_len = SUN_LEN(&sun);
+
+ i = 1;
+ if (setsockopt(listensock, 0, LOCAL_CREDS, &i, sizeof(i)) == -1)
+ err(1, "setsockopt");
+
+ if (bind(listensock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
+ err(1, "bind");
+
+ if (listen(listensock, 1) == -1)
+ err(1, "listen");
+
+ /*
+ * Create the sender.
+ */
+ (void) signal(SIGCHLD, catch_sigchld);
+ pid = fork();
+ switch (pid) {
+ case -1:
+ err(1, "fork");
+ /* NOTREACHED */
+
+ case 0:
+ child();
+ /* NOTREACHED */
+ }
+
+ /*
+ * Wait for the sender to connect.
+ */
+ if ((sock = accept(listensock, (struct sockaddr *)&csun,
+ &csunlen)) == -1)
+ err(1, "accept");
+
+ /*
+ * Give sender a chance to run. We will get going again
+ * once the SIGCHLD arrives.
+ */
+ (void) sleep(10);
+
+ /*
+ * Grab the descriptors and credentials passed to us.
+ */
+ (void) memset(&msg, 0, sizeof(msg));
+ msg.msg_control = (caddr_t) &message;
+ msg.msg_controllen = sizeof(message);
+
+ if (recvmsg(sock, &msg, 0) < 0)
+ err(1, "recvmsg");
+
+ (void) close(sock);
+
+ if (msg.msg_controllen == 0)
+ errx(1, "no control messages received");
+
+ if (msg.msg_flags & MSG_CTRUNC)
+ errx(1, "lost control message data");
+
+ cmp = CMSG_FIRSTHDR(&msg);
+ for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL;
+ cmp = CMSG_NXTHDR(&msg, cmp)) {
+ if (cmp->cmsg_level != SOL_SOCKET)
+ errx(1, "bad control message level %d",
+ cmp->cmsg_level);
+
+ switch (cmp->cmsg_type) {
+ case SCM_RIGHTS:
+ if (cmp->cmsg_len != sizeof(message.fdcm))
+ errx(1, "bad fd control message length");
+
+ files = (int *)CMSG_DATA(cmp);
+ break;
+
+ case SCM_CREDS:
+ if (cmp->cmsg_len < sizeof(struct sockcred))
+ errx(1, "bad cred control message length");
+
+ sc = (struct sockcred *)CMSG_DATA(cmp);
+ break;
+
+ default:
+ errx(1, "unexpected control message");
+ /* NOTREACHED */
+ }
+ }
+
+ /*
+ * Read the files and print their contents.
+ */
+ if (files == NULL)
+ warnx("didn't get fd control message");
+ else {
+ for (i = 0; i < 2; i++) {
+ (void) memset(buf, 0, sizeof(buf));
+ if (read(files[i], buf, sizeof(buf)) <= 0)
+ err(1, "read file %d", i + 1);
+ printf("%s", buf);
+ }
+ }
+
+ /*
+ * Double-check credentials.
+ */
+ if (sc == NULL)
+ warnx("didn't get cred control message");
+ else {
+ if (sc->sc_uid == getuid() &&
+ sc->sc_euid == geteuid() &&
+ sc->sc_gid == getgid() &&
+ sc->sc_egid == getegid())
+ printf("Credentials match.\n");
+ else
+ printf("Credentials do NOT match.\n");
+ }
+
+ /*
+ * All done!
+ */
+ exit(0);
+}
+
+void
+catch_sigchld(sig)
+ int sig;
+{
+ int status;
+
+ (void) wait(&status);
+}
+
+void
+child()
+{
+ struct msghdr msg;
+ char fname[16], buf[64];
+ struct cmsghdr *cmp;
+ struct fdcmessage fdcm;
+ int i, fd, sock;
+ struct sockaddr_un sun;
+
+ /*
+ * Create socket and connect to the receiver.
+ */
+ if ((sock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
+ errx(1, "child socket");
+
+ (void) memset(&sun, 0, sizeof(sun));
+ sun.sun_family = AF_LOCAL;
+ (void) strcpy(sun.sun_path, SOCK_NAME);
+ sun.sun_len = SUN_LEN(&sun);
+
+ if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
+ err(1, "child connect");
+
+ /*
+ * Open the files again, and pass them to the child over the socket.
+ */
+ for (i = 0; i < 2; i++) {
+ (void) sprintf(fname, "file%d", i + 1);
+ if ((fd = open(fname, O_RDONLY, 0666)) == -1)
+ err(1, "child open %s", fname);
+ fdcm.files[i] = fd;
+ }
+
+ (void) memset(&msg, 0, sizeof(msg));
+ msg.msg_control = (caddr_t) &fdcm;
+ msg.msg_controllen = sizeof(fdcm);
+
+ cmp = CMSG_FIRSTHDR(&msg);
+ cmp->cmsg_len = sizeof(fdcm);
+ cmp->cmsg_level = SOL_SOCKET;
+ cmp->cmsg_type = SCM_RIGHTS;
+
+ if (sendmsg(sock, &msg, 0))
+ err(1, "child sendmsg");
+
+ /*
+ * All done!
+ */
+ exit(0);
+}