Skip to content

Instantly share code, notes, and snippets.

@mneumann
Created August 5, 2016 20:02
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 mneumann/a2f6b6a0a03935b561d6185872a4b222 to your computer and use it in GitHub Desktop.
Save mneumann/a2f6b6a0a03935b561d6185872a4b222 to your computer and use it in GitHub Desktop.
current_exe
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdlib.h>
#include <errno.h>
int main() {
{
char buf[1024];
int sz = readlink("/proc/curproc/file", &buf, 1024);
buf[sz] = 0;
printf("procfs: %s\n", buf);
}
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
size_t sz = 0;
int err = sysctl(mib, 4, NULL, &sz, NULL, 0);
char *s = malloc(sz);
err = sysctl(mib, 4, s, &sz, NULL, 0);
s[sz-1] = 0;
printf("sysctl: %s\n", s);
return 0;
}
# uname -a
DragonFly babel.localnet 4.7-DEVELOPMENT DragonFly v4.7.0.94.g57eef-DEVELOPMENT #25: Wed Aug 3 20:20:48 CEST 2016 mneumann@babel.localnet:/usr/obj/usr/src/sys/X86_64_GENERIC x86_64
# ./current_exe
procfs: /home/mneumann/current_exe
sysctl: /data2/pfs/@@-1:00001/mneumann/current_exe
## The problem is, that the path sysctl returns contains a ":", which is not allowed inside a regular path.
@mneumann
Copy link
Author

mneumann commented Aug 5, 2016

This commit fixes the issue. DragonFly >= 4.6.1 will not be affected:

# uname -a
DragonFly babel.localnet 4.7-DEVELOPMENT DragonFly v4.7.0.114.g726f7-DEVELOPMENT #27: Fri Aug  5 22:24:40 CEST 2016     mneumann@babel.localnet:/usr/obj/usr/src/sys/X86_64_GENERIC  x86_64

# ./current_exe
procfs: /home/mneumann/current_exe
sysctl: /home/mneumann/current_exe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment