File: mlockfile.c

package info (click to toggle)
inn2 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,720 kB
  • ctags: 8,983
  • sloc: ansic: 92,499; sh: 13,509; perl: 12,921; makefile: 2,985; yacc: 842; python: 342; lex: 255
file content (182 lines) | stat: -rw-r--r-- 4,180 bytes parent folder | download | duplicates (4)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* $Id: mlockfile.c 6014 2002-12-16 11:28:07Z alexk $ */

/* Locks the files given on the command line into memory using mlock.
   This code has only been tested on Solaris and may not work on other
   platforms.

   Contributed by Alex Kiernan <alexk@demon.net>.  */

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

struct mlock {
    const char *path;
    struct stat st;
    void *base;
    off_t offset;
    size_t length;
};

char *progname;

int flush = 0;
int interval = 60000;

void
inn_lock_files(struct mlock *ml)
{
    for (; ml->path != NULL; ++ml) {
	int fd;

	fd = open(ml->path, O_RDONLY);
	if (fd == -1) {
	    fprintf(stderr, "%s: can't open `%s' - %s\n",
		    progname, ml->path, strerror(errno));
	} else {
	    struct stat st;

	    /* check if size, inode or device of the path have
	     * changed, if so unlock the previous file & lock the new
	     * one */
	    if (fstat(fd, &st) != 0) {
		fprintf(stderr, "%s: can't stat `%s' - %s\n",
			progname, ml->path, strerror(errno));
	    } else if (ml->st.st_ino != st.st_ino ||
		     ml->st.st_dev != st.st_dev ||
		     ml->st.st_size != st.st_size) {
		if (ml->base != MAP_FAILED)
		    munmap(ml->base,
			   ml->length ? ml->length : ml->st.st_size);

		/* free everything here, so in case of failure we try
		 * again next time */
		ml->st.st_ino = 0;
		ml->st.st_dev = 0;
		ml->st.st_size = 0;

		ml->base = mmap(NULL,
				ml->length ? ml->length : st.st_size,
				PROT_READ,
				MAP_SHARED, fd, ml->offset);

		if (ml->base == MAP_FAILED) {
		    fprintf(stderr, "%s: can't mmap `%s' - %s\n",
			    progname, ml->path, strerror(errno));
		} else {
		    if (mlock(ml->base,
			      ml->length ? ml->length : st.st_size) != 0) {
			fprintf(stderr, "%s: can't mlock `%s' - %s\n",
				progname, ml->path, strerror(errno));
		    } else {
			ml->st = st;
		    }
		}
	    } else if (flush) {
		msync(ml->base, ml->length ? ml->length : st.st_size, MS_SYNC);
	    }
	}
	close (fd);
    }
}

static void
usage(void)
{
    fprintf(stderr,
	    "usage: %s [-f] [-i interval] file[@offset[:length]] ...\n",
	    progname);
    fprintf(stderr, "    -f\tflush locked bitmaps at interval\n");
    fprintf(stderr, "    -i interval\n\tset interval between checks/flushes\n");
}

int
main(int argc, char *argv[])
{
    struct mlock *ml;
    int i;

    progname = *argv;
    while ((i = getopt(argc, argv, "fi:")) != EOF) {
	switch (i) {
	case 'i':
	    interval = 1000 * atoi(optarg);
	    break;

	case 'f':
	    flush = 1;
	    break;

	default:
	    usage();
	    return EX_USAGE;	    
	}
    }
    argc -= optind;
    argv += optind;

    /* construct list of pathnames which we're to operate on, zero out
     * the "cookies" so we lock it in core first time through */
    ml = malloc((1 + argc) * sizeof ml);
    for (i = 0; argc--; ++i, ++argv) {
	char *at;
	off_t offset = 0;
	size_t length = 0;

	ml[i].path = *argv;
	ml[i].st.st_ino = 0;
	ml[i].st.st_dev = 0;
	ml[i].st.st_size = 0;
	ml[i].base = MAP_FAILED;
	
	/* if we have a filename of the form ...@offset:length, only
	 * map in that portion of the file */
	at = strchr(*argv, '@');
	if (at != NULL) {
	    char *end;

	    *at++ = '\0';
	    errno = 0;
	    offset = strtoull(at, &end, 0);
	    if (errno != 0) {
		fprintf(stderr, "%s: can't parse offset `%s' - %s\n",
			progname, at, strerror(errno));
		return EX_USAGE;
	    }
	    if (*end == ':') {
		at = end + 1;
		errno = 0;
		length = strtoul(at, &end, 0);
		if (errno != 0) {
		    fprintf(stderr, "%s: can't parse length `%s' - %s\n",
			    progname, at, strerror(errno));
		    return EX_USAGE;
		}
	    }
	    if (*end != '\0') {
		fprintf(stderr, "%s: unrecognised separator `%c'\n",
			progname, *end);
		return EX_USAGE;
	    }
	}
	ml[i].offset = offset;
	ml[i].length = length;	    
    }
    ml[i].path = NULL;

    /* loop over the list of paths, sleeping 60s between iterations */
    for (;;) {
	inn_lock_files(ml);
	poll(NULL, 0, interval);
    }
    return EX_OSERR;
}