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 * Fill the watch queue and wait for a notification loss.
10f08c3bdfSopenharmony_ci */
11f08c3bdfSopenharmony_ci
12f08c3bdfSopenharmony_ci#define _GNU_SOURCE
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <unistd.h>
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci#include "lapi/keyctl.h"
17f08c3bdfSopenharmony_ci#include "common.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#define WATCH_QUEUE_NOTE_SIZE 128
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic int data_lost;
22f08c3bdfSopenharmony_cistatic key_serial_t key;
23f08c3bdfSopenharmony_cistatic int fd;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic void saw_data_loss(struct watch_notification *n,
26f08c3bdfSopenharmony_ci			  LTP_ATTRIBUTE_UNUSED size_t len, unsigned int wtype)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	if (wtype != WATCH_TYPE_META)
29f08c3bdfSopenharmony_ci		return;
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	if (n->subtype == WATCH_META_LOSS_NOTIFICATION)
32f08c3bdfSopenharmony_ci		data_lost = 1;
33f08c3bdfSopenharmony_ci}
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic void setup(void)
36f08c3bdfSopenharmony_ci{
37f08c3bdfSopenharmony_ci	fd = wqueue_watch(1, &wqueue_filter);
38f08c3bdfSopenharmony_ci	key = wqueue_add_key(fd);
39f08c3bdfSopenharmony_ci}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void run(void)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	int i, iterations;
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	iterations = (getpagesize() / WATCH_QUEUE_NOTE_SIZE) * 2;
46f08c3bdfSopenharmony_ci	for (i = 0; i < iterations; i++)
47f08c3bdfSopenharmony_ci		keyctl(KEYCTL_UPDATE, key, "b", 1);
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	data_lost = 0;
50f08c3bdfSopenharmony_ci	while (!data_lost)
51f08c3bdfSopenharmony_ci		wqueue_consumer(fd, saw_data_loss);
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	if (data_lost)
54f08c3bdfSopenharmony_ci		tst_res(TPASS, "Meta loss notification received");
55f08c3bdfSopenharmony_ci	else
56f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Event not recognized");
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic void cleanup(void)
60f08c3bdfSopenharmony_ci{
61f08c3bdfSopenharmony_ci	keyctl(KEYCTL_REVOKE, key);
62f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
63f08c3bdfSopenharmony_ci}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cistatic struct tst_test test = {
66f08c3bdfSopenharmony_ci	.test_all = run,
67f08c3bdfSopenharmony_ci	.setup = setup,
68f08c3bdfSopenharmony_ci	.cleanup = cleanup,
69f08c3bdfSopenharmony_ci};
70