Skip to contentSkip to conversation

Add wqueue01 test

PATCH In review Andrea Cervesato 3 participants 3 messages Jan 7, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Add wqueue01 test

This test is testing NOTIFY_KEY_UPDATED event

---
 testcases/kernel/Makefile              |   1 +
 testcases/kernel/watchqueue/.gitignore |   9 ++
 testcases/kernel/watchqueue/Makefile   |   9 ++
 testcases/kernel/watchqueue/common.h   | 132 +++++++++++++++++++++++++
 testcases/kernel/watchqueue/wqueue01.c |  41 ++++++++
 5 files changed, 192 insertions(+)
 create mode 100644 testcases/kernel/watchqueue/.gitignore
 create mode 100644 testcases/kernel/watchqueue/Makefile
 create mode 100644 testcases/kernel/watchqueue/common.h
 create mode 100644 testcases/kernel/watchqueue/wqueue01.c
testcases/kernel/Makefile +1 -0
index 4604f1f38..d44856c57 100644
@@ -38,6 +38,7 @@ SUBDIRS			+= connectors \
 			   sound \
 			   tracing \
 			   uevents \
+			   watchqueue \
 
 ifeq ($(WITH_POWER_MANAGEMENT_TESTSUITE),yes)
 SUBDIRS			+= power_management
testcases/kernel/watchqueue/.gitignore new file +9 -0
new file mode 100644
index 000000000..dcfcd8272
@@ -0,0 +1,9 @@
+wqueue01
+wqueue02
+wqueue03
+wqueue04
+wqueue05
+wqueue06
+wqueue07
+wqueue08
+wqueue09
testcases/kernel/watchqueue/Makefile new file +9 -0
new file mode 100644
index 000000000..8a9e4b32f
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+top_srcdir			?= ../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+LDLIBS += $(KEYUTILS_LIBS)
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/watchqueue/common.h new file +132 -0

132 lines not on this page · raw message

testcases/kernel/watchqueue/wqueue01.c new file +41 -0

41 lines not on this page · raw message

Signature · 5 lines
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi!

Quoted 46 lines
testcases/kernel/watchqueue/common.h new file +40 -0
new file mode 100644
index 000000000..97e210a29
@@ -0,0 +1,132 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+#ifndef WQUEUE_COMMON_H__
+#define WQUEUE_COMMON_H__
+
+#include "tst_test.h"
+#include "lapi/watch_queue.h"
+#include "lapi/keyctl.h"
+
+static struct watch_notification_filter wqueue_filter = {
+	.nr_filters	= 2,
+	.filters = {
+		[0]	= {
+			.type			= WATCH_TYPE_META,
+			.subtype_filter[0]	= UINT_MAX,
+		},
+		[1]	= {
+			.type			= WATCH_TYPE_KEY_NOTIFY,
+			.subtype_filter[0]	= UINT_MAX,
+		},
+	},
+};
+
+static const char *key_subtypes[256] = {
+	[NOTIFY_KEY_INSTANTIATED]	= "instantiated",
+	[NOTIFY_KEY_UPDATED]		= "updated",
+	[NOTIFY_KEY_LINKED]		= "linked",
+	[NOTIFY_KEY_UNLINKED]		= "unlinked",
+	[NOTIFY_KEY_CLEARED]		= "cleared",
+	[NOTIFY_KEY_REVOKED]		= "revoked",
+	[NOTIFY_KEY_INVALIDATED]	= "invalidated",
+	[NOTIFY_KEY_SETATTR]		= "setattr",
+};
+
+static inline int wqueue_key_event(struct watch_notification *n, size_t len, unsigned int wtype, int type)
+{
+	struct key_notification *k;

testcases/kernel/watchqueue/common.h line 50

Quoted 10 lines
testcases/kernel/watchqueue/common.h new file +10 -0
+	const char* msg;
+
+	if (wtype != WATCH_TYPE_KEY_NOTIFY)
+		return 0;
+
+	if (len != sizeof(struct key_notification))
+		tst_brk(TBROK, "Incorrect key message length");
+
+	k = (struct key_notification *)n;
+	msg = key_subtypes[n->subtype];

Actually we have no guarantee that the subtype is not pointing outside of the array, do we?

I tend to write the code that converts flags to names as a function with a switch:

	switch (foo) {
	case FOO_A:
		retrun "FOO_A";
	case FOO_B:
		return "FOO_B";
	default:
		return "Invalid value?!";
	}

which does not suffer this problem. Otherwise you would have to check the bounds before attempts to get the value.

testcases/kernel/watchqueue/common.h line 58

Quoted 7 lines
testcases/kernel/watchqueue/common.h new file +7 -0
+	tst_res(TINFO, "KEY %08x change=%u[%s] aux=%u",
+	    k->key_id, n->subtype, msg, k->aux);
+
+	if (n->subtype == type)
+        return 1;
+
+    return 0;

Wrong indentation here.

testcases/kernel/watchqueue/common.h line 70

Quoted 12 lines
testcases/kernel/watchqueue/common.h new file +12 -0
+}
+
+static inline key_serial_t wqueue_add_key(int fd)
+{
+	key_serial_t key;
+
+	key = add_key("user", "ltptestkey", "a", 1, KEY_SPEC_SESSION_KEYRING);
+	if (key == -1)
+		tst_brk(TBROK, "add_key error: %s", tst_strerrno(errno));
+
+	keyctl(KEYCTL_WATCH_KEY, key, fd, 0x01);
+	keyctl(KEYCTL_WATCH_KEY, KEY_SPEC_SESSION_KEYRING, fd, 0x02);

The KEYCTL_WATCH_KEY is recent addition you should add a fallback definition to the lapi/keyctl.h as well.

Quoted 90 lines
testcases/kernel/watchqueue/common.h new file +61 -0
+	return key;
+}
+
+static inline int wqueue_watch(int buf_size, struct watch_notification_filter *filter)
+{
+	int pipefd[2];
+	int fd;
+
+	SAFE_PIPE2(pipefd, O_NOTIFICATION_PIPE);
+
+	fd = pipefd[0];
+
+	SAFE_IOCTL(fd, IOC_WATCH_QUEUE_SET_SIZE, buf_size);
+	SAFE_IOCTL(fd, IOC_WATCH_QUEUE_SET_FILTER, filter);
+
+	return fd;
+}
+
+typedef void (*wqueue_callback) (struct watch_notification *n, size_t len, unsigned int wtype);
+
+static void wqueue_consumer(int fd, wqueue_callback cb)
+{
+	unsigned char buffer[433], *p, *end;
+	union {
+		struct watch_notification n;
+		unsigned char buf1[128];
+	} n;
+	ssize_t buf_len;
+
+	tst_res(TINFO, "Reading watch queue events");
+
+	buf_len = SAFE_READ(0, fd, buffer, sizeof(buffer));
+
+	p = buffer;
+	end = buffer + buf_len;
+	while (p < end) {
+		size_t largest, len;
+
+		largest = end - p;
+		if (largest > 128)
+			largest = 128;
+
+		if (largest < sizeof(struct watch_notification))
+			tst_brk(TBROK, "Short message header: %zu", largest);
+
+		memcpy(&n, p, largest);
+
+		tst_res(TINFO, "NOTIFY[%03zx]: ty=%06x sy=%02x i=%08x",
+				p - buffer, n.n.type, n.n.subtype, n.n.info);
+
+		len = n.n.info & WATCH_INFO_LENGTH;
+		if (len < sizeof(n.n) || len > largest)
+			tst_brk(TBROK, "Bad message length: %zu/%zu", len, largest);
+
+		cb(&n.n, len, n.n.type);
+
+		p += len;
+	}
+}
+
+#endif
testcases/kernel/watchqueue/wqueue01.c new file +23 -0
new file mode 100644
index 000000000..33a9de3db
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if keyctl update is correctly recognized by watch queue.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static void saw_key_updated(struct watch_notification *n, size_t len, unsigned int wtype)
+{
+	if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_UPDATED))
+		tst_res(TPASS, "keyctl update has been recognized");
+	else
+		tst_res(TFAIL, "keyctl update has not been recognized");
+}
+

testcases/kernel/watchqueue/wqueue01.c line 33

Quoted 10 lines
testcases/kernel/watchqueue/wqueue01.c new file +10 -0
+static void run(void)
+{
+	int fd;
+	key_serial_t key;
+
+	fd = wqueue_watch(256, &wqueue_filter);
+	key = wqueue_add_key(fd);
+
+	keyctl(KEYCTL_UPDATE, key, "b", 1);
+	wqueue_consumer(fd, saw_key_updated);

For some reason I do not like the callback here, but I guess that anything else would be a bit more complicated, so we may as well live with it.

testcases/kernel/watchqueue/wqueue01.c line 40

Quoted 6 lines
testcases/kernel/watchqueue/wqueue01.c new file +6 -0
+	SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.needs_root = 1,

What do we need the root for?

Quoted 7 lines
testcases/kernel/watchqueue/wqueue01.c new file +1 -0
+};
Signature · 5 lines
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hi !

Quoted 72 lines · Cyril Hrubis

Hi!

Quoted 46 lines
testcases/kernel/watchqueue/common.h new file +40 -0
new file mode 100644
index 000000000..97e210a29
@@ -0,0 +1,132 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+#ifndef WQUEUE_COMMON_H__
+#define WQUEUE_COMMON_H__
+
+#include "tst_test.h"
+#include "lapi/watch_queue.h"
+#include "lapi/keyctl.h"
+
+static struct watch_notification_filter wqueue_filter = {
+	.nr_filters	= 2,
+	.filters = {
+		[0]	= {
+			.type			= WATCH_TYPE_META,
+			.subtype_filter[0]	= UINT_MAX,
+		},
+		[1]	= {
+			.type			= WATCH_TYPE_KEY_NOTIFY,
+			.subtype_filter[0]	= UINT_MAX,
+		},
+	},
+};
+
+static const char *key_subtypes[256] = {
+	[NOTIFY_KEY_INSTANTIATED]	= "instantiated",
+	[NOTIFY_KEY_UPDATED]		= "updated",
+	[NOTIFY_KEY_LINKED]		= "linked",
+	[NOTIFY_KEY_UNLINKED]		= "unlinked",
+	[NOTIFY_KEY_CLEARED]		= "cleared",
+	[NOTIFY_KEY_REVOKED]		= "revoked",
+	[NOTIFY_KEY_INVALIDATED]	= "invalidated",
+	[NOTIFY_KEY_SETATTR]		= "setattr",
+};
+
+static inline int wqueue_key_event(struct watch_notification *n, size_t len, unsigned int wtype, int type)
+{
+	struct key_notification *k;

testcases/kernel/watchqueue/common.h line 50

Quoted 10 lines
testcases/kernel/watchqueue/common.h new file +10 -0
+	const char* msg;
+
+	if (wtype != WATCH_TYPE_KEY_NOTIFY)
+		return 0;
+
+	if (len != sizeof(struct key_notification))
+		tst_brk(TBROK, "Incorrect key message length");
+
+	k = (struct key_notification *)n;
+	msg = key_subtypes[n->subtype];

Actually we have no guarantee that the subtype is not pointing outside of the array, do we?

I tend to write the code that converts flags to names as a function with a switch:

	switch (foo) {
	case FOO_A:
		retrun "FOO_A";
	case FOO_B:
		return "FOO_B";
	default:
		return "Invalid value?!";
	}
Quoted 2 lines

which does not suffer this problem. Otherwise you would have to check the bounds before attempts to get the value.

Sure

Quoted 9 lines

testcases/kernel/watchqueue/common.h line 58

Quoted 7 lines
testcases/kernel/watchqueue/common.h new file +7 -0
+	tst_res(TINFO, "KEY %08x change=%u[%s] aux=%u",
+	    k->key_id, n->subtype, msg, k->aux);
+
+	if (n->subtype == type)
+        return 1;
+
+    return 0;

Wrong indentation here.

Quoted 14 lines

testcases/kernel/watchqueue/common.h line 70

Quoted 12 lines
testcases/kernel/watchqueue/common.h new file +12 -0
+}
+
+static inline key_serial_t wqueue_add_key(int fd)
+{
+	key_serial_t key;
+
+	key = add_key("user", "ltptestkey", "a", 1, KEY_SPEC_SESSION_KEYRING);
+	if (key == -1)
+		tst_brk(TBROK, "add_key error: %s", tst_strerrno(errno));
+
+	keyctl(KEYCTL_WATCH_KEY, key, fd, 0x01);
+	keyctl(KEYCTL_WATCH_KEY, KEY_SPEC_SESSION_KEYRING, fd, 0x02);

The KEYCTL_WATCH_KEY is recent addition you should add a fallback definition to the lapi/keyctl.h as well.

Alright

Quoted 104 lines
Quoted 90 lines
testcases/kernel/watchqueue/common.h new file +61 -0
+	return key;
+}
+
+static inline int wqueue_watch(int buf_size, struct watch_notification_filter *filter)
+{
+	int pipefd[2];
+	int fd;
+
+	SAFE_PIPE2(pipefd, O_NOTIFICATION_PIPE);
+
+	fd = pipefd[0];
+
+	SAFE_IOCTL(fd, IOC_WATCH_QUEUE_SET_SIZE, buf_size);
+	SAFE_IOCTL(fd, IOC_WATCH_QUEUE_SET_FILTER, filter);
+
+	return fd;
+}
+
+typedef void (*wqueue_callback) (struct watch_notification *n, size_t len, unsigned int wtype);
+
+static void wqueue_consumer(int fd, wqueue_callback cb)
+{
+	unsigned char buffer[433], *p, *end;
+	union {
+		struct watch_notification n;
+		unsigned char buf1[128];
+	} n;
+	ssize_t buf_len;
+
+	tst_res(TINFO, "Reading watch queue events");
+
+	buf_len = SAFE_READ(0, fd, buffer, sizeof(buffer));
+
+	p = buffer;
+	end = buffer + buf_len;
+	while (p < end) {
+		size_t largest, len;
+
+		largest = end - p;
+		if (largest > 128)
+			largest = 128;
+
+		if (largest < sizeof(struct watch_notification))
+			tst_brk(TBROK, "Short message header: %zu", largest);
+
+		memcpy(&n, p, largest);
+
+		tst_res(TINFO, "NOTIFY[%03zx]: ty=%06x sy=%02x i=%08x",
+				p - buffer, n.n.type, n.n.subtype, n.n.info);
+
+		len = n.n.info & WATCH_INFO_LENGTH;
+		if (len < sizeof(n.n) || len > largest)
+			tst_brk(TBROK, "Bad message length: %zu/%zu", len, largest);
+
+		cb(&n.n, len, n.n.type);
+
+		p += len;
+	}
+}
+
+#endif
testcases/kernel/watchqueue/wqueue01.c new file +23 -0
new file mode 100644
index 000000000..33a9de3db
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test if keyctl update is correctly recognized by watch queue.
+ */
+
+#include "tst_test.h"
+#include "lapi/keyctl.h"
+#include "common.h"
+
+static void saw_key_updated(struct watch_notification *n, size_t len, unsigned int wtype)
+{
+	if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_UPDATED))
+		tst_res(TPASS, "keyctl update has been recognized");
+	else
+		tst_res(TFAIL, "keyctl update has not been recognized");
+}
+

testcases/kernel/watchqueue/wqueue01.c line 33

Quoted 10 lines
testcases/kernel/watchqueue/wqueue01.c new file +10 -0
+static void run(void)
+{
+	int fd;
+	key_serial_t key;
+
+	fd = wqueue_watch(256, &wqueue_filter);
+	key = wqueue_add_key(fd);
+
+	keyctl(KEYCTL_UPDATE, key, "b", 1);
+	wqueue_consumer(fd, saw_key_updated);

For some reason I do not like the callback here, but I guess that anything else would be a bit more complicated, so we may as well live with it.

Quoted 7 lines

testcases/kernel/watchqueue/wqueue01.c line 40

Quoted 6 lines
testcases/kernel/watchqueue/wqueue01.c new file +6 -0
+	SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.needs_root = 1,

What do we need the root for?

I kept it there after having EPERM using ioctl, but it's not needed indeed.

Quoted 7 lines
Quoted 7 lines
testcases/kernel/watchqueue/wqueue01.c new file +1 -0
+};
Signature · 5 lines
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp