• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * Licensed under the GNU GPLv2 or later.
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program;  if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19  /*
20   * Block on a futex and wait for wakeup.
21   *
22   * This tests uses shared memory page to store the mutex variable.
23   */
24 
25 #include <sys/mman.h>
26 #include <sys/wait.h>
27 #include <errno.h>
28 
29 #include "test.h"
30 #include "safe_macros.h"
31 #include "futextest.h"
32 
33 const char *TCID="futex_wait02";
34 const int TST_TOTAL=1;
35 
36 static futex_t *futex;
37 
do_child(void)38 static void do_child(void)
39 {
40 	int ret;
41 
42 	tst_process_state_wait2(getppid(), 'S');
43 
44 	ret = futex_wake(futex, 1, 0);
45 
46 	if (ret != 1)
47 		tst_brkm(TFAIL, NULL, "futex_wake() returned %i", ret);
48 
49 	exit(TPASS);
50 }
51 
verify_futex_wait(void)52 static void verify_futex_wait(void)
53 {
54 	int res;
55 	int pid;
56 
57 	pid = tst_fork();
58 
59 	switch (pid) {
60 	case 0:
61 		do_child();
62 	break;
63 	case -1:
64 		tst_brkm(TBROK | TERRNO, NULL, "fork() failed");
65 	break;
66 	default:
67 	break;
68 	}
69 
70 	res = futex_wait(futex, *futex, NULL, 0);
71 
72 	if (res) {
73 		tst_resm(TFAIL, "futex_wait() returned %i, errno %s",
74 		         res, tst_strerrno(errno));
75 	}
76 
77 	SAFE_WAIT(NULL, &res);
78 
79 	if (WIFEXITED(res) && WEXITSTATUS(res) == TPASS)
80 		tst_resm(TPASS, "futex_wait() woken up");
81 	else
82 		tst_resm(TFAIL, "child failed");
83 }
84 
setup(void)85 static void setup(void)
86 {
87 	futex = SAFE_MMAP(NULL, NULL, sizeof(*futex), PROT_READ | PROT_WRITE,
88 			  MAP_ANONYMOUS | MAP_SHARED, -1, 0);
89 
90 	*futex = FUTEX_INITIALIZER;
91 }
92 
main(int argc,char * argv[])93 int main(int argc, char *argv[])
94 {
95 	int lc;
96 
97 	tst_parse_opts(argc, argv, NULL, NULL);
98 
99 	setup();
100 
101 	for (lc = 0; TEST_LOOPING(lc); lc++)
102 		verify_futex_wait();
103 
104 	tst_exit();
105 }
106