xref: /netbsd-src/external/mit/libuv/dist/src/unix/proctitle.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
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 struct 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 
33 extern void uv__set_process_title(const char* title);
34 
35 static uv_mutex_t process_title_mutex;
36 static uv_once_t process_title_mutex_once = UV_ONCE_INIT;
37 static struct uv__process_title process_title;
38 static void* args_mem;
39 
40 
41 static void init_process_title_mutex_once(void) {
42   uv_mutex_init(&process_title_mutex);
43 }
44 
45 
46 char** 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 
99 int uv_set_process_title(const char* title) {
100   struct uv__process_title* pt;
101   size_t len;
102 
103   pt = &process_title;
104   len = strlen(title);
105 
106   uv_once(&process_title_mutex_once, init_process_title_mutex_once);
107   uv_mutex_lock(&process_title_mutex);
108 
109   if (len >= pt->cap) {
110     len = 0;
111     if (pt->cap > 0)
112       len = pt->cap - 1;
113   }
114 
115   memcpy(pt->str, title, len);
116   memset(pt->str + len, '\0', pt->cap - len);
117   pt->len = len;
118 
119   uv_mutex_unlock(&process_title_mutex);
120 
121   return 0;
122 }
123 
124 
125 int uv_get_process_title(char* buffer, size_t size) {
126   if (buffer == NULL || size == 0)
127     return UV_EINVAL;
128 
129   uv_once(&process_title_mutex_once, init_process_title_mutex_once);
130   uv_mutex_lock(&process_title_mutex);
131 
132   if (size <= process_title.len) {
133     uv_mutex_unlock(&process_title_mutex);
134     return UV_ENOBUFS;
135   }
136 
137   if (process_title.len != 0)
138     memcpy(buffer, process_title.str, process_title.len + 1);
139 
140   buffer[process_title.len] = '\0';
141 
142   uv_mutex_unlock(&process_title_mutex);
143 
144   return 0;
145 }
146 
147 
148 void uv__process_title_cleanup(void) {
149   uv__free(args_mem);  /* Keep valgrind happy. */
150   args_mem = NULL;
151 }
152