This file is indexed.

/usr/share/perl5/IPC/Shareable.pm is in libipc-shareable-perl 0.61-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
package IPC::Shareable;

require 5.00503;
use strict;
use IPC::Semaphore;
use IPC::Shareable::SharedMem;
use IPC::SysV qw(
                 IPC_PRIVATE
                 IPC_CREAT
                 IPC_EXCL
                 IPC_NOWAIT
                 SEM_UNDO
                 );
use Storable 0.6 qw(
                    freeze
                    thaw
                    );
use Scalar::Util;

use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS );

$VERSION = 0.61;

use constant LOCK_SH => 1;
use constant LOCK_EX => 2;
use constant LOCK_NB => 4;
use constant LOCK_UN => 8;

require Exporter;
@ISA = 'Exporter';
@EXPORT = ();
@EXPORT_OK = qw(LOCK_EX LOCK_SH LOCK_NB LOCK_UN);
%EXPORT_TAGS = (
        all     => [qw( LOCK_EX LOCK_SH LOCK_NB LOCK_UN )],
        lock    => [qw( LOCK_EX LOCK_SH LOCK_NB LOCK_UN )],
        'flock' => [qw( LOCK_EX LOCK_SH LOCK_NB LOCK_UN )],
);
Exporter::export_ok_tags('all', 'lock', 'flock');

use constant DEBUGGING     => ($ENV{SHAREABLE_DEBUG} or 0);
use constant SHM_BUFSIZ    =>  65536;
use constant SEM_MARKER    =>  0;
use constant SHM_EXISTS    =>  1;

# Locking scheme copied from IPC::ShareLite -- ltl
my %semop_args = (
    (LOCK_EX),
        [       
                1, 0, 0,                        # wait for readers to finish
                2, 0, 0,                        # wait for writers to finish
                2, 1, SEM_UNDO,                 # assert write lock
        ],
    (LOCK_EX|LOCK_NB),
        [
                1, 0, IPC_NOWAIT,               # wait for readers to finish
                2, 0, IPC_NOWAIT,               # wait for writers to finish
                2, 1, (SEM_UNDO | IPC_NOWAIT),  # assert write lock
        ],
    (LOCK_EX|LOCK_UN),
        [
                2, -1, (SEM_UNDO | IPC_NOWAIT),
        ],

    (LOCK_SH),
        [
                2, 0, 0,                        # wait for writers to finish
                1, 1, SEM_UNDO,                 # assert shared read lock
        ],
    (LOCK_SH|LOCK_NB),
        [
                2, 0, IPC_NOWAIT,               # wait for writers to finish
                1, 1, (SEM_UNDO | IPC_NOWAIT),  # assert shared read lock
        ],
    (LOCK_SH|LOCK_UN),
        [
                1, -1, (SEM_UNDO | IPC_NOWAIT), # remove shared read lock
        ],
);

my %Def_Opts = (
                key       => IPC_PRIVATE,
                create    => '',
                exclusive => '',
                destroy   => '',
                mode      => 0666,
                size      => SHM_BUFSIZ,
                );

# XXX Perl seems to garbage collect nested referents before we're done with them
# XXX This cache holds a reference to things until END() is called

my %Global_Reg;
my %Proc_Reg;

sub _trace;
sub _debug;

###############################################################################
                                                                 # Debug mark

# --- Public methods
sub shlock {
    _trace @_                                                    if DEBUGGING;
    my ($self, $typelock) = @_;
    ($typelock = LOCK_EX) unless defined $typelock;

    return $self->shunlock if ($typelock & LOCK_UN);

    return 1 if ($self->{_lock} & $typelock);

    # If they have a different lock than they want, release it first
    $self->shunlock if ($self->{_lock});

    my $sem = $self->{_sem};
    _debug "Attempting type=", $typelock, " lock on", $self->{_shm}, 
        "via", $sem->id                                         if DEBUGGING;
    my $return_val = $sem->op(@{ $semop_args{$typelock} });
    if ($return_val) {
        $self->{_lock} = $typelock;
        _debug "Got lock on", $self->{_shm}, "via", $sem->id    if DEBUGGING;

        $self->{_data} = _thaw($self->{_shm}),

    } else {
        _debug "Failed lock on", $self->{_shm}, "via", $sem->id if DEBUGGING;
    }
    return $return_val;
}

sub shunlock {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    return 1 unless $self->{_lock};
    if ($self->{_was_changed}) {
        defined _freeze($self->{_shm} => $self->{_data}) or do {
            require Carp;
            Carp::croak "Could not write to shared memory: $!\n";
        };
        $self->{_was_changed} = 0;
    }
    my $sem = $self->{_sem};
    _debug "Freeing lock on", $self->{_shm}, "via", $sem->id     if DEBUGGING;
    my $typelock = $self->{_lock} | LOCK_UN;
    $typelock ^= LOCK_NB if ($typelock & LOCK_NB);
    $sem->op(@{ $semop_args{$typelock} });

    $self->{_lock} = 0;
    _debug "Lock on", $self->{_shm}, "via", $sem->id, "freed"    if DEBUGGING;

    1;
}

# --- "Magic" methods
sub TIESCALAR {
    _trace @_                                                    if DEBUGGING;
    return _tie(SCALAR => @_);
}

sub TIEARRAY {
    _trace @_                                                    if DEBUGGING;
    return _tie(ARRAY => @_);
}    

sub TIEHASH {
    _trace @_                                                    if DEBUGGING;
    return _tie(HASH => @_);
}

sub STORE {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    my $sid = $self->{_shm}->{_id};

    $Global_Reg{$self->{_shm}->id} ||= $self;

    $self->{_data} = _thaw($self->{_shm}) unless ($self->{_lock});
  TYPE: {
      if ($self->{_type} eq 'SCALAR') {
          my $val = shift;
          _mg_tie($self => $val) if _need_tie($val);
          $self->{_data} = \$val;
          last TYPE;
      }
      if ($self->{_type} eq 'ARRAY') {
          my $i   = shift;
          my $val = shift;
          _mg_tie($self => $val) if _need_tie($val);
          $self->{_data}->[$i] = $val;
          last TYPE;
      }   
      if ($self->{_type} eq 'HASH') {
          my $key = shift;
          my $val = shift;
          _mg_tie($self => $val) if _need_tie($val);
          $self->{_data}->{$key} = $val;
          last TYPE;
      }
      require Carp;
      Carp::croak "Variables of type $self->{_type} not supported";
  }

    if ($self->{_lock} & LOCK_EX) {
        $self->{_was_changed} = 1;
    } else {
        defined _freeze($self->{_shm} => $self->{_data}) or do {
            require Carp;
            Carp::croak "Could not write to shared memory: $!\n";
        };
    }
    return 1;
}

sub FETCH {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    $Global_Reg{$self->{_shm}->id} ||= $self;

    my $data;
    if ($self->{_lock} || $self->{_iterating}) {
        $self->{_iterating} = ''; # In case we break out
        $data = $self->{_data};
    } else {
        $data = _thaw($self->{_shm});
        $self->{_data} = $data;
    }

    my $val;
  TYPE: {
      if ($self->{_type} eq 'SCALAR') {
          if (defined $data) {
              $val = $$data;
              last TYPE;
          } else {
              return;
          }
      }
      if ($self->{_type} eq 'ARRAY') {
          if (defined $data) {
              my $i = shift;
              $val = $data->[$i];
              last TYPE;
          } else {
              return;
          }
      }
      if ($self->{_type} eq 'HASH') {
          if (defined $data) {
              my $key = shift;
              $val = $data->{$key};
              last TYPE;
          } else {
              return;
          }
      }
      require Carp;
      Carp::croak "Variables of type $self->{_type} not supported";
  }

    if (my $inner = _is_kid($val)) {
        my $s = $inner->{_shm};
        $inner->{_data} = _thaw($s);
    }
    return $val;

}

sub CLEAR {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    if ($self->{_type} eq 'ARRAY') {
        $self->{_data} = [ ];
    } elsif ($self->{_type} eq 'HASH') {
        $self->{_data} = { };
    } else {
        require Carp;
        Carp::croak "Attempt to clear non-aggegrate";
    }

    if ($self->{_lock} & LOCK_EX) {
        $self->{_was_changed} = 1;
    } else {
        defined _freeze($self->{_shm} => $self->{_data}) or do {
            require Carp;
            Carp::croak "Could not write to shared memory: $!";
        };
    }
}

sub DELETE {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;
    my $key  = shift;

    $self->{_data} = _thaw($self->{_shm}) unless $self->{_lock};
    my $val = delete $self->{_data}->{$key};
    if ($self->{_lock} & LOCK_EX) {
        $self->{_was_changed} = 1;
    } else {
        defined _freeze($self->{_shm} => $self->{_data}) or do {
            require Carp;
            Carp::croak "Could not write to shared memory: $!";
        };
    }

    return $val;
}

sub EXISTS {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;
    my $key  = shift;

    $self->{_data} = _thaw($self->{_shm}) unless $self->{_lock};
    return exists $self->{_data}->{$key};
}

sub FIRSTKEY {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;
    my $key  = shift;

    _debug "setting hash iterator on", $self->{_shm}->id         if DEBUGGING;
    $self->{_iterating} = 1;
    $self->{_data} = _thaw($self->{_shm}) unless $self->{_lock};
    my $reset = keys %{$self->{_data}};
    my $first = each %{$self->{_data}};
    return $first;
}

sub NEXTKEY {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    # caveat emptor if hash was changed by another process
    my $next = each %{$self->{_data}};
    if (not defined $next) {
        _debug "resetting hash iterator on", $self->{_shm}->id   if DEBUGGING;
        $self->{_iterating} = '';
        return;
    } else {
        $self->{_iterating} = 1;
        return $next;
    }
}

sub EXTEND {
    _trace @_                                                    if DEBUGGING;
    #XXX Noop
}

sub PUSH {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    $Global_Reg{$self->{_shm}->id} ||= $self;
    $self->{_data} = _thaw($self->{_shm}, $self->{_data}) unless $self->{_lock};

    push @{$self->{_data}} => @_;
    if ($self->{_lock} & LOCK_EX) {
        $self->{_was_changed} = 1;
    } else {
        defined _freeze($self->{_shm} => $self->{_data}) or do {
            require Carp;
            Carp::croak "Could not write to shared memory: $!";
        };
    }
}

sub POP {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    $self->{_data} = _thaw($self->{_shm}, $self->{_data}) unless $self->{_lock};

    my $val = pop @{$self->{_data}};
    if ($self->{_lock} & LOCK_EX) {
        $self->{_was_changed} = 1;
    } else {
        defined _freeze($self->{_shm} => $self->{_data}) or do {
            require Carp;
            Carp::croak "Could not write to shared memory: $!";
        };
    }
    return $val;
}

sub SHIFT {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    $self->{_data} = _thaw($self->{_shm}, $self->{_data}) unless $self->{_lock};
    my $val = shift @{$self->{_data}};
    if ($self->{_lock} & LOCK_EX) {
        $self->{_was_changed} = 1;
    } else {
        defined _freeze($self->{_shm} => $self->{_data}) or do {
            require Carp;
            Carp::croak "Could not write to shared memory: $!";
        };
    }
    return $val;
}

sub UNSHIFT {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    $self->{_data} = _thaw($self->{_shm}, $self->{_data}) unless $self->{_lock};
    my $val = unshift @{$self->{_data}} => @_;
    if ($self->{_lock} & LOCK_EX) {
        $self->{_was_changed} = 1;
    } else {
        defined _freeze($self->{_shm} => $self->{_data}) or do {
            require Carp;
            Carp::croak "Could not write to shared memory: $!";
        };
    }
    return $val;
}

sub SPLICE {
    _trace @_                                                    if DEBUGGING;
    my($self, $off, $n, @av) = @_;

    $self->{_data} = _thaw($self->{_shm}, $self->{_data}) unless $self->{_lock};
    my @val = splice @{$self->{_data}}, $off, $n => @av;
    if ($self->{_lock} & LOCK_EX) {
        $self->{_was_changed} = 1;
    } else {
        defined _freeze($self->{_shm} => $self->{_data}) or do {
            require Carp;
            Carp::croak "Could not write to shared memory: $!";
        };
    }
    return @val;
}

sub FETCHSIZE {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    $self->{_data} = _thaw($self->{_shm}) unless $self->{_lock};
    return scalar(@{$self->{_data}});
}

sub STORESIZE {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;
    my $n    = shift;

    $self->{_data} = _thaw($self->{_shm}) unless $self->{_lock};
    $#{$self->{_data}} = $n - 1;
    if ($self->{_lock} & LOCK_EX) {
        $self->{_was_changed} = 1;
    } else {
        defined _freeze($self->{_shm} => $self->{_data}) or do {
            require Carp;
            Carp::croak "Could not write to shared memory: $!";
        };
    }
    return $n;
}

sub clean_up {
    _trace @_                                                    if DEBUGGING;
    my $class = shift;

    for my $s (values %Proc_Reg) {
        next unless $s->{_opts}->{_owner} == $$;
        remove($s);
    }
}

sub clean_up_all {
    _trace @_                                                    if DEBUGGING;
    my $class = shift;

    for my $s (values %Global_Reg) {
        remove($s);
    }
}

sub remove {
    _trace @_                                                    if DEBUGGING;
    my $self = shift;

    my $s = $self->{_shm};
    my $id = $s->id;
    
    $s->remove or do {
        require Carp;
        Carp::carp "Couldn't remove shared memory segment $id: $!";
    };
    
    $s = $self->{_sem};
    $s->remove or do {
        require Carp;
        Carp::carp "Couldn't remove semaphore set $id: $!";
    };
    delete $Proc_Reg{$id};
    delete $Global_Reg{$id};
}

END {
    _trace @_                                                    if DEBUGGING;
    for my $s (values %Proc_Reg) {
        shunlock($s);
        next unless $s->{_opts}->{destroy};
        next unless $s->{_opts}->{_owner} == $$;
        remove($s);
    }
}

# --- Private methods below
sub _freeze {
    _trace @_                                                    if DEBUGGING;
    my $s  = shift;
    my $water = shift;

    my $ice = freeze $water;
    # Could be a large string.  No need to copy it.  substr more efficient
    substr $ice, 0, 0, 'IPC::Shareable';

    _debug "writing to shm segment ", $s->id, ": ", $ice         if DEBUGGING;
    if (length($ice) > $s->size) {
        require Carp;
        Carp::croak "Length of shared data exceeds shared segment size";
    };
    $s->shmwrite($ice);
}

sub _thaw {
    _trace @_                                                    if DEBUGGING;
    my $s = shift;

    my $ice = $s->shmread;
    _debug "read from shm segment ", $s->id, ": ", $ice          if DEBUGGING;

    my $tag = substr $ice, 0, 14, '';

    if ($tag eq 'IPC::Shareable') {
        my $water = thaw $ice;
        defined($water) or do {
            require Carp;
            Carp::croak "Munged shared memory segment (size exceeded?)";
        };
        return $water;
    } else {
        return;
    }
}

sub _tie {
    _trace @_                                                    if DEBUGGING;
    my $type  = shift;
    my $class = shift;
    my $opts  = _parse_args(@_);

    my $key      = _shm_key($opts);
    my $flags    = _shm_flags($opts);
    my $shm_size = $opts->{size};

    my $s = IPC::Shareable::SharedMem->new($key, $shm_size, $flags);
    defined $s or do {
        require Carp;
        Carp::croak "Could not create shared memory segment: $!\n";
    };
    _debug "shared memory id is", $s->id                         if DEBUGGING;

    my $sem = IPC::Semaphore->new($key, 3, $flags);
    defined $sem or do {
        require Carp;
        Carp::croak "Could not create semaphore set: $!\n";
    };
    _debug "semaphore id is", $sem->id                           if DEBUGGING;

    unless ( $sem->op(@{ $semop_args{(LOCK_SH)} }) ) {
        require Carp;
        Carp::croak "Could not obtain semaphore set lock: $!\n";
    }
    my $sh = {
        _iterating => '',
        _key       => $key,
        _lock      => 0,
        _opts      => $opts,
        _shm       => $s,
        _sem       => $sem,
        _type      => $type,
        _was_changed => 0,
    };
    $sh->{_data} = _thaw($s),

    my $there = $sem->getval(SEM_MARKER);
    if ($there == SHM_EXISTS) {
        _debug "binding to existing segment on ", $s->id         if DEBUGGING;
    } else {
        _debug "brand new segment on ", $s->id                   if DEBUGGING;
        $Proc_Reg{$sh->{_shm}->id} ||= $sh;
        $sem->setval(SEM_MARKER, SHM_EXISTS) or do {
            require Carp;
            Carp::croak "Couldn't set semaphore during object creation: $!";
          };
    }

    $sem->op(@{ $semop_args{(LOCK_SH|LOCK_UN)} });

    _debug "IPC::Shareable instance created:", $sh               if DEBUGGING;

    return bless $sh => $class;
}

sub _parse_args {
    _trace @_                                                    if DEBUGGING;
    my($proto, $opts) = @_;

    $proto = defined $proto ? $proto :  0;
    $opts  = defined $opts  ? $opts  : { %Def_Opts };
    if (ref $proto eq 'HASH') {
        $opts = $proto;
    } else {
        $opts->{key} = $proto;
    }
    for my $k (keys %Def_Opts) {
        if (not defined $opts->{$k}) {
            $opts->{$k} = $Def_Opts{$k};
        } elsif ($opts->{$k} eq 'no') {
            if ($^W) {
                require Carp;
                Carp::carp("Use of `no' in IPC::Shareable args is obsolete");
            }

            $opts->{$k} = '';
        }
    }
    $opts->{_owner} = ($opts->{_owner} or $$);
    $opts->{_magic} = ($opts->{_magic} or '');
    _debug "options are", $opts                                  if DEBUGGING;
    return $opts;
}
    
sub _shm_key {
    _trace @_                                                    if DEBUGGING;
    my $hv = shift;
    my $val = ($hv->{key} or '');

    if ($val eq '') {
        return IPC_PRIVATE;
    } elsif ($val =~ /^\d+$/) {
        return $val;
    } else {
        # XXX This only uses the first four characters
        $val = pack   A4 => $val;
        $val = unpack i  => $val;
        return $val;
    }
}
    
sub _shm_flags {
    # --- Parses the anonymous hash passed to constructors; returns a list
    # --- of args suitable for passing to shmget
    _trace @_                                                    if DEBUGGING;
    my $hv = shift;
    my $flags = 0;
    
    $flags |= IPC_CREAT if $hv->{create};
    $flags |= IPC_EXCL  if $hv->{exclusive};
    $flags |= ($hv->{mode} or 0666);

    return $flags;
}

sub _mg_tie {
    _trace @_                                                    if DEBUGGING;
    my $dad = shift;
    my $val = shift;

    # XXX How to generate a unique id ?
    my $key;
    if ($dad->{_key} == IPC_PRIVATE) {
        $key = IPC_PRIVATE;
    } else {
        $key = int(rand(1_000_000));
    }
    my %opts = (
                %{$dad->{_opts}},
                key       => $key,
                exclusive => 'yes',
                create    => 'yes',
                _magic    => 'yes'
               );

    # XXX I wish I didn't have to take a copy of data here and copy it back in
    # XXX Also, have to peek inside potential objects to see their implementation
    my $kid;
    my $type = Scalar::Util::reftype( $val ) || '';
    if ($type eq "SCALAR") {
        my $copy = $$val;
        $kid = tie $$val => 'IPC::Shareable', $key, { %opts } or do {
            require Carp;
            Carp::croak "Could not create inner tie";
        };
        $$val = $copy;
    } elsif ($type eq "ARRAY") {
        my @copy = @$val;
        $kid = tie @$val => 'IPC::Shareable', $key, { %opts } or do {
            require Carp;
            Carp::croak "Could not create inner tie";
        };
        @$val = @copy;
    } elsif ($type eq "HASH") {
        my %copy = %$val;
        $kid = tie %$val => 'IPC::Shareable', $key, { %opts } or do {
            require Carp;
            Carp::croak "Could not create inner tie";
        };
        %$val = %copy;
    } else {
        require Carp;
        Carp::croak "Variables of type $type not implemented";
    }

    return $kid;
}

sub _is_kid {
    my $data = shift or return;

    my $type = Scalar::Util::reftype( $data );
    return unless $type;

    my $obj;
    if ($type eq "HASH") {
        $obj = tied %$data;
    } elsif ($type eq "ARRAY") { 
        $obj = tied @$data;
    } elsif ($type eq "SCALAR") {
        $obj = tied $$data;
    }

    if (ref $obj eq 'IPC::Shareable') {
        return $obj;
    } else {
        return;
    }
}

sub _need_tie {
    my $val = shift;

    my $type = Scalar::Util::reftype( $val );
    return unless $type;
    if ($type eq "SCALAR") {
        return !(tied $$val);
    } elsif ($type eq "ARRAY") {
        return !(tied @$val);
    } elsif ($type eq "HASH") {
        return !(tied %$val);
    } else {
        return;
    }
}

sub _trace {
    require Carp;
    require Data::Dumper;
    my $caller = '    ' . (caller(1))[3] . " called with:\n";
    my $i = -1;
    my @msg = map {
        ++$i;
        my $obj;
        if (ref eq 'IPC::Shareable') {
            '        ' . "\$_[$i] = $_: shmid: $_->{_shm}->{_id}; " .
                Data::Dumper->Dump([ $_->{_opts} ], [ 'opts' ]);
        } else {
            '        ' . Data::Dumper->Dump( [ $_ ] => [ "\_[$i]" ]);
        }
    }  @_;
    Carp::carp "IPC::Shareable ($$) debug:\n", $caller, @msg;
}

sub _debug {
    require Carp;
    require Data::Dumper;
    local $Data::Dumper::Terse = 1;
    my $caller = '    ' . (caller(1))[3] . " tells us that:\n";
    my @msg = map {
        my $obj;
        if (ref eq 'IPC::Shareable') {
            '        ' . "$_: shmid: $_->{_shm}->{_id}; " .
                Data::Dumper->Dump([ $_->{_opts} ], [ 'opts' ]);
        } else {
            '        ' . Data::Dumper::Dumper($_);
        }
    }  @_;
    Carp::carp "IPC::Shareable ($$) debug:\n", $caller, @msg;
};

1;

__END__

=head1 NAME

IPC::Shareable - share Perl variables between processes

=head1 SYNOPSIS

 use IPC::Shareable (':lock');
 tie SCALAR, 'IPC::Shareable', GLUE, OPTIONS;
 tie ARRAY,  'IPC::Shareable', GLUE, OPTIONS;
 tie HASH,   'IPC::Shareable', GLUE, OPTIONS;

 (tied VARIABLE)->shlock;
 (tied VARIABLE)->shunlock;

 (tied VARIABLE)->shlock(LOCK_SH|LOCK_NB) 
        or print "resource unavailable\n";

 (tied VARIABLE)->remove;

 IPC::Shareable->clean_up;
 IPC::Shareable->clean_up_all;

=head1 CONVENTIONS

The occurrence of a number in square brackets, as in [N], in the text
of this document refers to a numbered note in the L</NOTES>.

=head1 DESCRIPTION

IPC::Shareable allows you to tie a variable to shared memory making it
easy to share the contents of that variable with other Perl processes.
Scalars, arrays, and hashes can be tied.  The variable being tied may
contain arbitrarily complex data structures - including references to
arrays, hashes of hashes, etc.

The association between variables in distinct processes is provided by
GLUE.  This is an integer number or 4 character string[1] that serves
as a common identifier for data across process space.  Hence the
statement

 tie $scalar, 'IPC::Shareable', 'data';

in program one and the statement

 tie $variable, 'IPC::Shareable', 'data';

in program two will bind $scalar in program one and $variable in
program two.

There is no pre-set limit to the number of processes that can bind to
data; nor is there a pre-set limit to the complexity of the underlying
data of the tied variables[2].  The amount of data that can be shared
within a single bound variable is limited by the system's maximum size
for a shared memory segment (the exact value is system-dependent).

The bound data structures are all linearized (using Raphael Manfredi's
Storable module) before being slurped into shared memory.  Upon
retrieval, the original format of the data structure is recovered.
Semaphore flags can be used for locking data between competing processes.

=head1 OPTIONS

Options are specified by passing a reference to a hash as the fourth
argument to the tie() function that enchants a variable.
Alternatively you can pass a reference to a hash as the third
argument; IPC::Shareable will then look at the field named B<key> in
this hash for the value of GLUE.  So,

 tie $variable, 'IPC::Shareable', 'data', \%options;

is equivalent to

 tie $variable, 'IPC::Shareable', { key => 'data', ... };

Boolean option values can be specified using a value that evaluates to
either true or false in the Perl sense.

NOTE: Earlier versions allowed you to use the word B<yes> for true and
the word B<no> for false, but support for this "feature" is being
removed.  B<yes> will still act as true (since it is true, in the Perl
sense), but use of the word B<no> now emits an (optional) warning and
then converts to a false value.  This warning will become mandatory in a
future release and then at some later date the use of B<no> will
stop working altogether.

The following fields are recognized in the options hash.

=over 4

=item B<key>

The B<key> field is used to determine the GLUE when using the
three-argument form of the call to tie().  This argument is then, in
turn, used as the KEY argument in subsequent calls to shmget() and
semget().

The default value is IPC_PRIVATE, meaning that your variables cannot
be shared with other processes.

=item B<create>

B<create> is used to control whether calls to tie() create new shared
memory segments or not.  If B<create> is set to a true value,
IPC::Shareable will create a new binding associated with GLUE as
needed.  If B<create> is false, IPC::Shareable will not attempt to
create a new shared memory segment associated with GLUE.  In this
case, a shared memory segment associated with GLUE must already exist
or the call to tie() will fail and return undef.  The default is
false.

=item B<exclusive>

If B<exclusive> field is set to a true value, calls to tie() will fail
(returning undef) if a data binding associated with GLUE already
exists.  If set to a false value, calls to tie() will succeed even if
a shared memory segment associated with GLUE already exists.  The
default is false

=item B<mode>

The I<mode> argument is an octal number specifying the access
permissions when a new data binding is being created.  These access
permission are the same as file access permissions in that 0666 is
world readable, 0600 is readable only by the effective UID of the
process creating the shared variable, etc.  The default is 0666 (world
readable and writable).

=item B<destroy>

If set to a true value, the shared memory segment underlying the data
binding will be removed when the process calling tie() exits
(gracefully)[3].  Use this option with care.  In particular
you should not use this option in a program that will fork
after binding the data.  On the other hand, shared memory is
a finite resource and should be released if it is not needed.
The default is false 

=item B<size>

This field may be used to specify the size of the shared memory
segment allocated.  The default is IPC::Shareable::SHM_BUFSIZ().

=back

Default values for options are

 key       => IPC_PRIVATE,
 create    => 0,
 exclusive => 0,
 destroy   => 0,
 mode      => 0,
 size      => IPC::Shareable::SHM_BUFSIZ(),

=head1 LOCKING

IPC::Shareable provides methods to implement application-level
advisory locking of the shared data structures.  These methods are
called shlock() and shunlock().  To use them you must first get the
object underlying the tied variable, either by saving the return
value of the original call to tie() or by using the built-in tied()
function.

To lock a variable, do this:

 $knot = tie $sv, 'IPC::Shareable', $glue, { %options };
 ...
 $knot->shlock;

or equivalently

 tie($scalar, 'IPC::Shareable', $glue, { %options });
 (tied $scalar)->shlock;

This will place an exclusive lock on the data of $scalar.  You can
also get shared locks or attempt to get a lock without blocking.
IPC::Shareable makes the constants LOCK_EX, LOCK_SH, LOCK_UN, and
LOCK_NB exportable to your address space with the export tags
C<:lock>, C<:flock>, or C<:all>.  The values should be the same as
the standard C<flock> option arguments.

 if ( (tied $scalar)->shlock(LOCK_SH|LOCK_NB) ) {
        print "The value is $scalar\n";
        (tied $scalar)->shunlock;
 } else {
        print "Another process has an exlusive lock.\n";
 }


If no argument is provided to C<shlock>, it defaults to LOCK_EX.  To
unlock a variable do this:

 $knot->shunlock;

or

 (tied $scalar)->shunlock;

or

 $knot->shlock(LOCK_UN);        # Same as calling shunlock

There are some pitfalls regarding locking and signals about which you
should make yourself aware; these are discussed in L</NOTES>.

If you use the advisory locking, IPC::Shareable assumes that you know
what you are doing and attempts some optimizations.  When you obtain
a lock, either exclusive or shared, a fetch and thaw of the data is
performed.  No additional fetch/thaw operations are performed until
you release the lock and access the bound variable again.  During the
time that the lock is kept, all accesses are perfomed on the copy in
program memory.  If other processes do not honor the lock, and update
the shared memory region unfairly, the process with the lock will not be in
sync.  In other words, IPC::Shareable does not enforce the lock
for you.  

A similar optimization is done if you obtain an exclusive lock.
Updates to the shared memory region will be postponed until you
release the lock (or downgrade to a shared lock).

Use of locking can significantly improve performance for operations
such as iterating over an array, retrieving a list from a slice or 
doing a slice assignment.

=head1 REFERENCES

When a reference to a non-tied scalar, hash, or array is assigned to a
tie()d variable, IPC::Shareable will attempt to tie() the thingy being
referenced[4].  This allows disparate processes to see changes to not
only the top-level variable, but also changes to nested data.  This
feature is intended to be transparent to the application, but there
are some caveats to be aware of.

First of all, IPC::Shareable does not (yet) guarantee that the ids
shared memory segments allocated automagically are unique.  The more
automagical tie()ing that happens, the greater the chance of a
collision.

Secondly, since a new shared memory segment is created for each thingy
being referenced, the liberal use of references could cause the system
to approach its limit for the total number of shared memory segments
allowed.

=head1 OBJECTS

IPC::Shareable implements tie()ing objects to shared memory too.
Since an object is just a reference, the same principles (and caveats)
apply to tie()ing objects as other reference types.

=head1 DESTRUCTION

perl(1) will destroy the object underlying a tied variable when then
tied variable goes out of scope.  Unfortunately for IPC::Shareable,
this may not be desirable: other processes may still need a handle on
the relevant shared memory segment.  IPC::Shareable therefore provides
an interface to allow the application to control the timing of removal
of shared memory segments.  The interface consists of three methods -
remove(), clean_up(), and clean_up_all() - and the B<destroy> option
to tie().

=over 4

=item B<destroy option>

As described in L</OPTIONS>, specifying the B<destroy> option when
tie()ing a variable coerces IPC::Shareable to remove the underlying
shared memory segment when the process calling tie() exits gracefully.
Note that any related shared memory segments created automagically by
the use of references will also be removed.

=item B<remove()>

 (tied $var)->remove;

Calling remove() on the object underlying a tie()d variable removes
the associated shared memory segment.  The segment is removed
irrespective of whether it has the B<destroy> option set or not and
irrespective of whether the calling process created the segment.

=item B<clean_up()>

 IPC::Shareable->clean_up;

This is a class method that provokes IPC::Shareable to remove all
shared memory segments created by the process.  Segments not created
by the calling process are not removed.

=item B<clean_up_all()>

 IPC::Shareable->clean_up_all;

This is a class method that provokes IPC::Shareable to remove all
shared memory segments encountered by the process.  Segments are
removed even if they were not created by the calling process.

=back

=head1 EXAMPLES

In a file called B<server>:

 #!/usr/bin/perl -w
 use strict;
 use IPC::Shareable;
 my $glue = 'data';
 my %options = (
     create    => 'yes',
     exclusive => 0,
     mode      => 0644,
     destroy   => 'yes',
 );
 my %colours;
 tie %colours, 'IPC::Shareable', $glue, { %options } or
     die "server: tie failed\n";
 %colours = (
     red => [
         'fire truck',
         'leaves in the fall',
     ],
     blue => [
         'sky',
         'police cars',
     ],
 );
 ((print "server: there are 2 colours\n"), sleep 5)
     while scalar keys %colours == 2;
 print "server: here are all my colours:\n";
 foreach my $c (keys %colours) {
     print "server: these are $c: ",
         join(', ', @{$colours{$c}}), "\n";
 }
 exit;

In a file called B<client>

 #!/usr/bin/perl -w
 use strict;
 use IPC::Shareable;
 my $glue = 'data';
 my %options = (
     create    => 0,
     exclusive => 0,
     mode      => 0644,
     destroy   => 0,
     );
 my %colours;
 tie %colours, 'IPC::Shareable', $glue, { %options } or
     die "client: tie failed\n";
 foreach my $c (keys %colours) {
     print "client: these are $c: ",
         join(', ', @{$colours{$c}}), "\n";
 }
 delete $colours{'red'};
 exit;

And here is the output (the sleep commands in the command line prevent
the output from being interrupted by shell prompts):

 bash$ ( ./server & ) ; sleep 10 ; ./client ; sleep 10
 server: there are 2 colours
 server: there are 2 colours
 server: there are 2 colours
 client: these are blue: sky, police cars
 client: these are red: fire truck, leaves in the fall
 server: here are all my colours:
 server: these are blue: sky, police cars

=head1 RETURN VALUES

Calls to tie() that try to implement IPC::Shareable will return true
if successful, I<undef> otherwise.  The value returned is an instance
of the IPC::Shareable class.

=head1 AUTHOR

Benjamin Sugars <bsugars@canoe.ca>

=head1 NOTES

=head2 Footnotes from the above sections

=over 4

=item 1

If GLUE is longer than 4 characters, only the 4 most significant
characters are used.  These characters are turned into integers by
unpack()ing them.  If GLUE is less than 4 characters, it is space
padded.

=item 2

IPC::Shareable provides no pre-set limits, but the system does.
Namely, there are limits on the number of shared memory segments that
can be allocated and the total amount of memory usable by shared
memory.

=item 3

If the process has been smoked by an untrapped signal, the binding
will remain in shared memory.  If you're cautious, you might try

 $SIG{INT} = \&catch_int;
 sub catch_int {
     die;
 }
 ...
 tie $variable, IPC::Shareable, 'data', { 'destroy' => 'Yes!' };

which will at least clean up after your user hits CTRL-C because
IPC::Shareable's END method will be called.  Or, maybe you'd like to
leave the binding in shared memory, so subsequent process can recover
the data...

=item 4

This behaviour is markedly different from previous versions of
IPC::Shareable.  Older versions would sometimes tie() referenced
thingies, and sometimes not.  The new approach is more reliable (I
think) and predictable (certainly) but uses more shared memory
segments.

=back

=head2 General Notes

=over 4

=item o

When using shlock() to lock a variable, be careful to guard against
signals.  Under normal circumstances, IPC::Shareable's END method
unlocks any locked variables when the process exits.  However, if an
untrapped signal is received while a process holds an exclusive lock,
DESTROY will not be called and the lock may be maintained even though
the process has exited.  If this scares you, you might be better off
implementing your own locking methods.  

One advantage of using C<flock> on some known file instead of the
locking implemented with semaphores in IPC::Shareable is that when a
process dies, it automatically releases any locks.  This only happens
with IPC::Shareable if the process dies gracefully.  The alternative
is to attempt to account for every possible calamitous ending for your
process (robust signal handling in Perl is a source of much debate,
though it usually works just fine) or to become familiar with your
system's tools for removing shared memory and semaphores.  This
concern should be balanced against the significant performance
improvements you can gain for larger data structures by using the
locking mechanism implemented in IPC::Shareable.

=item o

There is a program called ipcs(1/8) (and ipcrm(1/8)) that is
available on at least Solaris and Linux that might be useful for
cleaning moribund shared memory segments or semaphore sets produced
by bugs in either IPC::Shareable or applications using it.

=item o

This version of IPC::Shareable does not understand the format of
shared memory segments created by versions prior to 0.60.  If you try
to tie to such segments, you will get an error.  The only work around
is to clear the shared memory segments and start with a fresh set.

=item o

Iterating over a hash causes a special optimization if you have not
obtained a lock (it is better to obtain a read (or write) lock before
iterating over a hash tied to Shareable, but we attempt this
optimization if you do not).  The fetch/thaw operation is performed
when the first key is accessed.  Subsequent key and and value
accesses are done without accessing shared memory.  Doing an
assignment to the hash or fetching another value between key
accesses causes the hash to be replaced from shared memory.  The
state of the iterator in this case is not defined by the Perl
documentation.  Caveat Emptor.

=back

=head1 CREDITS

Thanks to all those with comments or bug fixes, especially

 Maurice Aubrey      <maurice@hevanet.com>
 Stephane Bortzmeyer <bortzmeyer@pasteur.fr>
 Doug MacEachern     <dougm@telebusiness.co.nz>
 Robert Emmery       <roberte@netscape.com>
 Mohammed J. Kabir   <kabir@intevo.com>
 Terry Ewing         <terry@intevo.com>
 Tim Fries           <timf@dicecorp.com>
 Joe Thomas          <jthomas@women.com>
 Paul Makepeace      <Paul.Makepeace@realprogrammers.com>
 Raphael Manfredi    <Raphael_Manfredi@pobox.com>
 Lee Lindley         <Lee.Lindley@bigfoot.com>
 Dave Rolsky         <autarch@urth.org>

=head1 BUGS

Certainly; this is beta software. When you discover an anomaly, send
an email to me at bsugars@canoe.ca.

=head1 SEE ALSO

perl(1), perltie(1), Storable(3), shmget(2), ipcs(1), ipcrm(1)
and other SysV IPC man pages.

=cut