]>
Commit | Line | Data |
---|---|---|
618234a5 LP |
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 | ||
b5efdb8a | 23 | #include "alloc-util.h" |
6bedfcbb | 24 | #include "cpu-set-util.h" |
84ac7bea | 25 | #include "extract-word.h" |
6bedfcbb | 26 | #include "parse-util.h" |
618234a5 | 27 | #include "util.h" |
618234a5 LP |
28 | |
29 | cpu_set_t* cpu_set_malloc(unsigned *ncpus) { | |
30 | cpu_set_t *c; | |
31 | unsigned n = 1024; | |
32 | ||
33 | /* Allocates the cpuset in the right size */ | |
34 | ||
35 | for (;;) { | |
36 | c = CPU_ALLOC(n); | |
37 | if (!c) | |
38 | return NULL; | |
39 | ||
40 | if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) { | |
41 | CPU_ZERO_S(CPU_ALLOC_SIZE(n), c); | |
42 | ||
43 | if (ncpus) | |
44 | *ncpus = n; | |
45 | ||
46 | return c; | |
47 | } | |
48 | ||
49 | CPU_FREE(c); | |
50 | ||
51 | if (errno != EINVAL) | |
52 | return NULL; | |
53 | ||
54 | n *= 2; | |
55 | } | |
56 | } | |
57 | ||
58 | int parse_cpu_set_and_warn( | |
59 | const char *rvalue, | |
60 | cpu_set_t **cpu_set, | |
61 | const char *unit, | |
62 | const char *filename, | |
63 | unsigned line, | |
64 | const char *lvalue) { | |
65 | ||
66 | const char *whole_rvalue = rvalue; | |
67 | _cleanup_cpu_free_ cpu_set_t *c = NULL; | |
68 | unsigned ncpus = 0; | |
69 | ||
70 | assert(lvalue); | |
71 | assert(rvalue); | |
72 | ||
73 | for (;;) { | |
74 | _cleanup_free_ char *word = NULL; | |
75 | unsigned cpu; | |
76 | int r; | |
77 | ||
4fc66acb | 78 | r = extract_first_word(&rvalue, &word, WHITESPACE ",", EXTRACT_QUOTES); |
618234a5 LP |
79 | if (r < 0) { |
80 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, whole_rvalue); | |
81 | return r; | |
82 | } | |
83 | if (r == 0) | |
84 | break; | |
85 | ||
86 | if (!c) { | |
87 | c = cpu_set_malloc(&ncpus); | |
88 | if (!c) | |
89 | return log_oom(); | |
90 | } | |
91 | ||
92 | r = safe_atou(word, &cpu); | |
93 | if (r < 0 || cpu >= ncpus) { | |
94 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", rvalue); | |
95 | return -EINVAL; | |
96 | } | |
97 | ||
98 | CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c); | |
99 | } | |
100 | ||
101 | /* On success, sets *cpu_set and returns ncpus for the system. */ | |
102 | if (c) { | |
103 | *cpu_set = c; | |
104 | c = NULL; | |
105 | } | |
106 | ||
107 | return (int) ncpus; | |
108 | } |