summaryrefslogtreecommitdiff
path: root/regress
diff options
context:
space:
mode:
authorVitaliy Makkoveev <mvs@cvs.openbsd.org>2021-12-09 17:25:55 +0000
committerVitaliy Makkoveev <mvs@cvs.openbsd.org>2021-12-09 17:25:55 +0000
commit0e0e796a9f00e43ce017b9c5bcb908403628ba6d (patch)
tree5e9766c639ea3513de23a9f492757eaaabf706cf /regress
parentc22a9e0f3e8b2abd1f474926fff5a1ba39295217 (diff)
Add sys/kern/unixsockets directory to contain all unix(4) sockets tests.
Add 'undgram_selfconn' test to check unix(4) socket connecting to itself. Discussed and ok visa@ bluhm@
Diffstat (limited to 'regress')
-rw-r--r--regress/sys/kern/Makefile4
-rw-r--r--regress/sys/kern/unixsockets/Makefile23
-rw-r--r--regress/sys/kern/unixsockets/undgram_selfconn.c81
3 files changed, 106 insertions, 2 deletions
diff --git a/regress/sys/kern/Makefile b/regress/sys/kern/Makefile
index 00daa065c97..23aeacf8eff 100644
--- a/regress/sys/kern/Makefile
+++ b/regress/sys/kern/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.95 2021/11/29 13:05:04 mvs Exp $
+# $OpenBSD: Makefile,v 1.96 2021/12/09 17:25:54 mvs Exp $
SUBDIR+= __syscall
SUBDIR+= accept access
@@ -24,7 +24,7 @@ SUBDIR+= setuid
SUBDIR+= signal sosplice stackjmp stackpivot syscall syscall_segment
SUBDIR+= sysvmsg sysvsem sysvshm
SUBDIR+= unalign unconacc undgram_conclose unfdpass unfdpassfail ungc
-SUBDIR+= unsendrecvthr unixsock unveil unveil-unmount
+SUBDIR+= unixsockets unsendrecvthr unixsock unveil unveil-unmount
SUBDIR+= wait
install:
diff --git a/regress/sys/kern/unixsockets/Makefile b/regress/sys/kern/unixsockets/Makefile
new file mode 100644
index 00000000000..f3f1a81c38f
--- /dev/null
+++ b/regress/sys/kern/unixsockets/Makefile
@@ -0,0 +1,23 @@
+# $OpenBSD: Makefile,v 1.1 2021/12/09 17:25:54 mvs Exp $
+
+# Copyright (c) 2021 Makkoveev Vitaliy <mvs@openbsd.org>
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+WARNINGS = yes
+
+PROGS = undgram_selfconn
+
+CLEANFILES += *.socket
+
+.include <bsd.regress.mk>
diff --git a/regress/sys/kern/unixsockets/undgram_selfconn.c b/regress/sys/kern/unixsockets/undgram_selfconn.c
new file mode 100644
index 00000000000..332188057be
--- /dev/null
+++ b/regress/sys/kern/unixsockets/undgram_selfconn.c
@@ -0,0 +1,81 @@
+/* $OpenBSD: undgram_selfconn.c,v 1.1 2021/12/09 17:25:54 mvs Exp $ */
+
+/*
+ * Copyright (c) 2021 Vitaliy Makkoveev <mvs@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * unix(4) datagram socket could be connected to itself. There are two
+ * cases: temporary connection for sendto(2) syscall and normal connection
+ * provided by connect(2) syscall.
+ * Be sure socket doesn't deadlock itself and doesn't crash kernel while
+ * disconnecting and connecting again.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/un.h>
+#include <stdio.h>
+#include <err.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(void)
+{
+ static struct sockaddr_un sun;
+ int s, buf;
+ ssize_t ret;
+
+ umask(0077);
+
+ memset(&sun, 0, sizeof(sun));
+ sun.sun_len = sizeof(sun);
+ sun.sun_family = AF_UNIX;
+ snprintf(sun.sun_path, sizeof(sun.sun_path) - 1,
+ "undgram_selfconn%d.socket", getpid());
+
+ unlink(sun.sun_path);
+
+ if ((s = socket(AF_UNIX, SOCK_DGRAM|SOCK_NONBLOCK, 0)) < 0)
+ err(1, "socket");
+ if (bind(s, (struct sockaddr *)&sun, sizeof(sun)) < 0)
+ err(1, "bind");
+
+ if (sendto(s, &s, sizeof(s), 0,
+ (struct sockaddr *)&sun, sizeof(sun)) < 0)
+ err(1, "sendto");
+
+ /*
+ * Check received data to be sure the temporary connection
+ * was successful.
+ */
+ if ((ret = recvfrom(s, &buf, sizeof(buf), 0, NULL, NULL)) < 0)
+ err(1, "recvfrom");
+ if (ret != sizeof(s))
+ errx(1, "recvfrom: wrong size");
+ if (buf != s)
+ errx(1, "recvfrom: wrong data");
+
+ if (connect(s, (struct sockaddr *)&sun, sizeof(sun)) < 0)
+ err(1, "connect");
+ /* Disconnect and connect it again */
+ if (connect(s, (struct sockaddr *)&sun, sizeof(sun)) < 0)
+ err(1, "connect");
+ close(s);
+
+ return 0;
+}