pitchfork 0.18.2 → 0.18.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,10 @@
2
2
 
3
3
  #include <ruby.h>
4
4
  #include <unistd.h>
5
+ #include <stdlib.h>
5
6
  #include <sys/mman.h>
7
+ #include <sys/stat.h>
8
+ #include <fcntl.h>
6
9
  #include <errno.h>
7
10
  #include <stddef.h>
8
11
  #include <string.h>
@@ -65,19 +68,43 @@ struct slot {
65
68
  struct memory_page {
66
69
  size_t size;
67
70
  size_t capa;
71
+ int fd;
68
72
  struct slot *slots;
69
73
  };
70
74
 
71
- static void memory_page_free(void *ptr)
75
+ static int memory_page_close_fd(struct memory_page *page)
72
76
  {
73
- struct memory_page *page = (struct memory_page *)ptr;
77
+ int ret = 0;
78
+ if (page->fd != -1) {
79
+ ret = close(page->fd);
80
+ page->fd = -1;
81
+ }
82
+ return ret;
83
+ }
74
84
 
85
+ static int memory_page_unmap(struct memory_page *page)
86
+ {
87
+ int ret = 0;
75
88
  if (page->slots != MAP_FAILED) {
76
- int rv = munmap(page->slots, slot_size * page->capa);
77
- if (rv != 0) {
78
- rb_bug("Pitchfork::MemoryPage munmap failed in gc: %s", strerror(errno));
89
+ ret = munmap(page->slots, slot_size * page->capa);
90
+ if (ret == 0) {
91
+ page->slots = MAP_FAILED;
79
92
  }
80
93
  }
94
+ return ret;
95
+ }
96
+
97
+ static void memory_page_free(void *ptr)
98
+ {
99
+ struct memory_page *page = (struct memory_page *)ptr;
100
+
101
+ if (memory_page_unmap(page)) {
102
+ rb_bug("Pitchfork::MemoryPage munmap failed in gc: %s", strerror(errno));
103
+ }
104
+
105
+ if (memory_page_close_fd(page)) {
106
+ rb_warn("Pitchfork::MemoryPage close failed in GC, was it closed twice?");
107
+ }
81
108
 
82
109
  xfree(ptr);
83
110
  }
@@ -108,22 +135,63 @@ static VALUE memory_page_alloc(VALUE klass)
108
135
  VALUE obj = TypedData_Make_Struct(klass, struct memory_page, &memory_page_type, page);
109
136
 
110
137
  page->slots = MAP_FAILED;
138
+ page->fd = -1;
111
139
  return obj;
112
140
  }
113
141
 
114
- static struct memory_page *memory_page_get(VALUE self)
142
+ static struct memory_page *memory_page_get_raw(VALUE self)
115
143
  {
116
144
  struct memory_page *page;
117
-
118
145
  TypedData_Get_Struct(self, struct memory_page, &memory_page_type, page);
146
+ return page;
147
+ }
119
148
 
149
+ static struct memory_page *memory_page_get(VALUE self)
150
+ {
151
+ struct memory_page *page = memory_page_get_raw(self);
120
152
  if (page->slots == MAP_FAILED) {
121
153
  rb_raise(rb_eStandardError, "invalid or freed Pitchfork::MemoryPage");
122
154
  }
123
-
124
155
  return page;
125
156
  }
126
157
 
158
+ static void memory_page_map(struct memory_page *page, size_t map_size)
159
+ {
160
+ int tries = 1;
161
+
162
+ retry_mmap:
163
+ page->slots = mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, page->fd, 0);
164
+
165
+ if (page->slots == MAP_FAILED) {
166
+ int err = errno;
167
+
168
+ if ((err == EAGAIN || err == ENOMEM) && tries-- > 0) {
169
+ rb_gc();
170
+ goto retry_mmap;
171
+ }
172
+
173
+ memory_page_close_fd(page);
174
+ rb_sys_fail("mmap");
175
+ }
176
+ }
177
+
178
+ static VALUE memory_page_for_fd(VALUE klass, VALUE fd_val)
179
+ {
180
+ VALUE self = memory_page_alloc(klass);
181
+ struct memory_page *page = memory_page_get_raw(self);
182
+
183
+ int fd = NUM2INT(fd_val);
184
+ struct stat shm_stat;
185
+ if (fstat(fd, &shm_stat)) {
186
+ rb_sys_fail("fstat");
187
+ }
188
+ page->fd = fd;
189
+ page->size = page->capa = shm_stat.st_size / slot_size;
190
+
191
+ memory_page_map(page, shm_stat.st_size);
192
+ return self;
193
+ }
194
+
127
195
  static unsigned long *memory_page_address(VALUE self, VALUE index)
128
196
  {
129
197
  struct memory_page *page = memory_page_get(self);
@@ -149,13 +217,109 @@ static VALUE memory_page_aset(VALUE self, VALUE index, VALUE value)
149
217
  return value;
150
218
  }
151
219
 
220
+ static VALUE memory_page_fileno(VALUE self)
221
+ {
222
+ struct memory_page *page = memory_page_get(self);
223
+ return INT2NUM(page->fd);
224
+ }
225
+
226
+ static VALUE memory_page_close(VALUE self)
227
+ {
228
+ struct memory_page *page = memory_page_get_raw(self);
229
+
230
+ if (memory_page_unmap(page)) {
231
+ rb_sys_fail("munmap");
232
+ }
233
+
234
+ if (memory_page_close_fd(page)) {
235
+ rb_sys_fail("close");
236
+ }
237
+
238
+ return Qnil;
239
+ }
240
+
241
+ static VALUE memory_page_closed_p(VALUE self)
242
+ {
243
+ struct memory_page *page = memory_page_get_raw(self);
244
+ return (page->fd == -1 || page->slots == MAP_FAILED) ? Qtrue : Qfalse;
245
+ }
246
+
247
+ static VALUE memory_page_close_on_exec_p(VALUE self)
248
+ {
249
+ struct memory_page *page = memory_page_get(self);
250
+ int ret = fcntl(page->fd, F_GETFD);
251
+ if (ret == -1) {
252
+ rb_sys_fail("fcntl F_GETFD");
253
+ }
254
+
255
+ return (ret & FD_CLOEXEC) ? Qtrue : Qfalse;
256
+ }
257
+
258
+ static VALUE memory_page_close_on_exec_set(VALUE self, VALUE close_on_exec)
259
+ {
260
+ struct memory_page *page = memory_page_get(self);
261
+ int ret = fcntl(page->fd, F_GETFD);
262
+ if (ret == -1) {
263
+ rb_sys_fail("fcntl F_GETFD");
264
+ }
265
+
266
+ int flag = RTEST(close_on_exec) ? FD_CLOEXEC : 0;
267
+ flag = (ret & ~FD_CLOEXEC) | flag;
268
+
269
+ if (fcntl(page->fd, F_SETFD, flag) == -1) {
270
+ rb_sys_fail("fcntl F_SETFD");
271
+ }
272
+
273
+ return close_on_exec;
274
+ }
275
+
276
+ #define SHM_NAME_BUF_SIZE 50
277
+
278
+ static int memory_page_memfd_create(size_t map_size)
279
+ {
280
+ #ifdef HAVE_MEMFD_CREATE
281
+ int fd = memfd_create("pitchfork:memory_page", MFD_CLOEXEC);
282
+ if (fd == -1) {
283
+ rb_sys_fail("memfd_create");
284
+ }
285
+ #else
286
+ char name[SHM_NAME_BUF_SIZE];
287
+ int fd;
288
+
289
+ retry_shm_open:
290
+
291
+ if (snprintf(name, SHM_NAME_BUF_SIZE, "/pitchfork-%d", rand()) < 0) {
292
+ rb_sys_fail("snprintf");
293
+ }
294
+
295
+ fd = shm_open(name, O_CREAT | O_EXCL | O_RDWR, 0600);
296
+ if (fd == -1) {
297
+ if (errno == EEXIST) {
298
+ goto retry_shm_open;
299
+ }
300
+ rb_sys_fail("shm_open");
301
+ }
302
+
303
+ if (shm_unlink(name) == -1) {
304
+ close(fd);
305
+ rb_sys_fail("shm_unlink");
306
+ }
307
+
308
+ #endif
309
+
310
+ if (ftruncate(fd, map_size)) {
311
+ close(fd);
312
+ rb_sys_fail("ftruncate");
313
+ }
314
+
315
+ return fd;
316
+ }
317
+
152
318
  static VALUE memory_page_initialize(VALUE self, VALUE size)
153
319
  {
154
320
  struct memory_page *page;
155
321
  TypedData_Get_Struct(self, struct memory_page, &memory_page_type, page);
156
322
 
157
- int tries = 1;
158
-
159
323
  if (page->slots != MAP_FAILED) {
160
324
  rb_raise(rb_eRuntimeError, "already initialized");
161
325
  }
@@ -165,24 +329,15 @@ static VALUE memory_page_initialize(VALUE self, VALUE size)
165
329
  rb_raise(rb_eArgError, "size must be >= 1");
166
330
  }
167
331
 
168
- size_t tmp = PAGE_ALIGN(slot_size * page->size);
169
- page->capa = tmp / slot_size;
170
- assert(PAGE_ALIGN(slot_size * page->capa) == tmp && "not aligned");
332
+ size_t map_size = PAGE_ALIGN(slot_size * page->size);
333
+ page->capa = map_size / slot_size;
334
+ assert(PAGE_ALIGN(slot_size * page->capa) == map_size && "not aligned");
171
335
 
172
- retry:
173
- page->slots = mmap(NULL, tmp, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
336
+ page->fd = memory_page_memfd_create(map_size);
174
337
 
175
- if (page->slots == MAP_FAILED) {
176
- int err = errno;
338
+ memory_page_map(page, map_size);
177
339
 
178
- if ((err == EAGAIN || err == ENOMEM) && tries-- > 0) {
179
- rb_gc();
180
- goto retry;
181
- }
182
- rb_sys_fail("mmap");
183
- }
184
-
185
- memset(page->slots, 0, tmp);
340
+ memset(page->slots, 0, map_size);
186
341
 
187
342
  return self;
188
343
  }
@@ -217,7 +372,15 @@ void init_pitchfork_memory_page(VALUE mPitchfork)
217
372
  rb_define_const(rb_cMemoryPage, "SLOT_MAX", ULONG2NUM((unsigned long)-1));
218
373
 
219
374
  rb_define_alloc_func(rb_cMemoryPage, memory_page_alloc);
375
+
376
+ rb_define_singleton_method(rb_cMemoryPage, "for_fd", memory_page_for_fd, 1);
377
+
220
378
  rb_define_private_method(rb_cMemoryPage, "initialize", memory_page_initialize, 1);
221
379
  rb_define_method(rb_cMemoryPage, "[]", memory_page_aref, 1);
222
380
  rb_define_method(rb_cMemoryPage, "[]=", memory_page_aset, 2);
381
+ rb_define_method(rb_cMemoryPage, "fileno", memory_page_fileno, 0);
382
+ rb_define_method(rb_cMemoryPage, "close", memory_page_close, 0);
383
+ rb_define_method(rb_cMemoryPage, "closed?", memory_page_closed_p, 0);
384
+ rb_define_method(rb_cMemoryPage, "close_on_exec?", memory_page_close_on_exec_p, 0);
385
+ rb_define_method(rb_cMemoryPage, "close_on_exec=", memory_page_close_on_exec_set, 1);
223
386
  }