1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. 4f08c3bdfSopenharmony_ci * Author: Xiao Yang <yangx.jy@cn.fujitsu.com> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/*\ 8f08c3bdfSopenharmony_ci * [Description] 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * Test PR_SET_CHILD_SUBREAPER and PR_GET_CHILD_SUBREAPER of prctl(2). 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * - If PR_SET_CHILD_SUBREAPER marks a process as a child subreaper, it 13f08c3bdfSopenharmony_ci * fulfills the role of init(1) for its descendant orphaned process. 14f08c3bdfSopenharmony_ci * The PPID of its orphaned process will be reparented to the subreaper 15f08c3bdfSopenharmony_ci * process, and the subreaper process can receive a SIGCHLD signal and 16f08c3bdfSopenharmony_ci * wait(2) on the orphaned process to discover corresponding termination 17f08c3bdfSopenharmony_ci * status. 18f08c3bdfSopenharmony_ci * 19f08c3bdfSopenharmony_ci * - The setting of PR_SET_CHILD_SUBREAPER is not inherited by children 20f08c3bdfSopenharmony_ci * reated by fork(2). 21f08c3bdfSopenharmony_ci * 22f08c3bdfSopenharmony_ci * - PR_GET_CHILD_SUBREAPER can get the setting of PR_SET_CHILD_SUBREAPER. 23f08c3bdfSopenharmony_ci * 24f08c3bdfSopenharmony_ci * These flags was added by kernel commit ebec18a6d3aa: 25f08c3bdfSopenharmony_ci * "prctl: add PR_{SET,GET}_CHILD_SUBREAPER to allow simple process supervision" 26f08c3bdfSopenharmony_ci */ 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_ci#include <errno.h> 29f08c3bdfSopenharmony_ci#include <stdlib.h> 30f08c3bdfSopenharmony_ci#include <unistd.h> 31f08c3bdfSopenharmony_ci#include <sys/types.h> 32f08c3bdfSopenharmony_ci#include <sys/wait.h> 33f08c3bdfSopenharmony_ci#include <signal.h> 34f08c3bdfSopenharmony_ci#include <sys/prctl.h> 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci#include "tst_test.h" 37f08c3bdfSopenharmony_ci#include "lapi/prctl.h" 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_cistatic volatile int sigchild_recv; 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_cistatic void check_get_subreaper(int exp_val) 42f08c3bdfSopenharmony_ci{ 43f08c3bdfSopenharmony_ci int get_val; 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci TEST(prctl(PR_GET_CHILD_SUBREAPER, &get_val)); 46f08c3bdfSopenharmony_ci if (TST_RET == -1) { 47f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "prctl(PR_GET_CHILD_SUBREAPER) failed"); 48f08c3bdfSopenharmony_ci return; 49f08c3bdfSopenharmony_ci } 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci if (get_val == exp_val) { 52f08c3bdfSopenharmony_ci tst_res(TPASS, "prctl(PR_GET_CHILD_SUBREAPER) got expected %d", 53f08c3bdfSopenharmony_ci get_val); 54f08c3bdfSopenharmony_ci } else { 55f08c3bdfSopenharmony_ci tst_res(TFAIL, "prctl(PR_GET_CHILD_SUBREAPER) got %d, expected %d", 56f08c3bdfSopenharmony_ci get_val, exp_val); 57f08c3bdfSopenharmony_ci } 58f08c3bdfSopenharmony_ci} 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_cistatic void verify_prctl(void) 61f08c3bdfSopenharmony_ci{ 62f08c3bdfSopenharmony_ci int status, ret; 63f08c3bdfSopenharmony_ci pid_t pid; 64f08c3bdfSopenharmony_ci pid_t ppid = getpid(); 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci sigchild_recv = 0; 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci TEST(prctl(PR_SET_CHILD_SUBREAPER, 1)); 69f08c3bdfSopenharmony_ci if (TST_RET == -1) { 70f08c3bdfSopenharmony_ci if (TST_ERR == EINVAL) { 71f08c3bdfSopenharmony_ci tst_res(TCONF, 72f08c3bdfSopenharmony_ci "prctl() doesn't support PR_SET_CHILD_SUBREAPER"); 73f08c3bdfSopenharmony_ci } else { 74f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, 75f08c3bdfSopenharmony_ci "prctl(PR_SET_CHILD_SUBREAPER) failed"); 76f08c3bdfSopenharmony_ci } 77f08c3bdfSopenharmony_ci return; 78f08c3bdfSopenharmony_ci } 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci tst_res(TPASS, "prctl(PR_SET_CHILD_SUBREAPER) succeeded"); 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci pid = SAFE_FORK(); 83f08c3bdfSopenharmony_ci if (!pid) { 84f08c3bdfSopenharmony_ci pid_t cpid; 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci cpid = SAFE_FORK(); 87f08c3bdfSopenharmony_ci if (!cpid) { 88f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAIT(0); 89f08c3bdfSopenharmony_ci if (getppid() != ppid) { 90f08c3bdfSopenharmony_ci tst_res(TFAIL, 91f08c3bdfSopenharmony_ci "PPID of orphaned process was not reparented"); 92f08c3bdfSopenharmony_ci exit(0); 93f08c3bdfSopenharmony_ci } 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci tst_res(TPASS, "PPID of orphaned process was reparented"); 96f08c3bdfSopenharmony_ci exit(0); 97f08c3bdfSopenharmony_ci } 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci check_get_subreaper(0); 100f08c3bdfSopenharmony_ci exit(0); 101f08c3bdfSopenharmony_ci } 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_ci SAFE_WAITPID(pid, NULL, 0); 104f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE(0); 105f08c3bdfSopenharmony_ci ret = wait(&status); 106f08c3bdfSopenharmony_ci if (ret > 0) { 107f08c3bdfSopenharmony_ci tst_res(TPASS, "wait() got orphaned process, pid %d, status %d", 108f08c3bdfSopenharmony_ci ret, status); 109f08c3bdfSopenharmony_ci } else { 110f08c3bdfSopenharmony_ci tst_res(TFAIL | TERRNO, 111f08c3bdfSopenharmony_ci "wait() failed to get orphaned process"); 112f08c3bdfSopenharmony_ci } 113f08c3bdfSopenharmony_ci 114f08c3bdfSopenharmony_ci if (sigchild_recv == 2) 115f08c3bdfSopenharmony_ci tst_res(TPASS, "received SIGCHLD from orphaned process"); 116f08c3bdfSopenharmony_ci else 117f08c3bdfSopenharmony_ci tst_res(TFAIL, "didn't receive SIGCHLD from orphaned process"); 118f08c3bdfSopenharmony_ci 119f08c3bdfSopenharmony_ci check_get_subreaper(1); 120f08c3bdfSopenharmony_ci} 121f08c3bdfSopenharmony_ci 122f08c3bdfSopenharmony_cistatic void sighandler(int sig LTP_ATTRIBUTE_UNUSED) 123f08c3bdfSopenharmony_ci{ 124f08c3bdfSopenharmony_ci sigchild_recv++; 125f08c3bdfSopenharmony_ci} 126f08c3bdfSopenharmony_ci 127f08c3bdfSopenharmony_cistatic void setup(void) 128f08c3bdfSopenharmony_ci{ 129f08c3bdfSopenharmony_ci SAFE_SIGNAL(SIGCHLD, sighandler); 130f08c3bdfSopenharmony_ci} 131f08c3bdfSopenharmony_ci 132f08c3bdfSopenharmony_cistatic struct tst_test test = { 133f08c3bdfSopenharmony_ci .setup = setup, 134f08c3bdfSopenharmony_ci .forks_child = 1, 135f08c3bdfSopenharmony_ci .needs_checkpoints = 1, 136f08c3bdfSopenharmony_ci .test_all = verify_prctl, 137f08c3bdfSopenharmony_ci}; 138