-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zdtm: Check pidfd can kill descendant processes
Signed-off-by: Bhavik Sachdev <[email protected]>
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ TST_NOFILE := \ | |
ptrace_sig \ | ||
pidfd_self \ | ||
pidfd_child \ | ||
pidfd_kill \ | ||
pipe00 \ | ||
pipe01 \ | ||
pipe02 \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |