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