1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. |
2 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
3 | * of this software and associated documentation files (the "Software"), to |
4 | * deal in the Software without restriction, including without limitation the |
5 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
6 | * sell copies of the Software, and to permit persons to whom the Software is |
7 | * furnished to do so, subject to the following conditions: |
8 | * |
9 | * The above copyright notice and this permission notice shall be included in |
10 | * all copies or substantial portions of the Software. |
11 | * |
12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
13 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
14 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
15 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
16 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
17 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
18 | * IN THE SOFTWARE. |
19 | */ |
20 | |
21 | #include "uv.h" |
22 | #include "internal.h" |
23 | |
24 | #include <stdlib.h> |
25 | #include <string.h> |
26 | |
27 | extern void uv__set_process_title(const char* title); |
28 | |
29 | static uv_mutex_t process_title_mutex; |
30 | static uv_once_t process_title_mutex_once = UV_ONCE_INIT; |
31 | static void* args_mem; |
32 | |
33 | static struct { |
34 | char* str; |
35 | size_t len; |
36 | } process_title; |
37 | |
38 | |
39 | static void init_process_title_mutex_once(void) { |
40 | uv_mutex_init(&process_title_mutex); |
41 | } |
42 | |
43 | |
44 | char** uv_setup_args(int argc, char** argv) { |
45 | char** new_argv; |
46 | size_t size; |
47 | char* s; |
48 | int i; |
49 | |
50 | if (argc <= 0) |
51 | return argv; |
52 | |
53 | /* Calculate how much memory we need for the argv strings. */ |
54 | size = 0; |
55 | for (i = 0; i < argc; i++) |
56 | size += strlen(argv[i]) + 1; |
57 | |
58 | #if defined(__MVS__) |
59 | /* argv is not adjacent. So just use argv[0] */ |
60 | process_title.str = argv[0]; |
61 | process_title.len = strlen(argv[0]); |
62 | #else |
63 | process_title.str = argv[0]; |
64 | process_title.len = argv[argc - 1] + strlen(argv[argc - 1]) - argv[0]; |
65 | assert(process_title.len + 1 == size); /* argv memory should be adjacent. */ |
66 | #endif |
67 | |
68 | /* Add space for the argv pointers. */ |
69 | size += (argc + 1) * sizeof(char*); |
70 | |
71 | new_argv = uv__malloc(size); |
72 | if (new_argv == NULL) |
73 | return argv; |
74 | args_mem = new_argv; |
75 | |
76 | /* Copy over the strings and set up the pointer table. */ |
77 | s = (char*) &new_argv[argc + 1]; |
78 | for (i = 0; i < argc; i++) { |
79 | size = strlen(argv[i]) + 1; |
80 | memcpy(s, argv[i], size); |
81 | new_argv[i] = s; |
82 | s += size; |
83 | } |
84 | new_argv[i] = NULL; |
85 | |
86 | return new_argv; |
87 | } |
88 | |
89 | |
90 | int uv_set_process_title(const char* title) { |
91 | uv_once(&process_title_mutex_once, init_process_title_mutex_once); |
92 | uv_mutex_lock(&process_title_mutex); |
93 | |
94 | if (process_title.len != 0) { |
95 | /* No need to terminate, byte after is always '\0'. */ |
96 | strncpy(process_title.str, title, process_title.len); |
97 | uv__set_process_title(title); |
98 | } |
99 | |
100 | uv_mutex_unlock(&process_title_mutex); |
101 | |
102 | return 0; |
103 | } |
104 | |
105 | |
106 | int uv_get_process_title(char* buffer, size_t size) { |
107 | if (buffer == NULL || size == 0) |
108 | return UV_EINVAL; |
109 | |
110 | uv_once(&process_title_mutex_once, init_process_title_mutex_once); |
111 | uv_mutex_lock(&process_title_mutex); |
112 | |
113 | if (size <= process_title.len) { |
114 | uv_mutex_unlock(&process_title_mutex); |
115 | return UV_ENOBUFS; |
116 | } |
117 | |
118 | if (process_title.len != 0) |
119 | memcpy(buffer, process_title.str, process_title.len + 1); |
120 | |
121 | buffer[process_title.len] = '\0'; |
122 | |
123 | uv_mutex_unlock(&process_title_mutex); |
124 | |
125 | return 0; |
126 | } |
127 | |
128 | |
129 | UV_DESTRUCTOR(static void free_args_mem(void)) { |
130 | uv__free(args_mem); /* Keep valgrind happy. */ |
131 | args_mem = NULL; |
132 | } |
133 | |