1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*\
7f08c3bdfSopenharmony_ci * [Description]
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * Test if key watch removal is correctly recognized by watch queue.
10f08c3bdfSopenharmony_ci */
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#define _GNU_SOURCE
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include "tst_test.h"
15f08c3bdfSopenharmony_ci#include "lapi/keyctl.h"
16f08c3bdfSopenharmony_ci#include "common.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic void saw_watch_removal(struct watch_notification *n,
19f08c3bdfSopenharmony_ci			      LTP_ATTRIBUTE_UNUSED size_t len,
20f08c3bdfSopenharmony_ci			      unsigned int wtype)
21f08c3bdfSopenharmony_ci{
22f08c3bdfSopenharmony_ci	if (wtype != WATCH_TYPE_META)
23f08c3bdfSopenharmony_ci		return;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	if (n->subtype == WATCH_META_REMOVAL_NOTIFICATION)
26f08c3bdfSopenharmony_ci		tst_res(TPASS, "Meta removal notification received");
27f08c3bdfSopenharmony_ci	else
28f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Event not recognized");
29f08c3bdfSopenharmony_ci}
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void run(void)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	int fd;
34f08c3bdfSopenharmony_ci	key_serial_t key;
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	fd = wqueue_watch(256, &wqueue_filter);
37f08c3bdfSopenharmony_ci	key = wqueue_add_key(fd);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	/* if watch_id = -1 key is removed from the watch queue */
40f08c3bdfSopenharmony_ci	keyctl(KEYCTL_WATCH_KEY, key, fd, -1);
41f08c3bdfSopenharmony_ci	wqueue_consumer(fd, saw_watch_removal);
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic struct tst_test test = {
47f08c3bdfSopenharmony_ci	.test_all = run,
48f08c3bdfSopenharmony_ci};
49