(root)/
strace-6.5/
tests/
kill_child.c
       1  /*
       2   * Check for the corner case that previously lead to segfault
       3   * due to an attempt to access unitialised tcp->s_ent.
       4   *
       5   * 13994 ???( <unfinished ...>
       6   * ...
       7   * 13994 <... ??? resumed>) = ?
       8   *
       9   * Copyright (c) 2019-2022 The strace developers.
      10   * All rights reserved.
      11   *
      12   * SPDX-License-Identifier: GPL-2.0-or-later
      13   */
      14  
      15  #include "tests.h"
      16  
      17  #include <sched.h>
      18  #include <signal.h>
      19  #include <unistd.h>
      20  #include <sys/mman.h>
      21  #include <sys/wait.h>
      22  
      23  #define ITERS    10000
      24  #define SC_ITERS 10000
      25  
      26  int
      27  main(void)
      28  {
      29  	volatile sig_atomic_t *const mem =
      30  		mmap(NULL, get_page_size(), PROT_READ | PROT_WRITE,
      31  		     MAP_SHARED | MAP_ANONYMOUS, -1, 0);
      32  	if (mem == MAP_FAILED)
      33  		perror_msg_and_fail("mmap");
      34  
      35  	for (unsigned int i = 0; i < ITERS; ++i) {
      36  		mem[0] = mem[1] = 0;
      37  
      38  		const pid_t pid = fork();
      39  		if (pid < 0)
      40  			perror_msg_and_fail("fork");
      41  
      42  		if (!pid) {
      43  			/* wait for the parent */
      44  			while (!mem[0])
      45  				;
      46  			/* let the parent know we are running */
      47  			mem[1] = 1;
      48  
      49  			for (unsigned int j = 0; j < SC_ITERS; j++)
      50  				sched_yield();
      51  
      52  			pause();
      53  			return 0;
      54  		}
      55  
      56  		/* let the child know we are running */
      57  		mem[0] = 1;
      58  		/* wait for the child */
      59  		while (!mem[1])
      60  			;
      61  
      62  		if (kill(pid, SIGKILL))
      63  			perror_msg_and_fail("kill");
      64  		if (wait(NULL) != pid)
      65  			perror_msg_and_fail("wait");
      66  	}
      67  
      68  	return 0;
      69  }