diff recpt1/recpt1.c @ 39:b03685d25fee

recpt1 now shows error explanation when write() fails.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Fri, 17 Apr 2009 22:52:35 +0900
parents 3228f5cde349
children a35e352f6c4a
line wrap: on
line diff
--- a/recpt1/recpt1.c	Thu Apr 16 22:18:58 2009 +0900
+++ b/recpt1/recpt1.c	Fri Apr 17 22:52:35 2009 +0900
@@ -208,6 +208,7 @@
 
     while(1) {
         ssize_t wc = 0;
+        int file_err = 0;
         qbuf = dequeue(p_queue);
         /* no entry in the queue */
         if(qbuf == NULL) {
@@ -235,10 +236,10 @@
                 int ws = size_remain < SIZE_CHANK ? size_remain : SIZE_CHANK;
                 wc = write(wfd, buf.data + offset, ws);
                 if(wc < 0) {
-                    if(errno == EPIPE)
-                        pthread_kill(signal_thread, SIGPIPE);
-                    else
-                        pthread_kill(signal_thread, SIGUSR2);
+                    perror("write");
+                    file_err = 1;
+                    pthread_kill(signal_thread,
+                                 errno == EPIPE ? SIGPIPE : SIGUSR2);
                     break;
                 }
                 size_remain -= wc;
@@ -266,7 +267,7 @@
         free(qbuf);
 
         /* normal exit */
-        if((f_exit) && (!p_queue->num_used)) {
+        if(f_exit && !p_queue->num_used && !file_err) {
 
             buf = sbuf; /* default */
 
@@ -281,10 +282,10 @@
             if(!fileless) {
                 wc = write(wfd, buf.data, buf.size);
                 if(wc < 0) {
-                    if(errno == EPIPE)
-                        pthread_kill(signal_thread, SIGPIPE);
-                    else
-                        pthread_kill(signal_thread, SIGUSR2);
+                    perror("write");
+                    file_err = 1;
+                    pthread_kill(signal_thread,
+                                 errno == EPIPE ? SIGPIPE : SIGUSR2);
                 }
             }
 
@@ -476,7 +477,7 @@
         cleanup(sdata);
         break;
     case SIGUSR2: /* error */
-        fprintf(stderr, "\nSomething is wrong. cleaning up...\n");
+        fprintf(stderr, "Detected an error. cleaning up...\n");
         cleanup(sdata);
         break;
     }