summaryrefslogtreecommitdiff
path: root/t/lib/ipc_sysv.t
blob: 1cbde9687a028685d6d308ed973fa8b9659a5493 (plain)
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
#!./perl

BEGIN {
    chdir 't' if -d 't';

    unshift @INC, '../lib';

    require Config; import Config;

    unless ($Config{'d_msg'} eq 'define' &&
	    $Config{'d_sem'} eq 'define') {
	print "1..0\n";
	exit;
    }
}

# These constants are common to all tests.
# Later the sem* tests will import more for themselves.

use IPC::SysV qw(IPC_PRIVATE IPC_NOWAIT IPC_STAT IPC_RMID
		 S_IRWXU S_IRWXG S_IRWXO S_IWGRP S_IROTH S_IWOTH);
use strict;

print "1..16\n";

my $msg;
my $sem;

$SIG{__DIE__} = 'cleanup'; # will cleanup $msg and $sem if needed

# FreeBSD is known to throw this if there's no SysV IPC in the kernel.
$SIG{SYS} = sub {
    print STDERR <<EOM;
SIGSYS caught.
It may be that your kernel does not have SysV IPC configured.

EOM
    if ($^O eq 'freebsd') {
	print STDERR <<EOM;
You must have following options in your kernel:

options         SYSVSHM
options         SYSVSEM
options         SYSVMSG

See config(8).
EOM
    }
    exit(1);
};

my $perm;

$perm = S_IRWXU | S_IRWXG | S_IRWXO | S_IWGRP | S_IROTH | S_IWOTH
    if $^O eq 'vmesa';

$perm = S_IRWXU | S_IRWXG | S_IRWXO unless defined $perm;

if ($Config{'d_msgget'} eq 'define' &&
    $Config{'d_msgctl'} eq 'define' &&
    $Config{'d_msgsnd'} eq 'define' &&
    $Config{'d_msgrcv'} eq 'define') {

    $msg = msgget(IPC_PRIVATE, $perm);
    # Very first time called after machine is booted value may be 0 
    die "msgget failed: $!\n" unless defined($msg) && $msg >= 0;

    print "ok 1\n";

    #Putting a message on the queue
    my $msgtype = 1;
    my $msgtext = "hello";

    msgsnd($msg,pack("L a*",$msgtype,$msgtext),0) or print "not ";
    print "ok 2\n";

    my $data;
    msgctl($msg,IPC_STAT,$data) or print "not ";
    print "ok 3\n";

    print "not " unless length($data);
    print "ok 4\n";

    my $msgbuf;
    msgrcv($msg,$msgbuf,256,0,IPC_NOWAIT) or print "not ";
    print "ok 5\n";

    my($rmsgtype,$rmsgtext) = unpack("L a*",$msgbuf);

    print "not " unless($rmsgtype == $msgtype && $rmsgtext eq $msgtext);
    print "ok 6\n";
} else {
    for (1..6) {
	print "ok $_\n"; # fake it
    }
}

if($Config{'d_semget'} eq 'define' &&
   $Config{'d_semctl'} eq 'define') {

    use IPC::SysV qw(IPC_CREAT GETALL SETALL);

    $sem = semget(IPC_PRIVATE, 10, $perm | IPC_CREAT);
    # Very first time called after machine is booted value may be 0 
    die "semget: $!\n" unless defined($sem) && $sem >= 0;

    print "ok 7\n";

    my $data;
    semctl($sem,0,IPC_STAT,$data) or print "not ";
    print "ok 8\n";

    print "not " unless length($data);
    print "ok 9\n";

    my $template;

    # Find the pack/unpack template capable of handling native C shorts.

    if      ($Config{shortsize} == 2) {
	$template = "s";
    } elsif ($Config{shortsize} == 4) {
	$template = "l";
    } elsif ($Config{shortsize} == 8) {
	# Try quad last because not supported everywhere.
	foreach my $t (qw(i q)) {
	    # We could trap the unsupported quad template with eval
	    # but if we get this far we should have quad support anyway.
	    if (length(pack($t, 0)) == 8) {
		$template = $t;
		last;
	    }
	}
    }

    die "$0: cannot pack native shorts\n" unless defined $template;

    $template .= "*";

    my $nsem = 10;

    semctl($sem,0,SETALL,pack($template,(0) x $nsem)) or print "not ";
    print "ok 10\n";

    $data = "";
    semctl($sem,0,GETALL,$data) or print "not ";
    print "ok 11\n";

    print "not " unless length($data) == length(pack($template,(0) x $nsem));
    print "ok 12\n";

    my @data = unpack($template,$data);

    my $adata = "0" x $nsem;

    print "not " unless @data == $nsem and join("",@data) eq $adata;
    print "ok 13\n";

    my $poke = 2;

    $data[$poke] = 1;
    semctl($sem,0,SETALL,pack($template,@data)) or print "not ";
    print "ok 14\n";
    
    $data = "";
    semctl($sem,0,GETALL,$data) or print "not ";
    print "ok 15\n";

    @data = unpack($template,$data);

    my $bdata = "0" x $poke . "1" . "0" x ($nsem-$poke-1);

    print "not " unless join("",@data) eq $bdata;
    print "ok 16\n";
} else {
    for (7..16) {
	print "ok $_\n"; # fake it
    }
}

sub cleanup {
    msgctl($msg,IPC_RMID,0)       if defined $msg;
    semctl($sem,0,IPC_RMID,undef) if defined $sem;
}

cleanup;