Codebase is FreeBSD11.1
Code:
****
* Attempt on implementing own sycstl
****/
#include<sys/cdefs.h>
#include<sys/param.h>
#include<sys/kernel.h>
#include<sys/systm.h>
#include<sys/types.h>`
#include<sys/sysctl.h>
int thats_my_stat_var=1; /*it won't work also when declared as static int*/
int thats_my_stat_var2=2;
/*I tried with this and without this, it's unrolls to an extern anyway*/
SYSCTL_DECL(_user);
SYSCTL_INT(_user,OID_AUTO,
thats_my_stat_var,CTLFLAG_RW|CTLFLAG_ANYBODY,
&thats_my_stat_var,1,"Static one");
SYSCTL_INT(_user,OID_AUTO,
thats_my_stat_var2,CTLFLAG_RW|CTLFLAG_ANYBODY,
&thats_my_stat_var2,2,"Static two");
I added it as a file kern/subr_examplectl.c to file list sys/conf/files, it is placed like this - 3 lines before and 3 lines after included, to give you an outlook on where I put that Code:
kern/vfs_subr.c standard
kern/vfs_syscalls.c standard
kern/vfs_vnops.c standard
kern/subr_examplectl.c standard
#
It does not show new variables in the list but I can display it when I specify the name of sysctl variable - but it has no value, and why I'm trying to set a value there is just an error
$sysctl user.thats_my_stat_var #yes nothing is displayed as result of this command
Lets check if it there is possible to get description.
$sysctl -d user.thats_my_stat_var #this one prints out description
user.thats_my_stat_var: Static one
Ok this actually works.
Trying to set a value.
$ sysctl user.thats_my_stat_var2=11
sysctl: user.thats_my_stat_var2=11: Operation not permitted
Nope, this is not working.
Secure level seems to not be the issue here:
$sysctl kern.securelevel
kern.securelevel: -1
What is missing here? I cannot grasp what's the matter. Is any export macro missing? Or I didn't import some specific header or maybe, or I have to add that file somewhere else too?
Seems like something is not stated openly enough in sources/manual/books and other manuals or was changed quiet recently. All sources I have access to are silent about any additional requirements for sysctls to be accesible. No root and no normal user is able to load their values or set/change them. But maybe I'm missing something. No idea. I may be just not able so far to connect this to any specific reason. I need at least a hint or open out pointing to the fact that I'm possibly dumb if solution is pretty simple.