<div dir="ltr">I've dug more into the code and came up with something a bit nicer than what I originally described. Feel free to look at the <a href="https://github.com/ffilozov/cffi/commit/e35e949891a0fe5faeca2a0a44050edff99b9f3c">changes</a> (comments and suggestions are always welcome).<div>
<br></div><div>My basic assumption is that the underlying CFFI-SYS functions like %foreign-funcall and defcfun-helper-forms would return two values whenever defcfun or foreign-funcall is configured to return errno. The first value is the foreign call return value, and the second is errno. errno is then captured to be passed to the user's structure, and the foreign call return value is propagated further.</div>
<div><br></div><div>Here's a sample of how it looks:</div><div><br></div><div><div>CL-USER> (defparameter errno-object (cffi:make-errno :value 0))</div><div>ERRNO-OBJECT</div><div><br></div><div>CL-USER> (cffi:defcfun ("socket" :errno t) :int</div>
<div> (a :int)</div><div> (b :int)</div><div> (c :int))</div><div>Warning: :call-direct ignored because :error-value specified.</div><div>SOCKET</div><div><br></div><div>CL-USER> (socket errno-object -1 -1 -1)</div>
<div>-1</div><div><br></div><div>CL-USER> errno-object</div><div>#S(CFFI::ERRNO :VALUE 22)</div></div><div><br></div><div>More:</div><div><br></div><div><div>CL-USER> (defparameter errno-object (cffi:make-errno :value 0))</div>
<div>ERRNO-OBJECT</div><div><br></div><div>CL-USER> (cffi:foreign-funcall ("getenv" :errno errno-object) :string "SHELL" :string+ptr)</div><div>("/bin/bash" 140735646882874)</div></div><div>
<br></div><div><div>CL-USER> errno-object</div><div>#S(CFFI::ERRNO :VALUE 0)</div></div><div><br></div><div>Last one:</div><div><br></div><div><div>CL-USER> (defparameter errno-object (cffi:make-errno :value 0))</div>
<div>ERRNO-OBJECT</div><div><br></div><div>CL-USER> (cffi:defcfun ("printf" :errno t) :int</div><div> (control :string)</div><div> &rest)</div><div>PRINTF</div><div><br></div>
<div>CL-USER> (printf errno-object "%s %s" :string "hi" :string "bob")</div><div>6</div><div><br></div><div>CL-USER> errno-object</div><div>#S(CFFI::ERRNO :VALUE 0)</div></div><div><br>
</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Aug 8, 2013 at 11:37 PM, Luís Oliveira <span dir="ltr"><<a href="mailto:luismbo@gmail.com" target="_blank">luismbo@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">Felix Filozov <<a href="mailto:ffilozov@gmail.com">ffilozov@gmail.com</a>> writes:<br>
<br>
> Looking into Allegro CL SMP's manual, there's a macro called<br>
> with-delayed-interrupts, which should prevent interrupts and GC from<br>
> running. However, the wrong errno was still returned when I used this<br>
> macro. I will ask Franz if such a macro exists.<br>
<br>
</div>What's your test case like?<br>
<div class="im"><br>
<br>
> I started exploring your second suggestion. I created an errno<br>
> defstruct (this is what the user passes), in the CFFI package, and I<br>
> tried passing that structure to the CFFI-SYS layer to be modified, but<br>
> then I realized that CFFI-SYS doesn't know anything about the CFFI<br>
> package so it can't modify the structure. So I'm passing a function to<br>
> CFFI-SYS, which gets called with the returned errno value as an<br>
> argument. I don't know if I like this callback mechanism.<br>
<br>
</div>Seems a bit heavy handed, yeah. We can split cffi-sys in two files: a<br>
shared file (with the package define and perhaps that structure<br>
definition) and an implementation-specific file.<br>
<div class="im"><br>
<br>
> Also, while reading through the code I noticed that there's some edge<br>
> cases that would need to be handled. For example %defcfun may make a<br>
> call to foreign-funcall if the argument types and the returns types<br>
> are considered call-by-value. What does that mean?<br>
<br>
</div>We've recently added support for passing structures by value. When that<br>
functionality is needed, we use libffi for invoking the foreign<br>
function. I have no idea how we can reliably get errno in that case.<br>
<div class="im"><br>
<br>
> I also don't quite understand what entry-vec is, if that's something<br>
> that can be ignored for now.<br>
<br>
</div>I don't remember the details, but doc/allegro-internals.txt has some<br>
info about it.<br>
<br>
Cheers,<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Luís Oliveira<br>
<a href="http://kerno.org/~luis" target="_blank">http://kerno.org/~luis</a><br>
<br>
<br>
</font></span></blockquote></div><br></div>