| 1 |
|
| |
| 2 |
|
| /* |
| 3 |
|
| * AUTHOR |
| 4 |
|
| * |
| 5 |
|
| * Rob Mueller <cpan@robm.fastmail.fm> |
| 6 |
|
| * |
| 7 |
|
| * COPYRIGHT AND LICENSE |
| 8 |
|
| * |
| 9 |
|
| * Copyright (C) 2003 by FastMail IP Partners |
| 10 |
|
| * |
| 11 |
|
| * This library is free software; you can redistribute it and/or modify |
| 12 |
|
| * it under the same terms as Perl itself. |
| 13 |
|
| * |
| 14 |
|
| */ |
| 15 |
|
| |
| 16 |
|
| #include <stdlib.h> |
| 17 |
|
| #include <stdio.h> |
| 18 |
|
| #include <string.h> |
| 19 |
|
| #include <unistd.h> |
| 20 |
|
| #include <fcntl.h> |
| 21 |
|
| #include <sys/mman.h> |
| 22 |
|
| #include <sys/types.h> |
| 23 |
|
| #include <sys/stat.h> |
| 24 |
|
| #include <time.h> |
| 25 |
|
| #include <errno.h> |
| 26 |
|
| #include <stdarg.h> |
| 27 |
|
| #include "mmap_cache.h" |
| 28 |
|
| #include "mmap_cache_internals.h" |
| 29 |
|
| |
| 30 |
8071 |
| char* _mmc_get_def_share_filename(mmap_cache * cache) |
| 31 |
|
| { |
| 32 |
8071 |
| return def_share_file; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
8070 |
| int mmc_open_cache_file(mmap_cache* cache, int * do_init) { |
| 36 |
|
| int res, i, fh; |
| 37 |
|
| void * tmp; |
| 38 |
|
| struct stat statbuf; |
| 39 |
|
| |
| 40 |
|
| /* Check if file exists */ |
| 41 |
8070 |
| res = stat(cache->share_file, &statbuf); |
| 42 |
|
| |
| 43 |
|
| /* Remove if different size or remove requested */ |
| 44 |
8070 |
▶ | if (!res && |
Branch: 1 branch
| Branch | True | False | Total |
| if (!res && |
2 |
8068 |
8070 |
|
| 45 |
2 |
▶ | (cache->init_file || (statbuf.st_size != cache->c_size))) { |
Branch: 2 branches
| Branch | True | False | Total |
| (cache->init_file || (statbuf.st_size != cache->c_size))) { |
1 |
1 |
2 |
| (cache->init_file || (statbuf.st_size != cache->c_size))) { |
0 |
1 |
1 |
|
| 46 |
1 |
| res = remove(cache->share_file); |
| 47 |
1 |
▶ | if (res == -1 && errno != ENOENT) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (res == -1 && errno != ENOENT) { |
0 |
1 |
1 |
| if (res == -1 && errno != ENOENT) { |
0 |
0 |
0 |
|
| 48 |
0 |
| return _mmc_set_error(cache, errno, "Unlink of existing share file %s failed", cache->share_file); |
| 49 |
|
| } |
| 50 |
|
| } |
| 51 |
|
| |
| 52 |
|
| /* Create file if it doesn't exist */ |
| 53 |
8070 |
| res = stat(cache->share_file, &statbuf); |
| 54 |
8070 |
▶ | if (res == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res == -1) { |
8069 |
1 |
8070 |
|
| 55 |
8069 |
| mode_t permissions = (mode_t)cache->permissions; |
| 56 |
8069 |
| res = open(cache->share_file, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_APPEND, permissions); |
| 57 |
8069 |
▶ | if (res == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res == -1) { |
0 |
8069 |
8069 |
|
| 58 |
0 |
| return _mmc_set_error(cache, errno, "Create of share file %s failed", cache->share_file); |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
|
| /* Fill file with 0's */ |
| 62 |
8069 |
| tmp = calloc(1, cache->c_page_size); |
| 63 |
8069 |
▶ | if (!tmp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tmp) { |
0 |
8069 |
8069 |
|
| 64 |
0 |
| return _mmc_set_error(cache, errno, "Calloc of tmp space failed"); |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
147158 |
▶ | for (i = 0; i < cache->c_num_pages; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < cache->c_num_pages; i++) { |
139089 |
8069 |
147158 |
|
| 68 |
139089 |
| int written = write(res, tmp, cache->c_page_size); |
| 69 |
139089 |
▶ | if (written < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (written < 0) { |
0 |
139089 |
139089 |
|
| 70 |
0 |
| free(tmp); |
| 71 |
0 |
| return _mmc_set_error(cache, errno, "Write to share file %s failed", cache->share_file); |
| 72 |
|
| } |
| 73 |
139089 |
▶ | if (written < cache->c_page_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (written < cache->c_page_size) { |
0 |
139089 |
139089 |
|
| 74 |
0 |
| free(tmp); |
| 75 |
0 |
| return _mmc_set_error(cache, 0, "Write to share file %s failed; short write (%d of %d bytes written)", cache->share_file, written, cache->c_page_size); |
| 76 |
|
| } |
| 77 |
|
| } |
| 78 |
8069 |
| free(tmp); |
| 79 |
|
| |
| 80 |
|
| /* Later on initialise page structures */ |
| 81 |
8069 |
| *do_init = 1; |
| 82 |
|
| |
| 83 |
8069 |
| close(res); |
| 84 |
|
| } |
| 85 |
|
| |
| 86 |
|
| /* Open for reading/writing */ |
| 87 |
8070 |
| fh = open(cache->share_file, O_RDWR); |
| 88 |
8070 |
▶ | if (fh == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fh == -1) { |
0 |
8070 |
8070 |
|
| 89 |
0 |
| return _mmc_set_error(cache, errno, "Open of share file %s failed", cache->share_file); |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
|
| /* Automatically close cache fd on exec */ |
| 93 |
8070 |
| fcntl(fh, F_SETFD, FD_CLOEXEC); |
| 94 |
|
| |
| 95 |
8070 |
| fstat(fh, &statbuf); |
| 96 |
8070 |
| cache->inode = statbuf.st_ino; |
| 97 |
|
| |
| 98 |
8070 |
| cache->fh = fh; |
| 99 |
|
| |
| 100 |
8070 |
| return 0; |
| 101 |
|
| |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
|
| /* |
| 105 |
|
| * mmc_map_memory(mmap_cache * cache) |
| 106 |
|
| * |
| 107 |
|
| * maps the cache file into memory, and sets cache->mm_var as needed. |
| 108 |
|
| */ |
| 109 |
16139 |
| int mmc_map_memory(mmap_cache* cache) { |
| 110 |
|
| /* Map file into memory */ |
| 111 |
16139 |
| cache->mm_var = mmap(0, cache->c_size, PROT_READ | PROT_WRITE, MAP_SHARED, cache->fh, 0); |
| 112 |
16139 |
▶ | if (cache->mm_var == (void *)MAP_FAILED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cache->mm_var == (void *)MAP_FAILED) { |
0 |
16139 |
16139 |
|
| 113 |
0 |
| _mmc_set_error(cache, errno, "Mmap of shared file %s failed", cache->share_file); |
| 114 |
0 |
| mmc_close_fh(cache); |
| 115 |
0 |
| return -1; |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
16139 |
| return 0; |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
|
| /* |
| 122 |
|
| * mmc_unmap_memory(mmap_cache * cache) |
| 123 |
|
| * |
| 124 |
|
| * Unmaps cache->mm_var |
| 125 |
|
| */ |
| 126 |
16139 |
| int mmc_unmap_memory(mmap_cache* cache) { |
| 127 |
16139 |
| int res = munmap(cache->mm_var, cache->c_size); |
| 128 |
16139 |
▶ | if (res == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res == -1) { |
0 |
16139 |
16139 |
|
| 129 |
0 |
| return _mmc_set_error(cache, errno, "Munmap of shared file %s failed", cache->share_file); |
| 130 |
|
| } |
| 131 |
16139 |
| return res; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
9050 |
| int mmc_check_fh(mmap_cache* cache) { |
| 135 |
|
| struct stat statbuf; |
| 136 |
|
| |
| 137 |
|
| /* fstat must succeed before we can trust statbuf.st_ino. If it fails |
| 138 |
|
| * (e.g. fd was closed externally) treat that as the same kind of "fd |
| 139 |
|
| * messed with" condition the inode check is here to catch. */ |
| 140 |
9050 |
▶ | if (fstat(cache->fh, &statbuf) == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fstat(cache->fh, &statbuf) == -1) { |
0 |
9050 |
9050 |
|
| 141 |
0 |
| _mmc_set_error(cache, errno, "Underlying cache file fd %d fstat failed, something messed up underlying file descriptors", cache->fh); |
| 142 |
0 |
| return 0; |
| 143 |
|
| } |
| 144 |
9050 |
▶ | if (cache->inode != statbuf.st_ino) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cache->inode != statbuf.st_ino) { |
0 |
9050 |
9050 |
|
| 145 |
0 |
| _mmc_set_error(cache, 0, "Underlying cache file fd %d was inode %ld but now %ld, something messed up underlying file descriptors", cache->fh, cache->inode, statbuf.st_ino); |
| 146 |
0 |
| return 0; |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
9050 |
| return 1; |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
8070 |
| int mmc_close_fh(mmap_cache* cache) { |
| 153 |
8070 |
| int res = close(cache->fh); |
| 154 |
8070 |
| return res; |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
420408 |
| int mmc_lock_page(mmap_cache* cache, MU64 p_offset) { |
| 158 |
|
| struct flock lock; |
| 159 |
420408 |
| int old_alarm, alarm_left = 10; |
| 160 |
420408 |
| int lock_res = -1; |
| 161 |
|
| |
| 162 |
|
| /* Setup fcntl locking structure */ |
| 163 |
420408 |
| lock.l_type = F_WRLCK; |
| 164 |
420408 |
| lock.l_whence = SEEK_SET; |
| 165 |
420408 |
| lock.l_start = p_offset; |
| 166 |
420408 |
| lock.l_len = cache->c_page_size; |
| 167 |
|
| |
| 168 |
420408 |
▶ | if (cache->catch_deadlocks) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cache->catch_deadlocks) |
0 |
420408 |
420408 |
|
| 169 |
0 |
| old_alarm = alarm(alarm_left); |
| 170 |
|
| |
| 171 |
420408 |
▶ | while (lock_res != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lock_res != 0) { |
420408 |
0 |
420408 |
|
| 172 |
|
| |
| 173 |
|
| /* Lock the page (block till done, signal, or timeout) */ |
| 174 |
420408 |
| lock_res = fcntl(cache->fh, F_SETLKW, &lock); |
| 175 |
|
| |
| 176 |
|
| /* Continue immediately if success */ |
| 177 |
420408 |
▶ | if (lock_res == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lock_res == 0) { |
420408 |
0 |
420408 |
|
| 178 |
420408 |
▶ | if (cache->catch_deadlocks) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cache->catch_deadlocks) |
0 |
420408 |
420408 |
|
| 179 |
0 |
| alarm(old_alarm); |
| 180 |
420408 |
| break; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
|
| /* Turn off alarm for a moment */ |
| 184 |
0 |
▶ | if (cache->catch_deadlocks) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cache->catch_deadlocks) |
0 |
0 |
0 |
|
| 185 |
0 |
| alarm_left = alarm(0); |
| 186 |
|
| |
| 187 |
|
| /* Some signal interrupted, and it wasn't the alarm? Rerun lock */ |
| 188 |
0 |
▶ | if (lock_res == -1 && errno == EINTR && alarm_left) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (lock_res == -1 && errno == EINTR && alarm_left) { |
0 |
0 |
0 |
| if (lock_res == -1 && errno == EINTR && alarm_left) { |
0 |
0 |
0 |
| if (lock_res == -1 && errno == EINTR && alarm_left) { |
0 |
0 |
0 |
|
| 189 |
0 |
▶ | if (cache->catch_deadlocks) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cache->catch_deadlocks) |
0 |
0 |
0 |
|
| 190 |
0 |
| alarm(alarm_left); |
| 191 |
0 |
| continue; |
| 192 |
|
| } |
| 193 |
|
| |
| 194 |
|
| /* Lock failed? */ |
| 195 |
0 |
| _mmc_set_error(cache, errno, "Lock failed"); |
| 196 |
0 |
▶ | if (cache->catch_deadlocks) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cache->catch_deadlocks) |
0 |
0 |
0 |
|
| 197 |
0 |
| alarm(old_alarm); |
| 198 |
0 |
| return -1; |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
420408 |
| return 0; |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
420408 |
| int mmc_unlock_page(mmap_cache * cache, MU64 p_offset) { |
| 205 |
|
| struct flock lock; |
| 206 |
|
| |
| 207 |
|
| /* Setup fcntl locking structure */ |
| 208 |
420408 |
| lock.l_type = F_UNLCK; |
| 209 |
420408 |
| lock.l_whence = SEEK_SET; |
| 210 |
420408 |
| lock.l_start = p_offset; |
| 211 |
420408 |
| lock.l_len = cache->c_page_size; |
| 212 |
|
| |
| 213 |
|
| /* And unlock page */ |
| 214 |
420408 |
| fcntl(cache->fh, F_SETLKW, &lock); |
| 215 |
|
| |
| 216 |
420408 |
| return 0; |
| 217 |
|
| } |
| 218 |
|
| |
| 219 |
|
| |
| 220 |
|
| /* |
| 221 |
|
| * int _mmc_set_error(mmap_cache *cache, int err, char * error_string, ...) |
| 222 |
|
| * |
| 223 |
|
| * Set internal error string/state |
| 224 |
|
| * |
| 225 |
|
| */ |
| 226 |
9 |
| int _mmc_set_error(mmap_cache *cache, int err, char * error_string, ...) { |
| 227 |
|
| va_list ap; |
| 228 |
|
| static char errbuf[1024]; |
| 229 |
|
| |
| 230 |
9 |
| va_start(ap, error_string); |
| 231 |
|
| |
| 232 |
|
| /* Make sure it's terminated */ |
| 233 |
9 |
| errbuf[1023] = '\0'; |
| 234 |
|
| |
| 235 |
|
| /* Start with error string passed */ |
| 236 |
9 |
| vsnprintf(errbuf, 1023, error_string, ap); |
| 237 |
|
| |
| 238 |
|
| /* Add system error code if passed. */ |
| 239 |
9 |
▶ | if (err) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) { |
0 |
9 |
9 |
|
| 240 |
0 |
| size_t used = strlen(errbuf); |
| 241 |
0 |
▶ | if (used < sizeof(errbuf) - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (used < sizeof(errbuf) - 1) { |
0 |
0 |
0 |
|
| 242 |
0 |
| snprintf(errbuf + used, sizeof(errbuf) - used, ": %s", strerror(err)); |
| 243 |
|
| } |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
|
| /* Save in cache object */ |
| 247 |
9 |
| cache->last_error = errbuf; |
| 248 |
|
| |
| 249 |
9 |
| va_end(ap); |
| 250 |
|
| |
| 251 |
9 |
| return -1; |
| 252 |
|
| } |