c语言 - 指向从'struct net *'分配给'u32'(也称为"无符号 int")的整数转换的不兼容指针



我想要的:

将网络命名空间选项添加到execsnoop bcc工具,以仅跟踪具有指定网络命名空间的日志,就像我们在许多其他密件抄送工具中过滤PID选项一样。例如:execsnoop -N "ns_id">

我正在使用 linux 内核结构来检索命名空间 idnet = task->nsproxy->net_ns;,并且需要将检索到的 ns 分配给 u32 intdata.netns

我在做什么:

int syscall__execve(struct pt_regs *ctx,
const char __user *filename,
const char __user *const __user *__argv,
const char __user *const __user *__envp)
{
// create data here and pass to submit_arg to save stack space (#555)
//int ret = PT_REGS_RC(ctx);
struct data_t data = {};
struct task_struct *task;
struct nsproxy *nsproxy;
struct net *net;
//struct mnt_namespace *mnt_ns;
data.pid = bpf_get_current_pid_tgid() >> 32;
u32 net_ns_inum = 0;
//net = (struct net *)get_net_ns_by_pid(data.pid); //
//net_ns_inum = (uintptr_t)net;

task = (struct task_struct *)bpf_get_current_task();
// Some kernels, like Ubuntu 4.13.0-generic, return 0
// as the real_parent->tgid.
// We use the get_ppid function as a fallback in those cases. (#1883)
data.ppid = task->real_parent->tgid;
net = task->nsproxy->net_ns;
FILTER_NETNS
data.netns =  (uintptr_t)net; //here have to perform casting

我已经添加了#include </usr/include/stdint.h>,但收到警告,尽管include <bits/libc-header-start.h>文件中存在stdint.h警告:

In file included from /virtual/main.c:8:
/usr/include/stdint.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
#include <bits/libc-header-start.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~

如果我解决这个问题,它会继续生成其他丢失的头文件错误。

我已经解决了这个问题:

我没有使用net = task->nsproxy->net_ns;而是使用了net = task->nsproxy->net_ns->ns.inum;它是无符号的int,我们可以直接从中检索命名空间。

此结构可以在<linux/ns_common.h>头文件中找到。

struct ns_common {
atomic_long_t stashed;
const struct proc_ns_operations *ops;
unsigned int inum;
};

要在execsnoop工具中获取带有添加命名空间选项的修改代码,请点击此链接:https://github.com/Sheenam3/ebpf/blob/master/execsnoop.py#L143

最新更新