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