VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/sharedfolders/testcase/tstmmap.c

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/* $Id: tstmmap.c 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * vboxsf - Simple writable mmap testcase.
4 */
5
6/*
7 * Copyright (C) 2019-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <stdio.h>
33#include <stdint.h>
34#include <string.h>
35#include <unistd.h>
36#include <fcntl.h>
37#include <sys/mman.h>
38
39
40int main(int argc, char **argv)
41{
42 uint8_t abBuf[4096];
43 int fd;
44 size_t cErrors = 0;
45 size_t cbFile;
46 size_t offFile;
47 uint8_t *pbMapping;
48 const char *pszFile = "tstmmap-file1";
49 if (argc > 1)
50 pszFile = argv[1];
51
52 fd = open(pszFile, O_CREAT | O_TRUNC | O_RDWR, 0660);
53 if (fd < 0)
54 {
55 fprintf(stderr, "error creating file: %s\n", pszFile);
56 return 1;
57 }
58
59 /* write 64 KB to the file: */
60 memset(abBuf, 0xf6, sizeof(abBuf));
61 for (cbFile = 0; cbFile < 0x10000; cbFile += sizeof(abBuf))
62 if (write(fd, abBuf, sizeof(abBuf)) != sizeof(abBuf))
63 {
64 fprintf(stderr, "error writing file: %s\n", pszFile);
65 return 1;
66 }
67 fsync(fd);
68
69 /* Map the file: */
70 pbMapping = (uint8_t *)mmap(NULL, cbFile, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
71 if (pbMapping == (void *)-1)
72 {
73 fprintf(stderr, "error mapping file: %s\n", pszFile);
74 return 1;
75 }
76
77 /* Modify the mapping and sync it: */
78 memset(pbMapping, 0xf7, cbFile);
79 if (msync(pbMapping, cbFile, MS_SYNC) != 0)
80 {
81 fprintf(stderr, "error msync'ing file: %s\n", pszFile);
82 return 1;
83 }
84
85 /* Unmap and close it: */
86 if (munmap(pbMapping, cbFile) != 0)
87 fprintf(stderr, "error munmap'ing file: %s\n", pszFile);
88 close(fd);
89
90 /*
91 * Open it again and check the content.
92 */
93 fd = open(pszFile, O_RDWR, 0);
94 if (fd < 0)
95 {
96 fprintf(stderr, "error reopening file: %s\n", pszFile);
97 return 1;
98 }
99
100 while (offFile < cbFile && cErrors < 42)
101 {
102 size_t offBuf;
103 ssize_t cbRead = read(fd, abBuf, sizeof(abBuf));
104 if (cbRead != (ssize_t)sizeof(abBuf))
105 {
106 fprintf(stderr, "error reading file: %zd, off %#zx (%s)\n", cbRead, offFile, pszFile);
107 return 1;
108 }
109
110 for (offBuf = 0; offBuf < sizeof(abBuf); offBuf++)
111 if (abBuf[offBuf] != 0xf7)
112 {
113 fprintf(stderr, "mismatch at %#zx: %#x, expected %#x\n", offFile + offBuf, abBuf[offBuf], 0xf7);
114 cErrors++;
115 if (cErrors > 42)
116 break;
117 }
118
119 offFile += sizeof(abBuf);
120 }
121
122 close(fd);
123
124 return cErrors == 0 ? 0 : 1;
125}
126
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use