Path: blob/master/Utilities/cmlibuv/src/unix/bsd-proctitle.c
3156 views
/* Copyright libuv project contributors. All rights reserved.1*2* Permission is hereby granted, free of charge, to any person obtaining a copy3* of this software and associated documentation files (the "Software"), to4* deal in the Software without restriction, including without limitation the5* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or6* sell copies of the Software, and to permit persons to whom the Software is7* furnished to do so, subject to the following conditions:8*9* The above copyright notice and this permission notice shall be included in10* all copies or substantial portions of the Software.11*12* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING17* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS18* IN THE SOFTWARE.19*/2021#include "uv.h"22#include "internal.h"2324#include <sys/types.h>25#include <unistd.h>262728static uv_mutex_t process_title_mutex;29static uv_once_t process_title_mutex_once = UV_ONCE_INIT;30static char* process_title;313233static void init_process_title_mutex_once(void) {34if (uv_mutex_init(&process_title_mutex))35abort();36}373839void uv__process_title_cleanup(void) {40uv_once(&process_title_mutex_once, init_process_title_mutex_once);41uv_mutex_destroy(&process_title_mutex);42}434445char** uv_setup_args(int argc, char** argv) {46process_title = argc > 0 ? uv__strdup(argv[0]) : NULL;47return argv;48}495051int uv_set_process_title(const char* title) {52char* new_title;5354new_title = uv__strdup(title);55if (new_title == NULL)56return UV_ENOMEM;5758uv_once(&process_title_mutex_once, init_process_title_mutex_once);59uv_mutex_lock(&process_title_mutex);6061uv__free(process_title);62process_title = new_title;63setproctitle("%s", title);6465uv_mutex_unlock(&process_title_mutex);6667return 0;68}697071int uv_get_process_title(char* buffer, size_t size) {72size_t len;7374if (buffer == NULL || size == 0)75return UV_EINVAL;7677uv_once(&process_title_mutex_once, init_process_title_mutex_once);78uv_mutex_lock(&process_title_mutex);7980if (process_title != NULL) {81len = strlen(process_title) + 1;8283if (size < len) {84uv_mutex_unlock(&process_title_mutex);85return UV_ENOBUFS;86}8788memcpy(buffer, process_title, len);89} else {90len = 0;91}9293uv_mutex_unlock(&process_title_mutex);9495buffer[len] = '\0';9697return 0;98}99100101