test/helpers: add ring creation helpers

Particularly useful for SQPOLL, where we can choose to skip a test
if we don't have the necessary privileges to setup the ring.

Signed-off-by: Jens Axboe <[email protected]>
diff --git a/test/Makefile b/test/Makefile
index 69de572..f02bbd1 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -137,13 +137,13 @@
 all: ${helpers} $(test_targets)
 
 helpers.o: helpers.c helpers.c
-	$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
+	$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $< -luring
 
 %: %.c ${helpers} helpers.h
-	$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $< -luring $(XCFLAGS) ${helpers}
+	$(QUIET_CC)$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $< ${helpers} -luring $(XCFLAGS)
 
 %: %.cc ${helpers} helpers.h
-	$(QUIET_CXX)$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $< -luring $(XCFLAGS) ${helpers}
+	$(QUIET_CXX)$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $< ${helpers} -luring $(XCFLAGS)
 
 test_srcs := \
 	helpers.c \
diff --git a/test/helpers.c b/test/helpers.c
index ea705fb..930d82a 100644
--- a/test/helpers.c
+++ b/test/helpers.c
@@ -8,6 +8,7 @@
 #include <stdio.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <sys/types.h>
 
 #include "helpers.h"
 #include "liburing.h"
@@ -83,3 +84,33 @@
 	}
 	return vecs;
 }
+
+/*
+ * Helper for setting up an io_uring instance, skipping if the given user isn't
+ * allowed to.
+ */
+enum t_setup_ret t_create_ring_params(int depth, struct io_uring *ring,
+				      struct io_uring_params *p)
+{
+	int ret;
+
+	ret = io_uring_queue_init_params(depth, ring, p);
+	if (!ret)
+		return T_SETUP_OK;
+	if ((p->flags & IORING_SETUP_SQPOLL) && ret == -EPERM && geteuid()) {
+		fprintf(stdout, "SQPOLL skipped for regular user\n");
+		return T_SETUP_SKIP;
+	}
+
+	fprintf(stderr, "queue_init: %s\n", strerror(-ret));
+	return ret;
+}
+
+enum t_setup_ret t_create_ring(int depth, struct io_uring *ring,
+			       unsigned int flags)
+{
+	struct io_uring_params p = { };
+
+	p.flags = flags;
+	return t_create_ring_params(depth, ring, &p);
+}
diff --git a/test/helpers.h b/test/helpers.h
index afe157c..74fe162 100644
--- a/test/helpers.h
+++ b/test/helpers.h
@@ -9,6 +9,13 @@
 extern "C" {
 #endif
 
+#include "liburing.h"
+
+enum t_setup_ret {
+	T_SETUP_OK	= 0,
+	T_SETUP_SKIP,
+};
+
 /*
  * Helper for allocating memory in tests.
  */
@@ -38,6 +45,15 @@
  * with @buf_size bytes buffer of each iovec.
  */
 struct iovec *t_create_buffers(size_t buf_num, size_t buf_size);
+
+/*
+ * Helper for setting up a ring and checking for user privs
+ */
+enum t_setup_ret t_create_ring_params(int depth, struct io_uring *ring,
+				      struct io_uring_params *p);
+enum t_setup_ret t_create_ring(int depth, struct io_uring *ring,
+			       unsigned int flags);
+
 #ifdef __cplusplus
 }
 #endif