0001
0002
0003
0004
0005
0006
0007
0008 #define _GNU_SOURCE
0009 #include <stdbool.h>
0010 #include <stdarg.h>
0011 #include <stdio.h>
0012 #include <stdlib.h>
0013 #include <string.h>
0014 #include <signal.h>
0015 #include <unistd.h>
0016 #include <errno.h>
0017 #include <sys/ioctl.h>
0018 #include <limits.h>
0019 #include <linux/watch_queue.h>
0020 #include <linux/unistd.h>
0021 #include <linux/keyctl.h>
0022
0023 #ifndef KEYCTL_WATCH_KEY
0024 #define KEYCTL_WATCH_KEY -1
0025 #endif
0026 #ifndef __NR_keyctl
0027 #define __NR_keyctl -1
0028 #endif
0029
0030 #define BUF_SIZE 256
0031
0032 static long keyctl_watch_key(int key, int watch_fd, int watch_id)
0033 {
0034 return syscall(__NR_keyctl, KEYCTL_WATCH_KEY, key, watch_fd, watch_id);
0035 }
0036
0037 static const char *key_subtypes[256] = {
0038 [NOTIFY_KEY_INSTANTIATED] = "instantiated",
0039 [NOTIFY_KEY_UPDATED] = "updated",
0040 [NOTIFY_KEY_LINKED] = "linked",
0041 [NOTIFY_KEY_UNLINKED] = "unlinked",
0042 [NOTIFY_KEY_CLEARED] = "cleared",
0043 [NOTIFY_KEY_REVOKED] = "revoked",
0044 [NOTIFY_KEY_INVALIDATED] = "invalidated",
0045 [NOTIFY_KEY_SETATTR] = "setattr",
0046 };
0047
0048 static void saw_key_change(struct watch_notification *n, size_t len)
0049 {
0050 struct key_notification *k = (struct key_notification *)n;
0051
0052 if (len != sizeof(struct key_notification)) {
0053 fprintf(stderr, "Incorrect key message length\n");
0054 return;
0055 }
0056
0057 printf("KEY %08x change=%u[%s] aux=%u\n",
0058 k->key_id, n->subtype, key_subtypes[n->subtype], k->aux);
0059 }
0060
0061
0062
0063
0064 static void consumer(int fd)
0065 {
0066 unsigned char buffer[433], *p, *end;
0067 union {
0068 struct watch_notification n;
0069 unsigned char buf1[128];
0070 } n;
0071 ssize_t buf_len;
0072
0073 for (;;) {
0074 buf_len = read(fd, buffer, sizeof(buffer));
0075 if (buf_len == -1) {
0076 perror("read");
0077 exit(1);
0078 }
0079
0080 if (buf_len == 0) {
0081 printf("-- END --\n");
0082 return;
0083 }
0084
0085 if (buf_len > sizeof(buffer)) {
0086 fprintf(stderr, "Read buffer overrun: %zd\n", buf_len);
0087 return;
0088 }
0089
0090 printf("read() = %zd\n", buf_len);
0091
0092 p = buffer;
0093 end = buffer + buf_len;
0094 while (p < end) {
0095 size_t largest, len;
0096
0097 largest = end - p;
0098 if (largest > 128)
0099 largest = 128;
0100 if (largest < sizeof(struct watch_notification)) {
0101 fprintf(stderr, "Short message header: %zu\n", largest);
0102 return;
0103 }
0104 memcpy(&n, p, largest);
0105
0106 printf("NOTIFY[%03zx]: ty=%06x sy=%02x i=%08x\n",
0107 p - buffer, n.n.type, n.n.subtype, n.n.info);
0108
0109 len = n.n.info & WATCH_INFO_LENGTH;
0110 if (len < sizeof(n.n) || len > largest) {
0111 fprintf(stderr, "Bad message length: %zu/%zu\n", len, largest);
0112 exit(1);
0113 }
0114
0115 switch (n.n.type) {
0116 case WATCH_TYPE_META:
0117 switch (n.n.subtype) {
0118 case WATCH_META_REMOVAL_NOTIFICATION:
0119 printf("REMOVAL of watchpoint %08x\n",
0120 (n.n.info & WATCH_INFO_ID) >>
0121 WATCH_INFO_ID__SHIFT);
0122 break;
0123 case WATCH_META_LOSS_NOTIFICATION:
0124 printf("-- LOSS --\n");
0125 break;
0126 default:
0127 printf("other meta record\n");
0128 break;
0129 }
0130 break;
0131 case WATCH_TYPE_KEY_NOTIFY:
0132 saw_key_change(&n.n, len);
0133 break;
0134 default:
0135 printf("other type\n");
0136 break;
0137 }
0138
0139 p += len;
0140 }
0141 }
0142 }
0143
0144 static struct watch_notification_filter filter = {
0145 .nr_filters = 1,
0146 .filters = {
0147 [0] = {
0148 .type = WATCH_TYPE_KEY_NOTIFY,
0149 .subtype_filter[0] = UINT_MAX,
0150 },
0151 },
0152 };
0153
0154 int main(int argc, char **argv)
0155 {
0156 int pipefd[2], fd;
0157
0158 if (pipe2(pipefd, O_NOTIFICATION_PIPE) == -1) {
0159 perror("pipe2");
0160 exit(1);
0161 }
0162 fd = pipefd[0];
0163
0164 if (ioctl(fd, IOC_WATCH_QUEUE_SET_SIZE, BUF_SIZE) == -1) {
0165 perror("watch_queue(size)");
0166 exit(1);
0167 }
0168
0169 if (ioctl(fd, IOC_WATCH_QUEUE_SET_FILTER, &filter) == -1) {
0170 perror("watch_queue(filter)");
0171 exit(1);
0172 }
0173
0174 if (keyctl_watch_key(KEY_SPEC_SESSION_KEYRING, fd, 0x01) == -1) {
0175 perror("keyctl");
0176 exit(1);
0177 }
0178
0179 if (keyctl_watch_key(KEY_SPEC_USER_KEYRING, fd, 0x02) == -1) {
0180 perror("keyctl");
0181 exit(1);
0182 }
0183
0184 consumer(fd);
0185 exit(0);
0186 }