]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/cpu-set-util.c
Merge pull request #8575 from keszybz/non-absolute-paths
[thirdparty/systemd.git] / src / basic / cpu-set-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
618234a5
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2010-2015 Lennart Poettering
6 Copyright 2015 Filipe Brandenburger
618234a5
LP
7***/
8
11c3a366
TA
9#include <errno.h>
10#include <stddef.h>
11#include <syslog.h>
12
b5efdb8a 13#include "alloc-util.h"
6bedfcbb 14#include "cpu-set-util.h"
84ac7bea 15#include "extract-word.h"
11c3a366
TA
16#include "log.h"
17#include "macro.h"
93cc7779 18#include "parse-util.h"
b11d6a7b 19#include "string-util.h"
618234a5
LP
20
21cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
22 cpu_set_t *c;
23 unsigned n = 1024;
24
25 /* Allocates the cpuset in the right size */
26
27 for (;;) {
28 c = CPU_ALLOC(n);
29 if (!c)
30 return NULL;
31
32 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
33 CPU_ZERO_S(CPU_ALLOC_SIZE(n), c);
34
35 if (ncpus)
36 *ncpus = n;
37
38 return c;
39 }
40
41 CPU_FREE(c);
42
43 if (errno != EINVAL)
44 return NULL;
45
46 n *= 2;
47 }
48}
49
6d8a29b2 50int parse_cpu_set_internal(
618234a5
LP
51 const char *rvalue,
52 cpu_set_t **cpu_set,
6d8a29b2 53 bool warn,
618234a5
LP
54 const char *unit,
55 const char *filename,
56 unsigned line,
57 const char *lvalue) {
58
618234a5 59 _cleanup_cpu_free_ cpu_set_t *c = NULL;
6d8a29b2 60 const char *p = rvalue;
618234a5
LP
61 unsigned ncpus = 0;
62
618234a5
LP
63 assert(rvalue);
64
65 for (;;) {
66 _cleanup_free_ char *word = NULL;
a26662ce 67 unsigned cpu, cpu_lower, cpu_upper;
618234a5
LP
68 int r;
69
6d8a29b2
YW
70 r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_QUOTES);
71 if (r == -ENOMEM)
72 return warn ? log_oom() : -ENOMEM;
a26662ce 73 if (r < 0)
6d8a29b2 74 return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, rvalue) : r;
618234a5
LP
75 if (r == 0)
76 break;
77
78 if (!c) {
79 c = cpu_set_malloc(&ncpus);
80 if (!c)
6d8a29b2 81 return warn ? log_oom() : -ENOMEM;
618234a5
LP
82 }
83
a26662ce
FB
84 r = parse_range(word, &cpu_lower, &cpu_upper);
85 if (r < 0)
6d8a29b2 86 return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word) : r;
a26662ce 87 if (cpu_lower >= ncpus || cpu_upper >= ncpus)
6d8a29b2 88 return warn ? log_syntax(unit, LOG_ERR, filename, line, EINVAL, "CPU out of range '%s' ncpus is %u", word, ncpus) : -EINVAL;
032cf8e4 89
6d8a29b2
YW
90 if (cpu_lower > cpu_upper) {
91 if (warn)
92 log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u, ignoring", word, cpu_lower, cpu_upper);
93 continue;
032cf8e4
YW
94 }
95
6d8a29b2
YW
96 for (cpu = cpu_lower; cpu <= cpu_upper; cpu++)
97 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
032cf8e4
YW
98 }
99
100 /* On success, sets *cpu_set and returns ncpus for the system. */
1cc6c93a
YW
101 if (c)
102 *cpu_set = TAKE_PTR(c);
032cf8e4
YW
103
104 return (int) ncpus;
105}