aboutsummaryrefslogtreecommitdiff
path: root/fmlock.c
blob: fa4f3c991b7aaf57325efe114b4a5d6bdb459f24 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stddef.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

int heat_the_cache(int fd)
{
	char buf[1024];
	int rv;
	do {
	retry_read:
		rv = read(fd, buf, sizeof(buf));
		if( -1 == rv
		 && EINTR == errno ) {
			goto retry_read;
		}
	} while( 0 < rv );

	return rv != 0;
}

int main(int argc, char *argv[])
{
	int i, fd_null, n_fileslocked;
	fd_set phony_fdset;
	char *buf;

	if( 2 > argc ) {
		fprintf(stderr,
			"Usage:\n%s [filenames]\n",
			argv[0] );
		return 1;
	}

	n_fileslocked = 0;
	for(i = 1; i < argc; ++i) {
		char const * const filename = argv[i];
		int fd;
		struct stat st;
		void *ptr;

	retry_open:
		fd = open(filename, O_RDONLY);
		if( -1 == fd ) {
			if( EINTR == errno ) {
				goto retry_open;
			} else
			{
				fprintf(stderr,
					"error open('%s'): %s\n",
					filename,
					strerror(errno) );
				continue;
			}
		}

		if( -1 == fstat(fd, &st) ) {
			fprintf(stderr,
				"error fstat(fd['%s']): %s\n",
				filename,
				strerror(errno) );
			goto finish_file;
		}

		ptr = mmap(
			NULL,
			st.st_size,
			PROT_READ,
			MAP_SHARED | MAP_LOCKED,
			fd,
			0 );
		if( MAP_FAILED == ptr ) {
			fprintf(stderr,
				"error mmap(fd['%s'], 0..%lld): %s\n",
				filename,
				(long long)st.st_size,
				strerror(errno) );
			goto finish_file;
		}

		if( -1 == mlock(ptr, st.st_size) ) {
			fprintf(stderr,
				"error mlock(ptr[fd['%s']]=%p): %s\n",
				filename,
				ptr,
				strerror(errno) );
			goto finish_file;
		}
		++n_fileslocked;

		heat_the_cache(fd);

	finish_file:
		close(fd);
	}

	if( !n_fileslocked ) {
		return 1;
	}

	fprintf(stderr, "Files locked and cache heated up. Going to sleep, .zZ...\n");

	/* At this point the program shall sleep until a terminating
	 * signal arrives. To do so a nice side effect of the definition
	 * of /dev/null behavior is used: read on a /dev/null fd always
	 * return 0, which correspond to EOF which is a "no content
	 * available for read (yet)" situation for which select waits.
	 * So by selecting for a read a fd on /dev/null we can put the
	 * process to sleep. */
	fd_null = open("/dev/null", O_RDONLY);
	if( -1 == fd_null ) {
		fprintf(stderr,
			"error open('/dev/null'): %s\n",
			strerror(errno) );
		return 1;
	}
	FD_ZERO(&phony_fdset);
	FD_SET(fd_null, &phony_fdset);
	if( -1 == select(fd_null, &phony_fdset, NULL, NULL, NULL) ) {
		fprintf(stderr,
			"error select(...): %s\n",
			strerror(errno) );
		return 1;
	}

	return 0;
}