Call for fopen to sys_open?

I am trying to find an example of how to do the system call in FreeBSD but when I go to fopen in /libc/stdio fopen only leads to a function that's called _open in which it has no documentation, is pretty ambiguous and can't seem to find it anywhere.
 
You can not find it because _open is really not a function but a complex macro that generates assembler and will depend on architecture. The macro itself is constructed in makefiles and base assembler code for it can be found in /src/lib/libc/amd64/SYS.h (replace 'amd64' with architecture of interest). Happy hacking.
 
Thanks for the reply, I had already looked into that before and there didn't seem to be a whole lot of information in there, it seems there is a macro that defines KERNCALL and some basic system call which interrupts into the kernel. I guess I am trying to figure out how exactly you transfer return values, I see how it's done in the kernel but how do you transfer them ?

Code:
push SYS_xxx
int  80h

// eax now holds the return of the kernel call
// where did td->td_retval[0] go ?

Is it also bad to use the actual return value for something other then errors ? I think I've seen gettid() used this way ?

Code:
pid_t pid = syscall(SYS_gettid); // I'd assume this doesn't use td_retval[0] (can't find source of it in kernel)
 
exiled said:
Thanks for the reply, I had already looked into that before and there didn't seem to be a whole lot of information in there, it seems there is a macro that defines KERNCALL and some basic system call which interrupts into the kernel.

RSYSCALL() from SYS.h is used to generate all basic/portable syscalls.

In http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libc/sys/Makefile.inc?rev=1.162

Look up ${SASM} target. It basically generates assembly files (*.S) for every syscall.

Most of syscalls end up like this on amd64/i386 (including open):

Code:
#include "compat.h"
#include "SYS.h"

RSYSCALL(NAME)
  .section .note.GNU-stack, "", %%progbits

Basically RSYSCALL/KERNCALL macros hold all the information you need to see how return values are handled on specific platforms.
 
Back
Top