Diffstat (limited to 'libopie2/opiecore/oprocess.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiecore/oprocess.cpp | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/libopie2/opiecore/oprocess.cpp b/libopie2/opiecore/oprocess.cpp index 0a361a1..6349c83 100644 --- a/libopie2/opiecore/oprocess.cpp +++ b/libopie2/opiecore/oprocess.cpp | |||
@@ -579,214 +579,214 @@ void OProcess::slotSendData( int ) | |||
579 | void OProcess::processHasExited( int state ) | 579 | void OProcess::processHasExited( int state ) |
580 | { | 580 | { |
581 | if ( runs ) | 581 | if ( runs ) |
582 | { | 582 | { |
583 | runs = false; | 583 | runs = false; |
584 | status = state; | 584 | status = state; |
585 | 585 | ||
586 | commClose(); // cleanup communication sockets | 586 | commClose(); // cleanup communication sockets |
587 | 587 | ||
588 | // also emit a signal if the process was run Blocking | 588 | // also emit a signal if the process was run Blocking |
589 | if ( DontCare != run_mode ) | 589 | if ( DontCare != run_mode ) |
590 | { | 590 | { |
591 | emit processExited( this ); | 591 | emit processExited( this ); |
592 | } | 592 | } |
593 | } | 593 | } |
594 | } | 594 | } |
595 | 595 | ||
596 | int OProcess::childOutput( int fdno ) | 596 | int OProcess::childOutput( int fdno ) |
597 | { | 597 | { |
598 | if ( communication & NoRead ) | 598 | if ( communication & NoRead ) |
599 | { | 599 | { |
600 | int len = -1; | 600 | int len = -1; |
601 | emit receivedStdout( fdno, len ); | 601 | emit receivedStdout( fdno, len ); |
602 | errno = 0; // Make sure errno doesn't read "EAGAIN" | 602 | errno = 0; // Make sure errno doesn't read "EAGAIN" |
603 | return len; | 603 | return len; |
604 | } | 604 | } |
605 | else | 605 | else |
606 | { | 606 | { |
607 | char buffer[ 1024 ]; | 607 | char buffer[ 1024 ]; |
608 | int len; | 608 | int len; |
609 | 609 | ||
610 | len = ::read( fdno, buffer, 1024 ); | 610 | len = ::read( fdno, buffer, 1024 ); |
611 | 611 | ||
612 | if ( 0 < len ) | 612 | if ( 0 < len ) |
613 | { | 613 | { |
614 | emit receivedStdout( this, buffer, len ); | 614 | emit receivedStdout( this, buffer, len ); |
615 | } | 615 | } |
616 | return len; | 616 | return len; |
617 | } | 617 | } |
618 | } | 618 | } |
619 | 619 | ||
620 | int OProcess::childError( int fdno ) | 620 | int OProcess::childError( int fdno ) |
621 | { | 621 | { |
622 | char buffer[ 1024 ]; | 622 | char buffer[ 1024 ]; |
623 | int len; | 623 | int len; |
624 | 624 | ||
625 | len = ::read( fdno, buffer, 1024 ); | 625 | len = ::read( fdno, buffer, 1024 ); |
626 | 626 | ||
627 | if ( 0 < len ) | 627 | if ( 0 < len ) |
628 | emit receivedStderr( this, buffer, len ); | 628 | emit receivedStderr( this, buffer, len ); |
629 | return len; | 629 | return len; |
630 | } | 630 | } |
631 | 631 | ||
632 | int OProcess::setupCommunication( Communication comm ) | 632 | int OProcess::setupCommunication( Communication comm ) |
633 | { | 633 | { |
634 | int ok; | 634 | int ok; |
635 | 635 | ||
636 | communication = comm; | 636 | communication = comm; |
637 | 637 | ||
638 | ok = 1; | 638 | ok = 1; |
639 | if ( comm & Stdin ) | 639 | if ( comm & Stdin ) |
640 | ok &= socketpair( AF_UNIX, SOCK_STREAM, 0, in ) >= 0; | 640 | ok &= socketpair( AF_UNIX, SOCK_STREAM, 0, in ) >= 0; |
641 | 641 | ||
642 | if ( comm & Stdout ) | 642 | if ( comm & Stdout ) |
643 | ok &= socketpair( AF_UNIX, SOCK_STREAM, 0, out ) >= 0; | 643 | ok &= socketpair( AF_UNIX, SOCK_STREAM, 0, out ) >= 0; |
644 | 644 | ||
645 | if ( comm & Stderr ) | 645 | if ( comm & Stderr ) |
646 | ok &= socketpair( AF_UNIX, SOCK_STREAM, 0, err ) >= 0; | 646 | ok &= socketpair( AF_UNIX, SOCK_STREAM, 0, err ) >= 0; |
647 | 647 | ||
648 | return ok; | 648 | return ok; |
649 | } | 649 | } |
650 | 650 | ||
651 | int OProcess::commSetupDoneP() | 651 | int OProcess::commSetupDoneP() |
652 | { | 652 | { |
653 | int ok = 1; | 653 | int ok = 1; |
654 | 654 | ||
655 | if ( communication != NoCommunication ) | 655 | if ( communication != NoCommunication ) |
656 | { | 656 | { |
657 | if ( communication & Stdin ) | 657 | if ( communication & Stdin ) |
658 | close( in[ 0 ] ); | 658 | close( in[ 0 ] ); |
659 | if ( communication & Stdout ) | 659 | if ( communication & Stdout ) |
660 | close( out[ 1 ] ); | 660 | close( out[ 1 ] ); |
661 | if ( communication & Stderr ) | 661 | if ( communication & Stderr ) |
662 | close( err[ 1 ] ); | 662 | close( err[ 1 ] ); |
663 | 663 | ||
664 | // Don't create socket notifiers and set the sockets non-blocking if | 664 | // Don't create socket notifiers and set the sockets non-blocking if |
665 | // blocking is requested. | 665 | // blocking is requested. |
666 | if ( run_mode == Block ) | 666 | if ( run_mode == Block ) |
667 | return ok; | 667 | return ok; |
668 | 668 | ||
669 | if ( communication & Stdin ) | 669 | if ( communication & Stdin ) |
670 | { | 670 | { |
671 | // ok &= (-1 != fcntl(in[1], F_SETFL, O_NONBLOCK)); | 671 | // ok &= (-1 != fcntl(in[1], F_SETFL, O_NONBLOCK)); |
672 | innot = new QSocketNotifier( in[ 1 ], QSocketNotifier::Write, this ); | 672 | innot = new QSocketNotifier( in[ 1 ], QSocketNotifier::Write, this ); |
673 | CHECK_PTR( innot ); | 673 | CHECK_PTR( innot ); |
674 | innot->setEnabled( false ); // will be enabled when data has to be sent | 674 | innot->setEnabled( false ); // will be enabled when data has to be sent |
675 | QObject::connect( innot, SIGNAL( activated( int ) ), | 675 | QObject::connect( innot, SIGNAL( activated(int) ), |
676 | this, SLOT( slotSendData( int ) ) ); | 676 | this, SLOT( slotSendData(int) ) ); |
677 | } | 677 | } |
678 | 678 | ||
679 | if ( communication & Stdout ) | 679 | if ( communication & Stdout ) |
680 | { | 680 | { |
681 | // ok &= (-1 != fcntl(out[0], F_SETFL, O_NONBLOCK)); | 681 | // ok &= (-1 != fcntl(out[0], F_SETFL, O_NONBLOCK)); |
682 | outnot = new QSocketNotifier( out[ 0 ], QSocketNotifier::Read, this ); | 682 | outnot = new QSocketNotifier( out[ 0 ], QSocketNotifier::Read, this ); |
683 | CHECK_PTR( outnot ); | 683 | CHECK_PTR( outnot ); |
684 | QObject::connect( outnot, SIGNAL( activated( int ) ), | 684 | QObject::connect( outnot, SIGNAL( activated(int) ), |
685 | this, SLOT( slotChildOutput( int ) ) ); | 685 | this, SLOT( slotChildOutput(int) ) ); |
686 | if ( communication & NoRead ) | 686 | if ( communication & NoRead ) |
687 | suspend(); | 687 | suspend(); |
688 | } | 688 | } |
689 | 689 | ||
690 | if ( communication & Stderr ) | 690 | if ( communication & Stderr ) |
691 | { | 691 | { |
692 | // ok &= (-1 != fcntl(err[0], F_SETFL, O_NONBLOCK)); | 692 | // ok &= (-1 != fcntl(err[0], F_SETFL, O_NONBLOCK)); |
693 | errnot = new QSocketNotifier( err[ 0 ], QSocketNotifier::Read, this ); | 693 | errnot = new QSocketNotifier( err[ 0 ], QSocketNotifier::Read, this ); |
694 | CHECK_PTR( errnot ); | 694 | CHECK_PTR( errnot ); |
695 | QObject::connect( errnot, SIGNAL( activated( int ) ), | 695 | QObject::connect( errnot, SIGNAL( activated(int) ), |
696 | this, SLOT( slotChildError( int ) ) ); | 696 | this, SLOT( slotChildError(int) ) ); |
697 | } | 697 | } |
698 | } | 698 | } |
699 | return ok; | 699 | return ok; |
700 | } | 700 | } |
701 | 701 | ||
702 | int OProcess::commSetupDoneC() | 702 | int OProcess::commSetupDoneC() |
703 | { | 703 | { |
704 | int ok = 1; | 704 | int ok = 1; |
705 | struct linger so; | 705 | struct linger so; |
706 | memset( &so, 0, sizeof( so ) ); | 706 | memset( &so, 0, sizeof( so ) ); |
707 | 707 | ||
708 | if ( communication & Stdin ) | 708 | if ( communication & Stdin ) |
709 | close( in[ 1 ] ); | 709 | close( in[ 1 ] ); |
710 | if ( communication & Stdout ) | 710 | if ( communication & Stdout ) |
711 | close( out[ 0 ] ); | 711 | close( out[ 0 ] ); |
712 | if ( communication & Stderr ) | 712 | if ( communication & Stderr ) |
713 | close( err[ 0 ] ); | 713 | close( err[ 0 ] ); |
714 | 714 | ||
715 | if ( communication & Stdin ) | 715 | if ( communication & Stdin ) |
716 | ok &= dup2( in[ 0 ], STDIN_FILENO ) != -1; | 716 | ok &= dup2( in[ 0 ], STDIN_FILENO ) != -1; |
717 | else | 717 | else |
718 | { | 718 | { |
719 | int null_fd = open( "/dev/null", O_RDONLY ); | 719 | int null_fd = open( "/dev/null", O_RDONLY ); |
720 | ok &= dup2( null_fd, STDIN_FILENO ) != -1; | 720 | ok &= dup2( null_fd, STDIN_FILENO ) != -1; |
721 | close( null_fd ); | 721 | close( null_fd ); |
722 | } | 722 | } |
723 | if ( communication & Stdout ) | 723 | if ( communication & Stdout ) |
724 | { | 724 | { |
725 | ok &= dup2( out[ 1 ], STDOUT_FILENO ) != -1; | 725 | ok &= dup2( out[ 1 ], STDOUT_FILENO ) != -1; |
726 | ok &= !setsockopt( out[ 1 ], SOL_SOCKET, SO_LINGER, ( char* ) & so, sizeof( so ) ); | 726 | ok &= !setsockopt( out[ 1 ], SOL_SOCKET, SO_LINGER, ( char* ) & so, sizeof( so ) ); |
727 | } | 727 | } |
728 | else | 728 | else |
729 | { | 729 | { |
730 | int null_fd = open( "/dev/null", O_WRONLY ); | 730 | int null_fd = open( "/dev/null", O_WRONLY ); |
731 | ok &= dup2( null_fd, STDOUT_FILENO ) != -1; | 731 | ok &= dup2( null_fd, STDOUT_FILENO ) != -1; |
732 | close( null_fd ); | 732 | close( null_fd ); |
733 | } | 733 | } |
734 | if ( communication & Stderr ) | 734 | if ( communication & Stderr ) |
735 | { | 735 | { |
736 | ok &= dup2( err[ 1 ], STDERR_FILENO ) != -1; | 736 | ok &= dup2( err[ 1 ], STDERR_FILENO ) != -1; |
737 | ok &= !setsockopt( err[ 1 ], SOL_SOCKET, SO_LINGER, reinterpret_cast<char *>( &so ), sizeof( so ) ); | 737 | ok &= !setsockopt( err[ 1 ], SOL_SOCKET, SO_LINGER, reinterpret_cast<char *>( &so ), sizeof( so ) ); |
738 | } | 738 | } |
739 | else | 739 | else |
740 | { | 740 | { |
741 | int null_fd = open( "/dev/null", O_WRONLY ); | 741 | int null_fd = open( "/dev/null", O_WRONLY ); |
742 | ok &= dup2( null_fd, STDERR_FILENO ) != -1; | 742 | ok &= dup2( null_fd, STDERR_FILENO ) != -1; |
743 | close( null_fd ); | 743 | close( null_fd ); |
744 | } | 744 | } |
745 | return ok; | 745 | return ok; |
746 | } | 746 | } |
747 | 747 | ||
748 | void OProcess::commClose() | 748 | void OProcess::commClose() |
749 | { | 749 | { |
750 | if ( NoCommunication != communication ) | 750 | if ( NoCommunication != communication ) |
751 | { | 751 | { |
752 | bool b_in = ( communication & Stdin ); | 752 | bool b_in = ( communication & Stdin ); |
753 | bool b_out = ( communication & Stdout ); | 753 | bool b_out = ( communication & Stdout ); |
754 | bool b_err = ( communication & Stderr ); | 754 | bool b_err = ( communication & Stderr ); |
755 | if ( b_in ) | 755 | if ( b_in ) |
756 | delete innot; | 756 | delete innot; |
757 | 757 | ||
758 | if ( b_out || b_err ) | 758 | if ( b_out || b_err ) |
759 | { | 759 | { |
760 | // If both channels are being read we need to make sure that one socket buffer | 760 | // If both channels are being read we need to make sure that one socket buffer |
761 | // doesn't fill up whilst we are waiting for data on the other (causing a deadlock). | 761 | // doesn't fill up whilst we are waiting for data on the other (causing a deadlock). |
762 | // Hence we need to use select. | 762 | // Hence we need to use select. |
763 | 763 | ||
764 | // Once one or other of the channels has reached EOF (or given an error) go back | 764 | // Once one or other of the channels has reached EOF (or given an error) go back |
765 | // to the usual mechanism. | 765 | // to the usual mechanism. |
766 | 766 | ||
767 | int fds_ready = 1; | 767 | int fds_ready = 1; |
768 | fd_set rfds; | 768 | fd_set rfds; |
769 | 769 | ||
770 | int max_fd = 0; | 770 | int max_fd = 0; |
771 | if ( b_out ) | 771 | if ( b_out ) |
772 | { | 772 | { |
773 | fcntl( out[ 0 ], F_SETFL, O_NONBLOCK ); | 773 | fcntl( out[ 0 ], F_SETFL, O_NONBLOCK ); |
774 | if ( out[ 0 ] > max_fd ) | 774 | if ( out[ 0 ] > max_fd ) |
775 | max_fd = out[ 0 ]; | 775 | max_fd = out[ 0 ]; |
776 | delete outnot; | 776 | delete outnot; |
777 | outnot = 0; | 777 | outnot = 0; |
778 | } | 778 | } |
779 | if ( b_err ) | 779 | if ( b_err ) |
780 | { | 780 | { |
781 | fcntl( err[ 0 ], F_SETFL, O_NONBLOCK ); | 781 | fcntl( err[ 0 ], F_SETFL, O_NONBLOCK ); |
782 | if ( err[ 0 ] > max_fd ) | 782 | if ( err[ 0 ] > max_fd ) |
783 | max_fd = err[ 0 ]; | 783 | max_fd = err[ 0 ]; |
784 | delete errnot; | 784 | delete errnot; |
785 | errnot = 0; | 785 | errnot = 0; |
786 | } | 786 | } |
787 | 787 | ||
788 | 788 | ||
789 | while ( b_out || b_err ) | 789 | while ( b_out || b_err ) |
790 | { | 790 | { |
791 | // * If the process is still running we block until we | 791 | // * If the process is still running we block until we |
792 | // receive data. (p_timeout = 0, no timeout) | 792 | // receive data. (p_timeout = 0, no timeout) |