Whamcloud - gitweb
LU-13474 gss: do not return -ERESTART when gss rpc times out
[fs/lustre-release.git] / lustre / tests / mmap_cat.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <sys/mman.h>
38 #include <sys/types.h>
39 #include <fcntl.h>
40 #include <sys/stat.h>
41 #include <assert.h>
42
43 int save_errno;
44
45 char usage[] =
46 "Usage: %s <file>\n"
47 "       mmap <file> and cat its content\n";
48
49 size_t getFilesize(const char *filename)
50 {
51         struct stat st;
52
53         if (stat(filename, &st) == -1) {
54                 save_errno = errno;
55                 perror("stat");
56                 exit(save_errno);
57         }
58         return st.st_size;
59 }
60
61 int main(int argc, char **argv)
62 {
63         size_t filesize;
64         int fd;
65         void *mmappedData;
66         int rc;
67
68         if (argc != 2) {
69                 fprintf(stderr, usage, argv[0]);
70                 exit(1);
71         }
72
73         filesize = getFilesize(argv[1]);
74
75         /* Open file */
76         fd = open(argv[1], O_RDONLY, 0);
77         if (fd == -1) {
78                 save_errno = errno;
79                 perror("open");
80                 exit(save_errno);
81         }
82
83         /* Execute mmap */
84         mmappedData = mmap(NULL, filesize, PROT_READ,
85                            MAP_PRIVATE | MAP_POPULATE, fd, 0);
86         if (mmappedData == MAP_FAILED) {
87                 save_errno = errno;
88                 perror("mmap");
89                 exit(save_errno);
90         }
91
92         /* Write the mmapped data to stdout (= FD #1) */
93         rc = write(1, mmappedData, filesize);
94         if (rc == -1) {
95                 save_errno = errno;
96                 perror("write");
97                 exit(save_errno);
98         }
99
100         rc = munmap(mmappedData, filesize);
101         if (rc == -1) {
102                 save_errno = errno;
103                 perror("munmap");
104                 exit(save_errno);
105         }
106
107         rc = close(fd);
108         if (rc == -1) {
109                 save_errno = errno;
110                 perror("close");
111                 exit(save_errno);
112         }
113
114         return 0;
115 }
116