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

sizeof(struct sembuf)

Thread Next
From:
Nicholas Clark
Date:
July 1, 2001 07:52
Subject:
sizeof(struct sembuf)
Message ID:
20010701155158.S59620@plum.flirble.org
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!*",@_);
    semop($$self,$data);
}

which finds its way do doio.c which has this

I32
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) {
	SETERRNO(EINVAL,LIB$_INVARG);
	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);
    semop($$self,$data);
}

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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About