| 1 | # Copyright (c) 2003, Cornell University | ||
| 2 | # See the file COPYING for the status of this software | ||
| 3 | |||
| 4 | package SOAP::Clean::Processes::Base; | ||
| 5 | |||
| 6 | use warnings; | ||
| 7 | use strict; | ||
| 8 | |||
| 9 | # Inheritance | ||
| 10 | our @ISA = qw(); | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $proto = shift; | ||
| 14 | my $class = ref($proto) || $proto; | ||
| 15 | my $self = {}; | ||
| 16 | bless $self, $class; | ||
| 17 | $self->initialize(@_); | ||
| 18 | return $self; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub initialize { | ||
| 22 | } | ||
| 23 | |||
| 24 | # "Virtual" methods | ||
| 25 | # | ||
| 26 | # $status = $self->process_run($cmd,$out_log) | ||
| 27 | # $process_info_str = $self->process_spawn($cmd,$out_log) | ||
| 28 | # $is_running = $self->process_running($process_info_str) | ||
| 29 | # $status = $self->process_result($process_info_str) | ||
| 30 | |||
| 31 | ######################################################################## | ||
| 32 | |||
| 33 | package SOAP::Clean::Processes::Basic; | ||
| 34 | |||
| 35 | use warnings; | ||
| 36 | use strict; | ||
| 37 | |||
| 38 | use POSIX ":sys_wait_h"; | ||
| 39 | use File::Temp qw/ :POSIX /; | ||
| 40 | |||
| 41 | use SOAP::Clean::Misc; | ||
| 42 | |||
| 43 | our @ISA = qw(SOAP::Clean::Processes::Base); | ||
| 44 | |||
| 45 | sub initialize { | ||
| 46 | my ($self) = @_; | ||
| 47 | $self->SUPER::initialize(); | ||
| 48 | } | ||
| 49 | |||
| 50 | ######################################################################## | ||
| 51 | |||
| 52 | sub process_run { | ||
| 53 | my ($self,$cmd,$out_log) = @_; | ||
| 54 | |||
| 55 | my $extended_cmd = | ||
| 56 | sprintf("sh -c '( %s ) < /dev/null > %s 2>&1'", | ||
| 57 | $cmd, $out_log); | ||
| 58 | my $status = system($extended_cmd); | ||
| 59 | return $status; | ||
| 60 | } | ||
| 61 | |||
| 62 | ######################################################################## | ||
| 63 | |||
| 64 | sub process_spawn { | ||
| 65 | my ($self,$cmd,$out_log) = @_; | ||
| 66 | |||
| 67 | my $status_file = tmpnam(); | ||
| 68 | my $extended_cmd = | ||
| 69 | sprintf("sh -c '( %s ) < /dev/null > %s 2>&1; echo \$? > %s'", | ||
| 70 | $cmd, $out_log, $status_file); | ||
| 71 | # Spawn the command. | ||
| 72 | my $pid = fork(); | ||
| 73 | if (!defined($pid)) { | ||
| 74 | die("SOAP-Server - fork failed"); | ||
| 75 | } elsif ($pid == 0) { | ||
| 76 | # the child we have to close all files, or Apache will wait for the | ||
| 77 | # child to finish. | ||
| 78 | close(STDIN); close(STDOUT); close(STDERR); | ||
| 79 | exec($extended_cmd); | ||
| 80 | die; # if we reach this, then the exec failed. | ||
| 81 | } | ||
| 82 | |||
| 83 | my $process_info = [ $pid, $status_file ]; | ||
| 84 | my $process_info_str = basic_info_to_string($process_info); | ||
| 85 | return $process_info_str; | ||
| 86 | } | ||
| 87 | |||
| 88 | ######################################################################## | ||
| 89 | |||
| 90 | sub process_running { | ||
| 91 | my ($self,$process_info_str) = @_; | ||
| 92 | my $process_info = basic_info_from_string($process_info_str); | ||
| 93 | my ($pid,$status_file) = @$process_info; | ||
| 94 | |||
| 95 | # First, try waitpid. This will succeed if the process is a child of | ||
| 96 | # the current process. waitpid is horrendously slow under Cygwin. It | ||
| 97 | # seems to run reasonably under Linux. | ||
| 98 | my $waitpid_result = waitpid($pid,WNOHANG); | ||
| 99 | if ( $waitpid_result == 0 ) { | ||
| 100 | # still running. | ||
| 101 | return 1; | ||
| 102 | } elsif ( $waitpid_result == $pid ) { | ||
| 103 | # done. | ||
| 104 | return 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | # Walk the process table | ||
| 108 | open F,"ps -aef |"; | ||
| 109 | |||
| 110 | # Find the index of the PID field. | ||
| 111 | my $l = <F> || die; | ||
| 112 | chomp $l; | ||
| 113 | $l =~ s/^[ \t]+//; | ||
| 114 | my @fields = split(/[ \t]+/,$l); | ||
| 115 | my $i = 0; | ||
| 116 | my $found_pid = 0; | ||
| 117 | FIELD: | ||
| 118 | foreach my $f ( @fields ) { | ||
| 119 | if ($f eq "PID") { | ||
| 120 | $found_pid = 1; | ||
| 121 | last FIELD; | ||
| 122 | } | ||
| 123 | $i++; | ||
| 124 | } | ||
| 125 | $found_pid || die; | ||
| 126 | |||
| 127 | |||
| 128 | # Look for the $pid in the PID field. | ||
| 129 | my $result = 0; | ||
| 130 | LINE: | ||
| 131 | while ($l = <F>) { | ||
| 132 | chomp $l; | ||
| 133 | $l =~ s/^[ \t]+//; | ||
| 134 | @fields = split(/[ \t]+/,$l); | ||
| 135 | if ($fields[$i] == $pid) { | ||
| 136 | # still running! | ||
| 137 | $result = 1; | ||
| 138 | last LINE; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | close F; | ||
| 143 | return $result; | ||
| 144 | } | ||
| 145 | |||
| 146 | ######################################################################## | ||
| 147 | |||
| 148 | sub process_result { | ||
| 149 | my ($self,$process_info_str) = @_; | ||
| 150 | my $process_info = basic_info_from_string($process_info_str); | ||
| 151 | my ($pid,$status_file) = @$process_info; | ||
| 152 | |||
| 153 | # The process had better be done! | ||
| 154 | (!$self->process_running($process_info)) || die; | ||
| 155 | |||
| 156 | if ( ! ( -r $status_file ) ) { return -1; } | ||
| 157 | |||
| 158 | open F,"<$status_file" || die; | ||
| 159 | my $status = <F>; | ||
| 160 | close F; | ||
| 161 | |||
| 162 | unlink $status_file; | ||
| 163 | |||
| 164 | return $status; | ||
| 165 | } | ||
| 166 | |||
| 167 | ######################################################################## | ||
| 168 | |||
| 169 | sub basic_info_to_string { | ||
| 170 | my ($process_info) = @_; | ||
| 171 | |||
| 172 | if ( !(ref $process_info) ) { return $process_info; } | ||
| 173 | |||
| 174 | my ($pid,$status_file) = @$process_info; | ||
| 175 | |||
| 176 | return sprintf("%d:%s",$pid,$status_file); | ||
| 177 | } | ||
| 178 | |||
| 179 | ######################################################################## | ||
| 180 | |||
| 181 | sub basic_info_from_string { | ||
| 182 | my ($str) = @_; | ||
| 183 | |||
| 184 | if ( ref $str eq "ARRAY" ) { return $str; } | ||
| 185 | |||
| 186 | assert($str =~ /([0-9]+):(.*)/ ); | ||
| 187 | |||
| 188 | return [ $1, $2 ]; | ||
| 189 | } | ||
| 190 | |||
| 191 | ######################################################################## | ||
| 192 | |||
| 193 | 1; |