Fix: destroy session removes the default config file
[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
40 static
41 char *_get_session_name(int quiet)
42 {
43 char *path, *session_name = NULL;
44
45 /* Get path to config file */
46 path = utils_get_home_dir();
47 if (path == NULL) {
48 goto error;
49 }
50
51 /* Get session name from config */
52 session_name = quiet ? config_read_session_name_quiet(path) :
53 config_read_session_name(path);
54 if (session_name == NULL) {
55 goto error;
56 }
57
58 DBG2("Config file path found: %s", path);
59 DBG("Session name found: %s", session_name);
60 return session_name;
61
62 error:
63 return NULL;
64 }
65
66 /*
67 * get_session_name
68 *
69 * Return allocated string with the session name found in the config
70 * directory.
71 */
72 char *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 */
83 char *get_session_name_quiet(void)
84 {
85 return _get_session_name(1);
86 }
87
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 */
94 void 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 }
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 */
113 void 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 }
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)
135 static inline
136 unsigned 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)
151 static inline
152 unsigned 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
167 static __attribute__((unused))
168 unsigned 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
204 static __attribute__((unused))
205 unsigned 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
235 static
236 unsigned 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 */
249 int 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 */
261 int 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 */
273 int get_count_order_ulong(unsigned long x)
274 {
275 if (!x)
276 return -1;
277
278 return fls_ulong(x - 1);
279 }
280
281 const 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 }
302
303 /*
304 * Spawn a lttng relayd daemon by forking and execv.
305 */
306 int 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
345 end:
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 */
354 int 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");
362 ret = -1;
363 goto error_socket;
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");
371 ret = -1;
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
388 error:
389 if (close(fd) < 0) {
390 perror("close relayd fd");
391 }
392 error_socket:
393 return ret;
394 }
This page took 0.05281 seconds and 4 git commands to generate.