Skip to contentSkip to conversation

Fix: agents ht leaks on destroy session

PATCH New Mathieu Desnoyers 2 participants 17 messages Dec 18, 2014 – Jan 7, 2015 4 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/13 Fix: agents ht leaks on destroy session

---
 src/bin/lttng-sessiond/trace-ust.c | 2 ++
 1 file changed, 2 insertions(+)
src/bin/lttng-sessiond/trace-ust.c +2 -0
index 971c56e..a3e25aa 100644
@@ -766,6 +766,8 @@ void trace_ust_destroy_session(struct ltt_ust_session *session)
 	}
 	rcu_read_unlock();
 
+	ht_cleanup_push(session->agents);
+
 	/* Cleanup UID buffer registry object(s). */
 	cds_list_for_each_entry_safe(reg, sreg, &session->buffer_reg_uid_list,
 			lnode) {
Signature · 1 line
2.1.1
#2 lore raw

PATCH 2/13 Fix: leak on UST buffer registry creation error

In reply to a message not in the archive
---
 src/bin/lttng-sessiond/ust-app.c      | 21 ++++++++++++++++++---
 src/bin/lttng-sessiond/ust-registry.c |  4 +++-
 2 files changed, 21 insertions(+), 4 deletions(-)
src/bin/lttng-sessiond/ust-app.c +18 -3
index 891d75f..9dbd49b 100644
@@ -1732,7 +1732,6 @@ static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
 		if (ret < 0) {
 			goto error;
 		}
-		buffer_reg_pid_add(reg_pid);
 	} else {
 		goto end;
 	}
@@ -1745,9 +1744,18 @@ static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
 			app->byte_order, app->version.major,
 			app->version.minor);
 	if (ret < 0) {
+		/*
+		 * reg_pid->registry->reg.ust is NULL upon error, so we need to
+		 * destroy the buffer registry, because it is always expected
+		 * that if the buffer registry can be found, its ust registry is
+		 * non-NULL.
+		 */
+		buffer_reg_pid_destroy(reg_pid);
 		goto error;
 	}
 
+	buffer_reg_pid_add(reg_pid);
+
 	DBG3("UST app buffer registry per PID created successfully");
 
 end:
@@ -1788,7 +1796,6 @@ static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
 		if (ret < 0) {
 			goto error;
 		}
-		buffer_reg_uid_add(reg_uid);
 	} else {
 		goto end;
 	}
@@ -1801,13 +1808,21 @@ static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
 			app->byte_order, app->version.major,
 			app->version.minor);
 	if (ret < 0) {
+		/*
+		 * reg_uid->registry->reg.ust is NULL upon error, so we need to
+		 * destroy the buffer registry, because it is always expected
+		 * that if the buffer registry can be found, its ust registry is
+		 * non-NULL.
+		 */
+		buffer_reg_uid_destroy(reg_uid, NULL);
 		goto error;
 	}
 	/* Add node to teardown list of the session. */
 	cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
 
-	DBG3("UST app buffer registry per UID created successfully");
+	buffer_reg_uid_add(reg_uid);
 
+	DBG3("UST app buffer registry per UID created successfully");
 end:
 	if (regp) {
 		*regp = reg_uid;
src/bin/lttng-sessiond/ust-registry.c +3 -1
index 3f4a962..1a00e93 100644
@@ -608,7 +608,9 @@ void ust_registry_session_destroy(struct ust_registry_session *reg)
 	struct lttng_ht_iter iter;
 	struct ust_registry_channel *chan;
 
-	assert(reg);
+	if (!reg) {
+		return;
+	}
 
 	/* On error, EBUSY can be returned if lock. Code flow error. */
 	ret = pthread_mutex_destroy(&reg->lock);
Signature · 1 line
2.1.1
#3 lore raw

PATCH 3/13 Fix: ust app leak on UST buffer creation error

In reply to a message not in the archive
---
 src/bin/lttng-sessiond/ust-app.c | 2 ++
 1 file changed, 2 insertions(+)
src/bin/lttng-sessiond/ust-app.c +2 -0
index 9dbd49b..3abd127 100644
@@ -1875,6 +1875,7 @@ static int create_ust_app_session(struct ltt_ust_session *usess,
 		/* Init local registry. */
 		ret = setup_buffer_reg_pid(ua_sess, app, NULL);
 		if (ret < 0) {
+			delete_ust_app_session(-1, ua_sess, app);
 			goto error;
 		}
 		break;
@@ -1882,6 +1883,7 @@ static int create_ust_app_session(struct ltt_ust_session *usess,
 		/* Look for a global registry. If none exists, create one. */
 		ret = setup_buffer_reg_uid(usess, app, NULL);
 		if (ret < 0) {
+			delete_ust_app_session(-1, ua_sess, app);
 			goto error;
 		}
 		break;
Signature · 1 line
2.1.1
#4 lore raw

PATCH 4/13 Missing error handling: ust_app_ht_alloc should return error status

In reply to a message not in the archive

In preparation for having sessiond main() handle the error.

---
 src/bin/lttng-sessiond/ust-app.c | 12 +++++++++++-
 src/bin/lttng-sessiond/ust-app.h |  2 +-
 2 files changed, 12 insertions(+), 2 deletions(-)
src/bin/lttng-sessiond/ust-app.c +11 -1
index 3abd127..9ab5f77 100644
@@ -3438,11 +3438,21 @@ void ust_app_clean_list(void)
 /*
  * Init UST app hash table.
  */
-void ust_app_ht_alloc(void)
+int ust_app_ht_alloc(void)
 {
 	ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
+	if (!ust_app_ht) {
+		return -1;
+	}
 	ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
+	if (!ust_app_ht_by_sock) {
+		return -1;
+	}
 	ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
+	if (!ust_app_ht_by_notify_sock) {
+		return -1;
+	}
+	return 0;
 }
 
 /*
src/bin/lttng-sessiond/ust-app.h +1 -1
index d1e260f..2d3695c 100644
@@ -313,7 +313,7 @@ int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
 void ust_app_global_update(struct ltt_ust_session *usess, int sock);
 
 void ust_app_clean_list(void);
-void ust_app_ht_alloc(void);
+int ust_app_ht_alloc(void);
 struct ust_app *ust_app_find_by_pid(pid_t pid);
 int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate);
 struct ust_app_stream *ust_app_alloc_stream(void);
Signature · 1 line
2.1.1
#5 lore raw

PATCH 5/13 Missing error handling: consumerd parse_args should return errors to caller

In reply to a message not in the archive

In preparation for having main() handle the return value.

---
 src/bin/lttng-consumerd/lttng-consumerd.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
src/bin/lttng-consumerd/lttng-consumerd.c +8 -3
index 299776a..ceb1da4 100644
@@ -186,9 +186,9 @@ static void usage(FILE *fp)
 /*
  * daemon argument parsing
  */
-static void parse_args(int argc, char **argv)
+static int parse_args(int argc, char **argv)
 {
-	int c;
+	int c, ret = 0;
 
 	static struct option long_options[] = {
 		{ "consumerd-cmd-sock", 1, 0, 'c' },
@@ -218,6 +218,8 @@ static void parse_args(int argc, char **argv)
 			fprintf(stderr, "option %s", long_options[option_index].name);
 			if (optarg) {
 				fprintf(stderr, " with arg %s\n", optarg);
+				ret = -1;
+				goto end;
 			}
 			break;
 		case 'c':
@@ -260,9 +262,12 @@ static void parse_args(int argc, char **argv)
 #endif
 		default:
 			usage(stderr);
-			exit(EXIT_FAILURE);
+			ret = -1;
+			goto end;
 		}
 	}
+end:
+	return ret;
 }
 
 /*
Signature · 1 line
2.1.1
#6 lore raw

PATCH 6/13 Misleading error handling: utils_create_pid_file() should return 0 on success

In reply to a message not in the archive

fprintf returns a positive value on success, which is used as return value for utils_create_pid_file(), which is odd. This is in preparation for main() refactoring.

---
 src/common/utils.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
src/common/utils.c +5 -1
index e7dccb7..3428d31 100644
@@ -477,10 +477,14 @@ int utils_create_pid_file(pid_t pid, const char *filepath)
 	ret = fprintf(fp, "%d\n", pid);
 	if (ret < 0) {
 		PERROR("fprintf pid file");
+		goto error;
 	}
 
-	fclose(fp);
+	if (fclose(fp)) {
+		PERROR("fclose");
+	}
 	DBG("Pid %d written in file %s", pid, filepath);
+	ret = 0;
 error:
 	return ret;
 }
Signature · 1 line
2.1.1
#7 lore raw

PATCH 7/13 Cleanup: lttng-consumerd: remove stale TODO comment

In reply to a message not in the archive
---
 src/bin/lttng-consumerd/lttng-consumerd.c | 2 --
 1 file changed, 2 deletions(-)
src/bin/lttng-consumerd/lttng-consumerd.c +0 -2
index ceb1da4..9bc3209 100644
@@ -53,8 +53,6 @@
 #include "lttng-consumerd.h"
 #include "health-consumerd.h"
 
-/* TODO : support UST (all direct kernel-ctl accesses). */
-
 /* threads (channel handling, poll, metadata, sessiond) */
 
 static pthread_t channel_thread, data_thread, metadata_thread,
Signature · 1 line
2.1.1
#8 lore raw

PATCH 8/13 Cleanup: consumerd: use PERROR rather than perror

In reply to a message not in the archive
---
 src/bin/lttng-consumerd/lttng-consumerd.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
src/bin/lttng-consumerd/lttng-consumerd.c +4 -4
index 9bc3209..d2c3ed4 100644
@@ -122,7 +122,7 @@ static int set_signal_handler(void)
 	sigset_t sigset;
 
 	if ((ret = sigemptyset(&sigset)) < 0) {
-		perror("sigemptyset");
+		PERROR("sigemptyset");
 		return ret;
 	}
 
@@ -130,17 +130,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;
 	}
 
Signature · 1 line
2.1.1
#9 lore raw

PATCH 9/13 Cleanup: consumerd: lines over 80 columns

In reply to a message not in the archive
---
 src/bin/lttng-consumerd/lttng-consumerd.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
src/bin/lttng-consumerd/lttng-consumerd.c +4 -2
index d2c3ed4..d0d8398 100644
@@ -206,14 +206,16 @@ static int parse_args(int argc, char **argv)
 
 	while (1) {
 		int option_index = 0;
-		c = getopt_long(argc, argv, "dhqvVku" "c:e:g:", long_options, &option_index);
+		c = getopt_long(argc, argv, "dhqvVku" "c:e:g:",
+				long_options, &option_index);
 		if (c == -1) {
 			break;
 		}
 
 		switch (c) {
 		case 0:
-			fprintf(stderr, "option %s", long_options[option_index].name);
+			fprintf(stderr, "option %s",
+				long_options[option_index].name);
 			if (optarg) {
 				fprintf(stderr, " with arg %s\n", optarg);
 				ret = -1;
Signature · 1 line
2.1.1
#10 lore raw

PATCH 10/13 Missing error handling: consumer_signal_init should return its error status

In reply to a message not in the archive
---
 src/common/consumer-timer.c | 4 +++-
 src/common/consumer-timer.h | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)
src/common/consumer-timer.c +3 -1
index e7343af..646d323 100644
@@ -460,7 +460,7 @@ void consumer_timer_live_stop(struct lttng_consumer_channel *channel)
  * Block the RT signals for the entire process. It must be called from the
  * consumer main before creating the threads
  */
-void consumer_signal_init(void)
+int consumer_signal_init(void)
 {
 	int ret;
 	sigset_t mask;
@@ -471,7 +471,9 @@ void consumer_signal_init(void)
 	if (ret) {
 		errno = ret;
 		PERROR("pthread_sigmask");
+		return -1;
 	}
+	return 0;
 }
 
 /*
src/common/consumer-timer.h +1 -1
index f3fac5d..baaa82b 100644
@@ -50,6 +50,6 @@ void consumer_timer_live_start(struct lttng_consumer_channel *channel,
 		int live_timer_interval);
 void consumer_timer_live_stop(struct lttng_consumer_channel *channel);
 void *consumer_timer_thread(void *data);
-void consumer_signal_init(void);
+int consumer_signal_init(void);
 
 #endif /* CONSUMER_TIMER_H */
Signature · 1 line
2.1.1
#11 lore raw

PATCH 11/13 Refactor sessiond main/cleanup/ht-cleanup

In reply to a message not in the archive

Main reason for this entire patchset: move teardown of ht-cleanup thread _after_ the sessiond cleanup which needs to destroy hash tables. This fixes leaks at sessiond teardown that makes the valgrind output hard to use. As this has been done, various other issues with error handling, leaks, and symmetry of allocation and teardown have been fixed, which makes this a refactoring.

- Enforce symmetry between allocation and teardown,
- Handle all errors,
- Return all errors as EXIT_FAILURE,
- Standardize on zero being success, nonzero being error, (rather than < 0 being error),
- Fix pthread PERROR: we need to store ret into errno before calling PERROR, since pthread API does not set errno,
- Join errors now fall-through, rather than rely on the OS to teardown the rest.

---
 src/bin/lttng-sessiond/ht-cleanup.c     |  41 +-
 src/bin/lttng-sessiond/lttng-sessiond.h |   4 +
 src/bin/lttng-sessiond/main.c           | 781 ++++++++++++++++++++------------
 3 files changed, 529 insertions(+), 297 deletions(-)
src/bin/lttng-sessiond/ht-cleanup.c +31 -10
index 0469f92..79fb9cd 100644
@@ -47,7 +47,7 @@ void *thread_ht_cleanup(void *data)
 
 	health_code_update();
 
-	ret = sessiond_set_thread_pollset(&events, 2);
+	ret = sessiond_set_ht_cleanup_thread_pollset(&events, 2);
 	if (ret < 0) {
 		goto error_poll_create;
 	}
@@ -61,11 +61,14 @@ void *thread_ht_cleanup(void *data)
 	health_code_update();
 
 	while (1) {
+		int handled_event;
+
 		DBG3("[ht-thread] Polling on %d fds.",
 			LTTNG_POLL_GETNB(&events));
 
 		/* Inifinite blocking call, waiting for transmission */
 restart:
+		handled_event = 0;
 		health_poll_entry();
 		ret = lttng_poll_wait(&events, -1);
 		health_poll_exit();
@@ -90,13 +93,9 @@ restart:
 			revents = LTTNG_POLL_GETEV(&events, i);
 			pollfd = LTTNG_POLL_GETFD(&events, i);
 
-			/* Thread quit pipe has been closed. Killing thread. */
-			ret = sessiond_check_thread_quit_pipe(pollfd, revents);
-			if (ret) {
-				err = 0;
-				goto exit;
+			if (pollfd != ht_cleanup_pipe[0]) {
+				continue;
 			}
-			assert(pollfd == ht_cleanup_pipe[0]);
 
 			if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
 				ERR("ht cleanup pipe error");
@@ -125,6 +124,30 @@ restart:
 
 			health_code_update();
 		}
+
+		/* Only check cleanup quit when no more work to do. */
+		if (handled_event) {
+			continue;
+		}
+
+		for (i = 0; i < nb_fd; i++) {
+			health_code_update();
+
+			/* Fetch once the poll data */
+			revents = LTTNG_POLL_GETEV(&events, i);
+			pollfd = LTTNG_POLL_GETFD(&events, i);
+
+			if (pollfd == ht_cleanup_pipe[0]) {
+				continue;
+			}
+
+			/* Thread quit pipe has been closed. Killing thread. */
+			ret = sessiond_check_ht_cleanup_quit(pollfd, revents);
+			if (ret) {
+				err = 0;
+				goto exit;
+			}
+		}
 	}
 
 exit:
@@ -132,9 +155,7 @@ error:
 	lttng_poll_clean(&events);
 error_poll_create:
 error_testpoint:
-	utils_close_pipe(ht_cleanup_pipe);
-	ht_cleanup_pipe[0] = ht_cleanup_pipe[1] = -1;
-	DBG("[ust-thread] cleanup complete.");
+	DBG("[ht-cleanup] Thread terminates.");
 	if (err) {
 		health_error();
 		ERR("Health error occurred in %s", __func__);
src/bin/lttng-sessiond/lttng-sessiond.h +4 -0
index 488cf2f..07a6edd 100644
@@ -121,6 +121,10 @@ extern int is_root;
 int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size);
 int sessiond_check_thread_quit_pipe(int fd, uint32_t events);
 
+int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event *events,
+		size_t size);
+int sessiond_check_ht_cleanup_quit(int fd, uint32_t events);
+
 void *thread_ht_cleanup(void *data);
 
 void sessiond_notify_ready(void);
src/bin/lttng-sessiond/main.c +494 -287

1,242 lines not on this page · raw message

Signature · 1 line
2.1.1
#12 lore raw

PATCH 12/13 Refactor consumerd main/cleanup

In reply to a message not in the archive

- Enforce symmetry between allocation and teardown,
- Handle all errors,
- Return all errors as EXIT_FAILURE,
- Standardize on zero being success, nonzero being error, (rather than < 0 being error),
- Fix pthread PERROR: we need to store ret into errno before calling PERROR, since pthread API does not set errno,
- Join errors now fall-through, rather than rely on the OS to teardown the rest.

---
 src/bin/lttng-consumerd/lttng-consumerd.c | 259 ++++++++++++++++++++----------
 1 file changed, 170 insertions(+), 89 deletions(-)
src/bin/lttng-consumerd/lttng-consumerd.c +170 -89
index d0d8398..b43e8f1 100644
@@ -294,12 +294,20 @@ static void set_ulimit(void)
  */
 int main(int argc, char **argv)
 {
-	int ret = 0;
+	int ret = 0, retval = 0;
 	void *status;
 
+	if (set_signal_handler()) {
+		retval = -1;
+		goto exit_set_signal_handler;
+	}
+
 	/* Parse arguments */
 	progname = argv[0];
-	parse_args(argc, argv);
+	if (parse_args(argc, argv)) {
+		retval = -1;
+		goto exit_options;
+	}
 
 	/* Daemonize */
 	if (opt_daemon) {
@@ -313,7 +321,8 @@ int main(int argc, char **argv)
 		ret = daemon(0, 0);
 		if (ret < 0) {
 			PERROR("daemon");
-			goto error;
+			retval = -1;
+			goto exit_options;
 		}
 		/*
 		 * We are in the child. Make sure all other file
@@ -325,36 +334,65 @@ int main(int argc, char **argv)
 		}
 	}
 
+	/*
+	 * Starting from here, we can create threads. This needs to be after
+	 * lttng_daemonize due to RCU.
+	 */
+
+	health_consumerd = health_app_create(NR_HEALTH_CONSUMERD_TYPES);
+	if (!health_consumerd) {
+		retval = -1;
+		goto exit_health_consumerd_cleanup;
+	}
+
 	/* Set up max poll set size */
 	lttng_poll_set_max_size();
 
 	if (*command_sock_path == '\0') {
 		switch (opt_type) {
 		case LTTNG_CONSUMER_KERNEL:
-			snprintf(command_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
+			ret = snprintf(command_sock_path, PATH_MAX,
+					DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
 					DEFAULT_LTTNG_RUNDIR);
+			if (ret < 0) {
+				retval = -1;
+				goto exit_init_data;
+			}
 			break;
 		case LTTNG_CONSUMER64_UST:
-			snprintf(command_sock_path, PATH_MAX,
-					DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
+			ret = snprintf(command_sock_path, PATH_MAX,
+					DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
+					DEFAULT_LTTNG_RUNDIR);
+			if (ret < 0) {
+				retval = -1;
+				goto exit_init_data;
+			}
 			break;
 		case LTTNG_CONSUMER32_UST:
-			snprintf(command_sock_path, PATH_MAX,
-					DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
+			ret = snprintf(command_sock_path, PATH_MAX,
+					DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
+					DEFAULT_LTTNG_RUNDIR);
+			if (ret < 0) {
+				retval = -1;
+				goto exit_init_data;
+			}
 			break;
 		default:
-			WARN("Unknown consumerd type");
-			goto error;
+			ERR("Unknown consumerd type");
+			retval = -1;
+			goto exit_init_data;
 		}
 	}
 
 	/* Init */
-	if (lttng_consumer_init() < 0) {
-		goto error;
+	if (lttng_consumer_init()) {
+		retval = -1;
+		goto exit_init_data;
 	}
 
-	/* Init socket timeouts */
+	/* Initialize communication library */
 	lttcomm_init();
+	/* Initialize TCP timeout values */
 	lttcomm_inet_init();
 
 	if (!getuid()) {
@@ -362,47 +400,58 @@ int main(int argc, char **argv)
 		set_ulimit();
 	}
 
-	health_consumerd = health_app_create(NR_HEALTH_CONSUMERD_TYPES);
-	if (!health_consumerd) {
-		goto error;
-	}
-
 	/* create the consumer instance with and assign the callbacks */
 	ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer,
 		NULL, lttng_consumer_on_recv_stream, NULL);
-	if (ctx == NULL) {
-		goto error;
+	if (!ctx) {
+		retval = -1;
+		goto exit_init_data;
 	}
 
 	lttng_consumer_set_command_sock_path(ctx, command_sock_path);
 	if (*error_sock_path == '\0') {
 		switch (opt_type) {
 		case LTTNG_CONSUMER_KERNEL:
-			snprintf(error_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
+			ret = snprintf(error_sock_path, PATH_MAX,
+					DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
 					DEFAULT_LTTNG_RUNDIR);
+			if (ret < 0) {
+				retval = -1;
+				goto exit_init_data;
+			}
 			break;
 		case LTTNG_CONSUMER64_UST:
-			snprintf(error_sock_path, PATH_MAX,
-					DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
+			ret = snprintf(error_sock_path, PATH_MAX,
+					DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
+					DEFAULT_LTTNG_RUNDIR);
+			if (ret < 0) {
+				retval = -1;
+				goto exit_init_data;
+			}
 			break;
 		case LTTNG_CONSUMER32_UST:
-			snprintf(error_sock_path, PATH_MAX,
-					DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
+			ret = snprintf(error_sock_path, PATH_MAX,
+					DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
+					DEFAULT_LTTNG_RUNDIR);
+			if (ret < 0) {
+				retval = -1;
+				goto exit_init_data;
+			}
 			break;
 		default:
-			WARN("Unknown consumerd type");
-			goto error;
+			ERR("Unknown consumerd type");
+			retval = -1;
+			goto exit_init_data;
 		}
 	}
 
-	if (set_signal_handler() < 0) {
-		goto error;
-	}
-
 	/* Connect to the socket created by lttng-sessiond to report errors */
 	DBG("Connecting to error socket %s", error_sock_path);
 	ret = lttcomm_connect_unix_sock(error_sock_path);
-	/* not a fatal error, but all communication with lttng-sessiond will fail */
+	/*
+	 * Not a fatal error, but all communication with lttng-sessiond will
+	 * fail.
+	 */
 	if (ret < 0) {
 		WARN("Cannot connect to error socket (is lttng-sessiond started?)");
 	}
@@ -412,21 +461,26 @@ int main(int argc, char **argv)
 	 * Block RT signals used for UST periodical metadata flush and the live
 	 * timer in main, and create a dedicated thread to handle these signals.
 	 */
-	consumer_signal_init();
+	if (consumer_signal_init()) {
+		retval = -1;
+		goto exit_init_data;
+	}
 
 	ctx->type = opt_type;
 
-	ret = utils_create_pipe(health_quit_pipe);
-	if (ret < 0) {
-		goto error_health_pipe;
+	if (utils_create_pipe(health_quit_pipe)) {
+		retval = -1;
+		goto exit_health_pipe;
 	}
 
 	/* Create thread to manage the client socket */
 	ret = pthread_create(&health_thread, NULL,
 			thread_manage_health, (void *) NULL);
-	if (ret != 0) {
+	if (ret) {
+		errno = ret;
 		PERROR("pthread_create health");
-		goto health_error;
+		retval = -1;
+		goto exit_health_thread;
 	}
 
 	/*
@@ -439,35 +493,46 @@ int main(int argc, char **argv)
 	cmm_smp_mb();	/* Read ready before following operations */
 
 	/* Create thread to manage channels */
-	ret = pthread_create(&channel_thread, NULL, consumer_thread_channel_poll,
+	ret = pthread_create(&channel_thread, NULL,
+			consumer_thread_channel_poll,
 			(void *) ctx);
-	if (ret != 0) {
-		perror("pthread_create");
-		goto channel_error;
+	if (ret) {
+		errno = ret;
+		PERROR("pthread_create");
+		retval = -1;
+		goto exit_channel_thread;
 	}
 
 	/* Create thread to manage the polling/writing of trace metadata */
-	ret = pthread_create(&metadata_thread, NULL, consumer_thread_metadata_poll,
+	ret = pthread_create(&metadata_thread, NULL,
+			consumer_thread_metadata_poll,
 			(void *) ctx);
-	if (ret != 0) {
-		perror("pthread_create");
-		goto metadata_error;
+	if (ret) {
+		errno = ret;
+		PERROR("pthread_create");
+		retval = -1;
+		goto exit_metadata_thread;
 	}
 
 	/* Create thread to manage the polling/writing of trace data */
 	ret = pthread_create(&data_thread, NULL, consumer_thread_data_poll,
 			(void *) ctx);
-	if (ret != 0) {
-		perror("pthread_create");
-		goto data_error;
+	if (ret) {
+		errno = ret;
+		PERROR("pthread_create");
+		retval = -1;
+		goto exit_data_thread;
 	}
 
 	/* Create the thread to manage the receive of fd */
-	ret = pthread_create(&sessiond_thread, NULL, consumer_thread_sessiond_poll,
+	ret = pthread_create(&sessiond_thread, NULL,
+			consumer_thread_sessiond_poll,
 			(void *) ctx);
-	if (ret != 0) {
-		perror("pthread_create");
-		goto sessiond_error;
+	if (ret) {
+		errno = ret;
+		PERROR("pthread_create");
+		retval = -1;
+		goto exit_sessiond_thread;
 	}
 
 	/*
@@ -476,70 +541,86 @@ int main(int argc, char **argv)
 	 */
 	ret = pthread_create(&metadata_timer_thread, NULL,
 			consumer_timer_thread, (void *) ctx);
-	if (ret != 0) {
-		perror("pthread_create");
-		goto metadata_timer_error;
+	if (ret) {
+		errno = ret;
+		PERROR("pthread_create");
+		retval = -1;
+		goto exit_metadata_timer_thread;
 	}
 
 	ret = pthread_detach(metadata_timer_thread);
 	if (ret) {
 		errno = ret;
-		perror("pthread_detach");
+		PERROR("pthread_detach");
+		retval = -1;
+		goto exit_metadata_timer_detach;
 	}
 
-metadata_timer_error:
+	/*
+	 * This is where we start awaiting program completion (e.g. through
+	 * signal that asks threads to teardown.
+	 */
+
+exit_metadata_timer_detach:
+exit_metadata_timer_thread:
 	ret = pthread_join(sessiond_thread, &status);
-	if (ret != 0) {
-		perror("pthread_join");
-		goto error;
+	if (ret) {
+		errno = ret;
+		PERROR("pthread_join sessiond_thread");
+		retval = -1;
 	}
+exit_sessiond_thread:
 
-sessiond_error:
 	ret = pthread_join(data_thread, &status);
-	if (ret != 0) {
-		perror("pthread_join");
-		goto error;
+	if (ret) {
+		errno = ret;
+		PERROR("pthread_join data_thread");
+		retval = -1;
 	}
+exit_data_thread:
 
-data_error:
 	ret = pthread_join(metadata_thread, &status);
-	if (ret != 0) {
-		perror("pthread_join");
-		goto error;
+	if (ret) {
+		errno = ret;
+		PERROR("pthread_join metadata_thread");
+		retval = -1;
 	}
+exit_metadata_thread:
 
-metadata_error:
 	ret = pthread_join(channel_thread, &status);
-	if (ret != 0) {
-		perror("pthread_join");
-		goto error;
+	if (ret) {
+		errno = ret;
+		PERROR("pthread_join channel_thread");
+		retval = -1;
 	}
+exit_channel_thread:
 
-channel_error:
 	ret = pthread_join(health_thread, &status);
-	if (ret != 0) {
-		PERROR("pthread_join health thread");
-		goto error;	/* join error, exit without cleanup */
+	if (ret) {
+		errno = ret;
+		PERROR("pthread_join health_thread");
+		retval = -1;
 	}
+exit_health_thread:
 
-health_error:
 	utils_close_pipe(health_quit_pipe);
+exit_health_pipe:
 
-error_health_pipe:
-	if (!ret) {
-		ret = EXIT_SUCCESS;
-		goto end;
-	}
-
-error:
-	ret = EXIT_FAILURE;
-
-end:
+exit_init_data:
 	lttng_consumer_destroy(ctx);
 	lttng_consumer_cleanup();
+
 	if (health_consumerd) {
 		health_app_destroy(health_consumerd);
 	}
+exit_health_consumerd_cleanup:
 
-	return ret;
+exit_options:
+
+exit_set_signal_handler:
+	if (!retval) {
+		exit(EXIT_SUCCESS);
+	} else {
+		exit(EXIT_FAILURE);
+	}
 }
Signature · 1 line
2.1.1
#13 lore raw

PATCH 13/13 Refactor relayd main/set_options/cleanup

In reply to a message not in the archive

- Enforce symmetry between allocation and teardown,
- Handle all errors,
- Return all errors as EXIT_FAILURE,
- Standardize on zero being success, nonzero being error, (rather than < 0 being error),
- Fix pthread PERROR: we need to store ret into errno before calling PERROR, since pthread API does not set errno,
- Join errors now fall-through, rather than rely on the OS to teardown the rest.

---
 src/bin/lttng-relayd/live.c | 127 ++++++++++++++++-----------
 src/bin/lttng-relayd/live.h |   5 +-
 src/bin/lttng-relayd/main.c | 207 ++++++++++++++++++++++++++------------------
 3 files changed, 202 insertions(+), 137 deletions(-)
src/bin/lttng-relayd/live.c +75 -52
index 93d458b..beb67b2 100644
@@ -95,7 +95,7 @@ static uint64_t last_relay_viewer_session_id;
  * Cleanup the daemon
  */
 static
-void cleanup(void)
+void cleanup_relayd_live(void)
 {
 	DBG("Cleaning up");
 
@@ -346,20 +346,22 @@ int notify_thread_pipe(int wpipe)
  * Stop all threads by closing the thread quit pipe.
  */
 static
-void stop_threads(void)
+int stop_threads(void)
 {
-	int ret;
+	int ret, retval = 0;
 
 	/* Stopping all threads */
 	DBG("Terminating all live threads");
 	ret = notify_thread_pipe(thread_quit_pipe[1]);
 	if (ret < 0) {
 		ERR("write error on thread quit pipe");
+		retval = -1;
 	}
 
 	/* Dispatch thread */
 	CMM_STORE_SHARED(live_dispatch_thread_exit, 1);
 	futex_nto1_wake(&viewer_conn_queue.futex);
+	return retval;
 }
 
 /*
@@ -591,7 +593,9 @@ error_sock_control:
 	}
 	health_unregister(health_relayd);
 	DBG("Live viewer listener thread cleanup complete");
-	stop_threads();
+	if (stop_threads()) {
+		ERR("Error stopping live threads");
+	}
 	return NULL;
 }
 
@@ -668,7 +672,9 @@ error_testpoint:
 	}
 	health_unregister(health_relayd);
 	DBG("Live viewer dispatch thread dying");
-	stop_threads();
+	if (stop_threads()) {
+		ERR("Error stopping live threads");
+	}
 	return NULL;
 }
 
@@ -2032,7 +2038,9 @@ error_testpoint:
 		ERR("Health error occurred in %s", __func__);
 	}
 	health_unregister(health_relayd);
-	stop_threads();
+	if (stop_threads()) {
+		ERR("Error stopping live threads");
+	}
 	rcu_unregister_thread();
 	return NULL;
 }
@@ -2043,55 +2051,59 @@ error_testpoint:
  */
 static int create_conn_pipe(void)
 {
-	int ret;
-
-	ret = utils_create_pipe_cloexec(live_conn_pipe);
+	return utils_create_pipe_cloexec(live_conn_pipe);
+}
 
-	return ret;
+int relayd_live_stop(void)
+{
+	return stop_threads();
 }
 
-void live_stop_threads(void)
+int relayd_live_join(void)
 {
-	int ret;
+	int ret, retval = 0;
 	void *status;
 
-	stop_threads();
-
 	ret = pthread_join(live_listener_thread, &status);
-	if (ret != 0) {
+	if (ret) {
+		errno = ret;
 		PERROR("pthread_join live listener");
-		goto error;	/* join error, exit without cleanup */
+		retval = -1;
 	}
 
 	ret = pthread_join(live_worker_thread, &status);
-	if (ret != 0) {
+	if (ret) {
+		errno = ret;
 		PERROR("pthread_join live worker");
-		goto error;	/* join error, exit without cleanup */
+		retval = -1;
 	}
 
 	ret = pthread_join(live_dispatcher_thread, &status);
-	if (ret != 0) {
+	if (ret) {
+		errno = ret;
 		PERROR("pthread_join live dispatcher");
-		goto error;	/* join error, exit without cleanup */
+		retval = -1;
 	}
 
-	cleanup();
+	cleanup_relayd_live();
 
-error:
-	return;
+	return retval;
 }
 
 /*
  * main
  */
-int live_start_threads(struct lttng_uri *uri,
+int relayd_live_create(struct lttng_uri *uri,
 		struct relay_local_data *relay_ctx)
 {
-	int ret = 0;
+	int ret = 0, retval = 0;
 	void *status;
 	int is_root;
 
-	assert(uri);
+	if (!uri) {
+		retval = -1;
+		goto exit_init_data;
+	}
 	live_uri = uri;
 
 	/* Check if daemon is UID = 0 */
@@ -2100,14 +2112,15 @@ int live_start_threads(struct lttng_uri *uri,
 	if (!is_root) {
 		if (live_uri->port < 1024) {
 			ERR("Need to be root to use ports < 1024");
-			ret = -1;
-			goto exit;
+			retval = -1;
+			goto exit_init_data;
 		}
 	}
 
 	/* Setup the thread apps communication pipe. */
-	if ((ret = create_conn_pipe()) < 0) {
-		goto exit;
+	if (create_conn_pipe()) {
+		retval = -1;
+		goto exit_init_data;
 	}
 
 	/* Init relay command queue. */
@@ -2119,55 +2132,65 @@ int live_start_threads(struct lttng_uri *uri,
 	/* Setup the dispatcher thread */
 	ret = pthread_create(&live_dispatcher_thread, NULL,
 			thread_dispatcher, (void *) NULL);
-	if (ret != 0) {
+	if (ret) {
+		errno = ret;
 		PERROR("pthread_create viewer dispatcher");
-		goto exit_dispatcher;
+		retval = -1;
+		goto exit_dispatcher_thread;
 	}
 
 	/* Setup the worker thread */
 	ret = pthread_create(&live_worker_thread, NULL,
 			thread_worker, relay_ctx);
-	if (ret != 0) {
+	if (ret) {
+		errno = ret;
 		PERROR("pthread_create viewer worker");
-		goto exit_worker;
+		retval = -1;
+		goto exit_worker_thread;
 	}
 
 	/* Setup the listener thread */
 	ret = pthread_create(&live_listener_thread, NULL,
 			thread_listener, (void *) NULL);
-	if (ret != 0) {
+	if (ret) {
+		errno = ret;
 		PERROR("pthread_create viewer listener");
-		goto exit_listener;
+		retval = -1;
+		goto exit_listener_thread;
 	}
 
-	ret = 0;
-	goto end;
+	/*
+	 * All OK, started all threads.
+	 */
+	return retval;
+
 
-exit_listener:
 	ret = pthread_join(live_listener_thread, &status);
-	if (ret != 0) {
+	if (ret) {
+		errno = ret;
 		PERROR("pthread_join live listener");
-		goto error;	/* join error, exit without cleanup */
+		retval = -1;
 	}
+exit_listener_thread:
 
-exit_worker:
 	ret = pthread_join(live_worker_thread, &status);
-	if (ret != 0) {
+	if (ret) {
+		errno = ret;
 		PERROR("pthread_join live worker");
-		goto error;	/* join error, exit without cleanup */
+		retval = -1;
 	}
+exit_worker_thread:
 
-exit_dispatcher:
 	ret = pthread_join(live_dispatcher_thread, &status);
-	if (ret != 0) {
+	if (ret) {
+		errno = ret;
 		PERROR("pthread_join live dispatcher");
-		goto error;	/* join error, exit without cleanup */
+		retval = -1;
 	}
+exit_dispatcher_thread:
 
-exit:
-	cleanup();
+exit_init_data:
+	cleanup_relayd_live();
 
-end:
-error:
-	return ret;
+	return retval;
 }
src/bin/lttng-relayd/live.h +3 -2
index 52608a4..5db940b 100644
@@ -23,9 +23,10 @@
 
 #include "lttng-relayd.h"
 
-int live_start_threads(struct lttng_uri *live_uri,
+int relayd_live_create(struct lttng_uri *live_uri,
 		struct relay_local_data *relay_ctx);
-void live_stop_threads(void);
+int relayd_live_stop(void);
+int relayd_live_join(void);
 
 struct relay_viewer_stream *live_find_viewer_stream_by_id(uint64_t stream_id);
 
src/bin/lttng-relayd/main.c +124 -83

413 lines not on this page · raw message

Signature · 1 line
2.1.1

A message that is not in the archive (sent off-list or not yet received): 1418867124-17343-12-git-send-email-mathieu.desnoyers@efficios.com

#15 lore raw

Re: [PATCH lttng-tools 11/13] Refactor sessiond main/cleanup/ht-cleanup

Quoted 29 lines · Mathieu Desnoyers

Main reason for this entire patchset: move teardown of ht-cleanup thread _after_ the sessiond cleanup which needs to destroy hash tables. This fixes leaks at sessiond teardown that makes the valgrind output hard to use. As this has been done, various other issues with error handling, leaks, and symmetry of allocation and teardown have been fixed, which makes this a refactoring.

- Enforce symmetry between allocation and teardown,
- Handle all errors,
- Return all errors as EXIT_FAILURE,
- Standardize on zero being success, nonzero being error, (rather than < 0 being error),
- Fix pthread PERROR: we need to store ret into errno before calling PERROR, since pthread API does not set errno,
- Join errors now fall-through, rather than rely on the OS to teardown the rest.

---
 src/bin/lttng-sessiond/ht-cleanup.c     |  41 +-
 src/bin/lttng-sessiond/lttng-sessiond.h |   4 +
 src/bin/lttng-sessiond/main.c           | 781 ++++++++++++++++++++------------
 3 files changed, 529 insertions(+), 297 deletions(-)
src/bin/lttng-sessiond/ht-cleanup.c +0 -0
index 0469f92..79fb9cd 100644

src/bin/lttng-sessiond/ht-cleanup.c void *thread_ht_cleanup(void *data) · line 50

Quoted 5 lines
src/bin/lttng-sessiond/ht-cleanup.c +1 -1
@@ -47,7 +47,7 @@ void *thread_ht_cleanup(void *data)
 
        health_code_update();
 
-       ret = sessiond_set_thread_pollset(&events, 2);
+       ret = sessiond_set_ht_cleanup_thread_pollset(&events, 2);

Won't this result in adding the ht_cleanup_quit_pipe twice to the pollset? (see line 56 in lttng-tools/src/bin/lttng-sessiond/ht-cleanup.c)

Quoted 228 lines
src/bin/lttng-sessiond/ht-cleanup.c +30 -9
        if (ret < 0) {
                goto error_poll_create;
        }
@@ -61,11 +61,14 @@ void *thread_ht_cleanup(void *data)
        health_code_update();
 
        while (1) {
+               int handled_event;
+
                DBG3("[ht-thread] Polling on %d fds.",
                        LTTNG_POLL_GETNB(&events));
 
                /* Inifinite blocking call, waiting for transmission */
 restart:
+               handled_event = 0;
                health_poll_entry();
                ret = lttng_poll_wait(&events, -1);
                health_poll_exit();
@@ -90,13 +93,9 @@ restart:
                        revents = LTTNG_POLL_GETEV(&events, i);
                        pollfd = LTTNG_POLL_GETFD(&events, i);
 
-                       /* Thread quit pipe has been closed. Killing thread. */
-                       ret = sessiond_check_thread_quit_pipe(pollfd, revents);
-                       if (ret) {
-                               err = 0;
-                               goto exit;
+                       if (pollfd != ht_cleanup_pipe[0]) {
+                               continue;
                        }
-                       assert(pollfd == ht_cleanup_pipe[0]);
 
                        if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
                                ERR("ht cleanup pipe error");
@@ -125,6 +124,30 @@ restart:
 
                        health_code_update();
                }
+
+               /* Only check cleanup quit when no more work to do. */
+               if (handled_event) {
+                       continue;
+               }
+
+               for (i = 0; i < nb_fd; i++) {
+                       health_code_update();
+
+                       /* Fetch once the poll data */
+                       revents = LTTNG_POLL_GETEV(&events, i);
+                       pollfd = LTTNG_POLL_GETFD(&events, i);
+
+                       if (pollfd == ht_cleanup_pipe[0]) {
+                               continue;
+                       }
+
+                       /* Thread quit pipe has been closed. Killing thread. */
+                       ret = sessiond_check_ht_cleanup_quit(pollfd, revents);
+                       if (ret) {
+                               err = 0;
+                               goto exit;
+                       }
+               }
        }
 
 exit:
@@ -132,9 +155,7 @@ error:
        lttng_poll_clean(&events);
 error_poll_create:
 error_testpoint:
-       utils_close_pipe(ht_cleanup_pipe);
-       ht_cleanup_pipe[0] = ht_cleanup_pipe[1] = -1;
-       DBG("[ust-thread] cleanup complete.");
+       DBG("[ht-cleanup] Thread terminates.");
        if (err) {
                health_error();
                ERR("Health error occurred in %s", __func__);
src/bin/lttng-sessiond/lttng-sessiond.h +4 -0
index 488cf2f..07a6edd 100644
@@ -121,6 +121,10 @@ extern int is_root;
 int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size);
 int sessiond_check_thread_quit_pipe(int fd, uint32_t events);
 
+int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event *events,
+               size_t size);
+int sessiond_check_ht_cleanup_quit(int fd, uint32_t events);
+
 void *thread_ht_cleanup(void *data);
 
 void sessiond_notify_ready(void);
src/bin/lttng-sessiond/main.c +58 -13
index 0b116e1..f418d74 100644
@@ -189,6 +189,7 @@ static int kernel_poll_pipe[2] = { -1, -1 };
  * for all threads when receiving an event on the pipe.
  */
 static int thread_quit_pipe[2] = { -1, -1 };
+static int ht_cleanup_quit_pipe[2] = { -1, -1 };
 
 /*
  * This pipe is used to inform the thread managing application communication
@@ -384,10 +385,9 @@ void setup_consumerd_path(void)
        }
 }
 
-/*
- * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
- */
-int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
+static
+int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size,
+               int *a_pipe)
 {
        int ret;
 
@@ -399,7 +399,7 @@ int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
        }
 
        /* Add quit pipe */
-       ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
+       ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR);
        if (ret < 0) {
                goto error;
        }
@@ -411,17 +411,52 @@ error:
 }
 
 /*
+ * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
+ */
+int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
+{
+       return __sessiond_set_thread_pollset(events, size, thread_quit_pipe);
+}
+
+/*
+ * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
+ */
+int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event *events,
+               size_t size)
+{
+       return __sessiond_set_thread_pollset(events, size,
+                       ht_cleanup_quit_pipe);
+}
+
+static
+int __sessiond_check_thread_quit_pipe(int fd, uint32_t events, int a_pipe)
+{
+       if (fd == a_pipe && (events & LPOLLIN)) {
+               return 1;
+       }
+       return 0;
+}
+
+/*
  * Check if the thread quit pipe was triggered.
  *
  * Return 1 if it was triggered else 0;
  */
 int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
 {
-       if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
-               return 1;
-       }
+       return __sessiond_check_thread_quit_pipe(fd, events,
+                       thread_quit_pipe[0]);
+}
 
-       return 0;
+/*
+ * Check if the ht_cleanup thread quit pipe was triggered.
+ *
+ * Return 1 if it was triggered else 0;
+ */
+int sessiond_check_ht_cleanup_quit(int fd, uint32_t events)
+{
+       return __sessiond_check_thread_quit_pipe(fd, events,
+                       ht_cleanup_quit_pipe[0]);
 }
 
 /*
@@ -429,18 +464,18 @@ int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
  *
  * Return -1 on error or 0 if all pipes are created.
  */
-static int init_thread_quit_pipe(void)
+static int __init_thread_quit_pipe(int *a_pipe)
 {
        int ret, i;
 
-       ret = pipe(thread_quit_pipe);
+       ret = pipe(a_pipe);
        if (ret < 0) {
                PERROR("thread quit pipe");
                goto error;
        }
 
        for (i = 0; i < 2; i++) {
-               ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
+               ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC);
                if (ret < 0) {
                        PERROR("fcntl");
                        goto error;
@@ -451,6 +486,16 @@ error:
        return ret;
 }
 
+static int init_thread_quit_pipe(void)
+{
+       return __init_thread_quit_pipe(thread_quit_pipe);
+}
+
+static int init_ht_cleanup_quit_pipe(void)
+{
+       return __init_thread_quit_pipe(ht_cleanup_quit_pipe);
+}
+
 /*
  * Stop all threads by closing the thread quit pipe.
  */
@@ -537,15 +582,15 @@ static int generate_lock_file_path(char *path, size_t len)
 }
 
 /*
- * Cleanup the daemon
+ * Cleanup the session daemon's data structures.

src/bin/lttng-sessiond/main.c static int generate_lock_file_path(char *path, size_t len) · line 593

Quoted 10 lines
src/bin/lttng-sessiond/main.c +2 -2
  */
-static void cleanup(void)
+static void sessiond_cleanup(void)
 {
        int ret;
        struct ltt_session *sess, *stmp;
        char path[PATH_MAX];
 
-       DBG("Cleaning up");
+       DBG("Cleanup up sessiond");

Slight typo here. I'll fix it on my end.

Quoted 1,140 lines
src/bin/lttng-sessiond/main.c +434 -273
        /*
         * Close the thread quit pipe. It has already done its job,
@@ -649,34 +694,6 @@ static void cleanup(void)
 
        close_consumer_sockets();
 
-       /*
-        * If the override option is set, the pointer points to a *non* const thus
-        * freeing it even though the variable type is set to const.
-        */
-       if (tracing_group_name_override) {
-               free((void *) tracing_group_name);
-       }
-       if (consumerd32_bin_override) {
-               free((void *) consumerd32_bin);
-       }
-       if (consumerd64_bin_override) {
-               free((void *) consumerd64_bin);
-       }
-       if (consumerd32_libdir_override) {
-               free((void *) consumerd32_libdir);
-       }
-       if (consumerd64_libdir_override) {
-               free((void *) consumerd64_libdir);
-       }
-
-       if (opt_pidfile) {
-               free(opt_pidfile);
-       }
-
-       if (opt_load_session_path) {
-               free(opt_load_session_path);
-       }
-
        if (load_info) {
                load_session_destroy_data(load_info);
                free(load_info);
@@ -689,7 +706,8 @@ static void cleanup(void)
        if (lockfile_fd >= 0) {
                char lockfile_path[PATH_MAX];
 
-               ret = generate_lock_file_path(lockfile_path, sizeof(lockfile_path));
+               ret = generate_lock_file_path(lockfile_path,
+                               sizeof(lockfile_path));
                if (ret > 0) {
                        ret = remove(lockfile_path);
                        if (ret < 0) {
@@ -709,6 +727,39 @@ static void cleanup(void)
         */
 
        free(rundir);
+}
+
+/*
+ * Cleanup the daemon's option data structures.
+ */
+static void sessiond_cleanup_options(void)
+{
+       DBG("Cleaning up options");
+
+       /*
+        * If the override option is set, the pointer points to a *non* const
+        * thus freeing it even though the variable type is set to const.
+        */
+       if (tracing_group_name_override) {
+               free((void *) tracing_group_name);
+       }
+       if (consumerd32_bin_override) {
+               free((void *) consumerd32_bin);
+       }
+       if (consumerd64_bin_override) {
+               free((void *) consumerd64_bin);
+       }
+       if (consumerd32_libdir_override) {
+               free((void *) consumerd32_libdir);
+       }
+       if (consumerd64_libdir_override) {
+               free((void *) consumerd64_libdir);
+       }
+
+       free(opt_pidfile);
+       free(opt_load_session_path);
+       free(kmod_probes_list);
+       free(kmod_extra_probes_list);
 
        /* <fun> */
        DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
@@ -873,42 +924,47 @@ static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
                }
                ksess = session->kernel_session;
 
-               cds_list_for_each_entry(channel, &ksess->channel_list.head, list) {
-                       if (channel->fd == fd) {
-                               DBG("Channel found, updating kernel streams");
-                               ret = kernel_open_channel_stream(channel);
-                               if (ret < 0) {
-                                       goto error;
-                               }
-                               /* Update the stream global counter */
-                               ksess->stream_count_global += ret;
+               cds_list_for_each_entry(channel,
+                               &ksess->channel_list.head, list) {
+                       struct lttng_ht_iter iter;
+                       struct consumer_socket *socket;
 
-                               /*
-                                * Have we already sent fds to the consumer? If yes, it means
-                                * that tracing is started so it is safe to send our updated
-                                * stream fds.
-                                */
-                               if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) {
-                                       struct lttng_ht_iter iter;
-                                       struct consumer_socket *socket;
-
-                                       rcu_read_lock();
-                                       cds_lfht_for_each_entry(ksess->consumer->socks->ht,
-                                                       &iter.iter, socket, node.node) {
-                                               pthread_mutex_lock(socket->lock);
-                                               ret = kernel_consumer_send_channel_stream(socket,
-                                                               channel, ksess,
-                                                               session->output_traces ? 1 : 0);
-                                               pthread_mutex_unlock(socket->lock);
-                                               if (ret < 0) {
-                                                       rcu_read_unlock();
-                                                       goto error;
-                                               }
-                                       }
+                       if (channel->fd != fd) {
+                               continue;
+                       }
+                       DBG("Channel found, updating kernel streams");
+                       ret = kernel_open_channel_stream(channel);
+                       if (ret < 0) {
+                               goto error;
+                       }
+                       /* Update the stream global counter */
+                       ksess->stream_count_global += ret;
+
+                       /*
+                        * Have we already sent fds to the consumer? If yes, it
+                        * means that tracing is started so it is safe to send
+                        * our updated stream fds.
+                        */
+                       if (ksess->consumer_fds_sent != 1
+                                       || ksess->consumer == NULL) {
+                               ret = -1;
+                               goto error;
+                       }
+
+                       rcu_read_lock();
+                       cds_lfht_for_each_entry(ksess->consumer->socks->ht,
+                                       &iter.iter, socket, node.node) {
+                               pthread_mutex_lock(socket->lock);
+                               ret = kernel_consumer_send_channel_stream(socket,
+                                               channel, ksess,
+                                               session->output_traces ? 1 : 0);
+                               pthread_mutex_unlock(socket->lock);
+                               if (ret < 0) {
                                        rcu_read_unlock();
+                                       goto error;
                                }
-                               goto error;
                        }
+                       rcu_read_unlock();
                }
                session_unlock(session);
        }
@@ -2148,7 +2204,7 @@ static int spawn_consumer_thread(struct consumer_data *consumer_data)
 
        /* Setup pthread condition */
        ret = pthread_condattr_init(&consumer_data->condattr);
-       if (ret != 0) {
+       if (ret) {
                errno = ret;
                PERROR("pthread_condattr_init consumer data");
                goto error;
@@ -2160,14 +2216,14 @@ static int spawn_consumer_thread(struct consumer_data *consumer_data)
         * for a more details and how we noticed it.
         */
        ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
-       if (ret != 0) {
+       if (ret) {
                errno = ret;
                PERROR("pthread_condattr_setclock consumer data");
                goto error;
        }
 
        ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
-       if (ret != 0) {
+       if (ret) {
                errno = ret;
                PERROR("pthread_cond_init consumer data");
                goto error;
@@ -2175,7 +2231,8 @@ static int spawn_consumer_thread(struct consumer_data *consumer_data)
 
        ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
                        consumer_data);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_create consumer");
                ret = -1;
                goto error;
@@ -2271,7 +2328,7 @@ static int join_consumer_thread(struct consumer_data *consumer_data)
                int ret;
                ret = kill(consumer_data->pid, SIGTERM);
                if (ret) {
-                       ERR("Error killing consumer daemon");
+                       PERROR("Error killing consumer daemon");
                        return ret;
                }
                return pthread_join(consumer_data->thread, &status);
@@ -4193,6 +4250,28 @@ error_create_poll:
        DBG("Client thread dying");
 
        rcu_unregister_thread();
+
+       /*
+        * Since we are creating the consumer threads, we own them, so we need
+        * to join them before our thread exits.
+        */
+       ret = join_consumer_thread(&kconsumer_data);
+       if (ret) {
+               errno = ret;
+               PERROR("join_consumer");
+       }
+
+       ret = join_consumer_thread(&ustconsumer32_data);
+       if (ret) {
+               errno = ret;
+               PERROR("join_consumer ust32");
+       }
+
+       ret = join_consumer_thread(&ustconsumer64_data);
+       if (ret) {
+               errno = ret;
+               PERROR("join_consumer ust64");
+       }
        return NULL;
 }
 
@@ -4291,7 +4370,7 @@ static int set_option(int opt, const char *arg, const char *optname)
                break;
        case 'h':
                usage();
-               exit(EXIT_FAILURE);
+               exit(EXIT_SUCCESS);
        case 'V':
                fprintf(stdout, "%s\n", VERSION);
                exit(EXIT_SUCCESS);
@@ -4926,7 +5005,7 @@ static void set_ulimit(void)
 /*
  * Write pidfile using the rundir and opt_pidfile.
  */
-static void write_pidfile(void)
+static int write_pidfile(void)
 {
        int ret;
        char pidfile_path[PATH_MAX];
@@ -4946,13 +5025,11 @@ static void write_pidfile(void)
        }
 
        /*
-        * Create pid file in rundir. Return value is of no importance. The
-        * execution will continue even though we are not able to write the file.
+        * Create pid file in rundir.
         */
-       (void) utils_create_pid_file(getpid(), pidfile_path);
-
+       ret = utils_create_pid_file(getpid(), pidfile_path);
 error:
-       return;
+       return ret;
 }
 
 /*
@@ -4976,7 +5053,7 @@ error:
 /*
  * Write agent TCP port using the rundir.
  */
-static void write_agent_port(void)
+static int write_agent_port(void)
 {
        int ret;
        char path[PATH_MAX];
@@ -4991,43 +5068,12 @@ static void write_agent_port(void)
        }
 
        /*
-        * Create TCP agent port file in rundir. Return value is of no importance.
-        * The execution will continue even though we are not able to write the
-        * file.
+        * Create TCP agent port file in rundir.
         */
-       (void) utils_create_pid_file(agent_tcp_port, path);
+       ret = utils_create_pid_file(agent_tcp_port, path);
 
 error:
-       return;
-}
-
-/*
- * Start the load session thread and dettach from it so the main thread can
- * continue. This does not return a value since whatever the outcome, the main
- * thread will continue.
- */
-static void start_load_session_thread(void)
-{
-       int ret;
-
-       /* Create session loading thread. */
-       ret = pthread_create(&load_session_thread, NULL, thread_load_session,
-                       load_info);
-       if (ret != 0) {
-               PERROR("pthread_create load_session_thread");
-               goto error_create;
-       }
-
-       ret = pthread_detach(load_session_thread);
-       if (ret != 0) {
-               PERROR("pthread_detach load_session_thread");
-       }
-
-       /* Everything went well so don't cleanup anything. */
-
-error_create:
-       /* The cleanup() function will destroy the load_info data. */
-       return;
+       return ret;
 }
 
 /*
@@ -5035,7 +5081,7 @@ error_create:
  */
 int main(int argc, char **argv)
 {
-       int ret = 0;
+       int ret = 0, retval = 0;
        void *status;
        const char *home_path, *env_app_timeout;
 
@@ -5043,8 +5089,9 @@ int main(int argc, char **argv)
 
        rcu_register_thread();
 
-       if ((ret = set_signal_handler()) < 0) {
-               goto error;
+       if (set_signal_handler()) {
+               retval = -1;
+               goto exit_set_signal_handler;
        }
 
        setup_consumerd_path();
@@ -5056,10 +5103,18 @@ int main(int argc, char **argv)
                WARN("Fallback page size to %ld", page_size);
        }
 
-       /* Parse arguments and load the daemon configuration file */
+       /*
+        * Parse arguments and load the daemon configuration file.
+        *
+        * We have an exit_options exit path to free memory reserved by
+        * set_options. This is needed because the rest of sessiond_cleanup()
+        * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
+        * depends on set_options.
+        */
        progname = argv[0];
-       if ((ret = set_options(argc, argv)) < 0) {
-               goto error;
+       if (set_options(argc, argv)) {
+               retval = -1;
+               goto exit_options;
        }
 
        /* Daemonize */
@@ -5069,22 +5124,61 @@ int main(int argc, char **argv)
                ret = lttng_daemonize(&child_ppid, &recv_child_signal,
                        !opt_background);
                if (ret < 0) {
-                       goto error;
+                       retval = -1;
+                       goto exit_options;
                }
 
                /*
                 * We are in the child. Make sure all other file descriptors are
-                * closed, in case we are called with more opened file descriptors than
-                * the standard ones.
+                * closed, in case we are called with more opened file
+                * descriptors than the standard ones.
                 */
                for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
                        (void) close(i);
                }
        }
 
+       /*
+        * Starting from here, we can create threads. This needs to be after
+        * lttng_daemonize due to RCU.
+        */
+
+       /*
+        * Initialize the health check subsystem. This call should set the
+        * appropriate time values.
+        */
+       health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
+       if (!health_sessiond) {
+               PERROR("health_app_create error");
+               retval = -1;
+               goto exit_health_sessiond_cleanup;
+       }
+
+       if (init_ht_cleanup_quit_pipe()) {
+               retval = -1;
+               goto exit_ht_cleanup_quit_pipe;
+       }
+
+       /* Setup the thread ht_cleanup communication pipe. */
+       if (utils_create_pipe_cloexec(ht_cleanup_pipe)) {
+               retval = -1;
+               goto exit_ht_cleanup_pipe;
+       }
+
+       /* Create thread to clean up RCU hash tables */
+       ret = pthread_create(&ht_cleanup_thread, NULL,
+                       thread_ht_cleanup, (void *) NULL);
+       if (ret) {
+               errno = ret;
+               PERROR("pthread_create ht_cleanup");
+               retval = -1;
+               goto exit_ht_cleanup;
+       }
+
        /* Create thread quit pipe */
-       if ((ret = init_thread_quit_pipe()) < 0) {
-               goto error;
+       if (init_thread_quit_pipe()) {
+               retval = -1;
+               goto exit_init_data;
        }
 
        /* Check if daemon is UID = 0 */
@@ -5093,42 +5187,67 @@ int main(int argc, char **argv)
        if (is_root) {
                rundir = strdup(DEFAULT_LTTNG_RUNDIR);
                if (!rundir) {
-                       ret = -ENOMEM;
-                       goto error;
+                       retval = -1;
+                       goto exit_init_data;
                }
 
                /* Create global run dir with root access */
-               ret = create_lttng_rundir(rundir);
-               if (ret < 0) {
-                       goto error;
+               if (create_lttng_rundir(rundir)) {
+                       retval = -1;
+                       goto exit_init_data;
                }
 
                if (strlen(apps_unix_sock_path) == 0) {
-                       snprintf(apps_unix_sock_path, PATH_MAX,
+                       ret = snprintf(apps_unix_sock_path, PATH_MAX,
                                        DEFAULT_GLOBAL_APPS_UNIX_SOCK);
+                       if (ret < 0) {
+                               retval = -1;
+                               goto exit_init_data;
+                       }
                }
 
                if (strlen(client_unix_sock_path) == 0) {
-                       snprintf(client_unix_sock_path, PATH_MAX,
+                       ret = snprintf(client_unix_sock_path, PATH_MAX,
                                        DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
+                       if (ret < 0) {
+                               retval = -1;
+                               goto exit_init_data;
+                       }
                }
 
                /* Set global SHM for ust */
                if (strlen(wait_shm_path) == 0) {
-                       snprintf(wait_shm_path, PATH_MAX,
+                       ret = snprintf(wait_shm_path, PATH_MAX,
                                        DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
+                       if (ret < 0) {
+                               retval = -1;
+                               goto exit_init_data;
+                       }
                }
 
                if (strlen(health_unix_sock_path) == 0) {
-                       snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
+                       ret = snprintf(health_unix_sock_path,
+                                       sizeof(health_unix_sock_path),
                                        DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
+                       if (ret < 0) {
+                               retval = -1;
+                               goto exit_init_data;
+                       }
                }
 
                /* Setup kernel consumerd path */
-               snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
+               ret = snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
                                DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
-               snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
+               if (ret < 0) {
+                       retval = -1;
+                       goto exit_init_data;
+               }
+               ret = snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
                                DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
+               if (ret < 0) {
+                       retval = -1;
+                       goto exit_init_data;
+               }
 
                DBG2("Kernel consumer err path: %s",
                                kconsumer_data.err_unix_sock_path);
@@ -5139,8 +5258,8 @@ int main(int argc, char **argv)
                if (home_path == NULL) {
                        /* TODO: Add --socket PATH option */
                        ERR("Can't get HOME directory for sockets creation.");
-                       ret = -EPERM;
-                       goto error;
+                       retval = -1;
+                       goto exit_init_data;
                }
 
                /*
@@ -5149,42 +5268,64 @@ int main(int argc, char **argv)
                 */
                ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
                if (ret < 0) {
-                       ret = -ENOMEM;
-                       goto error;
+                       retval = -1;
+                       goto exit_init_data;
                }
 
-               ret = create_lttng_rundir(rundir);
-               if (ret < 0) {
-                       goto error;
+               if (create_lttng_rundir(rundir)) {
+                       retval = -1;
+                       goto exit_init_data;
                }
 
                if (strlen(apps_unix_sock_path) == 0) {
-                       snprintf(apps_unix_sock_path, PATH_MAX,
-                                       DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
+                       ret = snprintf(apps_unix_sock_path, PATH_MAX,
+                                       DEFAULT_HOME_APPS_UNIX_SOCK,
+                                       home_path);
+                       if (ret < 0) {
+                               retval = -1;
+                               goto exit_init_data;
+                       }
                }
 
                /* Set the cli tool unix socket path */
                if (strlen(client_unix_sock_path) == 0) {
-                       snprintf(client_unix_sock_path, PATH_MAX,
-                                       DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
+                       ret = snprintf(client_unix_sock_path, PATH_MAX,
+                                       DEFAULT_HOME_CLIENT_UNIX_SOCK,
+                                       home_path);
+                       if (ret < 0) {
+                               retval = -1;
+                               goto exit_init_data;
+                       }
                }
 
                /* Set global SHM for ust */
                if (strlen(wait_shm_path) == 0) {
-                       snprintf(wait_shm_path, PATH_MAX,
-                                       DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
+                       ret = snprintf(wait_shm_path, PATH_MAX,
+                                       DEFAULT_HOME_APPS_WAIT_SHM_PATH,
+                                       getuid());
+                       if (ret < 0) {
+                               retval = -1;
+                               goto exit_init_data;
+                       }
                }
 
                /* Set health check Unix path */
                if (strlen(health_unix_sock_path) == 0) {
-                       snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
-                                       DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
+                       ret = snprintf(health_unix_sock_path,
+                                       sizeof(health_unix_sock_path),
+                                       DEFAULT_HOME_HEALTH_UNIX_SOCK,
+                                       home_path);
+                       if (ret < 0) {
+                               retval = -1;
+                               goto exit_init_data;
+                       }
                }
        }
 
        lockfile_fd = create_lockfile();
        if (lockfile_fd < 0) {
-               goto error;
+               retval = -1;
+               goto exit_init_data;
        }
 
        /* Set consumer initial state */
@@ -5197,10 +5338,20 @@ int main(int argc, char **argv)
        DBG("LTTng run directory path: %s", rundir);
 
        /* 32 bits consumerd path setup */
-       snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
+       ret = snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
                        DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
-       snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
+       if (ret < 0) {
+               PERROR("snprintf 32-bit consumer error socket path");
+               retval = -1;
+               goto exit_init_data;
+       }
+       ret = snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
                        DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
+       if (ret < 0) {
+               PERROR("snprintf 32-bit consumer command socket path");
+               retval = -1;
+               goto exit_init_data;
+       }
 
        DBG2("UST consumer 32 bits err path: %s",
                        ustconsumer32_data.err_unix_sock_path);
@@ -5208,10 +5359,20 @@ int main(int argc, char **argv)
                        ustconsumer32_data.cmd_unix_sock_path);
 
        /* 64 bits consumerd path setup */
-       snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
+       ret = snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
                        DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
-       snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
+       if (ret < 0) {
+               PERROR("snprintf 64-bit consumer error socket path");
+               retval = -1;
+               goto exit_init_data;
+       }
+       ret = snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
                        DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
+       if (ret < 0) {
+               PERROR("snprintf 64-bit consumer command socket path");
+               retval = -1;
+               goto exit_init_data;
+       }
 
        DBG2("UST consumer 64 bits err path: %s",
                        ustconsumer64_data.err_unix_sock_path);
@@ -5221,29 +5382,32 @@ int main(int argc, char **argv)
        /*
         * See if daemon already exist.
         */
-       if ((ret = check_existing_daemon()) < 0) {
+       if (check_existing_daemon()) {
                ERR("Already running daemon.\n");
                /*
                 * We do not goto exit because we must not cleanup()
                 * because a daemon is already running.
                 */
-               goto error;
+               retval = -1;
+               goto exit_init_data;
        }
 
        /*
         * Init UST app hash table. Alloc hash table before this point since
         * cleanup() can get called after that point.
         */
-       ust_app_ht_alloc();
+       if (ust_app_ht_alloc()) {
+               retval = -1;
+               goto exit_init_data;
+       }
 
        /* Initialize agent domain subsystem. */
-       if ((ret = agent_setup()) < 0) {
+       if (agent_setup()) {
                /* ENOMEM at this point. */
-               goto error;
+               retval = -1;
+               goto exit_init_data;
        }
 
-       /* After this point, we can safely call cleanup() with "goto exit" */
-
        /*
         * These actions must be executed as root. We do that *after* setting up
         * the sockets path because we MUST make the check for another daemon using
@@ -5251,9 +5415,9 @@ int main(int argc, char **argv)
         * kernel tracer.
         */
        if (is_root) {
-               ret = set_consumer_sockets(&kconsumer_data, rundir);
-               if (ret < 0) {
-                       goto exit;
+               if (set_consumer_sockets(&kconsumer_data, rundir)) {
+                       retval = -1;
+                       goto exit_init_data;
                }
 
                /* Setup kernel tracer */
@@ -5262,8 +5426,9 @@ int main(int argc, char **argv)
                        if (kernel_tracer_fd >= 0) {
                                ret = syscall_init_table();
                                if (ret < 0) {
-                                       ERR("Unable to populate syscall table. Syscall tracing"
-                                                       " won't work for this session daemon.");
+                                       ERR("Unable to populate syscall table. "
+                                               "Syscall tracing won't work "
+                                               "for this session daemon.");
                                }
                        }
                }
@@ -5274,24 +5439,26 @@ int main(int argc, char **argv)
        /* init lttng_fd tracking must be done after set_ulimit. */
        lttng_fd_init();
 
-       ret = set_consumer_sockets(&ustconsumer64_data, rundir);
-       if (ret < 0) {
-               goto exit;
+       if (set_consumer_sockets(&ustconsumer64_data, rundir)) {
+               retval = -1;
+               goto exit_init_data;
        }
 
-       ret = set_consumer_sockets(&ustconsumer32_data, rundir);
-       if (ret < 0) {
-               goto exit;
+       if (set_consumer_sockets(&ustconsumer32_data, rundir)) {
+               retval = -1;
+               goto exit_init_data;
        }
 
        /* Setup the needed unix socket */
-       if ((ret = init_daemon_socket()) < 0) {
-               goto exit;
+       if (init_daemon_socket()) {
+               retval = -1;
+               goto exit_init_data;
        }
 
        /* Set credentials to socket */
-       if (is_root && ((ret = set_permissions(rundir)) < 0)) {
-               goto exit;
+       if (is_root && set_permissions(rundir)) {
+               retval = -1;
+               goto exit_init_data;
        }
 
        /* Get parent pid if -S, --sig-parent is specified. */
@@ -5301,24 +5468,22 @@ int main(int argc, char **argv)
 
        /* Setup the kernel pipe for waking up the kernel thread */
        if (is_root && !opt_no_kernel) {
-               if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
-                       goto exit;
+               if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
+                       retval = -1;
+                       goto exit_init_data;
                }
        }
 
-       /* Setup the thread ht_cleanup communication pipe. */
-       if (utils_create_pipe_cloexec(ht_cleanup_pipe) < 0) {
-               goto exit;
-       }
-
        /* Setup the thread apps communication pipe. */
-       if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
-               goto exit;
+       if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
+               retval = -1;
+               goto exit_init_data;
        }
 
        /* Setup the thread apps notify communication pipe. */
-       if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) {
-               goto exit;
+       if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
+               retval = -1;
+               goto exit_init_data;
        }
 
        /* Initialize global buffer per UID and PID registry. */
@@ -5329,8 +5494,8 @@ int main(int argc, char **argv)
        cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
 
        /*
-        * Get session list pointer. This pointer MUST NOT be free(). This list is
-        * statically declared in session.c
+        * Get session list pointer. This pointer MUST NOT be free'd. This list
+        * is statically declared in session.c
         */
        session_list_ptr = session_get_list();
 
@@ -5347,90 +5512,97 @@ int main(int argc, char **argv)
                app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
        }
 
-       write_pidfile();
-       write_agent_port();
+       ret = write_pidfile();
+       if (ret) {
+               ERR("Error in write_pidfile");
+               retval = -1;
+               goto exit_init_data;
+       }
+       ret = write_agent_port();
+       if (ret) {
+               ERR("Error in write_agent_port");
+               retval = -1;
+               goto exit_init_data;
+       }
 
        /* Initialize communication library */
        lttcomm_init();
-       /* This is to get the TCP timeout value. */
+       /* Initialize TCP timeout values */
        lttcomm_inet_init();
 
        if (load_session_init_data(&load_info) < 0) {
-               goto exit;
+               retval = -1;
+               goto exit_init_data;
        }
        load_info->path = opt_load_session_path;
 
-       /*
-        * Initialize the health check subsystem. This call should set the
-        * appropriate time values.
-        */
-       health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
-       if (!health_sessiond) {
-               PERROR("health_app_create error");
-               goto exit_health_sessiond_cleanup;
-       }
-
-       /* Create thread to clean up RCU hash tables */
-       ret = pthread_create(&ht_cleanup_thread, NULL,
-                       thread_ht_cleanup, (void *) NULL);
-       if (ret != 0) {
-               PERROR("pthread_create ht_cleanup");
-               goto exit_ht_cleanup;
-       }
-
        /* Create health-check thread */
        ret = pthread_create(&health_thread, NULL,
                        thread_manage_health, (void *) NULL);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_create health");
+               retval = -1;
                goto exit_health;
        }
 
        /* Create thread to manage the client socket */
        ret = pthread_create(&client_thread, NULL,
                        thread_manage_clients, (void *) NULL);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_create clients");
+               retval = -1;
                goto exit_client;
        }
 
        /* Create thread to dispatch registration */
        ret = pthread_create(&dispatch_thread, NULL,
                        thread_dispatch_ust_registration, (void *) NULL);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_create dispatch");
+               retval = -1;
                goto exit_dispatch;
        }
 
        /* Create thread to manage application registration. */
        ret = pthread_create(&reg_apps_thread, NULL,
                        thread_registration_apps, (void *) NULL);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_create registration");
+               retval = -1;
                goto exit_reg_apps;
        }
 
        /* Create thread to manage application socket */
        ret = pthread_create(&apps_thread, NULL,
                        thread_manage_apps, (void *) NULL);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_create apps");
+               retval = -1;
                goto exit_apps;
        }
 
        /* Create thread to manage application notify socket */
        ret = pthread_create(&apps_notify_thread, NULL,
                        ust_thread_manage_notify, (void *) NULL);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_create notify");
+               retval = -1;
                goto exit_apps_notify;
        }
 
        /* Create agent registration thread. */
        ret = pthread_create(&agent_reg_thread, NULL,
                        agent_thread_manage_registration, (void *) NULL);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_create agent");
+               retval = -1;
                goto exit_agent_reg;
        }
 
@@ -5439,111 +5611,146 @@ int main(int argc, char **argv)
                /* Create kernel thread to manage kernel event */
                ret = pthread_create(&kernel_thread, NULL,
                                thread_manage_kernel, (void *) NULL);
-               if (ret != 0) {
+               if (ret) {
+                       errno = ret;
                        PERROR("pthread_create kernel");
+                       retval = -1;
                        goto exit_kernel;
                }
        }
 
-       /* Load possible session(s). */
-       start_load_session_thread();
+       /* Create session loading thread. */
+       ret = pthread_create(&load_session_thread, NULL, thread_load_session,
+                       load_info);
+       if (ret) {
+               errno = ret;
+               PERROR("pthread_create load_session_thread");
+               retval = -1;
+               goto exit_load_session;
+       }
+
+       /*
+        * This is where we start awaiting program completion (e.g. through
+        * signal that asks threads to teardown).
+        */
+
+       ret = pthread_join(load_session_thread, &status);
+       if (ret) {
+               errno = ret;
+               PERROR("pthread_join load_session_thread");
+               retval = -1;
+       }
+exit_load_session:
 
        if (is_root && !opt_no_kernel) {
                ret = pthread_join(kernel_thread, &status);
-               if (ret != 0) {
+               if (ret) {
+                       errno = ret;
                        PERROR("pthread_join");
-                       goto error;     /* join error, exit without cleanup */
+                       retval = -1;
                }
        }
-
 exit_kernel:
+
        ret = pthread_join(agent_reg_thread, &status);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_join agent");
-               goto error;     /* join error, exit without cleanup */
+               retval = -1;
        }
-
 exit_agent_reg:
+
        ret = pthread_join(apps_notify_thread, &status);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_join apps notify");
-               goto error;     /* join error, exit without cleanup */
+               retval = -1;
        }
-
 exit_apps_notify:
+
        ret = pthread_join(apps_thread, &status);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_join apps");
-               goto error;     /* join error, exit without cleanup */
+               retval = -1;
        }
-
-
 exit_apps:
+
        ret = pthread_join(reg_apps_thread, &status);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_join");
-               goto error;     /* join error, exit without cleanup */
+               retval = -1;
        }
-
 exit_reg_apps:
+
        ret = pthread_join(dispatch_thread, &status);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_join");
-               goto error;     /* join error, exit without cleanup */
+               retval = -1;
        }
-
 exit_dispatch:
+
        ret = pthread_join(client_thread, &status);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_join");
-               goto error;     /* join error, exit without cleanup */
-       }
-
-       ret = join_consumer_thread(&kconsumer_data);
-       if (ret != 0) {
-               PERROR("join_consumer");
-               goto error;     /* join error, exit without cleanup */
+               retval = -1;
        }
+exit_client:
 
-       ret = join_consumer_thread(&ustconsumer32_data);
-       if (ret != 0) {
-               PERROR("join_consumer ust32");
-               goto error;     /* join error, exit without cleanup */
+       ret = pthread_join(health_thread, &status);
+       if (ret) {
+               errno = ret;
+               PERROR("pthread_join health thread");
+               retval = -1;
        }
+exit_health:
 
-       ret = join_consumer_thread(&ustconsumer64_data);
-       if (ret != 0) {
-               PERROR("join_consumer ust64");
-               goto error;     /* join error, exit without cleanup */
-       }
+exit_init_data:
+       /*
+        * sessiond_cleanup() is called when no other thread is running, except
+        * the ht_cleanup thread, which is needed to destroy the hash tables.
+        */
+       rcu_thread_online();
+       sessiond_cleanup();
+       rcu_thread_offline();
+       rcu_unregister_thread();
 
-exit_client:
-       ret = pthread_join(health_thread, &status);
-       if (ret != 0) {
-               PERROR("pthread_join health thread");
-               goto error;     /* join error, exit without cleanup */
+       ret = notify_thread_pipe(ht_cleanup_quit_pipe[1]);
+       if (ret < 0) {
+               ERR("write error on ht_cleanup quit pipe");
+               retval = -1;
        }
 
-exit_health:
        ret = pthread_join(ht_cleanup_thread, &status);
-       if (ret != 0) {
+       if (ret) {
+               errno = ret;
                PERROR("pthread_join ht cleanup thread");
-               goto error;     /* join error, exit without cleanup */
+               retval = -1;
        }
 exit_ht_cleanup:
-       health_app_destroy(health_sessiond);
-exit_health_sessiond_cleanup:
-exit:
+
+       utils_close_pipe(ht_cleanup_pipe);
+exit_ht_cleanup_pipe:
+
        /*
-        * cleanup() is called when no other thread is running.
+        * Close the ht_cleanup quit pipe.
         */
-       rcu_thread_online();
-       cleanup();
-       rcu_thread_offline();
-       rcu_unregister_thread();
-       if (!ret) {
+       utils_close_pipe(ht_cleanup_quit_pipe);
+exit_ht_cleanup_quit_pipe:
+
+       health_app_destroy(health_sessiond);
+exit_health_sessiond_cleanup:
+
+exit_options:
+       sessiond_cleanup_options();
+
+exit_set_signal_handler:
+       if (!retval) {
                exit(EXIT_SUCCESS);
+       } else {
+               exit(EXIT_FAILURE);
        }
-error:
-       exit(EXIT_FAILURE);
 }
--

2.1.1

Signature · 8 lines
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): CA+jJMxvjRY0E0v2=PK9ZDurKB3Yjob=LCaB1yo6PpezX-E40Xg@mail.gmail.com

#17 lore raw

Re: [PATCH lttng-tools 11/13] Refactor sessiond main/cleanup/ht-cleanup

----- Original Message -----

Quoted 13 lines

From: "Jérémie Galarneau" <[email protected]>
To: "Mathieu Desnoyers" <[email protected]>
Cc: "Jeremie Galarneau" <[email protected]>, [email protected]
Sent: Tuesday, January 6, 2015 3:59:53 PM
Subject: Re: [PATCH lttng-tools 11/13] Refactor sessiond main/cleanup/ht-cleanup

Quoted 7 lines
src/bin/lttng-sessiond/ht-cleanup.c +0 -0
index 0469f92..79fb9cd 100644
@@ -47,7 +47,7 @@ void *thread_ht_cleanup(void *data)
 
Quoted 8 lines

src/bin/lttng-sessiond/ht-cleanup.c void *thread_ht_cleanup(void *data) · line 50

Quoted 4 lines
src/bin/lttng-sessiond/ht-cleanup.c +1 -1
        health_code_update();
 
-       ret = sessiond_set_thread_pollset(&events, 2);
+       ret = sessiond_set_ht_cleanup_thread_pollset(&events, 2);

Won't this result in adding the ht_cleanup_quit_pipe twice to the pollset? (see line 56 in lttng-tools/src/bin/lttng-sessiond/ht-cleanup.c)

No. ht_cleanup_quit_pipe and ht_cleanup_pipe are two different pipes.

[...]

Quoted 5 lines

src/bin/lttng-sessiond/ht-cleanup.c void *thread_ht_cleanup(void *data) · line 51

Quoted 3 lines
src/bin/lttng-sessiond/ht-cleanup.c +1 -1
-       DBG("Cleaning up");
+       DBG("Cleanup up sessiond");

Slight typo here. I'll fix it on my end.

ok

Thanks!

Mathieu

Signature · 8 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): [email protected]

#19 lore raw

Re: [PATCH lttng-tools 11/13] Refactor sessiond main/cleanup/ht-cleanup

Quoted 23 lines · Mathieu Desnoyers

----- Original Message -----

Quoted 13 lines

From: "Jérémie Galarneau" <[email protected]>
To: "Mathieu Desnoyers" <[email protected]>
Cc: "Jeremie Galarneau" <[email protected]>, [email protected]
Sent: Tuesday, January 6, 2015 3:59:53 PM
Subject: Re: [PATCH lttng-tools 11/13] Refactor sessiond main/cleanup/ht-cleanup

Quoted 7 lines
src/bin/lttng-sessiond/ht-cleanup.c +0 -0
index 0469f92..79fb9cd 100644
@@ -47,7 +47,7 @@ void *thread_ht_cleanup(void *data)
 
Quoted 8 lines

src/bin/lttng-sessiond/ht-cleanup.c void *thread_ht_cleanup(void *data) · line 50

Quoted 4 lines
src/bin/lttng-sessiond/ht-cleanup.c +1 -1
        health_code_update();
 
-       ret = sessiond_set_thread_pollset(&events, 2);
+       ret = sessiond_set_ht_cleanup_thread_pollset(&events, 2);

Won't this result in adding the ht_cleanup_quit_pipe twice to the pollset? (see line 56 in lttng-tools/src/bin/lttng-sessiond/ht-cleanup.c)

Quoted 3 lines

No. ht_cleanup_quit_pipe and ht_cleanup_pipe are two different pipes.

Right! Thanks for the clarification.

Jérémie

Quoted 18 lines

[...]

Quoted 5 lines

src/bin/lttng-sessiond/ht-cleanup.c

Quoted 3 lines
src/bin/lttng-sessiond/ht-cleanup.c +1 -1
-       DBG("Cleaning up");
+       DBG("Cleanup up sessiond");

Slight typo here. I'll fix it on my end.

ok

Thanks!

Mathieu

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

Signature · 8 lines
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
#20 lore raw

Re: [PATCH lttng-tools 00/13] lttng-tools leak fixes

In reply to a message not in the archive

All merged, thanks!

Jérémie

Quoted 41 lines · Mathieu Desnoyers

This patchset fixes various leaks. The main reason for refactoring the daemon main functions is to make it possible to use valgrind to spot other leaks without being incumbered by process teardown leaks.

Mathieu Desnoyers (13):
Fix: agents ht leaks on destroy session
Fix: leak on UST buffer registry creation error
Fix: ust app leak on UST buffer creation error
Missing error handling: ust_app_ht_alloc should return error status Missing error handling: consumerd parse_args should return errors to
caller
Misleading error handling: utils_create_pid_file() should return 0 on
success
Cleanup: lttng-consumerd: remove stale TODO comment
Cleanup: consumerd: use PERROR rather than perror
Cleanup: consumerd: lines over 80 columns
Missing error handling: consumer_signal_init should return its error
status
Refactor sessiond main/cleanup/ht-cleanup
Refactor consumerd main/cleanup
Refactor relayd main/set_options/cleanup

 src/bin/lttng-consumerd/lttng-consumerd.c | 286 +++++++----
 src/bin/lttng-relayd/live.c               | 127 +++--
 src/bin/lttng-relayd/live.h               |   5 +-
 src/bin/lttng-relayd/main.c               | 207 ++++----
 src/bin/lttng-sessiond/ht-cleanup.c       |  41 +-
 src/bin/lttng-sessiond/lttng-sessiond.h   |   4 +
 src/bin/lttng-sessiond/main.c             | 781 +++++++++++++++++++-----------
 src/bin/lttng-sessiond/trace-ust.c        |   2 +
 src/bin/lttng-sessiond/ust-app.c          |  35 +-
 src/bin/lttng-sessiond/ust-app.h          |   2 +-
 src/bin/lttng-sessiond/ust-registry.c     |   4 +-
 src/common/consumer-timer.c               |   4 +-
 src/common/consumer-timer.h               |   2 +-
 src/common/utils.c                        |   6 +-
 14 files changed, 963 insertions(+), 543 deletions(-)

--
2.1.1

Signature · 8 lines
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com

_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev