From da56e23dcfec3003fb4cc2736138eb0ccd9fc705 Mon Sep 17 00:00:00 2001
From: Patrick Mooney <pmooney@pfmooney.com>
Date: Tue, 9 Aug 2022 20:57:06 +0000
Subject: [PATCH 2/9] XXX: starting tests

---
 usr/src/test/os-tests/tests/signalfd/Makefile | 54 +++++++++++
 .../test/os-tests/tests/signalfd/basic_args.c | 91 +++++++++++++++++++
 usr/src/test/os-tests/tests/signalfd/common.c | 38 ++++++++
 3 files changed, 183 insertions(+)
 create mode 100644 usr/src/test/os-tests/tests/signalfd/Makefile
 create mode 100644 usr/src/test/os-tests/tests/signalfd/basic_args.c
 create mode 100644 usr/src/test/os-tests/tests/signalfd/common.c

diff --git a/usr/src/test/os-tests/tests/signalfd/Makefile b/usr/src/test/os-tests/tests/signalfd/Makefile
new file mode 100644
index 0000000000..69237d4f03
--- /dev/null
+++ b/usr/src/test/os-tests/tests/signalfd/Makefile
@@ -0,0 +1,54 @@
+#
+# This file and its contents are supplied under the terms of the
+# Common Development and Distribution License ("CDDL"), version 1.0.
+# You may only use this file in accordance with the terms of version
+# 1.0 of the CDDL.
+#
+# A full copy of the text of the CDDL should have accompanied this
+# source.  A copy of the CDDL is also available via the Internet at
+# http://www.illumos.org/license/CDDL.
+#
+
+#
+# Copyright 2022 Oxide Computer Company
+#
+
+include $(SRC)/cmd/Makefile.cmd
+include $(SRC)/test/Makefile.com
+
+PROGS = basic_args
+COMMON_OBJS = common.o
+
+poll_test := LDLIBS += -lsocket
+poll_test.ln := LDLIBS += -lsocket
+CSTD = $(CSTD_GNU99)
+
+ROOTOPTDIR = $(ROOT)/opt/os-tests/tests
+ROOTOPTSIGFD = $(ROOTOPTDIR)/signalfd
+
+CMDS = $(PROGS:%=$(ROOTOPTSIGFD)/%)
+$(CMDS) := FILEMODE = 0555
+
+all: $(PROGS)
+
+install: all $(CMDS)
+
+clobber: clean
+	-$(RM) $(PROGS)
+
+clean:
+	-$(RM) $(COMMON_OBJS)
+
+$(CMDS): $(TESTDIR) $(PROGS)
+
+$(PROGS): $(COMMON_OBJS)
+
+%: %.c
+	$(LINK.c) -o $@ $< $(COMMON_OBJS) $(LDLIBS)
+	$(POST_PROCESS)
+
+$(ROOTOPTSIGFD):
+	$(INS.dir)
+
+$(ROOTOPTSIGFD)/%: %
+	$(INS.file)
diff --git a/usr/src/test/os-tests/tests/signalfd/basic_args.c b/usr/src/test/os-tests/tests/signalfd/basic_args.c
new file mode 100644
index 0000000000..841ecdc3cb
--- /dev/null
+++ b/usr/src/test/os-tests/tests/signalfd/basic_args.c
@@ -0,0 +1,91 @@
+/*
+ * This file and its contents are supplied under the terms of the
+ * Common Development and Distribution License ("CDDL"), version 1.0.
+ * You may only use this file in accordance with the terms of version
+ * 1.0 of the CDDL.
+ *
+ * A full copy of the text of the CDDL should have accompanied this
+ * source.  A copy of the CDDL is also available via the Internet at
+ * http://www.illumos.org/license/CDDL.
+ */
+
+/*
+ * Copyright 2022 Oxide Computer Company
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <assert.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <sys/signalfd.h>
+
+#include "common.h"
+
+int
+main(void)
+{
+	int res, err, fd;
+
+	/* Open with a bad signal mask pointer */
+	res = signalfd(-1, NULL, 0);
+	err = errno;
+	if (res != -1 || err != EFAULT) {
+		test_fail("expected EFAULT for NULL signal mask"
+		    ", found res=%d errno=%d", res, err);
+	}
+
+	sigset_t mask;
+	assert(sigemptyset(&mask) == 0);
+
+	/* Open with bad flags */
+	res = signalfd(-1, &mask, ~0);
+	err = errno;
+	if (res != -1 || err != EINVAL) {
+		test_fail("expected EINVAL bad flags"
+		    ", found res=%d errno=%d", res, err);
+	}
+
+	/* Open basic instance and confirm empty flags */
+	res = signalfd(-1, &mask, 0);
+	err = errno;
+	if (res < 0) {
+		test_fail("failed to open signalfd, found res=%d errno=%d",
+		    res, err);
+	}
+	fd = res;
+	res = fcntl(fd, F_GETFL, 0);
+	assert(res >= 0);
+	if ((res & O_NONBLOCK) != 0) {
+		test_fail("expected no O_NONBLOCK, found flags=0x%x", res);
+	}
+	res = fcntl(fd, F_GETFD, 0);
+	assert(res >= 0);
+	if ((res & FD_CLOEXEC) != 0) {
+		test_fail("expected no FD_CLOEXEC, found fdflags=0x%x", res);
+	}
+	(void) close(fd);
+
+	/* Open with NONBLOCK and CLOEXEC, and confirm flags */
+	res = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
+	err = errno;
+	if (res < 0) {
+		test_fail("failed to open signalfd, found res=%d errno=%d",
+		    res, err);
+	}
+	fd = res;
+	res = fcntl(fd, F_GETFL, 0);
+	assert(res >= 0);
+	if ((res & O_NONBLOCK) == 0) {
+		test_fail("missing O_NONBLOCK, found flags=0x%x", res);
+	}
+	res = fcntl(fd, F_GETFD, 0);
+	assert(res >= 0);
+	if ((res & FD_CLOEXEC) == 0) {
+		test_fail("missing FD_CLOEXEC, found fdflags=0x%x", res);
+	}
+	(void) close(fd);
+
+	test_pass();
+}
diff --git a/usr/src/test/os-tests/tests/signalfd/common.c b/usr/src/test/os-tests/tests/signalfd/common.c
new file mode 100644
index 0000000000..6016f4a450
--- /dev/null
+++ b/usr/src/test/os-tests/tests/signalfd/common.c
@@ -0,0 +1,38 @@
+/*
+ * This file and its contents are supplied under the terms of the
+ * Common Development and Distribution License ("CDDL"), version 1.0.
+ * You may only use this file in accordance with the terms of version
+ * 1.0 of the CDDL.
+ *
+ * A full copy of the text of the CDDL should have accompanied this
+ * source.  A copy of the CDDL is also available via the Internet at
+ * http://www.illumos.org/license/CDDL.
+ */
+
+/*
+ * Copyright 2022 Oxide Computer Company
+ */
+
+#include <err.h>
+#include <string.h>
+#include <stdlib.h>
+
+void
+test_fail(const char *fmt, ...)
+{
+	va_list args;
+	char *fmt_hdr = NULL;
+
+	va_start(args, fmt);
+	(void) asprintf(&fmt_hdr, "TEST FAILED - %s", fmt);
+	verrx(EXIT_FAILURE, fmt_hdr, args);
+	/* NOTREATCHED */
+	va_end(args);
+}
+
+void
+test_pass(void)
+{
+	errx(EXIT_SUCCESS, "TEST PASSED");
+	/* NOTREATCHED */
+}
-- 
2.37.3

