1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "cpu-set-util.h"
9 #include "extract-word.h"
12 #include "parse-util.h"
13 #include "string-util.h"
15 cpu_set_t
* cpu_set_malloc(unsigned *ncpus
) {
19 /* Allocates the cpuset in the right size */
26 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n
), c
) >= 0) {
27 CPU_ZERO_S(CPU_ALLOC_SIZE(n
), c
);
44 int parse_cpu_set_internal(
53 _cleanup_cpu_free_ cpu_set_t
*c
= NULL
;
54 const char *p
= rvalue
;
60 _cleanup_free_
char *word
= NULL
;
61 unsigned cpu
, cpu_lower
, cpu_upper
;
64 r
= extract_first_word(&p
, &word
, WHITESPACE
",", EXTRACT_QUOTES
);
66 return warn
? log_oom() : -ENOMEM
;
68 return warn
? log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, rvalue
) : r
;
73 c
= cpu_set_malloc(&ncpus
);
75 return warn
? log_oom() : -ENOMEM
;
78 r
= parse_range(word
, &cpu_lower
, &cpu_upper
);
80 return warn
? log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse CPU affinity '%s'", word
) : r
;
81 if (cpu_lower
>= ncpus
|| cpu_upper
>= ncpus
)
82 return warn
? log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
, "CPU out of range '%s' ncpus is %u", word
, ncpus
) : -EINVAL
;
84 if (cpu_lower
> cpu_upper
) {
86 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Range '%s' is invalid, %u > %u, ignoring", word
, cpu_lower
, cpu_upper
);
90 for (cpu
= cpu_lower
; cpu
<= cpu_upper
; cpu
++)
91 CPU_SET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
);
94 /* On success, sets *cpu_set and returns ncpus for the system. */
96 *cpu_set
= TAKE_PTR(c
);