This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Under usethreads the dumped variable is IN_PAD.
[perl5.git] / t / lib / ipc_sysv.t
CommitLineData
3784f770
JH
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5
20822f61 6 @INC = '../lib';
3784f770
JH
7
8 require Config; import Config;
9
45c0de28
GS
10 my $reason;
11
be3174d2
GS
12 if ($Config{'extensions'} !~ /\bIPC\/SysV\b/) {
13 $reason = 'IPC::SysV was not built';
14 } elsif ($Config{'d_sem'} ne 'define') {
45c0de28
GS
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;
3784f770
JH
22 }
23}
24
25# These constants are common to all tests.
26# Later the sem* tests will import more for themselves.
27
41d6edb2 28use IPC::SysV qw(IPC_PRIVATE IPC_NOWAIT IPC_STAT IPC_RMID S_IRWXU);
3784f770
JH
29use strict;
30
31print "1..16\n";
32
33my $msg;
34my $sem;
35
36$SIG{__DIE__} = 'cleanup'; # will cleanup $msg and $sem if needed
37
6087ac44
JH
38# FreeBSD is known to throw this if there's no SysV IPC in the kernel.
39$SIG{SYS} = sub {
40 print STDERR <<EOM;
41SIGSYS caught.
42It may be that your kernel does not have SysV IPC configured.
43
44EOM
45 if ($^O eq 'freebsd') {
46 print STDERR <<EOM;
47You must have following options in your kernel:
48
49options SYSVSHM
50options SYSVSEM
51options SYSVMSG
52
53See config(8).
54EOM
55 }
56 exit(1);
57};
58
41d6edb2 59my $perm = S_IRWXU;
092bebab 60
3784f770
JH
61if ($Config{'d_msgget'} eq 'define' &&
62 $Config{'d_msgctl'} eq 'define' &&
63 $Config{'d_msgsnd'} eq 'define' &&
64 $Config{'d_msgrcv'} eq 'define') {
092bebab
JH
65
66 $msg = msgget(IPC_PRIVATE, $perm);
3784f770
JH
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
8f753cb5
JH
76 my $test2bad;
77 my $test5bad;
78 my $test6bad;
79
e4038a1f 80 unless (msgsnd($msg,pack("L! a*",$msgtype,$msgtext),IPC_NOWAIT)) {
8f753cb5 81 print "not ";
19e194ad 82 $test2bad = 1;
8f753cb5 83 }
3784f770 84 print "ok 2\n";
8f753cb5
JH
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#
102EOM
103 }
3784f770
JH
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;
8f753cb5
JH
113 unless (msgrcv($msg,$msgbuf,256,0,IPC_NOWAIT)) {
114 print "not ";
115 $test5bad = 1;
116 }
3784f770 117 print "ok 5\n";
8f753cb5
JH
118 if ($test5bad && $test2bad) {
119 print <<EOM;
120#
121# This failure was to be expected because the subtest #2 failed.
122#
123EOM
124 }
3784f770 125
19e194ad 126 my($rmsgtype,$rmsgtext);
9b85d4c3
GS
127 ($rmsgtype,$rmsgtext) = unpack("L! a*",$msgbuf);
128 unless ($rmsgtype == $msgtype && $rmsgtext eq $msgtext) {
8f753cb5
JH
129 print "not ";
130 $test6bad = 1;
131 }
3784f770 132 print "ok 6\n";
8f753cb5
JH
133 if ($test6bad && $test2bad) {
134 print <<EOM;
135#
136# This failure was to be expected because the subtest #2 failed.
137#
138EOM
139 }
3784f770
JH
140} else {
141 for (1..6) {
142 print "ok $_\n"; # fake it
143 }
144}
145
146if($Config{'d_semget'} eq 'define' &&
147 $Config{'d_semctl'} eq 'define') {
148
ae1e0c5f
JH
149 if ($Config{'d_semctl_semid_ds'} eq 'define' ||
150 $Config{'d_semctl_semun'} eq 'define') {
3784f770 151
ae1e0c5f 152 use IPC::SysV qw(IPC_CREAT GETALL SETALL);
3784f770 153
ae1e0c5f
JH
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;
3784f770 157
ae1e0c5f 158 print "ok 7\n";
3784f770 159
ae1e0c5f
JH
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";
3784f770 166
ae1e0c5f 167 my $nsem = 10;
3784f770 168
ae1e0c5f
JH
169 semctl($sem,0,SETALL,pack("s!*",(0) x $nsem)) or print "not ";
170 print "ok 10\n";
3784f770 171
ae1e0c5f
JH
172 $data = "";
173 semctl($sem,0,GETALL,$data) or print "not ";
174 print "ok 11\n";
3784f770 175
ae1e0c5f
JH
176 print "not " unless length($data) == length(pack("s!*",(0) x $nsem));
177 print "ok 12\n";
3784f770 178
ae1e0c5f 179 my @data = unpack("s!*",$data);
3784f770 180
ae1e0c5f 181 my $adata = "0" x $nsem;
3784f770 182
ae1e0c5f
JH
183 print "not " unless @data == $nsem and join("",@data) eq $adata;
184 print "ok 13\n";
3784f770 185
ae1e0c5f 186 my $poke = 2;
3784f770 187
ae1e0c5f
JH
188 $data[$poke] = 1;
189 semctl($sem,0,SETALL,pack("s!*",@data)) or print "not ";
190 print "ok 14\n";
3784f770 191
ae1e0c5f
JH
192 $data = "";
193 semctl($sem,0,GETALL,$data) or print "not ";
194 print "ok 15\n";
3784f770 195
ae1e0c5f 196 @data = unpack("s!*",$data);
3784f770 197
ae1e0c5f 198 my $bdata = "0" x $poke . "1" . "0" x ($nsem-$poke-1);
3784f770 199
ae1e0c5f
JH
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 }
3784f770
JH
207} else {
208 for (7..16) {
209 print "ok $_\n"; # fake it
210 }
211}
212
213sub cleanup {
214 msgctl($msg,IPC_RMID,0) if defined $msg;
215 semctl($sem,0,IPC_RMID,undef) if defined $sem;
216}
217
218cleanup;