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