11cb0ef41Sopenharmony_ci/* Copyright libuv project contributors. All rights reserved. 21cb0ef41Sopenharmony_ci * 31cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 41cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to 51cb0ef41Sopenharmony_ci * deal in the Software without restriction, including without limitation the 61cb0ef41Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 71cb0ef41Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is 81cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions: 91cb0ef41Sopenharmony_ci * 101cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 111cb0ef41Sopenharmony_ci * all copies or substantial portions of the Software. 121cb0ef41Sopenharmony_ci * 131cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 141cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 151cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 161cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 171cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 181cb0ef41Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 191cb0ef41Sopenharmony_ci * IN THE SOFTWARE. 201cb0ef41Sopenharmony_ci */ 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci#include "uv.h" 231cb0ef41Sopenharmony_ci#include "internal.h" 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci#include <stdlib.h> 261cb0ef41Sopenharmony_ci#include <string.h> 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_cistatic uv_mutex_t process_title_mutex; 291cb0ef41Sopenharmony_cistatic uv_once_t process_title_mutex_once = UV_ONCE_INIT; 301cb0ef41Sopenharmony_cistatic char* process_title = NULL; 311cb0ef41Sopenharmony_cistatic void* args_mem = NULL; 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_cistatic void init_process_title_mutex_once(void) { 351cb0ef41Sopenharmony_ci uv_mutex_init(&process_title_mutex); 361cb0ef41Sopenharmony_ci} 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cichar** uv_setup_args(int argc, char** argv) { 401cb0ef41Sopenharmony_ci char** new_argv; 411cb0ef41Sopenharmony_ci size_t size; 421cb0ef41Sopenharmony_ci char* s; 431cb0ef41Sopenharmony_ci int i; 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci if (argc <= 0) 461cb0ef41Sopenharmony_ci return argv; 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci /* Calculate how much memory we need for the argv strings. */ 491cb0ef41Sopenharmony_ci size = 0; 501cb0ef41Sopenharmony_ci for (i = 0; i < argc; i++) 511cb0ef41Sopenharmony_ci size += strlen(argv[i]) + 1; 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci /* Add space for the argv pointers. */ 541cb0ef41Sopenharmony_ci size += (argc + 1) * sizeof(char*); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci new_argv = uv__malloc(size); 571cb0ef41Sopenharmony_ci if (new_argv == NULL) 581cb0ef41Sopenharmony_ci return argv; 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci /* Copy over the strings and set up the pointer table. */ 611cb0ef41Sopenharmony_ci s = (char*) &new_argv[argc + 1]; 621cb0ef41Sopenharmony_ci for (i = 0; i < argc; i++) { 631cb0ef41Sopenharmony_ci size = strlen(argv[i]) + 1; 641cb0ef41Sopenharmony_ci memcpy(s, argv[i], size); 651cb0ef41Sopenharmony_ci new_argv[i] = s; 661cb0ef41Sopenharmony_ci s += size; 671cb0ef41Sopenharmony_ci } 681cb0ef41Sopenharmony_ci new_argv[i] = NULL; 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci args_mem = new_argv; 711cb0ef41Sopenharmony_ci process_title = uv__strdup(argv[0]); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci return new_argv; 741cb0ef41Sopenharmony_ci} 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ciint uv_set_process_title(const char* title) { 781cb0ef41Sopenharmony_ci char* new_title; 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci /* If uv_setup_args wasn't called or failed, we can't continue. */ 811cb0ef41Sopenharmony_ci if (args_mem == NULL) 821cb0ef41Sopenharmony_ci return UV_ENOBUFS; 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci /* We cannot free this pointer when libuv shuts down, 851cb0ef41Sopenharmony_ci * the process may still be using it. 861cb0ef41Sopenharmony_ci */ 871cb0ef41Sopenharmony_ci new_title = uv__strdup(title); 881cb0ef41Sopenharmony_ci if (new_title == NULL) 891cb0ef41Sopenharmony_ci return UV_ENOMEM; 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci uv_once(&process_title_mutex_once, init_process_title_mutex_once); 921cb0ef41Sopenharmony_ci uv_mutex_lock(&process_title_mutex); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci if (process_title != NULL) 951cb0ef41Sopenharmony_ci uv__free(process_title); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci process_title = new_title; 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci uv_mutex_unlock(&process_title_mutex); 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci return 0; 1021cb0ef41Sopenharmony_ci} 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ciint uv_get_process_title(char* buffer, size_t size) { 1061cb0ef41Sopenharmony_ci size_t len; 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci if (buffer == NULL || size == 0) 1091cb0ef41Sopenharmony_ci return UV_EINVAL; 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci /* If uv_setup_args wasn't called or failed, we can't continue. */ 1121cb0ef41Sopenharmony_ci if (args_mem == NULL || process_title == NULL) 1131cb0ef41Sopenharmony_ci return UV_ENOBUFS; 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci uv_once(&process_title_mutex_once, init_process_title_mutex_once); 1161cb0ef41Sopenharmony_ci uv_mutex_lock(&process_title_mutex); 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci len = strlen(process_title); 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci if (size <= len) { 1211cb0ef41Sopenharmony_ci uv_mutex_unlock(&process_title_mutex); 1221cb0ef41Sopenharmony_ci return UV_ENOBUFS; 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci strcpy(buffer, process_title); 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci uv_mutex_unlock(&process_title_mutex); 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci return 0; 1301cb0ef41Sopenharmony_ci} 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_civoid uv__process_title_cleanup(void) { 1341cb0ef41Sopenharmony_ci uv__free(args_mem); /* Keep valgrind happy. */ 1351cb0ef41Sopenharmony_ci args_mem = NULL; 1361cb0ef41Sopenharmony_ci} 137