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 * [ENXIO] Addresses in the range [off,off+len) are invalid
9 * for the object specified by fildes.
10 *
11 * Test Step:
12 * 1. mmap a shared memory object into memory;
13 * 2. (off + len) will beyond the the shared memory object;
14 *
15 * FIXME: Not sure what does "invalid" mean here
16 *
17 * This mmap might trigger SIGBUS on some system.
18 */
19
20#define _XOPEN_SOURCE 600
21#include <pthread.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include <sys/mman.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29#include <string.h>
30#include <errno.h>
31#include <signal.h>
32#include "posixtest.h"
33
34#define TNAME "mmap/28-1.c"
35
36void sigbus_handler (int signum)
37{
38  printf("Test UNRESOLVED: Triger SIGBUS\n");
39  exit(PTS_UNRESOLVED);
40}
41
42int main()
43{
44  char tmpfname[256];
45  int shm_fd;
46  long shm_size;
47
48  void *pa = NULL;
49  void *addr = NULL;
50  size_t len = 0;
51  int prot = PROT_READ | PROT_WRITE;
52  int flag;
53  int fd;
54  off_t off = 0;
55
56  long page_size = sysconf(_SC_PAGE_SIZE);
57  shm_size = 2 * page_size;
58
59  if (signal(SIGBUS, sigbus_handler) == SIG_ERR)
60  {
61		printf(TNAME " Error at signal(): %s\n", strerror(errno));
62		return PTS_UNRESOLVED;
63  }
64
65  snprintf(tmpfname, sizeof(tmpfname), "pts_mmap_28_1_%ld",
66           (long)getpid());
67  /* Create shared object */
68	shm_unlink(tmpfname);
69	shm_fd = shm_open(tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
70	if(shm_fd == -1)
71	{
72		printf(TNAME " Error at shm_open(): %s\n", strerror(errno));
73		return PTS_UNRESOLVED;
74	}
75  shm_unlink(tmpfname);
76  if(ftruncate(shm_fd, shm_size) == -1) {
77    printf(TNAME " Error at ftruncate(): %s\n", strerror(errno));
78    return PTS_UNRESOLVED;
79  }
80
81  /* (off + len) will go outside the object */
82  fd = shm_fd;
83  len = 2 * shm_size;
84  flag = MAP_SHARED;
85  off = page_size;
86  pa = mmap (addr, len, prot, flag, fd, off);
87  if (pa != MAP_FAILED )
88  {
89    printf ("Test Fail: " TNAME " Got no error at mmap()\n");
90    close(fd);
91    munmap(pa, len);
92    exit(PTS_FAIL);
93  }
94  else if (errno != ENXIO)
95  {
96    printf ("Test Fail: " TNAME " Did not get ENXIO,"
97            " get other error: %s\n", strerror(errno));
98    exit(PTS_FAIL);
99  }
100
101  printf ("Test Pass\n");
102  return PTS_PASS;
103}
104