Remove -s option from lttng load command
[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 /*
41 * get_session_name
42 *
43 * Return allocated string with the session name found in the config
44 * directory.
45 */
46 char *get_session_name(void)
47 {
48 char *path, *session_name = NULL;
49
50 /* Get path to config file */
51 path = utils_get_home_dir();
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) {
59 goto error;
60 }
61
62 DBG2("Config file path found: %s", path);
63 DBG("Session name found: %s", session_name);
64 return session_name;
65
66 error:
67 return NULL;
68 }
69
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 */
76 void 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 }
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 */
95 void 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 }
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)
117 static inline
118 unsigned 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)
133 static inline
134 unsigned 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
149 static __attribute__((unused))
150 unsigned 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
186 static __attribute__((unused))
187 unsigned 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
217 static
218 unsigned 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 */
231 int 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 */
243 int 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 */
255 int get_count_order_ulong(unsigned long x)
256 {
257 if (!x)
258 return -1;
259
260 return fls_ulong(x - 1);
261 }
262
263 const 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 }
284
285 /*
286 * Spawn a lttng relayd daemon by forking and execv.
287 */
288 int 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
327 end:
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 */
336 int 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");
344 ret = -1;
345 goto error_socket;
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");
353 ret = -1;
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
370 error:
371 if (close(fd) < 0) {
372 perror("close relayd fd");
373 }
374 error_socket:
375 return ret;
376 }
This page took 0.037237 seconds and 4 git commands to generate.