(root)/
strace-6.5/
tests/
count-f.c
       1  /*
       2   * This file is part of count-f strace test.
       3   *
       4   * Copyright (c) 2016-2021 Dmitry V. Levin <ldv@strace.io>
       5   * All rights reserved.
       6   *
       7   * SPDX-License-Identifier: GPL-2.0-or-later
       8   */
       9  
      10  #include "tests.h"
      11  #include <assert.h>
      12  #include <errno.h>
      13  #include <pthread.h>
      14  #include <stdio.h>
      15  #include <stdlib.h>
      16  #include <sys/wait.h>
      17  #include <unistd.h>
      18  
      19  #define N 32
      20  #define P 8
      21  #define T 4
      22  
      23  static void *
      24  thread(void *arg)
      25  {
      26  	assert(chdir(".") == 0);
      27  	for (unsigned int i = 0; i < N; ++i) {
      28  		assert(chdir("") == -1);
      29  		assert(chdir(".") == 0);
      30  	}
      31  
      32  	return NULL;
      33  }
      34  
      35  static int
      36  process(void)
      37  {
      38  	pthread_t t[T];
      39  
      40  	for (unsigned int i = 0; i < T; ++i) {
      41  		errno = pthread_create(&t[i], NULL, thread, NULL);
      42  		if (errno)
      43  			perror_msg_and_fail("pthread_create");
      44  	}
      45  
      46  	for (unsigned int i = 0; i < T; ++i) {
      47  		void *retval;
      48  		errno = pthread_join(t[i], &retval);
      49  		if (errno)
      50  			perror_msg_and_fail("pthread_join");
      51  	}
      52  
      53  	return 0;
      54  }
      55  
      56  int
      57  main(void)
      58  {
      59  	pid_t p[P];
      60  
      61  	for (unsigned int i = 0; i < P; ++i) {
      62  		p[i] = fork();
      63  		if (p[i] < 0)
      64  			perror_msg_and_fail("fork");
      65  		if (!p[i])
      66  			return process();
      67  	}
      68  	for (unsigned int i = 0; i < P; ++i) {
      69  		int s;
      70  		pid_t rc;
      71  
      72  		while ((rc = waitpid(p[i], &s, 0)) != p[i]) {
      73  			if (rc < 0 && errno == EINTR)
      74  				continue;
      75  			perror_msg_and_fail("waitpid: %d", p[i]);
      76  		}
      77  		assert(WIFEXITED(s));
      78  		if (WEXITSTATUS(s))
      79  			return WEXITSTATUS(s);
      80  	}
      81  
      82  	return 0;
      83  }