1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2015 Filipe Brandenburger
10 #include "alloc-util.h"
11 #include "cpu-set-util.h"
12 #include "extract-word.h"
15 #include "parse-util.h"
16 #include "string-util.h"
18 cpu_set_t
* cpu_set_malloc(unsigned *ncpus
) {
22 /* Allocates the cpuset in the right size */
29 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n
), c
) >= 0) {
30 CPU_ZERO_S(CPU_ALLOC_SIZE(n
), c
);
47 int parse_cpu_set_internal(
56 _cleanup_cpu_free_ cpu_set_t
*c
= NULL
;
57 const char *p
= rvalue
;
63 _cleanup_free_
char *word
= NULL
;
64 unsigned cpu
, cpu_lower
, cpu_upper
;
67 r
= extract_first_word(&p
, &word
, WHITESPACE
",", EXTRACT_QUOTES
);
69 return warn
? log_oom() : -ENOMEM
;
71 return warn
? log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, rvalue
) : r
;
76 c
= cpu_set_malloc(&ncpus
);
78 return warn
? log_oom() : -ENOMEM
;
81 r
= parse_range(word
, &cpu_lower
, &cpu_upper
);
83 return warn
? log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse CPU affinity '%s'", word
) : r
;
84 if (cpu_lower
>= ncpus
|| cpu_upper
>= ncpus
)
85 return warn
? log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
, "CPU out of range '%s' ncpus is %u", word
, ncpus
) : -EINVAL
;
87 if (cpu_lower
> cpu_upper
) {
89 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Range '%s' is invalid, %u > %u, ignoring", word
, cpu_lower
, cpu_upper
);
93 for (cpu
= cpu_lower
; cpu
<= cpu_upper
; cpu
++)
94 CPU_SET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
);
97 /* On success, sets *cpu_set and returns ncpus for the system. */
99 *cpu_set
= TAKE_PTR(c
);