mmap.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* The mmap test is useful, since syscalls with 6 arguments
  2. * (as mmap) are done differently on various architectures.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <sys/mman.h>
  10. #define SIZEOF_ARRAY(type) (sizeof(type)/sizeof(*type))
  11. struct mmap_test {
  12. void *ret;
  13. int err;
  14. struct {
  15. void *start;
  16. size_t length;
  17. int prot;
  18. int flags;
  19. int fd;
  20. off_t offset;
  21. } args;
  22. };
  23. struct mmap_test tests[] = {
  24. [0] {
  25. .err = 0,
  26. .args.start = NULL,
  27. .args.length = 4096,
  28. .args.prot = PROT_READ|PROT_WRITE,
  29. .args.flags = MAP_PRIVATE|MAP_ANONYMOUS,
  30. .args.fd = 0,
  31. .args.offset = 0
  32. },
  33. };
  34. #define err(fmt, args...) \
  35. do { \
  36. fprintf(stderr, fmt "\n" , ## args); \
  37. exit(1); \
  38. } while (0)
  39. #define errp(fmt, args...) err(fmt ": %s" , ## args , strerror(errno))
  40. int main(int argc, char **argv)
  41. {
  42. int i;
  43. struct mmap_test *t;
  44. for (i=0; i<SIZEOF_ARRAY(tests); ++i) {
  45. t = tests + i;
  46. errno = 0;
  47. t->ret = mmap(t->args.start, t->args.length, t->args.prot,
  48. t->args.flags, t->args.fd, t->args.offset);
  49. if (t->err) {
  50. if (t->ret != MAP_FAILED)
  51. err("mmap test %i should have failed, but gave us %p", i, t->ret);
  52. else if (t->err != errno)
  53. errp("mmap test %i failed, but gave us wrong errno (got %i instead of %i)", i, errno, t->err);
  54. } else {
  55. if (t->ret == MAP_FAILED)
  56. errp("mmap test %i failed", i);
  57. else if (munmap(t->ret, t->args.length) != 0)
  58. errp("munmap test %i failed", i);
  59. }
  60. }
  61. exit(0);
  62. }