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
27struct uv__process_title {
28 char* str;
29 size_t len; /* Length of the current process title. */
30 size_t cap; /* Maximum capacity. Computed once in uv_setup_args(). */
31};
32
33extern void uv__set_process_title(const char* title);
34
35static uv_mutex_t process_title_mutex;
36static uv_once_t process_title_mutex_once = UV_ONCE_INIT;
37static struct uv__process_title process_title;
38static void* args_mem;
39
40
41static void init_process_title_mutex_once(void) {
42 uv_mutex_init(&process_title_mutex);
43}
44
45
46char** uv_setup_args(int argc, char** argv) {
47 struct uv__process_title pt;
48 char** new_argv;
49 size_t size;
50 char* s;
51 int i;
52
53 if (argc <= 0)
54 return argv;
55
56 pt.str = argv[0];
57 pt.len = strlen(argv[0]);
58 pt.cap = pt.len + 1;
59
60 /* Calculate how much memory we need for the argv strings. */
61 size = pt.cap;
62 for (i = 1; i < argc; i++)
63 size += strlen(argv[i]) + 1;
64
65 /* Add space for the argv pointers. */
66 size += (argc + 1) * sizeof(char*);
67
68 new_argv = uv__malloc(size);
69 if (new_argv == NULL)
70 return argv;
71
72 /* Copy over the strings and set up the pointer table. */
73 i = 0;
74 s = (char*) &new_argv[argc + 1];
75 size = pt.cap;
76 goto loop;
77
78 for (/* empty */; i < argc; i++) {
79 size = strlen(argv[i]) + 1;
80 loop:
81 memcpy(s, argv[i], size);
82 new_argv[i] = s;
83 s += size;
84 }
85 new_argv[i] = NULL;
86
87 /* argv is not adjacent on z/os, we use just argv[0] on that platform. */
88#ifndef __MVS__
89 pt.cap = argv[i - 1] + size - argv[0];
90#endif
91
92 args_mem = new_argv;
93 process_title = pt;
94
95 return new_argv;
96}
97
98
99int uv_set_process_title(const char* title) {
100 struct uv__process_title* pt;
101 size_t len;
102
103 /* If uv_setup_args wasn't called or failed, we can't continue. */
104 if (args_mem == NULL)
105 return UV_ENOBUFS;
106
107 pt = &process_title;
108 len = strlen(title);
109
110 uv_once(&process_title_mutex_once, init_process_title_mutex_once);
111 uv_mutex_lock(&process_title_mutex);
112
113 if (len >= pt->cap) {
114 len = 0;
115 if (pt->cap > 0)
116 len = pt->cap - 1;
117 }
118
119 memcpy(pt->str, title, len);
120 memset(pt->str + len, '\0', pt->cap - len);
121 pt->len = len;
122 uv__set_process_title(pt->str);
123
124 uv_mutex_unlock(&process_title_mutex);
125
126 return 0;
127}
128
129
130int uv_get_process_title(char* buffer, size_t size) {
131 if (buffer == NULL || size == 0)
132 return UV_EINVAL;
133
134 /* If uv_setup_args wasn't called or failed, we can't continue. */
135 if (args_mem == NULL)
136 return UV_ENOBUFS;
137
138 uv_once(&process_title_mutex_once, init_process_title_mutex_once);
139 uv_mutex_lock(&process_title_mutex);
140
141 if (size <= process_title.len) {
142 uv_mutex_unlock(&process_title_mutex);
143 return UV_ENOBUFS;
144 }
145
146 if (process_title.len != 0)
147 memcpy(buffer, process_title.str, process_title.len + 1);
148
149 buffer[process_title.len] = '\0';
150
151 uv_mutex_unlock(&process_title_mutex);
152
153 return 0;
154}
155
156
157void uv__process_title_cleanup(void) {
158 uv__free(args_mem); /* Keep valgrind happy. */
159 args_mem = NULL;
160}
161