1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
| | /*
* Copyright (C) 2012-2015 all contributors <cmogstored-public@bogomips.org>
* License: GPLv3 or later (see COPYING for details)
*/
#include "cmogstored.h"
#include <sys/time.h>
#include <sys/resource.h>
#ifndef RLIM_INFINITY
# define RLIM_INFINITY ((rlim_t)(-1))
#endif
void mog_set_maxconns(unsigned long maxconns)
{
struct rlimit r;
rlim_t want;
struct rlimit orig;
if (getrlimit(RLIMIT_NOFILE, &r) != 0)
die_errno("getrlimit(RLIMIT_NOFILE) failed");
memcpy(&orig, &r, sizeof(struct rlimit));
if (maxconns == 0)
maxconns = MOG_DEFAULT_MAXCONNS;
want = maxconns;
if ((int)want < 0 || want > MOG_FD_MAX)
want = MOG_FD_MAX; /* LOL :D */
if (r.rlim_cur >= want)
return;
if (r.rlim_max == RLIM_INFINITY || r.rlim_cur == RLIM_INFINITY) {
/* insane? maybe... */
r.rlim_max = r.rlim_cur = want;
} else if (r.rlim_max == 0) {
warn("RLIMIT_NOFILE max=0, trying %ld anyways", (long)want);
r.rlim_max = r.rlim_cur = want;
} else if (r.rlim_max < want) {
warn("RLIMIT_NOFILE max=%ld less than wanted value=%ld",
(long)r.rlim_max, (long)want);
r.rlim_cur = r.rlim_max;
} else {
r.rlim_max = r.rlim_cur = want;
}
if (setrlimit(RLIMIT_NOFILE, &r) == 0) return;
warn("failed to set RLIMIT_NOFILE max=%ld cur=%ld (maxconns=%lu)",
(long)r.rlim_max, (long)r.rlim_cur, maxconns);
while ((want -= 64) >= maxconns) {
r.rlim_max = r.rlim_cur = want;
if (setrlimit(RLIMIT_NOFILE, &r) == 0)
goto eventual_success;
}
warn("RLIMIT_NOFILE stuck at max=%ld cur=%ld (maxconns=%lu)",
(long)orig.rlim_max, (long)orig.rlim_cur, maxconns);
return;
eventual_success:
warn("set RLIMIT_NOFILE max=%ld cur=%ld (maxconns=%lu)",
(long)r.rlim_max, (long)r.rlim_cur, maxconns);
}
|