comparison 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
comparison
equal deleted inserted replaced
23:f80731a89036 24:f2cce7becf52
269 if(code < 0) { 269 if(code < 0) {
270 fprintf(stderr, "b25_decode failed\n"); 270 fprintf(stderr, "b25_decode failed\n");
271 break; 271 break;
272 } 272 }
273 wc = write(wfd, dbuf.data, dbuf.size); 273 wc = write(wfd, dbuf.data, dbuf.size);
274 if(wc <= 0 && errno == EPIPE) { 274 if(wc < 0) {
275 pthread_kill(signal_thread, SIGPIPE); 275 if(errno == EPIPE)
276 pthread_kill(signal_thread, SIGPIPE);
277 else
278 pthread_kill(signal_thread, SIGUSR2);
276 } 279 }
277 280
278 if(use_udp && sfd != -1) { 281 if(use_udp && sfd != -1) {
279 sendto(sfd, dbuf.data, dbuf.size, 0, 282 sendto(sfd, dbuf.data, dbuf.size, 0,
280 addr, sizeof(struct sockaddr_in)); 283 addr, sizeof(struct sockaddr_in));
281 } 284 }
282 free(buf); 285 free(buf);
283 } 286 }
284 else { 287 else {
285 wc = write(wfd, sbuf.data, sbuf.size); 288 wc = write(wfd, sbuf.data, sbuf.size);
286 if(wc <= 0 && errno == EPIPE) { 289 if(wc < 0) {
287 pthread_kill(signal_thread, SIGPIPE); 290 if(errno == EPIPE)
291 pthread_kill(signal_thread, SIGPIPE);
292 else
293 pthread_kill(signal_thread, SIGUSR2);
288 } 294 }
289 295
290 if(use_udp && sfd != -1) { 296 if(use_udp && sfd != -1) {
291 sendto(sfd, sbuf.data, sbuf.size, 0, 297 sendto(sfd, sbuf.data, sbuf.size, 0,
292 addr, sizeof(struct sockaddr_in)); 298 addr, sizeof(struct sockaddr_in));
301 if(code < 0) { 307 if(code < 0) {
302 fprintf(stderr, "b25_finish failed\n"); 308 fprintf(stderr, "b25_finish failed\n");
303 break; 309 break;
304 } 310 }
305 wc = write(wfd, dbuf.data, dbuf.size); 311 wc = write(wfd, dbuf.data, dbuf.size);
306 if(wc <= 0 && errno == EPIPE) { 312 if(wc < 0) {
307 pthread_kill(signal_thread, SIGPIPE); 313 if(errno == EPIPE)
314 pthread_kill(signal_thread, SIGPIPE);
315 else
316 pthread_kill(signal_thread, SIGUSR2);
308 } 317 }
309 318
310 if(use_udp && sfd != -1) { 319 if(use_udp && sfd != -1) {
311 sendto(sfd, dbuf.data, dbuf.size, 0, 320 sendto(sfd, dbuf.data, dbuf.size, 0,
312 addr, sizeof(struct sockaddr_in)); 321 addr, sizeof(struct sockaddr_in));
468 477
469 sigemptyset(&waitset); 478 sigemptyset(&waitset);
470 sigaddset(&waitset, SIGPIPE); 479 sigaddset(&waitset, SIGPIPE);
471 sigaddset(&waitset, SIGINT); 480 sigaddset(&waitset, SIGINT);
472 sigaddset(&waitset, SIGTERM); 481 sigaddset(&waitset, SIGTERM);
482 sigaddset(&waitset, SIGUSR1);
483 sigaddset(&waitset, SIGUSR2);
473 484
474 sigwait(&waitset, &sig); 485 sigwait(&waitset, &sig);
475 486
476 switch(sig) { 487 switch(sig) {
477 case SIGPIPE: 488 case SIGPIPE:
484 break; 495 break;
485 case SIGTERM: 496 case SIGTERM:
486 fprintf(stderr, "\nSIGTERM received. cleaning up...\n"); 497 fprintf(stderr, "\nSIGTERM received. cleaning up...\n");
487 cleanup(sdata); 498 cleanup(sdata);
488 break; 499 break;
500 case SIGUSR1: /* normal exit*/
501 cleanup(sdata);
502 break;
503 case SIGUSR2: /* error */
504 fprintf(stderr, "\nSomething is wrong. cleaning up...\n");
505 cleanup(sdata);
506 break;
489 } 507 }
490 508
491 return NULL; /* dummy */ 509 return NULL; /* dummy */
492 } 510 }
493 511
498 516
499 sigemptyset(&blockset); 517 sigemptyset(&blockset);
500 sigaddset(&blockset, SIGPIPE); 518 sigaddset(&blockset, SIGPIPE);
501 sigaddset(&blockset, SIGINT); 519 sigaddset(&blockset, SIGINT);
502 sigaddset(&blockset, SIGTERM); 520 sigaddset(&blockset, SIGTERM);
521 sigaddset(&blockset, SIGUSR1);
522 sigaddset(&blockset, SIGUSR2);
503 523
504 if(pthread_sigmask(SIG_BLOCK, &blockset, NULL)) 524 if(pthread_sigmask(SIG_BLOCK, &blockset, NULL))
505 fprintf(stderr, "pthread_sigmask() failed.\n"); 525 fprintf(stderr, "pthread_sigmask() failed.\n");
506 526
507 pthread_create(signal_thread, NULL, process_signals, sdata); 527 pthread_create(signal_thread, NULL, process_signals, sdata);
515 int recsec; 535 int recsec;
516 int indefinite = FALSE; 536 int indefinite = FALSE;
517 time_t start_time, cur_time; 537 time_t start_time, cur_time;
518 FREQUENCY freq; 538 FREQUENCY freq;
519 ISDB_T_FREQ_CONV_TABLE *ptr; 539 ISDB_T_FREQ_CONV_TABLE *ptr;
520 pthread_t dequeue_thread; 540 pthread_t reader_thread;
521 pthread_t signal_thread; 541 pthread_t signal_thread;
522 QUEUE_T *p_queue = create_queue(MAX_QUEUE); 542 QUEUE_T *p_queue = create_queue(MAX_QUEUE);
523 BUFSZ *bufptr; 543 BUFSZ *bufptr;
524 decoder *dec = NULL; 544 decoder *dec = NULL;
525 static reader_thread_data tdata; 545 static reader_thread_data tdata;
726 tdata.queue = p_queue; 746 tdata.queue = p_queue;
727 tdata.decoder = dec; 747 tdata.decoder = dec;
728 tdata.wfd = wfd; 748 tdata.wfd = wfd;
729 tdata.sock_data = sockdata; 749 tdata.sock_data = sockdata;
730 tdata.signal_thread = signal_thread; 750 tdata.signal_thread = signal_thread;
731 pthread_create(&dequeue_thread, NULL, reader_func, &tdata); 751 pthread_create(&reader_thread, NULL, reader_func, &tdata);
732 752
733 /* start recording */ 753 /* start recording */
734 if(ioctl(tfd, START_REC, 0) < 0) { 754 if(ioctl(tfd, START_REC, 0) < 0) {
735 fprintf(stderr, "Tuner could not start recording\n"); 755 fprintf(stderr, "Tuner could not start recording\n");
736 return 1; 756 return 1;
775 } 795 }
776 break; 796 break;
777 } 797 }
778 } 798 }
779 799
800 pthread_kill(signal_thread, SIGUSR1);
801
802 /* wait for threads */
803 pthread_join(reader_thread, NULL);
804 pthread_join(signal_thread, NULL);
805
780 /* close tuner */ 806 /* close tuner */
781 if(ptr->type == CHTYPE_SATELLITE) { 807 if(ptr->type == CHTYPE_SATELLITE) {
782 if(ioctl(tfd, LNB_DISABLE, 0) < 0) { 808 if(ioctl(tfd, LNB_DISABLE, 0) < 0) {
783 return 0 ; 809 return 0 ;
784 } 810 }
785 } 811 }
786 close(tfd); 812 close(tfd);
787 813
788 /* wait for threads */
789 pthread_join(dequeue_thread, NULL);
790 pthread_join(signal_thread, NULL);
791
792 /* release queue */ 814 /* release queue */
793 destroy_queue(p_queue); 815 destroy_queue(p_queue);
794 816
795 /* close output file */ 817 /* close output file */
796 if(!use_stdout) 818 if(!use_stdout)