summaryrefslogtreecommitdiff
path: root/Utilities/cmlibuv/src/unix/netbsd.c
diff options
context:
space:
mode:
Diffstat (limited to 'Utilities/cmlibuv/src/unix/netbsd.c')
-rw-r--r--Utilities/cmlibuv/src/unix/netbsd.c57
1 files changed, 47 insertions, 10 deletions
diff --git a/Utilities/cmlibuv/src/unix/netbsd.c b/Utilities/cmlibuv/src/unix/netbsd.c
index 9b5546b7e..742507233 100644
--- a/Utilities/cmlibuv/src/unix/netbsd.c
+++ b/Utilities/cmlibuv/src/unix/netbsd.c
@@ -40,9 +40,16 @@
#include <unistd.h>
#include <time.h>
+static uv_mutex_t process_title_mutex;
+static uv_once_t process_title_mutex_once = UV_ONCE_INIT;
static char *process_title;
+static void init_process_title_mutex_once(void) {
+ uv_mutex_init(&process_title_mutex);
+}
+
+
int uv__platform_loop_init(uv_loop_t* loop) {
return uv__kqueue_init(loop);
}
@@ -66,22 +73,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;
@@ -124,11 +141,24 @@ char** uv_setup_args(int argc, char** argv) {
int uv_set_process_title(const char* title) {
- if (process_title) uv__free(process_title);
+ char* new_title;
+
+ new_title = uv__strdup(title);
+
+ uv_once(&process_title_mutex_once, init_process_title_mutex_once);
+ uv_mutex_lock(&process_title_mutex);
- process_title = uv__strdup(title);
+ if (process_title == NULL) {
+ uv_mutex_unlock(&process_title_mutex);
+ return -ENOMEM;
+ }
+
+ uv__free(process_title);
+ process_title = new_title;
setproctitle("%s", title);
+ uv_mutex_unlock(&process_title_mutex);
+
return 0;
}
@@ -139,17 +169,24 @@ int uv_get_process_title(char* buffer, size_t size) {
if (buffer == NULL || size == 0)
return -EINVAL;
+ uv_once(&process_title_mutex_once, init_process_title_mutex_once);
+ uv_mutex_lock(&process_title_mutex);
+
if (process_title) {
len = strlen(process_title) + 1;
- if (size < len)
+ if (size < len) {
+ uv_mutex_unlock(&process_title_mutex);
return -ENOBUFS;
+ }
memcpy(buffer, process_title, len);
} else {
len = 0;
}
+ uv_mutex_unlock(&process_title_mutex);
+
buffer[len] = '\0';
return 0;