tools/testing/selftests/bpf/prog_tests/lookup_key.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/lookup_key.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/lookup_key.c- Extension
.c- Size
- 2720 bytes
- Lines
- 113
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/keyctl.htest_progs.htest_lookup_key.skel.h
Detected Declarations
function libbpf_print_cbfunction test_lookup_key
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2022 Huawei Technologies Duesseldorf GmbH
*
* Author: Roberto Sassu <roberto.sassu@huawei.com>
*/
#include <linux/keyctl.h>
#include <test_progs.h>
#include "test_lookup_key.skel.h"
#define KEY_LOOKUP_CREATE 0x01
#define KEY_LOOKUP_PARTIAL 0x02
static bool kfunc_not_supported;
static int libbpf_print_cb(enum libbpf_print_level level, const char *fmt,
va_list args)
{
char *func;
if (strcmp(fmt, "libbpf: extern (func ksym) '%s': not found in kernel or module BTFs\n"))
return 0;
func = va_arg(args, char *);
if (strcmp(func, "bpf_lookup_user_key") && strcmp(func, "bpf_key_put") &&
strcmp(func, "bpf_lookup_system_key"))
return 0;
kfunc_not_supported = true;
return 0;
}
void test_lookup_key(void)
{
libbpf_print_fn_t old_print_cb;
struct test_lookup_key *skel;
__u32 next_id;
int ret;
skel = test_lookup_key__open();
if (!ASSERT_OK_PTR(skel, "test_lookup_key__open"))
return;
old_print_cb = libbpf_set_print(libbpf_print_cb);
ret = test_lookup_key__load(skel);
libbpf_set_print(old_print_cb);
if (ret < 0 && kfunc_not_supported) {
printf("%s:SKIP:bpf_lookup_*_key(), bpf_key_put() kfuncs not supported\n",
__func__);
test__skip();
goto close_prog;
}
if (!ASSERT_OK(ret, "test_lookup_key__load"))
goto close_prog;
ret = test_lookup_key__attach(skel);
if (!ASSERT_OK(ret, "test_lookup_key__attach"))
goto close_prog;
skel->bss->monitored_pid = getpid();
skel->bss->key_serial = KEY_SPEC_THREAD_KEYRING;
/* The thread-specific keyring does not exist, this test fails. */
skel->bss->flags = 0;
ret = bpf_prog_get_next_id(0, &next_id);
if (!ASSERT_LT(ret, 0, "bpf_prog_get_next_id"))
goto close_prog;
/* Force creation of the thread-specific keyring, this test succeeds. */
skel->bss->flags = KEY_LOOKUP_CREATE;
ret = bpf_prog_get_next_id(0, &next_id);
if (!ASSERT_OK(ret, "bpf_prog_get_next_id"))
goto close_prog;
/* Pass both lookup flags for parameter validation. */
skel->bss->flags = KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL;
ret = bpf_prog_get_next_id(0, &next_id);
if (!ASSERT_OK(ret, "bpf_prog_get_next_id"))
goto close_prog;
/* Pass invalid flags. */
Annotation
- Immediate include surface: `linux/keyctl.h`, `test_progs.h`, `test_lookup_key.skel.h`.
- Detected declarations: `function libbpf_print_cb`, `function test_lookup_key`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.