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