async def _init(): parallelism = config.get('parallelism', 1) logger.info('Using parallelism: %d', parallelism) for sandbox in await create_sandboxes(parallelism): _sandbox_pool.put_nowait(sandbox) try: with open(_LANGS_FILE) as file: langs_config = yaml.load(file, Loader=yaml.RoundTripLoader) except FileNotFoundError: logger.error('Language file %s not found.', _LANGS_FILE) exit(1) for lang_name, lang_config in langs_config.items(): if lang_config['type'] == 'compiler': compiler = Compiler(lang_config['compiler_file'], shlex.split(lang_config['compiler_args']), lang_config['code_file'], lang_config['execute_file'], shlex.split(lang_config['execute_args'])) _langs[lang_name] = partial( _compiler_build, compiler, time_limit_ns=lang_config.get('time_limit_ms', DEFAULT_TIME_MS) * 1000000, memory_limit_bytes=lang_config.get('memory_limit_kb', DEFAULT_MEM_KB) * 1024, process_limit=lang_config.get('process_limit', PROCESS_LIMIT)) elif lang_config['type'] == 'interpreter': interpreter = Interpreter(lang_config['code_file'], lang_config['execute_file'], shlex.split(lang_config['execute_args'])) _langs[lang_name] = partial(_interpreter_build, interpreter) else: logger.error('Unknown type %s', lang_config['type'])
def _load_config(): try: with open(_CONFIG_FILE) as file: return yaml.load(file, Loader=yaml.RoundTripLoader) except FileNotFoundError: logger.error('Config file %s not found.', _CONFIG_FILE) exit(1)
def _init(): try: with open(_LANGS_FILE) as file: langs_config = yaml.load(file, Loader=yaml.RoundTripLoader) except FileNotFoundError: logger.error('Language file %s not found.', _LANGS_FILE) exit(1) for lang_name, lang_config in langs_config.items(): if lang_config['type'] == 'compiler': compiler = Compiler(lang_config['compiler_file'], shlex.split(lang_config['compiler_args']), lang_config['code_file'], lang_config['execute_file'], shlex.split(lang_config['execute_args'])) _langs[lang_name] = partial( _compiler_build, compiler, parse_time_ns(lang_config.get('time', DEFAULT_TIME)), parse_memory_bytes(lang_config.get('memory', DEFAULT_MEMORY)), lang_config.get('process_limit', PROCESS_LIMIT)) elif lang_config['type'] == 'interpreter': interpreter = Interpreter(lang_config['code_file'], lang_config['execute_file'], shlex.split(lang_config['execute_args'])) _langs[lang_name] = partial(_interpreter_build, interpreter) else: logger.error('Unknown type %s', lang_config['type'])
def try_init_cgroup(): euid = geteuid() if euid == 0: logger.warning('Running as root') cgroups_to_init = list() if not (path.isdir(CPUACCT_CGROUP_ROOT) and access(CPUACCT_CGROUP_ROOT, W_OK)): cgroups_to_init.append(CPUACCT_CGROUP_ROOT) if not (path.isdir(MEMORY_CGROUP_ROOT) and access(MEMORY_CGROUP_ROOT, W_OK)): cgroups_to_init.append(MEMORY_CGROUP_ROOT) if not (path.isdir(PIDS_CGROUP_ROOT) and access(PIDS_CGROUP_ROOT, W_OK)): cgroups_to_init.append(PIDS_CGROUP_ROOT) if cgroups_to_init: if euid == 0: logger.info('Initializing cgroup: %s', ', '.join(cgroups_to_init)) for cgroup_to_init in cgroups_to_init: makedirs(cgroup_to_init, exist_ok=True) elif __stdin__.isatty(): logger.info('Initializing cgroup: %s', ', '.join(cgroups_to_init)) call([ 'sudo', 'sh', '-c', 'mkdir -p "{1}" && chown -R "{0}" "{1}"'.format( euid, '" "'.join(cgroups_to_init)) ]) else: logger.error('Cgroup not initialized')