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