kconsumerd: signals should only set flag and write into pipe (part 1)
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 14 Jul 2011 22:02:13 +0000 (18:02 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 14 Jul 2011 22:02:13 +0000 (18:02 -0400)
Simplify signal reception. Left todo.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttkconsumerd/liblttkconsumerd.c
liblttkconsumerd/liblttkconsumerd.h
ltt-kconsumerd/ltt-kconsumerd.c

index c1bad734dd4bbe1d616d777fb6c9e773e3885783..de942a1983182df755938bfe1b840c3262a22215 100644 (file)
@@ -69,6 +69,12 @@ static int kconsumerd_thread_pipe[2];
 /* pipe to wake the poll thread when necessary */
 static int kconsumerd_poll_pipe[2];
 
+/*
+ * TODO: create a should_quit pipe to let the signal handler wake up the
+ * fd receiver thread. It should be initialized before any signal can be
+ * received by the library.
+ */
+
 /* timeout parameter, to control the polling thread grace period */
 static int kconsumerd_poll_timeout = -1;
 
@@ -78,8 +84,13 @@ static int kconsumerd_error_socket;
 /* socket to exchange commands with sessiond */
 static char *kconsumerd_command_sock_path;
 
-/* flag to inform the polling thread to kconsumerd_quit when all fd hung up */
-static int kconsumerd_quit = 0;
+/*
+ * flag to inform the polling thread to quit when all fd hung up.
+ * Updated by the kconsumerd_thread_receive_fds when it notices that all
+ * fds has hung up. Also updated by the signal handler
+ * (kconsumerd_should_exit()). Read by the polling threads.
+ */
+static volatile int kconsumerd_quit = 0;
 
 /*
  * kconsumerd_set_error_socket
@@ -790,7 +801,6 @@ end:
                free(local_kconsumerd_fd);
                local_kconsumerd_fd = NULL;
        }
-       kconsumerd_cleanup();
        return NULL;
 }
 
@@ -835,6 +845,8 @@ void *kconsumerd_thread_receive_fds(void *data)
                goto end;
        }
 
+       /* TODO: poll on socket and "should_quit" fd pipe */
+       /* TODO: change blocking call into non-blocking call */
        /* Blocking call, waiting for transmission */
        sock = lttcomm_accept_unix_sock(client_socket);
        if (sock <= 0) {
@@ -843,6 +855,8 @@ void *kconsumerd_thread_receive_fds(void *data)
        }
        while (1) {
                /* We first get the number of fd we are about to receive */
+               /* TODO: poll on sock and "should_quit" fd pipe */
+               /* TODO: change recv into a non-blocking call */
                ret = lttcomm_recv_unix_sock(sock, &tmp,
                                sizeof(struct lttcomm_kconsumerd_header));
                if (ret <= 0) {
@@ -853,6 +867,10 @@ void *kconsumerd_thread_receive_fds(void *data)
                        DBG("Received STOP command");
                        goto end;
                }
+               if (kconsumerd_quit) {
+                       DBG("kconsumerd_thread_receive_fds received quit from signal");
+                       goto end;
+               }
                /* we received a command to add or update fds */
                ret = kconsumerd_consumerd_recv_fd(sock, tmp.payload_size, tmp.cmd_type);
                if (ret <= 0) {
@@ -890,19 +908,35 @@ end:
  *
  *  Cleanup the daemon's socket on exit
  */
-void kconsumerd_cleanup()
+void kconsumerd_cleanup(void)
 {
        struct kconsumerd_fd *iter;
 
        /* remove the socket file */
        unlink(kconsumerd_command_sock_path);
 
-       /* close all outfd */
+       /*
+        * close all outfd. Called when there are no more threads
+        * running (after joining on the threads), no need to protect
+        * list iteration with mutex.
+        */
        cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
                kconsumerd_del_fd(iter);
        }
 }
 
+/*
+ * Called from signal handler.
+ */
+void kconsumerd_should_exit(void)
+{
+       kconsumerd_quit = 1;
+       /*
+        * TODO: write into a should_quit pipe to wake up the fd
+        * receiver thread.
+        */
+}
+
 /*
  * kconsumerd_send_error
  *
index 8dc6b6270eff123d02da6d3f6b63a2290eb49801..83c769acf88c1633deb182190c60b7a15046a442 100644 (file)
@@ -61,7 +61,8 @@ int kconsumerd_create_poll_pipe();
 int kconsumerd_send_error(enum lttcomm_return_code cmd);
 void *kconsumerd_thread_poll_fds(void *data);
 void *kconsumerd_thread_receive_fds(void *data);
-void kconsumerd_cleanup();
+void kconsumerd_should_exit(void);
+void kconsumerd_cleanup(void);
 void kconsumerd_set_error_socket(int sock);
 void kconsumerd_set_command_socket_path(char *sock);
 
index 60134e831491e631f15766edfc51985c4c7d943f..b5e854179a4fd8f2a486700338dced2be5a63cdf 100644 (file)
@@ -67,7 +67,7 @@ static void sighandler(int sig)
                return;
        }
 
-       kconsumerd_cleanup();
+       kconsumerd_should_exit();
 }
 
 /*
This page took 0.02864 seconds and 4 git commands to generate.