Project

General

Profile

Bug #8040 » test-8040.c

Marcel Telka, 2017-06-13 08:26 AM

 
1
#include <unistd.h>
2
#include <stdio.h>
3
#include <sys/types.h>
4
#include <sys/stat.h>
5
#include <fcntl.h>
6
#include <thread.h>
7

    
8
int f;
9

    
10
void *
11
trnc(void *a)
12
{
13
	sleep(1);
14

    
15
	printf("truncate\n");
16
	if (ftruncate(f, 100000000) != 0) {
17
		printf("ftruncate failed\n");
18
		return NULL;
19
	}
20
	printf("truncate done\n");
21

    
22
	return NULL;
23
}
24

    
25
int
26
main(int argc, char *argv[])
27
{
28
	if (argc != 2) {
29
		printf("Filename missing\n");
30
		return 1;
31
	}
32

    
33
	/* To make sure we are granted delegation we need to open twice. */
34
	f = open(argv[1], O_RDWR);
35
	if (f < 0) {
36
		printf("open failed\n");
37
		return 1;
38
	}
39
	if (close(f) != 0) {
40
		printf("close failed\n");
41
		return 1;
42
	}
43
	f = open(argv[1], O_RDWR);
44
	if (f < 0) {
45
		printf("open #2 failed\n");
46
		return 1;
47
	}
48

    
49
	/* Modify some file pages so we have something to flush. */
50
	char wbuf[10 * 1024];
51
	if (write(f, wbuf, sizeof wbuf) != sizeof wbuf) {
52
		printf("write failed\n");
53
		return 1;
54
	}
55

    
56
	/* Both read and truncate operations will hang, so we need a separate thread. */
57
	if (thr_create(NULL, 32 * 1024, trnc, NULL, THR_DETACHED, NULL) != 0) {
58
		printf("thr_create failed\n");
59
		return 1;
60
	}
61

    
62
	/* Read something to trigger the readahead operation(s). */
63
	char rbuf[33 * 1024];
64
	ssize_t r = read(f, rbuf, sizeof rbuf);
65
	printf("read: %d\n", r);
66

    
67
	return 0;
68
}
(2-2/2)