summaryrefslogtreecommitdiff
path: root/usr/src/test/smbclient-tests/cmd/no_close/no_close.c
blob: 8a780c36df0ddce4f98c1d41d814293d4f8e3914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2012 Jilin Xpd <jilinxpd@gmail.com>
 * Copyright 2018 Nexenta Systems, Inc.
 */

/*
 * It doesn't matter if the userland forgets to close the file or
 * munmap it, because smbfs will help close it(including otW close)
 * and sync the dirty pages to the file.
 * This program tests if smbfs works as we said.
 */

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int
main(int argc, char **argv)
{
	char *file_addr;
	off_t offset;
	size_t filesize;
	size_t blksize;
	int fid;
	int i;
	char *c = "?#*%&";

	if (argc != 2) {
		fprintf(stderr, "\tusage:\n\tno_close <filename>\n");
		return (1);
	}

	/* open test file */
	fid = open(argv[1], O_RDWR | O_CREAT | O_TRUNC,
	    S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
	if (fid == -1) {
		fprintf(stderr, "open %s error=%d\n", argv[1], errno);
		return (1);
	}

	/* extend file */
	filesize = 64 * 1024;
	if (ftruncate(fid, filesize) == -1) {
		fprintf(stderr, "ftrunc %s error=%d\n", argv[1], errno);
		return (1);
	}

	/* map file */
	file_addr = mmap(NULL, filesize,
	    PROT_READ | PROT_WRITE, MAP_SHARED, fid, 0);
	if (file_addr == MAP_FAILED) {
		fprintf(stderr, "mmap %s error=%d\n", argv[1], errno);
		return (1);
	}

	/* write something into mapped addr */
	blksize = filesize / 4;
	for (i = 0, offset = 0; i < 4; i++, offset += blksize) {
		memset(file_addr + offset, c[i], blksize);
	}
	memset(file_addr + offset, c[i], filesize - offset);

	/* no msync, munmap, close */

	_exit(0);
	/* NOTREACHED */
}