Path: blob/master/Utilities/cmlibuv/src/unix/os390-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 <stdlib.h>25#include <string.h>2627static uv_mutex_t process_title_mutex;28static uv_once_t process_title_mutex_once = UV_ONCE_INIT;29static char* process_title = NULL;30static void* args_mem = NULL;313233static void init_process_title_mutex_once(void) {34uv_mutex_init(&process_title_mutex);35}363738char** uv_setup_args(int argc, char** argv) {39char** new_argv;40size_t size;41char* s;42int i;4344if (argc <= 0)45return argv;4647/* Calculate how much memory we need for the argv strings. */48size = 0;49for (i = 0; i < argc; i++)50size += strlen(argv[i]) + 1;5152/* Add space for the argv pointers. */53size += (argc + 1) * sizeof(char*);5455new_argv = uv__malloc(size);56if (new_argv == NULL)57return argv;5859/* Copy over the strings and set up the pointer table. */60s = (char*) &new_argv[argc + 1];61for (i = 0; i < argc; i++) {62size = strlen(argv[i]) + 1;63memcpy(s, argv[i], size);64new_argv[i] = s;65s += size;66}67new_argv[i] = NULL;6869args_mem = new_argv;70process_title = uv__strdup(argv[0]);7172return new_argv;73}747576int uv_set_process_title(const char* title) {77char* new_title;7879/* If uv_setup_args wasn't called or failed, we can't continue. */80if (args_mem == NULL)81return UV_ENOBUFS;8283/* We cannot free this pointer when libuv shuts down,84* the process may still be using it.85*/86new_title = uv__strdup(title);87if (new_title == NULL)88return UV_ENOMEM;8990uv_once(&process_title_mutex_once, init_process_title_mutex_once);91uv_mutex_lock(&process_title_mutex);9293if (process_title != NULL)94uv__free(process_title);9596process_title = new_title;9798uv_mutex_unlock(&process_title_mutex);99100return 0;101}102103104int uv_get_process_title(char* buffer, size_t size) {105size_t len;106107if (buffer == NULL || size == 0)108return UV_EINVAL;109110/* If uv_setup_args wasn't called or failed, we can't continue. */111if (args_mem == NULL || process_title == NULL)112return UV_ENOBUFS;113114uv_once(&process_title_mutex_once, init_process_title_mutex_once);115uv_mutex_lock(&process_title_mutex);116117len = strlen(process_title);118119if (size <= len) {120uv_mutex_unlock(&process_title_mutex);121return UV_ENOBUFS;122}123124strcpy(buffer, process_title);125126uv_mutex_unlock(&process_title_mutex);127128return 0;129}130131132void uv__process_title_cleanup(void) {133uv__free(args_mem); /* Keep valgrind happy. */134args_mem = NULL;135}136137138