Skip to content

Commit 0b0bb45

Browse files
olsajirianakryiko
authored andcommitted
selftests/bpf: Add child argument to spawn_child function
Adding child argument to spawn_child function to allow to create multiple children in following change. Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 900f362 commit 0b0bb45

File tree

1 file changed

+39
-46
lines changed

1 file changed

+39
-46
lines changed

tools/testing/selftests/bpf/prog_tests/uprobe_multi_test.c

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,27 @@ static void kick_child(struct child *child)
6868
fflush(NULL);
6969
}
7070

71-
static struct child *spawn_child(void)
71+
static int spawn_child(struct child *child)
7272
{
73-
static struct child child;
74-
int err;
75-
int c;
73+
int err, c;
7674

7775
/* pipe to notify child to execute the trigger functions */
78-
if (pipe(child.go))
79-
return NULL;
76+
if (pipe(child->go))
77+
return -1;
8078

81-
child.pid = child.tid = fork();
82-
if (child.pid < 0) {
83-
release_child(&child);
79+
child->pid = child->tid = fork();
80+
if (child->pid < 0) {
81+
release_child(child);
8482
errno = EINVAL;
85-
return NULL;
83+
return -1;
8684
}
8785

8886
/* child */
89-
if (child.pid == 0) {
90-
close(child.go[1]);
87+
if (child->pid == 0) {
88+
close(child->go[1]);
9189

9290
/* wait for parent's kick */
93-
err = read(child.go[0], &c, 1);
91+
err = read(child->go[0], &c, 1);
9492
if (err != 1)
9593
exit(err);
9694

@@ -102,7 +100,7 @@ static struct child *spawn_child(void)
102100
exit(errno);
103101
}
104102

105-
return &child;
103+
return 0;
106104
}
107105

108106
static void *child_thread(void *ctx)
@@ -131,39 +129,38 @@ static void *child_thread(void *ctx)
131129
pthread_exit(&err);
132130
}
133131

134-
static struct child *spawn_thread(void)
132+
static int spawn_thread(struct child *child)
135133
{
136-
static struct child child;
137134
int c, err;
138135

139136
/* pipe to notify child to execute the trigger functions */
140-
if (pipe(child.go))
141-
return NULL;
137+
if (pipe(child->go))
138+
return -1;
142139
/* pipe to notify parent that child thread is ready */
143-
if (pipe(child.c2p)) {
144-
close(child.go[0]);
145-
close(child.go[1]);
146-
return NULL;
140+
if (pipe(child->c2p)) {
141+
close(child->go[0]);
142+
close(child->go[1]);
143+
return -1;
147144
}
148145

149-
child.pid = getpid();
146+
child->pid = getpid();
150147

151-
err = pthread_create(&child.thread, NULL, child_thread, &child);
148+
err = pthread_create(&child->thread, NULL, child_thread, child);
152149
if (err) {
153150
err = -errno;
154-
close(child.go[0]);
155-
close(child.go[1]);
156-
close(child.c2p[0]);
157-
close(child.c2p[1]);
151+
close(child->go[0]);
152+
close(child->go[1]);
153+
close(child->c2p[0]);
154+
close(child->c2p[1]);
158155
errno = -err;
159-
return NULL;
156+
return -1;
160157
}
161158

162-
err = read(child.c2p[0], &c, 1);
159+
err = read(child->c2p[0], &c, 1);
163160
if (!ASSERT_EQ(err, 1, "child_thread_ready"))
164-
return NULL;
161+
return -1;
165162

166-
return &child;
163+
return 0;
167164
}
168165

169166
static void uprobe_multi_test_run(struct uprobe_multi *skel, struct child *child)
@@ -304,24 +301,22 @@ __test_attach_api(const char *binary, const char *pattern, struct bpf_uprobe_mul
304301
static void
305302
test_attach_api(const char *binary, const char *pattern, struct bpf_uprobe_multi_opts *opts)
306303
{
307-
struct child *child;
304+
static struct child child;
308305

309306
/* no pid filter */
310307
__test_attach_api(binary, pattern, opts, NULL);
311308

312309
/* pid filter */
313-
child = spawn_child();
314-
if (!ASSERT_OK_PTR(child, "spawn_child"))
310+
if (!ASSERT_OK(spawn_child(&child), "spawn_child"))
315311
return;
316312

317-
__test_attach_api(binary, pattern, opts, child);
313+
__test_attach_api(binary, pattern, opts, &child);
318314

319315
/* pid filter (thread) */
320-
child = spawn_thread();
321-
if (!ASSERT_OK_PTR(child, "spawn_thread"))
316+
if (!ASSERT_OK(spawn_thread(&child), "spawn_thread"))
322317
return;
323318

324-
__test_attach_api(binary, pattern, opts, child);
319+
__test_attach_api(binary, pattern, opts, &child);
325320
}
326321

327322
static void test_attach_api_pattern(void)
@@ -712,24 +707,22 @@ static void __test_link_api(struct child *child)
712707

713708
static void test_link_api(void)
714709
{
715-
struct child *child;
710+
static struct child child;
716711

717712
/* no pid filter */
718713
__test_link_api(NULL);
719714

720715
/* pid filter */
721-
child = spawn_child();
722-
if (!ASSERT_OK_PTR(child, "spawn_child"))
716+
if (!ASSERT_OK(spawn_child(&child), "spawn_child"))
723717
return;
724718

725-
__test_link_api(child);
719+
__test_link_api(&child);
726720

727721
/* pid filter (thread) */
728-
child = spawn_thread();
729-
if (!ASSERT_OK_PTR(child, "spawn_thread"))
722+
if (!ASSERT_OK(spawn_thread(&child), "spawn_thread"))
730723
return;
731724

732-
__test_link_api(child);
725+
__test_link_api(&child);
733726
}
734727

735728
static struct bpf_program *

0 commit comments

Comments
 (0)