1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010-2015 Lennart Poettering
7 Copyright 2015 Filipe Brandenburger
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "alloc-util.h"
28 #include "cpu-set-util.h"
29 #include "extract-word.h"
32 #include "parse-util.h"
33 #include "string-util.h"
35 cpu_set_t
* cpu_set_malloc(unsigned *ncpus
) {
39 /* Allocates the cpuset in the right size */
46 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n
), c
) >= 0) {
47 CPU_ZERO_S(CPU_ALLOC_SIZE(n
), c
);
64 int parse_cpu_set_and_warn(
72 const char *whole_rvalue
= rvalue
;
73 _cleanup_cpu_free_ cpu_set_t
*c
= NULL
;
80 _cleanup_free_
char *word
= NULL
;
81 unsigned cpu
, cpu_lower
, cpu_upper
;
84 r
= extract_first_word(&rvalue
, &word
, WHITESPACE
",", EXTRACT_QUOTES
);
86 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid value for %s: %s", lvalue
, whole_rvalue
);
91 c
= cpu_set_malloc(&ncpus
);
96 r
= parse_range(word
, &cpu_lower
, &cpu_upper
);
98 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse CPU affinity '%s'", word
);
99 if (cpu_lower
>= ncpus
|| cpu_upper
>= ncpus
)
100 return log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
, "CPU out of range '%s' ncpus is %u", word
, ncpus
);
102 if (cpu_lower
> cpu_upper
)
103 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Range '%s' is invalid, %u > %u", word
, cpu_lower
, cpu_upper
);
105 for (cpu
= cpu_lower
; cpu
<= cpu_upper
; cpu
++)
106 CPU_SET_S(cpu
, CPU_ALLOC_SIZE(ncpus
), c
);
109 /* On success, sets *cpu_set and returns ncpus for the system. */