-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraspirec.rb
executable file
·231 lines (194 loc) · 4.03 KB
/
raspirec.rb
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/local/bin/ruby
# coding: utf-8
#
#
# main
#
require 'rubygems'
require 'optparse'
require 'sqlite3'
base = File.dirname( $0 )
$: << base + "/src"
require 'require.rb'
$opt = {
:f => false, # forground
:lc => false, # log clear
:dc => false, # db clear
:conf => nil, # config file 指定
}
$debug = Debug
LoopMax = 99
OptionParser.new do |opt|
opt.on('-d') {|v| $debug = true }
opt.on('-f') {|v| $opt[:f] = !$opt[:f] }
opt.on('--kill') { Control.new.stop(); exit } # kill process
opt.on('--lc') {|v| $opt[:lc] = true } # log clear
opt.on('--dc') {|v| $opt[:dc] = true } # db clear
opt.on('--config FILE') {|v| $opt[:conf] = v } # config
opt.parse!(ARGV)
end
#
# 終了処理
#
def endParoc()
$endParoc = true
count = 0
[ $timer_pid, $httpd_pid ].each do |pid|
begin
DBlog::sto( "main::endParoc() kill #{pid}" )
Process.kill(:TERM, pid );
count += 1
rescue
end
end
count.times do |n|
sleep(1)
putc(".")
STDOUT.flush
end
exit
end
#
# デーモン化
#
def daemonStart( )
if fork # 親
exit!(0)
else # 子
Process::setsid
if fork # 親
exit!(0)
else # 子
Dir::chdir("/")
File::umask(0)
STDIN.reopen("/dev/null")
if $debug == true
STDOUT.reopen( StdoutM, "w")
STDERR.reopen( StderrM, "w")
else
STDOUT.reopen("/dev/null", "w")
STDERR.reopen("/dev/null", "w")
end
yield if block_given?
end
end
end
$timer_main = "#{SrcDir}/timer_main.rb"
$httpd_main = "#{SrcDir}/httpd_main.rb"
#
# main loop
#
def mainLoop()
DBlog::sto("main loop start #{Commlib::getVer()}")
File.open( PidFile, "w") do |fp|
fp.puts( Process.pid )
end
if $opt[:conf] != nil
ENV["RASPIREC_CONF_OPT"] = $opt[:conf]
end
if Object.const_defined?(:DevAutoDetection) == true and
DevAutoDetection == true
DeviceChk.new.run
end
pids = []
pids << Thread.new do
tcount = 0
while true
$timer_pid = fork do
args = [ $timer_main ]
args << "--debug" if $debug == true
exec("ruby", *args )
end
Process.waitpid( $timer_pid )
DBlog::warn(nil, "timer_main end")
sleep 5
break if $endParoc == true
break if tcount > LoopMax
tcount += 1
end
end
pids << Thread.new do
hcount = 0
while true
$httpd_pid = fork do
args = [ $httpd_main, "-o", "0.0.0.0"]
if Http_port != nil and Http_port > 0
args += [ "-p", Http_port.to_s ]
end
args += %w( -- --debug ) if $debug == true
exec("ruby", *args )
end
Process.waitpid( $httpd_pid )
DBlog::warn(nil, "httpd_main end")
sleep 5
break if $endParoc == true
break if hcount > LoopMax
hcount += 1
end
end
if $opt[:tail] == true
pids << Thread.new do
tailLog()
end
end
pids.each {|t| t.join}
end
#
# 初期化
#
rmlist = []
if $opt[:lc] == true
rmlist += [
EPGLockFN,
DbupdateFN,
LogFname,
StdoutM,
StderrM,
StdoutH,
StderrH,
StdoutT,
StderrT
]
end
if $opt[:dc] == true
rmlist << DbFname
end
if rmlist.size > 0
rmlist.each do |fn|
if test( ?f, fn )
pp "unlink(#{fn})"
File.unlink( fn )
File.open( fn, "w" ) {|fp| } unless fn == DbFname
end
end
exit
end
#
# 初期化
#
Commlib::makeSubDir()
File.open( MainLockFN, File::RDWR|File::CREAT, 0644) do |fl|
if fl.flock(File::LOCK_EX|File::LOCK_NB) == false
puts("raspirec locked\n")
exit
end
[ EPGLockFN ].each do |fn|
if test(?f, fn )
File.unlink( fn )
end
end
$rec_pid = {}
$endParoc = false
setTrap()
[ $timer_main , $httpd_main].each do |fname|
unless test( ?f, fname )
puts("Error: #{fname} not found")
exit
end
end
if $opt[:f] == false
daemonStart{ mainLoop() }
else
mainLoop()
end
end