1 /*
2     This file is part of darktable,
3     Copyright (C) 2016-2020 darktable developers.
4 
5     darktable is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     darktable is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with darktable.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "common/resource_limits.h"
24 #include <assert.h>       // for assert
25 #include <errno.h>        // for errno
26 #include <stdint.h>       // for uintmax_t
27 #include <stdio.h>        // for fprintf, stderr
28 #include <string.h>       // for strerror
29 #include <inttypes.h>
30 
31 #ifdef _WIN32
32 #include "win/rlimit.h"
33 #else
34 #include <sys/resource.h> // for rlimit, RLIMIT_STACK, getrlimit, setrlimit
35 #endif //_WIN32
36 
dt_set_rlimits_stack()37 static void dt_set_rlimits_stack()
38 {
39   // make sure that stack/frame limits are good (musl)
40 
41   int ret;
42 
43   struct rlimit rlim = { 0 };
44 
45   ret = getrlimit(RLIMIT_STACK, &rlim);
46 
47   if(ret != 0)
48   {
49     const int errsv = errno;
50     fprintf(stderr, "[dt_set_rlimits_stack] error: getrlimit(RLIMIT_STACK) returned %i: %i (%s)\n", ret, errsv,
51             strerror(errsv));
52   }
53 
54   assert((ret == 0 && (WANTED_STACK_SIZE <= rlim.rlim_max || RLIM_INFINITY == rlim.rlim_max)) || (ret != 0));
55 
56   if(ret != 0 || (rlim.rlim_cur < WANTED_STACK_SIZE && rlim.rlim_cur != RLIM_INFINITY) /*|| 1*/)
57   {
58     // looks like we need to bump/set it...
59 
60     fprintf(stderr, "[dt_set_rlimits_stack] info: bumping RLIMIT_STACK rlim_cur from %"PRIuMAX" to %"PRIuMAX"\n",
61             (uintmax_t)rlim.rlim_cur, (uintmax_t)WANTED_STACK_SIZE);
62 
63     rlim.rlim_cur = WANTED_STACK_SIZE;
64 
65     ret = setrlimit(RLIMIT_STACK, &rlim);
66     if(ret != 0)
67     {
68       int errsv = errno;
69       fprintf(stderr, "[dt_set_rlimits_stack] error: setrlimit(RLIMIT_STACK) returned %i: %i (%s)\n", ret, errsv,
70               strerror(errsv));
71     }
72   }
73 }
74 
dt_set_rlimits()75 void dt_set_rlimits()
76 {
77   dt_set_rlimits_stack();
78 }
79 
80 
81 // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
82 // vim: shiftwidth=2 expandtab tabstop=2 cindent
83 // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
84