-
Notifications
You must be signed in to change notification settings - Fork 5
/
getepg.php
executable file
·195 lines (160 loc) · 6.29 KB
/
getepg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/php
<?php
$script_path = dirname( __FILE__ );
chdir( $script_path );
include_once( $script_path . '/config.php');
include_once( INSTALL_PATH . '/DBRecord.class.php' );
include_once( INSTALL_PATH . '/Reservation.class.php' );
include_once( INSTALL_PATH . '/Keyword.class.php' );
include_once( INSTALL_PATH . '/Settings.class.php' );
include_once( INSTALL_PATH . '/storeProgram.inc.php' );
include_once( INSTALL_PATH . '/recLog.inc.php' );
// SIGTERMシグナル
function handler( $signo = 0 ) {
// とりあえずシグナルは無視する
}
// デーモン化
function daemon() {
if( pcntl_fork() != 0 )
exit();
posix_setsid();
if( pcntl_fork() != 0 )
exit;
pcntl_signal(SIGTERM, "handler");
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
}
function check_file( $file ) {
// ファイルがないなら無問題
if( ! file_exists( $file ) ) return true;
// 1時間以上前のファイルなら削除してやり直す
if( (time() - filemtime( $file )) > 3600 ) {
@unlink( $file );
return true;
}
return false;
}
// 子プロセス起動
function epgrec_exec( $cmd, $env = null ) {
$descspec = array(
0 => array( 'file','/dev/null','r' ),
1 => array( 'file','/dev/null','w' ),
2 => array( 'file','/dev/null','w' ),
);
$p = proc_open( $cmd, $descspec, $pipes, INSTALL_PATH, $env );
if( is_resource( $p ) ) return $p;
return false;
}
daemon();
$settings = Settings::factory();
// ユーザー/グループの切り替えを試みる
if(intval($settings->use_power_reduce) != 0 ) {
$userinfo = posix_getpwnam( $settings->www_user );
$groupinfp = posix_getgrnam( $settings->www_group );
posix_setgid( $groupinfo['gid'] );
posix_setuid( $userinfo['uid'] );
}
// 後方互換性
if( ! defined( "BS_EPG_CHANNEL" ) ) define( "BS_EPG_CHANNEL", "211" );
if( ! defined( "CS1_EPG_CHANNEL" ) ) define( "CS1_EPG_CHANNEL", "CS8" );
if( ! defined( "CS2_EPG_CHANNEL" ) ) define( "CS2_EPG_CHANNEL", "CS24" );
$bs_proc = false;
$gr_procs = array();
$cs1_proc = false;
$cs2_proc = false;
$temp_data_bs = $settings->temp_data.".bs";
$temp_data_cs1 = $settings->temp_data.".cs1";
$temp_data_cs2 = $settings->temp_data.".cs2";
$temp_data_gr = $settings->temp_data.".gr";
if( file_exists( $settings->temp_data ) ) @unlink( $settings->temp_data );
// 地上波を処理する
if( $settings->gr_tuners != 0 ) {
foreach( $GR_CHANNEL_MAP as $value ){
// 録画重複チェック
$num = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' AND type = 'GR' AND endtime > now() AND starttime < addtime( now(), '00:01:10')" );
if($num < $settings->gr_tuners && check_file($temp_data_gr.$value."")) {
$cmdline = "CHANNEL=".$value." DURATION=60 TYPE=GR TUNER=0 MODE=0 OUTPUT=".$temp_data_gr.$value." ".DO_RECORD . " >/dev/null 2>&1";
exec( $cmdline );
$cmdline = INSTALL_PATH."/storeProgram.php GR ".$temp_data_gr.$value." ".$value;
$gr_procs[] = epgrec_exec( $cmdline );
}
}
}
// BSを処理する
if( $settings->bs_tuners != 0 ) {
// 録画重複チェック
$num = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' AND (type = 'BS' OR type = 'CS') AND endtime > now() AND starttime < addtime( now(), '00:03:05')" );
if($num < $settings->bs_tuners && check_file($temp_data_bs)) {
$cmdline = "CHANNEL=".BS_EPG_CHANNEL." DURATION=180 TYPE=BS TUNER=0 MODE=0 OUTPUT=".$temp_data_bs." ".DO_RECORD . " >/dev/null 2>&1";
exec( $cmdline );
$cmdline = INSTALL_PATH."/storeProgram.php BS ".$temp_data_bs;
$bs_proc = epgrec_exec( $cmdline );
}
// CS
if ($settings->cs_rec_flg != 0) {
$num = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' AND (type = 'BS' OR type = 'CS') AND endtime > now() AND starttime < addtime( now(), '00:03:05')" );
if($num < $settings->bs_tuners && check_file($temp_data_cs1)) {
$cmdline = "CHANNEL=".CS1_EPG_CHANNEL." DURATION=120 TYPE=CS TUNER=0 MODE=0 OUTPUT=".$temp_data_cs1." ".DO_RECORD . " >/dev/null 2>&1";
exec( $cmdline );
$cmdline = INSTALL_PATH."/storeProgram.php CS1 ".$temp_data_cs1;
$cs1_proc = epgrec_exec($cmdline);
}
$num = DBRecord::countRecords( RESERVE_TBL, "WHERE complete = '0' AND (type = 'BS' OR type = 'CS') AND endtime > now() AND starttime < addtime( now(), '00:03:05')" );
if( ($num < $settings->bs_tuners) && check_file($temp_data_cs2) ) {
$cmdline = "CHANNEL=".CS2_EPG_CHANNEL." DURATION=120 TYPE=CS TUNER=0 MODE=0 OUTPUT=".$temp_data_cs2." ".DO_RECORD . " >/dev/null 2>&1";
exec( $cmdline );
$cmdline = INSTALL_PATH."/storeProgram.php CS2 ".$temp_data_cs2;
$cs2_proc = epgrec_exec( $cmdline );
}
}
}
// 終了を待つ
// 一時的にdefunctするがまあいいや
$counter = 0;
do {
sleep(1);
$counter = 0;
if( count($gr_procs) != 0 ) {
foreach( $gr_procs as $proc ) {
$status = proc_get_status( $proc );
if( $status['running'] ) $counter++;
}
}
if( $bs_proc !== false ){
$status = proc_get_status($bs_proc);
if( $status['running'] ) $counter++;
}
if( $cs1_proc !== false ){
$status = proc_get_status($bs_proc);
if( $status['running'] ) $counter++;
}
if( $cs2_proc !== false ){
$status = proc_get_status($bs_proc);
if( $status['running'] ) $counter++;
}
} while( $counter != 0 );
garbageClean(); // 不要プログラム削除
doKeywordReservation(); // キーワード予約
if( intval($settings->use_power_reduce) != 0 ) {
if( file_exists(INSTALL_PATH. "/settings/wakeupvars.xml") ) {
$wakeupvars_text = file_get_contents( INSTALL_PATH. "/settings/wakeupvars.xml" );
$wakeupvars = new SimpleXMLElement($wakeupvars_text);
// getepg終了時を書込み
$wakeupvars->getepg_time = time();
// 起動理由を調べる
if( strcasecmp( "getepg", $wakeupvars->reason ) == 0 ) {
// 1時間以内に録画はないか?
$count = DBRecord::countRecords( RESERVE_TBL, " WHERE complete <> '1' AND starttime < addtime( now(), '01:00:00') AND endtime > now()" );
if( $count != 0 ) { // 録画があるなら録画起動にして終了
$wakeupvars->reason = "reserve";
}
else {
exec( $settings->shutdown . " -h +".$settings->wakeup_before );
}
}
$wakeupvars->asXML(INSTALL_PATH. "/settings/wakeupvars.xml");
}
}
exit();
?>