@@ -215,6 +215,44 @@ def make_containers(name_prefix='', port_offset=0):
215215 return lst
216216
217217
218+ INTERESTING_COMMS = ('python' , 'ssh' , 'sudo' , 'su' , 'doas' )
219+
220+
221+ def proc_is_docker (pid ):
222+ try :
223+ fp = open ('/proc/%s/cgroup' % (pid ,), 'rb' )
224+ except IOError :
225+ return False
226+
227+ try :
228+ return 'docker' in fp .read ()
229+ finally :
230+ fp .close ()
231+
232+
233+ def get_interesting_procs (container_name = None ):
234+ args = ['ps' , '-a' , '-x' , '-oppid=' , '-opid=' , '-ocomm=' , '-ocommand=' ]
235+ if container_name is not None :
236+ args = ['docker' , 'exec' , container_name ] + args
237+
238+ out = []
239+ for line in subprocess__check_output (args ).splitlines ():
240+ ppid , pid , comm , rest = line .split (None , 3 )
241+ if (
242+ (
243+ any (comm .startswith (s ) for s in INTERESTING_COMMS ) or
244+ 'mitogen:' in rest
245+ ) and
246+ (
247+ container_name is not None or
248+ (not proc_is_docker (pid ))
249+ )
250+ ):
251+ out .append ((int (pid ), line ))
252+
253+ return sorted (out )
254+
255+
218256def start_containers (containers ):
219257 if os .environ .get ('KEEP' ):
220258 return
@@ -236,9 +274,44 @@ def start_containers(containers):
236274 ]
237275 for container in containers
238276 ])
277+
278+ for container in containers :
279+ container ['interesting' ] = get_interesting_procs (container ['name' ])
280+
239281 return containers
240282
241283
284+ def verify_procs (hostname , old , new ):
285+ oldpids = set (pid for pid , _ in old )
286+ if any (pid not in oldpids for pid , _ in new ):
287+ print ('%r had stray processes running:' % (hostname ,))
288+ for pid , line in new :
289+ if pid not in oldpids :
290+ print ('New process:' , line )
291+
292+ print ()
293+ return False
294+
295+ return True
296+
297+
298+ def check_stray_processes (old , containers = None ):
299+ ok = True
300+
301+ new = get_interesting_procs ()
302+ if old is not None :
303+ ok &= verify_procs ('test host machine' , old , new )
304+
305+ for container in containers or ():
306+ ok &= verify_procs (
307+ container ['name' ],
308+ container ['interesting' ],
309+ get_interesting_procs (container ['name' ])
310+ )
311+
312+ assert ok , 'stray processes were found'
313+
314+
242315def dump_file (path ):
243316 print ()
244317 print ('--- %s ---' % (path ,))
0 commit comments