Skip to content

Instantly share code, notes, and snippets.

@xordspar0
Created June 7, 2025 23:41
Show Gist options
  • Save xordspar0/fde6680066022cccf0ac5d9f53e4cafd to your computer and use it in GitHub Desktop.
Save xordspar0/fde6680066022cccf0ac5d9f53e4cafd to your computer and use it in GitHub Desktop.
argv[0] vs /proc/self/exe
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
char *executable_path;
executable_path = realpath(argv[0], NULL);
if (executable_path == NULL) {
perror("realpath");
return EXIT_FAILURE;
}
printf("argv[0] realpath:\t\t%s\n", executable_path);
free(executable_path);
executable_path = realpath("/proc/self/exe", NULL);
if (executable_path == NULL) {
perror("realpath");
return EXIT_FAILURE;
}
printf("/proc/self/exe realpath:\t%s\n", executable_path);
free(executable_path);
return EXIT_SUCCESS;
}
/*
* Build with: gcc self.c -o self
*
* Example of running this on Ubuntu:
*
* $ /self
* argv[0] realpath: /tmp/self
* /proc/self/exe realpath: /tmp/self
*
* $ /lib64/ld-linux-x86-64.so.2 ./self
* argv[0] realpath: /tmp/self
* /proc/self/exe realpath: /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
*
* Example of running this on Alpine:
*
* $ ./self
* argv[0] realpath: /tmp/self
* /proc/self/exe realpath: /tmp/self
*
* $ /lib/ld-musl-x86_64.so.1 ./self
* argv[0] realpath: /tmp/self
* /proc/self/exe realpath: /lib/ld-musl-x86_64.so.1
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment