def monitor_cpu_disk_and_mem(): if ostype == 'Linux' or ostype == 'Darwin': # Startup a CPU monitoring thread/process do_forked_resource_monitor() elif ostype == 'Windows' or ostype == 'WindowsCE': # Now we set up a cpu nanny... # Use an external CPU monitor for WinCE if ostype == 'WindowsCE': nannypath = "\"" + repy_constants.PATH_SEATTLE_INSTALL + 'win_cpu_nanny.py' + "\"" cmdline = str(os.getpid()) + " " + str( nanny.get_resource_limit("cpu")) + " " + str( repy_constants.CPU_POLLING_FREQ_WINCE) windows_api.launch_python_script(nannypath, cmdline) else: WinCPUNannyThread().start() # Launch mem./disk resource nanny WindowsNannyThread().start() # Start the nmstatusinterface. Windows means repy isn't run in an external # process, so pass None instead of a process id. nmstatusinterface.launch(None) else: raise UnsupportedSystemException, "Unsupported system type: '" + osrealtype + "' (alias: " + ostype + ")"
def do_forked_resource_monitor(): global repy_process_id # Get a pipe (readhandle, writehandle) = os.pipe() # I'll fork a copy of myself childpid = os.fork() if childpid == 0: # We are the child, close the write end of the pipe os.close(writehandle) # Start a thread to check on the survival of the parent parent_process_checker(readhandle).start() return else: # We are the parent, close the read end os.close(readhandle) # Store the childpid repy_process_id = childpid # Start the nmstatusinterface nmstatusinterface.launch(repy_process_id) # Small internal error handler function def _internal_error(message): try: print >> sys.stderr, message sys.stderr.flush() except: pass # Stop the nmstatusinterface, we don't want any more status updates nmstatusinterface.stop() # Kill repy harshexit.portablekill(childpid) try: # Write out status information, repy was Stopped statusstorage.write_status("Terminated") except: pass try: # Some OS's require that you wait on the pid at least once # before they do any accounting (pid, status) = os.waitpid(childpid,os.WNOHANG) # Launch the resource monitor, if it fails determine why and restart if necessary resource_monitor(childpid, writehandle) except ResourceException, exp: # Repy exceeded its resource limit, kill it _internal_error(str(exp)+" Impolitely killing child!") harshexit.harshexit(98)
def monitor_cpu_disk_and_mem(): if ostype == 'Linux' or ostype == 'Darwin': # Startup a CPU monitoring thread/process do_forked_resource_monitor() elif ostype == 'Windows': # Now we set up a cpu nanny... WinCPUNannyThread().start() # Launch mem./disk resource nanny WindowsNannyThread().start() # Start the nmstatusinterface. Windows means repy isn't run in an external # process, so pass None instead of a process id. nmstatusinterface.launch(None) else: raise UnsupportedSystemException, "Unsupported system type: '" + osrealtype + "' (alias: " + ostype + ")"
def monitor_cpu_disk_and_mem(): if ostype == 'Linux' or ostype == 'Darwin': # Startup a CPU monitoring thread/process do_forked_resource_monitor() elif ostype == 'Windows': # Now we set up a cpu nanny... WinCPUNannyThread().start() # Launch mem./disk resource nanny WindowsNannyThread().start() # Start the nmstatusinterface. Windows means repy isn't run in an external # process, so pass None instead of a process id. nmstatusinterface.launch(None) else: raise UnsupportedSystemException, "Unsupported system type: '"+osrealtype+"' (alias: "+ostype+")"
def monitor_cpu_disk_and_mem(): if ostype == 'Linux' or ostype == 'Darwin': # Startup a CPU monitoring thread/process do_forked_resource_monitor() elif ostype == 'Windows' or ostype == 'WindowsCE': # Now we set up a cpu nanny... # Use an external CPU monitor for WinCE if ostype == 'WindowsCE': nannypath = "\"" + repy_constants.PATH_SEATTLE_INSTALL + 'win_cpu_nanny.py' + "\"" cmdline = str(os.getpid())+" "+str(nanny_resource_limits.resource_limit("cpu"))+" "+str(repy_constants.CPU_POLLING_FREQ_WINCE) windows_api.launch_python_script(nannypath, cmdline) else: WinCPUNannyThread().start() # Launch mem./disk resource nanny WindowsNannyThread().start() # Start the nmstatusinterface. Windows means repy isn't run in an external # process, so pass None instead of a process id. nmstatusinterface.launch(None) else: raise UnsupportedSystemException, "Unsupported system type: '"+osrealtype+"' (alias: "+ostype+")"
def do_forked_resource_monitor_android(): repypid = os.getpid() (readhandle, writehandle) = os.pipe() os_api.monitored_process_procfs_stat_file = open("/proc/" + str(repypid) + "/stat") if os.fork(): # Parent does not need to write to the pipe os.close(writehandle) # Start a thread to check on the survival of the monitor process monitor_process_checker(readhandle).start() # Go do important stuff (execute repy code) return # The child does not need to read from the pipe os.close(readhandle) # Start the nmstatusinterface nmstatusinterface.launch(repypid) kill_repy = False error_msg = False monitor_exit_code = 0 try: # Launch the resource monitor, if it fails determine why and restart if necessary resource_monitor(repypid, writehandle) except ResourceException, exp: # Repy exceeded its resource limit, kill it kill_repy = True error_msg = str(exp) + " Impolitely killing repy process!" # XXX LP: Why 98? Not defined in harshexit or elsewhere... monitor_exit_code = 98
def main(): repy_location = sys.argv[0] repy_directory = os.path.dirname(repy_location) init_repy_location(repy_directory) usage = "USAGE: repy.py [options] program_to_run.r2py [program args]" parser = optparse.OptionParser(usage=usage) parser.disable_interspersed_args() add_repy_options(parser) options, args = parser.parse_args() if len(args) < 1: print("Repy requires a program to run!") parser.print_help() sys.exit(1) #resourcefn = args[0] progname = args[0] progargs = args[1:] parse_options(options) nmstatusinterface.launch(None) try: filehandle = open(progname) usercode = filehandle.read() filehandle.close() except: print( f"FATAL ERROR: Unable to read the specified program file: {progname}" ) sys.exit(1) try: newnamespace = virtual_namespace.VirtualNamespace(usercode, progname) except CodeUnsafeError as e: print("Specified repy program is unsafe!") print("Static-code analysis failed with error: " + str(e)) harshexit.harshexit(5) del usercode if options.execinfo: print("=" * 40) print("Running program: " + progname) print("Arguments: " + progargs) print("=" * 40) newcontext = get_safe_context(progargs) try: newnamespace.evaluate(newcontext) except SystemExit: raise except: tracebackrepy.handle_exception() harshexit.harshexit(6) harshexit.harshexit(0)