+ int i, ret, current_nb_fd;
+ unsigned int nb_fd = 2;
+ int update_poll_flag = 1;
+ struct pollfd *pollfd = NULL;
+ struct ust_command ust_cmd;
+
+ DBG("[thread] Manage application started");
+
+ ust_cmd.sock = -1;
+ current_nb_fd = nb_fd;
+
+ while (1) {
+ /* See if we have a valid socket to add to pollfd */
+ if (ust_cmd.sock != -1) {
+ nb_fd++;
+ update_poll_flag = 1;
+ }
+
+ /* The pollfd struct must be updated */
+ if (update_poll_flag) {
+ ret = update_apps_cmd_pollfd(nb_fd, current_nb_fd, &pollfd);
+ if (ret < 0) {
+ /* malloc failed so we quit */
+ goto error;
+ }
+
+ if (ust_cmd.sock != -1) {
+ /* Update pollfd with the new UST socket */
+ DBG("Adding sock %d to apps cmd pollfd", ust_cmd.sock);
+ pollfd[nb_fd - 1].fd = ust_cmd.sock;
+ pollfd[nb_fd - 1].events = POLLHUP | POLLNVAL | POLLERR;
+ ust_cmd.sock = -1;
+ }
+ }
+
+ DBG("Apps thread polling on %d fds", nb_fd);
+
+ /* Inifinite blocking call, waiting for transmission */
+ ret = poll(pollfd, nb_fd, -1);
+ if (ret < 0) {
+ perror("poll apps thread");
+ goto error;
+ }
+
+ /* Thread quit pipe has been closed. Killing thread. */
+ if (pollfd[0].revents == POLLNVAL) {
+ goto error;
+ } else {
+ /* apps_cmd_pipe pipe events */
+ switch (pollfd[1].revents) {
+ case POLLERR:
+ ERR("Apps command pipe poll error");
+ goto error;
+ case POLLIN:
+ /* 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");
+ goto error;
+ }
+
+ /* Register applicaton to the session daemon */
+ ret = register_traceable_app(&ust_cmd.reg_msg, ust_cmd.sock);
+ if (ret < 0) {
+ /* Only critical ENOMEM error can be returned here */
+ goto error;
+ }
+
+ ret = ustctl_register_done(ust_cmd.sock);
+ if (ret < 0) {
+ /*
+ * If the registration is not possible, we simply unregister
+ * the apps and continue
+ */
+ unregister_traceable_app(ust_cmd.sock);
+ }
+ break;
+ }
+ }
+
+ current_nb_fd = nb_fd;
+ for (i = 2; i < current_nb_fd; i++) {
+ /* Apps socket is closed/hungup */
+ switch (pollfd[i].revents) {
+ case POLLERR:
+ case POLLHUP:
+ case POLLNVAL:
+ /* Pipe closed */
+ unregister_traceable_app(pollfd[i].fd);
+ /* Indicate to remove this fd from the pollfd */
+ pollfd[i].fd = -1;
+ nb_fd--;
+ break;
+ }
+ }
+
+ if (nb_fd != current_nb_fd) {
+ update_poll_flag = 1;
+ }
+ }
+
+error:
+ DBG("Application communication apps dying");
+ close(apps_cmd_pipe[0]);
+ close(apps_cmd_pipe[1]);
+
+ free(pollfd);
+
+ return NULL;
+}
+
+/*
+ * Dispatch request from the registration threads to the application
+ * communication thread.
+ */
+static void *thread_dispatch_ust_registration(void *data)
+{
+ int ret;
+ struct cds_wfq_node *node;
+ struct ust_command *ust_cmd = NULL;
+
+ DBG("[thread] Dispatch UST command started");
+
+ while (!dispatch_thread_exit) {
+ /* Atomically prepare the queue futex */
+ futex_nto1_prepare(&ust_cmd_queue.futex);
+
+ do {
+ /* Dequeue command for registration */
+ node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
+ if (node == NULL) {
+ DBG("Waked up but nothing in the UST command queue");
+ /* Continue thread execution */
+ break;
+ }
+
+ ust_cmd = caa_container_of(node, struct ust_command, node);
+
+ DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
+ " gid:%d sock:%d name:%s (version %d.%d)",
+ ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
+ ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
+ ust_cmd->sock, ust_cmd->reg_msg.name,
+ ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
+ /*
+ * Inform apps thread of the new application registration. This
+ * call is blocking so we can be assured that the data will be read
+ * at some point in time or wait to the end of the world :)
+ */
+ ret = write(apps_cmd_pipe[1], ust_cmd,
+ sizeof(struct ust_command));
+ if (ret < 0) {
+ perror("write apps cmd pipe");
+ if (errno == EBADF) {
+ /*
+ * We can't inform the application thread to process
+ * registration. We will exit or else application
+ * registration will not occur and tracing will never
+ * start.
+ */
+ goto error;
+ }
+ }
+ free(ust_cmd);
+ } while (node != NULL);
+
+ /* Futex wait on queue. Blocking call on futex() */
+ futex_nto1_wait(&ust_cmd_queue.futex);
+ }
+
+error:
+ DBG("Dispatch thread dying");
+ return NULL;
+}