#!/usr/bin/php /dev/null 1> /dev/null &"; // $cmd = "/usr/bin/tzap -r ".escapeshellarg( $channel )." & 2> /dev/null"; if( DEBUG ) echo "Attempting to fork tzap with $cmd...\n"; echo exec( $cmd ); } function killOtherRecordings(){ $cmd = "/bin/ps ax | /bin/grep \"record \" | /bin/grep php | grep -v grep"; exec( $cmd, $out ); $thispid = posix_getpid(); $killed = true; foreach( $out as $line ){ if( preg_match( "/^\s*(\d+).*?([\s]+)/", $line, $m ) ){ $pid = $m[1]; if( $thispid != $pid ){ $killed = false; if( DEBUG ) echo "other pid recording: ".$pid.", killing\n"; kill( $pid ); sleep(3); } } } return $killed; } function killTzap(){ if( DEBUG ) echo "Attempting to kill tzap\n"; $cmd = "/usr/bin/killall tzap"; exec( $cmd ); } function forkWriteVideo($file){ $cmd = "/bin/cat ".DVR_DEVICE." > ".escapeshellcmd( $file )." &"; if( DEBUG ) echo "Attempting to fork $cmd\n"; exec( $cmd ); } function kill($pid){ posix_kill( $pid, SIGINT ); } function killWriteVideo(){ if( DEBUG ) echo "Attempting to kill write of video file\n"; $cmd = "/bin/ps ax | /bin/grep ".escapeshellarg( " \/bin\/cat ".str_replace( "\/", "\\/", DVR_DEVICE ) ); if( DEBUG ) echo "Looking for process with $cmd\n"; exec( $cmd, $out ); $line = join( "\n", $out ); if( DEBUG ) echo $line."\n"; if( preg_match( "/^\s*(\d+).*?([\s]+)/", $line, $m ) ){ if( DEBUG ) echo "Process ".$m[1]." found for writing video, attempting to kill\n"; /* $cmd = "/bin/kill ".escapeshellarg( $m[1] ); exec( $cmd ); */ posix_kill( $m[1], SIGKILL ); // Increase file permissions on recording file setRecordingPermissions( RECORDING_FILE, RECORDINGS_OWNER ); if( filesize( RECORDINGS_DIR.basename( RECORDING_FILE ) ) == 0 ){ if( DEBUG ) echo "Recording appears to be zero length, deleting\n"; unlink( RECORDINGS_DIR.basename( RECORDING_FILE ) ); } // Move to completed dir if( file_exists(RECORDINGS_DIR.basename( RECORDING_FILE ) ) ){ rename( RECORDINGS_DIR.basename( RECORDING_FILE ), COMPLETE_RECORDINGS_DIR.basename( RECORDING_FILE ) ); return COMPLETE_RECORDINGS_DIR.basename( RECORDING_FILE ); } }else{ if( DEBUG ) echo "No suitable file write processes found\n"; } } function adDetect($path){ if( !$path ) return false; if( !defined( "AD_DETECT_SCRIPT" ) ){ return false; } // Everything except BBC has ads if( preg_match( "/^BBC /", CURRENT_CHANNEL ) ) return false; echo "Current channel: ".CURRENT_CHANNEL." has ads, attempting to detect...\n"; exec( AD_DETECT_SCRIPT." ".escapeshellarg( $path ) ); } function organiseRecordings(){ if( file_exists( RECORDINGS_ORGANISESCRIPT ) ){ if( DEBUG ) echo "Running show organise script...\n"; $user = exec( "/usr/bin/whoami" ); $cmd = ""; if( $user == "root" ) $cmd = "sudo -u ".RECORDINGS_OWNER." "; $cmd .= "/usr/bin/php ".RECORDINGS_ORGANISESCRIPT; if( DEBUG ) echo $cmd."\n"; passthru( $cmd ); } } function setRecordingPermissions( $file, $owner ){ if( !file_exists( $file ) ) return false; if( DEBUG ) echo "Changing permissions on file ".$file."...\n"; $cmd = "/bin/chown ".$owner." ".escapeshellarg( $file ); exec( $cmd ); $cmd = "/bin/chmod u+wr ".escapeshellarg( $file ); exec( $cmd ); } ?>