forked from knu/ruby-mmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmap.c
2469 lines (2266 loc) · 56.3 KB
/
mmap.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ruby.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#if HAVE_SEMCTL && HAVE_SHMCTL
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#endif
#include <rubyio.h>
#include <intern.h>
#include <re.h>
#ifndef MADV_NORMAL
#ifdef POSIX_MADV_NORMAL
#define MADV_NORMAL POSIX_MADV_NORMAL
#define MADV_RANDOM POSIX_MADV_RANDOM
#define MADV_SEQUENTIAL POSIX_MADV_SEQUENTIAL
#define MADV_WILLNEED POSIX_MADV_WILLNEED
#define MADV_DONTNEED POSIX_MADV_DONTNEED
#define madvise posix_madvise
#endif
#endif
#define BEG(no) regs->beg[no]
#define END(no) regs->end[no]
#ifndef MMAP_RETTYPE
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309
#endif /* !_POSIX_C_SOURCE */
#ifdef _POSIX_VERSION
#if _POSIX_VERSION >= 199309
#define MMAP_RETTYPE void *
#endif /* _POSIX_VERSION >= 199309 */
#endif /* _POSIX_VERSION */
#endif /* !MMAP_RETTYPE */
#ifndef MMAP_RETTYPE
#define MMAP_RETTYPE caddr_t
#endif
#ifndef MAP_FAILED
#define MAP_FAILED ((caddr_t)-1)
#endif /* !MAP_FAILED */
#ifndef MAP_ANON
#ifdef MAP_ANONYMOUS
#define MAP_ANON MAP_ANONYMOUS
#endif
#endif
static VALUE mm_cMap;
#define EXP_INCR_SIZE 4096
typedef struct {
MMAP_RETTYPE addr;
int smode, pmode, vscope;
int advice, flag;
VALUE key;
int semid, shmid;
size_t len, real, incr;
off_t offset;
char *path, *template;
} mm_mmap;
typedef struct {
int count;
mm_mmap *t;
} mm_ipc;
typedef struct {
VALUE obj, *argv;
int flag, id, argc;
} mm_bang;
#define MM_MODIFY 1
#define MM_ORIGIN 2
#define MM_CHANGE (MM_MODIFY | 4)
#define MM_PROTECT 8
#define MM_FROZEN (1<<0)
#define MM_FIXED (1<<1)
#define MM_ANON (1<<2)
#define MM_LOCK (1<<3)
#define MM_IPC (1<<4)
#define MM_TMP (1<<5)
#if HAVE_SEMCTL && HAVE_SHMCTL
static char template[1024];
union semun
{
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
#endif
static void
mm_free(mm_ipc *i_mm)
{
#if HAVE_SEMCTL && HAVE_SHMCTL
if (i_mm->t->flag & MM_IPC) {
struct shmid_ds buf;
if (shmctl(i_mm->t->shmid, IPC_STAT, &buf) != -1) {
if (buf.shm_nattch == 1 && (i_mm->t->flag & MM_TMP)) {
semctl(i_mm->t->semid, 0, IPC_RMID);
if (i_mm->t->template) {
unlink(i_mm->t->template);
free(i_mm->t->template);
}
}
}
shmdt(i_mm->t);
}
else {
free(i_mm->t);
}
#endif
if (i_mm->t->path) {
munmap(i_mm->t->addr, i_mm->t->len);
if (i_mm->t->path != (char *)-1) {
if (i_mm->t->real < i_mm->t->len && i_mm->t->vscope != MAP_PRIVATE &&
truncate(i_mm->t->path, i_mm->t->real) == -1) {
free(i_mm->t->path);
free(i_mm);
rb_raise(rb_eTypeError, "truncate");
}
free(i_mm->t->path);
}
}
free(i_mm);
}
static void
mm_lock(mm_ipc *i_mm, int wait_lock)
{
#if HAVE_SEMCTL && HAVE_SHMCTL
struct sembuf sem_op;
if (i_mm->t->flag & MM_IPC) {
i_mm->count++;
if (i_mm->count == 1) {
retry:
sem_op.sem_num = 0;
sem_op.sem_op = -1;
sem_op.sem_flg = IPC_NOWAIT;
if (semop(i_mm->t->semid, &sem_op, 1) == -1) {
if (errno == EAGAIN) {
if (!wait_lock) {
rb_raise(rb_const_get(rb_mErrno, rb_intern("EAGAIN")), "EAGAIN");
}
rb_thread_sleep(1);
goto retry;
}
rb_sys_fail("semop()");
}
}
}
#endif
}
static void
mm_unlock(mm_ipc *i_mm)
{
#if HAVE_SEMCTL && HAVE_SHMCTL
struct sembuf sem_op;
if (i_mm->t->flag & MM_IPC) {
i_mm->count--;
if (!i_mm->count) {
retry:
sem_op.sem_num = 0;
sem_op.sem_op = 1;
sem_op.sem_flg = IPC_NOWAIT;
if (semop(i_mm->t->semid, &sem_op, 1) == -1) {
if (errno == EAGAIN) {
rb_thread_sleep(1);
goto retry;
}
rb_sys_fail("semop()");
}
}
}
#endif
}
#define GetMmap(obj, i_mm, t_modify) \
Data_Get_Struct(obj, mm_ipc, i_mm); \
if (!i_mm->t->path) { \
rb_raise(rb_eIOError, "unmapped file"); \
} \
if ((t_modify & MM_MODIFY) && (i_mm->t->flag & MM_FROZEN)) { \
rb_error_frozen("mmap"); \
}
static VALUE
mm_vunlock(VALUE obj)
{
mm_ipc *i_mm;
GetMmap(obj, i_mm, 0);
mm_unlock(i_mm);
return Qnil;
}
/*
* call-seq: semlock
*
* Create a lock
*/
static VALUE
mm_semlock(int argc, VALUE *argv, VALUE obj)
{
mm_ipc *i_mm;
GetMmap(obj, i_mm, 0);
if (!(i_mm->t->flag & MM_IPC)) {
rb_warning("useless use of #semlock");
rb_yield(obj);
}
else {
#if HAVE_SEMCTL && HAVE_SHMCTL
VALUE a;
int wait_lock = Qtrue;
if (rb_scan_args(argc, argv, "01", &a)) {
wait_lock = RTEST(a);
}
mm_lock(i_mm, wait_lock);
rb_ensure(rb_yield, obj, mm_vunlock, obj);
#endif
}
return Qnil;
}
/*
* call-seq: ipc_key
*
* Get the ipc key
*/
static VALUE
mm_ipc_key(VALUE obj)
{
mm_ipc *i_mm;
GetMmap(obj, i_mm, 0);
if (i_mm->t->flag & MM_IPC) {
return INT2NUM(i_mm->t->key);
}
return INT2NUM(-1);
}
/*
* Document-method: munmap
* Document-method: unmap
*
* call-seq: munmap
*
* terminate the association
*/
static VALUE
mm_unmap(VALUE obj)
{
mm_ipc *i_mm;
GetMmap(obj, i_mm, 0);
if (i_mm->t->path) {
mm_lock(i_mm, Qtrue);
munmap(i_mm->t->addr, i_mm->t->len);
if (i_mm->t->path != (char *)-1) {
if (i_mm->t->real < i_mm->t->len && i_mm->t->vscope != MAP_PRIVATE &&
truncate(i_mm->t->path, i_mm->t->real) == -1) {
rb_raise(rb_eTypeError, "truncate");
}
free(i_mm->t->path);
}
i_mm->t->path = '\0';
mm_unlock(i_mm);
}
return Qnil;
}
/*
* call-seq: freeze
*
* freeze the current file
*/
static VALUE
mm_freeze(VALUE obj)
{
mm_ipc *i_mm;
rb_obj_freeze(obj);
GetMmap(obj, i_mm, 0);
i_mm->t->flag |= MM_FROZEN;
return obj;
}
static VALUE
mm_str(VALUE obj, int modify)
{
mm_ipc *i_mm;
VALUE ret = Qnil;
GetMmap(obj, i_mm, modify & ~MM_ORIGIN);
if (modify & MM_MODIFY) {
if (i_mm->t->flag & MM_FROZEN) rb_error_frozen("mmap");
if (!OBJ_TAINTED(ret) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: can't modify mmap");
}
ret = rb_obj_alloc(rb_cString);
if (rb_obj_tainted(obj)) {
OBJ_TAINT(ret);
}
RSTRING(ret)->ptr = i_mm->t->addr;
RSTRING(ret)->len = i_mm->t->real;
if (modify & MM_ORIGIN) {
RSTRING(ret)->aux.shared = ret;
FL_SET(ret, ELTS_SHARED);
}
if (i_mm->t->flag & MM_FROZEN) {
ret = rb_obj_freeze(ret);
}
return ret;
}
/*
* call-seq: to_str
*
* Convert object to a string
*/
static VALUE
mm_to_str(VALUE obj)
{
return mm_str(obj, MM_ORIGIN);
}
extern char *ruby_strdup();
typedef struct {
mm_ipc *i_mm;
size_t len;
} mm_st;
static VALUE
mm_i_expand(mm_st *st_mm)
{
int fd;
mm_ipc *i_mm = st_mm->i_mm;
size_t len = st_mm->len;
if (munmap(i_mm->t->addr, i_mm->t->len)) {
rb_raise(rb_eArgError, "munmap failed");
}
if ((fd = open(i_mm->t->path, i_mm->t->smode)) == -1) {
rb_raise(rb_eArgError, "Can't open %s", i_mm->t->path);
}
if (len > i_mm->t->len) {
if (lseek(fd, len - i_mm->t->len - 1, SEEK_END) == -1) {
rb_raise(rb_eIOError, "Can't lseek %d", len - i_mm->t->len - 1);
}
if (write(fd, "\000", 1) != 1) {
rb_raise(rb_eIOError, "Can't extend %s", i_mm->t->path);
}
}
else if (len < i_mm->t->len && truncate(i_mm->t->path, len) == -1) {
rb_raise(rb_eIOError, "Can't truncate %s", i_mm->t->path);
}
i_mm->t->addr = mmap(0, len, i_mm->t->pmode, i_mm->t->vscope, fd, i_mm->t->offset);
close(fd);
if (i_mm->t->addr == MAP_FAILED) {
rb_raise(rb_eArgError, "mmap failed");
}
#ifdef MADV_NORMAL
if (i_mm->t->advice && madvise(i_mm->t->addr, len, i_mm->t->advice) == -1) {
rb_raise(rb_eArgError, "madvise(%d)", errno);
}
#endif
if ((i_mm->t->flag & MM_LOCK) && mlock(i_mm->t->addr, len) == -1) {
rb_raise(rb_eArgError, "mlock(%d)", errno);
}
i_mm->t->len = len;
return Qnil;
}
static void
mm_expandf(mm_ipc *i_mm, size_t len)
{
int status;
mm_st st_mm;
if (i_mm->t->vscope == MAP_PRIVATE) {
rb_raise(rb_eTypeError, "expand for a private map");
}
if (i_mm->t->flag & MM_FIXED) {
rb_raise(rb_eTypeError, "expand for a fixed map");
}
if (!i_mm->t->path || i_mm->t->path == (char *)-1) {
rb_raise(rb_eTypeError, "expand for an anonymous map");
}
st_mm.i_mm = i_mm;
st_mm.len = len;
if (i_mm->t->flag & MM_IPC) {
mm_lock(i_mm, Qtrue);
rb_protect((VALUE (*)(VALUE))mm_i_expand, (VALUE)&st_mm, &status);
mm_unlock(i_mm);
if (status) {
rb_jump_tag(status);
}
}
else {
mm_i_expand(&st_mm);
}
}
static void
mm_realloc(mm_ipc *i_mm, size_t len)
{
if (i_mm->t->flag & MM_FROZEN) rb_error_frozen("mmap");
if (len > i_mm->t->len) {
if ((len - i_mm->t->len) < i_mm->t->incr) {
len = i_mm->t->len + i_mm->t->incr;
}
mm_expandf(i_mm, len);
}
}
/*
* call-seq:
* extend(count)
*
* add <em>count</em> bytes to the file (i.e. pre-extend the file)
*/
static VALUE
mm_extend(VALUE obj, VALUE a)
{
mm_ipc *i_mm;
long len;
GetMmap(obj, i_mm, MM_MODIFY);
len = NUM2LONG(a);
if (len > 0) {
mm_expandf(i_mm, i_mm->t->len + len);
}
return UINT2NUM(i_mm->t->len);
}
static VALUE
mm_i_options(VALUE arg, VALUE obj)
{
mm_ipc *i_mm;
char *options;
VALUE key, value;
Data_Get_Struct(obj, mm_ipc, i_mm);
key = rb_ary_entry(arg, 0);
value = rb_ary_entry(arg, 1);
key = rb_obj_as_string(key);
options = StringValuePtr(key);
if (strcmp(options, "length") == 0) {
i_mm->t->len = NUM2UINT(value);
if (i_mm->t->len <= 0) {
rb_raise(rb_eArgError, "Invalid value for length %d", i_mm->t->len);
}
i_mm->t->flag |= MM_FIXED;
}
else if (strcmp(options, "offset") == 0) {
i_mm->t->offset = NUM2INT(value);
if (i_mm->t->offset < 0) {
rb_raise(rb_eArgError, "Invalid value for offset %d", i_mm->t->offset);
}
i_mm->t->flag |= MM_FIXED;
}
else if (strcmp(options, "advice") == 0) {
i_mm->t->advice = NUM2INT(value);
}
else if (strcmp(options, "increment") == 0) {
int incr = NUM2INT(value);
if (incr < 0) {
rb_raise(rb_eArgError, "Invalid value for increment %d", incr);
}
i_mm->t->incr = incr;
}
else if (strcmp(options, "initialize") == 0) {
}
#if HAVE_SEMCTL && HAVE_SHMCTL
else if (strcmp(options, "ipc") == 0) {
if (value != Qtrue && TYPE(value) != T_HASH) {
rb_raise(rb_eArgError, "Expected an Hash for :ipc");
}
i_mm->t->shmid = value;
i_mm->t->flag |= (MM_IPC | MM_TMP);
}
#endif
else {
rb_warning("Unknown option `%s'", options);
}
return Qnil;
}
#if HAVE_SEMCTL && HAVE_SHMCTL
static VALUE
mm_i_ipc(VALUE arg, VALUE obj)
{
mm_ipc *i_mm;
char *options;
VALUE key, value;
Data_Get_Struct(obj, mm_ipc, i_mm);
key = rb_ary_entry(arg, 0);
value = rb_ary_entry(arg, 1);
key = rb_obj_as_string(key);
options = StringValuePtr(key);
if (strcmp(options, "key") == 0) {
i_mm->t->key = rb_funcall2(value, rb_intern("to_int"), 0, 0);
}
else if (strcmp(options, "permanent") == 0) {
if (RTEST(value)) {
i_mm->t->flag &= ~MM_TMP;
}
}
else if (strcmp(options, "mode") == 0) {
i_mm->t->semid = NUM2INT(value);
}
else {
rb_warning("Unknown option `%s'", options);
}
return Qnil;
}
#endif
/*
* call-seq:
* new(file, mode = "r", protection = Mmap::MAP_SHARED, options = {})
*
* create a new Mmap object
*
* * <em>file</em>
*
* Pathname of the file, if <em>nil</em> is given an anonymous map
* is created <em>Mmanp::MAP_ANON</em>
*
* * <em>mode</em>
*
* Mode to open the file, it can be "r", "w", "rw", "a"
*
* * <em>protection</em>
*
* specify the nature of the mapping
*
* * <em>Mmap::MAP_SHARED</em>
*
* Creates a mapping that's shared with all other processes
* mapping the same areas of the file.
* The default value is <em>Mmap::MAP_SHARED</em>
*
* * <em>Mmap::MAP_PRIVATE</em>
*
* Creates a private copy-on-write mapping, so changes to the
* contents of the mmap object will be private to this process
*
* * <em>options</em>
*
* Hash. If one of the options <em>length</em> or <em>offset</em>
* is specified it will not possible to modify the size of
* the mapped file.
*
* length:: maps <em>length</em> bytes from the file
*
* offset:: the mapping begin at <em>offset</em>
*
* advice:: the type of the access (see #madvise)
*/
static VALUE
mm_s_new(int argc, VALUE *argv, VALUE obj)
{
VALUE res = rb_funcall2(obj, rb_intern("allocate"), 0, 0);
rb_obj_call_init(res, argc, argv);
return res;
}
static VALUE
mm_s_alloc(VALUE obj)
{
VALUE res;
mm_ipc *i_mm;
res = Data_Make_Struct(obj, mm_ipc, 0, mm_free, i_mm);
i_mm->t = ALLOC_N(mm_mmap, 1);
MEMZERO(i_mm->t, mm_mmap, 1);
i_mm->t->incr = EXP_INCR_SIZE;
return res;
}
/*
* call-seq: initialize
*
* Create a new Mmap object
*/
static VALUE
mm_init(int argc, VALUE *argv, VALUE obj)
{
struct stat st;
int fd, smode = 0, pmode = 0, vscope, perm, init;
MMAP_RETTYPE addr;
VALUE fname, fdv, vmode, scope, options;
mm_ipc *i_mm;
char *path, *mode;
size_t size = 0;
off_t offset;
int anonymous;
options = Qnil;
if (argc > 1 && TYPE(argv[argc - 1]) == T_HASH) {
options = argv[argc - 1];
argc--;
}
rb_scan_args(argc, argv, "12", &fname, &vmode, &scope);
vscope = 0;
path = 0;
fd = -1;
anonymous = 0;
fdv = Qnil;
#ifdef MAP_ANON
if (NIL_P(fname)) {
vscope = MAP_ANON | MAP_SHARED;
anonymous = 1;
}
else
#endif
{
if (rb_safe_level() > 0 && OBJ_TAINTED(fname)){
rb_raise(rb_eSecurityError, "Insecure operation");
}
rb_secure(4);
if (rb_respond_to(fname, rb_intern("fileno"))) {
fdv = rb_funcall2(fname, rb_intern("fileno"), 0, 0);
}
if (NIL_P(fdv)) {
fname = rb_str_to_str(fname);
SafeStringValue(fname);
path = StringValuePtr(fname);
}
else {
fd = NUM2INT(fdv);
if (fd < 0) {
rb_raise(rb_eArgError, "invalid file descriptor %d", fd);
}
}
if (!NIL_P(scope)) {
vscope = NUM2INT(scope);
#ifdef MAP_ANON
if (vscope & MAP_ANON) {
rb_raise(rb_eArgError, "filename specified for an anonymous map");
}
#endif
}
}
vscope |= NIL_P(scope) ? MAP_SHARED : NUM2INT(scope);
size = 0;
perm = 0666;
if (!anonymous) {
if (NIL_P(vmode)) {
mode = "r";
}
else if (rb_respond_to(vmode, rb_intern("to_ary"))) {
VALUE tmp;
vmode = rb_convert_type(vmode, T_ARRAY, "Array", "to_ary");
if (RARRAY(vmode)->len != 2) {
rb_raise(rb_eArgError, "Invalid length %d (expected 2)",
RARRAY(vmode)->len);
}
tmp = RARRAY(vmode)->ptr[0];
mode = StringValuePtr(tmp);
perm = NUM2INT(RARRAY(vmode)->ptr[1]);
}
else {
mode = StringValuePtr(vmode);
}
if (strcmp(mode, "r") == 0) {
smode = O_RDONLY;
pmode = PROT_READ;
}
else if (strcmp(mode, "w") == 0) {
smode = O_RDWR | O_TRUNC;
pmode = PROT_READ | PROT_WRITE;
}
else if (strcmp(mode, "rw") == 0 || strcmp(mode, "wr") == 0) {
smode = O_RDWR;
pmode = PROT_READ | PROT_WRITE;
}
else if (strcmp(mode, "a") == 0) {
smode = O_RDWR | O_CREAT;
pmode = PROT_READ | PROT_WRITE;
}
else {
rb_raise(rb_eArgError, "Invalid mode %s", mode);
}
if (NIL_P(fdv)) {
if ((fd = open(path, smode, perm)) == -1) {
rb_raise(rb_eArgError, "Can't open %s", path);
}
}
if (fstat(fd, &st) == -1) {
rb_raise(rb_eArgError, "Can't stat %s", path);
}
size = st.st_size;
}
else {
fd = -1;
if (!NIL_P(vmode) && TYPE(vmode) != T_STRING) {
size = NUM2INT(vmode);
}
}
Data_Get_Struct(obj, mm_ipc, i_mm);
if (i_mm->t->flag & MM_FROZEN) {
rb_raise(rb_eArgError, "frozen mmap");
}
i_mm->t->shmid = 0;
i_mm->t->semid = 0;
offset = 0;
if (options != Qnil) {
rb_iterate(rb_each, options, mm_i_options, obj);
if (path && (i_mm->t->len + i_mm->t->offset) > st.st_size) {
rb_raise(rb_eArgError, "invalid value for length (%d) or offset (%d)",
i_mm->t->len, i_mm->t->offset);
}
if (i_mm->t->len) size = i_mm->t->len;
offset = i_mm->t->offset;
#if HAVE_SEMCTL && HAVE_SHMCTL
if (i_mm->t->flag & MM_IPC) {
key_t key;
int shmid, semid, mode;
union semun sem_val;
struct shmid_ds buf;
mm_mmap *data;
if (!(vscope & MAP_SHARED)) {
rb_warning("Probably it will not do what you expect ...");
}
i_mm->t->key = -1;
i_mm->t->semid = 0;
if (TYPE(i_mm->t->shmid) == T_HASH) {
rb_iterate(rb_each, i_mm->t->shmid, mm_i_ipc, obj);
}
i_mm->t->shmid = 0;
if (i_mm->t->semid) {
mode = i_mm->t->semid;
i_mm->t->semid = 0;
}
else {
mode = 0644;
}
if ((int)i_mm->t->key <= 0) {
mode |= IPC_CREAT;
strcpy(template, "/tmp/ruby_mmap.XXXXXX");
if (mkstemp(template) == -1) {
rb_sys_fail("mkstemp()");
}
if ((key = ftok(template, 'R')) == -1) {
rb_sys_fail("ftok()");
}
}
else {
key = (key_t)i_mm->t->key;
}
if ((shmid = shmget(key, sizeof(mm_ipc), mode)) == -1) {
rb_sys_fail("shmget()");
}
data = shmat(shmid, (void *)0, 0);
if (data == (mm_mmap *)-1) {
rb_sys_fail("shmat()");
}
if (i_mm->t->flag & MM_TMP) {
if (shmctl(shmid, IPC_RMID, &buf) == -1) {
rb_sys_fail("shmctl()");
}
}
if ((semid = semget(key, 1, mode)) == -1) {
rb_sys_fail("semget()");
}
if (mode & IPC_CREAT) {
sem_val.val = 1;
if (semctl(semid, 0, SETVAL, sem_val) == -1) {
rb_sys_fail("semctl()");
}
}
memcpy(data, i_mm->t, sizeof(mm_mmap));
free(i_mm->t);
i_mm->t = data;
i_mm->t->key = key;
i_mm->t->semid = semid;
i_mm->t->shmid = shmid;
if (i_mm->t->flag & MM_TMP) {
i_mm->t->template = ALLOC_N(char, strlen(template) + 1);
strcpy(i_mm->t->template, template);
}
}
#endif
}
init = 0;
if (anonymous) {
if (size <= 0) {
rb_raise(rb_eArgError, "length not specified for an anonymous map");
}
if (offset) {
rb_warning("Ignoring offset for an anonymous map");
offset = 0;
}
smode = O_RDWR;
pmode = PROT_READ | PROT_WRITE;
i_mm->t->flag |= MM_FIXED | MM_ANON;
}
else {
if (size == 0 && (smode & O_RDWR)) {
if (lseek(fd, i_mm->t->incr - 1, SEEK_END) == -1) {
rb_raise(rb_eIOError, "Can't lseek %d", i_mm->t->incr - 1);
}
if (write(fd, "\000", 1) != 1) {
rb_raise(rb_eIOError, "Can't extend %s", path);
}
init = 1;
size = i_mm->t->incr;
}
if (!NIL_P(fdv)) {
i_mm->t->flag |= MM_FIXED;
}
}
addr = mmap(0, size, pmode, vscope, fd, offset);
if (NIL_P(fdv) && !anonymous) {
close(fd);
}
if (addr == MAP_FAILED || !addr) {
rb_raise(rb_eArgError, "mmap failed (%d)", errno);
}
#ifdef MADV_NORMAL
if (i_mm->t->advice && madvise(addr, size, i_mm->t->advice) == -1) {
rb_raise(rb_eArgError, "madvise(%d)", errno);
}
#endif
if (anonymous && TYPE(options) == T_HASH) {
VALUE val;
char *ptr;
val = rb_hash_aref(options, rb_str_new2("initialize"));
if (!NIL_P(val)) {
ptr = StringValuePtr(val);
memset(addr, ptr[0], size);
}
}
i_mm->t->addr = addr;
i_mm->t->len = size;
if (!init) i_mm->t->real = size;
i_mm->t->pmode = pmode;
i_mm->t->vscope = vscope;
i_mm->t->smode = smode & ~O_TRUNC;
i_mm->t->path = (path)?ruby_strdup(path):(char *)-1;
if (smode == O_RDONLY) {
obj = rb_obj_freeze(obj);
i_mm->t->flag |= MM_FROZEN;
}
else {
if (smode == O_WRONLY) {
i_mm->t->flag |= MM_FIXED;
}
OBJ_TAINT(obj);
}
return obj;
}
/*
* call-seq: initialize
*
* Create a new Mmap object
*/
static VALUE
mm_init_copy(VALUE obj, VALUE other)
{
rb_raise(rb_eTypeError, "can't copy %s", rb_obj_classname(other));
}
/*
* Document-method: msync
* Document-method: sync
* Document-method: flush
*
* call-seq: msync
*
* flush the file
*/
static VALUE
mm_msync(int argc, VALUE *argv, VALUE obj)
{
mm_ipc *i_mm;
VALUE oflag;
int ret;
int flag = MS_SYNC;
if (argc) {
rb_scan_args(argc, argv, "01", &oflag);
flag = NUM2INT(oflag);
}
GetMmap(obj, i_mm, MM_MODIFY);
if ((ret = msync(i_mm->t->addr, i_mm->t->len, flag)) != 0) {
rb_raise(rb_eArgError, "msync(%d)", ret);
}
if (i_mm->t->real < i_mm->t->len && i_mm->t->vscope != MAP_PRIVATE)
mm_expandf(i_mm, i_mm->t->real);
return obj;
}
/*
* Document-method: mprotect
* Document-method: protect
*
* call-seq: mprotect(mode)
*
* change the mode, value must be "r", "w" or "rw"
*/
static VALUE
mm_mprotect(VALUE obj, VALUE a)
{
mm_ipc *i_mm;
int ret, pmode;
char *smode;
GetMmap(obj, i_mm, 0);
if (TYPE(a) == T_STRING) {
smode = StringValuePtr(a);
if (strcmp(smode, "r") == 0) pmode = PROT_READ;
else if (strcmp(smode, "w") == 0) pmode = PROT_WRITE;
else if (strcmp(smode, "rw") == 0 || strcmp(smode, "wr") == 0)
pmode = PROT_READ | PROT_WRITE;
else {
rb_raise(rb_eArgError, "Invalid mode %s", smode);
}
}
else {
pmode = NUM2INT(a);
}
if ((pmode & PROT_WRITE) && (i_mm->t->flag & MM_FROZEN))
rb_error_frozen("mmap");
if ((ret = mprotect(i_mm->t->addr, i_mm->t->len, pmode | PROT_READ)) != 0) {
rb_raise(rb_eArgError, "mprotect(%d)", ret);
}
i_mm->t->pmode = pmode;
if (pmode & PROT_READ) {
if (pmode & PROT_WRITE) i_mm->t->smode = O_RDWR;
else {
i_mm->t->smode = O_RDONLY;
obj = rb_obj_freeze(obj);
i_mm->t->flag |= MM_FROZEN;
}
}
else if (pmode & PROT_WRITE) {
i_mm->t->flag |= MM_FIXED;
i_mm->t->smode = O_WRONLY;
}
return obj;
}
#ifdef MADV_NORMAL
/*
* Document-method: madvise
* Document-method: advise
*
* call-seq: madvise(advice)
*
* <em>advice</em> can have the value <em>Mmap::MADV_NORMAL</em>,
* <em>Mmap::MADV_RANDOM</em>, <em>Mmap::MADV_SEQUENTIAL</em>,
* <em>Mmap::MADV_WILLNEED</em>, <em>Mmap::MADV_DONTNEED</em>
*
*/
static VALUE
mm_madvise(VALUE obj, VALUE a)
{
mm_ipc *i_mm;
GetMmap(obj, i_mm, 0);
if (madvise(i_mm->t->addr, i_mm->t->len, NUM2INT(a)) == -1) {
rb_raise(rb_eTypeError, "madvise(%d)", errno);
}
i_mm->t->advice = NUM2INT(a);
return Qnil;
}
#endif
#define StringMmap(b, bp, bl) \