summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/pidfd/pidfd_autoreap_test.c
blob: 1c586482f25b914b8cf9de17a29e9ffca6176f72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2026 Christian Brauner <brauner@kernel.org>

#define _GNU_SOURCE
#include <errno.h>
#include <linux/types.h>
#include <poll.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syscall.h>
#include <sys/ioctl.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "pidfd.h"
#include "kselftest_harness.h"

#ifndef CLONE_AUTOREAP
#define CLONE_AUTOREAP (1ULL << 34)
#endif

#ifndef CLONE_NNP
#define CLONE_NNP (1ULL << 35)
#endif

#ifndef CLONE_PIDFD_AUTOKILL
#define CLONE_PIDFD_AUTOKILL (1ULL << 36)
#endif

#ifndef _LINUX_CAPABILITY_VERSION_3
#define _LINUX_CAPABILITY_VERSION_3 0x20080522
#endif

struct cap_header {
	__u32 version;
	int pid;
};

struct cap_data {
	__u32 effective;
	__u32 permitted;
	__u32 inheritable;
};

static int drop_all_caps(void)
{
	struct cap_header hdr = { .version = _LINUX_CAPABILITY_VERSION_3 };
	struct cap_data data[2] = {};

	return syscall(__NR_capset, &hdr, data);
}

static pid_t create_autoreap_child(int *pidfd)
{
	struct __clone_args args = {
		.flags		= CLONE_PIDFD | CLONE_AUTOREAP,
		.exit_signal	= 0,
		.pidfd		= ptr_to_u64(pidfd),
	};

	return sys_clone3(&args, sizeof(args));
}

/*
 * Test that CLONE_AUTOREAP works without CLONE_PIDFD (fire-and-forget).
 */
TEST(autoreap_without_pidfd)
{
	struct __clone_args args = {
		.flags		= CLONE_AUTOREAP,
		.exit_signal	= 0,
	};
	pid_t pid;
	int ret;

	pid = sys_clone3(&args, sizeof(args));
	if (pid < 0 && errno == EINVAL)
		SKIP(return, "CLONE_AUTOREAP not supported");
	ASSERT_GE(pid, 0);

	if (pid == 0)
		_exit(0);

	/*
	 * Give the child a moment to exit and be autoreaped.
	 * Then verify no zombie remains.
	 */
	usleep(200000);
	ret = waitpid(pid, NULL, WNOHANG);
	ASSERT_EQ(ret, -1);
	ASSERT_EQ(errno, ECHILD);
}

/*
 * Test that CLONE_AUTOREAP with a non-zero exit_signal fails.
 */
TEST(autoreap_rejects_exit_signal)
{
	struct __clone_args args = {
		.flags		= CLONE_AUTOREAP,
		.exit_signal	= SIGCHLD,
	};
	pid_t pid;

	pid = sys_clone3(&args, sizeof(args));
	ASSERT_EQ(pid, -1);
	ASSERT_EQ(errno, EINVAL);
}

/*
 * Test that CLONE_AUTOREAP with CLONE_PARENT fails.
 */
TEST(autoreap_rejects_parent)
{
	struct __clone_args args = {
		.flags		= CLONE_AUTOREAP | CLONE_PARENT,
		.exit_signal	= 0,
	};
	pid_t pid;

	pid = sys_clone3(&args, sizeof(args));
	ASSERT_EQ(pid, -1);
	ASSERT_EQ(errno, EINVAL);
}

/*
 * Test that CLONE_AUTOREAP with CLONE_THREAD fails.
 */
TEST(autoreap_rejects_thread)
{
	struct __clone_args args = {
		.flags		= CLONE_AUTOREAP | CLONE_THREAD |
				  CLONE_SIGHAND | CLONE_VM,
		.exit_signal	= 0,
	};
	pid_t pid;

	pid = sys_clone3(&args, sizeof(args));
	ASSERT_EQ(pid, -1);
	ASSERT_EQ(errno, EINVAL);
}

/*
 * Basic test: create an autoreap child, let it exit, verify:
 * - pidfd becomes readable (poll returns POLLIN)
 * - PIDFD_GET_INFO returns the correct exit code
 * - waitpid() returns -1/ECHILD (no zombie)
 */
TEST(autoreap_basic)
{
	struct pidfd_info info = { .mask = PIDFD_INFO_EXIT };
	int pidfd = -1, ret;
	struct pollfd pfd;
	pid_t pid;

	pid = create_autoreap_child(&pidfd);
	if (pid < 0 && errno == EINVAL)
		SKIP(return, "CLONE_AUTOREAP not supported");
	ASSERT_GE(pid, 0);

	if (pid == 0)
		_exit(42);

	ASSERT_GE(pidfd, 0);

	/* Wait for the child to exit via pidfd poll. */
	pfd.fd = pidfd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 5000);
	ASSERT_EQ(ret, 1);
	ASSERT_TRUE(pfd.revents & POLLIN);

	/* Verify exit info via PIDFD_GET_INFO. */
	ret = ioctl(pidfd, PIDFD_GET_INFO, &info);
	ASSERT_EQ(ret, 0);
	ASSERT_TRUE(info.mask & PIDFD_INFO_EXIT);
	/*
	 * exit_code is in waitpid format: for _exit(42),
	 * WIFEXITED is true and WEXITSTATUS is 42.
	 */
	ASSERT_TRUE(WIFEXITED(info.exit_code));
	ASSERT_EQ(WEXITSTATUS(info.exit_code), 42);

	/* Verify no zombie: waitpid should fail with ECHILD. */
	ret = waitpid(pid, NULL, WNOHANG);
	ASSERT_EQ(ret, -1);
	ASSERT_EQ(errno, ECHILD);

	close(pidfd);
}

/*
 * Test that an autoreap child killed by a signal reports
 * the correct exit info.
 */
TEST(autoreap_signaled)
{
	struct pidfd_info info = { .mask = PIDFD_INFO_EXIT };
	int pidfd = -1, ret;
	struct pollfd pfd;
	pid_t pid;

	pid = create_autoreap_child(&pidfd);
	if (pid < 0 && errno == EINVAL)
		SKIP(return, "CLONE_AUTOREAP not supported");
	ASSERT_GE(pid, 0);

	if (pid == 0) {
		pause();
		_exit(1);
	}

	ASSERT_GE(pidfd, 0);

	/* Kill the child. */
	ret = sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0);
	ASSERT_EQ(ret, 0);

	/* Wait for exit via pidfd. */
	pfd.fd = pidfd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 5000);
	ASSERT_EQ(ret, 1);
	ASSERT_TRUE(pfd.revents & POLLIN);

	/* Verify signal info. */
	ret = ioctl(pidfd, PIDFD_GET_INFO, &info);
	ASSERT_EQ(ret, 0);
	ASSERT_TRUE(info.mask & PIDFD_INFO_EXIT);
	ASSERT_TRUE(WIFSIGNALED(info.exit_code));
	ASSERT_EQ(WTERMSIG(info.exit_code), SIGKILL);

	/* No zombie. */
	ret = waitpid(pid, NULL, WNOHANG);
	ASSERT_EQ(ret, -1);
	ASSERT_EQ(errno, ECHILD);

	close(pidfd);
}

/*
 * Test autoreap survives reparenting: middle process creates an
 * autoreap grandchild, then exits. The grandchild gets reparented
 * to us (the grandparent, which is a subreaper). When the grandchild
 * exits, it should still be autoreaped - no zombie under us.
 */
TEST(autoreap_reparent)
{
	int ipc_sockets[2], ret;
	int pidfd = -1;
	struct pollfd pfd;
	pid_t mid_pid, grandchild_pid;
	char buf[32] = {};

	/* Make ourselves a subreaper so reparented children come to us. */
	ret = prctl(PR_SET_CHILD_SUBREAPER, 1);
	ASSERT_EQ(ret, 0);

	ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
	ASSERT_EQ(ret, 0);

	mid_pid = fork();
	ASSERT_GE(mid_pid, 0);

	if (mid_pid == 0) {
		/* Middle child: create an autoreap grandchild. */
		int gc_pidfd = -1;

		close(ipc_sockets[0]);

		grandchild_pid = create_autoreap_child(&gc_pidfd);
		if (grandchild_pid < 0) {
			write_nointr(ipc_sockets[1], "E", 1);
			close(ipc_sockets[1]);
			_exit(1);
		}

		if (grandchild_pid == 0) {
			/* Grandchild: wait for signal to exit. */
			close(ipc_sockets[1]);
			if (gc_pidfd >= 0)
				close(gc_pidfd);
			pause();
			_exit(0);
		}

		/* Send grandchild PID to grandparent. */
		snprintf(buf, sizeof(buf), "%d", grandchild_pid);
		write_nointr(ipc_sockets[1], buf, strlen(buf));
		close(ipc_sockets[1]);
		if (gc_pidfd >= 0)
			close(gc_pidfd);

		/* Middle child exits, grandchild gets reparented. */
		_exit(0);
	}

	close(ipc_sockets[1]);

	/* Read grandchild's PID. */
	ret = read_nointr(ipc_sockets[0], buf, sizeof(buf) - 1);
	close(ipc_sockets[0]);
	ASSERT_GT(ret, 0);

	if (buf[0] == 'E') {
		waitpid(mid_pid, NULL, 0);
		prctl(PR_SET_CHILD_SUBREAPER, 0);
		SKIP(return, "CLONE_AUTOREAP not supported");
	}

	grandchild_pid = atoi(buf);
	ASSERT_GT(grandchild_pid, 0);

	/* Wait for the middle child to exit. */
	ret = waitpid(mid_pid, NULL, 0);
	ASSERT_EQ(ret, mid_pid);

	/*
	 * Now the grandchild is reparented to us (subreaper).
	 * Open a pidfd for the grandchild and kill it.
	 */
	pidfd = sys_pidfd_open(grandchild_pid, 0);
	ASSERT_GE(pidfd, 0);

	ret = sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0);
	ASSERT_EQ(ret, 0);

	/* Wait for it to exit via pidfd poll. */
	pfd.fd = pidfd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 5000);
	ASSERT_EQ(ret, 1);
	ASSERT_TRUE(pfd.revents & POLLIN);

	/*
	 * The grandchild should have been autoreaped even though
	 * we (the new parent) haven't set SA_NOCLDWAIT.
	 * waitpid should return -1/ECHILD.
	 */
	ret = waitpid(grandchild_pid, NULL, WNOHANG);
	EXPECT_EQ(ret, -1);
	EXPECT_EQ(errno, ECHILD);

	close(pidfd);

	/* Clean up subreaper status. */
	prctl(PR_SET_CHILD_SUBREAPER, 0);
}

static int thread_sock_fd;

static void *thread_func(void *arg)
{
	/* Signal parent we're running. */
	write_nointr(thread_sock_fd, "1", 1);

	/* Give main thread time to call _exit() first. */
	usleep(200000);

	return NULL;
}

/*
 * Test that an autoreap child with multiple threads is properly
 * autoreaped only after all threads have exited.
 */
TEST(autoreap_multithreaded)
{
	struct pidfd_info info = { .mask = PIDFD_INFO_EXIT };
	int ipc_sockets[2], ret;
	int pidfd = -1;
	struct pollfd pfd;
	pid_t pid;
	char c;

	ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
	ASSERT_EQ(ret, 0);

	pid = create_autoreap_child(&pidfd);
	if (pid < 0 && errno == EINVAL) {
		close(ipc_sockets[0]);
		close(ipc_sockets[1]);
		SKIP(return, "CLONE_AUTOREAP not supported");
	}
	ASSERT_GE(pid, 0);

	if (pid == 0) {
		pthread_t thread;

		close(ipc_sockets[0]);

		/*
		 * Create a sub-thread that outlives the main thread.
		 * The thread signals readiness, then sleeps.
		 * The main thread waits briefly, then calls _exit().
		 */
		thread_sock_fd = ipc_sockets[1];
		pthread_create(&thread, NULL, thread_func, NULL);
		pthread_detach(thread);

		/* Wait for thread to be running. */
		usleep(100000);

		/* Main thread exits; sub-thread is still alive. */
		_exit(99);
	}

	close(ipc_sockets[1]);

	/* Wait for the sub-thread to signal readiness. */
	ret = read_nointr(ipc_sockets[0], &c, 1);
	close(ipc_sockets[0]);
	ASSERT_EQ(ret, 1);

	/* Wait for the process to fully exit via pidfd poll. */
	pfd.fd = pidfd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 5000);
	ASSERT_EQ(ret, 1);
	ASSERT_TRUE(pfd.revents & POLLIN);

	/* Verify exit info. */
	ret = ioctl(pidfd, PIDFD_GET_INFO, &info);
	ASSERT_EQ(ret, 0);
	ASSERT_TRUE(info.mask & PIDFD_INFO_EXIT);
	ASSERT_TRUE(WIFEXITED(info.exit_code));
	ASSERT_EQ(WEXITSTATUS(info.exit_code), 99);

	/* No zombie. */
	ret = waitpid(pid, NULL, WNOHANG);
	ASSERT_EQ(ret, -1);
	ASSERT_EQ(errno, ECHILD);

	close(pidfd);
}

/*
 * Test that autoreap is NOT inherited by grandchildren.
 */
TEST(autoreap_no_inherit)
{
	int ipc_sockets[2], ret;
	int pidfd = -1;
	pid_t pid;
	char buf[2] = {};
	struct pollfd pfd;

	ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
	ASSERT_EQ(ret, 0);

	pid = create_autoreap_child(&pidfd);
	if (pid < 0 && errno == EINVAL) {
		close(ipc_sockets[0]);
		close(ipc_sockets[1]);
		SKIP(return, "CLONE_AUTOREAP not supported");
	}
	ASSERT_GE(pid, 0);

	if (pid == 0) {
		pid_t gc;
		int status;

		close(ipc_sockets[0]);

		/* Autoreap child forks a grandchild (without autoreap). */
		gc = fork();
		if (gc < 0) {
			write_nointr(ipc_sockets[1], "E", 1);
			_exit(1);
		}
		if (gc == 0) {
			/* Grandchild: exit immediately. */
			close(ipc_sockets[1]);
			_exit(77);
		}

		/*
		 * The grandchild should become a regular zombie
		 * since it was NOT created with CLONE_AUTOREAP.
		 * Wait for it to verify.
		 */
		ret = waitpid(gc, &status, 0);
		if (ret == gc && WIFEXITED(status) &&
		    WEXITSTATUS(status) == 77) {
			write_nointr(ipc_sockets[1], "P", 1);
		} else {
			write_nointr(ipc_sockets[1], "F", 1);
		}
		close(ipc_sockets[1]);
		_exit(0);
	}

	close(ipc_sockets[1]);

	ret = read_nointr(ipc_sockets[0], buf, 1);
	close(ipc_sockets[0]);
	ASSERT_EQ(ret, 1);

	/*
	 * 'P' means the autoreap child was able to waitpid() its
	 * grandchild (correct - grandchild should be a normal zombie,
	 * not autoreaped).
	 */
	ASSERT_EQ(buf[0], 'P');

	/* Wait for the autoreap child to exit. */
	pfd.fd = pidfd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 5000);
	ASSERT_EQ(ret, 1);

	/* Autoreap child itself should be autoreaped. */
	ret = waitpid(pid, NULL, WNOHANG);
	ASSERT_EQ(ret, -1);
	ASSERT_EQ(errno, ECHILD);

	close(pidfd);
}

/*
 * Test that CLONE_NNP sets no_new_privs on the child.
 * The child checks via prctl(PR_GET_NO_NEW_PRIVS) and reports back.
 * The parent must NOT have no_new_privs set afterwards.
 */
TEST(nnp_sets_no_new_privs)
{
	struct __clone_args args = {
		.flags		= CLONE_PIDFD | CLONE_AUTOREAP | CLONE_NNP,
		.exit_signal	= 0,
	};
	struct pidfd_info info = { .mask = PIDFD_INFO_EXIT };
	int pidfd = -1, ret;
	struct pollfd pfd;
	pid_t pid;

	/* Ensure parent does not already have no_new_privs. */
	ret = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
	ASSERT_EQ(ret, 0) {
		TH_LOG("Parent already has no_new_privs set, cannot run test");
	}

	args.pidfd = ptr_to_u64(&pidfd);

	pid = sys_clone3(&args, sizeof(args));
	if (pid < 0 && errno == EINVAL)
		SKIP(return, "CLONE_NNP not supported");
	ASSERT_GE(pid, 0);

	if (pid == 0) {
		/*
		 * Child: check no_new_privs. Exit 0 if set, 1 if not.
		 */
		ret = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
		_exit(ret == 1 ? 0 : 1);
	}

	ASSERT_GE(pidfd, 0);

	/* Parent must still NOT have no_new_privs. */
	ret = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
	ASSERT_EQ(ret, 0) {
		TH_LOG("Parent got no_new_privs after creating CLONE_NNP child");
	}

	/* Wait for child to exit. */
	pfd.fd = pidfd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 5000);
	ASSERT_EQ(ret, 1);

	/* Verify child exited with 0 (no_new_privs was set). */
	ret = ioctl(pidfd, PIDFD_GET_INFO, &info);
	ASSERT_EQ(ret, 0);
	ASSERT_TRUE(info.mask & PIDFD_INFO_EXIT);
	ASSERT_TRUE(WIFEXITED(info.exit_code));
	ASSERT_EQ(WEXITSTATUS(info.exit_code), 0) {
		TH_LOG("Child did not have no_new_privs set");
	}

	close(pidfd);
}

/*
 * Test that CLONE_NNP with CLONE_THREAD fails with EINVAL.
 */
TEST(nnp_rejects_thread)
{
	struct __clone_args args = {
		.flags		= CLONE_NNP | CLONE_THREAD |
				  CLONE_SIGHAND | CLONE_VM,
		.exit_signal	= 0,
	};
	pid_t pid;

	pid = sys_clone3(&args, sizeof(args));
	ASSERT_EQ(pid, -1);
	ASSERT_EQ(errno, EINVAL);
}

/*
 * Test that a plain CLONE_AUTOREAP child does NOT get no_new_privs.
 * Only CLONE_NNP should set it.
 */
TEST(autoreap_no_new_privs_unset)
{
	struct pidfd_info info = { .mask = PIDFD_INFO_EXIT };
	int pidfd = -1, ret;
	struct pollfd pfd;
	pid_t pid;

	pid = create_autoreap_child(&pidfd);
	if (pid < 0 && errno == EINVAL)
		SKIP(return, "CLONE_AUTOREAP not supported");
	ASSERT_GE(pid, 0);

	if (pid == 0) {
		/*
		 * Child: check no_new_privs. Exit 0 if NOT set, 1 if set.
		 */
		ret = prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
		_exit(ret == 0 ? 0 : 1);
	}

	ASSERT_GE(pidfd, 0);

	pfd.fd = pidfd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 5000);
	ASSERT_EQ(ret, 1);

	ret = ioctl(pidfd, PIDFD_GET_INFO, &info);
	ASSERT_EQ(ret, 0);
	ASSERT_TRUE(info.mask & PIDFD_INFO_EXIT);
	ASSERT_TRUE(WIFEXITED(info.exit_code));
	ASSERT_EQ(WEXITSTATUS(info.exit_code), 0) {
		TH_LOG("Plain autoreap child unexpectedly has no_new_privs");
	}

	close(pidfd);
}

/*
 * Helper: create a child with CLONE_PIDFD | CLONE_PIDFD_AUTOKILL | CLONE_AUTOREAP | CLONE_NNP.
 */
static pid_t create_autokill_child(int *pidfd)
{
	struct __clone_args args = {
		.flags		= CLONE_PIDFD | CLONE_PIDFD_AUTOKILL |
				  CLONE_AUTOREAP | CLONE_NNP,
		.exit_signal	= 0,
		.pidfd		= ptr_to_u64(pidfd),
	};

	return sys_clone3(&args, sizeof(args));
}

/*
 * Basic autokill test: child blocks in pause(), parent closes the
 * clone3 pidfd, child should be killed and autoreaped.
 */
TEST(autokill_basic)
{
	int pidfd = -1, pollfd_fd = -1, ret;
	struct pollfd pfd;
	pid_t pid;

	pid = create_autokill_child(&pidfd);
	if (pid < 0 && errno == EINVAL)
		SKIP(return, "CLONE_PIDFD_AUTOKILL not supported");
	ASSERT_GE(pid, 0);

	if (pid == 0) {
		pause();
		_exit(1);
	}

	ASSERT_GE(pidfd, 0);

	/*
	 * Open a second pidfd via pidfd_open() so we can observe the
	 * child's death after closing the clone3 pidfd.
	 */
	pollfd_fd = sys_pidfd_open(pid, 0);
	ASSERT_GE(pollfd_fd, 0);

	/* Close the clone3 pidfd — this should trigger autokill. */
	close(pidfd);

	/* Wait for the child to die via the pidfd_open'd fd. */
	pfd.fd = pollfd_fd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 5000);
	ASSERT_EQ(ret, 1);
	ASSERT_TRUE(pfd.revents & POLLIN);

	/* Child should be autoreaped — no zombie. */
	usleep(100000);
	ret = waitpid(pid, NULL, WNOHANG);
	ASSERT_EQ(ret, -1);
	ASSERT_EQ(errno, ECHILD);

	close(pollfd_fd);
}

/*
 * CLONE_PIDFD_AUTOKILL without CLONE_PIDFD must fail with EINVAL.
 */
TEST(autokill_requires_pidfd)
{
	struct __clone_args args = {
		.flags		= CLONE_PIDFD_AUTOKILL | CLONE_AUTOREAP,
		.exit_signal	= 0,
	};
	pid_t pid;

	pid = sys_clone3(&args, sizeof(args));
	ASSERT_EQ(pid, -1);
	ASSERT_EQ(errno, EINVAL);
}

/*
 * CLONE_PIDFD_AUTOKILL without CLONE_AUTOREAP must fail with EINVAL.
 */
TEST(autokill_requires_autoreap)
{
	int pidfd = -1;
	struct __clone_args args = {
		.flags		= CLONE_PIDFD | CLONE_PIDFD_AUTOKILL,
		.exit_signal	= 0,
		.pidfd		= ptr_to_u64(&pidfd),
	};
	pid_t pid;

	pid = sys_clone3(&args, sizeof(args));
	ASSERT_EQ(pid, -1);
	ASSERT_EQ(errno, EINVAL);
}

/*
 * CLONE_PIDFD_AUTOKILL with CLONE_THREAD must fail with EINVAL.
 */
TEST(autokill_rejects_thread)
{
	int pidfd = -1;
	struct __clone_args args = {
		.flags		= CLONE_PIDFD | CLONE_PIDFD_AUTOKILL |
				  CLONE_AUTOREAP | CLONE_THREAD |
				  CLONE_SIGHAND | CLONE_VM,
		.exit_signal	= 0,
		.pidfd		= ptr_to_u64(&pidfd),
	};
	pid_t pid;

	pid = sys_clone3(&args, sizeof(args));
	ASSERT_EQ(pid, -1);
	ASSERT_EQ(errno, EINVAL);
}

/*
 * Test that only the clone3 pidfd triggers autokill, not pidfd_open().
 * Close the pidfd_open'd fd first — child should survive.
 * Then close the clone3 pidfd — child should be killed and autoreaped.
 */
TEST(autokill_pidfd_open_no_effect)
{
	int pidfd = -1, open_fd = -1, ret;
	struct pollfd pfd;
	pid_t pid;

	pid = create_autokill_child(&pidfd);
	if (pid < 0 && errno == EINVAL)
		SKIP(return, "CLONE_PIDFD_AUTOKILL not supported");
	ASSERT_GE(pid, 0);

	if (pid == 0) {
		pause();
		_exit(1);
	}

	ASSERT_GE(pidfd, 0);

	/* Open a second pidfd via pidfd_open(). */
	open_fd = sys_pidfd_open(pid, 0);
	ASSERT_GE(open_fd, 0);

	/*
	 * Close the pidfd_open'd fd — child should survive because
	 * only the clone3 pidfd has autokill.
	 */
	close(open_fd);
	usleep(200000);

	/* Verify child is still alive by polling the clone3 pidfd. */
	pfd.fd = pidfd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 0);
	ASSERT_EQ(ret, 0) {
		TH_LOG("Child died after closing pidfd_open fd — should still be alive");
	}

	/* Open another observation fd before triggering autokill. */
	open_fd = sys_pidfd_open(pid, 0);
	ASSERT_GE(open_fd, 0);

	/* Now close the clone3 pidfd — this triggers autokill. */
	close(pidfd);

	pfd.fd = open_fd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 5000);
	ASSERT_EQ(ret, 1);
	ASSERT_TRUE(pfd.revents & POLLIN);

	/* Child should be autoreaped — no zombie. */
	usleep(100000);
	ret = waitpid(pid, NULL, WNOHANG);
	ASSERT_EQ(ret, -1);
	ASSERT_EQ(errno, ECHILD);

	close(open_fd);
}

/*
 * Test that CLONE_PIDFD_AUTOKILL without CLONE_NNP fails with EPERM
 * for an unprivileged caller.
 */
TEST(autokill_requires_cap_sys_admin)
{
	int pidfd = -1, ret;
	struct __clone_args args = {
		.flags		= CLONE_PIDFD | CLONE_PIDFD_AUTOKILL |
				  CLONE_AUTOREAP,
		.exit_signal	= 0,
		.pidfd		= ptr_to_u64(&pidfd),
	};
	pid_t pid;

	/* Drop all capabilities so we lack CAP_SYS_ADMIN. */
	ret = drop_all_caps();
	ASSERT_EQ(ret, 0);

	pid = sys_clone3(&args, sizeof(args));
	ASSERT_EQ(pid, -1);
	ASSERT_EQ(errno, EPERM);
}

/*
 * Test that CLONE_PIDFD_AUTOKILL without CLONE_NNP succeeds with
 * CAP_SYS_ADMIN.
 */
TEST(autokill_without_nnp_with_cap)
{
	struct __clone_args args = {
		.flags		= CLONE_PIDFD | CLONE_PIDFD_AUTOKILL |
				  CLONE_AUTOREAP,
		.exit_signal	= 0,
	};
	struct pidfd_info info = { .mask = PIDFD_INFO_EXIT };
	int pidfd = -1, ret;
	struct pollfd pfd;
	pid_t pid;

	if (geteuid() != 0)
		SKIP(return, "Need root/CAP_SYS_ADMIN");

	args.pidfd = ptr_to_u64(&pidfd);

	pid = sys_clone3(&args, sizeof(args));
	if (pid < 0 && errno == EINVAL)
		SKIP(return, "CLONE_PIDFD_AUTOKILL not supported");
	ASSERT_GE(pid, 0);

	if (pid == 0)
		_exit(0);

	ASSERT_GE(pidfd, 0);

	/* Wait for child to exit. */
	pfd.fd = pidfd;
	pfd.events = POLLIN;
	ret = poll(&pfd, 1, 5000);
	ASSERT_EQ(ret, 1);

	ret = ioctl(pidfd, PIDFD_GET_INFO, &info);
	ASSERT_EQ(ret, 0);
	ASSERT_TRUE(info.mask & PIDFD_INFO_EXIT);
	ASSERT_TRUE(WIFEXITED(info.exit_code));
	ASSERT_EQ(WEXITSTATUS(info.exit_code), 0);

	close(pidfd);
}

TEST_HARNESS_MAIN