0

How to get 'sysctl vm.max_map_count', or the '/proc/sys/vm/max_map_count' using C++

I do not want to open the /proc/sys file.. Is there a http://man7.org/linux/man-pages/man2/sysctl.2.html call to get the number ?

Adi
  • 475
  • 5
  • 15
  • `I do not want to open the /proc/sys file` why? – SingerOfTheFall Jan 10 '17 at 08:01
  • @SingerOfTheFall I already use sysctl and other libc.so.6 calls without opening /proc files and I want to keep it this way – Adi Jan 10 '17 at 08:10
  • Have you tried the `sysctl` system call? – Simon Kraemer Jan 10 '17 at 08:20
  • @Simon Kraemer - I was able to set new max map count from bash using sysctl, but when I : 'strace -v sysctl -w vm.max_map_count=12345' the sysctl command doesn't make sense to me. – Adi Jan 10 '17 at 08:55
  • So what is your question? That the sysctl command doesn't use the sysctl function? I honestly don't get your problem. Please update your question description so we actually know what you have tried so far and where you encounter a problem. – Simon Kraemer Jan 10 '17 at 09:08
  • @Simon Kraemer - I asked how to get the max map count value using C++ code. using a syscall... but not using 'open /proc/sys/vm/max_map_count'. I want to do that the same way the kernel does. I think my question describes that – Adi Jan 10 '17 at 09:19
  • And why don't you just use the sysctl syscall? – Simon Kraemer Jan 10 '17 at 09:27
  • @Simon Kraemer - that is my question : how to use sysctl syscall to get max map count – Adi Jan 10 '17 at 09:42
  • Without any further knowledge or being able to try it out at the moment: I would initialize the `__sysctl_args` with `name="vm.max_map_count"` and `nlen=16` and the remaining fields with `0` or `nullptr`. – Simon Kraemer Jan 10 '17 at 10:36
  • Update: I would recommend you provide a reasonable sized buffer for `newval` and set `newlen` accordingly – Simon Kraemer Jan 10 '17 at 10:43
  • Tried that with name = int [] { VM_MAX_MAP_COUNT } . sysctl returning -1 with errno = 20 (ENOTDIR) which means it doesn't find the name. I tried with name = 22 with no help either. I'm including sys/sysctl.h + linux/sysctl.h. On standard ubuntu 16.04. – Adi Jan 11 '17 at 09:12

1 Answers1

0

This sysctl command will return a key value pair.

sysctl -q vm.max_map_count
vm.max_map_count = xxxxxx

This sysctl command will return the same key value pair.

sysctl -e -q vm.max_map_count
vm.max_map_count = xxxxxx

This sysctl command will return the same key value pair.

sysctl -e -q vm.max_map_count
vm.max_map_count = xxxxxx

This sysctl command will return the same key value pair.

sysctl -n -e -q vm.max_map_count
xxxxxx

There is your answer. However, I would rather open /proc/sys/vm/max_map_count as a file and read the value verses executing a process from a C program. Calling sysctl in a bash script to set a variable would make more sense.

vmval=`sysctl -n -e -q vm.max_map_count`
echo $vmval
xxxxxx
Greg
  • 233
  • 2
  • 12
  • Hi, perhaps I wasn't clear in my question. I meant - how to get those values programmatically, through an API. Using bash scripts as a mandatory execution requirement is not an option in my case. – Adi Mar 24 '19 at 12:33