1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010-2015 Lennart Poettering
6 Copyright 2015 Filipe Brandenburger
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
27 #include "cpu-set-util.h"
28 #include "extract-word.h"
31 #include "parse-util.h"
32 #include "string-util.h"
34 cpu_set_t
* cpu_set_malloc(unsigned *ncpus
) {
38 /* Allocates the cpuset in the right size */
45 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n
), c
) >= 0) {
46 CPU_ZERO_S(CPU_ALLOC_SIZE(n
), c
);
63 int parse_cpu_set_internal(
72 _cleanup_cpu_free_ cpu_set_t
*c
= NULL
;
73 const char *p
= rvalue
;
79 _cleanup_free_
char *word
= NULL
;
80 unsigned cpu
, cpu_lower
, cpu_upper
;
83 r
= extract_first_word(&p
, &word
, WHITESPACE
",", EXTRACT_QUOTES
);
85 return warn
? log_oom() : -ENOMEM
;
87 return warn
? log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, rvalue
) : r
;
92 c
= cpu_set_malloc(&ncpus
);
94 return warn
? log_oom() : -ENOMEM
;
97 r
= parse_range(word
, &cpu_lower
, &cpu_upper
);
99 return warn
? log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse CPU affinity '%s'", word
) : r
;
100 if (cpu_lower
>= ncpus
|| cpu_upper
>= ncpus
)
101 return warn
? log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
, "CPU out of range '%s' ncpus is %u", word
, ncpus
) : -EINVAL
;
103 if (cpu_lower
> cpu_upper
) {
105 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Range '%s' is invalid, %u > %u, ignoring", word
, cpu_lower
, cpu_upper
);
109 for (cpu
= cpu_lower
; cpu
<= cpu_upper
; cpu
++)
110 CPU_SET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
);
113 /* On success, sets *cpu_set and returns ncpus for the system. */
115 *cpu_set
= TAKE_PTR(c
);