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_platform_init(void); |
28 | extern void uv__set_process_title(const char* title); |
29 | |
30 | static uv_mutex_t process_title_mutex; |
31 | static uv_once_t process_title_mutex_once = UV_ONCE_INIT; |
32 | static void* args_mem; |
33 | |
34 | static struct { |
35 | char* str; |
36 | size_t len; |
37 | } process_title; |
38 | |
39 | |
40 | static void init_process_title_mutex_once(void) { |
41 | uv_mutex_init(&process_title_mutex); |
42 | #ifdef __APPLE__ |
43 | uv__set_process_title_platform_init(); |
44 | #endif |
45 | } |
46 | |
47 | |
48 | char** uv_setup_args(int argc, char** argv) { |
49 | char** new_argv; |
50 | size_t size; |
51 | char* s; |
52 | int i; |
53 | |
54 | if (argc <= 0) |
55 | return argv; |
56 | |
57 | /* Calculate how much memory we need for the argv strings. */ |
58 | size = 0; |
59 | for (i = 0; i < argc; i++) |
60 | size += strlen(argv[i]) + 1; |
61 | |
62 | #if defined(__MVS__) |
63 | /* argv is not adjacent. So just use argv[0] */ |
64 | process_title.str = argv[0]; |
65 | process_title.len = strlen(argv[0]); |
66 | #else |
67 | process_title.str = argv[0]; |
68 | process_title.len = argv[argc - 1] + strlen(argv[argc - 1]) - argv[0]; |
69 | assert(process_title.len + 1 == size); /* argv memory should be adjacent. */ |
70 | #endif |
71 | |
72 | /* Add space for the argv pointers. */ |
73 | size += (argc + 1) * sizeof(char*); |
74 | |
75 | new_argv = uv__malloc(size); |
76 | if (new_argv == NULL) |
77 | return argv; |
78 | args_mem = new_argv; |
79 | |
80 | /* Copy over the strings and set up the pointer table. */ |
81 | s = (char*) &new_argv[argc + 1]; |
82 | for (i = 0; i < argc; i++) { |
83 | size = strlen(argv[i]) + 1; |
84 | memcpy(s, argv[i], size); |
85 | new_argv[i] = s; |
86 | s += size; |
87 | } |
88 | new_argv[i] = NULL; |
89 | |
90 | return new_argv; |
91 | } |
92 | |
93 | |
94 | int uv_set_process_title(const char* title) { |
95 | uv_once(&process_title_mutex_once, init_process_title_mutex_once); |
96 | uv_mutex_lock(&process_title_mutex); |
97 | |
98 | if (process_title.len != 0) { |
99 | /* No need to terminate, byte after is always '\0'. */ |
100 | strncpy(process_title.str, title, process_title.len); |
101 | uv__set_process_title(title); |
102 | } |
103 | |
104 | uv_mutex_unlock(&process_title_mutex); |
105 | |
106 | return 0; |
107 | } |
108 | |
109 | |
110 | int uv_get_process_title(char* buffer, size_t size) { |
111 | if (buffer == NULL || size == 0) |
112 | return UV_EINVAL; |
113 | |
114 | uv_once(&process_title_mutex_once, init_process_title_mutex_once); |
115 | uv_mutex_lock(&process_title_mutex); |
116 | |
117 | if (size <= process_title.len) { |
118 | uv_mutex_unlock(&process_title_mutex); |
119 | return UV_ENOBUFS; |
120 | } |
121 | |
122 | if (process_title.len != 0) |
123 | memcpy(buffer, process_title.str, process_title.len + 1); |
124 | |
125 | buffer[process_title.len] = '\0'; |
126 | |
127 | uv_mutex_unlock(&process_title_mutex); |
128 | |
129 | return 0; |
130 | } |
131 | |
132 | |
133 | UV_DESTRUCTOR(static void free_args_mem(void)) { |
134 | uv__free(args_mem); /* Keep valgrind happy. */ |
135 | args_mem = NULL; |
136 | } |
137 | |