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