Skip to content

Instantly share code, notes, and snippets.

@josteink
Last active February 1, 2017 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josteink/18c7249e6e0d15053f6435b673ba5b08 to your computer and use it in GitHub Desktop.
Save josteink/18c7249e6e0d15053f6435b673ba5b08 to your computer and use it in GitHub Desktop.
Getting your own process without using procfs on Linux
#!/bin/sh
gcc test.c
./a.out
# /home/josteink/demo/a.out
cp a.out b.out
./b.out
# /home/josteink/demo/b.out
ln -s a.out c.out
./c.out
# /home/josteink/demo/a.out
// Shamelessly copied from Stack Overflow
// http://stackoverflow.com/a/23621200
//
// In the lack of a SYSCTL enum for process self-identification,
// the following seems to work:
#include <stdlib.h>
#include <stdio.h>
#include <sys/auxv.h>
#include <linux/limits.h>
int main(int argc, char **argv)
{
char* relative = (char*)getauxval(AT_EXECFN);
char absolute[PATH_MAX];
char *res = realpath(relative, absolute);
if (res) {
printf("%s\n", absolute);
}
else {
printf("Unable to resolve self!\n");
}
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment