diff options
author | Chris Robinson <[email protected]> | 2018-01-13 03:22:41 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-01-13 03:22:41 -0800 |
commit | af8bbefcab1d7ff68dc9c9ce7bc0a0deffe39845 (patch) | |
tree | 9af71abd4fa26220df4f5f18dbdbd23febaf5067 | |
parent | 26043269e43f4f1096b2f4981ed5386ddf02db3e (diff) |
Support procfs on *BSD to get the process binary
-rw-r--r-- | Alc/helpers.c | 79 |
1 files changed, 45 insertions, 34 deletions
diff --git a/Alc/helpers.c b/Alc/helpers.c index 77090d6e..2aded6c5 100644 --- a/Alc/helpers.c +++ b/Alc/helpers.c @@ -731,54 +731,65 @@ void UnmapFileMem(const struct FileMapping *mapping) void GetProcBinary(al_string *path, al_string *fname) { - char *pathname, *sep; + char *pathname = NULL; size_t pathlen; #ifdef __FreeBSD__ int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; mib[3] = getpid(); if(sysctl(mib, 4, NULL, &pathlen, NULL, 0) == -1) - { WARN("Failed to sysctl kern.proc.pathname.%d: %s\n", mib[3], strerror(errno)); - return; - } - - pathname = malloc(pathlen + 1); - sysctl(mib, 4, (void*)pathname, &pathlen, NULL, 0); - pathname[pathlen] = 0; -#else - const char *selfname; - ssize_t len; - - pathlen = 256; - pathname = malloc(pathlen); - - selfname = "/proc/self/exe"; - len = readlink(selfname, pathname, pathlen); - if(len == -1 && errno == ENOENT) + else { - selfname = "/proc/self/file"; - len = readlink(selfname, pathname, pathlen); + pathname = malloc(pathlen + 1); + sysctl(mib, 4, (void*)pathname, &pathlen, NULL, 0); + pathname[pathlen] = 0; } - - while(len > 0 && (size_t)len == pathlen) +#endif + if(!pathname) { - free(pathname); - pathlen <<= 1; + const char *selfname; + ssize_t len; + + pathlen = 256; pathname = malloc(pathlen); + + selfname = "/proc/self/exe"; len = readlink(selfname, pathname, pathlen); - } - if(len <= 0) - { - free(pathname); - WARN("Failed to readlink %s: %s\n", selfname, strerror(errno)); - return; - } + if(len == -1 && errno == ENOENT) + { + selfname = "/proc/self/file"; + len = readlink(selfname, pathname, pathlen); + } + if(len == -1 && errno == ENOENT) + { + selfname = "/proc/curproc/exe"; + len = readlink(selfname, pathname, pathlen); + } + if(len == -1 && errno == ENOENT) + { + selfname = "/proc/curproc/file"; + len = readlink(selfname, pathname, pathlen); + } - pathname[len] = 0; -#endif + while(len > 0 && (size_t)len == pathlen) + { + free(pathname); + pathlen <<= 1; + pathname = malloc(pathlen); + len = readlink(selfname, pathname, pathlen); + } + if(len <= 0) + { + free(pathname); + WARN("Failed to readlink %s: %s\n", selfname, strerror(errno)); + return; + } + + pathname[len] = 0; + } - sep = strrchr(pathname, '/'); + char *sep = strrchr(pathname, '/'); if(sep) { if(path) alstr_copy_range(path, pathname, sep); |