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