add_key()函数 Unix/Linux
名称
add_key - 添加到内核的密钥管理机制一个键
内容简介
#include <keyutils.h> key_serial_t add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t keyring); |
描述
add_key() 要求内核给定类型和描述来创建或更新一个键,它的有效载荷plen 长度实例,将它安装到提名 keyringand,返回其序列号。
密钥类型可能会拒绝该数据,如果它是在错误的格式或以其他方式无效。
如果目标的钥匙圈已经包含匹配指定类型和描述,然后,如果密钥类型支持一个键,该键将被更新,而不是创建一个新的密钥,如果没有,将创建一个新的密钥,它将取代链接到现存的核心,从钥匙圈。
目的地钥匙圈序号可能是一个有效的钥匙圈,主调用写入权限,或者它可以是一个特殊的密钥环ID:
标签 | 描述 |
---|---|
KEY_SPEC_THREAD_KEYRING | This specifies the caller’s thread-specific keyring. |
KEY_SPEC_PROCESS_KEYRING | This specifies the caller’s process-specific keyring. |
KEY_SPEC_SESSION_KEYRING | This specifies the caller’s session-specific keyring. |
KEY_SPEC_USER_KEYRING | This specifies the caller’s UID-specific keyring. |
KEY_SPEC_USER_SESSION_KEYRING | This specifies the caller’s UID-session keyring. |
密钥类型
有很多可供选择的核心密钥管理代码的密钥类型,而这些可以被指定为这个函数:
标签 | 描述 |
---|---|
“user” | Keys of the user-defined key type may contain a blob of arbitrary data, and thedescription may be any valid string, though it is preferred that the description be prefixed with a string representing the service to which the key is of interest and a colon (for instance “afs:mykey”). The payload may be empty or NULL for keys of this type. |
“keyring” | Keyrings are special key types that may contain links to sequences of other keys of any type. If this interface is used to create a keyring, then a NULL payload should be specified, andplen should be zero. |
返回值
成功 add_key() 返回序列号密钥,它创建或更新。错误将返回值-1并且errno将被设置为一个适当的错误。
错误
标签 | 描述 |
---|---|
ENOKEY | The keyring doesn’t exist. |
EKEYEXPIRED | The keyring has expired. |
EKEYREVOKED | The keyring has been revoked. |
EINVAL | The payload data was invalid. |
ENOMEM | Insufficient memory to create a key. |
EDQUOT | The key quota for this user would be exceeded by creating this key or linking it to the keyring. |
EACCES | The keyring wasn’t available for modification by the user. |
链接
虽然这是一个Linux系统调用,它是在libc中不存在,但可以发现合适的 libkey 工具。链接时,lkey 工具应指定给链接器。