Implementation for sysctl(...) call

Noob here. I am by no means a C developer but from what I can see it is pretty straightforward and close to metal.

I need help with /sbin/sysctl.c:
I am looking through it and at some point there is a call to a function sysctl(3). I found the appropriate header file (/sys/sys/sysctl.h) which forward declares int sysctl(const int *, u_int, void *, size_t *, const void *, size_t);, but there's no implementation. I can't seem to figure it out

Cheers if you can help me understand this. :)
 
The implementation of the sysctl(3) syscall is in sys/kern/kern_sysctl.c, the entry point is sys___sysctl(struct thread *td, struct sysctl_args *uap), then userland_sysctl() -> sysctl_root() -> finally sysctl_root_handler_locked() calls the handler of the node error = oid->oid_handler(oid, arg1, arg2, req).
Obviously this is an extreme simplification ;).

To note, the sysctl(8) utility calls sysctl(3) to get the value-size and value of an object, its properties: name, description, type and format, finally to explore the sysctl MIB-Tree by getting the next object.
 
It's a system call, you need to look in the kernel source code.

Finding system calls in user code is quite tricky, as the transition from user mode to kernel mode is not done with a normal function call that looks like C. While functionally it is quite similar to a function call, the code can be really hard to decipher, and is full of #define's and arrays of pointers. I saw some discussion of it recently in the "Design and Implementation ... of FreeBSD" book recently, but can't remember the details. Start by reading the man page for syscall, and looking at the implementation of syscall. And search the web for how system calls in general are implemented in FreeBSD, or get a copy of the daemon book.
 
Kudos all around.

For future reference: /sys/kern/init_sysent.c is a generated file [using makesyscalls.sh and /sys/kern/syscalls.master] that contains prototypes and reference to the implementations of all syscalls. In this case __sysctl(...) is sys___sysctl(...) inside /sys/kern/kern_sysctl.c

I hope I got it right
 
Back
Top