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