[Gambas-user] Get last errno from external function call

Matheus Castello matheus at castello.eng.br
Wed Oct 5 15:24:52 CEST 2022



On 10/5/2022 7:50 AM, vuott--- via User wrote:
> Hello,
> sorry, I can't see in libgpiodheader file, indicated by you, where and 
> how the functions, which return a data of type Structure, set the error 
> code to the `errno`.
> Am I missing something?
> 

The line that I'm referring is the comment about it:

"General note on error handling: all routines exported by libgpiod  set 
errno to one of the error values defined in errno.h upon failure. The 
way of notifying the caller that an error occurred varies between 
functions, but in general a function that returns an int, returns -1 on 
error, while a function returning a pointer bails out on error condition 
by returning a NULL pointer."

One example of implementation 
https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/lib/core.c#n215

BR,
Matheus Castello

> 
> 5 ott 2022, 05:46 da matheus at castello.eng.br:
> 
>     For example some calls from libgpiod return structs, if the return
>     is a null pointer then you have an error. For this cases libgpiod
>     set the error code to the `errno` and you need to read it as soon
>     the function returns the null pointer.
> 
>     Reference:
>     https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/include/gpiod.h#n28
> 
> 
>     On 10/5/2022 12:25 AM, Jussi Lahtinen wrote:
> 
>         You mean an error occurring inside the library? I don't think
>         there is any other way, than the library itself reporting the
>         error code.
>         I don't see why using __errno_location inside the library would
>         fail. Are you calling it from the Gambas code?
> 
> 
>     Yeah, I add an `External` declaration to it to try to get the errno,
>     but as I said "surely the runtime (I mean the Gambas3 runtime) must
>     be making other calls that are overwriting the errno" and I'm always
>     getting `errno = 0` that means success.
> 
>     .NET for example have an annotation to store the errno from an
>     external function the `SetLastError`:
>     https://github.com/dotnet/iot/blob/main/src/System.Device.Gpio/Interop/Unix/libgpiod/Interop.libgpiod.cs#L49
>     I was looking for something similar in Gambas3.
> 
>     BR,
>     Matheus Castello
> 
>         If this is about a missing error message, then are you reading
>         it from stderr?
>         You need to give more details and/or some code to see.
> 
>         Jussi
> 
> 
> 
> 
>         On Wed, Oct 5, 2022 at 5:54 AM Matheus Castello
>         <matheus at castello.eng.br <mailto:matheus at castello.eng.br>> wrote:
> 
>         Hi guys,
> 
>         I'm new to Gambas3, I'm trying to use it for embedded devices
>         and I'm
>         doing some external function calls. There is some way in Gambas3 to
>         get,
>         or Gambas runtime stores, the last errno from a external
>         function call?
> 
>         I tried to get it directly from libc `__errno_location` but
>         surely the
>         runtime must be making other calls that are overwriting the errno.
> 
>         Thanks in advance,
>         Matheus Castello
> 
>         ----[ http://gambaswiki.org/wiki/doc/netiquette
>         <http://gambaswiki.org/wiki/doc/netiquette> ]----
> 
> 
> 
>         ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
> 
> 
>     ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
> 
> 
> 
> 
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----


More information about the User mailing list