1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vdso_test_gettimeofday.c: Sample code to test parse_vdso.c and
4  *                           vDSO gettimeofday()
5  * Copyright (c) 2014 Andy Lutomirski
6  *
7  * Compile with:
8  * gcc -std=gnu99 vdso_test_gettimeofday.c parse_vdso_gettimeofday.c
9  *
10  * Tested on x86, 32-bit and 64-bit.  It may work on other architectures, too.
11  */
12 
13 #include <stdio.h>
14 #include <sys/auxv.h>
15 #include <sys/time.h>
16 
17 #include "kselftest.h"
18 #include "parse_vdso.h"
19 #include "vdso_config.h"
20 #include "vdso_call.h"
21 
22 int main(int argc, char **argv)
23 {
24 	const char *version = versions[VDSO_VERSION];
25 	const char **name = (const char **)&names[VDSO_NAMES];
26 
27 	unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
28 	if (!sysinfo_ehdr) {
29 		printf("AT_SYSINFO_EHDR is not present!\n");
30 		return KSFT_SKIP;
31 	}
32 
33 	vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
34 
35 	/* Find gettimeofday. */
36 	typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
37 	gtod_t gtod = (gtod_t)vdso_sym(version, name[0]);
38 
39 	if (!gtod) {
40 		printf("Could not find %s\n", name[0]);
41 		return KSFT_SKIP;
42 	}
43 
44 	struct timeval tv;
45 	long ret = VDSO_CALL(gtod, 2, &tv, 0);
46 
47 	if (ret == 0) {
48 		printf("The time is %lld.%06lld\n",
49 		       (long long)tv.tv_sec, (long long)tv.tv_usec);
50 	} else {
51 		printf("%s failed\n", name[0]);
52 		return KSFT_FAIL;
53 	}
54 
55 	return 0;
56 }
57