From 31516fa150b9cde16f53c0ad3dd02542bd1ff5e8 Mon Sep 17 00:00:00 2001 From: Robert Mustacchi Date: Mon, 20 Jun 2011 23:24:13 -0700 Subject: [PATCH] Fixes #49. test-get-currentexe fails on solaris --- uv-sunos.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/uv-sunos.c b/uv-sunos.c index 0f1fad3c..8e068822 100644 --- a/uv-sunos.c +++ b/uv-sunos.c @@ -20,7 +20,10 @@ #include "uv.h" +#include +#include #include +#include #include @@ -29,8 +32,30 @@ uint64_t uv_get_hrtime() { } +/* + * We could use a static buffer for the path manipulations that we need outside + * of the function, but this function could be called by multiple consumers and + * we don't want to potentially create a race condition in the use of snprintf. + */ int uv_get_exepath(char* buffer, size_t* size) { - assert(0 && "implement me"); - /* Need to return argv[0] */ - return -1; + size_t res; + pid_t pid; + char buf[PATH_MAX]; + + if (buffer == NULL) + return (-1); + + if (size == NULL) + return (-1); + + pid = getpid(); + (void) snprintf(buf, sizeof (buf), "/proc/%d/path/a.out", pid); + res = readlink(buf, buffer, *size - 1); + + if (res < 0) + return (res); + + buffer[res] = '\0'; + *size = res; + return (0); }