ext/libuv/src/unix/openbsd.c in libuv-4.0.0 vs ext/libuv/src/unix/openbsd.c in libuv-4.0.1

- old
+ new

@@ -34,13 +34,20 @@ #include <stdlib.h> #include <string.h> #include <unistd.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); } @@ -69,15 +76,15 @@ size_t exepath_size; pid_t mypid; int err; if (buffer == NULL || size == NULL || *size == 0) - return -EINVAL; + return UV_EINVAL; mypid = getpid(); for (;;) { - err = -ENOMEM; + err = UV_ENOMEM; argsbuf_tmp = uv__realloc(argsbuf, argsbuf_size); if (argsbuf_tmp == NULL) goto out; argsbuf = argsbuf_tmp; mib[0] = CTL_KERN; @@ -86,18 +93,18 @@ mib[3] = KERN_PROC_ARGV; if (sysctl(mib, 4, argsbuf, &argsbuf_size, NULL, 0) == 0) { break; } if (errno != ENOMEM) { - err = -errno; + err = UV__ERR(errno); goto out; } argsbuf_size *= 2U; } if (argsbuf[0] == NULL) { - err = -EINVAL; /* FIXME(bnoordhuis) More appropriate error. */ + err = UV_EINVAL; /* FIXME(bnoordhuis) More appropriate error. */ goto out; } *size -= 1; exepath_size = strlen(argsbuf[0]); @@ -119,11 +126,11 @@ struct uvmexp info; size_t size = sizeof(info); int which[] = {CTL_VM, VM_UVMEXP}; if (sysctl(which, 2, &info, &size, NULL, 0)) - return -errno; + return UV__ERR(errno); return (uint64_t) info.free * sysconf(_SC_PAGESIZE); } @@ -131,11 +138,11 @@ uint64_t info; int which[] = {CTL_HW, HW_PHYSMEM64}; size_t size = sizeof(info); if (sysctl(which, 2, &info, &size, NULL, 0)) - return -errno; + return UV__ERR(errno); return (uint64_t) info; } @@ -147,36 +154,53 @@ int uv_set_process_title(const char* title) { char* new_title; new_title = uv__strdup(title); - if (process_title == NULL) - return -ENOMEM; + + uv_once(&process_title_mutex_once, init_process_title_mutex_once); + uv_mutex_lock(&process_title_mutex); + + if (process_title == NULL) { + uv_mutex_unlock(&process_title_mutex); + return UV_ENOMEM; + } + uv__free(process_title); process_title = new_title; setproctitle("%s", title); + + uv_mutex_unlock(&process_title_mutex); + return 0; } int uv_get_process_title(char* buffer, size_t size) { size_t len; if (buffer == NULL || size == 0) - return -EINVAL; + return UV_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) - return -ENOBUFS; + if (size < len) { + uv_mutex_unlock(&process_title_mutex); + return UV_ENOBUFS; + } memcpy(buffer, process_title, len); } else { len = 0; } + uv_mutex_unlock(&process_title_mutex); + buffer[len] = '\0'; return 0; } @@ -193,11 +217,11 @@ mib[3] = getpid(); mib[4] = sizeof(struct kinfo_proc); mib[5] = 1; if (sysctl(mib, 6, &kinfo, &size, NULL, 0) < 0) - return -errno; + return UV__ERR(errno); *rss = kinfo.p_vm_rssize * page_size; return 0; } @@ -207,11 +231,11 @@ struct timeval info; size_t size = sizeof(info); static int which[] = {CTL_KERN, KERN_BOOTTIME}; if (sysctl(which, 2, &info, &size, NULL, 0)) - return -errno; + return UV__ERR(errno); now = time(NULL); *uptime = (double)(now - info.tv_sec); return 0; @@ -229,38 +253,38 @@ int i; uv_cpu_info_t* cpu_info; size = sizeof(model); if (sysctl(which, 2, &model, &size, NULL, 0)) - return -errno; + return UV__ERR(errno); which[1] = HW_NCPU; size = sizeof(numcpus); if (sysctl(which, 2, &numcpus, &size, NULL, 0)) - return -errno; + return UV__ERR(errno); *cpu_infos = uv__malloc(numcpus * sizeof(**cpu_infos)); if (!(*cpu_infos)) - return -ENOMEM; + return UV_ENOMEM; *count = numcpus; which[1] = HW_CPUSPEED; size = sizeof(cpuspeed); if (sysctl(which, 2, &cpuspeed, &size, NULL, 0)) { uv__free(*cpu_infos); - return -errno; + return UV__ERR(errno); } size = sizeof(info); which[0] = CTL_KERN; which[1] = KERN_CPTIME2; for (i = 0; i < numcpus; i++) { which[2] = i; size = sizeof(info); if (sysctl(which, 3, &info, &size, NULL, 0)) { uv__free(*cpu_infos); - return -errno; + return UV__ERR(errno); } cpu_info = &(*cpu_infos)[i]; cpu_info->cpu_times.user = (uint64_t)(info[CP_USER]) * multiplier;