# Problems adding new kernel variable



## Logan89 (Feb 28, 2013)

I can not find online detailed steps on how to add a system variable, so I tried to improvise and I stuck.

I'll add my detailed steps, for future reference

Step1:
open /sys/sys/sysctl.h and add the following lines

```
#define USER_TEST_VAR     22   /*test var*/
```

and in the define CTL_USER_NAMES section of the same file I add the following line

```
{ "test_var", CTLTYPE_INT }, \
```
(added that coma and backslash just to follow the existent pattern)

Step2:
open /usr/src/sys/kern/kern_mib.c and add the following line:

```
SYSCTL_INT(_user, USER_TEST_VAR, test_var, CTLFLAG_RD, 0, 0, "test Var");
```

Step3:
Recompile the kernel

Step4:
write the following on the command line

```
sysctl -A
```
and in the output I can not find just added variable


I took already existent variable from the "USER" parent node and checked what files contain it and  based on this I tried to do for a new variable. I have a feeling that the index from the Step1 which is 20 has to be included somewhere.

Can anyone point me at my mistake or indicate what step I am missing.

Thank you


----------



## fluca1978 (Feb 28, 2013)

Seems correct to me, but just attached to the CTL_USER structure there is the following comment:


```
/*                                                                                              
 * This is really cheating.  These actually live in the libc, something                         
 * which I'm not quite sure is a good idea anyway, but in order for                             
 * getnext and friends to actually work, we define dummies here.                                
 *                                                                                              
 * XXXRW: These probably should be CTLFLAG_CAPRD.                                               
 */
```

I'm not sure the sys/lib/libc/sysctl.c is responsible for the creation of the sysctl, but it has _sysctl_ function that manipulates all CTL_USER entries.


----------

