generic: add fstests for idmapped mounts
[xfstests-dev.git] / src / t_mmap_writev.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2010
4  *      by D.Buczek - Max Planck Institute for Molecular Genetics Berlin
5  *
6  * mmap() a file and writev() back to another file
7  *      - kernel bug #22452 testcase
8  */
9
10 #include <sys/types.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include <sys/mman.h>
16 #include <string.h>
17 #include <sys/uio.h>
18 #include <unistd.h>
19
20 int main(int argc, char **argv)
21 {
22         char *file = argv[1];
23         char *new_file = argv[2];
24         int fd;
25         int fd_new;
26         void *base;
27
28         struct iovec iovec[3]=
29         {
30                 { "aaaaaaaaaa" , 10 },
31                 { "bbbbbbbbbb" , 10 },
32                 { NULL , 10 }
33         };
34
35         int i;
36
37         fd=open(file, O_RDONLY);
38         if (fd==-1) {perror("open");exit(1);}
39
40         base = mmap(NULL,16384,PROT_READ,MAP_SHARED,fd,0);
41         if  (base == MAP_FAILED) { perror("mmap");exit(1); }
42
43         unlink(new_file);
44
45         fd_new=open(new_file,O_RDWR|O_CREAT,0666);
46         if (fd_new==-1) {perror("creat");exit(1);}
47
48         iovec[2].iov_base=(char *)base;
49         i=writev(fd_new,iovec,sizeof(iovec)/sizeof(*iovec));
50         if (i==-1) {perror("writev");exit(1);}
51
52         close(fd_new);
53         munmap(base,16384);
54         close(fd);
55
56         return 0;
57 }