21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

#endif<br />

-1<br />

};<br />

void spc_rsrclimit(int max_cpu, int max_data, int max_stack, int max_fsize,<br />

int max_proc, int max_files) {<br />

int limit, *resource;<br />

struct rlimit r;<br />

for (resource = resources; *resource >= 0; resource++) {<br />

switch (*resource) {<br />

case RLIMIT_CPU: limit = max_cpu; break;<br />

case RLIMIT_DATA: limit = max_data; break;<br />

case RLIMIT_STACK: limit = max_stack; break;<br />

case RLIMIT_FSIZE: limit = max_fsize; break;<br />

#ifdef RLIMIT_NPROC<br />

case RLIMIT_NPROC: limit = max_proc; break;<br />

#endif<br />

#ifdef RLIMIT_NOFILE<br />

case RLIMIT_NOFILE: limit = max_files; break;<br />

#endif<br />

#ifdef RLIMIT_OFILE<br />

case RLIMIT_OFILE: limit = max_files; break;<br />

#endif<br />

}<br />

getrlimit(*resource, &r);<br />

r.rlim_cur = (limit < r.rlim_max ? limit : r.rlim_max);<br />

setrlimit(*resource, &r);<br />

}<br />

}<br />

See Also<br />

Recipe 1.9<br />

13.10 Guarding Against Resource Starvation<br />

Attacks on Windows<br />

<strong>Problem</strong><br />

You need to prevent resource starvation attacks against your application.<br />

Solution<br />

As we noted in the previous recipe, the operating system does not trust the applications<br />

that it allows to run. For this reason, the operating system imposes limits on<br />

certain resources. The limitations are imposed to prevent an application from using<br />

up all of the available system resources, thus denying other running applications the<br />

730 | Chapter 13: Other Topics<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!