1*96c8483aSYuri Pankov /*
2*96c8483aSYuri Pankov  * This file and its contents are supplied under the terms of the
3*96c8483aSYuri Pankov  * Common Development and Distribution License ("CDDL"), version 1.0.
4*96c8483aSYuri Pankov  * You may only use this file in accordance with the terms of version
5*96c8483aSYuri Pankov  * 1.0 of the CDDL.
6*96c8483aSYuri Pankov  *
7*96c8483aSYuri Pankov  * A full copy of the text of the CDDL should have accompanied this
8*96c8483aSYuri Pankov  * source.  A copy of the CDDL is also available via the Internet at
9*96c8483aSYuri Pankov  * http://www.illumos.org/license/CDDL.
10*96c8483aSYuri Pankov  */
11*96c8483aSYuri Pankov 
12*96c8483aSYuri Pankov /*
13*96c8483aSYuri Pankov  * Copyright 2012 Jilin Xpd <jilinxpd@gmail.com>
14*96c8483aSYuri Pankov  * Copyright 2018 Nexenta Systems, Inc.
15*96c8483aSYuri Pankov  */
16*96c8483aSYuri Pankov 
17*96c8483aSYuri Pankov /*
18*96c8483aSYuri Pankov  * It doesn't matter if the userland forgets to close the file or
19*96c8483aSYuri Pankov  * munmap it, because smbfs will help close it(including otW close)
20*96c8483aSYuri Pankov  * and sync the dirty pages to the file.
21*96c8483aSYuri Pankov  * This program tests if smbfs works as we said.
22*96c8483aSYuri Pankov  */
23*96c8483aSYuri Pankov 
24*96c8483aSYuri Pankov #include <sys/mman.h>
25*96c8483aSYuri Pankov #include <sys/types.h>
26*96c8483aSYuri Pankov #include <sys/stat.h>
27*96c8483aSYuri Pankov #include <fcntl.h>
28*96c8483aSYuri Pankov #include <stdio.h>
29*96c8483aSYuri Pankov #include <stdlib.h>
30*96c8483aSYuri Pankov #include <unistd.h>
31*96c8483aSYuri Pankov #include <string.h>
32*96c8483aSYuri Pankov #include <errno.h>
33*96c8483aSYuri Pankov 
34*96c8483aSYuri Pankov int
main(int argc,char ** argv)35*96c8483aSYuri Pankov main(int argc, char **argv)
36*96c8483aSYuri Pankov {
37*96c8483aSYuri Pankov 	char *file_addr;
38*96c8483aSYuri Pankov 	off_t offset;
39*96c8483aSYuri Pankov 	size_t filesize;
40*96c8483aSYuri Pankov 	size_t blksize;
41*96c8483aSYuri Pankov 	int fid;
42*96c8483aSYuri Pankov 	int i;
43*96c8483aSYuri Pankov 	char *c = "?#*%&";
44*96c8483aSYuri Pankov 
45*96c8483aSYuri Pankov 	if (argc != 2) {
46*96c8483aSYuri Pankov 		fprintf(stderr, "\tusage:\n\tno_close <filename>\n");
47*96c8483aSYuri Pankov 		return (1);
48*96c8483aSYuri Pankov 	}
49*96c8483aSYuri Pankov 
50*96c8483aSYuri Pankov 	/* open test file */
51*96c8483aSYuri Pankov 	fid = open(argv[1], O_RDWR | O_CREAT | O_TRUNC,
52*96c8483aSYuri Pankov 	    S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
53*96c8483aSYuri Pankov 	if (fid == -1) {
54*96c8483aSYuri Pankov 		fprintf(stderr, "open %s error=%d\n", argv[1], errno);
55*96c8483aSYuri Pankov 		return (1);
56*96c8483aSYuri Pankov 	}
57*96c8483aSYuri Pankov 
58*96c8483aSYuri Pankov 	/* extend file */
59*96c8483aSYuri Pankov 	filesize = 64 * 1024;
60*96c8483aSYuri Pankov 	if (ftruncate(fid, filesize) == -1) {
61*96c8483aSYuri Pankov 		fprintf(stderr, "ftrunc %s error=%d\n", argv[1], errno);
62*96c8483aSYuri Pankov 		return (1);
63*96c8483aSYuri Pankov 	}
64*96c8483aSYuri Pankov 
65*96c8483aSYuri Pankov 	/* map file */
66*96c8483aSYuri Pankov 	file_addr = mmap(NULL, filesize,
67*96c8483aSYuri Pankov 	    PROT_READ | PROT_WRITE, MAP_SHARED, fid, 0);
68*96c8483aSYuri Pankov 	if (file_addr == MAP_FAILED) {
69*96c8483aSYuri Pankov 		fprintf(stderr, "mmap %s error=%d\n", argv[1], errno);
70*96c8483aSYuri Pankov 		return (1);
71*96c8483aSYuri Pankov 	}
72*96c8483aSYuri Pankov 
73*96c8483aSYuri Pankov 	/* write something into mapped addr */
74*96c8483aSYuri Pankov 	blksize = filesize / 4;
75*96c8483aSYuri Pankov 	for (i = 0, offset = 0; i < 4; i++, offset += blksize) {
76*96c8483aSYuri Pankov 		memset(file_addr + offset, c[i], blksize);
77*96c8483aSYuri Pankov 	}
78*96c8483aSYuri Pankov 	memset(file_addr + offset, c[i], filesize - offset);
79*96c8483aSYuri Pankov 
80*96c8483aSYuri Pankov 	/* no msync, munmap, close */
81*96c8483aSYuri Pankov 
82*96c8483aSYuri Pankov 	_exit(0);
83*96c8483aSYuri Pankov 	/* NOTREACHED */
84*96c8483aSYuri Pankov }