diff recpt1/recpt1.c @ 24:f2cce7becf52

fixed a bug that recpt1 could not exit when recsec is specified.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Fri, 27 Feb 2009 22:17:39 +0900
parents b63f5c100e5a
children 011cb9337729
line wrap: on
line diff
--- a/recpt1/recpt1.c	Thu Feb 26 16:14:38 2009 +0900
+++ b/recpt1/recpt1.c	Fri Feb 27 22:17:39 2009 +0900
@@ -271,8 +271,11 @@
                     break;
                 }
                 wc = write(wfd, dbuf.data, dbuf.size);
-                if(wc <= 0 && errno == EPIPE) {
-                    pthread_kill(signal_thread, SIGPIPE);
+                if(wc < 0) {
+                    if(errno == EPIPE)
+                        pthread_kill(signal_thread, SIGPIPE);
+                    else
+                        pthread_kill(signal_thread, SIGUSR2);
                 }
 
                 if(use_udp && sfd != -1) {
@@ -283,8 +286,11 @@
             }
             else {
                 wc = write(wfd, sbuf.data, sbuf.size);
-                if(wc <= 0 && errno == EPIPE) {
-                    pthread_kill(signal_thread, SIGPIPE);
+                if(wc < 0) {
+                    if(errno == EPIPE)
+                        pthread_kill(signal_thread, SIGPIPE);
+                    else
+                        pthread_kill(signal_thread, SIGUSR2);
                 }
 
                 if(use_udp && sfd != -1) {
@@ -303,8 +309,11 @@
                         break;
                     }
                     wc = write(wfd, dbuf.data, dbuf.size);
-                    if(wc <= 0 && errno == EPIPE) {
-                        pthread_kill(signal_thread, SIGPIPE);
+                    if(wc < 0) {
+                        if(errno == EPIPE)
+                            pthread_kill(signal_thread, SIGPIPE);
+                        else
+                            pthread_kill(signal_thread, SIGUSR2);
                     }
 
                     if(use_udp && sfd != -1) {
@@ -470,6 +479,8 @@
     sigaddset(&waitset, SIGPIPE);
     sigaddset(&waitset, SIGINT);
     sigaddset(&waitset, SIGTERM);
+    sigaddset(&waitset, SIGUSR1);
+    sigaddset(&waitset, SIGUSR2);
 
     sigwait(&waitset, &sig);
 
@@ -486,6 +497,13 @@
         fprintf(stderr, "\nSIGTERM received. cleaning up...\n");
         cleanup(sdata);
         break;
+    case SIGUSR1: /* normal exit*/
+        cleanup(sdata);
+        break;
+    case SIGUSR2: /* error */
+        fprintf(stderr, "\nSomething is wrong. cleaning up...\n");
+        cleanup(sdata);
+        break;
     }
 
     return NULL; /* dummy */
@@ -500,6 +518,8 @@
     sigaddset(&blockset, SIGPIPE);
     sigaddset(&blockset, SIGINT);
     sigaddset(&blockset, SIGTERM);
+    sigaddset(&blockset, SIGUSR1);
+    sigaddset(&blockset, SIGUSR2);
 
     if(pthread_sigmask(SIG_BLOCK, &blockset, NULL))
         fprintf(stderr, "pthread_sigmask() failed.\n");
@@ -517,7 +537,7 @@
     time_t start_time, cur_time;
     FREQUENCY freq;
     ISDB_T_FREQ_CONV_TABLE *ptr;
-    pthread_t dequeue_thread;
+    pthread_t reader_thread;
     pthread_t signal_thread;
     QUEUE_T *p_queue = create_queue(MAX_QUEUE);
     BUFSZ   *bufptr;
@@ -728,7 +748,7 @@
     tdata.wfd = wfd;
     tdata.sock_data = sockdata;
     tdata.signal_thread = signal_thread;
-    pthread_create(&dequeue_thread, NULL, reader_func, &tdata);
+    pthread_create(&reader_thread, NULL, reader_func, &tdata);
 
     /* start recording */
     if(ioctl(tfd, START_REC, 0) < 0) {
@@ -777,6 +797,12 @@
         }
     }
 
+    pthread_kill(signal_thread, SIGUSR1);
+
+    /* wait for threads */
+    pthread_join(reader_thread, NULL);
+    pthread_join(signal_thread, NULL);
+
     /* close tuner */
     if(ptr->type == CHTYPE_SATELLITE) {
         if(ioctl(tfd, LNB_DISABLE, 0) < 0) {
@@ -785,10 +811,6 @@
     }
     close(tfd);
 
-    /* wait for threads */
-    pthread_join(dequeue_thread, NULL);
-    pthread_join(signal_thread, NULL);
-
     /* release queue */
     destroy_queue(p_queue);