1*e4b17023SJohn Marino /* Increase stack size limit if possible.
2*e4b17023SJohn Marino Copyright (C) 2011 Free Software Foundation, Inc.
3*e4b17023SJohn Marino
4*e4b17023SJohn Marino This file is part of the libiberty library. This library is free
5*e4b17023SJohn Marino software; you can redistribute it and/or modify it under the
6*e4b17023SJohn Marino terms of the GNU General Public License as published by the
7*e4b17023SJohn Marino Free Software Foundation; either version 2, or (at your option)
8*e4b17023SJohn Marino any later version.
9*e4b17023SJohn Marino
10*e4b17023SJohn Marino This library is distributed in the hope that it will be useful,
11*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
12*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*e4b17023SJohn Marino GNU General Public License for more details.
14*e4b17023SJohn Marino
15*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
16*e4b17023SJohn Marino along with GNU CC; see the file COPYING. If not, write to
17*e4b17023SJohn Marino the Free Software Foundation, 51 Franklin Street - Fifth Floor,
18*e4b17023SJohn Marino Boston, MA 02110-1301, USA.
19*e4b17023SJohn Marino
20*e4b17023SJohn Marino As a special exception, if you link this library with files
21*e4b17023SJohn Marino compiled with a GNU compiler to produce an executable, this does not cause
22*e4b17023SJohn Marino the resulting executable to be covered by the GNU General Public License.
23*e4b17023SJohn Marino This exception does not however invalidate any other reasons why
24*e4b17023SJohn Marino the executable file might be covered by the GNU General Public License. */
25*e4b17023SJohn Marino
26*e4b17023SJohn Marino /*
27*e4b17023SJohn Marino
28*e4b17023SJohn Marino @deftypefn Extension void stack_limit_increase (unsigned long @var{pref})
29*e4b17023SJohn Marino
30*e4b17023SJohn Marino Attempt to increase stack size limit to @var{pref} bytes if possible.
31*e4b17023SJohn Marino
32*e4b17023SJohn Marino @end deftypefn
33*e4b17023SJohn Marino
34*e4b17023SJohn Marino */
35*e4b17023SJohn Marino
36*e4b17023SJohn Marino #include "config.h"
37*e4b17023SJohn Marino
38*e4b17023SJohn Marino #ifdef HAVE_STDINT_H
39*e4b17023SJohn Marino #include <stdint.h>
40*e4b17023SJohn Marino #endif
41*e4b17023SJohn Marino #ifdef HAVE_SYS_RESOURCE_H
42*e4b17023SJohn Marino #include <sys/resource.h>
43*e4b17023SJohn Marino #endif
44*e4b17023SJohn Marino
45*e4b17023SJohn Marino void
stack_limit_increase(unsigned long pref)46*e4b17023SJohn Marino stack_limit_increase (unsigned long pref)
47*e4b17023SJohn Marino {
48*e4b17023SJohn Marino #if defined(HAVE_SETRLIMIT) && defined(HAVE_GETRLIMIT) \
49*e4b17023SJohn Marino && defined(RLIMIT_STACK) && defined(RLIM_INFINITY)
50*e4b17023SJohn Marino struct rlimit rlim;
51*e4b17023SJohn Marino if (getrlimit (RLIMIT_STACK, &rlim) == 0
52*e4b17023SJohn Marino && rlim.rlim_cur != RLIM_INFINITY
53*e4b17023SJohn Marino && rlim.rlim_cur < pref
54*e4b17023SJohn Marino && (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_cur < rlim.rlim_max))
55*e4b17023SJohn Marino {
56*e4b17023SJohn Marino rlim.rlim_cur = pref;
57*e4b17023SJohn Marino if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_cur > rlim.rlim_max)
58*e4b17023SJohn Marino rlim.rlim_cur = rlim.rlim_max;
59*e4b17023SJohn Marino setrlimit (RLIMIT_STACK, &rlim);
60*e4b17023SJohn Marino }
61*e4b17023SJohn Marino #endif
62*e4b17023SJohn Marino }
63