21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Table 13-1. Resources that may be limited with setrlimit( )<br />

Resource Description<br />

RLIMIT_CORE Maximum size in bytes of a core file (see Recipe 1.9)<br />

RLIMIT_CPU Maximum amount of CPU time in seconds<br />

RLIMIT_DATA Maximum size in bytes of .data, .bss, and the heap<br />

RLIMIT_FSIZE Maximum size in bytes of a file<br />

RLIMIT_NOFILE Maximum number of open files per process<br />

RLIMIT_NPROC Maximum number of child processes per user ID<br />

RLIMIT_RSS Maximum resident set size in bytes<br />

RLIMIT_STACK Maximum size in bytes of the process stack<br />

RLIMIT_VMEM Maximum size in bytes of mapped memory<br />

The setrlimit( ) function has the following syntax:<br />

struct rlimit<br />

{<br />

rlim_t rlim_cur;<br />

rlim_t rlim_max;<br />

};<br />

int setrlimit(int resource, const struct rlimit *rlim);<br />

The resource parameter is one of the constants listed in Table 13-1. The programmer<br />

may increase or decrease the rlim_cur field at will; increasing the rlim_max field<br />

requires root privileges. For this reason, it is important to read the rlimit structure<br />

before modifying it in order to preserve the rlim_max field, thus allowing the system<br />

call to complete successfully. The current settings for rlim_cur and rlim_max can be<br />

obtained with the getrlimit( ) function, which has a similar signature to setrlimit( ):<br />

int getrlimit(int resource, struct rlimit *rlim);<br />

We’ve implemented a function here called spc_rsrclimit( ) that can be used to conveniently<br />

adjust the resource limits for the process that calls it. It does nothing more<br />

than make the necessary calls to getrlimit( ) and setrlimit( ). Note that the signal<br />

handlers have been left unimplemented because they will be application-specific.<br />

#include <br />

#include <br />

#include <br />

static int resources[ ] = {<br />

RLIMIT_CPU, RLIMIT_DATA, RLIMIT_STACK, RLIMIT_FSIZE,<br />

#ifdef RLIMIT_NPROC<br />

RLIMIT_NPROC,<br />

#endif<br />

#ifdef RLIMIT_NOFILE<br />

RLIMIT_NOFILE,<br />

#endif<br />

#ifdef RLIMIT_OFILE<br />

RLIMIT_OFILE,<br />

Guarding Against Resource Starvation Attacks on Unix | 729<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!