• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 13665439908

04 Mar 2025 09:54PM UTC coverage: 71.855% (+0.04%) from 71.819%
13665439908

push

github

poettering
dirent-util: add several assertions in posix_getdents()

Follow-up for e86a492ff.

4 of 4 new or added lines in 1 file covered. (100.0%)

8029 existing lines in 84 files now uncovered.

294783 of 410245 relevant lines covered (71.86%)

717882.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.8
/src/basic/rlimit-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4

5
#include "alloc-util.h"
6
#include "errno-util.h"
7
#include "extract-word.h"
8
#include "fd-util.h"
9
#include "fileio.h"
10
#include "format-util.h"
11
#include "macro.h"
12
#include "process-util.h"
13
#include "rlimit-util.h"
14
#include "string-table.h"
15
#include "strv.h"
16
#include "time-util.h"
17

18
int setrlimit_closest(int resource, const struct rlimit *rlim) {
45,558✔
19
        struct rlimit highest, fixed;
45,558✔
20

21
        assert(rlim);
45,558✔
22

23
        if (setrlimit(resource, rlim) >= 0)
45,558✔
24
                return 0;
45,558✔
25

26
        if (errno != EPERM)
223✔
27
                return -errno;
1✔
28

29
        /* So we failed to set the desired setrlimit, then let's try
30
         * to get as close as we can */
31
        if (getrlimit(resource, &highest) < 0)
222✔
UNCOV
32
                return -errno;
×
33

34
        /* If the hard limit is unbounded anyway, then the EPERM had other reasons, let's propagate the original EPERM
35
         * then */
36
        if (highest.rlim_max == RLIM_INFINITY)
222✔
37
                return -EPERM;
38

39
        fixed = (struct rlimit) {
222✔
40
                .rlim_cur = MIN(rlim->rlim_cur, highest.rlim_max),
222✔
41
                .rlim_max = MIN(rlim->rlim_max, highest.rlim_max),
222✔
42
        };
43

44
        /* Shortcut things if we wouldn't change anything. */
45
        if (fixed.rlim_cur == highest.rlim_cur &&
222✔
46
            fixed.rlim_max == highest.rlim_max)
47
                return 0;
48

49
        log_debug("Failed at setting rlimit " RLIM_FMT " for resource RLIMIT_%s. Will attempt setting value " RLIM_FMT " instead.", rlim->rlim_max, rlimit_to_string(resource), fixed.rlim_max);
112✔
50

51
        return RET_NERRNO(setrlimit(resource, &fixed));
112✔
52
}
53

54
int setrlimit_closest_all(const struct rlimit *const *rlim, int *which_failed) {
11,219✔
55
        int r;
11,219✔
56

57
        assert(rlim);
11,219✔
58

59
        /* On failure returns the limit's index that failed in *which_failed, but only if non-NULL */
60

61
        for (int i = 0; i < _RLIMIT_MAX; i++) {
190,723✔
62
                if (!rlim[i])
179,504✔
63
                        continue;
155,183✔
64

65
                r = setrlimit_closest(i, rlim[i]);
24,321✔
66
                if (r < 0) {
24,321✔
UNCOV
67
                        if (which_failed)
×
68
                                *which_failed = i;
×
69

UNCOV
70
                        return r;
×
71
                }
72
        }
73

74
        if (which_failed)
11,219✔
75
                *which_failed = -1;
11,219✔
76

77
        return 0;
78
}
79

80
static int rlimit_parse_u64(const char *val, rlim_t *ret) {
27,024✔
81
        uint64_t u;
27,024✔
82
        int r;
27,024✔
83

84
        assert(val);
27,024✔
85
        assert(ret);
27,024✔
86

87
        if (streq(val, "infinity")) {
27,024✔
88
                *ret = RLIM_INFINITY;
12✔
89
                return 0;
12✔
90
        }
91

92
        /* setrlimit(2) suggests rlim_t is always 64-bit on Linux. */
93
        assert_cc(sizeof(rlim_t) == sizeof(uint64_t));
27,012✔
94

95
        r = safe_atou64(val, &u);
27,012✔
96
        if (r < 0)
27,012✔
97
                return r;
98
        if (u >= (uint64_t) RLIM_INFINITY)
27,009✔
99
                return -ERANGE;
100

101
        *ret = (rlim_t) u;
27,009✔
102
        return 0;
27,009✔
103
}
104

105
static int rlimit_parse_size(const char *val, rlim_t *ret) {
13,510✔
106
        uint64_t u;
13,510✔
107
        int r;
13,510✔
108

109
        assert(val);
13,510✔
110
        assert(ret);
13,510✔
111

112
        if (streq(val, "infinity")) {
13,510✔
113
                *ret = RLIM_INFINITY;
6✔
114
                return 0;
6✔
115
        }
116

117
        r = parse_size(val, 1024, &u);
13,504✔
118
        if (r < 0)
13,504✔
119
                return r;
120
        if (u >= (uint64_t) RLIM_INFINITY)
13,504✔
121
                return -ERANGE;
122

123
        *ret = (rlim_t) u;
13,504✔
124
        return 0;
13,504✔
125
}
126

127
static int rlimit_parse_sec(const char *val, rlim_t *ret) {
15✔
128
        uint64_t u;
15✔
129
        usec_t t;
15✔
130
        int r;
15✔
131

132
        assert(val);
15✔
133
        assert(ret);
15✔
134

135
        if (streq(val, "infinity")) {
15✔
136
                *ret = RLIM_INFINITY;
1✔
137
                return 0;
1✔
138
        }
139

140
        r = parse_sec(val, &t);
14✔
141
        if (r < 0)
14✔
142
                return r;
143
        if (t == USEC_INFINITY) {
14✔
UNCOV
144
                *ret = RLIM_INFINITY;
×
145
                return 0;
×
146
        }
147

148
        u = (uint64_t) DIV_ROUND_UP(t, USEC_PER_SEC);
14✔
149
        if (u >= (uint64_t) RLIM_INFINITY)
14✔
150
                return -ERANGE;
151

152
        *ret = (rlim_t) u;
14✔
153
        return 0;
14✔
154
}
155

156
static int rlimit_parse_usec(const char *val, rlim_t *ret) {
12✔
157
        usec_t t;
12✔
158
        int r;
12✔
159

160
        assert(val);
12✔
161
        assert(ret);
12✔
162

163
        if (streq(val, "infinity")) {
12✔
164
                *ret = RLIM_INFINITY;
3✔
165
                return 0;
3✔
166
        }
167

168
        r = parse_time(val, &t, 1);
9✔
169
        if (r < 0)
9✔
170
                return r;
171
        if (t == USEC_INFINITY) {
9✔
UNCOV
172
                *ret = RLIM_INFINITY;
×
173
                return 0;
×
174
        }
175

176
        *ret = (rlim_t) t;
9✔
177
        return 0;
9✔
178
}
179

180
static int rlimit_parse_nice(const char *val, rlim_t *ret) {
23✔
181
        uint64_t rl;
23✔
182
        int r;
23✔
183

184
        /* So, Linux is weird. The range for RLIMIT_NICE is 40..1, mapping to the nice levels -20..19. However, the
185
         * RLIMIT_NICE limit defaults to 0 by the kernel, i.e. a value that maps to nice level 20, which of course is
186
         * bogus and does not exist. In order to permit parsing the RLIMIT_NICE of 0 here we hence implement a slight
187
         * asymmetry: when parsing as positive nice level we permit 0..19. When parsing as negative nice level, we
188
         * permit -20..0. But when parsing as raw resource limit value then we also allow the special value 0.
189
         *
190
         * Yeah, Linux is quality engineering sometimes... */
191

192
        if (val[0] == '+') {
23✔
193

194
                /* Prefixed with "+": Parse as positive user-friendly nice value */
195
                r = safe_atou64(val + 1, &rl);
4✔
196
                if (r < 0)
4✔
197
                        return r;
23✔
198

199
                if (rl >= PRIO_MAX)
4✔
200
                        return -ERANGE;
201

202
                rl = 20 - rl;
3✔
203

204
        } else if (val[0] == '-') {
19✔
205

206
                /* Prefixed with "-": Parse as negative user-friendly nice value */
207
                r = safe_atou64(val + 1, &rl);
4✔
208
                if (r < 0)
4✔
209
                        return r;
210

211
                if (rl > (uint64_t) (-PRIO_MIN))
4✔
212
                        return -ERANGE;
213

214
                rl = 20 + rl;
3✔
215
        } else {
216

217
                /* Not prefixed: parse as raw resource limit value */
218
                r = safe_atou64(val, &rl);
15✔
219
                if (r < 0)
15✔
220
                        return r;
221

222
                if (rl > (uint64_t) (20 - PRIO_MIN))
15✔
223
                        return -ERANGE;
224
        }
225

226
        *ret = (rlim_t) rl;
20✔
227
        return 0;
20✔
228
}
229

230
static int (*const rlimit_parse_table[_RLIMIT_MAX])(const char *val, rlim_t *ret) = {
231
        [RLIMIT_CPU] = rlimit_parse_sec,
232
        [RLIMIT_FSIZE] = rlimit_parse_size,
233
        [RLIMIT_DATA] = rlimit_parse_size,
234
        [RLIMIT_STACK] = rlimit_parse_size,
235
        [RLIMIT_CORE] = rlimit_parse_size,
236
        [RLIMIT_RSS] = rlimit_parse_size,
237
        [RLIMIT_NOFILE] = rlimit_parse_u64,
238
        [RLIMIT_AS] = rlimit_parse_size,
239
        [RLIMIT_NPROC] = rlimit_parse_u64,
240
        [RLIMIT_MEMLOCK] = rlimit_parse_size,
241
        [RLIMIT_LOCKS] = rlimit_parse_u64,
242
        [RLIMIT_SIGPENDING] = rlimit_parse_u64,
243
        [RLIMIT_MSGQUEUE] = rlimit_parse_size,
244
        [RLIMIT_NICE] = rlimit_parse_nice,
245
        [RLIMIT_RTPRIO] = rlimit_parse_u64,
246
        [RLIMIT_RTTIME] = rlimit_parse_usec,
247
};
248

249
int rlimit_parse_one(int resource, const char *val, rlim_t *ret) {
40,584✔
250
        assert(val);
40,584✔
251
        assert(ret);
40,584✔
252

253
        if (resource < 0)
40,584✔
254
                return -EINVAL;
255
        if (resource >= _RLIMIT_MAX)
40,584✔
256
                return -EINVAL;
257

258
        return rlimit_parse_table[resource](val, ret);
40,584✔
259
}
260

261
int rlimit_parse(int resource, const char *val, struct rlimit *ret) {
28,187✔
262
        _cleanup_free_ char *hard = NULL, *soft = NULL;
28,187✔
263
        rlim_t hl, sl;
28,187✔
264
        int r;
28,187✔
265

266
        assert(val);
28,187✔
267
        assert(ret);
28,187✔
268

269
        r = extract_first_word(&val, &soft, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
28,187✔
270
        if (r < 0)
28,187✔
271
                return r;
272
        if (r == 0)
28,187✔
273
                return -EINVAL;
274

275
        r = rlimit_parse_one(resource, soft, &sl);
28,187✔
276
        if (r < 0)
28,187✔
277
                return r;
278

279
        r = extract_first_word(&val, &hard, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
28,182✔
280
        if (r < 0)
28,182✔
281
                return r;
282
        if (!isempty(val))
56,369✔
283
                return -EINVAL;
284
        if (r == 0)
28,180✔
285
                hl = sl;
15,783✔
286
        else {
287
                r = rlimit_parse_one(resource, hard, &hl);
12,397✔
288
                if (r < 0)
12,397✔
289
                        return r;
290
                if (sl > hl)
12,396✔
291
                        return -EILSEQ;
292
        }
293

294
        *ret = (struct rlimit) {
28,177✔
295
                .rlim_cur = sl,
296
                .rlim_max = hl,
297
        };
298

299
        return 0;
28,177✔
300
}
301

302
int rlimit_format(const struct rlimit *rl, char **ret) {
17,118✔
303
        _cleanup_free_ char *s = NULL;
17,118✔
304
        int r;
17,118✔
305

306
        assert(rl);
17,118✔
307
        assert(ret);
17,118✔
308

309
        if (rl->rlim_cur >= RLIM_INFINITY && rl->rlim_max >= RLIM_INFINITY)
17,118✔
310
                r = free_and_strdup(&s, "infinity");
4,133✔
311
        else if (rl->rlim_cur >= RLIM_INFINITY)
12,985✔
312
                r = asprintf(&s, "infinity:" RLIM_FMT, rl->rlim_max);
1✔
313
        else if (rl->rlim_max >= RLIM_INFINITY)
12,984✔
314
                r = asprintf(&s, RLIM_FMT ":infinity", rl->rlim_cur);
1,184✔
315
        else if (rl->rlim_cur == rl->rlim_max)
11,800✔
316
                r = asprintf(&s, RLIM_FMT, rl->rlim_cur);
7,779✔
317
        else
318
                r = asprintf(&s, RLIM_FMT ":" RLIM_FMT, rl->rlim_cur, rl->rlim_max);
4,021✔
319
        if (r < 0)
17,118✔
320
                return -ENOMEM;
321

322
        *ret = TAKE_PTR(s);
17,118✔
323
        return 0;
17,118✔
324
}
325

326
static const char* const rlimit_table[_RLIMIT_MAX] = {
327
        [RLIMIT_AS]         = "AS",
328
        [RLIMIT_CORE]       = "CORE",
329
        [RLIMIT_CPU]        = "CPU",
330
        [RLIMIT_DATA]       = "DATA",
331
        [RLIMIT_FSIZE]      = "FSIZE",
332
        [RLIMIT_LOCKS]      = "LOCKS",
333
        [RLIMIT_MEMLOCK]    = "MEMLOCK",
334
        [RLIMIT_MSGQUEUE]   = "MSGQUEUE",
335
        [RLIMIT_NICE]       = "NICE",
336
        [RLIMIT_NOFILE]     = "NOFILE",
337
        [RLIMIT_NPROC]      = "NPROC",
338
        [RLIMIT_RSS]        = "RSS",
339
        [RLIMIT_RTPRIO]     = "RTPRIO",
340
        [RLIMIT_RTTIME]     = "RTTIME",
341
        [RLIMIT_SIGPENDING] = "SIGPENDING",
342
        [RLIMIT_STACK]      = "STACK",
343
};
344

345
DEFINE_STRING_TABLE_LOOKUP(rlimit, int);
74,456✔
346

347
int rlimit_from_string_harder(const char *s) {
54✔
348
        const char *suffix;
54✔
349

350
        /* The official prefix */
351
        suffix = startswith(s, "RLIMIT_");
54✔
352
        if (suffix)
54✔
353
                return rlimit_from_string(suffix);
17✔
354

355
        /* Our own unit file setting prefix */
356
        suffix = startswith(s, "Limit");
37✔
357
        if (suffix)
37✔
358
                return rlimit_from_string(suffix);
17✔
359

360
        return rlimit_from_string(s);
20✔
361
}
362

363
void rlimit_free_all(struct rlimit **rl) {
56,214✔
364
        free_many((void**) rl, _RLIMIT_MAX);
56,214✔
365
}
56,214✔
366

367
int rlimit_copy_all(struct rlimit* target[static _RLIMIT_MAX], struct rlimit* const source[static _RLIMIT_MAX]) {
273✔
368
        struct rlimit* copy[_RLIMIT_MAX] = {};
273✔
369

370
        assert(target);
273✔
371
        assert(source);
273✔
372

373
        for (int i = 0; i < _RLIMIT_MAX; i++) {
4,641✔
374
                if (!source[i])
4,368✔
375
                        continue;
3,822✔
376

377
                copy[i] = newdup(struct rlimit, source[i], 1);
546✔
378
                if (!copy[i]) {
546✔
UNCOV
379
                        rlimit_free_all(copy);
×
380
                        return -ENOMEM;
×
381
                }
382
        }
383

384
        memcpy(target, copy, sizeof(struct rlimit*) * _RLIMIT_MAX);
273✔
385
        return 0;
273✔
386
}
387

388
int rlimit_nofile_bump(int limit) {
20,730✔
389
        int r;
20,730✔
390

391
        /* Bumps the (soft) RLIMIT_NOFILE resource limit as close as possible to the specified limit. If a negative
392
         * limit is specified, bumps it to the maximum the kernel and the hard resource limit allows. This call should
393
         * be used by all our programs that might need a lot of fds, and that know how to deal with high fd numbers
394
         * (i.e. do not use select() — which chokes on fds >= 1024) */
395

396
        if (limit < 0)
20,730✔
397
                limit = read_nr_open();
110✔
398

399
        if (limit < 3)
20,730✔
UNCOV
400
                limit = 3;
×
401

402
        r = setrlimit_closest(RLIMIT_NOFILE, &RLIMIT_MAKE_CONST(limit));
20,730✔
403
        if (r < 0)
20,730✔
UNCOV
404
                return log_debug_errno(r, "Failed to set RLIMIT_NOFILE: %m");
×
405

406
        return 0;
407
}
408

409
int rlimit_nofile_safe(void) {
14,974✔
410
        struct rlimit rl;
14,974✔
411

412
        /* Resets RLIMIT_NOFILE's soft limit FD_SETSIZE (i.e. 1024), for compatibility with software still using
413
         * select() */
414

415
        if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
14,974✔
UNCOV
416
                return log_debug_errno(errno, "Failed to query RLIMIT_NOFILE: %m");
×
417

418
        if (rl.rlim_cur <= FD_SETSIZE)
14,974✔
419
                return 0;
420

421
        /* So we might have inherited a hard limit that's larger than the kernel's maximum limit as stored in
422
         * /proc/sys/fs/nr_open. If we pass this hard limit unmodified to setrlimit(), we'll get EPERM. To
423
         * make sure that doesn't happen, let's limit our hard limit to the value from nr_open. */
424
        rl.rlim_max = MIN(rl.rlim_max, (rlim_t) read_nr_open());
13,098✔
425
        rl.rlim_cur = MIN((rlim_t) FD_SETSIZE, rl.rlim_max);
13,098✔
426
        if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
13,098✔
UNCOV
427
                return log_debug_errno(errno, "Failed to lower RLIMIT_NOFILE's soft limit to " RLIM_FMT ": %m", rl.rlim_cur);
×
428

429
        return 1;
430
}
431

432
int pid_getrlimit(pid_t pid, int resource, struct rlimit *ret) {
1,194✔
433

434
        static const char * const prefix_table[_RLIMIT_MAX] = {
1,194✔
435
                [RLIMIT_CPU]        = "Max cpu time",
436
                [RLIMIT_FSIZE]      = "Max file size",
437
                [RLIMIT_DATA]       = "Max data size",
438
                [RLIMIT_STACK]      = "Max stack size",
439
                [RLIMIT_CORE]       = "Max core file size",
440
                [RLIMIT_RSS]        = "Max resident set",
441
                [RLIMIT_NPROC]      = "Max processes",
442
                [RLIMIT_NOFILE]     = "Max open files",
443
                [RLIMIT_MEMLOCK]    = "Max locked memory",
444
                [RLIMIT_AS]         = "Max address space",
445
                [RLIMIT_LOCKS]      = "Max file locks",
446
                [RLIMIT_SIGPENDING] = "Max pending signals",
447
                [RLIMIT_MSGQUEUE]   = "Max msgqueue size",
448
                [RLIMIT_NICE]       = "Max nice priority",
449
                [RLIMIT_RTPRIO]     = "Max realtime priority",
450
                [RLIMIT_RTTIME]     = "Max realtime timeout",
451
        };
452

453
        int r;
1,194✔
454

455
        assert(resource >= 0);
1,194✔
456
        assert(resource < _RLIMIT_MAX);
1,194✔
457
        assert(pid >= 0);
1,194✔
458
        assert(ret);
1,194✔
459

460
        if (pid == 0 || pid == getpid_cached())
1,194✔
461
                return RET_NERRNO(getrlimit(resource, ret));
1,194✔
462

463
        r = RET_NERRNO(prlimit(pid, resource, /* new_limit= */ NULL, ret));
1,194✔
464
        if (!ERRNO_IS_NEG_PRIVILEGE(r))
1,194✔
465
                return r;
1,154✔
466

467
        /* We don't have access? Then try to go via /proc/$PID/limits. Weirdly that's world readable in
468
         * contrast to querying the data via prlimit() */
469

470
        const char *p = procfs_file_alloca(pid, "limits");
40✔
471
        _cleanup_free_ char *limits = NULL;
40✔
472

473
        r = read_full_virtual_file(p, &limits, NULL);
40✔
474
        if (r < 0)
40✔
475
                return -EPERM; /* propagate original permission error if we can't access the limits file */
476

477
        _cleanup_strv_free_ char **l = NULL;
40✔
478
        l = strv_split(limits, "\n");
40✔
479
        if (!l)
40✔
480
                return -ENOMEM;
481

482
        STRV_FOREACH(i, strv_skip(l, 1)) {
364✔
483
                _cleanup_free_ char *soft = NULL, *hard = NULL;
364✔
484
                uint64_t sv, hv;
364✔
485
                const char *e;
364✔
486

487
                e = startswith(*i, prefix_table[resource]);
364✔
488
                if (!e)
364✔
489
                        continue;
324✔
490

491
                if (*e != ' ')
40✔
UNCOV
492
                        continue;
×
493

494
                e += strspn(e, WHITESPACE);
40✔
495

496
                size_t n;
40✔
497
                n = strcspn(e, WHITESPACE);
40✔
498
                if (n == 0)
40✔
UNCOV
499
                        continue;
×
500

501
                soft = strndup(e, n);
40✔
502
                if (!soft)
40✔
503
                        return -ENOMEM;
504

505
                e += n;
40✔
506
                if (*e != ' ')
40✔
UNCOV
507
                        continue;
×
508

509
                e += strspn(e, WHITESPACE);
40✔
510
                n = strcspn(e, WHITESPACE);
40✔
511
                if (n == 0)
40✔
UNCOV
512
                        continue;
×
513

514
                hard = strndup(e, n);
40✔
515
                if (!hard)
40✔
516
                        return -ENOMEM;
517

518
                if (streq(soft, "unlimited"))
40✔
519
                        sv = RLIM_INFINITY;
8✔
520
                else {
521
                        r = safe_atou64(soft, &sv);
32✔
522
                        if (r < 0)
32✔
523
                                return r;
524
                }
525

526
                if (streq(hard, "unlimited"))
40✔
527
                        hv = RLIM_INFINITY;
9✔
528
                else {
529
                        r = safe_atou64(hard, &hv);
31✔
530
                        if (r < 0)
31✔
531
                                return r;
532
                }
533

534
                *ret = (struct rlimit) {
40✔
535
                        .rlim_cur = sv,
536
                        .rlim_max = hv,
537
                };
538

539
                return 0;
40✔
540
        }
541

542
        return -ENOTRECOVERABLE;
543
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc