57
* log the limit at debug level
59
static void chassis_fdlimit_log(struct rlimit *limit, const char *msg) {
60
if (RLIM_INFINITY == limit->rlim_max && RLIM_INFINITY == limit->rlim_cur) {
61
g_debug("%s: %s = unlimited (hard: unlimited)",
64
} else if (RLIM_INFINITY == limit->rlim_max) {
65
g_debug("%s: %s = %"G_RLIM_T_FORMAT" (hard: unlimited)",
69
} else if (RLIM_INFINITY == limit->rlim_cur) {
70
g_debug("%s: %s = unlimited (hard: %"G_RLIM_T_FORMAT")",
75
g_debug("%s: %s = %"G_RLIM_T_FORMAT" (hard: %"G_RLIM_T_FORMAT")",
55
84
int chassis_set_fdlimit(int max_files_number) {
57
86
g_debug("%s: current maximum number of open stdio file descriptors: %d", G_STRLOC, _getmaxstdio());
72
101
G_STRLOC, g_strerror(errno), errno);
74
103
rlim_t soft_limit = max_files_rlimit.rlim_cur;
75
g_debug("%s: current RLIMIT_NOFILE = %"G_RLIM_T_FORMAT" (hard: %"G_RLIM_T_FORMAT")", G_STRLOC, max_files_rlimit.rlim_cur, max_files_rlimit.rlim_max);
104
rlim_t hard_limit = max_files_rlimit.rlim_max;
106
chassis_fdlimit_log(&max_files_rlimit, "current RLIMIT_NOFILE");
77
108
max_files_rlimit.rlim_cur = max_files_number;
109
if (hard_limit < max_files_number) { /* raise the hard-limit too in case it is smaller than the soft-limit, otherwise we get a EINVAL */
110
max_files_rlimit.rlim_max = max_files_number;
79
g_debug("%s: trying to set new RLIMIT_NOFILE = %"G_RLIM_T_FORMAT" (hard: %"G_RLIM_T_FORMAT")", G_STRLOC, max_files_rlimit.rlim_cur, max_files_rlimit.rlim_max);
113
chassis_fdlimit_log(&max_files_rlimit, "setting RLIMIT_NOFILE");
80
114
if (-1 == setrlimit(RLIMIT_NOFILE, &max_files_rlimit)) {
81
g_critical("%s: could not raise RLIMIT_NOFILE to %u, %s (%d). Current limit still %"G_RLIM_T_FORMAT".", G_STRLOC, max_files_number, g_strerror(errno), errno, soft_limit);
115
g_critical("%s: could not raise RLIMIT_NOFILE to %u, %s (%d). Current limit still %"G_RLIM_T_FORMAT".",
118
g_strerror(errno), errno,
83
121
if (-1 == getrlimit(RLIMIT_NOFILE, &max_files_rlimit)) {
84
122
g_warning("%s: cannot get limit of open files for this process. %s (%d)",
85
123
G_STRLOC, g_strerror(errno), errno);
87
g_debug("%s: set new RLIMIT_NOFILE = %"G_RLIM_T_FORMAT" (hard: %"G_RLIM_T_FORMAT")", G_STRLOC, max_files_rlimit.rlim_cur, max_files_rlimit.rlim_max);
125
chassis_fdlimit_log(&max_files_rlimit, "successfully set RLIMIT_NOFILE");