1/*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * This file is licensed under the GPL license.  For the full content
4 * of this license, see the COPYING file at the top level of this
5 * source tree.
6 *
7 * The mmap( ) function shall fail if:
8 * [EOVERFLOW] The file is a regular file and the value of off
9 * plus len exceeds the offset maximum established in the open
10 * file description associated with fildes.
11 *
12 * Test Steps:
13 * 1. Set off and let to ULONG_MAX (make them align to page_size
14 * 2. Assume the offset maximum is ULONG_MAX (on both 32 and 64 system).
15 *
16 * FIXME: Not quite sure how to make "the value of off plus len
17 * exceeds the offset maxium established in the open file description
18 * associated with files".
19 *
20 */
21
22#define _XOPEN_SOURCE 600
23
24#include <pthread.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <limits.h>
29#include <sys/mman.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/wait.h>
33#include <fcntl.h>
34#include <string.h>
35#include <errno.h>
36#include "posixtest.h"
37
38#define TNAME "mmap/31-1.c"
39
40int main()
41{
42  char tmpfname[256];
43
44  void *pa = NULL;
45  void *addr = NULL;
46  size_t len;
47  int flag;
48  int fd;
49  off_t off = 0;
50  int prot;
51
52  long page_size = sysconf(_SC_PAGE_SIZE);
53
54
55  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_31_1_%ld",
56           (long)getpid());
57  unlink(tmpfname);
58  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL,
59            S_IRUSR | S_IWUSR);
60  if (fd == -1)
61  {
62    printf(TNAME " Error at open(): %s\n",
63           strerror(errno));
64    exit(PTS_UNRESOLVED);
65  }
66  unlink(tmpfname);
67
68  flag = MAP_SHARED;
69  prot = PROT_READ | PROT_WRITE;
70
71  /* len + off > maximum offset
72   * FIXME: We assume maximum offset is ULONG_MAX
73   * */
74
75  len = ULONG_MAX;
76  if (len % page_size)
77  {
78    /* Lower boundary */
79    len &= ~(page_size - 1);
80  }
81
82  off = ULONG_MAX;
83  if (off % page_size)
84  {
85    /* Lower boundary */
86    off &= ~(page_size - 1);
87  }
88
89  printf("off: %lx, len: %lx\n", (unsigned long)off,
90		(unsigned long)len);
91  pa = mmap(addr, len, prot, flag, fd, off);
92  if (pa == MAP_FAILED && errno == EOVERFLOW)
93  {
94  	printf ("Test Pass: " TNAME " Error at mmap: %s\n",
95            strerror(errno));
96    exit(PTS_PASS);
97  }
98
99  if (pa == MAP_FAILED)
100    perror("Test FAIL: expect EOVERFLOW but get other error");
101  else
102    printf ("Test FAIL : Expect EOVERFLOW but got no error\n");
103
104  close (fd);
105  munmap (pa, len);
106  return PTS_FAIL;
107}
108