Solved sysctlbyname from driver module

alemoppo

New Member

Reaction score: 1
Messages: 19

Hello,

I'm trying to read "hw.ncpu" from a kernel module, but I can't find the header to use the sysctlbyname(3) function.

With:
C:
#include <sys/types.h>
#include <sys/sysctl.h>

/* ... */

int ncpu, result;
size_t len;

len = sizeof(ncpu);
result = sysctlbyname("hw.ncpu", &ncpu, &len , 0, 0);

I get:
Code:
error: implicit declar
         result = sysctlbyname("hw.ncpu", &ncpu, &len , 0, 0);
                  ^
1 error generated.
*** Error code 1

Stop.

How can I read this value?

Thank you,
Best Regards.
 

covacat

Well-Known Member

Reaction score: 171
Messages: 367

use extern int mb_ncpus

Code:
root@ns:/extra/fs/home/me # sysctl hw.ncpu
hw.ncpu: 24
root@ns:/extra/fs/home/me # nm -D /boot/kernel/kernel |grep mp_ncpus
ffffffff81eba0f8 B mp_ncpus
root@ns:/extra/fs/home/me # dd if=/dev/mem count=4 bs=1 iseek=0x1eba0f8|hexdump -Cv
4+0 records in
4+0 records out
4 bytes transferred in 0.000038 secs (104638 bytes/sec)
00000000  18 00 00 00                                       |....|
00000004
0x18 is 24
 
OP
alemoppo

alemoppo

New Member

Reaction score: 1
Messages: 19

Thank you!

Meanwhile, I continued to search in the FreeBSD source code, and I resolved with:

C:
    int cpu_count, err;
    size_t len;
    len = sizeof(cpu_count);
    err = kernel_sysctlbyname(curthread, "hw.ncpu", &cpu_count, &len, NULL,
            0, NULL, 0);

But maybe read the "mb_ncpus" variable is faster, but i can't see how include it.

Thank you.
 
OP
alemoppo

alemoppo

New Member

Reaction score: 1
Messages: 19

Yeah, i tried also both things (include + extern), but with "extern" i can't "kldload"; i get:
kldload: an error occurred while loading module ./test_ram.ko. Please check dmesg(8) for more details.
dmesg:
link_elf_obj: symbol mb_ncpus undefined
linker_load_file: ./myModule.ko - unsupported file type

Anyway i solved my problem with "kernel_sysctlbyname". Thanks.
 

covacat

Well-Known Member

Reaction score: 171
Messages: 367

weird. lots of modules seem to use it
Code:
$ nm /boot/kernel/netgraph.ko |grep mp_ncpus
                 U mp_ncpus
probably all that include sys/smp.h
 
Top