|
160
|
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 <stdlib.h>
|
|
|
26 #include <string.h>
|
|
|
27
|
|
|
28 static uv_mutex_t process_title_mutex;
|
|
|
29 static uv_once_t process_title_mutex_once = UV_ONCE_INIT;
|
|
|
30 static char* process_title = NULL;
|
|
|
31 static void* args_mem = NULL;
|
|
|
32
|
|
|
33
|
|
|
34 static void init_process_title_mutex_once(void) {
|
|
|
35 uv_mutex_init(&process_title_mutex);
|
|
|
36 }
|
|
|
37
|
|
|
38
|
|
|
39 char** uv_setup_args(int argc, char** argv) {
|
|
|
40 char** new_argv;
|
|
|
41 size_t size;
|
|
|
42 char* s;
|
|
|
43 int i;
|
|
|
44
|
|
|
45 if (argc <= 0)
|
|
|
46 return argv;
|
|
|
47
|
|
|
48 /* Calculate how much memory we need for the argv strings. */
|
|
|
49 size = 0;
|
|
|
50 for (i = 0; i < argc; i++)
|
|
|
51 size += strlen(argv[i]) + 1;
|
|
|
52
|
|
|
53 /* Add space for the argv pointers. */
|
|
|
54 size += (argc + 1) * sizeof(char*);
|
|
|
55
|
|
|
56 new_argv = uv__malloc(size);
|
|
|
57 if (new_argv == NULL)
|
|
|
58 return argv;
|
|
|
59
|
|
|
60 /* Copy over the strings and set up the pointer table. */
|
|
|
61 s = (char*) &new_argv[argc + 1];
|
|
|
62 for (i = 0; i < argc; i++) {
|
|
|
63 size = strlen(argv[i]) + 1;
|
|
|
64 memcpy(s, argv[i], size);
|
|
|
65 new_argv[i] = s;
|
|
|
66 s += size;
|
|
|
67 }
|
|
|
68 new_argv[i] = NULL;
|
|
|
69
|
|
|
70 args_mem = new_argv;
|
|
|
71 process_title = uv__strdup(argv[0]);
|
|
|
72
|
|
|
73 return new_argv;
|
|
|
74 }
|
|
|
75
|
|
|
76
|
|
|
77 int uv_set_process_title(const char* title) {
|
|
|
78 char* new_title;
|
|
|
79
|
|
|
80 /* If uv_setup_args wasn't called or failed, we can't continue. */
|
|
|
81 if (args_mem == NULL)
|
|
|
82 return UV_ENOBUFS;
|
|
|
83
|
|
|
84 /* We cannot free this pointer when libuv shuts down,
|
|
|
85 * the process may still be using it.
|
|
|
86 */
|
|
|
87 new_title = uv__strdup(title);
|
|
|
88 if (new_title == NULL)
|
|
|
89 return UV_ENOMEM;
|
|
|
90
|
|
|
91 uv_once(&process_title_mutex_once, init_process_title_mutex_once);
|
|
|
92 uv_mutex_lock(&process_title_mutex);
|
|
|
93
|
|
|
94 if (process_title != NULL)
|
|
|
95 uv__free(process_title);
|
|
|
96
|
|
|
97 process_title = new_title;
|
|
|
98
|
|
|
99 uv_mutex_unlock(&process_title_mutex);
|
|
|
100
|
|
|
101 return 0;
|
|
|
102 }
|
|
|
103
|
|
|
104
|
|
|
105 int uv_get_process_title(char* buffer, size_t size) {
|
|
|
106 size_t len;
|
|
|
107
|
|
|
108 if (buffer == NULL || size == 0)
|
|
|
109 return UV_EINVAL;
|
|
|
110
|
|
|
111 /* If uv_setup_args wasn't called or failed, we can't continue. */
|
|
|
112 if (args_mem == NULL || process_title == NULL)
|
|
|
113 return UV_ENOBUFS;
|
|
|
114
|
|
|
115 uv_once(&process_title_mutex_once, init_process_title_mutex_once);
|
|
|
116 uv_mutex_lock(&process_title_mutex);
|
|
|
117
|
|
|
118 len = strlen(process_title);
|
|
|
119
|
|
|
120 if (size <= len) {
|
|
|
121 uv_mutex_unlock(&process_title_mutex);
|
|
|
122 return UV_ENOBUFS;
|
|
|
123 }
|
|
|
124
|
|
|
125 strcpy(buffer, process_title);
|
|
|
126
|
|
|
127 uv_mutex_unlock(&process_title_mutex);
|
|
|
128
|
|
|
129 return 0;
|
|
|
130 }
|
|
|
131
|
|
|
132
|
|
|
133 void uv__process_title_cleanup(void) {
|
|
|
134 uv__free(args_mem); /* Keep valgrind happy. */
|
|
|
135 args_mem = NULL;
|
|
|
136 }
|