1 /* $Id: mlockfile.c 6014 2002-12-16 11:28:07Z alexk $ */
3 /* Locks the files given on the command line into memory using mlock.
4 This code has only been tested on Solaris and may not work on other
7 Contributed by Alex Kiernan <alexk@demon.net>. */
20 #include <sys/stropts.h>
36 inn_lock_files(struct mlock *ml)
38 for (; ml->path != NULL; ++ml) {
41 fd = open(ml->path, O_RDONLY);
43 fprintf(stderr, "%s: can't open `%s' - %s\n",
44 progname, ml->path, strerror(errno));
48 /* check if size, inode or device of the path have
49 * changed, if so unlock the previous file & lock the new
51 if (fstat(fd, &st) != 0) {
52 fprintf(stderr, "%s: can't stat `%s' - %s\n",
53 progname, ml->path, strerror(errno));
54 } else if (ml->st.st_ino != st.st_ino ||
55 ml->st.st_dev != st.st_dev ||
56 ml->st.st_size != st.st_size) {
57 if (ml->base != MAP_FAILED)
59 ml->length ? ml->length : ml->st.st_size);
61 /* free everything here, so in case of failure we try
68 ml->length ? ml->length : st.st_size,
70 MAP_SHARED, fd, ml->offset);
72 if (ml->base == MAP_FAILED) {
73 fprintf(stderr, "%s: can't mmap `%s' - %s\n",
74 progname, ml->path, strerror(errno));
77 ml->length ? ml->length : st.st_size) != 0) {
78 fprintf(stderr, "%s: can't mlock `%s' - %s\n",
79 progname, ml->path, strerror(errno));
85 msync(ml->base, ml->length ? ml->length : st.st_size, MS_SYNC);
96 "usage: %s [-f] [-i interval] file[@offset[:length]] ...\n",
98 fprintf(stderr, " -f\tflush locked bitmaps at interval\n");
99 fprintf(stderr, " -i interval\n\tset interval between checks/flushes\n");
103 main(int argc, char *argv[])
109 while ((i = getopt(argc, argv, "fi:")) != EOF) {
112 interval = 1000 * atoi(optarg);
127 /* construct list of pathnames which we're to operate on, zero out
128 * the "cookies" so we lock it in core first time through */
129 ml = malloc((1 + argc) * sizeof ml);
130 for (i = 0; argc--; ++i, ++argv) {
138 ml[i].st.st_size = 0;
139 ml[i].base = MAP_FAILED;
141 /* if we have a filename of the form ...@offset:length, only
142 * map in that portion of the file */
143 at = strchr(*argv, '@');
149 offset = strtoull(at, &end, 0);
151 fprintf(stderr, "%s: can't parse offset `%s' - %s\n",
152 progname, at, strerror(errno));
158 length = strtoul(at, &end, 0);
160 fprintf(stderr, "%s: can't parse length `%s' - %s\n",
161 progname, at, strerror(errno));
166 fprintf(stderr, "%s: unrecognised separator `%c'\n",
171 ml[i].offset = offset;
172 ml[i].length = length;
176 /* loop over the list of paths, sleeping 60s between iterations */
179 poll(NULL, 0, interval);