Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
fac6795d DG |
3 | * |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
82a3637f DG |
6 | * as published by the Free Software Foundation; only version 2 |
7 | * of the License. | |
91d76f53 | 8 | * |
fac6795d DG |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
91d76f53 | 13 | * |
fac6795d DG |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <fcntl.h> | |
21 | #include <getopt.h> | |
22 | #include <grp.h> | |
23 | #include <limits.h> | |
7a485870 | 24 | #include <poll.h> |
fac6795d | 25 | #include <pthread.h> |
8c0faa1d | 26 | #include <semaphore.h> |
fac6795d DG |
27 | #include <signal.h> |
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <sys/ipc.h> | |
b73401da | 32 | #include <sys/mount.h> |
fac6795d DG |
33 | #include <sys/shm.h> |
34 | #include <sys/socket.h> | |
35 | #include <sys/stat.h> | |
36 | #include <sys/types.h> | |
f3ed775e DG |
37 | #include <sys/time.h> |
38 | #include <sys/resource.h> | |
fac6795d DG |
39 | #include <unistd.h> |
40 | ||
41 | #include <urcu/list.h> /* URCU list library (-lurcu) */ | |
5b97ec60 | 42 | #include <lttng/lttng.h> |
fac6795d DG |
43 | |
44 | #include "liblttsessiondcomm.h" | |
45 | #include "ltt-sessiond.h" | |
75462a81 | 46 | #include "lttngerr.h" |
20fe2104 | 47 | #include "kernel-ctl.h" |
9e78d6ae | 48 | #include "ust-ctl.h" |
5b74c7b1 | 49 | #include "session.h" |
91d76f53 | 50 | #include "traceable-app.h" |
5dc18550 | 51 | #include "lttng-kconsumerd.h" |
8e68d1c8 | 52 | #include "utils.h" |
fac6795d | 53 | |
5e16da05 MD |
54 | /* |
55 | * TODO: | |
56 | * teardown: signal SIGTERM handler -> write into pipe. Threads waits | |
57 | * with epoll on pipe and on other pipes/sockets for commands. Main | |
58 | * simply waits on pthread join. | |
59 | */ | |
60 | ||
75462a81 | 61 | /* Const values */ |
686204ab | 62 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
64a23ac4 | 63 | const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP; |
686204ab MD |
64 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
65 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; | |
66 | ||
fac6795d | 67 | /* Variables */ |
1d4b027a DG |
68 | int opt_verbose; /* Not static for lttngerr.h */ |
69 | int opt_quiet; /* Not static for lttngerr.h */ | |
fac6795d DG |
70 | const char *progname; |
71 | const char *opt_tracing_group; | |
5b8719f5 | 72 | static int opt_sig_parent; |
fac6795d DG |
73 | static int opt_daemon; |
74 | static int is_root; /* Set to 1 if the daemon is running as root */ | |
1d4b027a DG |
75 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
76 | static pid_t kconsumerd_pid; | |
7a485870 | 77 | static struct pollfd *kernel_pollfd; |
fac6795d | 78 | |
d6f42150 DG |
79 | static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */ |
80 | static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */ | |
81 | static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */ | |
82 | static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */ | |
fac6795d | 83 | |
1d4b027a | 84 | /* Sockets and FDs */ |
d6f42150 DG |
85 | static int client_sock; |
86 | static int apps_sock; | |
87 | static int kconsumerd_err_sock; | |
8c0faa1d | 88 | static int kconsumerd_cmd_sock; |
20fe2104 | 89 | static int kernel_tracer_fd; |
7a485870 | 90 | static int kernel_poll_pipe[2]; |
1d4b027a | 91 | |
273ea72c DG |
92 | /* |
93 | * Quit pipe for all threads. This permits a single cancellation point | |
94 | * for all threads when receiving an event on the pipe. | |
95 | */ | |
96 | static int thread_quit_pipe[2]; | |
97 | ||
1d4b027a | 98 | /* Pthread, Mutexes and Semaphores */ |
8c0faa1d | 99 | static pthread_t kconsumerd_thread; |
1d4b027a DG |
100 | static pthread_t apps_thread; |
101 | static pthread_t client_thread; | |
7a485870 | 102 | static pthread_t kernel_thread; |
8c0faa1d DG |
103 | static sem_t kconsumerd_sem; |
104 | ||
1d4b027a | 105 | static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */ |
fac6795d | 106 | |
b5541356 DG |
107 | /* |
108 | * Pointer initialized before thread creation. | |
109 | * | |
110 | * This points to the tracing session list containing the session count and a | |
111 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
112 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f DG |
113 | * |
114 | * The lock is nested inside the structure: session_list_ptr->lock. | |
b5541356 DG |
115 | */ |
116 | static struct ltt_session_list *session_list_ptr; | |
117 | ||
273ea72c DG |
118 | /* |
119 | * Init quit pipe. | |
120 | * | |
121 | * Return -1 on error or 0 if all pipes are created. | |
122 | */ | |
123 | static int init_thread_quit_pipe(void) | |
124 | { | |
125 | int ret; | |
126 | ||
127 | ret = pipe2(thread_quit_pipe, O_CLOEXEC); | |
128 | if (ret < 0) { | |
129 | perror("thread quit pipe"); | |
130 | goto error; | |
131 | } | |
132 | ||
133 | error: | |
134 | return ret; | |
135 | } | |
136 | ||
fac6795d | 137 | /* |
1d4b027a | 138 | * teardown_kernel_session |
fac6795d | 139 | * |
b5541356 DG |
140 | * Complete teardown of a kernel session. This free all data structure related |
141 | * to a kernel session and update counter. | |
fac6795d | 142 | */ |
1d4b027a | 143 | static void teardown_kernel_session(struct ltt_session *session) |
fac6795d | 144 | { |
1d4b027a DG |
145 | if (session->kernel_session != NULL) { |
146 | DBG("Tearing down kernel session"); | |
c363b55d | 147 | trace_destroy_kernel_session(session->kernel_session); |
1d4b027a DG |
148 | /* Extra precaution */ |
149 | session->kernel_session = NULL; | |
fac6795d | 150 | } |
fac6795d DG |
151 | } |
152 | ||
153 | /* | |
273ea72c | 154 | * Cleanup the daemon |
fac6795d | 155 | */ |
1d4b027a | 156 | static void cleanup() |
fac6795d | 157 | { |
1d4b027a DG |
158 | int ret; |
159 | char *cmd; | |
160 | struct ltt_session *sess; | |
fac6795d | 161 | |
1d4b027a | 162 | DBG("Cleaning up"); |
e07ae692 | 163 | |
1d4b027a | 164 | /* <fun> */ |
273ea72c DG |
165 | MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm" |
166 | "Matthew, BEET driven development works!%c[%dm", | |
167 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); | |
1d4b027a | 168 | /* </fun> */ |
fac6795d | 169 | |
1d4b027a DG |
170 | /* Stopping all threads */ |
171 | DBG("Terminating all threads"); | |
273ea72c DG |
172 | close(thread_quit_pipe[0]); |
173 | close(thread_quit_pipe[1]); | |
fac6795d | 174 | |
1d4b027a DG |
175 | DBG("Removing %s directory", LTTNG_RUNDIR); |
176 | ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR); | |
177 | if (ret < 0) { | |
178 | ERR("asprintf failed. Something is really wrong!"); | |
179 | } | |
5461b305 | 180 | |
1d4b027a DG |
181 | /* Remove lttng run directory */ |
182 | ret = system(cmd); | |
183 | if (ret < 0) { | |
184 | ERR("Unable to clean " LTTNG_RUNDIR); | |
185 | } | |
5461b305 | 186 | |
1d4b027a | 187 | DBG("Cleaning up all session"); |
fac6795d | 188 | |
b5541356 | 189 | /* Destroy session list mutex */ |
273ea72c DG |
190 | if (session_list_ptr != NULL) { |
191 | pthread_mutex_destroy(&session_list_ptr->lock); | |
192 | ||
193 | /* Cleanup ALL session */ | |
194 | cds_list_for_each_entry(sess, &session_list_ptr->head, list) { | |
195 | teardown_kernel_session(sess); | |
196 | // TODO complete session cleanup (including UST) | |
197 | } | |
198 | } | |
199 | ||
200 | pthread_mutex_destroy(&kconsumerd_pid_mutex); | |
b5541356 | 201 | |
f3ed775e | 202 | DBG("Closing kernel fd"); |
1d4b027a | 203 | close(kernel_tracer_fd); |
fac6795d DG |
204 | } |
205 | ||
e065084a DG |
206 | /* |
207 | * send_unix_sock | |
208 | * | |
209 | * Send data on a unix socket using the liblttsessiondcomm API. | |
210 | * | |
211 | * Return lttcomm error code. | |
212 | */ | |
213 | static int send_unix_sock(int sock, void *buf, size_t len) | |
214 | { | |
215 | /* Check valid length */ | |
216 | if (len <= 0) { | |
217 | return -1; | |
218 | } | |
219 | ||
220 | return lttcomm_send_unix_sock(sock, buf, len); | |
221 | } | |
222 | ||
5461b305 DG |
223 | /* |
224 | * clean_command_ctx | |
225 | * | |
226 | * Free memory of a command context structure. | |
227 | */ | |
a2fb29a5 | 228 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 229 | { |
a2fb29a5 DG |
230 | DBG("Clean command context structure"); |
231 | if (*cmd_ctx) { | |
232 | if ((*cmd_ctx)->llm) { | |
233 | free((*cmd_ctx)->llm); | |
5461b305 | 234 | } |
a2fb29a5 DG |
235 | if ((*cmd_ctx)->lsm) { |
236 | free((*cmd_ctx)->lsm); | |
5461b305 | 237 | } |
a2fb29a5 DG |
238 | free(*cmd_ctx); |
239 | *cmd_ctx = NULL; | |
5461b305 DG |
240 | } |
241 | } | |
242 | ||
f3ed775e | 243 | /* |
7a485870 | 244 | * send_kconsumerd_channel_fds |
f3ed775e | 245 | * |
7a485870 | 246 | * Send all stream fds of kernel channel to the consumer. |
f3ed775e | 247 | */ |
7a485870 | 248 | static int send_kconsumerd_channel_fds(int sock, struct ltt_kernel_channel *channel) |
f3ed775e DG |
249 | { |
250 | int ret; | |
251 | size_t nb_fd; | |
252 | struct ltt_kernel_stream *stream; | |
f3ed775e DG |
253 | struct lttcomm_kconsumerd_header lkh; |
254 | struct lttcomm_kconsumerd_msg lkm; | |
255 | ||
7a485870 DG |
256 | DBG("Sending fds of channel %s to kernel consumer", channel->channel->name); |
257 | ||
258 | nb_fd = channel->stream_count; | |
f3ed775e DG |
259 | |
260 | /* Setup header */ | |
7a485870 | 261 | lkh.payload_size = nb_fd * sizeof(struct lttcomm_kconsumerd_msg); |
f3ed775e DG |
262 | lkh.cmd_type = ADD_STREAM; |
263 | ||
264 | DBG("Sending kconsumerd header"); | |
265 | ||
266 | ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header)); | |
267 | if (ret < 0) { | |
268 | perror("send kconsumerd header"); | |
269 | goto error; | |
270 | } | |
271 | ||
7a485870 DG |
272 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { |
273 | if (stream->fd != 0) { | |
f3ed775e DG |
274 | lkm.fd = stream->fd; |
275 | lkm.state = stream->state; | |
7a485870 | 276 | lkm.max_sb_size = channel->channel->attr.subbuf_size; |
f3ed775e DG |
277 | strncpy(lkm.path_name, stream->pathname, PATH_MAX); |
278 | ||
279 | DBG("Sending fd %d to kconsumerd", lkm.fd); | |
280 | ||
281 | ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm)); | |
282 | if (ret < 0) { | |
283 | perror("send kconsumerd fd"); | |
284 | goto error; | |
285 | } | |
286 | } | |
287 | } | |
288 | ||
7a485870 | 289 | DBG("Kconsumerd channel fds sent"); |
f3ed775e DG |
290 | |
291 | return 0; | |
292 | ||
293 | error: | |
294 | return ret; | |
295 | } | |
296 | ||
297 | /* | |
7a485870 | 298 | * send_kconsumerd_fds |
f3ed775e | 299 | * |
7a485870 | 300 | * Send all stream fds of the kernel session to the consumer. |
f3ed775e | 301 | */ |
7a485870 | 302 | static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session) |
f3ed775e DG |
303 | { |
304 | int ret; | |
305 | struct ltt_kernel_channel *chan; | |
7a485870 DG |
306 | struct lttcomm_kconsumerd_header lkh; |
307 | struct lttcomm_kconsumerd_msg lkm; | |
308 | ||
309 | /* Setup header */ | |
310 | lkh.payload_size = sizeof(struct lttcomm_kconsumerd_msg); | |
311 | lkh.cmd_type = ADD_STREAM; | |
312 | ||
313 | DBG("Sending kconsumerd header for metadata"); | |
314 | ||
315 | ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header)); | |
316 | if (ret < 0) { | |
317 | perror("send kconsumerd header"); | |
318 | goto error; | |
319 | } | |
320 | ||
321 | DBG("Sending metadata stream fd"); | |
322 | ||
323 | if (session->metadata_stream_fd != 0) { | |
324 | /* Send metadata stream fd first */ | |
325 | lkm.fd = session->metadata_stream_fd; | |
326 | lkm.state = ACTIVE_FD; | |
327 | lkm.max_sb_size = session->metadata->conf->attr.subbuf_size; | |
328 | strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX); | |
329 | ||
330 | ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm)); | |
331 | if (ret < 0) { | |
332 | perror("send kconsumerd fd"); | |
333 | goto error; | |
334 | } | |
335 | } | |
f3ed775e | 336 | |
f3ed775e | 337 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
7a485870 | 338 | ret = send_kconsumerd_channel_fds(sock, chan); |
f3ed775e | 339 | if (ret < 0) { |
7a485870 | 340 | goto error; |
f3ed775e DG |
341 | } |
342 | } | |
343 | ||
7a485870 DG |
344 | DBG("Kconsumerd fds (metadata and channel streams) sent"); |
345 | ||
f3ed775e DG |
346 | return 0; |
347 | ||
348 | error: | |
349 | return ret; | |
350 | } | |
351 | ||
97b1a726 | 352 | #ifdef DISABLED |
fac6795d | 353 | /* |
471d1693 | 354 | * ust_connect_app |
fac6795d DG |
355 | * |
356 | * Return a socket connected to the libust communication socket | |
357 | * of the application identified by the pid. | |
379473d2 DG |
358 | * |
359 | * If the pid is not found in the traceable list, | |
360 | * return -1 to indicate error. | |
fac6795d | 361 | */ |
471d1693 | 362 | static int ust_connect_app(pid_t pid) |
fac6795d | 363 | { |
91d76f53 DG |
364 | int sock; |
365 | struct ltt_traceable_app *lta; | |
379473d2 | 366 | |
e07ae692 DG |
367 | DBG("Connect to application pid %d", pid); |
368 | ||
91d76f53 DG |
369 | lta = find_app_by_pid(pid); |
370 | if (lta == NULL) { | |
371 | /* App not found */ | |
7442b2ba | 372 | DBG("Application pid %d not found", pid); |
379473d2 DG |
373 | return -1; |
374 | } | |
fac6795d | 375 | |
91d76f53 | 376 | sock = ustctl_connect_pid(lta->pid); |
fac6795d | 377 | if (sock < 0) { |
62d3069f | 378 | ERR("Fail connecting to the PID %d", pid); |
fac6795d DG |
379 | } |
380 | ||
381 | return sock; | |
382 | } | |
97b1a726 | 383 | #endif /* DISABLED */ |
fac6795d DG |
384 | |
385 | /* | |
386 | * notify_apps | |
387 | * | |
388 | * Notify apps by writing 42 to a named pipe using name. | |
389 | * Every applications waiting for a ltt-sessiond will be notified | |
390 | * and re-register automatically to the session daemon. | |
391 | * | |
392 | * Return open or write error value. | |
393 | */ | |
394 | static int notify_apps(const char *name) | |
395 | { | |
396 | int fd; | |
397 | int ret = -1; | |
398 | ||
e07ae692 DG |
399 | DBG("Notify the global application pipe"); |
400 | ||
fac6795d DG |
401 | /* Try opening the global pipe */ |
402 | fd = open(name, O_WRONLY); | |
403 | if (fd < 0) { | |
404 | goto error; | |
405 | } | |
406 | ||
407 | /* Notify by writing on the pipe */ | |
408 | ret = write(fd, "42", 2); | |
409 | if (ret < 0) { | |
410 | perror("write"); | |
411 | } | |
412 | ||
413 | error: | |
414 | return ret; | |
415 | } | |
416 | ||
e065084a | 417 | /* |
5461b305 | 418 | * setup_lttng_msg |
ca95a216 | 419 | * |
5461b305 DG |
420 | * Setup the outgoing data buffer for the response (llm) by allocating the |
421 | * right amount of memory and copying the original information from the lsm | |
422 | * structure. | |
ca95a216 DG |
423 | * |
424 | * Return total size of the buffer pointed by buf. | |
425 | */ | |
5461b305 | 426 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 427 | { |
f3ed775e | 428 | int ret, buf_size; |
ca95a216 | 429 | |
f3ed775e | 430 | buf_size = size; |
5461b305 DG |
431 | |
432 | cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size); | |
433 | if (cmd_ctx->llm == NULL) { | |
ca95a216 | 434 | perror("malloc"); |
5461b305 | 435 | ret = -ENOMEM; |
ca95a216 DG |
436 | goto error; |
437 | } | |
438 | ||
5461b305 DG |
439 | /* Copy common data */ |
440 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
441 | cmd_ctx->llm->pid = cmd_ctx->lsm->pid; | |
5461b305 | 442 | |
5461b305 DG |
443 | cmd_ctx->llm->data_size = size; |
444 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
445 | ||
ca95a216 DG |
446 | return buf_size; |
447 | ||
448 | error: | |
449 | return ret; | |
450 | } | |
451 | ||
7a485870 DG |
452 | /* |
453 | * update_kernel_pollfd | |
454 | * | |
455 | * Update the kernel pollfd set of all channel fd available over | |
456 | * all tracing session. Add the wakeup pipe at the end of the set. | |
457 | */ | |
458 | static int update_kernel_pollfd(void) | |
459 | { | |
460 | int i = 0; | |
273ea72c DG |
461 | /* |
462 | * The wakup pipe and the quit pipe are needed so the number of fds starts | |
463 | * at 2 for those pipes. | |
464 | */ | |
465 | unsigned int nb_fd = 2; | |
7a485870 DG |
466 | struct ltt_session *session; |
467 | struct ltt_kernel_channel *channel; | |
468 | ||
469 | DBG("Updating kernel_pollfd"); | |
470 | ||
471 | /* Get the number of channel of all kernel session */ | |
6c9cc2ab | 472 | lock_session_list(); |
b5541356 DG |
473 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
474 | lock_session(session); | |
7a485870 | 475 | if (session->kernel_session == NULL) { |
b5541356 | 476 | unlock_session(session); |
7a485870 DG |
477 | continue; |
478 | } | |
479 | nb_fd += session->kernel_session->channel_count; | |
b5541356 | 480 | unlock_session(session); |
7a485870 DG |
481 | } |
482 | ||
483 | DBG("Resizing kernel_pollfd to size %d", nb_fd); | |
484 | ||
485 | kernel_pollfd = realloc(kernel_pollfd, nb_fd * sizeof(struct pollfd)); | |
486 | if (kernel_pollfd == NULL) { | |
487 | perror("malloc kernel_pollfd"); | |
488 | goto error; | |
489 | } | |
490 | ||
b5541356 DG |
491 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
492 | lock_session(session); | |
7a485870 | 493 | if (session->kernel_session == NULL) { |
b5541356 | 494 | unlock_session(session); |
7a485870 DG |
495 | continue; |
496 | } | |
497 | if (i >= nb_fd) { | |
498 | ERR("To much channel for kernel_pollfd size"); | |
b5541356 | 499 | unlock_session(session); |
7a485870 DG |
500 | break; |
501 | } | |
502 | cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) { | |
503 | kernel_pollfd[i].fd = channel->fd; | |
504 | kernel_pollfd[i].events = POLLIN | POLLRDNORM; | |
505 | i++; | |
506 | } | |
b5541356 | 507 | unlock_session(session); |
7a485870 | 508 | } |
6c9cc2ab | 509 | unlock_session_list(); |
7a485870 DG |
510 | |
511 | /* Adding wake up pipe */ | |
273ea72c DG |
512 | kernel_pollfd[nb_fd - 2].fd = kernel_poll_pipe[0]; |
513 | kernel_pollfd[nb_fd - 2].events = POLLIN; | |
514 | ||
515 | /* Adding the quit pipe */ | |
516 | kernel_pollfd[nb_fd - 1].fd = thread_quit_pipe[0]; | |
7a485870 DG |
517 | |
518 | return nb_fd; | |
519 | ||
520 | error: | |
6c9cc2ab | 521 | unlock_session_list(); |
7a485870 DG |
522 | return -1; |
523 | } | |
524 | ||
525 | /* | |
526 | * update_kernel_stream | |
527 | * | |
528 | * Find the channel fd from 'fd' over all tracing session. When found, check | |
529 | * for new channel stream and send those stream fds to the kernel consumer. | |
530 | * | |
531 | * Useful for CPU hotplug feature. | |
532 | */ | |
533 | static int update_kernel_stream(int fd) | |
534 | { | |
535 | int ret = 0; | |
536 | struct ltt_session *session; | |
537 | struct ltt_kernel_channel *channel; | |
538 | ||
539 | DBG("Updating kernel streams for channel fd %d", fd); | |
540 | ||
6c9cc2ab | 541 | lock_session_list(); |
b5541356 DG |
542 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
543 | lock_session(session); | |
7a485870 | 544 | if (session->kernel_session == NULL) { |
b5541356 | 545 | unlock_session(session); |
7a485870 DG |
546 | continue; |
547 | } | |
548 | cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) { | |
549 | if (channel->fd == fd) { | |
550 | DBG("Channel found, updating kernel streams"); | |
551 | ret = kernel_open_channel_stream(channel); | |
552 | if (ret < 0) { | |
553 | goto end; | |
554 | } | |
555 | /* | |
556 | * Have we already sent fds to the consumer? If yes, it means that | |
557 | * tracing is started so it is safe to send our updated stream fds. | |
558 | */ | |
559 | if (session->kernel_session->kconsumer_fds_sent == 1) { | |
560 | ret = send_kconsumerd_channel_fds(kconsumerd_cmd_sock, channel); | |
561 | if (ret < 0) { | |
562 | goto end; | |
563 | } | |
564 | } | |
565 | goto end; | |
566 | } | |
567 | } | |
b5541356 | 568 | unlock_session(session); |
7a485870 DG |
569 | } |
570 | ||
571 | end: | |
6c9cc2ab | 572 | unlock_session_list(); |
b5541356 DG |
573 | if (session) { |
574 | unlock_session(session); | |
575 | } | |
7a485870 DG |
576 | return ret; |
577 | } | |
578 | ||
579 | /* | |
580 | * thread_manage_kernel | |
581 | * | |
582 | * This thread manage event coming from the kernel. | |
583 | * | |
584 | * Features supported in this thread: | |
585 | * -) CPU Hotplug | |
586 | */ | |
587 | static void *thread_manage_kernel(void *data) | |
588 | { | |
589 | int ret, i, nb_fd = 0; | |
590 | char tmp; | |
591 | int update_poll_flag = 1; | |
592 | ||
593 | DBG("Thread manage kernel started"); | |
594 | ||
595 | while (1) { | |
596 | if (update_poll_flag == 1) { | |
597 | nb_fd = update_kernel_pollfd(); | |
598 | if (nb_fd < 0) { | |
599 | goto error; | |
600 | } | |
601 | update_poll_flag = 0; | |
602 | } | |
603 | ||
604 | DBG("Polling on %d fds", nb_fd); | |
605 | ||
606 | /* Poll infinite value of time */ | |
607 | ret = poll(kernel_pollfd, nb_fd, -1); | |
608 | if (ret < 0) { | |
609 | perror("poll kernel thread"); | |
610 | goto error; | |
611 | } else if (ret == 0) { | |
612 | /* Should not happen since timeout is infinite */ | |
613 | continue; | |
614 | } | |
615 | ||
273ea72c DG |
616 | /* Thread quit pipe has been closed. Killing thread. */ |
617 | if (kernel_pollfd[nb_fd - 1].revents == POLLNVAL) { | |
618 | goto error; | |
619 | } | |
620 | ||
7a485870 DG |
621 | DBG("Kernel poll event triggered"); |
622 | ||
623 | /* | |
624 | * Check if the wake up pipe was triggered. If so, the kernel_pollfd | |
625 | * must be updated. | |
626 | */ | |
273ea72c | 627 | switch (kernel_pollfd[nb_fd - 2].revents) { |
b5541356 | 628 | case POLLIN: |
7a485870 DG |
629 | ret = read(kernel_poll_pipe[0], &tmp, 1); |
630 | update_poll_flag = 1; | |
631 | continue; | |
b5541356 DG |
632 | case POLLERR: |
633 | goto error; | |
634 | default: | |
635 | break; | |
7a485870 DG |
636 | } |
637 | ||
638 | for (i = 0; i < nb_fd; i++) { | |
639 | switch (kernel_pollfd[i].revents) { | |
640 | /* | |
641 | * New CPU detected by the kernel. Adding kernel stream to kernel | |
642 | * session and updating the kernel consumer | |
643 | */ | |
644 | case POLLIN | POLLRDNORM: | |
645 | ret = update_kernel_stream(kernel_pollfd[i].fd); | |
646 | if (ret < 0) { | |
647 | continue; | |
648 | } | |
649 | break; | |
650 | } | |
651 | } | |
652 | } | |
653 | ||
654 | error: | |
655 | DBG("Kernel thread dying"); | |
656 | if (kernel_pollfd) { | |
657 | free(kernel_pollfd); | |
658 | } | |
273ea72c DG |
659 | |
660 | close(kernel_poll_pipe[0]); | |
661 | close(kernel_poll_pipe[1]); | |
7a485870 DG |
662 | return NULL; |
663 | } | |
664 | ||
1d4b027a DG |
665 | /* |
666 | * thread_manage_kconsumerd | |
667 | * | |
668 | * This thread manage the kconsumerd error sent | |
669 | * back to the session daemon. | |
670 | */ | |
671 | static void *thread_manage_kconsumerd(void *data) | |
672 | { | |
273ea72c | 673 | int sock = 0, ret; |
1d4b027a | 674 | enum lttcomm_return_code code; |
273ea72c | 675 | struct pollfd pollfd[2]; |
1d4b027a DG |
676 | |
677 | DBG("[thread] Manage kconsumerd started"); | |
678 | ||
679 | ret = lttcomm_listen_unix_sock(kconsumerd_err_sock); | |
680 | if (ret < 0) { | |
681 | goto error; | |
682 | } | |
683 | ||
273ea72c DG |
684 | /* First fd is always the quit pipe */ |
685 | pollfd[0].fd = thread_quit_pipe[0]; | |
686 | ||
687 | /* Apps socket */ | |
688 | pollfd[1].fd = kconsumerd_err_sock; | |
689 | pollfd[1].events = POLLIN; | |
690 | ||
691 | /* Inifinite blocking call, waiting for transmission */ | |
692 | ret = poll(pollfd, 2, -1); | |
693 | if (ret < 0) { | |
694 | perror("poll kconsumerd thread"); | |
695 | goto error; | |
696 | } | |
697 | ||
698 | /* Thread quit pipe has been closed. Killing thread. */ | |
699 | if (pollfd[0].revents == POLLNVAL) { | |
700 | goto error; | |
701 | } else if (pollfd[1].revents == POLLERR) { | |
702 | ERR("Kconsumerd err socket poll error"); | |
703 | goto error; | |
704 | } | |
705 | ||
1d4b027a DG |
706 | sock = lttcomm_accept_unix_sock(kconsumerd_err_sock); |
707 | if (sock < 0) { | |
708 | goto error; | |
709 | } | |
710 | ||
712ea556 | 711 | /* Getting status code from kconsumerd */ |
1d4b027a DG |
712 | ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code)); |
713 | if (ret <= 0) { | |
714 | goto error; | |
715 | } | |
716 | ||
717 | if (code == KCONSUMERD_COMMAND_SOCK_READY) { | |
718 | kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path); | |
719 | if (kconsumerd_cmd_sock < 0) { | |
712ea556 | 720 | sem_post(&kconsumerd_sem); |
1d4b027a DG |
721 | perror("kconsumerd connect"); |
722 | goto error; | |
723 | } | |
724 | /* Signal condition to tell that the kconsumerd is ready */ | |
725 | sem_post(&kconsumerd_sem); | |
726 | DBG("Kconsumerd command socket ready"); | |
727 | } else { | |
6f61d021 | 728 | DBG("Kconsumerd error when waiting for SOCK_READY : %s", |
1d4b027a DG |
729 | lttcomm_get_readable_code(-code)); |
730 | goto error; | |
731 | } | |
732 | ||
712ea556 DG |
733 | /* Wait for any kconsumerd error */ |
734 | ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code)); | |
735 | if (ret <= 0) { | |
736 | ERR("Kconsumerd closed the command socket"); | |
737 | goto error; | |
6f61d021 | 738 | } |
1d4b027a | 739 | |
712ea556 DG |
740 | ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code)); |
741 | ||
1d4b027a | 742 | error: |
6f61d021 | 743 | DBG("Kconsumerd thread dying"); |
273ea72c DG |
744 | if (kconsumerd_err_sock) { |
745 | close(kconsumerd_err_sock); | |
746 | } | |
747 | if (kconsumerd_cmd_sock) { | |
748 | close(kconsumerd_cmd_sock); | |
749 | } | |
750 | if (sock) { | |
751 | close(sock); | |
752 | } | |
753 | ||
754 | unlink(kconsumerd_err_unix_sock_path); | |
755 | unlink(kconsumerd_cmd_unix_sock_path); | |
756 | ||
757 | kconsumerd_pid = 0; | |
1d4b027a DG |
758 | return NULL; |
759 | } | |
760 | ||
761 | /* | |
762 | * thread_manage_apps | |
763 | * | |
764 | * This thread manage the application socket communication | |
765 | */ | |
766 | static void *thread_manage_apps(void *data) | |
767 | { | |
273ea72c DG |
768 | int sock = 0, ret; |
769 | struct pollfd pollfd[2]; | |
1d4b027a DG |
770 | |
771 | /* TODO: Something more elegant is needed but fine for now */ | |
772 | /* FIXME: change all types to either uint8_t, uint32_t, uint64_t | |
773 | * for 32-bit vs 64-bit compat processes. */ | |
774 | /* replicate in ust with version number */ | |
775 | struct { | |
776 | int reg; /* 1:register, 0:unregister */ | |
777 | pid_t pid; | |
778 | uid_t uid; | |
779 | } reg_msg; | |
780 | ||
781 | DBG("[thread] Manage apps started"); | |
782 | ||
783 | ret = lttcomm_listen_unix_sock(apps_sock); | |
784 | if (ret < 0) { | |
785 | goto error; | |
786 | } | |
787 | ||
273ea72c DG |
788 | /* First fd is always the quit pipe */ |
789 | pollfd[0].fd = thread_quit_pipe[0]; | |
790 | ||
791 | /* Apps socket */ | |
792 | pollfd[1].fd = apps_sock; | |
793 | pollfd[1].events = POLLIN; | |
794 | ||
1d4b027a DG |
795 | /* Notify all applications to register */ |
796 | notify_apps(default_global_apps_pipe); | |
797 | ||
798 | while (1) { | |
799 | DBG("Accepting application registration"); | |
273ea72c DG |
800 | |
801 | /* Inifinite blocking call, waiting for transmission */ | |
802 | ret = poll(pollfd, 2, -1); | |
803 | if (ret < 0) { | |
804 | perror("poll apps thread"); | |
805 | goto error; | |
806 | } | |
807 | ||
808 | /* Thread quit pipe has been closed. Killing thread. */ | |
809 | if (pollfd[0].revents == POLLNVAL) { | |
810 | goto error; | |
811 | } else if (pollfd[1].revents == POLLERR) { | |
812 | ERR("Apps socket poll error"); | |
813 | goto error; | |
814 | } | |
815 | ||
1d4b027a DG |
816 | sock = lttcomm_accept_unix_sock(apps_sock); |
817 | if (sock < 0) { | |
818 | goto error; | |
819 | } | |
820 | ||
273ea72c DG |
821 | /* |
822 | * Basic recv here to handle the very simple data | |
1d4b027a DG |
823 | * that the libust send to register (reg_msg). |
824 | */ | |
825 | ret = recv(sock, ®_msg, sizeof(reg_msg), 0); | |
826 | if (ret < 0) { | |
827 | perror("recv"); | |
828 | continue; | |
829 | } | |
830 | ||
831 | /* Add application to the global traceable list */ | |
832 | if (reg_msg.reg == 1) { | |
833 | /* Registering */ | |
834 | ret = register_traceable_app(reg_msg.pid, reg_msg.uid); | |
835 | if (ret < 0) { | |
836 | /* register_traceable_app only return an error with | |
837 | * ENOMEM. At this point, we better stop everything. | |
838 | */ | |
839 | goto error; | |
840 | } | |
841 | } else { | |
842 | /* Unregistering */ | |
843 | unregister_traceable_app(reg_msg.pid); | |
844 | } | |
845 | } | |
846 | ||
847 | error: | |
273ea72c DG |
848 | DBG("Apps thread dying"); |
849 | if (apps_sock) { | |
850 | close(apps_sock); | |
851 | } | |
852 | if (sock) { | |
853 | close(sock); | |
854 | } | |
1d4b027a | 855 | |
273ea72c | 856 | unlink(apps_unix_sock_path); |
1d4b027a DG |
857 | return NULL; |
858 | } | |
859 | ||
8c0faa1d | 860 | /* |
693bd40b | 861 | * spawn_kconsumerd_thread |
8c0faa1d DG |
862 | * |
863 | * Start the thread_manage_kconsumerd. This must be done after a kconsumerd | |
864 | * exec or it will fails. | |
865 | */ | |
693bd40b | 866 | static int spawn_kconsumerd_thread(void) |
8c0faa1d DG |
867 | { |
868 | int ret; | |
869 | ||
870 | /* Setup semaphore */ | |
871 | sem_init(&kconsumerd_sem, 0, 0); | |
872 | ||
1d4b027a | 873 | ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL); |
8c0faa1d DG |
874 | if (ret != 0) { |
875 | perror("pthread_create kconsumerd"); | |
876 | goto error; | |
877 | } | |
878 | ||
693bd40b | 879 | /* Wait for the kconsumerd thread to be ready */ |
8c0faa1d DG |
880 | sem_wait(&kconsumerd_sem); |
881 | ||
712ea556 DG |
882 | if (kconsumerd_pid == 0) { |
883 | ERR("Kconsumerd did not start"); | |
884 | goto error; | |
885 | } | |
886 | ||
8c0faa1d DG |
887 | return 0; |
888 | ||
889 | error: | |
712ea556 | 890 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
8c0faa1d DG |
891 | return ret; |
892 | } | |
893 | ||
894 | /* | |
693bd40b | 895 | * spawn_kconsumerd |
8c0faa1d | 896 | * |
693bd40b DG |
897 | * Fork and exec a kernel consumer daemon (kconsumerd). |
898 | * | |
899 | * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel | |
900 | * file descriptor using the libkernelctl or kernel-ctl functions. So, a | |
901 | * kernel consumer MUST only be spawned before creating a kernel session. | |
8c0faa1d DG |
902 | * |
903 | * Return pid if successful else -1. | |
904 | */ | |
693bd40b | 905 | static pid_t spawn_kconsumerd(void) |
8c0faa1d DG |
906 | { |
907 | int ret; | |
908 | pid_t pid; | |
53086306 | 909 | const char *verbosity; |
8c0faa1d | 910 | |
c49dc785 DG |
911 | DBG("Spawning kconsumerd"); |
912 | ||
8c0faa1d DG |
913 | pid = fork(); |
914 | if (pid == 0) { | |
915 | /* | |
916 | * Exec kconsumerd. | |
917 | */ | |
53086306 DG |
918 | if (opt_verbose > 1) { |
919 | verbosity = "--verbose"; | |
920 | } else { | |
921 | verbosity = "--quiet"; | |
922 | } | |
923 | execl(INSTALL_BIN_PATH "/ltt-kconsumerd", "ltt-kconsumerd", verbosity, NULL); | |
8c0faa1d DG |
924 | if (errno != 0) { |
925 | perror("kernel start consumer exec"); | |
926 | } | |
927 | exit(EXIT_FAILURE); | |
928 | } else if (pid > 0) { | |
929 | ret = pid; | |
930 | goto error; | |
931 | } else { | |
932 | perror("kernel start consumer fork"); | |
933 | ret = -errno; | |
934 | goto error; | |
935 | } | |
936 | ||
937 | error: | |
938 | return ret; | |
939 | } | |
940 | ||
693bd40b DG |
941 | /* |
942 | * start_kconsumerd | |
943 | * | |
944 | * Spawn the kconsumerd daemon and session daemon thread. | |
945 | */ | |
946 | static int start_kconsumerd(void) | |
947 | { | |
948 | int ret; | |
949 | ||
693bd40b | 950 | pthread_mutex_lock(&kconsumerd_pid_mutex); |
c49dc785 | 951 | if (kconsumerd_pid != 0) { |
817153bb | 952 | pthread_mutex_unlock(&kconsumerd_pid_mutex); |
c49dc785 DG |
953 | goto end; |
954 | } | |
693bd40b | 955 | |
c49dc785 DG |
956 | ret = spawn_kconsumerd(); |
957 | if (ret < 0) { | |
958 | ERR("Spawning kconsumerd failed"); | |
959 | ret = LTTCOMM_KERN_CONSUMER_FAIL; | |
960 | pthread_mutex_unlock(&kconsumerd_pid_mutex); | |
961 | goto error; | |
693bd40b | 962 | } |
c49dc785 DG |
963 | |
964 | /* Setting up the global kconsumerd_pid */ | |
965 | kconsumerd_pid = ret; | |
693bd40b DG |
966 | pthread_mutex_unlock(&kconsumerd_pid_mutex); |
967 | ||
6f61d021 DG |
968 | DBG("Kconsumerd pid %d", ret); |
969 | ||
693bd40b | 970 | DBG("Spawning kconsumerd thread"); |
693bd40b DG |
971 | ret = spawn_kconsumerd_thread(); |
972 | if (ret < 0) { | |
973 | ERR("Fatal error spawning kconsumerd thread"); | |
693bd40b DG |
974 | goto error; |
975 | } | |
976 | ||
c49dc785 | 977 | end: |
693bd40b DG |
978 | return 0; |
979 | ||
980 | error: | |
981 | return ret; | |
982 | } | |
983 | ||
b73401da DG |
984 | /* |
985 | * modprobe_kernel_modules | |
986 | */ | |
987 | static int modprobe_kernel_modules(void) | |
988 | { | |
989 | int ret = 0, i = 0; | |
990 | char modprobe[256]; | |
991 | ||
992 | while (kernel_modules_list[i] != NULL) { | |
993 | ret = snprintf(modprobe, sizeof(modprobe), "/sbin/modprobe %s", | |
994 | kernel_modules_list[i]); | |
995 | if (ret < 0) { | |
996 | perror("snprintf modprobe"); | |
997 | goto error; | |
998 | } | |
999 | ret = system(modprobe); | |
1000 | if (ret < 0) { | |
1001 | ERR("Unable to load module %s", kernel_modules_list[i]); | |
1002 | } | |
1003 | DBG("Modprobe successfully %s", kernel_modules_list[i]); | |
1004 | i++; | |
1005 | } | |
1006 | ||
1007 | error: | |
1008 | return ret; | |
1009 | } | |
1010 | ||
1011 | /* | |
1012 | * mount_debugfs | |
1013 | */ | |
1014 | static int mount_debugfs(char *path) | |
1015 | { | |
1016 | int ret; | |
1017 | char *type = "debugfs"; | |
1018 | ||
1019 | ret = mkdir_recursive(path, S_IRWXU | S_IRWXG); | |
1020 | if (ret < 0) { | |
1021 | goto error; | |
1022 | } | |
1023 | ||
1024 | ret = mount(type, path, type, 0, NULL); | |
1025 | if (ret < 0) { | |
1026 | perror("mount debugfs"); | |
1027 | goto error; | |
1028 | } | |
1029 | ||
1030 | DBG("Mounted debugfs successfully at %s", path); | |
1031 | ||
1032 | error: | |
1033 | return ret; | |
1034 | } | |
1035 | ||
8c0faa1d | 1036 | /* |
f3ed775e | 1037 | * init_kernel_tracer |
8c0faa1d | 1038 | * |
f3ed775e | 1039 | * Setup necessary data for kernel tracer action. |
8c0faa1d | 1040 | */ |
f3ed775e | 1041 | static void init_kernel_tracer(void) |
8c0faa1d | 1042 | { |
b73401da DG |
1043 | int ret; |
1044 | char *proc_mounts = "/proc/mounts"; | |
1045 | char line[256]; | |
1046 | char *debugfs_path = NULL, *lttng_path; | |
1047 | FILE *fp; | |
1048 | ||
1049 | /* Detect debugfs */ | |
1050 | fp = fopen(proc_mounts, "r"); | |
1051 | if (fp == NULL) { | |
1052 | ERR("Unable to probe %s", proc_mounts); | |
1053 | goto error; | |
1054 | } | |
1055 | ||
1056 | while (fgets(line, sizeof(line), fp) != NULL) { | |
1057 | if (strstr(line, "debugfs") != NULL) { | |
1058 | /* Remove first string */ | |
1059 | strtok(line, " "); | |
1060 | /* Dup string here so we can reuse line later on */ | |
1061 | debugfs_path = strdup(strtok(NULL, " ")); | |
1062 | DBG("Got debugfs path : %s", debugfs_path); | |
1063 | break; | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | fclose(fp); | |
1068 | ||
1069 | /* Mount debugfs if needded */ | |
1070 | if (debugfs_path == NULL) { | |
1071 | ret = asprintf(&debugfs_path, "/mnt/debugfs"); | |
1072 | if (ret < 0) { | |
1073 | perror("asprintf debugfs path"); | |
1074 | goto error; | |
1075 | } | |
1076 | ret = mount_debugfs(debugfs_path); | |
1077 | if (ret < 0) { | |
1078 | goto error; | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | /* Modprobe lttng kernel modules */ | |
1083 | ret = modprobe_kernel_modules(); | |
1084 | if (ret < 0) { | |
1085 | goto error; | |
1086 | } | |
1087 | ||
1088 | /* Setup lttng kernel path */ | |
1089 | ret = asprintf(<tng_path, "%s/lttng", debugfs_path); | |
1090 | if (ret < 0) { | |
1091 | perror("asprintf lttng path"); | |
1092 | goto error; | |
1093 | } | |
1094 | ||
1095 | /* Open debugfs lttng */ | |
1096 | kernel_tracer_fd = open(lttng_path, O_RDWR); | |
f3ed775e | 1097 | if (kernel_tracer_fd < 0) { |
b73401da DG |
1098 | DBG("Failed to open %s", lttng_path); |
1099 | goto error; | |
8c0faa1d DG |
1100 | } |
1101 | ||
b73401da DG |
1102 | free(lttng_path); |
1103 | free(debugfs_path); | |
f3ed775e | 1104 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
b73401da DG |
1105 | return; |
1106 | ||
1107 | error: | |
1108 | if (lttng_path) { | |
1109 | free(lttng_path); | |
1110 | } | |
1111 | if (debugfs_path) { | |
1112 | free(debugfs_path); | |
1113 | } | |
1114 | WARN("No kernel tracer available"); | |
1115 | kernel_tracer_fd = 0; | |
1116 | return; | |
f3ed775e | 1117 | } |
33a2b854 | 1118 | |
f3ed775e DG |
1119 | /* |
1120 | * start_kernel_trace | |
1121 | * | |
1122 | * Start tracing by creating trace directory and sending FDs to the kernel | |
1123 | * consumer. | |
1124 | */ | |
1125 | static int start_kernel_trace(struct ltt_kernel_session *session) | |
1126 | { | |
f40799e8 | 1127 | int ret = 0; |
8c0faa1d | 1128 | |
f3ed775e DG |
1129 | if (session->kconsumer_fds_sent == 0) { |
1130 | ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session); | |
1131 | if (ret < 0) { | |
1132 | ERR("Send kconsumerd fds failed"); | |
1133 | ret = LTTCOMM_KERN_CONSUMER_FAIL; | |
1134 | goto error; | |
1135 | } | |
1d4b027a | 1136 | |
f3ed775e DG |
1137 | session->kconsumer_fds_sent = 1; |
1138 | } | |
1d4b027a DG |
1139 | |
1140 | error: | |
1141 | return ret; | |
8c0faa1d DG |
1142 | } |
1143 | ||
7a485870 DG |
1144 | /* |
1145 | * Notify kernel thread to update it's pollfd. | |
1146 | */ | |
1147 | static int notify_kernel_pollfd(void) | |
1148 | { | |
1149 | int ret; | |
1150 | ||
1151 | /* Inform kernel thread of the new kernel channel */ | |
1152 | ret = write(kernel_poll_pipe[1], "!", 1); | |
1153 | if (ret < 0) { | |
1154 | perror("write kernel poll pipe"); | |
1155 | } | |
1156 | ||
1157 | return ret; | |
1158 | } | |
1159 | ||
8c0faa1d | 1160 | /* |
f3ed775e | 1161 | * init_default_channel |
8c0faa1d | 1162 | * |
f3ed775e | 1163 | * Allocate a channel structure and fill it. |
8c0faa1d | 1164 | */ |
f3ed775e | 1165 | static struct lttng_channel *init_default_channel(void) |
8c0faa1d | 1166 | { |
f3ed775e | 1167 | struct lttng_channel *chan; |
1d4b027a | 1168 | |
f3ed775e DG |
1169 | chan = malloc(sizeof(struct lttng_channel)); |
1170 | if (chan == NULL) { | |
1171 | perror("init channel malloc"); | |
1172 | goto error; | |
8c0faa1d | 1173 | } |
1d4b027a | 1174 | |
f3ed775e DG |
1175 | if (snprintf(chan->name, NAME_MAX, DEFAULT_CHANNEL_NAME) < 0) { |
1176 | perror("snprintf defautl channel name"); | |
1177 | return NULL; | |
1178 | } | |
1179 | ||
1180 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
1181 | chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE; | |
1182 | chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM; | |
1183 | chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
1184 | chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
7d452e12 | 1185 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
1d4b027a DG |
1186 | |
1187 | error: | |
f3ed775e | 1188 | return chan; |
8c0faa1d DG |
1189 | } |
1190 | ||
333f285e | 1191 | /* |
f3ed775e | 1192 | * create_kernel_session |
333f285e | 1193 | * |
f3ed775e | 1194 | * Create a kernel tracer session then create the default channel. |
333f285e | 1195 | */ |
f3ed775e | 1196 | static int create_kernel_session(struct ltt_session *session) |
333f285e | 1197 | { |
f3ed775e | 1198 | int ret; |
f3ed775e DG |
1199 | |
1200 | DBG("Creating kernel session"); | |
1201 | ||
1202 | ret = kernel_create_session(session, kernel_tracer_fd); | |
1203 | if (ret < 0) { | |
1204 | ret = LTTCOMM_KERN_SESS_FAIL; | |
1205 | goto error; | |
333f285e DG |
1206 | } |
1207 | ||
7a485870 DG |
1208 | ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG ); |
1209 | if (ret < 0) { | |
1210 | if (ret != EEXIST) { | |
1211 | ERR("Trace directory creation error"); | |
1212 | goto error; | |
1213 | } | |
1214 | } | |
1215 | ||
f3ed775e DG |
1216 | error: |
1217 | return ret; | |
333f285e DG |
1218 | } |
1219 | ||
6c9cc2ab DG |
1220 | /* |
1221 | * Using the session list, filled a lttng_session array to send back to the | |
1222 | * client for session listing. | |
1223 | * | |
1224 | * The session list lock MUST be acquired before calling this function. Use | |
1225 | * lock_session_list() and unlock_session_list(). | |
1226 | */ | |
1227 | static void list_lttng_sessions(struct lttng_session *sessions) | |
1228 | { | |
1229 | int i = 0; | |
1230 | struct ltt_session *session; | |
1231 | ||
1232 | DBG("Getting all available session"); | |
1233 | /* | |
1234 | * Iterate over session list and append data after the control struct in | |
1235 | * the buffer. | |
1236 | */ | |
1237 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
1238 | strncpy(sessions[i].path, session->path, PATH_MAX); | |
1239 | strncpy(sessions[i].name, session->name, NAME_MAX); | |
1240 | i++; | |
1241 | } | |
1242 | } | |
1243 | ||
fac6795d DG |
1244 | /* |
1245 | * process_client_msg | |
1246 | * | |
5461b305 DG |
1247 | * Process the command requested by the lttng client within the command |
1248 | * context structure. This function make sure that the return structure (llm) | |
1249 | * is set and ready for transmission before returning. | |
fac6795d | 1250 | * |
e065084a | 1251 | * Return any error encountered or 0 for success. |
fac6795d | 1252 | */ |
5461b305 | 1253 | static int process_client_msg(struct command_ctx *cmd_ctx) |
fac6795d | 1254 | { |
f40799e8 | 1255 | int ret = LTTCOMM_OK; |
fac6795d | 1256 | |
5461b305 | 1257 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); |
fac6795d | 1258 | |
f3ed775e | 1259 | /* Listing commands don't need a session */ |
5461b305 | 1260 | switch (cmd_ctx->lsm->cmd_type) { |
5e16da05 MD |
1261 | case LTTNG_CREATE_SESSION: |
1262 | case LTTNG_LIST_SESSIONS: | |
f3ed775e DG |
1263 | case LTTNG_LIST_EVENTS: |
1264 | case LTTNG_KERNEL_LIST_EVENTS: | |
1265 | case LTTNG_LIST_TRACEABLE_APPS: | |
5e16da05 MD |
1266 | break; |
1267 | default: | |
f3ed775e DG |
1268 | DBG("Getting session %s by name", cmd_ctx->lsm->session_name); |
1269 | cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session_name); | |
5461b305 | 1270 | if (cmd_ctx->session == NULL) { |
f3ed775e DG |
1271 | /* If session name not found */ |
1272 | if (cmd_ctx->lsm->session_name != NULL) { | |
1273 | ret = LTTCOMM_SESS_NOT_FOUND; | |
1274 | } else { /* If no session name specified */ | |
1275 | ret = LTTCOMM_SELECT_SESS; | |
1276 | } | |
5461b305 | 1277 | goto error; |
b5541356 DG |
1278 | } else { |
1279 | /* Acquire lock for the session */ | |
1280 | lock_session(cmd_ctx->session); | |
379473d2 | 1281 | } |
5e16da05 | 1282 | break; |
379473d2 DG |
1283 | } |
1284 | ||
f3ed775e DG |
1285 | /* |
1286 | * Check kernel command for kernel session. | |
1287 | */ | |
20fe2104 | 1288 | switch (cmd_ctx->lsm->cmd_type) { |
d65106b1 | 1289 | case LTTNG_KERNEL_ADD_CONTEXT: |
f3ed775e DG |
1290 | case LTTNG_KERNEL_DISABLE_ALL_EVENT: |
1291 | case LTTNG_KERNEL_DISABLE_CHANNEL: | |
1292 | case LTTNG_KERNEL_DISABLE_EVENT: | |
1293 | case LTTNG_KERNEL_ENABLE_ALL_EVENT: | |
1294 | case LTTNG_KERNEL_ENABLE_CHANNEL: | |
1295 | case LTTNG_KERNEL_ENABLE_EVENT: | |
1296 | case LTTNG_KERNEL_LIST_EVENTS: | |
333f285e | 1297 | /* Kernel tracer check */ |
20fe2104 | 1298 | if (kernel_tracer_fd == 0) { |
333f285e DG |
1299 | init_kernel_tracer(); |
1300 | if (kernel_tracer_fd == 0) { | |
1301 | ret = LTTCOMM_KERN_NA; | |
1302 | goto error; | |
1303 | } | |
20fe2104 | 1304 | } |
f3ed775e DG |
1305 | |
1306 | /* Need a session for kernel command */ | |
1307 | if (cmd_ctx->lsm->cmd_type != LTTNG_KERNEL_LIST_EVENTS && | |
1308 | cmd_ctx->session->kernel_session == NULL) { | |
7b395890 | 1309 | |
f3ed775e DG |
1310 | ret = create_kernel_session(cmd_ctx->session); |
1311 | if (ret < 0) { | |
1312 | ret = LTTCOMM_KERN_SESS_FAIL; | |
1313 | goto error; | |
1314 | } | |
1315 | ||
7b395890 | 1316 | /* Start the kernel consumer daemon */ |
f3ed775e DG |
1317 | if (kconsumerd_pid == 0) { |
1318 | ret = start_kconsumerd(); | |
1319 | if (ret < 0) { | |
1320 | goto error; | |
1321 | } | |
1322 | } | |
1323 | } | |
20fe2104 DG |
1324 | } |
1325 | ||
97b1a726 | 1326 | #ifdef DISABLED |
471d1693 | 1327 | /* Connect to ust apps if available pid */ |
5461b305 | 1328 | if (cmd_ctx->lsm->pid > 0) { |
471d1693 | 1329 | /* Connect to app using ustctl API */ |
5461b305 DG |
1330 | cmd_ctx->ust_sock = ust_connect_app(cmd_ctx->lsm->pid); |
1331 | if (cmd_ctx->ust_sock < 0) { | |
471d1693 | 1332 | ret = LTTCOMM_NO_TRACEABLE; |
5461b305 | 1333 | goto error; |
471d1693 DG |
1334 | } |
1335 | } | |
97b1a726 | 1336 | #endif /* DISABLED */ |
471d1693 | 1337 | |
fac6795d | 1338 | /* Process by command type */ |
5461b305 | 1339 | switch (cmd_ctx->lsm->cmd_type) { |
d65106b1 DG |
1340 | case LTTNG_KERNEL_ADD_CONTEXT: |
1341 | { | |
1342 | int found = 0, no_event = 0; | |
1343 | struct ltt_kernel_channel *chan; | |
1344 | struct ltt_kernel_event *event; | |
7d29a247 | 1345 | struct lttng_kernel_context ctx; |
d65106b1 DG |
1346 | |
1347 | /* Setup lttng message with no payload */ | |
1348 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1349 | if (ret < 0) { | |
1350 | goto setup_error; | |
1351 | } | |
1352 | ||
1353 | /* Check if event name is given */ | |
1354 | if (strlen(cmd_ctx->lsm->u.context.event_name) == 0) { | |
1355 | no_event = 1; | |
1356 | } | |
1357 | ||
7d29a247 DG |
1358 | /* Create Kernel context */ |
1359 | ctx.ctx = cmd_ctx->lsm->u.context.ctx.ctx; | |
1360 | ctx.u.perf_counter.type = cmd_ctx->lsm->u.context.ctx.u.perf_counter.type; | |
1361 | ctx.u.perf_counter.config = cmd_ctx->lsm->u.context.ctx.u.perf_counter.config; | |
1362 | strncpy(ctx.u.perf_counter.name, | |
1363 | cmd_ctx->lsm->u.context.ctx.u.perf_counter.name, | |
1364 | sizeof(ctx.u.perf_counter.name)); | |
1365 | ||
d65106b1 DG |
1366 | if (strlen(cmd_ctx->lsm->u.context.channel_name) == 0) { |
1367 | /* Go over all channels */ | |
1368 | DBG("Adding context to all channels"); | |
1369 | cds_list_for_each_entry(chan, | |
1370 | &cmd_ctx->session->kernel_session->channel_list.head, list) { | |
1371 | if (no_event) { | |
7d29a247 | 1372 | ret = kernel_add_channel_context(chan, &ctx); |
d65106b1 DG |
1373 | if (ret < 0) { |
1374 | continue; | |
1375 | } | |
1376 | } else { | |
1377 | event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan); | |
1378 | if (event != NULL) { | |
7d29a247 | 1379 | ret = kernel_add_event_context(event, &ctx); |
d65106b1 DG |
1380 | if (ret < 0) { |
1381 | ret = LTTCOMM_KERN_CONTEXT_FAIL; | |
1382 | goto error; | |
1383 | } | |
1384 | found = 1; | |
1385 | break; | |
1386 | } | |
1387 | } | |
1388 | } | |
1389 | } else { | |
1390 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.context.channel_name, | |
1391 | cmd_ctx->session->kernel_session); | |
1392 | if (chan == NULL) { | |
1393 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1394 | goto error; | |
1395 | } | |
1396 | ||
1397 | if (no_event) { | |
7d29a247 | 1398 | ret = kernel_add_channel_context(chan, &ctx); |
d65106b1 DG |
1399 | if (ret < 0) { |
1400 | ret = LTTCOMM_KERN_CONTEXT_FAIL; | |
1401 | goto error; | |
1402 | } | |
1403 | } else { | |
1404 | event = get_kernel_event_by_name(cmd_ctx->lsm->u.context.event_name, chan); | |
1405 | if (event != NULL) { | |
7d29a247 | 1406 | ret = kernel_add_event_context(event, &ctx); |
d65106b1 DG |
1407 | if (ret < 0) { |
1408 | ret = LTTCOMM_KERN_CONTEXT_FAIL; | |
1409 | goto error; | |
1410 | } | |
1411 | } | |
1412 | } | |
1413 | } | |
1414 | ||
1415 | if (!found && !no_event) { | |
1416 | ret = LTTCOMM_NO_EVENT; | |
1417 | goto error; | |
1418 | } | |
1419 | ||
1420 | ret = LTTCOMM_OK; | |
1421 | break; | |
1422 | } | |
26cc6b4e DG |
1423 | case LTTNG_KERNEL_DISABLE_CHANNEL: |
1424 | { | |
1425 | struct ltt_kernel_channel *chan; | |
1426 | ||
1427 | /* Setup lttng message with no payload */ | |
1428 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1429 | if (ret < 0) { | |
1430 | goto setup_error; | |
1431 | } | |
1432 | ||
1433 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name, | |
1434 | cmd_ctx->session->kernel_session); | |
1435 | if (chan == NULL) { | |
1436 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1437 | goto error; | |
1438 | } else if (chan->enabled == 1) { | |
1439 | ret = kernel_disable_channel(chan); | |
1440 | if (ret < 0) { | |
1441 | if (ret != EEXIST) { | |
1442 | ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL; | |
1443 | } | |
1444 | goto error; | |
1445 | } | |
1446 | } | |
1447 | ||
1448 | kernel_wait_quiescent(kernel_tracer_fd); | |
1449 | ret = LTTCOMM_OK; | |
1450 | break; | |
1451 | } | |
e953ef25 DG |
1452 | case LTTNG_KERNEL_DISABLE_EVENT: |
1453 | { | |
e953ef25 | 1454 | struct ltt_kernel_channel *chan; |
19e70852 | 1455 | struct ltt_kernel_event *ev; |
e953ef25 DG |
1456 | |
1457 | /* Setup lttng message with no payload */ | |
1458 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1459 | if (ret < 0) { | |
1460 | goto setup_error; | |
1461 | } | |
1462 | ||
19e70852 DG |
1463 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name, |
1464 | cmd_ctx->session->kernel_session); | |
1465 | if (chan == NULL) { | |
1466 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1467 | goto error; | |
1468 | } | |
e953ef25 | 1469 | |
19e70852 DG |
1470 | ev = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, chan); |
1471 | if (ev != NULL) { | |
1472 | DBG("Disabling kernel event %s for channel %s.", | |
1473 | cmd_ctx->lsm->u.disable.name, cmd_ctx->lsm->u.disable.channel_name); | |
1474 | ret = kernel_disable_event(ev); | |
1475 | if (ret < 0) { | |
1476 | ret = LTTCOMM_KERN_ENABLE_FAIL; | |
1477 | goto error; | |
e953ef25 DG |
1478 | } |
1479 | } | |
1480 | ||
19e70852 DG |
1481 | kernel_wait_quiescent(kernel_tracer_fd); |
1482 | ret = LTTCOMM_OK; | |
e953ef25 DG |
1483 | break; |
1484 | } | |
950131af DG |
1485 | case LTTNG_KERNEL_DISABLE_ALL_EVENT: |
1486 | { | |
1487 | struct ltt_kernel_channel *chan; | |
1488 | struct ltt_kernel_event *ev; | |
1489 | ||
1490 | /* Setup lttng message with no payload */ | |
1491 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1492 | if (ret < 0) { | |
1493 | goto setup_error; | |
1494 | } | |
1495 | ||
1496 | DBG("Disabling all enabled kernel events"); | |
1497 | ||
1498 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name, | |
1499 | cmd_ctx->session->kernel_session); | |
1500 | if (chan == NULL) { | |
1501 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1502 | goto error; | |
1503 | } | |
1504 | ||
1505 | /* For each event in the kernel session */ | |
1506 | cds_list_for_each_entry(ev, &chan->events_list.head, list) { | |
1507 | DBG("Disabling kernel event %s for channel %s.", | |
1508 | ev->event->name, cmd_ctx->lsm->u.disable.channel_name); | |
1509 | ret = kernel_disable_event(ev); | |
1510 | if (ret < 0) { | |
1511 | continue; | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | /* Quiescent wait after event disable */ | |
1516 | kernel_wait_quiescent(kernel_tracer_fd); | |
1517 | ret = LTTCOMM_OK; | |
1518 | break; | |
1519 | } | |
d36b8583 DG |
1520 | case LTTNG_KERNEL_ENABLE_CHANNEL: |
1521 | { | |
1522 | struct ltt_kernel_channel *chan; | |
1523 | ||
1524 | /* Setup lttng message with no payload */ | |
1525 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1526 | if (ret < 0) { | |
1527 | goto setup_error; | |
1528 | } | |
1529 | ||
1530 | chan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name, | |
1531 | cmd_ctx->session->kernel_session); | |
1532 | if (chan == NULL) { | |
7d29a247 DG |
1533 | /* Channel not found, creating it */ |
1534 | DBG("Creating kernel channel"); | |
1535 | ||
1536 | ret = kernel_create_channel(cmd_ctx->session->kernel_session, | |
1537 | &cmd_ctx->lsm->u.channel.chan, cmd_ctx->session->path); | |
1538 | if (ret < 0) { | |
1539 | ret = LTTCOMM_KERN_CHAN_FAIL; | |
1540 | goto error; | |
1541 | } | |
1542 | ||
1543 | /* Notify kernel thread that there is a new channel */ | |
1544 | ret = notify_kernel_pollfd(); | |
1545 | if (ret < 0) { | |
1546 | ret = LTTCOMM_FATAL; | |
1547 | goto error; | |
1548 | } | |
d36b8583 DG |
1549 | } else if (chan->enabled == 0) { |
1550 | ret = kernel_enable_channel(chan); | |
1551 | if (ret < 0) { | |
1552 | if (ret != EEXIST) { | |
1553 | ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL; | |
1554 | } | |
1555 | goto error; | |
1556 | } | |
1557 | } | |
1558 | ||
1559 | kernel_wait_quiescent(kernel_tracer_fd); | |
1560 | ret = LTTCOMM_OK; | |
1561 | break; | |
1562 | } | |
f3ed775e | 1563 | case LTTNG_KERNEL_ENABLE_EVENT: |
894be886 | 1564 | { |
7d29a247 DG |
1565 | char *channel_name; |
1566 | struct ltt_kernel_channel *kchan; | |
19e70852 | 1567 | struct ltt_kernel_event *ev; |
7d29a247 | 1568 | struct lttng_channel *chan; |
f3ed775e | 1569 | |
894be886 DG |
1570 | /* Setup lttng message with no payload */ |
1571 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1572 | if (ret < 0) { | |
1573 | goto setup_error; | |
1574 | } | |
1575 | ||
7d29a247 DG |
1576 | channel_name = cmd_ctx->lsm->u.enable.channel_name; |
1577 | ||
1578 | do { | |
1579 | kchan = get_kernel_channel_by_name(channel_name, | |
1580 | cmd_ctx->session->kernel_session); | |
1581 | if (kchan == NULL) { | |
1582 | DBG("Creating default channel"); | |
1583 | ||
1584 | chan = init_default_channel(); | |
1585 | if (chan == NULL) { | |
1586 | ret = LTTCOMM_FATAL; | |
1587 | goto error; | |
1588 | } | |
1589 | ||
1590 | ret = kernel_create_channel(cmd_ctx->session->kernel_session, | |
1591 | chan, cmd_ctx->session->path); | |
1592 | if (ret < 0) { | |
1593 | ret = LTTCOMM_KERN_CHAN_FAIL; | |
1594 | goto error; | |
1595 | } | |
1596 | } | |
1597 | } while (kchan == NULL); | |
f34daff7 | 1598 | |
7d29a247 | 1599 | ev = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan); |
19e70852 DG |
1600 | if (ev == NULL) { |
1601 | DBG("Creating kernel event %s for channel %s.", | |
7d29a247 DG |
1602 | cmd_ctx->lsm->u.enable.event.name, channel_name); |
1603 | ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, kchan); | |
f3ed775e | 1604 | } else { |
19e70852 | 1605 | DBG("Enabling kernel event %s for channel %s.", |
7d29a247 | 1606 | cmd_ctx->lsm->u.enable.event.name, channel_name); |
19e70852 | 1607 | ret = kernel_enable_event(ev); |
7d29a247 DG |
1608 | if (ret == -EEXIST) { |
1609 | ret = LTTCOMM_KERN_EVENT_EXIST; | |
1610 | goto error; | |
1611 | } | |
19e70852 DG |
1612 | } |
1613 | ||
1614 | if (ret < 0) { | |
1615 | ret = LTTCOMM_KERN_ENABLE_FAIL; | |
1616 | goto error; | |
f3ed775e | 1617 | } |
19e70852 DG |
1618 | |
1619 | kernel_wait_quiescent(kernel_tracer_fd); | |
1620 | ret = LTTCOMM_OK; | |
894be886 DG |
1621 | break; |
1622 | } | |
f3ed775e | 1623 | case LTTNG_KERNEL_ENABLE_ALL_EVENT: |
33a2b854 | 1624 | { |
950131af | 1625 | int pos, size; |
7d29a247 DG |
1626 | char *event_list, *event, *ptr, *channel_name; |
1627 | struct ltt_kernel_channel *kchan; | |
950131af DG |
1628 | struct ltt_kernel_event *ev; |
1629 | struct lttng_event ev_attr; | |
7d29a247 | 1630 | struct lttng_channel *chan; |
33a2b854 DG |
1631 | |
1632 | /* Setup lttng message with no payload */ | |
1633 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1634 | if (ret < 0) { | |
1635 | goto setup_error; | |
1636 | } | |
1637 | ||
1638 | DBG("Enabling all kernel event"); | |
1639 | ||
7d29a247 DG |
1640 | channel_name = cmd_ctx->lsm->u.enable.channel_name; |
1641 | ||
1642 | do { | |
1643 | kchan = get_kernel_channel_by_name(channel_name, | |
1644 | cmd_ctx->session->kernel_session); | |
1645 | if (kchan == NULL) { | |
1646 | DBG("Creating default channel"); | |
1647 | ||
1648 | chan = init_default_channel(); | |
1649 | if (chan == NULL) { | |
1650 | ret = LTTCOMM_FATAL; | |
1651 | goto error; | |
1652 | } | |
1653 | ||
1654 | ret = kernel_create_channel(cmd_ctx->session->kernel_session, | |
0c11f72b | 1655 | chan, cmd_ctx->session->path); |
7d29a247 DG |
1656 | if (ret < 0) { |
1657 | ret = LTTCOMM_KERN_CHAN_FAIL; | |
1658 | goto error; | |
1659 | } | |
1660 | } | |
1661 | } while (kchan == NULL); | |
33a2b854 | 1662 | |
950131af | 1663 | /* For each event in the kernel session */ |
7d29a247 | 1664 | cds_list_for_each_entry(ev, &kchan->events_list.head, list) { |
950131af | 1665 | DBG("Enabling kernel event %s for channel %s.", |
7d29a247 | 1666 | ev->event->name, channel_name); |
950131af DG |
1667 | ret = kernel_enable_event(ev); |
1668 | if (ret < 0) { | |
1669 | continue; | |
f3ed775e DG |
1670 | } |
1671 | } | |
1672 | ||
950131af DG |
1673 | size = kernel_list_events(kernel_tracer_fd, &event_list); |
1674 | if (size < 0) { | |
1675 | ret = LTTCOMM_KERN_LIST_FAIL; | |
f3ed775e DG |
1676 | goto error; |
1677 | } | |
1678 | ||
33a2b854 DG |
1679 | ptr = event_list; |
1680 | while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) { | |
7d29a247 | 1681 | ev = get_kernel_event_by_name(event, kchan); |
950131af DG |
1682 | if (ev == NULL) { |
1683 | strncpy(ev_attr.name, event, LTTNG_SYM_NAME_LEN); | |
1684 | /* Default event type for enable all */ | |
e6ddca71 | 1685 | ev_attr.type = LTTNG_EVENT_TRACEPOINT; |
950131af | 1686 | /* Enable each single tracepoint event */ |
7d29a247 | 1687 | ret = kernel_create_event(&ev_attr, kchan); |
950131af DG |
1688 | if (ret < 0) { |
1689 | /* Ignore error here and continue */ | |
950131af | 1690 | } |
33a2b854 | 1691 | } |
950131af | 1692 | |
33a2b854 DG |
1693 | /* Move pointer to the next line */ |
1694 | ptr += pos + 1; | |
1695 | free(event); | |
1696 | } | |
1697 | ||
1698 | free(event_list); | |
1699 | ||
f3ed775e DG |
1700 | /* Quiescent wait after event enable */ |
1701 | kernel_wait_quiescent(kernel_tracer_fd); | |
33a2b854 DG |
1702 | ret = LTTCOMM_OK; |
1703 | break; | |
1704 | } | |
f3ed775e | 1705 | case LTTNG_KERNEL_LIST_EVENTS: |
2ef84c95 DG |
1706 | { |
1707 | char *event_list; | |
f3ed775e DG |
1708 | ssize_t size = 0; |
1709 | ||
1710 | DBG("Listing kernel events"); | |
2ef84c95 DG |
1711 | |
1712 | size = kernel_list_events(kernel_tracer_fd, &event_list); | |
1713 | if (size < 0) { | |
1714 | ret = LTTCOMM_KERN_LIST_FAIL; | |
1715 | goto error; | |
1716 | } | |
1717 | ||
1718 | /* | |
1719 | * Setup lttng message with payload size set to the event list size in | |
1720 | * bytes and then copy list into the llm payload. | |
1721 | */ | |
1722 | ret = setup_lttng_msg(cmd_ctx, size); | |
1723 | if (ret < 0) { | |
1724 | goto setup_error; | |
1725 | } | |
1726 | ||
1727 | /* Copy event list into message payload */ | |
1728 | memcpy(cmd_ctx->llm->payload, event_list, size); | |
1729 | ||
1730 | free(event_list); | |
1731 | ||
1732 | ret = LTTCOMM_OK; | |
1733 | break; | |
1734 | } | |
f3ed775e | 1735 | case LTTNG_START_TRACE: |
8c0faa1d DG |
1736 | { |
1737 | struct ltt_kernel_channel *chan; | |
f3ed775e | 1738 | |
8c0faa1d DG |
1739 | /* Setup lttng message with no payload */ |
1740 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1741 | if (ret < 0) { | |
1742 | goto setup_error; | |
1743 | } | |
1744 | ||
f3ed775e DG |
1745 | /* Kernel tracing */ |
1746 | if (cmd_ctx->session->kernel_session != NULL) { | |
1747 | if (cmd_ctx->session->kernel_session->metadata == NULL) { | |
1748 | DBG("Open kernel metadata"); | |
58a97671 DG |
1749 | ret = kernel_open_metadata(cmd_ctx->session->kernel_session, |
1750 | cmd_ctx->session->path); | |
f3ed775e DG |
1751 | if (ret < 0) { |
1752 | ret = LTTCOMM_KERN_META_FAIL; | |
1753 | goto error; | |
1754 | } | |
1755 | } | |
8c0faa1d | 1756 | |
f3ed775e DG |
1757 | if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) { |
1758 | DBG("Opening kernel metadata stream"); | |
1759 | if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) { | |
1760 | ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session); | |
1761 | if (ret < 0) { | |
1762 | ERR("Kernel create metadata stream failed"); | |
1763 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
1764 | goto error; | |
1765 | } | |
1766 | } | |
1767 | } | |
8c0faa1d | 1768 | |
f3ed775e DG |
1769 | /* For each channel */ |
1770 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { | |
1771 | if (chan->stream_count == 0) { | |
1772 | ret = kernel_open_channel_stream(chan); | |
1773 | if (ret < 0) { | |
1774 | ERR("Kernel create channel stream failed"); | |
1775 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
1776 | goto error; | |
1777 | } | |
1778 | /* Update the stream global counter */ | |
1779 | cmd_ctx->session->kernel_session->stream_count_global += ret; | |
1780 | } | |
1781 | } | |
1782 | ||
1783 | DBG("Start kernel tracing"); | |
1784 | ret = kernel_start_session(cmd_ctx->session->kernel_session); | |
8c0faa1d | 1785 | if (ret < 0) { |
f3ed775e DG |
1786 | ERR("Kernel start session failed"); |
1787 | ret = LTTCOMM_KERN_START_FAIL; | |
8c0faa1d DG |
1788 | goto error; |
1789 | } | |
8c0faa1d | 1790 | |
f3ed775e DG |
1791 | ret = start_kernel_trace(cmd_ctx->session->kernel_session); |
1792 | if (ret < 0) { | |
1793 | ret = LTTCOMM_KERN_START_FAIL; | |
8c0faa1d DG |
1794 | goto error; |
1795 | } | |
8c0faa1d | 1796 | |
f3ed775e DG |
1797 | /* Quiescent wait after starting trace */ |
1798 | kernel_wait_quiescent(kernel_tracer_fd); | |
8c0faa1d DG |
1799 | } |
1800 | ||
f3ed775e | 1801 | /* TODO: Start all UST traces */ |
8c0faa1d DG |
1802 | |
1803 | ret = LTTCOMM_OK; | |
1804 | break; | |
1805 | } | |
f3ed775e | 1806 | case LTTNG_STOP_TRACE: |
8c0faa1d | 1807 | { |
f3ed775e | 1808 | struct ltt_kernel_channel *chan; |
8c0faa1d DG |
1809 | /* Setup lttng message with no payload */ |
1810 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1811 | if (ret < 0) { | |
1812 | goto setup_error; | |
1813 | } | |
1814 | ||
f3ed775e DG |
1815 | /* Kernel tracer */ |
1816 | if (cmd_ctx->session->kernel_session != NULL) { | |
1817 | DBG("Stop kernel tracing"); | |
84291629 | 1818 | |
f3ed775e DG |
1819 | ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd); |
1820 | if (ret < 0) { | |
1821 | ERR("Kernel metadata flush failed"); | |
1822 | } | |
8c0faa1d | 1823 | |
f3ed775e DG |
1824 | cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) { |
1825 | ret = kernel_flush_buffer(chan); | |
1826 | if (ret < 0) { | |
1827 | ERR("Kernel flush buffer error"); | |
1828 | } | |
1829 | } | |
1830 | ||
1831 | ret = kernel_stop_session(cmd_ctx->session->kernel_session); | |
1832 | if (ret < 0) { | |
1833 | ERR("Kernel stop session failed"); | |
1834 | ret = LTTCOMM_KERN_STOP_FAIL; | |
1835 | goto error; | |
1836 | } | |
1837 | ||
1838 | /* Quiescent wait after stopping trace */ | |
1839 | kernel_wait_quiescent(kernel_tracer_fd); | |
8c0faa1d DG |
1840 | } |
1841 | ||
f3ed775e | 1842 | /* TODO : User-space tracer */ |
8c0faa1d DG |
1843 | |
1844 | ret = LTTCOMM_OK; | |
1845 | break; | |
1846 | } | |
5e16da05 MD |
1847 | case LTTNG_CREATE_SESSION: |
1848 | { | |
5461b305 DG |
1849 | /* Setup lttng message with no payload */ |
1850 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1851 | if (ret < 0) { | |
1852 | goto setup_error; | |
1853 | } | |
1854 | ||
f3ed775e | 1855 | ret = create_session(cmd_ctx->lsm->session_name, cmd_ctx->lsm->path); |
5e16da05 | 1856 | if (ret < 0) { |
f3ed775e | 1857 | if (ret == -EEXIST) { |
5e16da05 MD |
1858 | ret = LTTCOMM_EXIST_SESS; |
1859 | } else { | |
aaf97519 | 1860 | ret = LTTCOMM_FATAL; |
aaf97519 | 1861 | } |
5461b305 | 1862 | goto error; |
8028d920 | 1863 | } |
1657e9bb | 1864 | |
5461b305 | 1865 | ret = LTTCOMM_OK; |
5e16da05 MD |
1866 | break; |
1867 | } | |
1868 | case LTTNG_DESTROY_SESSION: | |
1869 | { | |
5461b305 DG |
1870 | /* Setup lttng message with no payload */ |
1871 | ret = setup_lttng_msg(cmd_ctx, 0); | |
1872 | if (ret < 0) { | |
1873 | goto setup_error; | |
1874 | } | |
1875 | ||
f3ed775e DG |
1876 | /* Clean kernel session teardown */ |
1877 | teardown_kernel_session(cmd_ctx->session); | |
1878 | ||
1879 | ret = destroy_session(cmd_ctx->lsm->session_name); | |
5e16da05 | 1880 | if (ret < 0) { |
f3ed775e | 1881 | ret = LTTCOMM_FATAL; |
5461b305 | 1882 | goto error; |
5e16da05 | 1883 | } |
1657e9bb | 1884 | |
7a485870 DG |
1885 | /* |
1886 | * Must notify the kernel thread here to update it's pollfd in order to | |
1887 | * remove the channel(s)' fd just destroyed. | |
1888 | */ | |
1889 | ret = notify_kernel_pollfd(); | |
1890 | if (ret < 0) { | |
1891 | ret = LTTCOMM_FATAL; | |
1892 | goto error; | |
1893 | } | |
1894 | ||
5461b305 DG |
1895 | ret = LTTCOMM_OK; |
1896 | break; | |
5e16da05 | 1897 | } |
f3ed775e | 1898 | /* |
5e16da05 MD |
1899 | case LTTNG_LIST_TRACES: |
1900 | { | |
5461b305 | 1901 | unsigned int trace_count; |
1657e9bb | 1902 | |
5461b305 | 1903 | trace_count = get_trace_count_per_session(cmd_ctx->session); |
5e16da05 MD |
1904 | if (trace_count == 0) { |
1905 | ret = LTTCOMM_NO_TRACE; | |
5461b305 | 1906 | goto error; |
1657e9bb | 1907 | } |
df0da139 | 1908 | |
5461b305 DG |
1909 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count); |
1910 | if (ret < 0) { | |
1911 | goto setup_error; | |
df0da139 | 1912 | } |
ca95a216 | 1913 | |
5461b305 DG |
1914 | get_traces_per_session(cmd_ctx->session, |
1915 | (struct lttng_trace *)(cmd_ctx->llm->payload)); | |
1916 | ||
1917 | ret = LTTCOMM_OK; | |
5e16da05 MD |
1918 | break; |
1919 | } | |
f3ed775e DG |
1920 | */ |
1921 | /* | |
5e16da05 MD |
1922 | case UST_CREATE_TRACE: |
1923 | { | |
5461b305 | 1924 | ret = setup_lttng_msg(cmd_ctx, 0); |
5e16da05 | 1925 | if (ret < 0) { |
5461b305 | 1926 | goto setup_error; |
fac6795d | 1927 | } |
ce3d728c | 1928 | |
5461b305 DG |
1929 | ret = ust_create_trace(cmd_ctx); |
1930 | if (ret < 0) { | |
f3ed775e | 1931 | goto error; |
5461b305 DG |
1932 | } |
1933 | break; | |
5e16da05 | 1934 | } |
f3ed775e DG |
1935 | */ |
1936 | case LTTNG_LIST_TRACEABLE_APPS: | |
5e16da05 | 1937 | { |
5461b305 DG |
1938 | unsigned int app_count; |
1939 | ||
1940 | app_count = get_app_count(); | |
1941 | DBG("Traceable application count : %d", app_count); | |
5e16da05 MD |
1942 | if (app_count == 0) { |
1943 | ret = LTTCOMM_NO_APPS; | |
5461b305 | 1944 | goto error; |
ce3d728c | 1945 | } |
520ff687 | 1946 | |
5461b305 DG |
1947 | ret = setup_lttng_msg(cmd_ctx, sizeof(pid_t) * app_count); |
1948 | if (ret < 0) { | |
1949 | goto setup_error; | |
520ff687 | 1950 | } |
57167058 | 1951 | |
5461b305 | 1952 | get_app_list_pids((pid_t *)(cmd_ctx->llm->payload)); |
5e16da05 | 1953 | |
5461b305 | 1954 | ret = LTTCOMM_OK; |
5e16da05 MD |
1955 | break; |
1956 | } | |
f3ed775e | 1957 | /* |
5e16da05 MD |
1958 | case UST_START_TRACE: |
1959 | { | |
5461b305 DG |
1960 | ret = setup_lttng_msg(cmd_ctx, 0); |
1961 | if (ret < 0) { | |
1962 | goto setup_error; | |
1963 | } | |
ca95a216 | 1964 | |
5461b305 DG |
1965 | ret = ust_start_trace(cmd_ctx); |
1966 | if (ret < 0) { | |
1967 | goto setup_error; | |
1968 | } | |
1969 | break; | |
5e16da05 MD |
1970 | } |
1971 | case UST_STOP_TRACE: | |
1972 | { | |
5461b305 DG |
1973 | ret = setup_lttng_msg(cmd_ctx, 0); |
1974 | if (ret < 0) { | |
1975 | goto setup_error; | |
1976 | } | |
ca95a216 | 1977 | |
5461b305 DG |
1978 | ret = ust_stop_trace(cmd_ctx); |
1979 | if (ret < 0) { | |
1980 | goto setup_error; | |
1981 | } | |
1982 | break; | |
5e16da05 | 1983 | } |
f3ed775e | 1984 | */ |
5e16da05 MD |
1985 | case LTTNG_LIST_SESSIONS: |
1986 | { | |
6c9cc2ab | 1987 | lock_session_list(); |
5461b305 | 1988 | |
6c9cc2ab | 1989 | if (session_list_ptr->count == 0) { |
f3ed775e | 1990 | ret = LTTCOMM_NO_SESSION; |
5461b305 | 1991 | goto error; |
57167058 | 1992 | } |
5e16da05 | 1993 | |
6c9cc2ab DG |
1994 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * |
1995 | session_list_ptr->count); | |
5461b305 DG |
1996 | if (ret < 0) { |
1997 | goto setup_error; | |
e065084a | 1998 | } |
5e16da05 | 1999 | |
6c9cc2ab DG |
2000 | /* Filled the session array */ |
2001 | list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload)); | |
2002 | ||
2003 | unlock_session_list(); | |
5e16da05 | 2004 | |
5461b305 | 2005 | ret = LTTCOMM_OK; |
5e16da05 MD |
2006 | break; |
2007 | } | |
2008 | default: | |
2009 | /* Undefined command */ | |
5461b305 DG |
2010 | ret = setup_lttng_msg(cmd_ctx, 0); |
2011 | if (ret < 0) { | |
2012 | goto setup_error; | |
2013 | } | |
2014 | ||
5e16da05 | 2015 | ret = LTTCOMM_UND; |
5461b305 | 2016 | break; |
fac6795d DG |
2017 | } |
2018 | ||
5461b305 DG |
2019 | /* Set return code */ |
2020 | cmd_ctx->llm->ret_code = ret; | |
ca95a216 | 2021 | |
b5541356 DG |
2022 | if (cmd_ctx->session) { |
2023 | unlock_session(cmd_ctx->session); | |
2024 | } | |
2025 | ||
e065084a DG |
2026 | return ret; |
2027 | ||
5461b305 | 2028 | error: |
5461b305 DG |
2029 | if (cmd_ctx->llm == NULL) { |
2030 | DBG("Missing llm structure. Allocating one."); | |
894be886 | 2031 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
5461b305 DG |
2032 | goto setup_error; |
2033 | } | |
2034 | } | |
e065084a | 2035 | /* Notify client of error */ |
5461b305 | 2036 | cmd_ctx->llm->ret_code = ret; |
e065084a | 2037 | |
5461b305 | 2038 | setup_error: |
b5541356 DG |
2039 | if (cmd_ctx->session) { |
2040 | unlock_session(cmd_ctx->session); | |
2041 | } | |
8028d920 | 2042 | return ret; |
fac6795d DG |
2043 | } |
2044 | ||
1d4b027a DG |
2045 | /* |
2046 | * thread_manage_clients | |
2047 | * | |
2048 | * This thread manage all clients request using the unix | |
2049 | * client socket for communication. | |
2050 | */ | |
2051 | static void *thread_manage_clients(void *data) | |
2052 | { | |
273ea72c DG |
2053 | int sock = 0, ret; |
2054 | struct command_ctx *cmd_ctx = NULL; | |
2055 | struct pollfd pollfd[2]; | |
1d4b027a DG |
2056 | |
2057 | DBG("[thread] Manage client started"); | |
2058 | ||
2059 | ret = lttcomm_listen_unix_sock(client_sock); | |
2060 | if (ret < 0) { | |
2061 | goto error; | |
2062 | } | |
2063 | ||
273ea72c DG |
2064 | /* First fd is always the quit pipe */ |
2065 | pollfd[0].fd = thread_quit_pipe[0]; | |
2066 | ||
2067 | /* Apps socket */ | |
2068 | pollfd[1].fd = client_sock; | |
2069 | pollfd[1].events = POLLIN; | |
2070 | ||
1d4b027a DG |
2071 | /* Notify parent pid that we are ready |
2072 | * to accept command for client side. | |
2073 | */ | |
2074 | if (opt_sig_parent) { | |
2075 | kill(ppid, SIGCHLD); | |
2076 | } | |
2077 | ||
2078 | while (1) { | |
1d4b027a | 2079 | DBG("Accepting client command ..."); |
273ea72c DG |
2080 | |
2081 | /* Inifinite blocking call, waiting for transmission */ | |
2082 | ret = poll(pollfd, 2, -1); | |
2083 | if (ret < 0) { | |
2084 | perror("poll client thread"); | |
2085 | goto error; | |
2086 | } | |
2087 | ||
2088 | /* Thread quit pipe has been closed. Killing thread. */ | |
2089 | if (pollfd[0].revents == POLLNVAL) { | |
2090 | goto error; | |
2091 | } else if (pollfd[1].revents == POLLERR) { | |
2092 | ERR("Client socket poll error"); | |
2093 | goto error; | |
2094 | } | |
2095 | ||
1d4b027a DG |
2096 | sock = lttcomm_accept_unix_sock(client_sock); |
2097 | if (sock < 0) { | |
2098 | goto error; | |
2099 | } | |
2100 | ||
2101 | /* Allocate context command to process the client request */ | |
2102 | cmd_ctx = malloc(sizeof(struct command_ctx)); | |
2103 | ||
2104 | /* Allocate data buffer for reception */ | |
2105 | cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg)); | |
2106 | cmd_ctx->llm = NULL; | |
2107 | cmd_ctx->session = NULL; | |
2108 | ||
2109 | /* | |
2110 | * Data is received from the lttng client. The struct | |
2111 | * lttcomm_session_msg (lsm) contains the command and data request of | |
2112 | * the client. | |
2113 | */ | |
2114 | DBG("Receiving data from client ..."); | |
2115 | ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg)); | |
2116 | if (ret <= 0) { | |
2117 | continue; | |
2118 | } | |
2119 | ||
f7776ea7 DG |
2120 | // TODO: Validate cmd_ctx including sanity check for security purpose. |
2121 | ||
1d4b027a DG |
2122 | /* |
2123 | * This function dispatch the work to the kernel or userspace tracer | |
2124 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
2125 | * informations for the client. The command context struct contains | |
2126 | * everything this function may needs. | |
2127 | */ | |
2128 | ret = process_client_msg(cmd_ctx); | |
2129 | if (ret < 0) { | |
2130 | /* TODO: Inform client somehow of the fatal error. At this point, | |
2131 | * ret < 0 means that a malloc failed (ENOMEM). */ | |
2132 | /* Error detected but still accept command */ | |
a2fb29a5 | 2133 | clean_command_ctx(&cmd_ctx); |
1d4b027a DG |
2134 | continue; |
2135 | } | |
2136 | ||
2137 | DBG("Sending response (size: %d, retcode: %d)", | |
2138 | cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code); | |
2139 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); | |
2140 | if (ret < 0) { | |
2141 | ERR("Failed to send data back to client"); | |
2142 | } | |
2143 | ||
a2fb29a5 | 2144 | clean_command_ctx(&cmd_ctx); |
d6e4fca4 DG |
2145 | |
2146 | /* End of transmission */ | |
2147 | close(sock); | |
1d4b027a DG |
2148 | } |
2149 | ||
2150 | error: | |
273ea72c DG |
2151 | DBG("Client thread dying"); |
2152 | if (client_sock) { | |
2153 | close(client_sock); | |
2154 | } | |
2155 | if (sock) { | |
2156 | close(sock); | |
2157 | } | |
2158 | ||
2159 | unlink(client_unix_sock_path); | |
2160 | ||
a2fb29a5 | 2161 | clean_command_ctx(&cmd_ctx); |
1d4b027a DG |
2162 | return NULL; |
2163 | } | |
2164 | ||
2165 | ||
fac6795d DG |
2166 | /* |
2167 | * usage function on stderr | |
2168 | */ | |
2169 | static void usage(void) | |
2170 | { | |
b716ce68 | 2171 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
d6f42150 DG |
2172 | fprintf(stderr, " -h, --help Display this usage.\n"); |
2173 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); | |
2174 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); | |
2175 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); | |
2176 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); | |
2177 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); | |
2178 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); | |
2179 | fprintf(stderr, " -V, --version Show version number.\n"); | |
2180 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); | |
2181 | fprintf(stderr, " -q, --quiet No output at all.\n"); | |
2182 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
fac6795d DG |
2183 | } |
2184 | ||
2185 | /* | |
2186 | * daemon argument parsing | |
2187 | */ | |
2188 | static int parse_args(int argc, char **argv) | |
2189 | { | |
2190 | int c; | |
2191 | ||
2192 | static struct option long_options[] = { | |
2193 | { "client-sock", 1, 0, 'c' }, | |
2194 | { "apps-sock", 1, 0, 'a' }, | |
d6f42150 DG |
2195 | { "kconsumerd-cmd-sock", 1, 0, 0 }, |
2196 | { "kconsumerd-err-sock", 1, 0, 0 }, | |
fac6795d | 2197 | { "daemonize", 0, 0, 'd' }, |
5b8719f5 | 2198 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
2199 | { "help", 0, 0, 'h' }, |
2200 | { "group", 1, 0, 'g' }, | |
2201 | { "version", 0, 0, 'V' }, | |
75462a81 | 2202 | { "quiet", 0, 0, 'q' }, |
3f9947db | 2203 | { "verbose", 0, 0, 'v' }, |
fac6795d DG |
2204 | { NULL, 0, 0, 0 } |
2205 | }; | |
2206 | ||
2207 | while (1) { | |
2208 | int option_index = 0; | |
d6f42150 | 2209 | c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:", long_options, &option_index); |
fac6795d DG |
2210 | if (c == -1) { |
2211 | break; | |
2212 | } | |
2213 | ||
2214 | switch (c) { | |
2215 | case 0: | |
2216 | fprintf(stderr, "option %s", long_options[option_index].name); | |
2217 | if (optarg) { | |
2218 | fprintf(stderr, " with arg %s\n", optarg); | |
2219 | } | |
2220 | break; | |
b716ce68 | 2221 | case 'c': |
fac6795d DG |
2222 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
2223 | break; | |
2224 | case 'a': | |
2225 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
2226 | break; | |
2227 | case 'd': | |
2228 | opt_daemon = 1; | |
2229 | break; | |
2230 | case 'g': | |
2231 | opt_tracing_group = strdup(optarg); | |
2232 | break; | |
2233 | case 'h': | |
2234 | usage(); | |
2235 | exit(EXIT_FAILURE); | |
2236 | case 'V': | |
2237 | fprintf(stdout, "%s\n", VERSION); | |
2238 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
2239 | case 'S': |
2240 | opt_sig_parent = 1; | |
2241 | break; | |
d6f42150 DG |
2242 | case 'E': |
2243 | snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg); | |
2244 | break; | |
2245 | case 'C': | |
2246 | snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg); | |
2247 | break; | |
75462a81 DG |
2248 | case 'q': |
2249 | opt_quiet = 1; | |
2250 | break; | |
3f9947db | 2251 | case 'v': |
53086306 DG |
2252 | /* Verbose level can increase using multiple -v */ |
2253 | opt_verbose += 1; | |
3f9947db | 2254 | break; |
fac6795d DG |
2255 | default: |
2256 | /* Unknown option or other error. | |
2257 | * Error is printed by getopt, just return */ | |
2258 | return -1; | |
2259 | } | |
2260 | } | |
2261 | ||
2262 | return 0; | |
2263 | } | |
2264 | ||
2265 | /* | |
2266 | * init_daemon_socket | |
2267 | * | |
2268 | * Creates the two needed socket by the daemon. | |
d6f42150 DG |
2269 | * apps_sock - The communication socket for all UST apps. |
2270 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d DG |
2271 | */ |
2272 | static int init_daemon_socket() | |
2273 | { | |
2274 | int ret = 0; | |
2275 | mode_t old_umask; | |
2276 | ||
2277 | old_umask = umask(0); | |
2278 | ||
2279 | /* Create client tool unix socket */ | |
d6f42150 DG |
2280 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
2281 | if (client_sock < 0) { | |
2282 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
2283 | ret = -1; |
2284 | goto end; | |
2285 | } | |
2286 | ||
2287 | /* File permission MUST be 660 */ | |
2288 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
2289 | if (ret < 0) { | |
d6f42150 | 2290 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
fac6795d DG |
2291 | perror("chmod"); |
2292 | goto end; | |
2293 | } | |
2294 | ||
2295 | /* Create the application unix socket */ | |
d6f42150 DG |
2296 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
2297 | if (apps_sock < 0) { | |
2298 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
2299 | ret = -1; |
2300 | goto end; | |
2301 | } | |
2302 | ||
d6f42150 | 2303 | /* File permission MUST be 666 */ |
fac6795d DG |
2304 | ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
2305 | if (ret < 0) { | |
d6f42150 | 2306 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
fac6795d DG |
2307 | perror("chmod"); |
2308 | goto end; | |
2309 | } | |
2310 | ||
2311 | end: | |
2312 | umask(old_umask); | |
2313 | return ret; | |
2314 | } | |
2315 | ||
2316 | /* | |
2317 | * check_existing_daemon | |
2318 | * | |
2319 | * Check if the global socket is available. | |
62bd06d8 | 2320 | * If yes, error is returned. |
fac6795d DG |
2321 | */ |
2322 | static int check_existing_daemon() | |
2323 | { | |
2324 | int ret; | |
2325 | ||
2326 | ret = access(client_unix_sock_path, F_OK); | |
2327 | if (ret == 0) { | |
2328 | ret = access(apps_unix_sock_path, F_OK); | |
2329 | } | |
2330 | ||
2331 | return ret; | |
2332 | } | |
2333 | ||
fac6795d | 2334 | /* |
d6f42150 | 2335 | * set_permissions |
fac6795d | 2336 | * |
5e16da05 MD |
2337 | * Set the tracing group gid onto the client socket. |
2338 | * | |
2339 | * Race window between mkdir and chown is OK because we are going from | |
a463f419 | 2340 | * more permissive (root.root) to les permissive (root.tracing). |
fac6795d | 2341 | */ |
d6f42150 | 2342 | static int set_permissions(void) |
fac6795d DG |
2343 | { |
2344 | int ret; | |
2345 | struct group *grp; | |
2346 | ||
2347 | /* Decide which group name to use */ | |
2348 | (opt_tracing_group != NULL) ? | |
2349 | (grp = getgrnam(opt_tracing_group)) : | |
2350 | (grp = getgrnam(default_tracing_group)); | |
2351 | ||
2352 | if (grp == NULL) { | |
a463f419 DG |
2353 | if (is_root) { |
2354 | WARN("No tracing group detected"); | |
2355 | ret = 0; | |
2356 | } else { | |
2357 | ERR("Missing tracing group. Aborting execution."); | |
2358 | ret = -1; | |
2359 | } | |
fac6795d DG |
2360 | goto end; |
2361 | } | |
2362 | ||
d6f42150 DG |
2363 | /* Set lttng run dir */ |
2364 | ret = chown(LTTNG_RUNDIR, 0, grp->gr_gid); | |
2365 | if (ret < 0) { | |
2366 | ERR("Unable to set group on " LTTNG_RUNDIR); | |
2367 | perror("chown"); | |
2368 | } | |
2369 | ||
2370 | /* lttng client socket path */ | |
fac6795d DG |
2371 | ret = chown(client_unix_sock_path, 0, grp->gr_gid); |
2372 | if (ret < 0) { | |
d6f42150 DG |
2373 | ERR("Unable to set group on %s", client_unix_sock_path); |
2374 | perror("chown"); | |
2375 | } | |
2376 | ||
2377 | /* kconsumerd error socket path */ | |
2378 | ret = chown(kconsumerd_err_unix_sock_path, 0, grp->gr_gid); | |
2379 | if (ret < 0) { | |
2380 | ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path); | |
fac6795d DG |
2381 | perror("chown"); |
2382 | } | |
2383 | ||
d6f42150 | 2384 | DBG("All permissions are set"); |
e07ae692 | 2385 | |
fac6795d DG |
2386 | end: |
2387 | return ret; | |
2388 | } | |
2389 | ||
7a485870 DG |
2390 | /* |
2391 | * create_kernel_poll_pipe | |
2392 | * | |
2393 | * Create the pipe used to wake up the kernel thread. | |
2394 | */ | |
2395 | static int create_kernel_poll_pipe(void) | |
2396 | { | |
2397 | return pipe2(kernel_poll_pipe, O_CLOEXEC); | |
2398 | } | |
2399 | ||
d6f42150 DG |
2400 | /* |
2401 | * create_lttng_rundir | |
2402 | * | |
2403 | * Create the lttng run directory needed for all | |
2404 | * global sockets and pipe. | |
2405 | */ | |
2406 | static int create_lttng_rundir(void) | |
2407 | { | |
2408 | int ret; | |
2409 | ||
2410 | ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG ); | |
2411 | if (ret < 0) { | |
b1f11e69 DG |
2412 | if (errno != EEXIST) { |
2413 | ERR("Unable to create " LTTNG_RUNDIR); | |
2414 | goto error; | |
2415 | } else { | |
2416 | ret = 0; | |
2417 | } | |
d6f42150 DG |
2418 | } |
2419 | ||
2420 | error: | |
2421 | return ret; | |
2422 | } | |
2423 | ||
2424 | /* | |
2425 | * set_kconsumerd_sockets | |
2426 | * | |
2427 | * Setup sockets and directory needed by the kconsumerd | |
2428 | * communication with the session daemon. | |
2429 | */ | |
2430 | static int set_kconsumerd_sockets(void) | |
2431 | { | |
2432 | int ret; | |
2433 | ||
2434 | if (strlen(kconsumerd_err_unix_sock_path) == 0) { | |
2435 | snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH); | |
2436 | } | |
2437 | ||
2438 | if (strlen(kconsumerd_cmd_unix_sock_path) == 0) { | |
2439 | snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH); | |
2440 | } | |
2441 | ||
2442 | ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG); | |
2443 | if (ret < 0) { | |
6beb2242 DG |
2444 | if (errno != EEXIST) { |
2445 | ERR("Failed to create " KCONSUMERD_PATH); | |
2446 | goto error; | |
2447 | } | |
2448 | ret = 0; | |
d6f42150 DG |
2449 | } |
2450 | ||
2451 | /* Create the kconsumerd error unix socket */ | |
2452 | kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path); | |
2453 | if (kconsumerd_err_sock < 0) { | |
2454 | ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path); | |
2455 | ret = -1; | |
2456 | goto error; | |
2457 | } | |
2458 | ||
2459 | /* File permission MUST be 660 */ | |
2460 | ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
2461 | if (ret < 0) { | |
2462 | ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path); | |
2463 | perror("chmod"); | |
2464 | goto error; | |
2465 | } | |
2466 | ||
2467 | error: | |
2468 | return ret; | |
2469 | } | |
2470 | ||
fac6795d | 2471 | /* |
62bd06d8 | 2472 | * sighandler |
fac6795d | 2473 | * |
62bd06d8 | 2474 | * Signal handler for the daemon |
fac6795d DG |
2475 | */ |
2476 | static void sighandler(int sig) | |
2477 | { | |
2478 | switch (sig) { | |
2479 | case SIGPIPE: | |
e07ae692 | 2480 | DBG("SIGPIPE catched"); |
87378cf5 | 2481 | return; |
fac6795d | 2482 | case SIGINT: |
e07ae692 DG |
2483 | DBG("SIGINT catched"); |
2484 | cleanup(); | |
2485 | break; | |
fac6795d | 2486 | case SIGTERM: |
e07ae692 | 2487 | DBG("SIGTERM catched"); |
fac6795d | 2488 | cleanup(); |
686204ab | 2489 | break; |
fac6795d DG |
2490 | default: |
2491 | break; | |
2492 | } | |
2493 | ||
2494 | exit(EXIT_SUCCESS); | |
2495 | } | |
2496 | ||
2497 | /* | |
1d4b027a | 2498 | * set_signal_handler |
fac6795d | 2499 | * |
1d4b027a DG |
2500 | * Setup signal handler for : |
2501 | * SIGINT, SIGTERM, SIGPIPE | |
fac6795d | 2502 | */ |
1d4b027a | 2503 | static int set_signal_handler(void) |
fac6795d | 2504 | { |
1d4b027a DG |
2505 | int ret = 0; |
2506 | struct sigaction sa; | |
2507 | sigset_t sigset; | |
fac6795d | 2508 | |
1d4b027a DG |
2509 | if ((ret = sigemptyset(&sigset)) < 0) { |
2510 | perror("sigemptyset"); | |
2511 | return ret; | |
2512 | } | |
d6f42150 | 2513 | |
1d4b027a DG |
2514 | sa.sa_handler = sighandler; |
2515 | sa.sa_mask = sigset; | |
2516 | sa.sa_flags = 0; | |
2517 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
2518 | perror("sigaction"); | |
2519 | return ret; | |
d6f42150 DG |
2520 | } |
2521 | ||
1d4b027a DG |
2522 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
2523 | perror("sigaction"); | |
2524 | return ret; | |
d6f42150 | 2525 | } |
aaf26714 | 2526 | |
1d4b027a DG |
2527 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
2528 | perror("sigaction"); | |
2529 | return ret; | |
8c0faa1d DG |
2530 | } |
2531 | ||
1d4b027a DG |
2532 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
2533 | ||
2534 | return ret; | |
fac6795d DG |
2535 | } |
2536 | ||
f3ed775e DG |
2537 | /* |
2538 | * set_ulimit | |
2539 | * | |
2540 | * Set open files limit to unlimited. This daemon can open a large number of | |
2541 | * file descriptors in order to consumer multiple kernel traces. | |
2542 | */ | |
2543 | static void set_ulimit(void) | |
2544 | { | |
2545 | int ret; | |
2546 | struct rlimit lim; | |
2547 | ||
a88df331 | 2548 | /* The kernel does not allowed an infinite limit for open files */ |
f3ed775e DG |
2549 | lim.rlim_cur = 65535; |
2550 | lim.rlim_max = 65535; | |
2551 | ||
2552 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
2553 | if (ret < 0) { | |
2554 | perror("failed to set open files limit"); | |
2555 | } | |
2556 | } | |
2557 | ||
fac6795d DG |
2558 | /* |
2559 | * main | |
2560 | */ | |
2561 | int main(int argc, char **argv) | |
2562 | { | |
fac6795d DG |
2563 | int ret = 0; |
2564 | void *status; | |
b082db07 | 2565 | const char *home_path; |
fac6795d | 2566 | |
273ea72c DG |
2567 | /* Create thread quit pipe */ |
2568 | if (init_thread_quit_pipe() < 0) { | |
a88df331 | 2569 | goto exit; |
273ea72c DG |
2570 | } |
2571 | ||
fac6795d DG |
2572 | /* Parse arguments */ |
2573 | progname = argv[0]; | |
2574 | if ((ret = parse_args(argc, argv) < 0)) { | |
a88df331 | 2575 | goto exit; |
fac6795d DG |
2576 | } |
2577 | ||
2578 | /* Daemonize */ | |
2579 | if (opt_daemon) { | |
53094c05 DG |
2580 | ret = daemon(0, 0); |
2581 | if (ret < 0) { | |
2582 | perror("daemon"); | |
a88df331 | 2583 | goto exit; |
53094c05 | 2584 | } |
fac6795d DG |
2585 | } |
2586 | ||
2587 | /* Check if daemon is UID = 0 */ | |
2588 | is_root = !getuid(); | |
2589 | ||
fac6795d | 2590 | if (is_root) { |
d6f42150 DG |
2591 | ret = create_lttng_rundir(); |
2592 | if (ret < 0) { | |
a88df331 | 2593 | goto exit; |
d6f42150 DG |
2594 | } |
2595 | ||
fac6795d | 2596 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 DG |
2597 | snprintf(apps_unix_sock_path, PATH_MAX, |
2598 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
fac6795d DG |
2599 | } |
2600 | ||
2601 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
2602 | snprintf(client_unix_sock_path, PATH_MAX, |
2603 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); | |
2604 | } | |
fac6795d | 2605 | } else { |
b082db07 DG |
2606 | home_path = get_home_dir(); |
2607 | if (home_path == NULL) { | |
273ea72c DG |
2608 | /* TODO: Add --socket PATH option */ |
2609 | ERR("Can't get HOME directory for sockets creation."); | |
a88df331 | 2610 | goto exit; |
b082db07 DG |
2611 | } |
2612 | ||
fac6795d | 2613 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 | 2614 | snprintf(apps_unix_sock_path, PATH_MAX, |
b082db07 | 2615 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
fac6795d DG |
2616 | } |
2617 | ||
2618 | /* Set the cli tool unix socket path */ | |
2619 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 | 2620 | snprintf(client_unix_sock_path, PATH_MAX, |
b082db07 | 2621 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
fac6795d DG |
2622 | } |
2623 | } | |
2624 | ||
847177cd DG |
2625 | DBG("Client socket path %s", client_unix_sock_path); |
2626 | DBG("Application socket path %s", apps_unix_sock_path); | |
2627 | ||
273ea72c DG |
2628 | /* |
2629 | * See if daemon already exist. If any of the two socket needed by the | |
2630 | * daemon are present, this test fails. However, if the daemon is killed | |
2631 | * with a SIGKILL, those unix socket must be unlinked by hand. | |
fac6795d DG |
2632 | */ |
2633 | if ((ret = check_existing_daemon()) == 0) { | |
75462a81 | 2634 | ERR("Already running daemon.\n"); |
273ea72c | 2635 | /* |
a88df331 DG |
2636 | * We do not goto error because we must not cleanup() because a daemon |
2637 | * is already running. | |
ab118b20 | 2638 | */ |
a88df331 DG |
2639 | goto exit; |
2640 | } | |
2641 | ||
2642 | /* After this point, we can safely call cleanup() so goto error is used */ | |
2643 | ||
2644 | /* | |
2645 | * These actions must be executed as root. We do that *after* setting up | |
2646 | * the sockets path because we MUST make the check for another daemon using | |
2647 | * those paths *before* trying to set the kernel consumer sockets and init | |
2648 | * kernel tracer. | |
2649 | */ | |
2650 | if (is_root) { | |
2651 | ret = set_kconsumerd_sockets(); | |
2652 | if (ret < 0) { | |
2653 | goto error; | |
2654 | } | |
2655 | ||
2656 | /* Setup kernel tracer */ | |
2657 | init_kernel_tracer(); | |
2658 | ||
2659 | /* Set ulimit for open files */ | |
2660 | set_ulimit(); | |
fac6795d DG |
2661 | } |
2662 | ||
2663 | if (set_signal_handler() < 0) { | |
2664 | goto error; | |
2665 | } | |
2666 | ||
d6f42150 | 2667 | /* Setup the needed unix socket */ |
fac6795d DG |
2668 | if (init_daemon_socket() < 0) { |
2669 | goto error; | |
2670 | } | |
2671 | ||
2672 | /* Set credentials to socket */ | |
d6f42150 | 2673 | if (is_root && (set_permissions() < 0)) { |
fac6795d DG |
2674 | goto error; |
2675 | } | |
2676 | ||
5b8719f5 DG |
2677 | /* Get parent pid if -S, --sig-parent is specified. */ |
2678 | if (opt_sig_parent) { | |
2679 | ppid = getppid(); | |
2680 | } | |
2681 | ||
7a485870 DG |
2682 | /* Setup the kernel pipe for waking up the kernel thread */ |
2683 | if (create_kernel_poll_pipe() < 0) { | |
2684 | goto error; | |
2685 | } | |
2686 | ||
273ea72c DG |
2687 | /* |
2688 | * Get session list pointer. This pointer MUST NOT be free(). | |
2689 | * This list is statically declared in session.c | |
2690 | */ | |
b5541356 DG |
2691 | session_list_ptr = get_session_list(); |
2692 | ||
fac6795d DG |
2693 | while (1) { |
2694 | /* Create thread to manage the client socket */ | |
1d4b027a | 2695 | ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL); |
fac6795d DG |
2696 | if (ret != 0) { |
2697 | perror("pthread_create"); | |
2698 | goto error; | |
2699 | } | |
2700 | ||
2701 | /* Create thread to manage application socket */ | |
1d4b027a | 2702 | ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL); |
fac6795d DG |
2703 | if (ret != 0) { |
2704 | perror("pthread_create"); | |
2705 | goto error; | |
2706 | } | |
2707 | ||
7a485870 DG |
2708 | /* Create kernel thread to manage kernel event */ |
2709 | ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL); | |
2710 | if (ret != 0) { | |
2711 | perror("pthread_create"); | |
2712 | goto error; | |
2713 | } | |
2714 | ||
1d4b027a DG |
2715 | ret = pthread_join(client_thread, &status); |
2716 | if (ret != 0) { | |
2717 | perror("pthread_join"); | |
2718 | goto error; | |
fac6795d DG |
2719 | } |
2720 | } | |
2721 | ||
2722 | cleanup(); | |
5e16da05 | 2723 | exit(EXIT_SUCCESS); |
fac6795d DG |
2724 | |
2725 | error: | |
2726 | cleanup(); | |
a88df331 DG |
2727 | |
2728 | exit: | |
5e16da05 | 2729 | exit(EXIT_FAILURE); |
fac6795d | 2730 | } |