我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用resource.setrlimit()。
def test_collision(): import resource resource.setrlimit(resource.RLIMIT_NOFILE, (102400, 102400)) dd = {} ls = [] for i in range(1 << 15): key = str(hashlib.sha1(str(i)).hexdigest()) lck = key print 'lock is', i, lck l = Portlock(lck, timeout=8) r = l.try_lock() if not r: print 'collide', i, l.addr print l.socks dd[l.addr] = i ls.append(l)
def startFactory(self): # disable coredumps if resource: resource.setrlimit(resource.RLIMIT_CORE, (0,0)) else: log.msg('INSECURE: unable to disable core dumps.') if not hasattr(self,'publicKeys'): self.publicKeys = self.getPublicKeys() if not hasattr(self,'privateKeys'): self.privateKeys = self.getPrivateKeys() if not self.publicKeys or not self.privateKeys: raise error.ConchError('no host keys, failing') if not hasattr(self,'primes'): self.primes = self.getPrimes() #if not self.primes: # log.msg('disabling diffie-hellman-group-exchange because we cannot find moduli file') # transport.SSHServerTransport.supportedKeyExchanges.remove('diffie-hellman-group-exchange-sha1') if self.primes: self.primesKeys = self.primes.keys()
def set_open_file_limit_up_to(limit=65536): import settings soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) limit = max(soft, limit) limit = min(limit, hard) while limit > soft: try: resource.setrlimit(resource.RLIMIT_NOFILE, (limit, hard)) break except ValueError: limit -= 256 except: settings.CONFIG_LOG.exception('unexpected exception') soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) settings.CONFIG_LOG.info('open file limit set to %d:%d', soft, hard) return (soft, hard)
def prevent_core_dump(): """ Prevent this process from generating a core dump. :return: ``None``. Set the soft and hard limits for core dump size to zero. On Unix, this entirely prevents the process from creating core dump. """ core_resource = resource.RLIMIT_CORE try: # Ensure the resource limit exists on this platform, by requesting # its current value. core_limit_prev = resource.getrlimit(core_resource) except ValueError as exc: error = DaemonOSEnvironmentError( "System does not support RLIMIT_CORE resource limit" " ({exc})".format(exc=exc)) raise error # Set hard and soft limits to zero, i.e. no core dump at all. core_limit = (0, 0) resource.setrlimit(core_resource, core_limit)
def __enter__(self): """Try to save previous ulimit, then set it to (0, 0).""" if resource is not None: try: self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) except (ValueError, resource.error): pass if sys.platform == 'darwin': # Check if the 'Crash Reporter' on OSX was configured # in 'Developer' mode and warn that it will get triggered # when it is. # # This assumes that this context manager is used in tests # that might trigger the next manager. value = subprocess.Popen(['/usr/bin/defaults', 'read', 'com.apple.CrashReporter', 'DialogType'], stdout=subprocess.PIPE).communicate()[0] if value.strip() == b'developer': print("this tests triggers the Crash Reporter, " "that is intentional", end='') sys.stdout.flush()
def test_urandom_failure(self): # Check urandom() failing when it is not able to open /dev/random. # We spawn a new process to make the test more robust (if getrlimit() # failed to restore the file descriptor limit after this, the whole # test suite would crash; this actually happened on the OS X Tiger # buildbot). code = """if 1: import errno import os import resource soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) try: os.urandom(16) except OSError as e: assert e.errno == errno.EMFILE, e.errno else: raise AssertionError("OSError not raised") """ assert_python_ok('-c', code)
def __enter__(self): """Try to save previous ulimit, then set it to (0, 0).""" if resource is not None: try: self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) except (ValueError, resource.error): pass if sys.platform == 'darwin': # Check if the 'Crash Reporter' on OSX was configured # in 'Developer' mode and warn that it will get triggered # when it is. # # This assumes that this context manager is used in tests # that might trigger the next manager. value = subprocess.Popen(['/usr/bin/defaults', 'read', 'com.apple.CrashReporter', 'DialogType'], stdout=subprocess.PIPE).communicate()[0] if value.strip() == b'developer': print "this tests triggers the Crash Reporter, that is intentional" sys.stdout.flush()
def _child_main(self): self.host.close() for fd in map(int, os.listdir('/proc/self/fd')): if fd != self.child.fileno(): try: os.close(fd) except OSError: pass resource.setrlimit(resource.RLIMIT_CPU, (1, 1)) prctl.set_seccomp(True) while True: sz, = struct.unpack('>L', read_exact(self.child, 4)) doc = marshal.loads(read_exact(self.child, sz)) if doc['cmd'] == 'eval': resp = self.do_eval(doc) elif doc['cmd'] == 'exit': _exit(0) goobs = marshal.dumps(resp) write_exact(self.child, struct.pack('>L', len(goobs))) write_exact(self.child, goobs)
def test_worst_case(self): sys.setrecursionlimit(10 ** 7) # max depth of recursion resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) # Worst case is a tree with one child per node, that is, a list test_input = [-1 if x == 0 else x - 1 for x in range(10 ** 5)] tree = tree_height.TreeHeight(len(test_input), test_input) assert len(test_input) == tree.compute_height() # def test_provided_input_data(self): # files_wildcard = os.path.dirname(__file__) + "/tree_height_test_input/*" # files = glob.glob(files_wildcard) # for file in files: # with open(file) as f: # size = f.readline() # parent = f.readline() # test_input = [int(x) for x in parent.split()] # tree = tree_height.TreeHeight(len(test_input), test_input)
def stdinpwn(self, payload): resource.setrlimit(resource.RLIMIT_STACK, (-1, -1)) resource.setrlimit(resource.RLIMIT_CORE, (-1, -1)) P = Popen(self.target, stdin=PIPE) print "[*] Sending buffer with lenght: "+str(len(payload)) P.stdin.write(payload) while True: line = sys.stdin.readline() P.poll() ret = P.returncode if ret is None: P.stdin.write(line) else: if ret == -11: print "[*] Child program crashed with SIGSEGV" else: print "[-] Child program exited with code %d" % ret break print "\n If it does not work automatically, run on terminal: (cat buffer.txt ; cat) | {}".format(self.target)
def test_huge_file_descriptor(self): from kazoo.handlers.threading import _HAS_EPOLL if not _HAS_EPOLL: self.skipTest('only run on systems with epoll()') import resource import socket from kazoo.handlers.utils import create_tcp_socket try: resource.setrlimit(resource.RLIMIT_NOFILE, (4096, 4096)) except (ValueError, resource.error): self.skipTest('couldnt raise fd limit high enough') fd = 0 socks = [] while fd < 4000: sock = create_tcp_socket(socket) fd = sock.fileno() socks.append(sock) h = self._makeOne() h.start() h.select(socks, [], []) with self.assertRaises(ValueError): h._select(socks, [], []) h._epoll_select(socks, [], []) h.stop()
def set_memory_limit(bytes, allow_override=True): """Limit memory consumption of current process to 'bytes'.""" if allow_override: try: bytes = int(os.environ["PKG_CLIENT_MAX_PROCESS_SIZE"]) except (KeyError, ValueError): pass try: resource.setrlimit(resource.RLIMIT_DATA, (bytes, bytes)) except AttributeError: # If platform doesn't support RLIMIT_DATA, just ignore it. pass except ValueError: # An unprivileged user can not raise a previously set limit, # if that ever happens, just ignore it. pass
def _setup_env(self): prefix = "/tmp/tracer_" curdir = os.getcwd() tmpdir = tempfile.mkdtemp(prefix=prefix) # allow cores to be dumped saved_limit = resource.getrlimit(resource.RLIMIT_CORE) resource.setrlimit(resource.RLIMIT_CORE, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) binaries_old = [ ] for binary in self._binaries: binaries_old.append(binary) binary_replacements = [ ] for i, binary in enumerate(self._binaries): binary_replacements.append(os.path.join(tmpdir,"binary_replacement_%d" % i)) for binary_o, binary_r in zip(binaries_old, binary_replacements): shutil.copy(binary_o, binary_r) self._binaries = binary_replacements if self.argv is not None and not self.is_multicb: self.argv = self._binaries + self.argv[1:] os.chdir(tmpdir) try: yield (tmpdir,binary_replacements) finally: assert tmpdir.startswith(prefix) shutil.rmtree(tmpdir) os.chdir(curdir) resource.setrlimit(resource.RLIMIT_CORE, saved_limit) self._binaries = binaries_old
def setUp(self): self.openSockets = [] if resource is not None: self.originalFileLimit = resource.getrlimit(resource.RLIMIT_NOFILE) resource.setrlimit(resource.RLIMIT_NOFILE, (128, self.originalFileLimit[1])) self.socketLimit = 256
def tearDown(self): while self.openSockets: self.openSockets.pop().close() if resource is not None: # OS X implicitly lowers the hard limit in the setrlimit call # above. Retrieve the new hard limit to pass in to this # setrlimit call, so that it doesn't give us a permission denied # error. currentHardLimit = resource.getrlimit(resource.RLIMIT_NOFILE)[1] newSoftLimit = min(self.originalFileLimit[0], currentHardLimit) resource.setrlimit(resource.RLIMIT_NOFILE, (newSoftLimit, currentHardLimit))
def test_above_fd_setsize(self): # A scalable implementation should have no problem with more than # FD_SETSIZE file descriptors. Since we don't know the value, we just # try to set the soft RLIMIT_NOFILE to the hard RLIMIT_NOFILE ceiling. soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) try: resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard)) self.addCleanup(resource.setrlimit, resource.RLIMIT_NOFILE, (soft, hard)) NUM_FDS = min(hard, 2**16) except (OSError, ValueError): NUM_FDS = soft # guard for already allocated FDs (stdin, stdout...) NUM_FDS -= 32 s = self.SELECTOR() self.addCleanup(s.close) for i in range(NUM_FDS // 2): try: rd, wr = self.make_socketpair() except OSError: # too many FDs, skip - note that we should only catch EMFILE # here, but apparently *BSD and Solaris can fail upon connect() # or bind() with EADDRNOTAVAIL, so let's be safe self.skipTest("FD limit reached") try: s.register(rd, selectors.EVENT_READ) s.register(wr, selectors.EVENT_WRITE) except OSError as e: if e.errno == errno.ENOSPC: # this can be raised by epoll if we go over # fs.epoll.max_user_watches sysctl self.skipTest("FD limit reached") raise self.assertEqual(NUM_FDS // 2, len(s.select()))
def setlimits(opts): if opts.memory_limit.isdigit(): m = int(opts.memory_limit) * 2**20 resource.setrlimit(resource.RLIMIT_AS, (m, m)) resource.setrlimit(resource.RLIMIT_DATA, (m, m)) resource.setrlimit(resource.RLIMIT_STACK, (m, m)) resource.setrlimit(resource.RLIMIT_RSS, (m, m))
def test_above_fd_setsize(self): # A scalable implementation should have no problem with more than # FD_SETSIZE file descriptors. Since we don't know the value, we just # try to set the soft RLIMIT_NOFILE to the hard RLIMIT_NOFILE ceiling. soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) if hard == resource.RLIM_INFINITY: self.skipTest("RLIMIT_NOFILE is infinite") try: # If we're on a *BSD system, the limit tag is different. _, bsd_hard = resource.getrlimit(resource.RLIMIT_OFILE) if bsd_hard == resource.RLIM_INFINITY: self.skipTest("RLIMIT_OFILE is infinite") if bsd_hard < hard: hard = bsd_hard # NOTE: AttributeError resource.RLIMIT_OFILE is not defined on Mac OS. except (OSError, resource.error, AttributeError): pass try: resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard)) self.addCleanup(resource.setrlimit, resource.RLIMIT_NOFILE, (soft, hard)) limit_nofile = min(hard, 2 ** 16) except (OSError, ValueError): limit_nofile = soft # Guard against already allocated FDs limit_nofile -= 256 limit_nofile = max(0, limit_nofile) s = self.make_selector() for i in range(limit_nofile // 2): rd, wr = self.make_socketpair() s.register(rd, selectors2.EVENT_READ) s.register(wr, selectors2.EVENT_WRITE) self.assertEqual(limit_nofile // 2, len(s.select()))
def _set_memory_limit(ml): resource.setrlimit(resource.RLIMIT_AS, (ml, ml)) # create a tmp dir in /dev/shm, chdir into it, set rlimit, save the current self.binary # at the end, it restores everything
def _setup_env(self): prefix = "/dev/shm/tracer_" curdir = os.getcwd() tmpdir = tempfile.mkdtemp(prefix=prefix) # dont prefilter the core if len(self.binaries) > 1: with open("/proc/self/coredump_filter", "wb") as f: f.write("00000077") # allow cores to be dumped saved_limit = resource.getrlimit(resource.RLIMIT_CORE) resource.setrlimit(resource.RLIMIT_CORE, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) binaries_old = [ ] for binary in self.binaries: binaries_old.append(os.path.abspath(binary)) self.binaries = list(binaries_old) os.chdir(tmpdir) try: yield (tmpdir, self.binaries[0]) finally: assert tmpdir.startswith(prefix) shutil.rmtree(tmpdir) os.chdir(curdir) resource.setrlimit(resource.RLIMIT_CORE, saved_limit) self.binaries = binaries_old
def set_max_runtime(seconds): # Install the signal handler and set a resource limit soft, hard = resource.getrlimit(resource.RLIMIT_CPU) resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard)) signal.signal(signal.SIGXCPU, time_exceeded)
def __init__(self,nvim): Base.__init__(self, nvim) self._snippet_engine = nvim.vars['cm_completed_snippet_engine'] # workaround for #62 try: import resource import psutil mem = psutil.virtual_memory() resource.setrlimit(resource.RLIMIT_DATA, (mem.total/3, resource.RLIM_INFINITY)) except Exception as ex: logger.exception("set RLIMIT_DATA failed. %s", ex) pass
def __exit__(self, *args): """Return core file behavior to default.""" if self.old_limit is None: return if resource is not None: try: resource.setrlimit(resource.RLIMIT_CORE, self.old_limit) except (ValueError, resource.error): pass
def run(self): # START LINUX: used to receive Memory Error faster in Linux try: import resource resource.setrlimit(resource.RLIMIT_AS, (2 ** 30, 2 ** 30)) # 2 ** 30 == 1GB in bytes except ImportError: pass # END LINUX: while True: exception = None self.stop.value = False script = self.consume.get() if script: help_globals = {'stop': self.stop} try: exec(script, help_globals) except BaseException as e: exc_type, exc_obj, exc_tb = sys.exc_info() exception = '{} {} {}'.format(exc_type, exc_obj, e.args) if exception: self.produce.put({'exception': exception}) else: self.produce.put({key: value for key, value in help_globals.items() if not callable(value) and # cannot be a function not isinstance(value, ModuleType) and # cannot be a module key not in ['__builtins__', 'stop']}) # cannot be builtins or synchronized objects del help_globals else: break
def setlimits(): # Set maximum CPU time to 90 seconds in child process try: import resource resource.setrlimit(resource.RLIMIT_CPU, (90, 90)) except: pass
def limit_resources(): _, hard = resource.getrlimit(resource.RLIMIT_NOFILE) resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard)) # XXX compare to max threads etc. _, hard = resource.getrlimit(resource.RLIMIT_AS) # RLIMIT_VMEM does not exist?! resource.setrlimit(resource.RLIMIT_AS, (16 * 1024 * 1024 * 1024, hard)) # XXX config
def setsplimits(self, cputime, memory): def _setlimits(): resource.setrlimit(resource.RLIMIT_CPU, cputime) resource.setrlimit(resource.RLIMIT_RSS, memory) resource.setrlimit(resource.RLIMIT_NPROC, (1024, 1024)) return _setlimits
def setsplimits(cputime, memory): def _setlimits(): resource.setrlimit(resource.RLIMIT_CPU, cputime) resource.setrlimit(resource.RLIMIT_RSS, memory) resource.setrlimit(resource.RLIMIT_NPROC, (2048, 2048)) return _setlimits # {"id": 1, "cmd": "bf", "args": [",[.,]", "asdasdf"]}
def setMemoryLimit(maxMem): if maxMem is None: maxMem = -1 try: resource.setrlimit(resource.RLIMIT_MEMLOCK, (1, maxMem)) return True except ValueError: return False
def raise_limits(): import resource try: _, hard = resource.getrlimit(resource.RLIMIT_NOFILE) info("Current limits, soft and hard : {} {}".format(_, hard)) resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard)) except ValueError: error("Exceeds limit {}, infinity is {}".format(hard, resource.RLIM_INFINITY)) except resource.error: return False except OSError as e: critical('You may need to check ulimit parameter: {}'.format(e)) raise e return True
def __enter__(self): """On Windows, disable Windows Error Reporting dialogs using SetErrorMode. On UNIX, try to save the previous core file size limit, then set soft limit to 0. """ if sys.platform.startswith('win'): # see http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621.aspx # GetErrorMode is not available on Windows XP and Windows Server 2003, # but SetErrorMode returns the previous value, so we can use that import ctypes self._k32 = ctypes.windll.kernel32 SEM_NOGPFAULTERRORBOX = 0x02 self.old_value = self._k32.SetErrorMode(SEM_NOGPFAULTERRORBOX) self._k32.SetErrorMode(self.old_value | SEM_NOGPFAULTERRORBOX) else: if resource is not None: try: self.old_value = resource.getrlimit(resource.RLIMIT_CORE) resource.setrlimit(resource.RLIMIT_CORE, (0, self.old_value[1])) except (ValueError, OSError): pass if sys.platform == 'darwin': # Check if the 'Crash Reporter' on OSX was configured # in 'Developer' mode and warn that it will get triggered # when it is. # # This assumes that this context manager is used in tests # that might trigger the next manager. value = subprocess.Popen(['/usr/bin/defaults', 'read', 'com.apple.CrashReporter', 'DialogType'], stdout=subprocess.PIPE).communicate()[0] if value.strip() == b'developer': print("this test triggers the Crash Reporter, " "that is intentional", end='', flush=True) return self
def __exit__(self, *ignore_exc): """Restore Windows ErrorMode or core file behavior to initial value.""" if self.old_value is None: return if sys.platform.startswith('win'): self._k32.SetErrorMode(self.old_value) else: if resource is not None: try: resource.setrlimit(resource.RLIMIT_CORE, self.old_value) except (ValueError, OSError): pass
def main(): global logger args = get_args() if args.debug: coloredlogs.install(level="DEBUG") logger.debug("Command arguments: %s" % args) # Adjust file limits from_limit = resource.getrlimit(resource.RLIMIT_NOFILE) (soft_limit, hard_limit) = from_limit soft_limit = min(10000, hard_limit) to_limit = (soft_limit, hard_limit) logger.debug("Raising open file limit from %s to %s" % (repr(from_limit), repr(to_limit))) resource.setrlimit(resource.RLIMIT_NOFILE, to_limit) try: result = modes.run(args) except KeyboardInterrupt: logger.critical("User abort") return 5 if result != 0: logger.error("Command failed") return result
def setup_class(cls): """ setup any state specific to the execution of the given class (which usually contains tests). """ sys.setrecursionlimit(10 ** 6) # max depth of recursion resource.setrlimit(resource.RLIMIT_STACK, (2 ** 27, 2 ** 27))