develooper Front page | perl.perl5.porters | Postings from July 2001

sizeof(struct sembuf)

Thread Next
Nicholas Clark
July 1, 2001 07:52
sizeof(struct sembuf)
Message ID:
I'm not sure why this hasn't failed before, but I'm now seeing
ext/IPC/SysV/t/sem.t fail test 8 on ARM linux.
[And it would fail on BSD on ARM I think, assuming the ABI is the same]

Test 8 is the innocuous looking

$sem->op(2,-1,IPC_NOWAIT) || print "not ";
print "ok 8\n";

which calls

sub op {
    @_ >= 4 || croak '$sem->op( OPLIST )';
    my $self = shift;
    croak 'Bad arg count' if @_ % 3;
    my $data = pack("s!*",@_);

which finds its way do doio.c which has this

Perl_do_semop(pTHX_ SV **mark, SV **sp)
#ifdef HAS_SEM
    SV *opstr;
    char *opbuf;
    I32 id;
    STRLEN opsize;

    id = SvIVx(*++mark);
    opstr = *++mark;
    opbuf = SvPV(opstr, opsize);
    if (opsize < sizeof(struct sembuf)
	|| (opsize % sizeof(struct sembuf)) != 0) {
	return -1;

=item semop KEY,OPSTRING

Calls the System V IPC function semop to perform semaphore operations
such as signaling and waiting.  OPSTRING must be a packed array of
semop structures.  Each semop structure can be generated with
C<pack("sss", $semnum, $semop, $semflag)>.  The number of semaphore
operations is implied by the length of OPSTRING.  Returns true if
successful, or false if there is an error.  As an example, the
following code waits on semaphore $semnum of semaphore id $semid:

so it seems that it's documented that perl expects 3 shorts in this order.

Bletch. Why is perl making structure size assumptions?

# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/sem.h>
# include <stdio.h>

int main () {
  printf ("sizeof(short)=         %d\n", sizeof(short));
  printf ("sizeof(struct sembuf)= %d\n", sizeof(struct sembuf));
  return 0;

gives me:

sizeof(short)=         2
sizeof(struct sembuf)= 8

and if I tweak sub op to read

sub op {
    @_ >= 4 || croak '$sem->op( OPLIST )';
    my $self = shift;
    croak 'Bad arg count' if @_ % 3;
    my $data = pack("s!*",@_, 0);

then the test passes, but this isn't a solution; core perl is buggy.

What's the best strategy to detect this (configure or run time?)
so that Perl_do_semop can repack structures if needed.
Are these structures supposed to be read/write?

Nicholas Clark

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About