vdso_test_gettimeofday.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include <stdint.h>
  13. #include <elf.h>
  14. #include <stdio.h>
  15. #include <sys/auxv.h>
  16. #include <sys/time.h>
  17. #include "../kselftest.h"
  18. #include "parse_vdso.h"
  19. #include "vdso_config.h"
  20. #include "vdso_call.h"
  21. int main(int argc, char **argv)
  22. {
  23. const char *version = versions[VDSO_VERSION];
  24. const char **name = (const char **)&names[VDSO_NAMES];
  25. unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
  26. if (!sysinfo_ehdr) {
  27. printf("AT_SYSINFO_EHDR is not present!\n");
  28. return KSFT_SKIP;
  29. }
  30. vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
  31. /* Find gettimeofday. */
  32. typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
  33. gtod_t gtod = (gtod_t)vdso_sym(version, name[0]);
  34. if (!gtod) {
  35. printf("Could not find %s\n", name[0]);
  36. return KSFT_SKIP;
  37. }
  38. struct timeval tv;
  39. long ret = VDSO_CALL(gtod, 2, &tv, 0);
  40. if (ret == 0) {
  41. printf("The time is %lld.%06lld\n",
  42. (long long)tv.tv_sec, (long long)tv.tv_usec);
  43. } else {
  44. printf("%s failed\n", name[0]);
  45. return KSFT_FAIL;
  46. }
  47. return 0;
  48. }