Last active
February 1, 2017 08:37
-
-
Save josteink/18c7249e6e0d15053f6435b673ba5b08 to your computer and use it in GitHub Desktop.
Getting your own process without using procfs on Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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