(root)/
strace-6.5/
tests/
status-successful-threads.c
       1  /*
       2   * Check status=successful filtering when a non-leader thread invokes execve.
       3   *
       4   * Copyright (c) 2019 Paul Chaignon <paul.chaignon@gmail.com>
       5   * Copyright (c) 2022 Dmitry V. Levin <ldv@strace.io>
       6   * All rights reserved.
       7   *
       8   * SPDX-License-Identifier: GPL-2.0-or-later
       9   */
      10  
      11  #include "tests.h"
      12  #include "scno.h"
      13  
      14  #include <errno.h>
      15  #include <pthread.h>
      16  #include <stdio.h>
      17  #include <unistd.h>
      18  #include <sys/uio.h>
      19  
      20  static pid_t leader;
      21  
      22  static void *
      23  thread_exec(void *arg)
      24  {
      25  	pid_t tid = syscall(__NR_gettid);
      26  	printf("%-5d +++ superseded by execve in pid %u +++\n",
      27  	       leader, tid);
      28  
      29  	char *argv[] = {((char **) arg)[0], (char *) "0", NULL};
      30  	execve(argv[0], argv, NULL);
      31  	perror_msg_and_fail("execve");
      32  }
      33  
      34  int
      35  main(int ac, char **av)
      36  {
      37  	if (ac > 1)
      38  		return 0;
      39  
      40  	int fds[2];
      41  	if (pipe(fds))
      42  		perror_msg_and_fail("pipe");
      43  
      44  	pid_t child = fork();
      45  	if (child < 0)
      46  		perror_msg_and_fail("fork");
      47  
      48  	leader = getpid();
      49  	setvbuf(stdout, NULL, _IONBF, 0);
      50  
      51  	if (!child) {
      52  		close(fds[0]);
      53  		pthread_t thre;
      54  		errno = pthread_create(&thre, NULL, thread_exec, av);
      55  		if (errno)
      56  			perror_msg_and_fail("pthread_create");
      57  		for (;;) { /* wait for execve */ }
      58  		return 1;
      59  	}
      60  
      61  	close(fds[1]);
      62  	unsigned int len = sizeof(fds[1]);
      63  	struct iovec rio = { .iov_base = &fds[1], .iov_len = len };
      64  	if (readv(fds[0], &rio, 1))
      65  		perror_msg_and_fail("readv");
      66  
      67  	printf("%-5d readv(%d, [{iov_base=\"\", iov_len=%u}], 1) = 0\n"
      68  	       "%-5d +++ exited with 0 +++\n",
      69  	       leader, fds[0], len, leader);
      70  	return 0;
      71  }