Skip to content

Commit

Permalink
zdtm: Check pidfd can kill descendant processes
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavik Sachdev <[email protected]>
  • Loading branch information
bsach64 committed Jul 29, 2024
1 parent f9c80f5 commit 9f40628
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/zdtm/static/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ TST_NOFILE := \
ptrace_sig \
pidfd_self \
pidfd_child \
pidfd_kill \
pipe00 \
pipe01 \
pipe02 \
Expand Down
99 changes: 99 additions & 0 deletions test/zdtm/static/pidfd_kill.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <wait.h>

#include "zdtmtst.h"

const char *test_doc = "Kill child and grandchild process using pidfds\n";
const char *test_author = "Bhavik Sachdev <[email protected]>";

static int pidfd_open(pid_t pid, unsigned int flags)
{
return syscall(SYS_pidfd_open, pid, flags);
}

static int pidfd_send_signal(int pidfd, int sig, siginfo_t* info, unsigned int flags)
{
return syscall(SYS_pidfd_send_signal, pidfd, sig, info, flags);
}

int main(int argc, char* argv[])
{
#define READ 0
#define WRITE 1

int child, gchild, cpidfd, gpidfd, gchild_pid;
int p[2];

if (pipe(p)) {
pr_perror("pipe");
return 1;
}

test_init(argc, argv);

child = fork();
if (child < 0) {
pr_perror("fork");
return 1;
} else if (child == 0) {
gchild = fork();
if (gchild < 0) {
pr_perror("fork");
return 1;
} else if (gchild == 0) {
test_waitsig();
test_msg("Grand child process waiting to be terminated...\n");
while(1);
}
close(p[READ]);
if (write(p[WRITE], &gchild, sizeof(gchild))
!= sizeof(gchild)) {
pr_perror("write");
return 1;
}
close(p[WRITE]);
test_waitsig();
test_msg("Child process waiting to be terminated...\n");
waitpid(-1, NULL, 0);
while(1);
}
cpidfd = pidfd_open(child, 0);
if (cpidfd < 0) {
pr_perror("pidfd_open");
return 1;
}
close(p[WRITE]);
if (read(p[READ], &gchild_pid, sizeof(gchild_pid))
!= sizeof(gchild_pid)) {
pr_perror("read");
return 1;
}
close(p[READ]);
gpidfd = pidfd_open(gchild_pid, 0);
if (gpidfd < 0) {
pr_perror("pidfd_open");
return 1;
}

test_daemon();
test_waitsig();

if (pidfd_send_signal(gpidfd, SIGKILL, NULL, 0)) {
fail("Could not send signal");
goto out;
}

if (pidfd_send_signal(cpidfd, SIGKILL, NULL, 0)) {
fail("Could not send signal");
goto out;
}

waitpid(-1, NULL, 0);
pass();
out:
close(cpidfd);
close(gpidfd);
return 0;
}

0 comments on commit 9f40628

Please sign in to comment.