Cygwin: Fix fd hangup in thread_manage_apps of sessiond
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
index 03fbff63324b38fc31c34d8c75e126b77a9835dc..7225495547b2276d0df20dfaac64f415fb434b54 100644 (file)
@@ -1287,16 +1287,27 @@ static void *thread_manage_apps(void *data)
                                 * At this point, we know that a registered application made
                                 * the event at poll_wait.
                                 */
-                               if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
-                                       /* Removing from the poll set */
-                                       ret = lttng_poll_del(&events, pollfd);
-                                       if (ret < 0) {
-                                               goto error;
-                                       }
 
-                                       /* Socket closed on remote end. */
-                                       ust_app_unregister(pollfd);
-                                       break;
+                               if (revents & (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+                                       ssize_t readlen;
+                                       char dummy;
+
+                                       do {
+                                               readlen = read(pollfd, &dummy, 1);
+                                       } while (readlen == -1 && errno == EINTR);
+
+                                       if (readlen == 0) {
+                                               /* Removing from the poll set */
+                                               ret = lttng_poll_del(&events, pollfd);
+                                               if (ret < 0) {
+                                                       goto error;
+                                               }
+
+                                               /* Socket closed on remote end. */
+                                               ust_app_unregister(pollfd);
+                                               break;
+
+                                       }
                                }
                        }
                }
@@ -3226,6 +3237,9 @@ static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
                        nb_chan = session->kernel_session->channel_count;
                }
                DBG3("Number of kernel channels %zd", nb_chan);
+               if (nb_chan <= 0) {
+                       ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
+               }
                break;
        case LTTNG_DOMAIN_UST:
                if (session->ust_session != NULL) {
@@ -3233,6 +3247,9 @@ static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
                                        session->ust_session->domain_global.channels);
                }
                DBG3("Number of UST global channels %zd", nb_chan);
+               if (nb_chan <= 0) {
+                       ret = LTTCOMM_UST_CHAN_NOT_FOUND;
+               }
                break;
        default:
                *channels = NULL;
@@ -3250,6 +3267,8 @@ static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
                list_lttng_channels(domain, session, *channels);
        } else {
                *channels = NULL;
+               /* Ret value was set in the domain switch case */
+               goto error;
        }
 
        return nb_chan;
@@ -4485,11 +4504,6 @@ int main(int argc, char **argv)
 
        rcu_register_thread();
 
-       /* Create thread quit pipe */
-       if ((ret = init_thread_quit_pipe()) < 0) {
-               goto error;
-       }
-
        setup_consumerd_path();
 
        /* Parse arguments */
@@ -4500,11 +4514,31 @@ int main(int argc, char **argv)
 
        /* Daemonize */
        if (opt_daemon) {
+               int i;
+
+               /*
+                * fork
+                * child: setsid, close FD 0, 1, 2, chdir /
+                * parent: exit (if fork is successful)
+                */
                ret = daemon(0, 0);
                if (ret < 0) {
                        PERROR("daemon");
                        goto error;
                }
+               /*
+                * 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.
+                */
+               for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
+                       (void) close(i);
+               }
+       }
+
+       /* Create thread quit pipe */
+       if ((ret = init_thread_quit_pipe()) < 0) {
+               goto error;
        }
 
        /* Check if daemon is UID = 0 */
This page took 0.026149 seconds and 4 git commands to generate.