]> granicus.if.org Git - strace/blob - tests/count-f.c
tests: change the license to GPL-2.0-or-later
[strace] / tests / count-f.c
1 /*
2  * This file is part of count-f strace test.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
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         unsigned int i;
27
28         assert(chdir(".") == 0);
29         for (i = 0; i < N; ++i) {
30                 assert(chdir("") == -1);
31                 assert(chdir(".") == 0);
32         }
33
34         return NULL;
35 }
36
37 static int
38 process(void)
39 {
40         unsigned int i;
41         pthread_t t[T];
42
43         for (i = 0; i < T; ++i) {
44                 errno = pthread_create(&t[i], NULL, thread, NULL);
45                 if (errno)
46                         perror_msg_and_fail("pthread_create");
47         }
48
49         for (i = 0; i < T; ++i) {
50                 void *retval;
51                 errno = pthread_join(t[i], &retval);
52                 if (errno)
53                         perror_msg_and_fail("pthread_join");
54         }
55
56         return 0;
57 }
58
59 int
60 main(void)
61 {
62         unsigned int i;
63         pid_t p[P];
64
65         for (i = 0; i < P; ++i) {
66                 p[i] = fork();
67                 if (p[i] < 0)
68                         perror_msg_and_fail("fork");
69                 if (!p[i])
70                         return process();
71         }
72         for (i = 0; i < P; ++i) {
73                 int s;
74
75                 assert(waitpid(p[i], &s, 0) == p[i]);
76                 assert(WIFEXITED(s));
77                 if (WEXITSTATUS(s))
78                         return WEXITSTATUS(s);
79         }
80
81         return 0;
82 }