Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mmap001.c « ltp « winsup.api « testsuite « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c47d49c1036de6d1ecd1db5f3c0e48d71019113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * mmap001.c - Tests mmapping a big file and writing it once
 *
 * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
 *                    Aaron Laffin <alaffin@sgi.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>

#include "test.h"
#include "usctest.h"

const char *TCID="mmap001";
int TST_TOTAL=5;
extern int Tst_count;
static char *filename=NULL;
static int m_opt = 0;
static char *m_copt;

void cleanup()
{
  /*
   * remove the tmp directory and exit
   */

  if ( filename )
    free(filename);

  TEST_CLEANUP;

  tst_rmdir();

  tst_exit();
}

void setup()
{
  char buf[1024];
  /*
   * setup a default signal hander and a
   * temporary working directory.
   */
  tst_sig(FORK, DEF_HANDLER, cleanup);

  tst_tmpdir();

  TEST_PAUSE;

  snprintf(buf,1024,"testfile.%d",getpid());

  filename = strdup(buf);
}

void help()
{
  printf("  -m x    size of mmap in pages (default 1000)\n");
}

/*
 * add the -m option whose parameter is the
 * pages that should be mapped.
 */
option_t options[] = 
{
  { "m:", &m_opt, &m_copt },
  { NULL, NULL, NULL }
};

int main(int argc, char * argv[])
{
  char *array;
  const char *msg;
  int i,lc;
  int fd;
  int pages,memsize;

  if ( (msg=parse_opts(argc, argv, options, help)) != (char *) NULL )
   tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);

  if ( m_opt )
  {
    memsize = pages = atoi( m_copt );

    if (memsize < 1)
    {
      tst_brkm(TBROK, cleanup, "Invalid arg for -m: %s",m_copt);
    }

    memsize *= getpagesize(); /* N PAGES */

  }
  else
  {
    /*
     * default size 1000 pages;
     */
    memsize = pages = 1000;
    memsize *= getpagesize();
  }

  tst_resm(TINFO,"mmap()ing file of %u pages or %u bytes", pages,memsize);

  setup();

  for (lc=0; TEST_LOOPING(lc); lc++)
  {
    Tst_count=0;

    fd = open(filename, O_RDWR | O_CREAT, 0666);
    if ((fd == -1))
      tst_brkm(TBROK, cleanup, "Problems opening files");
        
    if (lseek(fd, memsize, SEEK_SET) != memsize)
    {
      close(fd);
      tst_brkm(TBROK, cleanup, "Problems doing the lseek: %d: %s",
          errno,strerror(errno));
    }

    if (write(fd,"\0",1) !=1)
    {
      close(fd);
      tst_brkm(TBROK, cleanup, "Problems writing: %d: %s",
        errno,strerror(errno));
    }
 
    array = mmap(0, memsize, PROT_WRITE, MAP_SHARED,fd,0);
    if (array == (char *)MAP_FAILED)
    {
      tst_resm(TFAIL, "mmap() failed: %d: %s",
        errno,strerror(errno));
      tst_exit();
    }
    else
    {
      tst_resm(TPASS, "mmap() completed successfully.");
    }

    if ( STD_FUNCTIONAL_TEST )
    {
      tst_resm(TINFO,"touching mmaped memory");

      for(i = 0; i < memsize; i++)
      {
       array[i] = (char) i;
      } 
 
      /*
      * seems that if the map area was bad, we'd get SEGV, hence we can
      * indicate a PASS.
      */
      tst_resm(TPASS, "we're still here, mmaped area must be good");

      TEST( msync(array, memsize, MS_SYNC) );

      if ( TEST_RETURN == -1 )
      {
        tst_resm(TFAIL, "msync() failed: errno: %d: %s",
          TEST_ERRNO, strerror(TEST_ERRNO));
      }
      else
      {
        tst_resm(TPASS, "msync() was successful");
      }

    } /* STD_FUNCTIONAL_TEST */

    TEST( munmap(array, memsize) );

    if ( TEST_RETURN == -1 )
    {
      tst_resm(TFAIL, "munmap() failed: errno: %d: %s",
        TEST_ERRNO, strerror(TEST_ERRNO));
    }
    else
    {
      tst_resm(TPASS, "munmap() was successful");
    }

    close(fd);
    unlink(filename);
  }
  cleanup();
  return 0;
}