Skip to content
This repository has been archived by the owner on Sep 14, 2018. It is now read-only.

Commit

Permalink
use a different method to get the executable path on openbsd
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoechtrager committed Jul 19, 2015
1 parent 9372d4c commit e1524ae
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 57 deletions.
70 changes: 56 additions & 14 deletions cctools/ld64/src/3rd/helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ const char ldVersionString[] = "242\n";
#include <mach/host_info.h>
#include <sys/time.h>
#include <assert.h>
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)

#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#include <sys/sysctl.h>
#endif

#ifdef __OpenBSD__
#include <sys/types.h>
#include <sys/user.h>
#include <sys/stat.h>
#endif

#include "helper.h"

void __assert_rtn(const char *func, const char *file, int line, const char *msg)
Expand All @@ -36,39 +42,75 @@ void __assert_rtn(const char *func, const char *file, int line, const char *msg)
}


int _NSGetExecutablePath(char *path, unsigned int *size)
int _NSGetExecutablePath(char *epath, unsigned int *size)
{
#ifdef __FreeBSD__
int mib[4];
mib[0] = CTL_KERN;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
size_t cb = *size;
if (sysctl(mib, 4, path, &cb, NULL, 0) != 0)
if (sysctl(mib, 4, epath, &cb, NULL, 0) != 0)
return -1;
*size = cb;
return 0;
return 0;
#elif defined(__OpenBSD__)
int mib[4];
const char *tmp[100];
size_t l = sizeof(tmp);
char **argv;
size_t len;
const char *comm;
int ok = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = getpid();
mib[3] = KERN_PROC_ARGV;
if (sysctl(mib, 4, tmp, &l, NULL, 0) != 0)
return -1;
*size = strlcpy(path, tmp[0], *size);
return 0;
if (sysctl(mib, 4, NULL, &len, NULL, 0) < 0)
abort();
if (!(argv = malloc(len)))
abort();
if (sysctl(mib, 4, argv, &len, NULL, 0) < 0)
abort();
comm = argv[0];
if (*comm == '/' || *comm == '.') {
char *rpath;
if ((rpath = realpath(comm, NULL))) {
snprintf(epath, *size, "%s", rpath);
free(rpath);
ok = 1;
}
} else {
char *sp;
char *xpath = strdup(getenv("PATH"));
char *path = strtok_r(xpath, ":", &sp);
struct stat st;
if (!xpath)
abort();
while (path) {
snprintf(epath, *size, "%s/%s", path, comm);
if (!stat(epath, &st) && (st.st_mode & S_IXUSR)) {
ok = 1;
break;
}
path = strtok_r(NULL, ":", &sp);
}
free(xpath);
}
free(argv);
if (ok) {
*strrchr(epath, '/') = '\0';
*size = strlen(epath);
return 0;
}
return -1;
#else
int bufsize = *size;
int ret_size;
ret_size = readlink("/proc/self/exe", path, bufsize-1);
ret_size = readlink("/proc/self/exe", epath, bufsize-1);
if (ret_size != -1)
{
*size = ret_size;
path[ret_size]=0;
epath[ret_size]=0;
return 0;
}
else
Expand Down
102 changes: 72 additions & 30 deletions cctools/libstuff/emulated.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,88 @@
#include <mach/host_info.h>
#include <sys/time.h>

#if defined(__FreeBSD__) || defined(__OpenBSD__)
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#include <sys/sysctl.h>
#endif

int _NSGetExecutablePath(char *path, unsigned int *size)
#ifdef __OpenBSD__
#include <sys/types.h>
#include <sys/user.h>
#include <sys/stat.h>
#endif

int _NSGetExecutablePath(char *epath, unsigned int *size)
{
#ifdef __FreeBSD__
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
size_t cb = *size;
if (sysctl(mib, 4, path, &cb, NULL, 0) != 0)
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
size_t cb = *size;
if (sysctl(mib, 4, epath, &cb, NULL, 0) != 0)
return -1;
*size = cb;
return 0;
*size = cb;
return 0;
#elif defined(__OpenBSD__)
int mib[4];
const char *tmp[100];
size_t l = sizeof(tmp);
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = getpid();
mib[3] = KERN_PROC_ARGV;
if (sysctl(mib, 4, tmp, &l, NULL, 0) != 0)
return -1;
*size = strlcpy(path, tmp[0], *size);
return 0;
int mib[4];
char **argv;
size_t len;
const char *comm;
int ok = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = getpid();
mib[3] = KERN_PROC_ARGV;
if (sysctl(mib, 4, NULL, &len, NULL, 0) < 0)
abort();
if (!(argv = malloc(len)))
abort();
if (sysctl(mib, 4, argv, &len, NULL, 0) < 0)
abort();
comm = argv[0];
if (*comm == '/' || *comm == '.') {
char *rpath;
if ((rpath = realpath(comm, NULL))) {
snprintf(epath, *size, "%s", rpath);
free(rpath);
ok = 1;
}
} else {
char *sp;
char *xpath = strdup(getenv("PATH"));
char *path = strtok_r(xpath, ":", &sp);
struct stat st;
if (!xpath)
abort();
while (path) {
snprintf(epath, *size, "%s/%s", path, comm);
if (!stat(epath, &st) && (st.st_mode & S_IXUSR)) {
ok = 1;
break;
}
path = strtok_r(NULL, ":", &sp);
}
free(xpath);
}
free(argv);
if (ok) {
*strrchr(epath, '/') = '\0';
*size = strlen(epath);
return 0;
}
return -1;
#else
int bufsize = *size;
int ret_size;
ret_size = readlink("/proc/self/exe", path, bufsize-1);
if (ret_size != -1)
{
int bufsize = *size;
int ret_size;
ret_size = readlink("/proc/self/exe", epath, bufsize-1);
if (ret_size != -1)
{
*size = ret_size;
path[ret_size]=0;
epath[ret_size]=0;
return 0;
}
else
}
else
return -1;
#endif
}
Expand Down
75 changes: 62 additions & 13 deletions usage_examples/ios_toolchain/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,79 @@
#include <mach-o/dyld.h>
#endif

#ifdef __FreeBSD__
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#include <sys/sysctl.h>
#endif

char *get_executable_path(char *buf, size_t len)
#ifdef __OpenBSD__
#include <sys/types.h>
#include <sys/user.h>
#include <sys/stat.h>
#endif

char *get_executable_path(char *epath, size_t buflen)
{
char *p;
#ifdef __APPLE__
unsigned int l = len;
if (_NSGetExecutablePath(buf, &l) != 0) return NULL;
unsigned int l = buflen;
if (_NSGetExecutablePath(epath, &l) != 0) return NULL;
#elif defined(__FreeBSD__)
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
size_t l = len;
if (sysctl(mib, 4, buf, &l, NULL, 0) != 0) return NULL;
#elif defined(_WIN32)
size_t l = GetModuleFileName(NULL, buf, (DWORD)len);
size_t l = buflen;
if (sysctl(mib, 4, epath, &l, NULL, 0) != 0) return NULL;
#elif defined(__OpenBSD__)
int mib[4];
char **argv;
size_t len;
size_t l;
const char *comm;
int ok = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = getpid();
mib[3] = KERN_PROC_ARGV;
if (sysctl(mib, 4, NULL, &len, NULL, 0) < 0)
abort();
if (!(argv = malloc(len)))
abort();
if (sysctl(mib, 4, argv, &len, NULL, 0) < 0)
abort();
comm = argv[0];
if (*comm == '/' || *comm == '.') {
char *rpath;
if ((rpath = realpath(comm, NULL))) {
snprintf(epath, buflen, "%s", rpath);
free(rpath);
ok = 1;
}
} else {
char *sp;
char *xpath = strdup(getenv("PATH"));
char *path = strtok_r(xpath, ":", &sp);
struct stat st;
if (!xpath)
abort();
while (path) {
snprintf(epath, buflen, "%s/%s", path, comm);
if (!stat(epath, &st) && (st.st_mode & S_IXUSR)) {
ok = 1;
break;
}
path = strtok_r(NULL, ":", &sp);
}
free(xpath);
}
free(argv);
if (!ok) return NULL;
l = strlen(epath);
#else
ssize_t l = readlink("/proc/self/exe", buf, len);
ssize_t l = readlink("/proc/self/exe", epath, buflen);
#endif
if (l <= 0) return NULL;
buf[len - 1] = '\0';
p = strrchr(buf, '/');
epath[buflen - 1] = '\0';
p = strrchr(epath, '/');
if (p) *p = '\0';
return buf;
return epath;
}

char *get_filename(char *str)
Expand Down Expand Up @@ -88,7 +137,7 @@ int main(int argc, char *argv[])
target_info(argv, &target, &compiler);
if (!get_executable_path(execpath, sizeof(execpath))) abort();
snprintf(sdkpath, sizeof(sdkpath), "%s/../SDK", execpath);

snprintf(codesign_allocate, sizeof(codesign_allocate),
"%s-codesign_allocate", target);

Expand Down

0 comments on commit e1524ae

Please sign in to comment.