xref: /linux/tools/testing/selftests/futex/functional/futex_wait_wouldblock.c (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
3  *
4  *   Copyright © International Business Machines  Corp., 2009
5  *
6  * DESCRIPTION
7  *      Test if FUTEX_WAIT op returns -EWOULDBLOCK if the futex value differs
8  *      from the expected one.
9  *
10  * AUTHOR
11  *      Gowrishankar <gowrishankar.m@in.ibm.com>
12  *
13  * HISTORY
14  *      2009-Nov-14: Initial version by Gowrishankar <gowrishankar.m@in.ibm.com>
15  *
16  *****************************************************************************/
17 
18 #include <errno.h>
19 #include <getopt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include "futextest.h"
25 #include "logging.h"
26 
27 #define TEST_NAME "futex-wait-wouldblock"
28 #define timeout_ns 100000
29 
30 void usage(char *prog)
31 {
32 	printf("Usage: %s\n", prog);
33 	printf("  -c	Use color\n");
34 	printf("  -h	Display this help message\n");
35 	printf("  -v L	Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
36 	       VQUIET, VCRITICAL, VINFO);
37 }
38 
39 int main(int argc, char *argv[])
40 {
41 	struct timespec to = {.tv_sec = 0, .tv_nsec = timeout_ns};
42 	futex_t f1 = FUTEX_INITIALIZER;
43 	int res, ret = RET_PASS;
44 	int c;
45 
46 	while ((c = getopt(argc, argv, "cht:v:")) != -1) {
47 		switch (c) {
48 		case 'c':
49 			log_color(1);
50 			break;
51 		case 'h':
52 			usage(basename(argv[0]));
53 			exit(0);
54 		case 'v':
55 			log_verbosity(atoi(optarg));
56 			break;
57 		default:
58 			usage(basename(argv[0]));
59 			exit(1);
60 		}
61 	}
62 
63 	ksft_print_header();
64 	ksft_set_plan(1);
65 	ksft_print_msg("%s: Test the unexpected futex value in FUTEX_WAIT\n",
66 	       basename(argv[0]));
67 
68 	info("Calling futex_wait on f1: %u @ %p with val=%u\n", f1, &f1, f1+1);
69 	res = futex_wait(&f1, f1+1, &to, FUTEX_PRIVATE_FLAG);
70 	if (!res || errno != EWOULDBLOCK) {
71 		fail("futex_wait returned: %d %s\n",
72 		     res ? errno : res, res ? strerror(errno) : "");
73 		ret = RET_FAIL;
74 	}
75 
76 	print_result(TEST_NAME, ret);
77 	return ret;
78 }
79