From 5fa0f0b6e1a2f922ce7d365cb1a1d3cb2a322f60 Mon Sep 17 00:00:00 2001 From: Kamil Rytarowski Date: Sat, 9 Sep 2017 13:41:34 +0200 Subject: [PATCH] netbsd: correct uv_exepath() on NetBSD Setup proper sysctl(2) arguments in order to retrieve the executable name. Use an intermediate buffer beacause if it is too short, the sysctl(2) call will return error. With this intermediate buffer populate the outer one. Note issue with too long file names on NetBSD-8(beta), this is caused by the current implementation of vnode to path translator in the kernel. It does not cache longer file names than 31 character ones. PR-URL: https://github.com/libuv/libuv/pull/1532 Reviewed-By: Ben Noordhuis Reviewed-By: Santiago Gimeno --- src/unix/netbsd.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/unix/netbsd.c b/src/unix/netbsd.c index c54c04df..d9066349 100644 --- a/src/unix/netbsd.c +++ b/src/unix/netbsd.c @@ -66,22 +66,32 @@ void uv_loadavg(double avg[3]) { int uv_exepath(char* buffer, size_t* size) { + /* Intermediate buffer, retrieving partial path name does not work + * As of NetBSD-8(beta), vnode->path translator does not handle files + * with longer names than 31 characters. + */ + char int_buf[PATH_MAX]; + size_t int_size; int mib[4]; - size_t cb; - pid_t mypid; if (buffer == NULL || size == NULL || *size == 0) return -EINVAL; - mypid = getpid(); mib[0] = CTL_KERN; mib[1] = KERN_PROC_ARGS; - mib[2] = mypid; - mib[3] = KERN_PROC_ARGV; + mib[2] = -1; + mib[3] = KERN_PROC_PATHNAME; + int_size = ARRAY_SIZE(int_buf); - cb = *size; - if (sysctl(mib, 4, buffer, &cb, NULL, 0)) + if (sysctl(mib, 4, int_buf, &int_size, NULL, 0)) return -errno; + + /* Copy string from the intermediate buffer to outer one with appropriate + * length. + */ + strlcpy(buffer, int_buf, *size); + + /* Set new size. */ *size = strlen(buffer); return 0;