-
Notifications
You must be signed in to change notification settings - Fork 0
/
detectorvnc.py
executable file
·312 lines (259 loc) · 9.22 KB
/
detectorvnc.py
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#! /usr/local/bin/python
#
# detectorvnc.py
#
# Automatically start marccd in vnc (display :1.0) when someone logs into
# collect data.
#
# (C) 2011 by Keith Brister
# All rights reserved
#
import pg # postgresql support
import sys #
import os #
import subprocess #
import select # for poll support
import pwd # to find our home directory
import time # sleep
import signal #
class DetectorVNCError( Exception):
value = None
def __init__( self, value):
self.value = value
print >> sys.stderr, sys.exc_info()[0]
print >> sys.stderr, '-'*60
traceback.print_exc(file=sys.stderr)
print >> sys.stderr, '-'*60
def __str__( self):
return repr( self.value)
class _Q:
db = None # our database connection
def open( self):
self.db = pg.connect( dbname="ls", host="postgres.ls-cat.net", user="lsuser" )
def close( self):
self.db.close()
def __init__( self):
self.open()
def reset( self):
self.db.reset()
def query( self, qs):
if qs == '':
return rtn
if self.db.status == 0:
self.reset()
try:
# ping the server
qr = self.db.query(qs)
except:
print "Failed query: %s" % (qs)
if self.db.status == 1:
print >> sys.stderr, sys.exc_info()[0]
print >> sys.stderr, '-'*60
traceback.print_exc(file=sys.stderr)
print >> sys.stderr, '-'*60
return None
# reset the connection, should
# put in logic here to deal with transactions
# as transactions are rolled back
#
self.db.reset()
if self.db.status != 1:
# Bad status even after a reset, bail
raise DetectorVNCError( 'Database Connection Lost')
qr = self.db.query( qs)
return qr
def dictresult( self, qr):
return qr.dictresult()
def e( self, s):
return pg.escape_string( s)
def fileno( self):
return self.db.fileno()
def getnotify( self):
return self.db.getnotify()
class DetectorVNC:
def killprocess( self, signum=None, sframe=None):
#
# In CHILD
#
if self.screen != None:
self.screen.terminate()
self.vnc.terminate()
time.sleep( 2) # wait for the excitement to die down
self.screen.poll()
if self.screen.returncode == None:
self.screen.kill()
self.vnc.poll()
if self.vnc.returncode == None:
self.vnc.kill()
def __init__(self):
self._q = _Q()
self._q.query( "select px.detectorvncinit()");
self.pid = None # child that runs as the esaf user
self.vnc = None # Popen object of the vnc process IN CHILD
self.screen = None # Popen object of the screen process IN CHILD
def stop_vnc( self):
#
# In PARENT
#
if self.pid != None:
#
# ask the child process to move out
#
os.kill( self.pid, signal.SIGABRT)
i=0
while i<10:
xc = os.waitpid( self.pid, os.WNOHANG)
if xc != (0,0):
break
time.sleep(1)
if i>=10:
os.kill( self.pid, signal.SIGKILL)
os.waitpid( self.pid, 0)
self.pid = None
pid2 = os.fork()
if pid2 != 0:
#
# In Parent
#
os.waitpid( pid2, 0)
return
#
# In Child
#
uid = self.esaf * 100
os.setgid( uid)
os.setuid( uid)
hd = pwd.getpwuid( uid).pw_dir
os.chdir( hd)
print >> sys.stderr, "stopping VNC"
vnc = subprocess.Popen( ["/usr/bin/vncserver",
"-kill",
":1"
],
env={"HOME" : hd, "PATH" : "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"},
stdin=None, stdout=None, stderr=None,
close_fds=True, shell=False, cwd=hd
)
vnc.wait()
os._exit(0)
def start_vnc( self):
xstartup = """#!/bin/sh
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
gnome-wm &
gnome-panel &
"""
self.stop_vnc()
self.pid = os.fork()
if self.pid != 0:
#
# In Parent
#
return
#
# In Child
#
uid = self.esaf * 100
os.setgid( uid)
os.setuid( uid)
hd = pwd.getpwuid( uid).pw_dir
os.chdir( hd)
signal.signal( signal.SIGABRT, self.killprocess);
#
# Create VNC directory (if needed) and force our xstartup script
#
try:
st = os.stat( "%s/.vnc" % (hd))
except OSError as e:
if e.errno == 2:
os.mkdir( "%s/.vnc" % (hd), 0770)
f = open( "%s/.vnc/xstartup" % (hd), "w")
f.write(xstartup)
f.close()
#
# Make our own VNC Password
# This is not secure, but only local users can access vnc
# so one should restrict logins from /etc/ssh/sshd_config
# to keep the server accessable only to staff
#
vnc_env={"HOME" : hd, "PATH" : "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"}
vncpwp = subprocess.Popen( ["/usr/bin/vncpasswd"],
env = vnc_env, shell=False, cwd=hd, close_fds=True,
stdin=subprocess.PIPE, stdout=None, stderr=None
)
vncpwp.stdin.write("curolonge\n")
vncpwp.stdin.write("curolonge\n")
vncpwp.wait()
#
# now to start our vncserver
#
print >> sys.stderr, "starting VNC"
self.vnc = subprocess.Popen( ["/usr/bin/vncserver",
":1",
"-name", "ESAF %d" % (self.esaf),
"-depth", "24",
"-cc", "4",
"-geometry", "1920x1200",
"-SecurityTypes", "None",
"-AlwaysShared",
"-localhost"
],
env = vnc_env,
stdin=None, stdout=None, stderr=None,
close_fds=True, shell=False, cwd=hd
)
time.sleep(5)
print >> sys.stderr, "starting screen"
self.screen = subprocess.Popen( ["/usr/bin/screen",
"-d", "-m",
"-s", "/bin/bash",
"-t", "ESAF %d marccd" % (self.esaf),
"/usr/local/bin/marccd"
],
stdin=None, stdout=None, stderr=None,
env={"DISPLAY":"localhost:1.0", "HOME" : hd, "PATH" : "/usr/local/bin:/usr/bin:/bin/:/usr/sbin:/sbin"},
shell=False, cwd=hd, close_fds=True
)
print >> sys.stderr, "waiting for screen to finish"
self.screen.wait()
print >> sys.stderr, "waiting for vnc to finish"
self.vnc.wait()
print >> sys.stderr, "Finished"
os._exit( 0)
def get_esaf( self):
rtn = None
qr = self._q.query( "select px.currentesaf() as esaf");
if qr != None and qr.ntuples() == 1:
tmp = qr.dictresult()[0]["esaf"]
if tmp != None:
rtn = int(tmp)
return rtn
def run(self):
self.esaf = None
self.pid = None
self.esaf = self.get_esaf()
if self.esaf != None:
print >>sys.stderr, "Initializing with ESAF: %d" % (self.esaf)
self.start_vnc()
p = select.poll()
p.register( self._q, select.POLLIN)
running = True
while running:
p.poll()
while self._q.getnotify() != None:
# eat the notifies
pass
next_esaf = self.get_esaf()
if next_esaf == None or next_esaf != self.esaf:
if self.esaf != None:
print >>sys.stderr, "Stopping ESAF: %d" % (self.esaf)
self.stop_vnc()
self.esaf = next_esaf
if self.esaf != None:
print >>sys.stderr, "Starting ESAF: %d" % (self.esaf)
self.start_vnc()
if __name__ == "__main__":
dvnc = DetectorVNC()
dvnc.run()