[FIX] broken pipe or unclosed pipe on client extraordinary disconnect
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Wed, 4 Sep 2013 08:58:37 +0000 (12:58 +0400)
committerVitaliy Cherepanov <v.cherepanov@samsung.com>
Wed, 4 Sep 2013 08:58:37 +0000 (12:58 +0400)
 - add cleanup for transfer_thread which close pipes
on thread terminate or exit from thread function on some internal
errors

Change-Id: I99074ae909bbb315eca2ce3bfcded50603345103
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
daemon/main.c
daemon/transfer_thread.c

index 40594bb..211be81 100644 (file)
@@ -387,6 +387,7 @@ static void setup_signals()
        sigaction(SIGINT, &sigact, 0);
 
        signal(SIGHUP, SIG_IGN);
+       signal(SIGPIPE,SIG_IGN);
 }
 
 // main function
index 197c8b5..5e8dea7 100644 (file)
 
 #define BUF_SIZE 4096
 
+static void transfer_thread_cleanup(void *arg)
+{
+       int *fd_pipe;
+       fd_pipe = (int *) arg;
+
+       close(fd_pipe[0]);
+       close(fd_pipe[1]);
+
+}
+
 static void *transfer_thread(void *arg)
 {
        (void)arg;
        int fd_pipe[2];
        ssize_t nrd, nwr;
 
-       LOGI("transfer thread started\n");
+       //init thread
+       pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
 
+       //init pipe
        pipe(fd_pipe);
+       //set cleanup function
+       pthread_cleanup_push(transfer_thread_cleanup, (void *)fd_pipe);
+
+       pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+
+       LOGI("transfer thread started\n");
+
        while (1) {
                nrd = splice(manager.buf_fd, NULL,
                             fd_pipe[1], NULL,
@@ -64,6 +83,7 @@ static void *transfer_thread(void *arg)
                nwr = splice(fd_pipe[0], NULL,
                             manager.host.data_socket, NULL,
                             nrd, 0);
+
                if (nwr == -1) {
                        LOGE("Cannot splice write: %s\n", strerror(errno));
                        goto thread_exit;
@@ -74,11 +94,9 @@ static void *transfer_thread(void *arg)
        }
 
        thread_exit:
+       pthread_cleanup_pop(1);
 
-       close(fd_pipe[0]);
-       close(fd_pipe[1]);
-
-       LOGI("transfer thread finished\n");
+       LOGI("transfer thread finished. return\n");
 
        return NULL;
 }