[plt-scheme] FFI + errno

From: Steve Huffman (steve.huffman at gmail.com)
Date: Wed Dec 9 13:30:33 EST 2009

> I almost suggested wrapping the call to the function that sets `errno'
> and the access of `errno' with `scheme_start_atomic()' and
> `scheme_end_atomic()' (which you can access them with the FFI). But
> that's not good enough, because the runtime system occasionally uses
> `errno'-setting function internally, even when not switching Scheme
> threads.

The PLT runtime can preempt a FFI function call?



On Wed, Dec 9, 2009 at 10:07 AM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> Yes, that's true.
>
> I almost suggested wrapping the call to the function that sets `errno'
> and the access of `errno' with `scheme_start_atomic()' and
> `scheme_end_atomic()' (which you can access them with the FFI). But
> that's not good enough, because the runtime system occasionally uses
> `errno'-setting function internally, even when not switching Scheme
> threads.
>
> If the `scheme/tcp', `scheme/udp', or other bindings don't suffice,
> then a wrapper function that captures and stores `errno' looks like the
> only option right now.
>
> I'll think more about how to solve this in the FFI, though. Most
> problems like this can be solved with atomic mode, but `errno' is a
> special case. Probably the FFI should provide more direct support for
> getting the right `errno' value.
>
>
> At Mon, 07 Dec 2009 15:48:55 -0800, Ben Goetter wrote:
>> If the PLT runtime itself calls any C runtime function between the time
>> that your call to socket() sets errno and the time that you can access
>> your thunk to errno, then errno will no longer contain the value set by
>> socket().
>>
>> Hence you cannot use errno usefully through the FFI.  Mechanically, you
>> could access the contents of that variable through make-c-parameter or
>> the like.
>>
>> I would favor the existing scheme/tcp or scheme/udp bindings, if at all
>> possible.
>>
>>
>> Steve Huffman wrote:
>> > Hi All,
>> >
>> > I'm using OS X and trying to access the raw socket api using PLT's
>> > FFI. For example:
>> >
>> > (define _socket
>> >   (get-ffi-obj "socket" #f (_fun _int _int _int -> _int)))
>> >
>> > (define _connect
>> >   (get-ffi-obj "connect" #f (_fun _int _sockaddr_in-pointer _int -> _int)))
>> >
>> > Things are mostly working, except I'm having trouble accessing the
>> > global errno value to see error codes. I've tried wrapping the
>> > __error() function, but am not receive relevant error codes.
>> >
>> > I gather from searching this list that PLT itself might be using errno
>> > for its own purposes before I get a chance to see it. Is that
>> > accurate?
>> >
>> > Should I write my own wrapper functions in C around the socket
>> > functions and capture/store errno someplace else?
>> >
>> > Thanks,
>> >
>> > Steve
>> > _________________________________________________
>> >   For list-related administrative tasks:
>> >   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>> >
>>
>> _________________________________________________
>>   For list-related administrative tasks:
>>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.