1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
// SPDX-License-Identifier: GPL-2.0
/* Use /dev/watch_queue to watch for notifications.
*
* Copyright (C) 2019 Red Hat, Inc. All Rights Reserved.
* Written by David Howells (dhowells@redhat.com)
*/
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <poll.h>
#include <limits.h>
#include <linux/watch_queue.h>
#include <linux/unistd.h>
#include <linux/keyctl.h>
#ifndef KEYCTL_WATCH_KEY
#define KEYCTL_WATCH_KEY -1
#endif
#ifndef __NR_watch_devices
#define __NR_watch_devices -1
#endif
#define BUF_SIZE 4
static long keyctl_watch_key(int key, int watch_fd, int watch_id)
{
return syscall(__NR_keyctl, KEYCTL_WATCH_KEY, key, watch_fd, watch_id);
}
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 void saw_key_change(struct watch_notification *n)
{
struct key_notification *k = (struct key_notification *)n;
unsigned int len = (n->info & WATCH_INFO_LENGTH) >> WATCH_INFO_LENGTH__SHIFT;
if (len != sizeof(struct key_notification) / WATCH_LENGTH_GRANULARITY)
return;
printf("KEY %08x change=%u[%s] aux=%u\n",
k->key_id, n->subtype, key_subtypes[n->subtype], k->aux);
}
static const char *block_subtypes[256] = {
[NOTIFY_BLOCK_ERROR_TIMEOUT] = "timeout",
[NOTIFY_BLOCK_ERROR_NO_SPACE] = "critical space allocation",
[NOTIFY_BLOCK_ERROR_RECOVERABLE_TRANSPORT] = "recoverable transport",
[NOTIFY_BLOCK_ERROR_CRITICAL_TARGET] = "critical target",
[NOTIFY_BLOCK_ERROR_CRITICAL_NEXUS] = "critical nexus",
[NOTIFY_BLOCK_ERROR_CRITICAL_MEDIUM] = "critical medium",
[NOTIFY_BLOCK_ERROR_PROTECTION] = "protection",
[NOTIFY_BLOCK_ERROR_KERNEL_RESOURCE] = "kernel resource",
[NOTIFY_BLOCK_ERROR_DEVICE_RESOURCE] = "device resource",
[NOTIFY_BLOCK_ERROR_IO] = "I/O",
};
static void saw_block_change(struct watch_notification *n)
{
struct block_notification *b = (struct block_notification *)n;
unsigned int len = (n->info & WATCH_INFO_LENGTH) >> WATCH_INFO_LENGTH__SHIFT;
if (len < sizeof(struct block_notification) / WATCH_LENGTH_GRANULARITY)
return;
printf("BLOCK %08llx e=%u[%s] s=%llx\n",
(unsigned long long)b->dev,
n->subtype, block_subtypes[n->subtype],
(unsigned long long)b->sector);
}
static const char *usb_subtypes[256] = {
[NOTIFY_USB_DEVICE_ADD] = "dev-add",
[NOTIFY_USB_DEVICE_REMOVE] = "dev-remove",
[NOTIFY_USB_DEVICE_RESET] = "dev-reset",
[NOTIFY_USB_DEVICE_ERROR] = "dev-error",
};
static void saw_usb_event(struct watch_notification *n)
{
struct usb_notification *u = (struct usb_notification *)n;
unsigned int len = (n->info & WATCH_INFO_LENGTH) >> WATCH_INFO_LENGTH__SHIFT;
if (len < sizeof(struct usb_notification) / WATCH_LENGTH_GRANULARITY)
return;
printf("USB %*.*s %s e=%x r=%x\n",
u->name_len, u->name_len, u->name,
usb_subtypes[n->subtype],
u->error, u->reserved);
}
/*
* Consume and display events.
*/
static int consumer(int fd, struct watch_queue_buffer *buf)
{
struct watch_notification *n;
struct pollfd p[1];
unsigned int head, tail, mask = buf->meta.mask;
for (;;) {
p[0].fd = fd;
p[0].events = POLLIN | POLLERR;
p[0].revents = 0;
if (poll(p, 1, -1) == -1) {
perror("poll");
break;
}
printf("ptrs h=%x t=%x m=%x\n",
buf->meta.head, buf->meta.tail, buf->meta.mask);
while (head = __atomic_load_n(&buf->meta.head, __ATOMIC_ACQUIRE),
tail = buf->meta.tail,
tail != head
) {
n = &buf->slots[tail & mask];
printf("NOTIFY[%08x-%08x] ty=%04x sy=%04x i=%08x\n",
head, tail, n->type, n->subtype, n->info);
if ((n->info & WATCH_INFO_LENGTH) == 0)
goto out;
switch (n->type) {
case WATCH_TYPE_META:
if (n->subtype == WATCH_META_REMOVAL_NOTIFICATION)
printf("REMOVAL of watchpoint %08x\n",
(n->info & WATCH_INFO_ID) >>
WATCH_INFO_ID__SHIFT);
break;
case WATCH_TYPE_KEY_NOTIFY:
saw_key_change(n);
break;
case WATCH_TYPE_BLOCK_NOTIFY:
saw_block_change(n);
break;
case WATCH_TYPE_USB_NOTIFY:
saw_usb_event(n);
break;
}
tail += (n->info & WATCH_INFO_LENGTH) >> WATCH_INFO_LENGTH__SHIFT;
__atomic_store_n(&buf->meta.tail, tail, __ATOMIC_RELEASE);
}
}
out:
return 0;
}
static struct watch_notification_filter filter = {
.nr_filters = 3,
.__reserved = 0,
.filters = {
[0] = {
.type = WATCH_TYPE_KEY_NOTIFY,
.subtype_filter[0] = UINT_MAX,
},
[1] = {
.type = WATCH_TYPE_BLOCK_NOTIFY,
.subtype_filter[0] = UINT_MAX,
},
[2] = {
.type = WATCH_TYPE_USB_NOTIFY,
.subtype_filter[0] = UINT_MAX,
},
},
};
int main(int argc, char **argv)
{
struct watch_queue_buffer *buf;
size_t page_size;
int fd;
fd = open("/dev/watch_queue", O_RDWR);
if (fd == -1) {
perror("/dev/watch_queue");
exit(1);
}
if (ioctl(fd, IOC_WATCH_QUEUE_SET_SIZE, BUF_SIZE) == -1) {
perror("/dev/watch_queue(size)");
exit(1);
}
if (ioctl(fd, IOC_WATCH_QUEUE_SET_FILTER, &filter) == -1) {
perror("/dev/watch_queue(filter)");
exit(1);
}
page_size = sysconf(_SC_PAGESIZE);
buf = mmap(NULL, BUF_SIZE * page_size, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
perror("mmap");
exit(1);
}
if (keyctl_watch_key(KEY_SPEC_SESSION_KEYRING, fd, 0x01) == -1) {
perror("keyctl");
exit(1);
}
if (syscall(__NR_watch_devices, fd, 0x04, 0) == -1) {
perror("watch_devices");
exit(1);
}
return consumer(fd, buf);
}
|