Mercurial
comparison third_party/libuv/src/unix/bsd-proctitle.c @ 160:948de3f54cea
[ThirdParty] Added libuv
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 14 Jan 2026 19:39:52 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 159:05cf9467a1c3 | 160:948de3f54cea |
|---|---|
| 1 /* Copyright libuv project contributors. All rights reserved. | |
| 2 * | |
| 3 * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 4 * of this software and associated documentation files (the "Software"), to | |
| 5 * deal in the Software without restriction, including without limitation the | |
| 6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
| 7 * sell copies of the Software, and to permit persons to whom the Software is | |
| 8 * furnished to do so, subject to the following conditions: | |
| 9 * | |
| 10 * The above copyright notice and this permission notice shall be included in | |
| 11 * all copies or substantial portions of the Software. | |
| 12 * | |
| 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| 18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
| 19 * IN THE SOFTWARE. | |
| 20 */ | |
| 21 | |
| 22 #include "uv.h" | |
| 23 #include "internal.h" | |
| 24 | |
| 25 #include <sys/types.h> | |
| 26 #include <unistd.h> | |
| 27 | |
| 28 | |
| 29 static uv_mutex_t process_title_mutex; | |
| 30 static uv_once_t process_title_mutex_once = UV_ONCE_INIT; | |
| 31 static char* process_title; | |
| 32 | |
| 33 | |
| 34 static void init_process_title_mutex_once(void) { | |
| 35 if (uv_mutex_init(&process_title_mutex)) | |
| 36 abort(); | |
| 37 } | |
| 38 | |
| 39 | |
| 40 void uv__process_title_cleanup(void) { | |
| 41 uv_once(&process_title_mutex_once, init_process_title_mutex_once); | |
| 42 uv_mutex_destroy(&process_title_mutex); | |
| 43 } | |
| 44 | |
| 45 | |
| 46 char** uv_setup_args(int argc, char** argv) { | |
| 47 process_title = argc > 0 ? uv__strdup(argv[0]) : NULL; | |
| 48 return argv; | |
| 49 } | |
| 50 | |
| 51 | |
| 52 int uv_set_process_title(const char* title) { | |
| 53 char* new_title; | |
| 54 | |
| 55 new_title = uv__strdup(title); | |
| 56 if (new_title == NULL) | |
| 57 return UV_ENOMEM; | |
| 58 | |
| 59 uv_once(&process_title_mutex_once, init_process_title_mutex_once); | |
| 60 uv_mutex_lock(&process_title_mutex); | |
| 61 | |
| 62 uv__free(process_title); | |
| 63 process_title = new_title; | |
| 64 setproctitle("%s", title); | |
| 65 | |
| 66 uv_mutex_unlock(&process_title_mutex); | |
| 67 | |
| 68 return 0; | |
| 69 } | |
| 70 | |
| 71 | |
| 72 int uv_get_process_title(char* buffer, size_t size) { | |
| 73 size_t len; | |
| 74 | |
| 75 if (buffer == NULL || size == 0) | |
| 76 return UV_EINVAL; | |
| 77 | |
| 78 uv_once(&process_title_mutex_once, init_process_title_mutex_once); | |
| 79 uv_mutex_lock(&process_title_mutex); | |
| 80 | |
| 81 if (process_title != NULL) { | |
| 82 len = strlen(process_title) + 1; | |
| 83 | |
| 84 if (size < len) { | |
| 85 uv_mutex_unlock(&process_title_mutex); | |
| 86 return UV_ENOBUFS; | |
| 87 } | |
| 88 | |
| 89 memcpy(buffer, process_title, len); | |
| 90 } else { | |
| 91 len = 0; | |
| 92 } | |
| 93 | |
| 94 uv_mutex_unlock(&process_title_mutex); | |
| 95 | |
| 96 buffer[len] = '\0'; | |
| 97 | |
| 98 return 0; | |
| 99 } |