2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
29 #include <common/error.h>
30 #include <common/utils.h>
36 static const char *str_kernel
= "Kernel";
37 static const char *str_ust
= "UST";
38 static const char *str_jul
= "JUL";
39 static const char *str_log4j
= "LOG4J";
42 char *_get_session_name(int quiet
)
44 char *path
, *session_name
= NULL
;
46 /* Get path to config file */
47 path
= utils_get_home_dir();
52 /* Get session name from config */
53 session_name
= quiet
? config_read_session_name_quiet(path
) :
54 config_read_session_name(path
);
55 if (session_name
== NULL
) {
59 DBG2("Config file path found: %s", path
);
60 DBG("Session name found: %s", session_name
);
70 * Return allocated string with the session name found in the config
73 char *get_session_name(void)
75 return _get_session_name(0);
79 * get_session_name_quiet (no warnings/errors emitted)
81 * Return allocated string with the session name found in the config
84 char *get_session_name_quiet(void)
86 return _get_session_name(1);
92 * List commands line by line. This is mostly for bash auto completion and to
93 * avoid difficult parsing.
95 void list_commands(struct cmd_struct
*commands
, FILE *ofp
)
98 struct cmd_struct
*cmd
= NULL
;
101 while (cmd
->name
!= NULL
) {
102 fprintf(ofp
, "%s\n", cmd
->name
);
111 * Prints a simple list of the options available to a command. This is intended
112 * to be easily parsed for bash completion.
114 void list_cmd_options(FILE *ofp
, struct poptOption
*options
)
117 struct poptOption
*option
= NULL
;
119 for (i
= 0; options
[i
].longName
!= NULL
; i
++) {
120 option
= &options
[i
];
122 fprintf(ofp
, "--%s\n", option
->longName
);
124 if (isprint(option
->shortName
)) {
125 fprintf(ofp
, "-%c\n", option
->shortName
);
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).
135 #if defined(__i386) || defined(__x86_64)
137 unsigned int fls_u32(uint32_t x
)
145 : "=r" (r
) : "rm" (x
));
151 #if defined(__x86_64)
153 unsigned int fls_u64(uint64_t x
)
161 : "=r" (r
) : "rm" (x
));
168 static __attribute__((unused
))
169 unsigned int fls_u64(uint64_t x
)
176 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
180 if (!(x
& 0xFFFF000000000000ULL
)) {
184 if (!(x
& 0xFF00000000000000ULL
)) {
188 if (!(x
& 0xF000000000000000ULL
)) {
192 if (!(x
& 0xC000000000000000ULL
)) {
196 if (!(x
& 0x8000000000000000ULL
)) {
205 static __attribute__((unused
))
206 unsigned int fls_u32(uint32_t x
)
212 if (!(x
& 0xFFFF0000U
)) {
216 if (!(x
& 0xFF000000U
)) {
220 if (!(x
& 0xF0000000U
)) {
224 if (!(x
& 0xC0000000U
)) {
228 if (!(x
& 0x80000000U
)) {
237 unsigned int fls_ulong(unsigned long x
)
239 #if (CAA_BITS_PER_LONG == 32)
247 * Return the minimum order for which x <= (1UL << order).
248 * Return -1 if x is 0.
250 int get_count_order_u32(uint32_t x
)
255 return fls_u32(x
- 1);
259 * Return the minimum order for which x <= (1UL << order).
260 * Return -1 if x is 0.
262 int get_count_order_u64(uint64_t x
)
267 return fls_u64(x
- 1);
271 * Return the minimum order for which x <= (1UL << order).
272 * Return -1 if x is 0.
274 int get_count_order_ulong(unsigned long x
)
279 return fls_ulong(x
- 1);
282 const char *get_domain_str(enum lttng_domain_type domain
)
287 case LTTNG_DOMAIN_KERNEL
:
288 str_dom
= str_kernel
;
290 case LTTNG_DOMAIN_UST
:
293 case LTTNG_DOMAIN_JUL
:
296 case LTTNG_DOMAIN_LOG4J
:
300 /* Should not have an unknown domain or else define it. */
308 * Spawn a lttng relayd daemon by forking and execv.
310 int spawn_relayd(const char *pathname
, int port
)
317 port
= DEFAULT_NETWORK_VIEWER_PORT
;
320 ret
= snprintf(url
, sizeof(url
), "tcp://localhost:%d", port
);
325 MSG("Spawning a relayd daemon");
329 * Spawn session daemon and tell
330 * it to signal us when ready.
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.");
339 kill(getppid(), SIGTERM
); /* wake parent */
341 } else if (pid
> 0) {
354 * Check if relayd is alive.
356 * Return 1 if found else 0 if NOT found. Negative value on error.
358 int check_relayd(void)
361 struct sockaddr_in sin
;
363 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
365 perror("socket check relayd");
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
);
374 perror("inet_pton check relayd");
380 * A successful connect means the relayd exists thus returning 0 else a
381 * negative value means it does NOT exists.
383 ret
= connect(fd
, &sin
, sizeof(sin
));
388 /* Already spawned. */
394 perror("close relayd fd");