(root)/
strace-6.5/
tests-m32/
filter-unavailable.c
       1  /*
       2   * Copyright (c) 2015-2021 Dmitry V. Levin <ldv@strace.io>
       3   * All rights reserved.
       4   *
       5   * SPDX-License-Identifier: GPL-2.0-or-later
       6   */
       7  
       8  #include "tests.h"
       9  #include <assert.h>
      10  #include <stdlib.h>
      11  #include <unistd.h>
      12  #include <pthread.h>
      13  #include <sys/wait.h>
      14  
      15  #define P 16
      16  #define T 7
      17  
      18  static void *
      19  thread(void *arg)
      20  {
      21  	assert(write(1, "", 1) == 1);
      22  	pause();
      23  	return arg;
      24  }
      25  
      26  static int
      27  process(void)
      28  {
      29  	int fds[2];
      30  	pthread_t t;
      31  	struct timespec ts = { .tv_nsec = 10000000 };
      32  
      33  	(void) close(0);
      34  	(void) close(1);
      35  	if (pipe(fds))
      36  		perror_msg_and_fail("pipe");
      37  
      38  	for (int i = 0; i < T; ++i)
      39  		assert(pthread_create(&t, NULL, thread, NULL) == 0);
      40  	for (int i = 0; i < T; ++i)
      41  		assert(read(0, fds, 1) == 1);
      42  
      43  	(void) nanosleep(&ts, 0);
      44  	return 0;
      45  }
      46  
      47  int
      48  main(void)
      49  {
      50  	for (int i = 0; i < P; ++i) {
      51  		pid_t p = fork();
      52  		if (p < 0)
      53  			perror_msg_and_fail("fork");
      54  		if (p == 0)
      55  			return process();
      56  		int s;
      57  		assert(waitpid(p, &s, 0) == p);
      58  		assert(WIFEXITED(s));
      59  		if (WEXITSTATUS(s))
      60  			return WEXITSTATUS(s);
      61  	}
      62  	return 0;
      63  }