+ if (sock_info->socket != -1) {
+ ret = close(sock_info->socket);
+ if (ret) {
+ ERR("Error closing local apps socket");
+ }
+ sock_info->socket = -1;
+ }
+ if (sock_info->root_handle != -1) {
+ ret = objd_unref(sock_info->root_handle);
+ if (ret) {
+ ERR("Error unref root handle");
+ }
+ sock_info->root_handle = -1;
+ }
+}
+
+/*
+ * This thread does not allocate any resource, except within
+ * handle_message, within mutex protection. This mutex protects against
+ * fork and exit.
+ * The other moment it allocates resources is at socket connexion, which
+ * is also protected by the mutex.
+ */
+static
+void *ust_listener_thread(void *arg)
+{
+ struct sock_info *sock_info = arg;
+ int sock, ret;
+
+ /* Restart trying to connect to the session daemon */
+restart:
+ pthread_mutex_lock(<tng_ust_comm_mutex);
+
+ if (lttng_ust_comm_should_quit) {
+ pthread_mutex_unlock(<tng_ust_comm_mutex);
+ goto quit;
+ }
+
+ if (sock_info->socket != -1) {
+ ret = close(sock_info->socket);
+ if (ret) {
+ ERR("Error closing %s apps socket", sock_info->name);
+ }
+ sock_info->socket = -1;
+ }
+
+ /* Check for sessiond availability with pipe TODO */
+
+ /* Register */
+ ret = lttcomm_connect_unix_sock(sock_info->sock_path);
+ if (ret < 0) {
+ ERR("Error connecting to %s apps socket", sock_info->name);
+ /*
+ * If we cannot find the sessiond daemon, don't delay
+ * constructor execution.
+ */
+ ret = handle_register_done();
+ assert(!ret);
+ pthread_mutex_unlock(<tng_ust_comm_mutex);
+ sleep(5);
+ goto restart;
+ }
+
+ sock_info->socket = sock = ret;
+
+ /*
+ * Create only one root handle per listener thread for the whole
+ * process lifetime.
+ */
+ if (sock_info->root_handle == -1) {
+ ret = lttng_abi_create_root_handle();
+ if (ret) {
+ ERR("Error creating root handle");
+ pthread_mutex_unlock(<tng_ust_comm_mutex);
+ goto quit;
+ }
+ sock_info->root_handle = ret;
+ }
+
+ ret = register_app_to_sessiond(sock);
+ if (ret < 0) {
+ ERR("Error registering to %s apps socket", sock_info->name);
+ /*
+ * If we cannot register to the sessiond daemon, don't
+ * delay constructor execution.
+ */
+ ret = handle_register_done();
+ assert(!ret);
+ pthread_mutex_unlock(<tng_ust_comm_mutex);
+ sleep(5);
+ goto restart;
+ }
+ pthread_mutex_unlock(<tng_ust_comm_mutex);
+