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