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