1212#include <sys/wait.h>
1313#include <sys/mount.h>
1414#include <sys/fcntl.h>
15+ #include "network_helpers.h"
1516
1617#define STACK_SIZE (1024 * 1024)
1718static char child_stack [STACK_SIZE ];
@@ -74,6 +75,50 @@ static int test_current_pid_tgid_tp(void *args)
7475 return ret ;
7576}
7677
78+ static int test_current_pid_tgid_cgrp (void * args )
79+ {
80+ struct test_ns_current_pid_tgid__bss * bss ;
81+ struct test_ns_current_pid_tgid * skel ;
82+ int server_fd = -1 , ret = -1 , err ;
83+ int cgroup_fd = * (int * )args ;
84+ pid_t tgid , pid ;
85+
86+ skel = test_ns_current_pid_tgid__open ();
87+ if (!ASSERT_OK_PTR (skel , "test_ns_current_pid_tgid__open" ))
88+ return ret ;
89+
90+ bpf_program__set_autoload (skel -> progs .cgroup_bind4 , true);
91+
92+ err = test_ns_current_pid_tgid__load (skel );
93+ if (!ASSERT_OK (err , "test_ns_current_pid_tgid__load" ))
94+ goto cleanup ;
95+
96+ bss = skel -> bss ;
97+ if (get_pid_tgid (& pid , & tgid , bss ))
98+ goto cleanup ;
99+
100+ skel -> links .cgroup_bind4 = bpf_program__attach_cgroup (
101+ skel -> progs .cgroup_bind4 , cgroup_fd );
102+ if (!ASSERT_OK_PTR (skel -> links .cgroup_bind4 , "bpf_program__attach_cgroup" ))
103+ goto cleanup ;
104+
105+ server_fd = start_server (AF_INET , SOCK_STREAM , NULL , 0 , 0 );
106+ if (!ASSERT_GE (server_fd , 0 , "start_server" ))
107+ goto cleanup ;
108+
109+ if (!ASSERT_EQ (bss -> user_pid , pid , "pid" ))
110+ goto cleanup ;
111+ if (!ASSERT_EQ (bss -> user_tgid , tgid , "tgid" ))
112+ goto cleanup ;
113+ ret = 0 ;
114+
115+ cleanup :
116+ if (server_fd >= 0 )
117+ close (server_fd );
118+ test_ns_current_pid_tgid__destroy (skel );
119+ return ret ;
120+ }
121+
77122static void test_ns_current_pid_tgid_new_ns (int (* fn )(void * ), void * arg )
78123{
79124 int wstatus ;
@@ -95,11 +140,39 @@ static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg)
95140 return ;
96141}
97142
143+ static void test_in_netns (int (* fn )(void * ), void * arg )
144+ {
145+ struct nstoken * nstoken = NULL ;
146+
147+ SYS (cleanup , "ip netns add ns_current_pid_tgid" );
148+ SYS (cleanup , "ip -net ns_current_pid_tgid link set dev lo up" );
149+
150+ nstoken = open_netns ("ns_current_pid_tgid" );
151+ if (!ASSERT_OK_PTR (nstoken , "open_netns" ))
152+ goto cleanup ;
153+
154+ test_ns_current_pid_tgid_new_ns (fn , arg );
155+
156+ cleanup :
157+ if (nstoken )
158+ close_netns (nstoken );
159+ SYS_NOFAIL ("ip netns del ns_current_pid_tgid" );
160+ }
161+
98162/* TODO: use a different tracepoint */
99163void serial_test_ns_current_pid_tgid (void )
100164{
101165 if (test__start_subtest ("root_ns_tp" ))
102166 test_current_pid_tgid_tp (NULL );
103167 if (test__start_subtest ("new_ns_tp" ))
104168 test_ns_current_pid_tgid_new_ns (test_current_pid_tgid_tp , NULL );
169+ if (test__start_subtest ("new_ns_cgrp" )) {
170+ int cgroup_fd = -1 ;
171+
172+ cgroup_fd = test__join_cgroup ("/sock_addr" );
173+ if (ASSERT_GE (cgroup_fd , 0 , "join_cgroup" )) {
174+ test_in_netns (test_current_pid_tgid_cgrp , & cgroup_fd );
175+ close (cgroup_fd );
176+ }
177+ }
105178}
0 commit comments