]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/cpu-set-util.c
Merge pull request #1768 from vcaputo/sd-daemon-listen-fds-overflow-bis
[thirdparty/systemd.git] / src / basic / cpu-set-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010-2015 Lennart Poettering
7 Copyright 2015 Filipe Brandenburger
8
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.
13
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.
18
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/>.
21 ***/
22
23 #include "alloc-util.h"
24 #include "cpu-set-util.h"
25 #include "extract-word.h"
26 #include "parse-util.h"
27 #include "string-util.h"
28 #include "util.h"
29
30 cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
31 cpu_set_t *c;
32 unsigned n = 1024;
33
34 /* Allocates the cpuset in the right size */
35
36 for (;;) {
37 c = CPU_ALLOC(n);
38 if (!c)
39 return NULL;
40
41 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
42 CPU_ZERO_S(CPU_ALLOC_SIZE(n), c);
43
44 if (ncpus)
45 *ncpus = n;
46
47 return c;
48 }
49
50 CPU_FREE(c);
51
52 if (errno != EINVAL)
53 return NULL;
54
55 n *= 2;
56 }
57 }
58
59 int parse_cpu_set_and_warn(
60 const char *rvalue,
61 cpu_set_t **cpu_set,
62 const char *unit,
63 const char *filename,
64 unsigned line,
65 const char *lvalue) {
66
67 const char *whole_rvalue = rvalue;
68 _cleanup_cpu_free_ cpu_set_t *c = NULL;
69 unsigned ncpus = 0;
70
71 assert(lvalue);
72 assert(rvalue);
73
74 for (;;) {
75 _cleanup_free_ char *word = NULL;
76 unsigned cpu, cpu_lower, cpu_upper;
77 int r;
78
79 r = extract_first_word(&rvalue, &word, WHITESPACE ",", EXTRACT_QUOTES);
80 if (r < 0)
81 return log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue);
82 if (r == 0)
83 break;
84
85 if (!c) {
86 c = cpu_set_malloc(&ncpus);
87 if (!c)
88 return log_oom();
89 }
90
91 r = parse_range(word, &cpu_lower, &cpu_upper);
92 if (r < 0)
93 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word);
94 if (cpu_lower >= ncpus || cpu_upper >= ncpus)
95 return log_syntax(unit, LOG_ERR, filename, line, EINVAL, "CPU out of range '%s' ncpus is %u", word, ncpus);
96
97 if (cpu_lower > cpu_upper)
98 log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u", word, cpu_lower, cpu_upper);
99 else
100 for (cpu = cpu_lower; cpu <= cpu_upper; cpu++)
101 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
102 }
103
104 /* On success, sets *cpu_set and returns ncpus for the system. */
105 if (c) {
106 *cpu_set = c;
107 c = NULL;
108 }
109
110 return (int) ncpus;
111 }