Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 21113: Use sysctl for thisExePath on BSD. #7578

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 48 additions & 12 deletions std/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -3493,10 +3493,26 @@ version (Darwin)
else version (FreeBSD)
private extern (C) int sysctl (const int* name, uint namelen, void* oldp,
size_t* oldlenp, const void* newp, size_t newlen);
else version (DragonFlyBSD)
private extern (C) int sysctl (const int* name, uint namelen, void* oldp,
size_t* oldlenp, const void* newp, size_t newlen);
else version (NetBSD)
private extern (C) int sysctl (const int* name, uint namelen, void* oldp,
size_t* oldlenp, const void* newp, size_t newlen);

version (FreeBSD)
{
version = thisExePathHasSysCtlProcName;
}
else version (NetBSD)
{
version = thisExePathHasSysCtlProcName;
}
else version (DragonFlyBSD)
{
version = thisExePathHasSysCtlProcName;
}

/**
* Returns the full path of the current executable.
*
Expand Down Expand Up @@ -3552,17 +3568,45 @@ else version (NetBSD)
buffer.length *= 2;
}
}
else version (FreeBSD)
else version (thisExePathHasSysCtlProcName)
{
import std.exception : errnoEnforce, assumeUnique;
enum
{
CTL_KERN = 1,
KERN_PROC = 14,
KERN_PROC_PATHNAME = 12
KERN_PROC = 14
}

version (NetBSD)
{
enum
{
KERN_PROC_ARGS = 48,
KERN_PROC_PATHNAME = 5
}

int[4] mib = [CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME];
}
else
{
version (FreeBSD)
{
enum
{
KERN_PROC_PATHNAME = 12
}
}
else
{
enum
{
KERN_PROC_PATHNAME = 9
}
}

int[4] mib = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1];
}

int[4] mib = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1];
size_t len;

auto result = sysctl(mib.ptr, mib.length, null, &len, null, 0); // get the length of the path
Expand All @@ -3574,14 +3618,6 @@ else version (NetBSD)

return buffer.assumeUnique;
}
else version (NetBSD)
{
return readLink("/proc/self/exe");
}
else version (DragonFlyBSD)
{
return readLink("/proc/curproc/file");
}
else version (Solaris)
{
import core.sys.posix.unistd : getpid;
Expand Down