Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Last active June 28, 2022 22:17
Show Gist options
  • Save ammarfaizi2/306ec3b25284b6974e693f0b39b81cb5 to your computer and use it in GitHub Desktop.
Save ammarfaizi2/306ec3b25284b6974e693f0b39b81cb5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <fcntl.h>
#include <elf.h>
#include <string.h>
#include <errno.h>
#include <sys/auxv.h>
#include <unistd.h>
// See: https://elixir.bootlin.com/linux/v5.19-rc4/source/fs/binfmt_elf.c#L260
static long get_page_size(void)
{
Elf64_Off buf[2];
long page_size;
int fd;
fd = open("/proc/self/auxv", O_RDONLY);
if (fd < 0)
return -errno;
while (1) {
ssize_t ret;
ret = read(fd, buf, sizeof(buf));
if (ret < 0) {
page_size = -errno;
break;
}
if (ret < sizeof(buf)) {
page_size = -ENOENT;
break;
}
if (buf[0] == AT_PAGESZ) {
page_size = buf[1];
break;
}
}
close(fd);
return page_size;
}
int main(void)
{
printf("pg = %ld\n", get_page_size());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment