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