Fix: use max between index and nbmem for syscall table realloc
[lttng-tools.git] / src / bin / lttng / utils.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <signal.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28
29 #include <common/error.h>
30 #include <common/utils.h>
31
32 #include "conf.h"
33 #include "utils.h"
34 #include "command.h"
35
36 static const char *str_kernel = "Kernel";
37 static const char *str_ust = "UST";
38 static const char *str_jul = "JUL";
39 static const char *str_log4j = "LOG4J";
40
41 /*
42 * get_session_name
43 *
44 * Return allocated string with the session name found in the config
45 * directory.
46 */
47 char *get_session_name(void)
48 {
49 char *path, *session_name = NULL;
50
51 /* Get path to config file */
52 path = utils_get_home_dir();
53 if (path == NULL) {
54 goto error;
55 }
56
57 /* Get session name from config */
58 session_name = config_read_session_name(path);
59 if (session_name == NULL) {
60 goto error;
61 }
62
63 DBG2("Config file path found: %s", path);
64 DBG("Session name found: %s", session_name);
65 return session_name;
66
67 error:
68 return NULL;
69 }
70
71 /*
72 * list_commands
73 *
74 * List commands line by line. This is mostly for bash auto completion and to
75 * avoid difficult parsing.
76 */
77 void list_commands(struct cmd_struct *commands, FILE *ofp)
78 {
79 int i = 0;
80 struct cmd_struct *cmd = NULL;
81
82 cmd = &commands[i];
83 while (cmd->name != NULL) {
84 fprintf(ofp, "%s\n", cmd->name);
85 i++;
86 cmd = &commands[i];
87 }
88 }
89
90 /*
91 * list_cmd_options
92 *
93 * Prints a simple list of the options available to a command. This is intended
94 * to be easily parsed for bash completion.
95 */
96 void list_cmd_options(FILE *ofp, struct poptOption *options)
97 {
98 int i;
99 struct poptOption *option = NULL;
100
101 for (i = 0; options[i].longName != NULL; i++) {
102 option = &options[i];
103
104 fprintf(ofp, "--%s\n", option->longName);
105
106 if (isprint(option->shortName)) {
107 fprintf(ofp, "-%c\n", option->shortName);
108 }
109 }
110 }
111
112 /*
113 * fls: returns the position of the most significant bit.
114 * Returns 0 if no bit is set, else returns the position of the most
115 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
116 */
117 #if defined(__i386) || defined(__x86_64)
118 static inline
119 unsigned int fls_u32(uint32_t x)
120 {
121 int r;
122
123 asm("bsrl %1,%0\n\t"
124 "jnz 1f\n\t"
125 "movl $-1,%0\n\t"
126 "1:\n\t"
127 : "=r" (r) : "rm" (x));
128 return r + 1;
129 }
130 #define HAS_FLS_U32
131 #endif
132
133 #if defined(__x86_64)
134 static inline
135 unsigned int fls_u64(uint64_t x)
136 {
137 long r;
138
139 asm("bsrq %1,%0\n\t"
140 "jnz 1f\n\t"
141 "movq $-1,%0\n\t"
142 "1:\n\t"
143 : "=r" (r) : "rm" (x));
144 return r + 1;
145 }
146 #define HAS_FLS_U64
147 #endif
148
149 #ifndef HAS_FLS_U64
150 static __attribute__((unused))
151 unsigned int fls_u64(uint64_t x)
152 {
153 unsigned int r = 64;
154
155 if (!x)
156 return 0;
157
158 if (!(x & 0xFFFFFFFF00000000ULL)) {
159 x <<= 32;
160 r -= 32;
161 }
162 if (!(x & 0xFFFF000000000000ULL)) {
163 x <<= 16;
164 r -= 16;
165 }
166 if (!(x & 0xFF00000000000000ULL)) {
167 x <<= 8;
168 r -= 8;
169 }
170 if (!(x & 0xF000000000000000ULL)) {
171 x <<= 4;
172 r -= 4;
173 }
174 if (!(x & 0xC000000000000000ULL)) {
175 x <<= 2;
176 r -= 2;
177 }
178 if (!(x & 0x8000000000000000ULL)) {
179 x <<= 1;
180 r -= 1;
181 }
182 return r;
183 }
184 #endif
185
186 #ifndef HAS_FLS_U32
187 static __attribute__((unused))
188 unsigned int fls_u32(uint32_t x)
189 {
190 unsigned int r = 32;
191
192 if (!x)
193 return 0;
194 if (!(x & 0xFFFF0000U)) {
195 x <<= 16;
196 r -= 16;
197 }
198 if (!(x & 0xFF000000U)) {
199 x <<= 8;
200 r -= 8;
201 }
202 if (!(x & 0xF0000000U)) {
203 x <<= 4;
204 r -= 4;
205 }
206 if (!(x & 0xC0000000U)) {
207 x <<= 2;
208 r -= 2;
209 }
210 if (!(x & 0x80000000U)) {
211 x <<= 1;
212 r -= 1;
213 }
214 return r;
215 }
216 #endif
217
218 static
219 unsigned int fls_ulong(unsigned long x)
220 {
221 #if (CAA_BITS_PER_LONG == 32)
222 return fls_u32(x);
223 #else
224 return fls_u64(x);
225 #endif
226 }
227
228 /*
229 * Return the minimum order for which x <= (1UL << order).
230 * Return -1 if x is 0.
231 */
232 int get_count_order_u32(uint32_t x)
233 {
234 if (!x)
235 return -1;
236
237 return fls_u32(x - 1);
238 }
239
240 /*
241 * Return the minimum order for which x <= (1UL << order).
242 * Return -1 if x is 0.
243 */
244 int get_count_order_u64(uint64_t x)
245 {
246 if (!x)
247 return -1;
248
249 return fls_u64(x - 1);
250 }
251
252 /*
253 * Return the minimum order for which x <= (1UL << order).
254 * Return -1 if x is 0.
255 */
256 int get_count_order_ulong(unsigned long x)
257 {
258 if (!x)
259 return -1;
260
261 return fls_ulong(x - 1);
262 }
263
264 const char *get_domain_str(enum lttng_domain_type domain)
265 {
266 const char *str_dom;
267
268 switch (domain) {
269 case LTTNG_DOMAIN_KERNEL:
270 str_dom = str_kernel;
271 break;
272 case LTTNG_DOMAIN_UST:
273 str_dom = str_ust;
274 break;
275 case LTTNG_DOMAIN_JUL:
276 str_dom = str_jul;
277 break;
278 case LTTNG_DOMAIN_LOG4J:
279 str_dom = str_log4j;
280 break;
281 default:
282 /* Should not have an unknown domain or else define it. */
283 assert(0);
284 }
285
286 return str_dom;
287 }
288
289 /*
290 * Spawn a lttng relayd daemon by forking and execv.
291 */
292 int spawn_relayd(const char *pathname, int port)
293 {
294 int ret = 0;
295 pid_t pid;
296 char url[255];
297
298 if (!port) {
299 port = DEFAULT_NETWORK_VIEWER_PORT;
300 }
301
302 ret = snprintf(url, sizeof(url), "tcp://localhost:%d", port);
303 if (ret < 0) {
304 goto end;
305 }
306
307 MSG("Spawning a relayd daemon");
308 pid = fork();
309 if (pid == 0) {
310 /*
311 * Spawn session daemon and tell
312 * it to signal us when ready.
313 */
314 execlp(pathname, "lttng-relayd", "-L", url, NULL);
315 /* execlp only returns if error happened */
316 if (errno == ENOENT) {
317 ERR("No relayd found. Use --relayd-path.");
318 } else {
319 perror("execlp");
320 }
321 kill(getppid(), SIGTERM); /* wake parent */
322 exit(EXIT_FAILURE);
323 } else if (pid > 0) {
324 goto end;
325 } else {
326 perror("fork");
327 ret = -1;
328 goto end;
329 }
330
331 end:
332 return ret;
333 }
334
335 /*
336 * Check if relayd is alive.
337 *
338 * Return 1 if found else 0 if NOT found. Negative value on error.
339 */
340 int check_relayd(void)
341 {
342 int ret, fd;
343 struct sockaddr_in sin;
344
345 fd = socket(AF_INET, SOCK_STREAM, 0);
346 if (fd < 0) {
347 perror("socket check relayd");
348 ret = -1;
349 goto error_socket;
350 }
351
352 sin.sin_family = AF_INET;
353 sin.sin_port = htons(DEFAULT_NETWORK_VIEWER_PORT);
354 ret = inet_pton(sin.sin_family, "127.0.0.1", &sin.sin_addr);
355 if (ret < 1) {
356 perror("inet_pton check relayd");
357 ret = -1;
358 goto error;
359 }
360
361 /*
362 * A successful connect means the relayd exists thus returning 0 else a
363 * negative value means it does NOT exists.
364 */
365 ret = connect(fd, &sin, sizeof(sin));
366 if (ret < 0) {
367 /* Not found. */
368 ret = 0;
369 } else {
370 /* Already spawned. */
371 ret = 1;
372 }
373
374 error:
375 if (close(fd) < 0) {
376 perror("close relayd fd");
377 }
378 error_socket:
379 return ret;
380 }
This page took 0.038564 seconds and 4 git commands to generate.