Skip to content

Instantly share code, notes, and snippets.

@datenwolf
Created December 21, 2020 12:09
Show Gist options
  • Save datenwolf/1c4b91de09a9c3a59bf6bba1e740a7d6 to your computer and use it in GitHub Desktop.
Save datenwolf/1c4b91de09a9c3a59bf6bba1e740a7d6 to your computer and use it in GitHub Desktop.
Create Linux pidfds for several processes and dump their inode numbers.
#define _GNU_SOURCE
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wait.h>
#include <stdio.h>
static int pidfd_open(pid_t pid, unsigned flags)
{
enum { __NR_pidfd_open = 434 };
return syscall(__NR_pidfd_open, pid, flags);
}
int main(int argc, char *argv[])
{
for( int i = 0; i < 10; ++i ){
if( !fork() ){
pid_t const pid = getpid();
int fd = pidfd_open(pid, 0);
struct statx st = {0};
statx(fd, "", AT_EMPTY_PATH, STATX_TYPE|STATX_INO, &st);
printf( "pid=%u: inode=%llu\n",
(unsigned)pid, (unsigned long long)st.stx_ino);
raise(SIGSTOP);
return 0;
}
}
kill(0, SIGCONT);
waitpid(-1, NULL, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment