Rizin
unix-like reverse engineering framework and cli tools
proctitle.c
Go to the documentation of this file.
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 
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 
37 static struct uv__process_title process_title;
38 static void* args_mem;
39 
40 
41 static void init_process_title_mutex_once(void) {
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  /* 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 
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 
124 
125  return 0;
126 }
127 
128 
129 int uv_get_process_title(char* buffer, size_t size) {
130  if (buffer == NULL || size == 0)
131  return UV_EINVAL;
132 
133  /* If uv_setup_args wasn't called or failed, we can't continue. */
134  if (args_mem == NULL)
135  return UV_ENOBUFS;
136 
139 
140  if (size <= process_title.len) {
142  return UV_ENOBUFS;
143  }
144 
145  if (process_title.len != 0)
147 
148  buffer[process_title.len] = '\0';
149 
151 
152  return 0;
153 }
154 
155 
157  uv__free(args_mem); /* Keep valgrind happy. */
158  args_mem = NULL;
159 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
voidpf void uLong size
Definition: ioapi.h:138
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
void uv__set_process_title(const char *title)
static void init_process_title_mutex_once(void)
Definition: proctitle.c:41
static void * args_mem
Definition: proctitle.c:38
void uv__process_title_cleanup(void)
Definition: proctitle.c:156
static uv_once_t process_title_mutex_once
Definition: proctitle.c:36
char ** uv_setup_args(int argc, char **argv)
Definition: proctitle.c:46
static struct uv__process_title process_title
Definition: proctitle.c:37
int uv_set_process_title(const char *title)
Definition: proctitle.c:99
int uv_get_process_title(char *buffer, size_t size)
Definition: proctitle.c:129
static uv_mutex_t process_title_mutex
Definition: proctitle.c:35
static RzSocket * s
Definition: rtr.c:28
Definition: buffer.h:15
uv_loop_t * loop
Definition: main.c:7
pthread_mutex_t uv_mutex_t
Definition: unix.h:137
pthread_once_t uv_once_t
Definition: unix.h:135
#define UV_ONCE_INIT
Definition: unix.h:133
void * uv__malloc(size_t size)
Definition: uv-common.c:75
void uv__free(void *ptr)
Definition: uv-common.c:81
UV_EXTERN void uv_mutex_lock(uv_mutex_t *handle)
Definition: thread.c:330
UV_EXTERN void uv_once(uv_once_t *guard, void(*callback)(void))
Definition: thread.c:419
UV_EXTERN void uv_mutex_unlock(uv_mutex_t *handle)
Definition: thread.c:350
UV_EXTERN int uv_mutex_init(uv_mutex_t *handle)
Definition: thread.c:282