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 implementation shall require that addr be a multiple
8 * of the page size {PAGESIZE}.
9 *
10 * Test step:
11 * Try to call unmap, with addr NOT a multiple of page size.
12 * Should get EINVAL.
13 */
14
15#define _XOPEN_SOURCE 600
16
17#include <pthread.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21#include <sys/mman.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <sys/wait.h>
25#include <fcntl.h>
26#include <string.h>
27#include <errno.h>
28#include "posixtest.h"
29
30#define TNAME "munmap/3-1.c"
31
32int main()
33{
34  char tmpfname[256];
35  long file_size;
36
37  void *pa = NULL;
38  void *addr = NULL;
39  size_t len;
40  int flag;
41  int fd;
42  off_t off = 0;
43  int prot;
44
45  int page_size;
46
47  char *pa2;
48
49
50  page_size = sysconf(_SC_PAGE_SIZE);
51  file_size = 2 * page_size;
52
53  /* We hope to map 2 pages */
54  len = page_size + 1;
55
56  /* Create tmp file */
57  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_munmap_1_1_%ld",
58           (long)getpid());
59  unlink(tmpfname);
60  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL,
61            S_IRUSR | S_IWUSR);
62  if (fd == -1)
63  {
64    printf(TNAME " Error at open(): %s\n",
65           strerror(errno));
66    exit(PTS_UNRESOLVED);
67  }
68  unlink(tmpfname);
69
70  if (ftruncate (fd, file_size) == -1)
71  {
72    printf("Error at ftruncate: %s\n", strerror(errno));
73    exit(PTS_UNRESOLVED);
74  }
75
76  flag = MAP_SHARED;
77  prot = PROT_READ | PROT_WRITE;
78  pa = mmap(addr, len, prot, flag, fd, off);
79  if (pa == MAP_FAILED)
80  {
81  	printf ("Test Unresolved: " TNAME " Error at mmap: %s\n",
82            strerror(errno));
83    exit(PTS_UNRESOLVED);
84  }
85
86  /* pa should be a multiple of page size */
87  pa2 = pa;
88
89  while (((unsigned long)pa2 % page_size) == 0)
90    pa2++;
91
92  close (fd);
93  if (munmap (pa2, len) == -1 && errno == EINVAL)
94  {
95  	printf ("Got EINVAL\n");
96  	printf ("Test PASSED\n");
97    exit(PTS_PASS);
98  }
99  else
100  {
101  	printf ("Test FAILED: " TNAME " munmap returns: %s\n",
102            strerror(errno));
103    exit(PTS_FAIL);
104  }
105}
106