unix.c

filter: Statementstmt 67.3% 70 / 104 Branchbran 32.1% 18 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.0% 88 / 160 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Cache/FastMmap.pm ↑ Summary FastMmap.xs »
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 8068 char* _mmc_get_def_share_filename(mmap_cache * cache)
31 {
32 8068 return def_share_file;
33 }
34  
35 8068 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 8068 res = stat(cache->share_file, &statbuf);
42  
43 /* Remove if different size or remove requested */
44 8068 ▶ if (!res &&
Branch: 1 branch
BranchTrueFalseTotal
if (!res && 0 8068 8068
45 0 ▶ (cache->init_file || (statbuf.st_size != cache->c_size))) {
Branch: 2 branches
BranchTrueFalseTotal
(cache->init_file || (statbuf.st_size != cache->c_size))) { 0 0 0
(cache->init_file || (statbuf.st_size != cache->c_size))) { 0 0 0
46 0 res = remove(cache->share_file);
47 0 ▶ if (res == -1 && errno != ENOENT) {
Branch: 2 branches
BranchTrueFalseTotal
if (res == -1 && errno != ENOENT) { 0 0 0
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 8068 res = stat(cache->share_file, &statbuf);
54 8068 ▶ if (res == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (res == -1) { 8068 0 8068
55 8068 mode_t permissions = (mode_t)cache->permissions;
56 8068 res = open(cache->share_file, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_APPEND, permissions);
57 8068 ▶ if (res == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (res == -1) { 0 8068 8068
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 8068 tmp = calloc(1, cache->c_page_size);
63 8068 ▶ if (!tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) { 0 8068 8068
64 0 return _mmc_set_error(cache, errno, "Calloc of tmp space failed");
65 }
66  
67 147150 ▶ for (i = 0; i < cache->c_num_pages; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cache->c_num_pages; i++) { 139082 8068 147150
68 139082 int written = write(res, tmp, cache->c_page_size);
69 139082 ▶ if (written < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (written < 0) { 0 139082 139082
70 0 free(tmp);
71 0 return _mmc_set_error(cache, errno, "Write to share file %s failed", cache->share_file);
72 }
73 139082 ▶ if (written < cache->c_page_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (written < cache->c_page_size) { 0 139082 139082
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 8068 free(tmp);
79  
80 /* Later on initialise page structures */
81 8068 *do_init = 1;
82  
83 8068 close(res);
84 }
85  
86 /* Open for reading/writing */
87 8068 fh = open(cache->share_file, O_RDWR);
88 8068 ▶ if (fh == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fh == -1) { 0 8068 8068
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 8068 fcntl(fh, F_SETFD, FD_CLOEXEC);
94  
95 8068 fstat(fh, &statbuf);
96 8068 cache->inode = statbuf.st_ino;
97  
98 8068 cache->fh = fh;
99  
100 8068 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 16136 int mmc_map_memory(mmap_cache* cache) {
110 /* Map file into memory */
111 16136 cache->mm_var = mmap(0, cache->c_size, PROT_READ | PROT_WRITE, MAP_SHARED, cache->fh, 0);
112 16136 ▶ if (cache->mm_var == (void *)MAP_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->mm_var == (void *)MAP_FAILED) { 0 16136 16136
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 16136 return 0;
119 }
120  
121 /*
122 * mmc_unmap_memory(mmap_cache * cache)
123 *
124 * Unmaps cache->mm_var
125 */
126 16136 int mmc_unmap_memory(mmap_cache* cache) {
127 16136 int res = munmap(cache->mm_var, cache->c_size);
128 16136 ▶ if (res == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (res == -1) { 0 16136 16136
129 0 return _mmc_set_error(cache, errno, "Munmap of shared file %s failed", cache->share_file);
130 }
131 16136 return res;
132 }
133  
134 9065 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 9065 ▶ if (fstat(cache->fh, &statbuf) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(cache->fh, &statbuf) == -1) { 0 9065 9065
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 9065 ▶ if (cache->inode != statbuf.st_ino) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->inode != statbuf.st_ino) { 0 9065 9065
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 9065 return 1;
150 }
151  
152 8068 int mmc_close_fh(mmap_cache* cache) {
153 8068 int res = close(cache->fh);
154 8068 return res;
155 }
156  
157 420175 int mmc_lock_page(mmap_cache* cache, MU64 p_offset) {
158 struct flock lock;
159 420175 int old_alarm, alarm_left = 10;
160 420175 int lock_res = -1;
161  
162 /* Setup fcntl locking structure */
163 420175 lock.l_type = F_WRLCK;
164 420175 lock.l_whence = SEEK_SET;
165 420175 lock.l_start = p_offset;
166 420175 lock.l_len = cache->c_page_size;
167  
168 420175 ▶ if (cache->catch_deadlocks)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->catch_deadlocks) 0 420175 420175
169 0 old_alarm = alarm(alarm_left);
170  
171 420175 ▶ while (lock_res != 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (lock_res != 0) { 420175 0 420175
172  
173 /* Lock the page (block till done, signal, or timeout) */
174 420175 lock_res = fcntl(cache->fh, F_SETLKW, &lock);
175  
176 /* Continue immediately if success */
177 420175 ▶ if (lock_res == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lock_res == 0) { 420175 0 420175
178 420175 ▶ if (cache->catch_deadlocks)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->catch_deadlocks) 0 420175 420175
179 0 alarm(old_alarm);
180 420175 break;
181 }
182  
183 /* Turn off alarm for a moment */
184 0 ▶ if (cache->catch_deadlocks)
Branch: 1 branch
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
if (cache->catch_deadlocks) 0 0 0
197 0 alarm(old_alarm);
198 0 return -1;
199 }
200  
201 420175 return 0;
202 }
203  
204 420175 int mmc_unlock_page(mmap_cache * cache, MU64 p_offset) {
205 struct flock lock;
206  
207 /* Setup fcntl locking structure */
208 420175 lock.l_type = F_UNLCK;
209 420175 lock.l_whence = SEEK_SET;
210 420175 lock.l_start = p_offset;
211 420175 lock.l_len = cache->c_page_size;
212  
213 /* And unlock page */
214 420175 fcntl(cache->fh, F_SETLKW, &lock);
215  
216 420175 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 1 int _mmc_set_error(mmap_cache *cache, int err, char * error_string, ...) {
227 va_list ap;
228 static char errbuf[1024];
229  
230 1 va_start(ap, error_string);
231  
232 /* Make sure it's terminated */
233 1 errbuf[1023] = '\0';
234  
235 /* Start with error string passed */
236 1 vsnprintf(errbuf, 1023, error_string, ap);
237  
238 /* Add system error code if passed. */
239 1 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 0 1 1
240 0 size_t used = strlen(errbuf);
241 0 ▶ if (used < sizeof(errbuf) - 1) {
Branch: 1 branch
BranchTrueFalseTotal
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 1 cache->last_error = errbuf;
248  
249 1 va_end(ap);
250  
251 1 return -1;
252 }
« blib/lib/Cache/FastMmap.pm ↑ Summary FastMmap.xs »