This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
No more ext/*/*.t, move them all to ext/*/t.
[perl5.git] / ext / IPC / SysV / t / ipcsysv.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5
6     @INC = '../lib';
7
8     require Config; import Config;
9
10     my $reason;
11
12     if ($Config{'extensions'} !~ /\bIPC\/SysV\b/) {
13       $reason = 'IPC::SysV was not built';
14     } elsif ($Config{'d_sem'} ne 'define') {
15       $reason = '$Config{d_sem} undefined';
16     } elsif ($Config{'d_msg'} ne 'define') {
17       $reason = '$Config{d_msg} undefined';
18     }
19     if ($reason) {
20         print "1..0 # Skip: $reason\n";
21         exit 0;
22     }
23 }
24
25 # These constants are common to all tests.
26 # Later the sem* tests will import more for themselves.
27
28 use IPC::SysV qw(IPC_PRIVATE IPC_NOWAIT IPC_STAT IPC_RMID S_IRWXU);
29 use strict;
30
31 print "1..16\n";
32
33 my $msg;
34 my $sem;
35
36 $SIG{__DIE__} = 'cleanup'; # will cleanup $msg and $sem if needed
37
38 # FreeBSD is known to throw this if there's no SysV IPC in the kernel.
39 $SIG{SYS} = sub {
40     print STDERR <<EOM;
41 SIGSYS caught.
42 It may be that your kernel does not have SysV IPC configured.
43
44 EOM
45     if ($^O eq 'freebsd') {
46         print STDERR <<EOM;
47 You must have following options in your kernel:
48
49 options         SYSVSHM
50 options         SYSVSEM
51 options         SYSVMSG
52
53 See config(8).
54 EOM
55     }
56     exit(1);
57 };
58
59 my $perm = S_IRWXU;
60
61 if ($Config{'d_msgget'} eq 'define' &&
62     $Config{'d_msgctl'} eq 'define' &&
63     $Config{'d_msgsnd'} eq 'define' &&
64     $Config{'d_msgrcv'} eq 'define') {
65
66     $msg = msgget(IPC_PRIVATE, $perm);
67     # Very first time called after machine is booted value may be 0 
68     die "msgget failed: $!\n" unless defined($msg) && $msg >= 0;
69
70     print "ok 1\n";
71
72     #Putting a message on the queue
73     my $msgtype = 1;
74     my $msgtext = "hello";
75
76     my $test2bad;
77     my $test5bad;
78     my $test6bad;
79
80     unless (msgsnd($msg,pack("L! a*",$msgtype,$msgtext),IPC_NOWAIT)) {
81         print "not ";
82         $test2bad = 1;
83     }
84     print "ok 2\n";
85     if ($test2bad) {
86         print <<EOM;
87 #
88 # The failure of the subtest #2 may indicate that the message queue
89 # resource limits either of the system or of the testing account
90 # have been reached.  Error message "Operating would block" is
91 # usually indicative of this situation.  The error message was now:
92 # "$!"
93 #
94 # You can check the message queues with the 'ipcs' command and
95 # you can remove unneeded queues with the 'ipcrm -q id' command.
96 # You may also consider configuring your system or account
97 # to have more message queue resources.
98 #
99 # Because of the subtest #2 failing also the substests #5 and #6 will
100 # very probably also fail.
101 #
102 EOM
103     }
104
105     my $data;
106     msgctl($msg,IPC_STAT,$data) or print "not ";
107     print "ok 3\n";
108
109     print "not " unless length($data);
110     print "ok 4\n";
111
112     my $msgbuf;
113     unless (msgrcv($msg,$msgbuf,256,0,IPC_NOWAIT)) {
114         print "not ";
115         $test5bad = 1;
116     }
117     print "ok 5\n";
118     if ($test5bad && $test2bad) {
119         print <<EOM;
120 #
121 # This failure was to be expected because the subtest #2 failed.
122 #
123 EOM
124     }
125
126     my($rmsgtype,$rmsgtext);
127     ($rmsgtype,$rmsgtext) = unpack("L! a*",$msgbuf);
128     unless ($rmsgtype == $msgtype && $rmsgtext eq $msgtext) {
129         print "not ";
130         $test6bad = 1;
131     }
132     print "ok 6\n";
133     if ($test6bad && $test2bad) {
134         print <<EOM;
135 #
136 # This failure was to be expected because the subtest #2 failed.
137 #
138 EOM
139      }
140 } else {
141     for (1..6) {
142         print "ok $_\n"; # fake it
143     }
144 }
145
146 if($Config{'d_semget'} eq 'define' &&
147    $Config{'d_semctl'} eq 'define') {
148
149     if ($Config{'d_semctl_semid_ds'} eq 'define' ||
150         $Config{'d_semctl_semun'}    eq 'define') {
151
152         use IPC::SysV qw(IPC_CREAT GETALL SETALL);
153
154         $sem = semget(IPC_PRIVATE, 10, $perm | IPC_CREAT);
155         # Very first time called after machine is booted value may be 0 
156         die "semget: $!\n" unless defined($sem) && $sem >= 0;
157
158         print "ok 7\n";
159
160         my $data;
161         semctl($sem,0,IPC_STAT,$data) or print "not ";
162         print "ok 8\n";
163         
164         print "not " unless length($data);
165         print "ok 9\n";
166
167         my $nsem = 10;
168
169         semctl($sem,0,SETALL,pack("s!*",(0) x $nsem)) or print "not ";
170         print "ok 10\n";
171
172         $data = "";
173         semctl($sem,0,GETALL,$data) or print "not ";
174         print "ok 11\n";
175
176         print "not " unless length($data) == length(pack("s!*",(0) x $nsem));
177         print "ok 12\n";
178
179         my @data = unpack("s!*",$data);
180
181         my $adata = "0" x $nsem;
182
183         print "not " unless @data == $nsem and join("",@data) eq $adata;
184         print "ok 13\n";
185
186         my $poke = 2;
187
188         $data[$poke] = 1;
189         semctl($sem,0,SETALL,pack("s!*",@data)) or print "not ";
190         print "ok 14\n";
191     
192         $data = "";
193         semctl($sem,0,GETALL,$data) or print "not ";
194         print "ok 15\n";
195
196         @data = unpack("s!*",$data);
197
198         my $bdata = "0" x $poke . "1" . "0" x ($nsem-$poke-1);
199
200         print "not " unless join("",@data) eq $bdata;
201         print "ok 16\n";
202     } else {
203         for (7..16) {
204             print "ok $_ # skipped, no semctl possible\n";
205         }
206     }
207 } else {
208     for (7..16) {
209         print "ok $_\n"; # fake it
210     }
211 }
212
213 sub cleanup {
214     msgctl($msg,IPC_RMID,0)       if defined $msg;
215     semctl($sem,0,IPC_RMID,undef) if defined $sem;
216 }
217
218 cleanup;