authorgravatar for i@mgxm.meMarcio Giaxa <i@mgxm.me> 2018-12-18 16:22:20-02:00
committergravatar for i@mgxm.meMarcio Giaxa <i@mgxm.me> 2018-12-19 18:42:00-02:00
log9900f94afe5556db048b4b147bbf75ff0e0c9974
tree13fa41829db8973d445e8b9ffad9856d2681c8a4
parent1811e7e6c96844a8381e6a69017948cae0b8b261

freebsd: use sysctl to get the current executable path

FreeBSD doesn't mount procfs as default on the base system, so we can't depend on it to get the current path, In this case, we use sysctl(3) to retrieves the system information and get the same information. - CTL_KERN: High kernel limits - KERN_PROC: Return selected information about specific running processes. - KERN_PROC_PATHNAME: The path of the process - Process ID: a process ID of -1 implies the current process.

2 files changed, 20 insertions(+), 1 deletions(-)

std/os/freebsd/index.zig+6
......@@ -9,6 +9,12 @@ const assert = std.debug.assert;
99const maxInt = std.math.maxInt;
1010pub const Kevent = c.Kevent;
1111
12pub const CTL_KERN = 1;
13pub const CTL_DEBUG = 5;
14
15pub const KERN_PROC = 14; // struct: process entries
16pub const KERN_PROC_PATHNAME = 12; // path to executable
17
1218pub const PATH_MAX = 1024;
1319
1420pub const STDIN_FILENO = 0;
std/os/index.zig+14-1
......@@ -2291,7 +2291,20 @@ pub fn selfExePathW(out_buffer: *[windows_util.PATH_MAX_WIDE]u16) ![]u16 {
22912291pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
22922292 switch (builtin.os) {
22932293 Os.linux => return readLink(out_buffer, "/proc/self/exe"),
2294 Os.freebsd => return readLink(out_buffer, "/proc/curproc/file"),
2294 Os.freebsd => {
2295 var mib = [4]c_int{ posix.CTL_KERN, posix.KERN_PROC, posix.KERN_PROC_PATHNAME, -1};
2296 var out_len: usize = out_buffer.len;
2297 const err = posix.getErrno(posix.sysctl(&mib, 4, out_buffer, &out_len, null, 0));
2298
2299 if (err == 0 ) return mem.toSlice(u8, out_buffer);
2300
2301 return switch (err) {
2302 posix.EFAULT => error.BadAdress,
2303 posix.EPERM => error.PermissionDenied,
2304 else => unexpectedErrorPosix(err),
2305 };
2306
2307 },
22952308 Os.windows => {
22962309 var utf16le_buf: [windows_util.PATH_MAX_WIDE]u16 = undefined;
22972310 const utf16le_slice = try selfExePathW(&utf16le_buf);