From d0b15844dd00f949a1c503d9e95db5d0b7f5c87c Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Tue, 16 Oct 2018 22:04:44 +0200 Subject: [PATCH] core,bsd: refactor process_title functions Use `setproctitle()` in `FreeBSD` as in the other `BSD` flavours. Move `uv_setup_args()`, `uv_set_process_title()` and `uv_get_process_title()` to `bsd-proctitle.c` so it's used by the supported BSDs. PR-URL: https://github.com/libuv/libuv/pull/2042 Reviewed-By: Ben Noordhuis Reviewed-By: Sakthipriyan Vairamani --- CMakeLists.txt | 2 +- Makefile.am | 4 ++ src/unix/bsd-proctitle.c | 93 ++++++++++++++++++++++++++++++++++++++++ src/unix/freebsd.c | 79 ---------------------------------- src/unix/netbsd.c | 68 ----------------------------- src/unix/openbsd.c | 69 ----------------------------- uv.gyp | 5 ++- 7 files changed, 102 insertions(+), 218 deletions(-) create mode 100644 src/unix/bsd-proctitle.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a631a87..61c21031 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -260,7 +260,7 @@ if(CMAKE_SYSTEM_NAME MATCHES "DragonFly|FreeBSD") endif() if(CMAKE_SYSTEM_NAME MATCHES "DragonFly|FreeBSD|NetBSD|OpenBSD") - list(APPEND uv_sources src/unix/posix-hrtime.c) + list(APPEND uv_sources src/unix/posix-hrtime.c src/unix/bsd-proctitle.c) list(APPEND uv_libraries kvm) endif() diff --git a/Makefile.am b/Makefile.am index 92c63051..433d1212 100644 --- a/Makefile.am +++ b/Makefile.am @@ -374,6 +374,7 @@ endif if DRAGONFLY uvinclude_HEADERS += include/uv/bsd.h libuv_la_SOURCES += src/unix/bsd-ifaddrs.c \ + src/unix/bsd-proctitle.c \ src/unix/freebsd.c \ src/unix/kqueue.c \ src/unix/posix-hrtime.c @@ -383,6 +384,7 @@ endif if FREEBSD uvinclude_HEADERS += include/uv/bsd.h libuv_la_SOURCES += src/unix/bsd-ifaddrs.c \ + src/unix/bsd-proctitle.c \ src/unix/freebsd.c \ src/unix/kqueue.c \ src/unix/posix-hrtime.c @@ -426,6 +428,7 @@ endif if NETBSD uvinclude_HEADERS += include/uv/bsd.h libuv_la_SOURCES += src/unix/bsd-ifaddrs.c \ + src/unix/bsd-proctitle.c \ src/unix/kqueue.c \ src/unix/netbsd.c \ src/unix/posix-hrtime.c @@ -435,6 +438,7 @@ endif if OPENBSD uvinclude_HEADERS += include/uv/bsd.h libuv_la_SOURCES += src/unix/bsd-ifaddrs.c \ + src/unix/bsd-proctitle.c \ src/unix/kqueue.c \ src/unix/openbsd.c \ src/unix/posix-hrtime.c diff --git a/src/unix/bsd-proctitle.c b/src/unix/bsd-proctitle.c new file mode 100644 index 00000000..0ce47c8f --- /dev/null +++ b/src/unix/bsd-proctitle.c @@ -0,0 +1,93 @@ +/* Copyright libuv project contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" +#include "internal.h" + +#include +#include + + +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) { + if (uv_mutex_init(&process_title_mutex)) + abort(); +} + + +char** uv_setup_args(int argc, char** argv) { + process_title = argc > 0 ? uv__strdup(argv[0]) : NULL; + return argv; +} + + +int uv_set_process_title(const char* title) { + char* new_title; + + new_title = uv__strdup(title); + if (new_title == NULL) + return UV_ENOMEM; + + uv_once(&process_title_mutex_once, init_process_title_mutex_once); + uv_mutex_lock(&process_title_mutex); + + 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 UV_EINVAL; + + uv_once(&process_title_mutex_once, init_process_title_mutex_once); + uv_mutex_lock(&process_title_mutex); + + if (process_title != NULL) { + len = strlen(process_title) + 1; + + 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; +} diff --git a/src/unix/freebsd.c b/src/unix/freebsd.c index 70ccb130..0f729cfd 100644 --- a/src/unix/freebsd.c +++ b/src/unix/freebsd.c @@ -47,15 +47,6 @@ # define CP_INTR 4 #endif -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); @@ -159,76 +150,6 @@ void uv_loadavg(double avg[3]) { } -char** uv_setup_args(int argc, char** argv) { - process_title = argc ? uv__strdup(argv[0]) : NULL; - return argv; -} - - -int uv_set_process_title(const char* title) { - int oid[4]; - 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); - - if (process_title == NULL) { - uv_mutex_unlock(&process_title_mutex); - return UV_ENOMEM; - } - - uv__free(process_title); - process_title = new_title; - - oid[0] = CTL_KERN; - oid[1] = KERN_PROC; - oid[2] = KERN_PROC_ARGS; - oid[3] = getpid(); - - sysctl(oid, - ARRAY_SIZE(oid), - NULL, - NULL, - process_title, - strlen(process_title) + 1); - - 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 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) { - 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; -} - int uv_resident_set_memory(size_t* rss) { struct kinfo_proc kinfo; size_t page_size; diff --git a/src/unix/netbsd.c b/src/unix/netbsd.c index 2605c114..4cfde1a5 100644 --- a/src/unix/netbsd.c +++ b/src/unix/netbsd.c @@ -40,15 +40,6 @@ #include #include -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); @@ -134,65 +125,6 @@ uint64_t uv_get_total_memory(void) { } -char** uv_setup_args(int argc, char** argv) { - process_title = argc ? uv__strdup(argv[0]) : NULL; - return argv; -} - - -int uv_set_process_title(const char* 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); - - 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 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) { - 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; -} - - int uv_resident_set_memory(size_t* rss) { kvm_t *kd = NULL; struct kinfo_proc2 *kinfo = NULL; diff --git a/src/unix/openbsd.c b/src/unix/openbsd.c index ce937cd3..bffb58bc 100644 --- a/src/unix/openbsd.c +++ b/src/unix/openbsd.c @@ -36,16 +36,6 @@ #include -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); } @@ -146,65 +136,6 @@ uint64_t uv_get_total_memory(void) { } -char** uv_setup_args(int argc, char** argv) { - process_title = argc ? uv__strdup(argv[0]) : NULL; - return argv; -} - - -int uv_set_process_title(const char* 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); - - 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 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) { - 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; -} - - int uv_resident_set_memory(size_t* rss) { struct kinfo_proc kinfo; size_t page_size = getpagesize(); diff --git a/uv.gyp b/uv.gyp index 5148a850..1e8d913d 100644 --- a/uv.gyp +++ b/uv.gyp @@ -326,7 +326,10 @@ 'sources': [ 'src/unix/netbsd.c' ], }], [ 'OS in "freebsd dragonflybsd openbsd netbsd".split()', { - 'sources': [ 'src/unix/posix-hrtime.c' ], + 'sources': [ + 'src/unix/posix-hrtime.c', + 'src/unix/bsd-proctitle.c' + ], }], [ 'OS in "ios mac freebsd dragonflybsd openbsd netbsd".split()', { 'sources': [