Debian Codegraph @

src/common/rlimit.cpp

raw symbols SCIP
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17 #include "common/setup_before.h"
18 #include "rlimit.h"
19
20 #include <cerrno>
21 #include <cstring>
22
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26 #ifdef HAVE_SYS_RESOURCE_H
27 # include <sys/resource.h>
28 #endif
29
30 #include "common/eventlog.h"
31 #include "common/bn_type.h"
32 #include "common/setup_after.h"
33
34
35 /* file descriptor limit has different name on BSD and Linux */
36 #ifdef RLIMIT_NOFILE
37 # define RLIM_NUMFILES RLIMIT_NOFILE
38 #else
39 # ifdef RLIMIT_OFILE
40 # define RLIM_NUMFILES RLIMIT_OFILE
41 # endif
42 #endif
43
44 namespace pvpgn
45 {
46
47 extern int get_socket_limit(void)
48 {
49 int socklimit = 0;
50 #ifdef HAVE_GETRLIMIT
51 struct rlimit rlim;
52 if (getrlimit(RLIM_NUMFILES, &rlim) < 0)
53 eventlog(eventlog_level_error, __FUNCTION__, "getrlimit returned error: {}", std::strerror(errno));
54 socklimit = rlim.rlim_cur;
55 #else
56 /* FIXME: WIN32: somehow get WSAData win32 socket limit here */
57 #endif
58
59 #if !(defined HAVE_POLL || defined HAVE_KQUEUE || defined HAVE_EPOLL)
60 if (!socklimit || FD_SETSIZE < socklimit)
61 socklimit = FD_SETSIZE;
62 #endif
63
64 /* make socket limit smaller than file limit to make sure log files,
65 db connections and save files will still work */
66 socklimit -= 64;
67
68 return socklimit;
69 }
70
71 }