From: David Goulet Date: Fri, 2 Mar 2012 20:00:16 +0000 (-0500) Subject: Merge branch 'master' of git://git.lttng.org/lttng-tools X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=commitdiff_plain;h=e71aad1fa4b06a5f91ddceace42366f3d79bd77e;hp=1af0e2e4d4a770e32f4fc169122b0c338bd799f6 Merge branch 'master' of git://git.lttng.org/lttng-tools --- diff --git a/src/bin/lttng-sessiond/kernel.c b/src/bin/lttng-sessiond/kernel.c index 8e960dfed..fea51b761 100644 --- a/src/bin/lttng-sessiond/kernel.c +++ b/src/bin/lttng-sessiond/kernel.c @@ -42,7 +42,7 @@ int kernel_add_channel_context(struct ltt_kernel_channel *chan, ret = kernctl_add_context(chan->fd, ctx); if (ret < 0) { if (errno != EEXIST) { - perror("add context ioctl"); + PERROR("add context ioctl"); } else { /* If EEXIST, we just ignore the error */ ret = 0; @@ -52,7 +52,7 @@ int kernel_add_channel_context(struct ltt_kernel_channel *chan, chan->ctx = zmalloc(sizeof(struct lttng_kernel_context)); if (chan->ctx == NULL) { - perror("zmalloc event context"); + PERROR("zmalloc event context"); goto error; } @@ -75,13 +75,13 @@ int kernel_add_event_context(struct ltt_kernel_event *event, DBG("Adding context to event %s", event->event->name); ret = kernctl_add_context(event->fd, ctx); if (ret < 0) { - perror("add context ioctl"); + PERROR("add context ioctl"); goto error; } event->ctx = zmalloc(sizeof(struct lttng_kernel_context)); if (event->ctx == NULL) { - perror("zmalloc event context"); + PERROR("zmalloc event context"); goto error; } @@ -112,7 +112,7 @@ int kernel_create_session(struct ltt_session *session, int tracer_fd) /* Kernel tracer session creation */ ret = kernctl_create_session(tracer_fd); if (ret < 0) { - perror("ioctl kernel create session"); + PERROR("ioctl kernel create session"); goto error; } @@ -120,7 +120,7 @@ int kernel_create_session(struct ltt_session *session, int tracer_fd) /* Prevent fd duplication after execlp() */ ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); if (ret < 0) { - perror("fcntl session fd"); + PERROR("fcntl session fd"); } lks->consumer_fds_sent = 0; @@ -153,7 +153,7 @@ int kernel_create_channel(struct ltt_kernel_session *session, /* Kernel tracer channel creation */ ret = kernctl_create_channel(session->fd, &lkc->channel->attr); if (ret < 0) { - perror("ioctl kernel create channel"); + PERROR("ioctl kernel create channel"); goto error; } @@ -162,7 +162,7 @@ int kernel_create_channel(struct ltt_kernel_session *session, /* Prevent fd duplication after execlp() */ ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC); if (ret < 0) { - perror("fcntl session fd"); + PERROR("fcntl session fd"); } /* Add channel to session */ @@ -208,6 +208,11 @@ int kernel_create_event(struct lttng_event *ev, */ if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) { DBG2("Kernel event syscall creation success"); + /* + * We use fd == -1 to ensure that we never trigger a close of fd + * 0. + */ + event->fd = -1; goto add_list; } @@ -215,7 +220,7 @@ int kernel_create_event(struct lttng_event *ev, /* Prevent fd duplication after execlp() */ ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC); if (ret < 0) { - perror("fcntl session fd"); + PERROR("fcntl session fd"); } add_list: @@ -242,7 +247,7 @@ int kernel_disable_channel(struct ltt_kernel_channel *chan) ret = kernctl_disable(chan->fd); if (ret < 0) { - perror("disable chan ioctl"); + PERROR("disable chan ioctl"); ret = errno; goto error; } @@ -265,7 +270,7 @@ int kernel_enable_channel(struct ltt_kernel_channel *chan) ret = kernctl_enable(chan->fd); if (ret < 0 && errno != EEXIST) { - perror("Enable kernel chan"); + PERROR("Enable kernel chan"); goto error; } @@ -287,7 +292,7 @@ int kernel_enable_event(struct ltt_kernel_event *event) ret = kernctl_enable(event->fd); if (ret < 0 && errno != EEXIST) { - perror("enable kernel event"); + PERROR("enable kernel event"); goto error; } @@ -309,7 +314,7 @@ int kernel_disable_event(struct ltt_kernel_event *event) ret = kernctl_disable(event->fd); if (ret < 0 && errno != EEXIST) { - perror("disable kernel event"); + PERROR("disable kernel event"); goto error; } @@ -347,7 +352,7 @@ int kernel_open_metadata(struct ltt_kernel_session *session, char *path) /* Prevent fd duplication after execlp() */ ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); if (ret < 0) { - perror("fcntl session fd"); + PERROR("fcntl session fd"); } session->metadata = lkm; @@ -369,7 +374,7 @@ int kernel_start_session(struct ltt_kernel_session *session) ret = kernctl_start_session(session->fd); if (ret < 0) { - perror("ioctl start session"); + PERROR("ioctl start session"); goto error; } @@ -392,7 +397,7 @@ void kernel_wait_quiescent(int fd) ret = kernctl_wait_quiescent(fd); if (ret < 0) { - perror("wait quiescent ioctl"); + PERROR("wait quiescent ioctl"); ERR("Kernel quiescent wait failed"); } } @@ -406,7 +411,7 @@ int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate) ret = kernctl_calibrate(fd, calibrate); if (ret < 0) { - perror("calibrate ioctl"); + PERROR("calibrate ioctl"); return -1; } @@ -443,7 +448,7 @@ int kernel_flush_buffer(struct ltt_kernel_channel *channel) DBG("Flushing channel stream %d", stream->fd); ret = kernctl_buffer_flush(stream->fd); if (ret < 0) { - perror("ioctl"); + PERROR("ioctl"); ERR("Fail to flush buffer for stream %d (ret: %d)", stream->fd, ret); } @@ -483,10 +488,13 @@ int kernel_open_channel_stream(struct ltt_kernel_channel *channel) int ret; struct ltt_kernel_stream *lks; - while ((ret = kernctl_create_stream(channel->fd)) > 0) { + while ((ret = kernctl_create_stream(channel->fd)) >= 0) { lks = trace_kernel_create_stream(); if (lks == NULL) { - close(ret); + ret = close(ret); + if (ret) { + PERROR("close"); + } goto error; } @@ -494,13 +502,13 @@ int kernel_open_channel_stream(struct ltt_kernel_channel *channel) /* Prevent fd duplication after execlp() */ ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); if (ret < 0) { - perror("fcntl session fd"); + PERROR("fcntl session fd"); } ret = asprintf(&lks->pathname, "%s/%s_%d", channel->pathname, channel->channel->name, channel->stream_count); if (ret < 0) { - perror("asprintf kernel create stream"); + PERROR("asprintf kernel create stream"); goto error; } @@ -527,7 +535,7 @@ int kernel_open_metadata_stream(struct ltt_kernel_session *session) ret = kernctl_create_stream(session->metadata->fd); if (ret < 0) { - perror("kernel create metadata stream"); + PERROR("kernel create metadata stream"); goto error; } @@ -536,7 +544,7 @@ int kernel_open_metadata_stream(struct ltt_kernel_session *session) /* Prevent fd duplication after execlp() */ ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); if (ret < 0) { - perror("fcntl session fd"); + PERROR("fcntl session fd"); } return 0; @@ -550,7 +558,7 @@ error: */ ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) { - int fd, pos; + int fd, pos, ret; char *event; size_t nbmem, count = 0; ssize_t size; @@ -559,13 +567,13 @@ ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) fd = kernctl_tracepoint_list(tracer_fd); if (fd < 0) { - perror("kernel tracepoint list"); + PERROR("kernel tracepoint list"); goto error; } fp = fdopen(fd, "r"); if (fp == NULL) { - perror("kernel tracepoint list fdopen"); + PERROR("kernel tracepoint list fdopen"); goto error_fp; } @@ -584,7 +592,7 @@ ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) nbmem <<= 1; elist = realloc(elist, nbmem * sizeof(struct lttng_event)); if (elist == NULL) { - perror("realloc list events"); + PERROR("realloc list events"); count = -ENOMEM; goto end; } @@ -598,11 +606,17 @@ ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) *events = elist; DBG("Kernel list events done (%zu events)", count); end: - fclose(fp); /* closes both fp and fd */ + ret = fclose(fp); /* closes both fp and fd */ + if (ret) { + PERROR("fclose"); + } return count; error_fp: - close(fd); + ret = close(fd); + if (ret) { + PERROR("close"); + } error: return -1; } @@ -664,8 +678,10 @@ int init_kernel_workarounds(void) /* Ignore error, we don't really care */ } } - fclose(fp); + ret = fclose(fp); + if (ret) { + PERROR("fclose"); + } end_boot_id: - return 0; } diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index 8654031f1..2ef18ae09 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -122,22 +122,22 @@ static char client_unix_sock_path[PATH_MAX]; static char wait_shm_path[PATH_MAX]; /* Sockets and FDs */ -static int client_sock; -static int apps_sock; -static int kernel_tracer_fd; -static int kernel_poll_pipe[2]; +static int client_sock = -1; +static int apps_sock = -1; +static int kernel_tracer_fd = -1; +static int kernel_poll_pipe[2] = { -1, -1 }; /* * Quit pipe for all threads. This permits a single cancellation point * for all threads when receiving an event on the pipe. */ -static int thread_quit_pipe[2]; +static int thread_quit_pipe[2] = { -1, -1 }; /* * This pipe is used to inform the thread managing application communication * that a command is queued and ready to be processed. */ -static int apps_cmd_pipe[2]; +static int apps_cmd_pipe[2] = { -1, -1 }; /* Pthread, Mutexes and Semaphores */ static pthread_t apps_thread; @@ -386,7 +386,7 @@ static void stop_threads(void) */ static void cleanup(void) { - int ret; + int ret, i; char *cmd; struct ltt_session *sess, *stmp; @@ -427,13 +427,44 @@ static void cleanup(void) if (is_root && !opt_no_kernel) { DBG2("Closing kernel fd"); - close(kernel_tracer_fd); + if (kernel_tracer_fd >= 0) { + ret = close(kernel_tracer_fd); + if (ret) { + PERROR("close"); + } + } DBG("Unloading kernel modules"); modprobe_remove_lttng_all(); } - close(thread_quit_pipe[0]); - close(thread_quit_pipe[1]); + /* + * Closing all pipes used for communication between threads. + */ + for (i = 0; i < 2; i++) { + if (kernel_poll_pipe[i] >= 0) { + ret = close(kernel_poll_pipe[i]); + if (ret) { + PERROR("close"); + } + + } + } + for (i = 0; i < 2; i++) { + if (thread_quit_pipe[i] >= 0) { + ret = close(thread_quit_pipe[i]); + if (ret) { + PERROR("close"); + } + } + } + for (i = 0; i < 2; i++) { + if (apps_cmd_pipe[i] >= 0) { + ret = close(apps_cmd_pipe[i]); + if (ret) { + PERROR("close"); + } + } + } /* */ DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm" @@ -497,7 +528,7 @@ static int send_kconsumer_channel_streams(struct consumer_data *consumer_data, DBG("Sending channel %d to consumer", lkm.u.channel.channel_key); ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); if (ret < 0) { - perror("send consumer channel"); + PERROR("send consumer channel"); goto error; } @@ -519,12 +550,12 @@ static int send_kconsumer_channel_streams(struct consumer_data *consumer_data, DBG("Sending stream %d to consumer", lkm.u.stream.stream_key); ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); if (ret < 0) { - perror("send consumer stream"); + PERROR("send consumer stream"); goto error; } ret = lttcomm_send_fds_unix_sock(sock, &stream->fd, 1); if (ret < 0) { - perror("send consumer stream ancillary data"); + PERROR("send consumer stream ancillary data"); goto error; } } @@ -564,7 +595,7 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data, DBG("Sending metadata channel %d to consumer", lkm.u.stream.stream_key); ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); if (ret < 0) { - perror("send consumer channel"); + PERROR("send consumer channel"); goto error; } @@ -582,12 +613,12 @@ static int send_kconsumer_session_streams(struct consumer_data *consumer_data, DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key); ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); if (ret < 0) { - perror("send consumer stream"); + PERROR("send consumer stream"); goto error; } ret = lttcomm_send_fds_unix_sock(sock, &session->metadata_stream_fd, 1); if (ret < 0) { - perror("send consumer stream"); + PERROR("send consumer stream"); goto error; } } @@ -648,7 +679,7 @@ static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size); if (cmd_ctx->llm == NULL) { - perror("zmalloc"); + PERROR("zmalloc"); ret = -ENOMEM; goto error; } @@ -808,7 +839,7 @@ static void *thread_manage_kernel(void *data) ret = create_thread_poll_set(&events, 2); if (ret < 0) { - goto error; + goto error_poll_create; } ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); @@ -893,12 +924,9 @@ static void *thread_manage_kernel(void *data) } error: - DBG("Kernel thread dying"); - close(kernel_poll_pipe[0]); - close(kernel_poll_pipe[1]); - lttng_poll_clean(&events); - +error_poll_create: + DBG("Kernel thread dying"); return NULL; } @@ -907,7 +935,7 @@ error: */ static void *thread_manage_consumer(void *data) { - int sock = 0, i, ret, pollfd; + int sock = -1, i, ret, pollfd; uint32_t revents, nb_fd; enum lttcomm_return_code code; struct lttng_poll_event events; @@ -917,7 +945,7 @@ static void *thread_manage_consumer(void *data) ret = lttcomm_listen_unix_sock(consumer_data->err_sock); if (ret < 0) { - goto error; + goto error_listen; } /* @@ -926,7 +954,7 @@ static void *thread_manage_consumer(void *data) */ ret = create_thread_poll_set(&events, 2); if (ret < 0) { - goto error; + goto error_poll; } ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); @@ -1058,16 +1086,33 @@ restart_poll: ERR("consumer return code : %s", lttcomm_get_readable_code(-code)); error: - DBG("consumer thread dying"); - close(consumer_data->err_sock); - close(consumer_data->cmd_sock); - close(sock); + if (consumer_data->err_sock >= 0) { + ret = close(consumer_data->err_sock); + if (ret) { + PERROR("close"); + } + } + if (consumer_data->cmd_sock >= 0) { + ret = close(consumer_data->cmd_sock); + if (ret) { + PERROR("close"); + } + } + if (sock >= 0) { + ret = close(sock); + if (ret) { + PERROR("close"); + } + } unlink(consumer_data->err_unix_sock_path); unlink(consumer_data->cmd_unix_sock_path); consumer_data->pid = 0; lttng_poll_clean(&events); +error_poll: +error_listen: + DBG("consumer thread cleanup completed"); return NULL; } @@ -1089,7 +1134,7 @@ static void *thread_manage_apps(void *data) ret = create_thread_poll_set(&events, 2); if (ret < 0) { - goto error; + goto error_poll_create; } ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); @@ -1138,7 +1183,7 @@ static void *thread_manage_apps(void *data) /* Empty pipe */ ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd)); if (ret < 0 || ret < sizeof(ust_cmd)) { - perror("read apps cmd pipe"); + PERROR("read apps cmd pipe"); goto error; } @@ -1209,12 +1254,9 @@ static void *thread_manage_apps(void *data) } error: - DBG("Application communication apps dying"); - close(apps_cmd_pipe[0]); - close(apps_cmd_pipe[1]); - lttng_poll_clean(&events); - +error_poll_create: + DBG("Application communication apps thread cleanup complete"); rcu_thread_offline(); rcu_unregister_thread(); return NULL; @@ -1261,7 +1303,7 @@ static void *thread_dispatch_ust_registration(void *data) ret = write(apps_cmd_pipe[1], ust_cmd, sizeof(struct ust_command)); if (ret < 0) { - perror("write apps cmd pipe"); + PERROR("write apps cmd pipe"); if (errno == EBADF) { /* * We can't inform the application thread to process @@ -1289,7 +1331,7 @@ error: */ static void *thread_registration_apps(void *data) { - int sock = 0, i, ret, pollfd; + int sock = -1, i, ret, pollfd; uint32_t revents, nb_fd; struct lttng_poll_event events; /* @@ -1302,7 +1344,7 @@ static void *thread_registration_apps(void *data) ret = lttcomm_listen_unix_sock(apps_sock); if (ret < 0) { - goto error; + goto error_listen; } /* @@ -1311,13 +1353,13 @@ static void *thread_registration_apps(void *data) */ ret = create_thread_poll_set(&events, 2); if (ret < 0) { - goto error; + goto error_create_poll; } /* Add the application registration socket */ ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); if (ret < 0) { - goto error; + goto error_poll_add; } /* Notify all applications to register */ @@ -1371,7 +1413,7 @@ static void *thread_registration_apps(void *data) /* Create UST registration command for enqueuing */ ust_cmd = zmalloc(sizeof(struct ust_command)); if (ust_cmd == NULL) { - perror("ust command zmalloc"); + PERROR("ust command zmalloc"); goto error; } @@ -1383,12 +1425,16 @@ static void *thread_registration_apps(void *data) sizeof(struct ust_register_msg)); if (ret < 0 || ret < sizeof(struct ust_register_msg)) { if (ret < 0) { - perror("lttcomm_recv_unix_sock register apps"); + PERROR("lttcomm_recv_unix_sock register apps"); } else { ERR("Wrong size received on apps register"); } free(ust_cmd); - close(sock); + ret = close(sock); + if (ret) { + PERROR("close"); + } + sock = -1; continue; } @@ -1418,16 +1464,28 @@ static void *thread_registration_apps(void *data) } error: - DBG("UST Registration thread dying"); - /* Notify that the registration thread is gone */ notify_ust_apps(0); - close(apps_sock); - close(sock); + if (apps_sock >= 0) { + ret = close(apps_sock); + if (ret) { + PERROR("close"); + } + } + if (clock >= 0) { + ret = close(sock); + if (ret) { + PERROR("close"); + } + } unlink(apps_unix_sock_path); +error_poll_add: lttng_poll_clean(&events); +error_listen: +error_create_poll: + DBG("UST Registration thread cleanup complete"); return NULL; } @@ -1667,17 +1725,17 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data) break; } default: - perror("unknown consumer type"); + PERROR("unknown consumer type"); exit(EXIT_FAILURE); } if (errno != 0) { - perror("kernel start consumer exec"); + PERROR("kernel start consumer exec"); } exit(EXIT_FAILURE); } else if (pid > 0) { ret = pid; } else { - perror("start consumer fork"); + PERROR("start consumer fork"); ret = -errno; } error: @@ -1768,19 +1826,25 @@ static int init_kernel_tracer(void) error_version: modprobe_remove_lttng_control(); - close(kernel_tracer_fd); - kernel_tracer_fd = 0; + ret = close(kernel_tracer_fd); + if (ret) { + PERROR("close"); + } + kernel_tracer_fd = -1; return LTTCOMM_KERN_VERSION; error_modules: - close(kernel_tracer_fd); + ret = close(kernel_tracer_fd); + if (ret) { + PERROR("close"); + } error_open: modprobe_remove_lttng_control(); error: WARN("No kernel tracer available"); - kernel_tracer_fd = 0; + kernel_tracer_fd = -1; return LTTCOMM_KERN_NA; } @@ -2951,7 +3015,7 @@ static int cmd_destroy_session(struct ltt_session *session, char *name) */ ret = notify_thread_pipe(kernel_poll_pipe[1]); if (ret < 0) { - perror("write kernel poll pipe"); + PERROR("write kernel poll pipe"); } ret = session_destroy(session); @@ -3258,7 +3322,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx) } /* Kernel tracer check */ - if (kernel_tracer_fd == 0) { + if (kernel_tracer_fd == -1) { /* Basically, load kernel tracer modules */ ret = init_kernel_tracer(); if (ret != 0) { @@ -3602,7 +3666,7 @@ init_setup_error: */ static void *thread_manage_clients(void *data) { - int sock = 0, ret, i, pollfd; + int sock = -1, ret, i, pollfd; uint32_t revents, nb_fd; struct command_ctx *cmd_ctx = NULL; struct lttng_poll_event events; @@ -3692,14 +3756,14 @@ static void *thread_manage_clients(void *data) /* Allocate context command to process the client request */ cmd_ctx = zmalloc(sizeof(struct command_ctx)); if (cmd_ctx == NULL) { - perror("zmalloc cmd_ctx"); + PERROR("zmalloc cmd_ctx"); goto error; } /* Allocate data buffer for reception */ cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg)); if (cmd_ctx->lsm == NULL) { - perror("zmalloc cmd_ctx->lsm"); + PERROR("zmalloc cmd_ctx->lsm"); goto error; } @@ -3716,7 +3780,11 @@ static void *thread_manage_clients(void *data) sizeof(struct lttcomm_session_msg), &cmd_ctx->creds); if (ret <= 0) { DBG("Nothing recv() from client... continuing"); - close(sock); + ret = close(sock); + if (ret) { + PERROR("close"); + } + sock = -1; free(cmd_ctx); continue; } @@ -3752,7 +3820,11 @@ static void *thread_manage_clients(void *data) } /* End of transmission */ - close(sock); + ret = close(sock); + if (ret) { + PERROR("close"); + } + sock = -1; clean_command_ctx(&cmd_ctx); } @@ -3760,8 +3832,18 @@ static void *thread_manage_clients(void *data) error: DBG("Client thread dying"); unlink(client_unix_sock_path); - close(client_sock); - close(sock); + if (client_sock >= 0) { + ret = close(client_sock); + if (ret) { + PERROR("close"); + } + } + if (sock >= 0) { + ret = close(sock); + if (ret) { + PERROR("close"); + } + } lttng_poll_clean(&events); clean_command_ctx(&cmd_ctx); @@ -3945,7 +4027,7 @@ static int init_daemon_socket(void) ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (ret < 0) { ERR("Set file permissions failed: %s", client_unix_sock_path); - perror("chmod"); + PERROR("chmod"); goto end; } @@ -3962,7 +4044,7 @@ static int init_daemon_socket(void) S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (ret < 0) { ERR("Set file permissions failed: %s", apps_unix_sock_path); - perror("chmod"); + PERROR("chmod"); goto end; } @@ -4007,42 +4089,42 @@ static int set_permissions(char *rundir) ret = chown(rundir, 0, gid); if (ret < 0) { ERR("Unable to set group on %s", rundir); - perror("chown"); + PERROR("chown"); } /* Ensure tracing group can search the run dir */ ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH); if (ret < 0) { ERR("Unable to set permissions on %s", rundir); - perror("chmod"); + PERROR("chmod"); } /* lttng client socket path */ ret = chown(client_unix_sock_path, 0, gid); if (ret < 0) { ERR("Unable to set group on %s", client_unix_sock_path); - perror("chown"); + PERROR("chown"); } /* kconsumer error socket path */ ret = chown(kconsumer_data.err_unix_sock_path, 0, gid); if (ret < 0) { ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); - perror("chown"); + PERROR("chown"); } /* 64-bit ustconsumer error socket path */ ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid); if (ret < 0) { ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); - perror("chown"); + PERROR("chown"); } /* 32-bit ustconsumer compat32 error socket path */ ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid); if (ret < 0) { ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); - perror("chown"); + PERROR("chown"); } DBG("All permissions are set"); @@ -4053,6 +4135,7 @@ end: /* * Create the pipe used to wake up the kernel thread. + * Closed in cleanup(). */ static int create_kernel_poll_pipe(void) { @@ -4078,6 +4161,7 @@ error: /* * Create the application command pipe to wake thread_manage_apps. + * Closed in cleanup(). */ static int create_apps_cmd_pipe(void) { @@ -4219,7 +4303,7 @@ static int set_signal_handler(void) sigset_t sigset; if ((ret = sigemptyset(&sigset)) < 0) { - perror("sigemptyset"); + PERROR("sigemptyset"); return ret; } @@ -4227,17 +4311,17 @@ static int set_signal_handler(void) sa.sa_mask = sigset; sa.sa_flags = 0; if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { - perror("sigaction"); + PERROR("sigaction"); return ret; } if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { - perror("sigaction"); + PERROR("sigaction"); return ret; } if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { - perror("sigaction"); + PERROR("sigaction"); return ret; } @@ -4261,7 +4345,7 @@ static void set_ulimit(void) ret = setrlimit(RLIMIT_NOFILE, &lim); if (ret < 0) { - perror("failed to set open files limit"); + PERROR("failed to set open files limit"); } } @@ -4295,7 +4379,7 @@ int main(int argc, char **argv) if (opt_daemon) { ret = daemon(0, 0); if (ret < 0) { - perror("daemon"); + PERROR("daemon"); goto error; } } @@ -4499,7 +4583,7 @@ int main(int argc, char **argv) ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL); if (ret != 0) { - perror("pthread_create clients"); + PERROR("pthread_create clients"); goto exit_client; } @@ -4507,7 +4591,7 @@ int main(int argc, char **argv) ret = pthread_create(&dispatch_thread, NULL, thread_dispatch_ust_registration, (void *) NULL); if (ret != 0) { - perror("pthread_create dispatch"); + PERROR("pthread_create dispatch"); goto exit_dispatch; } @@ -4515,7 +4599,7 @@ int main(int argc, char **argv) ret = pthread_create(®_apps_thread, NULL, thread_registration_apps, (void *) NULL); if (ret != 0) { - perror("pthread_create registration"); + PERROR("pthread_create registration"); goto exit_reg_apps; } @@ -4523,7 +4607,7 @@ int main(int argc, char **argv) ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL); if (ret != 0) { - perror("pthread_create apps"); + PERROR("pthread_create apps"); goto exit_apps; } @@ -4531,47 +4615,47 @@ int main(int argc, char **argv) ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL); if (ret != 0) { - perror("pthread_create kernel"); + PERROR("pthread_create kernel"); goto exit_kernel; } ret = pthread_join(kernel_thread, &status); if (ret != 0) { - perror("pthread_join"); + PERROR("pthread_join"); goto error; /* join error, exit without cleanup */ } exit_kernel: ret = pthread_join(apps_thread, &status); if (ret != 0) { - perror("pthread_join"); + PERROR("pthread_join"); goto error; /* join error, exit without cleanup */ } exit_apps: ret = pthread_join(reg_apps_thread, &status); if (ret != 0) { - perror("pthread_join"); + PERROR("pthread_join"); goto error; /* join error, exit without cleanup */ } exit_reg_apps: ret = pthread_join(dispatch_thread, &status); if (ret != 0) { - perror("pthread_join"); + PERROR("pthread_join"); goto error; /* join error, exit without cleanup */ } exit_dispatch: ret = pthread_join(client_thread, &status); if (ret != 0) { - perror("pthread_join"); + PERROR("pthread_join"); goto error; /* join error, exit without cleanup */ } ret = join_consumer_thread(&kconsumer_data); if (ret != 0) { - perror("join_consumer"); + PERROR("join_consumer"); goto error; /* join error, exit without cleanup */ } diff --git a/src/bin/lttng-sessiond/modprobe.c b/src/bin/lttng-sessiond/modprobe.c index a87d70754..65032e414 100644 --- a/src/bin/lttng-sessiond/modprobe.c +++ b/src/bin/lttng-sessiond/modprobe.c @@ -101,7 +101,7 @@ void modprobe_remove_lttng_data(void) "/sbin/modprobe -r -q %s", kern_modules_list[i].name); if (ret < 0) { - perror("snprintf modprobe -r"); + PERROR("snprintf modprobe -r"); goto error; } modprobe[sizeof(modprobe) - 1] = '\0'; @@ -182,7 +182,7 @@ int modprobe_lttng_data(void) kern_modules_list[i].required ? "" : "-q ", kern_modules_list[i].name); if (ret < 0) { - perror("snprintf modprobe"); + PERROR("snprintf modprobe"); goto error; } modprobe[sizeof(modprobe) - 1] = '\0'; diff --git a/src/bin/lttng-sessiond/session.c b/src/bin/lttng-sessiond/session.c index e890d07ba..e7fc7c35a 100644 --- a/src/bin/lttng-sessiond/session.c +++ b/src/bin/lttng-sessiond/session.c @@ -177,7 +177,7 @@ int session_create(char *name, char *path, uid_t uid, gid_t gid) /* Allocate session data structure */ new_session = zmalloc(sizeof(struct ltt_session)); if (new_session == NULL) { - perror("zmalloc"); + PERROR("zmalloc"); ret = LTTCOMM_FATAL; goto error_malloc; } diff --git a/src/bin/lttng-sessiond/shm.c b/src/bin/lttng-sessiond/shm.c index 55d00e67f..432d07ece 100644 --- a/src/bin/lttng-sessiond/shm.c +++ b/src/bin/lttng-sessiond/shm.c @@ -51,7 +51,7 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) ret = chown(shm_path, 0, 0); if (ret < 0) { if (errno != ENOENT) { - perror("chown wait shm"); + PERROR("chown wait shm"); goto error; } } @@ -65,7 +65,7 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) ret = chown(shm_path, getuid(), getgid()); if (ret < 0) { if (errno != ENOENT) { - perror("chown wait shm"); + PERROR("chown wait shm"); goto error; } } @@ -77,7 +77,7 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) ret = chmod(shm_path, mode); if (ret < 0) { if (errno != ENOENT) { - perror("chmod wait shm"); + PERROR("chmod wait shm"); goto error; } } @@ -94,20 +94,20 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) */ wait_shm_fd = shm_open(shm_path, O_RDWR | O_CREAT, mode); if (wait_shm_fd < 0) { - perror("shm_open wait shm"); + PERROR("shm_open wait shm"); goto error; } ret = ftruncate(wait_shm_fd, mmap_size); if (ret < 0) { - perror("ftruncate wait shm"); + PERROR("ftruncate wait shm"); exit(EXIT_FAILURE); } #ifndef __FreeBSD__ ret = fchmod(wait_shm_fd, mode); if (ret < 0) { - perror("fchmod"); + PERROR("fchmod"); exit(EXIT_FAILURE); } #else @@ -149,7 +149,7 @@ char *shm_ust_get_mmap(char *shm_path, int global) /* close shm fd immediately after taking the mmap reference */ ret = close(wait_shm_fd); if (ret) { - perror("Error closing fd"); + PERROR("Error closing fd"); } if (wait_shm_mmap == MAP_FAILED) { diff --git a/src/bin/lttng-sessiond/trace-kernel.c b/src/bin/lttng-sessiond/trace-kernel.c index 36f7c9708..8fb92f12a 100644 --- a/src/bin/lttng-sessiond/trace-kernel.c +++ b/src/bin/lttng-sessiond/trace-kernel.c @@ -91,7 +91,7 @@ struct ltt_kernel_session *trace_kernel_create_session(char *path) /* Allocate a new ltt kernel session */ lks = zmalloc(sizeof(struct ltt_kernel_session)); if (lks == NULL) { - perror("create kernel session zmalloc"); + PERROR("create kernel session zmalloc"); goto error; } @@ -107,7 +107,7 @@ struct ltt_kernel_session *trace_kernel_create_session(char *path) /* Set session path */ ret = asprintf(&lks->trace_path, "%s/kernel", path); if (ret < 0) { - perror("asprintf kernel traces path"); + PERROR("asprintf kernel traces path"); goto error; } @@ -129,13 +129,13 @@ struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *cha lkc = zmalloc(sizeof(struct ltt_kernel_channel)); if (lkc == NULL) { - perror("ltt_kernel_channel zmalloc"); + PERROR("ltt_kernel_channel zmalloc"); goto error; } lkc->channel = zmalloc(sizeof(struct lttng_channel)); if (lkc->channel == NULL) { - perror("lttng_channel zmalloc"); + PERROR("lttng_channel zmalloc"); goto error; } memcpy(lkc->channel, chan, sizeof(struct lttng_channel)); @@ -151,7 +151,7 @@ struct ltt_kernel_channel *trace_kernel_create_channel(struct lttng_channel *cha /* Set default trace output path */ ret = asprintf(&lkc->pathname, "%s", path); if (ret < 0) { - perror("asprintf kernel create channel"); + PERROR("asprintf kernel create channel"); goto error; } @@ -174,7 +174,7 @@ struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev) lke = zmalloc(sizeof(struct ltt_kernel_event)); attr = zmalloc(sizeof(struct lttng_kernel_event)); if (lke == NULL || attr == NULL) { - perror("kernel event zmalloc"); + PERROR("kernel event zmalloc"); goto error; } @@ -246,7 +246,7 @@ struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path) lkm = zmalloc(sizeof(struct ltt_kernel_metadata)); chan = zmalloc(sizeof(struct lttng_channel)); if (lkm == NULL || chan == NULL) { - perror("kernel metadata zmalloc"); + PERROR("kernel metadata zmalloc"); goto error; } @@ -264,7 +264,7 @@ struct ltt_kernel_metadata *trace_kernel_create_metadata(char *path) /* Set default metadata path */ ret = asprintf(&lkm->pathname, "%s/metadata", path); if (ret < 0) { - perror("asprintf kernel metadata"); + PERROR("asprintf kernel metadata"); goto error; } @@ -286,7 +286,7 @@ struct ltt_kernel_stream *trace_kernel_create_stream(void) lks = zmalloc(sizeof(struct ltt_kernel_stream)); if (lks == NULL) { - perror("kernel stream zmalloc"); + PERROR("kernel stream zmalloc"); goto error; } @@ -306,9 +306,14 @@ error: */ void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream) { + int ret; + DBG("[trace] Closing stream fd %d", stream->fd); /* Close kernel fd */ - close(stream->fd); + ret = close(stream->fd); + if (ret) { + PERROR("close"); + } /* Remove from stream list */ cds_list_del(&stream->list); @@ -321,9 +326,18 @@ void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream) */ void trace_kernel_destroy_event(struct ltt_kernel_event *event) { - DBG("[trace] Closing event fd %d", event->fd); - /* Close kernel fd */ - close(event->fd); + int ret; + + if (event->fd >= 0) { + DBG("[trace] Closing event fd %d", event->fd); + /* Close kernel fd */ + ret = close(event->fd); + if (ret) { + PERROR("close"); + } + } else { + DBG("[trace] Tearing down event (no associated fd)"); + } /* Remove from event list */ cds_list_del(&event->list); @@ -340,10 +354,14 @@ void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel) { struct ltt_kernel_stream *stream, *stmp; struct ltt_kernel_event *event, *etmp; + int ret; DBG("[trace] Closing channel fd %d", channel->fd); /* Close kernel fd */ - close(channel->fd); + ret = close(channel->fd); + if (ret) { + PERROR("close"); + } /* For each stream in the channel list */ cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) { @@ -369,9 +387,14 @@ void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel) */ void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata) { + int ret; + DBG("[trace] Closing metadata fd %d", metadata->fd); /* Close kernel fd */ - close(metadata->fd); + ret = close(metadata->fd); + if (ret) { + PERROR("close"); + } free(metadata->conf); free(metadata->pathname); @@ -384,14 +407,21 @@ void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata) void trace_kernel_destroy_session(struct ltt_kernel_session *session) { struct ltt_kernel_channel *channel, *ctmp; + int ret; DBG("[trace] Closing session fd %d", session->fd); /* Close kernel fds */ - close(session->fd); + ret = close(session->fd); + if (ret) { + PERROR("close"); + } if (session->metadata_stream_fd != 0) { DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd); - close(session->metadata_stream_fd); + ret = close(session->metadata_stream_fd); + if (ret) { + PERROR("close"); + } } if (session->metadata != NULL) { diff --git a/src/bin/lttng-sessiond/trace-ust.c b/src/bin/lttng-sessiond/trace-ust.c index 55e73930a..9c7cfb690 100644 --- a/src/bin/lttng-sessiond/trace-ust.c +++ b/src/bin/lttng-sessiond/trace-ust.c @@ -139,7 +139,7 @@ struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan, luc = zmalloc(sizeof(struct ltt_ust_channel)); if (luc == NULL) { - perror("ltt_ust_channel zmalloc"); + PERROR("ltt_ust_channel zmalloc"); goto error; } @@ -171,7 +171,7 @@ struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan, /* Set trace output path */ ret = snprintf(luc->pathname, PATH_MAX, "%s", path); if (ret < 0) { - perror("asprintf ust create channel"); + PERROR("asprintf ust create channel"); goto error_free_channel; } @@ -270,7 +270,7 @@ struct ltt_ust_metadata *trace_ust_create_metadata(char *path) lum = zmalloc(sizeof(struct ltt_ust_metadata)); if (lum == NULL) { - perror("ust metadata zmalloc"); + PERROR("ust metadata zmalloc"); goto error; } @@ -286,7 +286,7 @@ struct ltt_ust_metadata *trace_ust_create_metadata(char *path) /* Set metadata trace path */ ret = snprintf(lum->pathname, PATH_MAX, "%s/metadata", path); if (ret < 0) { - perror("asprintf ust metadata"); + PERROR("asprintf ust metadata"); goto error_free_metadata; } diff --git a/src/bin/lttng-sessiond/ust-app.c b/src/bin/lttng-sessiond/ust-app.c index 030358241..6e1dd60fd 100644 --- a/src/bin/lttng-sessiond/ust-app.c +++ b/src/bin/lttng-sessiond/ust-app.c @@ -198,7 +198,10 @@ void delete_ust_app(struct ust_app *app) * closing this socket, otherwise an application could re-use the socket ID * and race with the teardown, using the same hash table entry. */ - close(sock); + ret = close(sock); + if (ret) { + PERROR("close"); + } DBG2("UST app pid %d deleted", app->key.pid); free(app); @@ -1268,20 +1271,27 @@ error: int ust_app_register(struct ust_register_msg *msg, int sock) { struct ust_app *lta; + int ret; if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL) || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) { ERR("Registration failed: application \"%s\" (pid: %d) has " "%d-bit long, but no consumerd for this long size is available.\n", msg->name, msg->pid, msg->bits_per_long); - close(sock); + ret = close(sock); + if (ret) { + PERROR("close"); + } return -EINVAL; } if (msg->major != LTTNG_UST_COMM_MAJOR) { ERR("Registration failed: application \"%s\" (pid: %d) has " "communication protocol version %u.%u, but sessiond supports 2.x.\n", msg->name, msg->pid, msg->major, msg->minor); - close(sock); + ret = close(sock); + if (ret) { + PERROR("close"); + } return -EINVAL; } lta = zmalloc(sizeof(struct ust_app)); diff --git a/src/bin/lttng-sessiond/ust-consumer.c b/src/bin/lttng-sessiond/ust-consumer.c index d758c8821..ee54e7985 100644 --- a/src/bin/lttng-sessiond/ust-consumer.c +++ b/src/bin/lttng-sessiond/ust-consumer.c @@ -58,13 +58,13 @@ static int send_channel_streams(int sock, DBG("Sending channel %d to consumer", lum.u.channel.channel_key); ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); if (ret < 0) { - perror("send consumer channel"); + PERROR("send consumer channel"); goto error; } fd = uchan->obj->shm_fd; ret = lttcomm_send_fds_unix_sock(sock, &fd, 1); if (ret < 0) { - perror("send consumer channel ancillary data"); + PERROR("send consumer channel ancillary data"); goto error; } @@ -91,7 +91,7 @@ static int send_channel_streams(int sock, DBG("Sending stream %d to consumer", lum.u.stream.stream_key); ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); if (ret < 0) { - perror("send consumer stream"); + PERROR("send consumer stream"); goto error; } @@ -99,7 +99,7 @@ static int send_channel_streams(int sock, fds[1] = stream->obj->wait_fd; ret = lttcomm_send_fds_unix_sock(sock, fds, 2); if (ret < 0) { - perror("send consumer stream ancillary data"); + PERROR("send consumer stream ancillary data"); goto error; } } @@ -142,13 +142,13 @@ int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess) DBG("Sending metadata channel %d to consumer", lum.u.channel.channel_key); ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); if (ret < 0) { - perror("send consumer channel"); + PERROR("send consumer channel"); goto error; } fd = usess->metadata->obj->shm_fd; ret = lttcomm_send_fds_unix_sock(sock, &fd, 1); if (ret < 0) { - perror("send consumer metadata channel"); + PERROR("send consumer metadata channel"); goto error; } @@ -166,14 +166,14 @@ int ust_consumer_send_session(int consumer_fd, struct ust_app_session *usess) DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key); ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum)); if (ret < 0) { - perror("send consumer metadata stream"); + PERROR("send consumer metadata stream"); goto error; } fds[0] = usess->metadata->stream_obj->shm_fd; fds[1] = usess->metadata->stream_obj->wait_fd; ret = lttcomm_send_fds_unix_sock(sock, fds, 2); if (ret < 0) { - perror("send consumer stream"); + PERROR("send consumer stream"); goto error; } } diff --git a/src/bin/lttng/utils.c b/src/bin/lttng/utils.c index 1f491db1b..514bfe645 100644 --- a/src/bin/lttng/utils.c +++ b/src/bin/lttng/utils.c @@ -16,6 +16,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#define _GNU_SOURCE #include #include diff --git a/src/common/compat/compat-epoll.c b/src/common/compat/compat-epoll.c index 77f5b922a..008dba702 100644 --- a/src/common/compat/compat-epoll.c +++ b/src/common/compat/compat-epoll.c @@ -15,6 +15,7 @@ * Place - Suite 330, Boston, MA 02111-1307, USA. */ +#define _GNU_SOURCE #include #include #include @@ -49,7 +50,7 @@ int compat_epoll_create(struct lttng_poll_event *events, int size, int flags) ret = epoll_create1(flags); if (ret < 0) { /* At this point, every error is fatal */ - perror("epoll_create1"); + PERROR("epoll_create1"); goto error; } @@ -58,7 +59,7 @@ int compat_epoll_create(struct lttng_poll_event *events, int size, int flags) /* This *must* be freed by using lttng_poll_free() */ events->events = zmalloc(size * sizeof(struct epoll_event)); if (events->events == NULL) { - perror("zmalloc epoll set"); + PERROR("zmalloc epoll set"); goto error_close; } @@ -68,7 +69,10 @@ int compat_epoll_create(struct lttng_poll_event *events, int size, int flags) return 0; error_close: - close(events->epfd); + ret = close(events->epfd); + if (ret) { + PERROR("close"); + } error: return -1; } @@ -97,11 +101,11 @@ int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_event goto end; case ENOSPC: case EPERM: - /* Print perror and goto end not failing. Show must go on. */ - perror("epoll_ctl ADD"); + /* Print PERROR and goto end not failing. Show must go on. */ + PERROR("epoll_ctl ADD"); goto end; default: - perror("epoll_ctl ADD fatal"); + PERROR("epoll_ctl ADD fatal"); goto error; } } @@ -112,7 +116,7 @@ int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_event new_size = 2 * events->events_size; ptr = realloc(events->events, new_size * sizeof(struct epoll_event)); if (ptr == NULL) { - perror("realloc epoll add"); + PERROR("realloc epoll add"); goto error; } events->events = ptr; @@ -142,14 +146,14 @@ int compat_epoll_del(struct lttng_poll_event *events, int fd) switch (errno) { case ENOENT: case EPERM: - /* Print perror and goto end not failing. Show must go on. */ - perror("epoll_ctl DEL"); + /* Print PERROR and goto end not failing. Show must go on. */ + PERROR("epoll_ctl DEL"); goto end; default: - perror("epoll_ctl DEL fatal"); + PERROR("epoll_ctl DEL fatal"); goto error; } - perror("epoll_ctl del"); + PERROR("epoll_ctl del"); goto error; } @@ -180,7 +184,7 @@ int compat_epoll_wait(struct lttng_poll_event *events, int timeout) } while (ret == -1 && errno == EINTR); if (ret < 0) { /* At this point, every error is fatal */ - perror("epoll_wait"); + PERROR("epoll_wait"); goto error; } @@ -207,7 +211,7 @@ void compat_epoll_set_max_size(void) ret = read(fd, buf, sizeof(buf)); if (ret < 0) { - perror("read set max size"); + PERROR("read set max size"); goto error; } @@ -220,5 +224,8 @@ void compat_epoll_set_max_size(void) DBG("epoll set max size is %d", poll_max_size); error: - close(fd); + ret = close(fd); + if (ret) { + PERROR("close"); + } } diff --git a/src/common/compat/poll.h b/src/common/compat/poll.h index bb046c342..e3a13cede 100644 --- a/src/common/compat/poll.h +++ b/src/common/compat/poll.h @@ -50,6 +50,7 @@ static inline void __lttng_poll_free(void *events) */ #ifdef HAVE_EPOLL #include +#include /* See man epoll(7) for this define path */ #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches" @@ -146,8 +147,13 @@ static inline void lttng_poll_reset(struct lttng_poll_event *events) */ static inline void lttng_poll_clean(struct lttng_poll_event *events) { + int ret; + if (events) { - close(events->epfd); + ret = close(events->epfd); + if (ret) { + perror("close"); + } __lttng_poll_free((void *) events->events); } } diff --git a/src/common/consumer.c b/src/common/consumer.c index ae59b6b60..e7d51078e 100644 --- a/src/common/consumer.c +++ b/src/common/consumer.c @@ -173,13 +173,22 @@ void consumer_del_stream(struct lttng_consumer_stream *stream) goto end; } if (stream->out_fd >= 0) { - close(stream->out_fd); + ret = close(stream->out_fd); + if (ret) { + PERROR("close"); + } } if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) { - close(stream->wait_fd); + ret = close(stream->wait_fd); + if (ret) { + PERROR("close"); + } } if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) { - close(stream->shm_fd); + ret = close(stream->shm_fd); + if (ret) { + PERROR("close"); + } } if (!--stream->chan->refcount) free_chan = stream->chan; @@ -360,10 +369,16 @@ void consumer_del_channel(struct lttng_consumer_channel *channel) } } if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) { - close(channel->wait_fd); + ret = close(channel->wait_fd); + if (ret) { + PERROR("close"); + } } if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) { - close(channel->shm_fd); + ret = close(channel->shm_fd); + if (ret) { + PERROR("close"); + } } free(channel); end: @@ -698,14 +713,18 @@ error_thread_pipe: int err; err = close(ctx->consumer_should_quit[i]); - assert(!err); + if (err) { + PERROR("close"); + } } error_quit_pipe: for (i = 0; i < 2; i++) { int err; err = close(ctx->consumer_poll_pipe[i]); - assert(!err); + if (err) { + PERROR("close"); + } } error_poll_pipe: free(ctx); @@ -718,13 +737,36 @@ error: */ void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx) { - close(ctx->consumer_error_socket); - close(ctx->consumer_thread_pipe[0]); - close(ctx->consumer_thread_pipe[1]); - close(ctx->consumer_poll_pipe[0]); - close(ctx->consumer_poll_pipe[1]); - close(ctx->consumer_should_quit[0]); - close(ctx->consumer_should_quit[1]); + int ret; + + ret = close(ctx->consumer_error_socket); + if (ret) { + PERROR("close"); + } + ret = close(ctx->consumer_thread_pipe[0]); + if (ret) { + PERROR("close"); + } + ret = close(ctx->consumer_thread_pipe[1]); + if (ret) { + PERROR("close"); + } + ret = close(ctx->consumer_poll_pipe[0]); + if (ret) { + PERROR("close"); + } + ret = close(ctx->consumer_poll_pipe[1]); + if (ret) { + PERROR("close"); + } + ret = close(ctx->consumer_should_quit[0]); + if (ret) { + PERROR("close"); + } + ret = close(ctx->consumer_should_quit[1]); + if (ret) { + PERROR("close"); + } unlink(ctx->consumer_command_sock_path); free(ctx); } diff --git a/src/common/error.h b/src/common/error.h index 14b5e7c67..e5255e4a4 100644 --- a/src/common/error.h +++ b/src/common/error.h @@ -22,6 +22,10 @@ #include #include +#ifndef _GNU_SOURCE +#error "lttng-tools error.h needs _GNU_SOURCE" +#endif + /* Stringify the expansion of a define */ #define XSTR(d) STR(d) #define STR(s) #s @@ -40,21 +44,22 @@ extern int opt_verbose; /* * Macro for printing message depending on command line option and verbosity. */ -#define __lttng_print(type, fmt, args...) \ - do { \ - if (opt_quiet == 0) { \ - if (type == PRINT_MSG) { \ - fprintf(stdout, fmt, ## args); \ - } else if (((type & PRINT_DBG) && opt_verbose == 1) || \ - ((type & (PRINT_DBG | PRINT_DBG2)) && \ - opt_verbose == 2) || \ - ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) && \ - opt_verbose == 3)) { \ - fprintf(stderr, fmt, ## args); \ - } else if (type & (PRINT_ERR | PRINT_WARN | PRINT_BUG)) { \ - fprintf(stderr, fmt, ## args); \ - } \ - } \ +#define __lttng_print(type, fmt, args...) \ + do { \ + if (opt_quiet == 0 && type == PRINT_MSG) { \ + fprintf(stdout, fmt, ## args); \ + } else if (opt_quiet == 0 && \ + (((type & PRINT_DBG) && opt_verbose == 1) || \ + ((type & (PRINT_DBG | PRINT_DBG2)) && \ + opt_verbose == 2) || \ + ((type & (PRINT_DBG | PRINT_DBG2 | PRINT_DBG3)) && \ + opt_verbose == 3))) { \ + fprintf(stderr, fmt, ## args); \ + } else if (opt_quiet == 0 && (type & (PRINT_WARN))) { \ + fprintf(stderr, fmt, ## args); \ + } else if (type & (PRINT_ERR | PRINT_BUG)) { \ + fprintf(stderr, fmt, ## args); \ + } \ } while (0); #define MSG(fmt, args...) \ @@ -75,7 +80,8 @@ extern int opt_verbose; " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) #define _PERROR(fmt, args...) \ - __lttng_print(PRINT_ERR, "perror " fmt "\n", ## args) + __lttng_print(PRINT_ERR, "PERROR: " fmt \ + " [in %s() at " __FILE__ ":" XSTR(__LINE__) "]\n", ## args, __func__) #if !defined(__linux__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) /* diff --git a/src/common/runas.c b/src/common/runas.c index 4742a792e..e0c317c4e 100644 --- a/src/common/runas.c +++ b/src/common/runas.c @@ -167,14 +167,14 @@ int child_run_as(void *_data) if (data->gid != getegid()) { ret = setegid(data->gid); if (ret < 0) { - perror("setegid"); + PERROR("setegid"); return EXIT_FAILURE; } } if (data->uid != geteuid()) { ret = seteuid(data->uid); if (ret < 0) { - perror("seteuid"); + PERROR("seteuid"); return EXIT_FAILURE; } } @@ -190,7 +190,7 @@ int child_run_as(void *_data) writelen = write(data->retval_pipe, &sendret.c[index], writeleft); if (writelen < 0) { - perror("write"); + PERROR("write"); return EXIT_FAILURE; } writeleft -= writelen; @@ -227,7 +227,7 @@ int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) ret = pipe(retval_pipe); if (ret < 0) { - perror("pipe"); + PERROR("pipe"); retval.i = ret; goto end; } @@ -241,7 +241,7 @@ int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK, -1, 0); if (child_stack == MAP_FAILED) { - perror("mmap"); + PERROR("mmap"); retval.i = -ENOMEM; goto close_pipe; } @@ -252,7 +252,7 @@ int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2), &run_as_data); if (pid < 0) { - perror("clone"); + PERROR("clone"); retval.i = pid; goto unmap_stack; } @@ -262,7 +262,7 @@ int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) do { readlen = read(retval_pipe[0], &retval.c[index], readleft); if (readlen < 0) { - perror("read"); + PERROR("read"); ret = -1; break; } @@ -276,18 +276,24 @@ int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) */ pid = waitpid(pid, &status, 0); if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { - perror("wait"); + PERROR("wait"); retval.i = -1; } unmap_stack: ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE); if (ret < 0) { - perror("munmap"); + PERROR("munmap"); retval.i = ret; } close_pipe: - close(retval_pipe[0]); - close(retval_pipe[1]); + ret = close(retval_pipe[0]); + if (ret) { + PERROR("close"); + } + ret = close(retval_pipe[1]); + if (ret) { + PERROR("close"); + } end: return retval.i; } diff --git a/src/common/sessiond-comm/sessiond-comm.c b/src/common/sessiond-comm/sessiond-comm.c index 3b224c6ff..aa24e82dd 100644 --- a/src/common/sessiond-comm/sessiond-comm.c +++ b/src/common/sessiond-comm/sessiond-comm.c @@ -28,6 +28,7 @@ #include #include +#include #include "sessiond-comm.h" @@ -140,12 +141,11 @@ const char *lttcomm_get_readable_code(enum lttcomm_return_code code) int lttcomm_connect_unix_sock(const char *pathname) { struct sockaddr_un sun; - int fd; - int ret; + int fd, ret, closeret; fd = socket(PF_UNIX, SOCK_STREAM, 0); if (fd < 0) { - perror("socket"); + PERROR("socket"); ret = fd; goto error; } @@ -167,7 +167,10 @@ int lttcomm_connect_unix_sock(const char *pathname) return fd; error_connect: - close(fd); + closeret = close(fd); + if (closeret) { + PERROR("close"); + } error: return ret; } @@ -185,7 +188,7 @@ int lttcomm_accept_unix_sock(int sock) /* Blocking call */ new_fd = accept(sock, (struct sockaddr *) &sun, &len); if (new_fd < 0) { - perror("accept"); + PERROR("accept"); } return new_fd; @@ -203,7 +206,7 @@ int lttcomm_create_unix_sock(const char *pathname) /* Create server socket */ if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { - perror("socket"); + PERROR("socket"); goto error; } @@ -216,7 +219,7 @@ int lttcomm_create_unix_sock(const char *pathname) (void) unlink(pathname); ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); if (ret < 0) { - perror("bind"); + PERROR("bind"); goto error; } @@ -235,7 +238,7 @@ int lttcomm_listen_unix_sock(int sock) ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN); if (ret < 0) { - perror("listen"); + PERROR("listen"); } return ret; @@ -262,7 +265,7 @@ ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) ret = recvmsg(sock, &msg, MSG_WAITALL); if (ret < 0) { - perror("recvmsg"); + PERROR("recvmsg"); } return ret; @@ -288,7 +291,7 @@ ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) ret = sendmsg(sock, &msg, 0); if (ret < 0) { - perror("sendmsg"); + PERROR("sendmsg"); } return ret; @@ -299,15 +302,18 @@ ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) */ int lttcomm_close_unix_sock(int sock) { - int ret; + int ret, closeret; /* Shutdown receptions and transmissions */ ret = shutdown(sock, SHUT_RDWR); if (ret < 0) { - perror("shutdown"); + PERROR("shutdown"); } - close(sock); + closeret = close(sock); + if (closeret) { + PERROR("close"); + } return ret; } @@ -350,7 +356,7 @@ ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) ret = sendmsg(sock, &msg, 0); if (ret < 0) { - perror("sendmsg"); + PERROR("sendmsg"); } return ret; } @@ -385,7 +391,7 @@ ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) ret = recvmsg(sock, &msg, 0); if (ret < 0) { - perror("recvmsg fds"); + PERROR("recvmsg fds"); goto end; } if (ret != 1) { @@ -463,7 +469,7 @@ ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) ret = sendmsg(sock, &msg, 0); if (ret < 0) { - perror("sendmsg"); + PERROR("sendmsg"); } return ret; @@ -507,7 +513,7 @@ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, ret = recvmsg(sock, &msg, 0); if (ret < 0) { - perror("recvmsg fds"); + PERROR("recvmsg fds"); goto end; } @@ -568,7 +574,7 @@ int lttcomm_setsockopt_creds_unix_sock(int sock) /* Set socket for credentials retrieval */ ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); if (ret < 0) { - perror("setsockopt creds unix sock"); + PERROR("setsockopt creds unix sock"); } return ret; } diff --git a/src/common/ust-consumer/ust-consumer.c b/src/common/ust-consumer/ust-consumer.c index c8ba08460..8dec2d783 100644 --- a/src/common/ust-consumer/ust-consumer.c +++ b/src/common/ust-consumer/ust-consumer.c @@ -59,7 +59,7 @@ ssize_t lttng_ustconsumer_on_read_subbuffer_mmap( stream->buf, &mmap_offset); if (ret != 0) { errno = -ret; - perror("ustctl_get_mmap_read_offset"); + PERROR("ustctl_get_mmap_read_offset"); goto end; } while (len > 0) { @@ -68,7 +68,7 @@ ssize_t lttng_ustconsumer_on_read_subbuffer_mmap( len = 0; } else if (ret < 0) { errno = -ret; - perror("Error in file write"); + PERROR("Error in file write"); goto end; } /* This won't block, but will start writeout asynchronously */ @@ -110,7 +110,7 @@ int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx, ret = ustctl_snapshot(stream->chan->handle, stream->buf); if (ret != 0) { errno = -ret; - perror("Getting sub-buffer snapshot."); + PERROR("Getting sub-buffer snapshot."); } return ret; @@ -132,7 +132,7 @@ int lttng_ustconsumer_get_produced_snapshot( stream->buf, pos); if (ret != 0) { errno = -ret; - perror("kernctl_snapshot_get_produced"); + PERROR("kernctl_snapshot_get_produced"); } return ret; @@ -261,7 +261,7 @@ end: /* signal the poll thread */ ret = write(ctx->consumer_poll_pipe[1], "4", 1); if (ret < 0) { - perror("write consumer poll"); + PERROR("write consumer poll"); } end_nosignal: return 0; @@ -401,7 +401,7 @@ int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream) stream->uid, stream->gid); if (ret < 0) { ERR("Opening %s", stream->path_name); - perror("open"); + PERROR("open"); goto error; } stream->out_fd = ret;