KEYCTL_WATCH_KEY(3)
π NAME π
keyctl_watch_key β watch for changes to a key πβ¨
okay so bestie, quick context before we dive in: this lil guy lives in the Linux Key Management Calls section of the manual (section 3, the library-function section, we love her). the header line up at the top of the page literally reads KEYCTL_GRANT_PERMISSION(3), which is a whole different function's name, and honestly? iconic messy behaviour, but we move π«Ά
β¨ SYNOPSIS β¨
#include <keyutils.h>
long keyctl_watch_key(key_serial_t key,
int watch_queue_fd
int watch_id);
(yes the comma situation in there is exactly how the original prints it, no I will not be fixing it, I am a messenger π )
πΈ DESCRIPTION πΈ
keyctl_watch_key() sets or removes a watch on key. that's it, that's the girlboss move. π
watch_id specifies the ID for a watch that will be included in notification messages. it can be between 0 and 255 to add a key; it should be -1 to remove a key. so like, adding = a cute lil number, removing = the dramatic -1, no in-between, periodt.
watch_queue_fd is a file descriptor attached to a watch_queue device instance. Multiple openings of a device provide separate instances. Each device instance can only have one watch on any particular key. so if you try to double-watch the same key from the same instance, that's a whole thing β see EBUSY below, bestie, it's giving possessive π
π¬ Notification Record π¬
Key-specific notification messages that the kernel emits into the buffer have the following format:
struct key_notification {
struct watch_notification watch;
__u32 key_id;
__u32 aux;
};
The watch.type field will be set to WATCH_TYPE_KEY_NOTIFY and the watch.subtype field will contain one of the following constants, indicating the event that occurred β and the watch_id passed to keyctl_watch_key() will be placed in watch.info in the ID field. (so you always know which watch is gossiping to you, fr fr π£οΈ)
The following events are defined:
NOTIFY_KEY_INSTANTIATEDβ This indicates that a watched key got instantiated or negatively instantiated.key_idindicates the key that was instantiated andauxis unused. π£NOTIFY_KEY_UPDATEDβ This indicates that a watched key got updated or instantiated by update.key_idindicates the key that was updated andauxis unused. πNOTIFY_KEY_LINKEDβ This indicates that a key got linked into a watched keyring.key_idindicates the keyring that was modifiedauxindicates the key that was added. πNOTIFY_KEY_UNLINKEDβ This indicates that a key got unlinked from a watched keyring.key_idindicates the keyring that was modifiedauxindicates the key that was removed. βοΈNOTIFY_KEY_CLEAREDβ This indicates that a watched keyring got cleared.key_idindicates the keyring that was cleared andauxis unused. π§ΉNOTIFY_KEY_REVOKEDβ This indicates that a watched key got revoked.key_idindicates the key that was revoked andauxis unused. π«NOTIFY_KEY_INVALIDATEDβ This indicates that a watched key got invalidated.key_idindicates the key that was invalidated andauxis unused. π₯NOTIFY_KEY_SETATTRβ This indicates that a watched key had its attributes (owner, group, permissions, timeout) modified.key_idindicates the key that was modified andauxis unused. π
ποΈ Removal Notification ποΈ
When a watched key is garbage collected, all of its watches are automatically destroyed and a notification is delivered to each watcher. (the key literally got collected, it's over, no cap π)
This will normally be an extended notification of the form:
struct watch_notification_removal {
struct watch_notification watch;
__u64 id;
};
The watch.type field will be set to WATCH_TYPE_META and the watch.subtype field will contain WATCH_META_REMOVAL_NOTIFICATION. If the extended notification is given, then the length will be 2 units, otherwise it will be 1 and only the header will be present.
The watch_id passed to keyctl_watch_key() will be placed in watch.info in the ID field.
If the extension is present, id will be set to the ID of the destroyed key.
π«Ά RETURN VALUE π«Ά
On success keyctl_watch_key() returns 0. On error, the value -1 will be returned and errno will have been set to an appropriate error. the duality of girl π
π ERRORS π
ENOKEYβ The specified key does not exist.EKEYEXPIREDβ The specified key has expired.EKEYREVOKEDβ The specified key has been revoked.EACCESβ The named key exists, but does not grant view permission to the calling process. (it's there, you just can't look, so rude π)EBUSYβ The specified key already has a watch on it for that device instance (add only).EBADSLTβ The specified key doesn't have a watch on it (removal only).
notice how the last two are picky about which direction they apply to β adding and removing have different moods, and the kernel will absolutely let you know π«
π LINKING π
This is a library function that can be found in libkeyutils. When linking, -lkeyutils should be specified to the linker. don't skip this step bestie, the linker will simply not know her π₯
π SEE ALSO π
keyctl(1), add_key(2), keyctl(2), request_key(2), keyctl(3), keyrings(7), keyutils(7)
and that's the whole page, you ate that π β¨
this came to you from Linux, dated 28 Aug 2019, and the footer of the original signs off as KEYCTL_GRANT_PERMISSION(3) (yes, still that name, we've made peace with it π). π
π¦ where this page comes from
- alpine 3.22: keyutils-doc 1.6.3-r4 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- alpine 3.23: keyutils-doc 1.6.3-r4 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- alpine 3.24: keyutils-doc 1.6.3-r4 Β· GPL-2.0-or-later AND LGPL-2.0-or-later
- arch rolling: keyutils 1.6.3-4 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- centos-stream 10: keyutils-libs-devel 1.6.3-5.el10 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- centos-stream 9: keyutils-libs-devel 1.6.3-1.el9 Β· GPLv2+ and LGPLv2+
- centos-stream 9: keyutils-libs-devel 1.6.3-1.el9 Β· GPLv2+ and LGPLv2+
- debian bookworm: libkeyutils-dev 1.6.3-2 Β· GPL-2.0-or-later
- debian trixie: libkeyutils-dev 1.6.3-6 Β· GPL-2.0-or-later
- fedora 43: keyutils-libs-devel 1.6.3-6.fc43 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- fedora 43: keyutils-libs-devel 1.6.3-6.fc43 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- fedora 44: keyutils-libs-devel 1.6.3-7.fc44 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- fedora 44: keyutils-libs-devel 1.6.3-7.fc44 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- nixpkgs unstable: keyutils 1.6.3
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- opensuse-leap 16.0: keyutils 1.6.3-160000.3.2 Β· GPL-2.0-or-later AND LGPL-2.1-or-later
- ubuntu noble: libkeyutils-dev 1.6.3-3build1 Β· GPL-2.0-or-later
- ubuntu questing: libkeyutils-dev 1.6.3-6ubuntu2
- ubuntu resolute: libkeyutils-dev 1.6.3-6ubuntu3 Β· GPL-2.0-or-later
- ubuntu stonking: libkeyutils-dev 1.6.3-6ubuntu3 Β· GPL-2.0-or-later
- Copyright (C) 2019 Red Hat, Inc. All Rights Reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms