Skip to content

Commit

Permalink
fix: agent - eBPF Handle the scenario where CONFIG_NET_NS is disabled
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yinjiping authored and lzf575 committed Oct 12, 2024
1 parent 7eab979 commit 94f200a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
25 changes: 23 additions & 2 deletions agent/src/ebpf/user/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions agent/src/ebpf/user/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions agent/src/ebpf/user/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,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));

Expand Down
5 changes: 5 additions & 0 deletions agent/src/ebpf/user/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,11 @@ int bpf_tracer_init(const char *log_file, bool is_stdout)
prev_sys_boot_time_ns = sys_boot_time_ns;
ebpf_info("sys_boot_time_ns : %llu\n", sys_boot_time_ns);

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.
Expand Down

0 comments on commit 94f200a

Please sign in to comment.