From 0cfe3653d875afc0508333f444944ebb965ba300 Mon Sep 17 00:00:00 2001 From: Jiping Yin Date: Sat, 12 Oct 2024 11:01:04 +0800 Subject: [PATCH] fix: agent - eBPF Handle the scenario where CONFIG_NET_NS is disabled (#8282) Fix issues in certain custom kernel systems where the lack of the 'CONFIG_NET_NS' compile option causes process information to fail during the agent initialization phase. Conflicts: agent/src/ebpf/user/tracer.c --- agent/src/ebpf/user/common.c | 25 +++++++++++++++++++++++-- agent/src/ebpf/user/common.h | 1 + agent/src/ebpf/user/proc.c | 2 -- agent/src/ebpf/user/tracer.c | 5 +++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/agent/src/ebpf/user/common.c b/agent/src/ebpf/user/common.c index 9f2fae9d3fa..11f470a13b5 100644 --- a/agent/src/ebpf/user/common.c +++ b/agent/src/ebpf/user/common.c @@ -537,12 +537,17 @@ u64 get_process_starttime_and_comm(pid_t pid, char *name_base, int len) unsigned long long etime_ticks = 0; snprintf(file, sizeof(file), "/proc/%d/stat", pid); - if (access(file, F_OK)) + if (access(file, F_OK)) { + ebpf_warning("file %s is not exited\n", file); return 0; + } fd = open(file, O_RDONLY); - if (fd <= 2) + if (fd <= 2) { + ebpf_warning("open %s failed with %s(%d)\n", file, + strerror(errno), errno); return 0; + } read(fd, buff, sizeof(buff)); close(fd); @@ -551,6 +556,7 @@ u64 get_process_starttime_and_comm(pid_t pid, char *name_base, int len) if (sscanf(buff, "%*s %ms %*s %*s %*s %*s %*s %*s %*s %*s %*s" " %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %llu ", &start, &etime_ticks) != 2) { + ebpf_warning("sscanf() failed. pid %d buff %s\n", pid, buff); return 0; } @@ -911,6 +917,21 @@ u64 get_netns_id_from_pid(pid_t pid) return strtoull(netns_id_str, NULL, 10); } +bool check_netns_enabled(void) +{ + char netns_path[MAX_PATH_LENGTH]; + snprintf(netns_path, sizeof(netns_path), "/proc/1/ns/net"); + + char target_path[MAX_PATH_LENGTH]; + ssize_t len = + readlink(netns_path, target_path, sizeof(target_path) - 1); + if (len == -1) { + return false; + } + + return true; +} + // Function to retrieve the host PID from the first line of /proc/pid/sched // The expected format is "java (1234, #threads: 12)" // where 1234 is the host PID (before Linux 4.1) diff --git a/agent/src/ebpf/user/common.h b/agent/src/ebpf/user/common.h index a4f756ce6f7..29a895f88fe 100644 --- a/agent/src/ebpf/user/common.h +++ b/agent/src/ebpf/user/common.h @@ -281,6 +281,7 @@ u64 get_process_starttime_and_comm(pid_t pid, int len); u32 legacy_fetch_log2_page_size(void); u64 get_netns_id_from_pid(pid_t pid); +bool check_netns_enabled(void); int get_nspid(int pid); int get_target_uid_and_gid(int target_pid, int *uid, int *gid); int copy_file(const char *src_file, const char *dest_file); diff --git a/agent/src/ebpf/user/proc.c b/agent/src/ebpf/user/proc.c index 461ddc6b0ee..9ed0fa79cdb 100644 --- a/agent/src/ebpf/user/proc.c +++ b/agent/src/ebpf/user/proc.c @@ -538,8 +538,6 @@ static int config_symbolizer_proc_info(struct symbolizer_proc_info *p, int pid) p->thread_names = NULL; p->thread_names_lock = 0; p->netns_id = get_netns_id_from_pid(pid); - if (p->netns_id == 0) - return ETR_INVAL; fetch_container_id(pid, p->container_id, sizeof(p->container_id)); diff --git a/agent/src/ebpf/user/tracer.c b/agent/src/ebpf/user/tracer.c index efc40999a6f..27d301a0c1c 100644 --- a/agent/src/ebpf/user/tracer.c +++ b/agent/src/ebpf/user/tracer.c @@ -2056,6 +2056,11 @@ int bpf_tracer_init(const char *log_file, bool is_stdout) init_thread_ids(); + if (!check_netns_enabled()) + ebpf_warning("If the system has not enabled the 'CONFIG_NET_NS'" + " option, the 'netns_id' for continuously profiling" + " data will be 0.\n"); + /* * Set up the lock now, so we can use it to make the first add * thread-safe for tracer alloc.