]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/cpu-set-util.c
basic: add log_level argument to timezone_is_valid
[thirdparty/systemd.git] / src / basic / cpu-set-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010-2015 Lennart Poettering
6 Copyright 2015 Filipe Brandenburger
7 ***/
8
9 #include <errno.h>
10 #include <stddef.h>
11 #include <syslog.h>
12
13 #include "alloc-util.h"
14 #include "cpu-set-util.h"
15 #include "extract-word.h"
16 #include "log.h"
17 #include "macro.h"
18 #include "parse-util.h"
19 #include "string-util.h"
20
21 cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
22 cpu_set_t *c;
23 unsigned n = 1024;
24
25 /* Allocates the cpuset in the right size */
26
27 for (;;) {
28 c = CPU_ALLOC(n);
29 if (!c)
30 return NULL;
31
32 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
33 CPU_ZERO_S(CPU_ALLOC_SIZE(n), c);
34
35 if (ncpus)
36 *ncpus = n;
37
38 return c;
39 }
40
41 CPU_FREE(c);
42
43 if (errno != EINVAL)
44 return NULL;
45
46 n *= 2;
47 }
48 }
49
50 int parse_cpu_set_internal(
51 const char *rvalue,
52 cpu_set_t **cpu_set,
53 bool warn,
54 const char *unit,
55 const char *filename,
56 unsigned line,
57 const char *lvalue) {
58
59 _cleanup_cpu_free_ cpu_set_t *c = NULL;
60 const char *p = rvalue;
61 unsigned ncpus = 0;
62
63 assert(rvalue);
64
65 for (;;) {
66 _cleanup_free_ char *word = NULL;
67 unsigned cpu, cpu_lower, cpu_upper;
68 int r;
69
70 r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_QUOTES);
71 if (r == -ENOMEM)
72 return warn ? log_oom() : -ENOMEM;
73 if (r < 0)
74 return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, rvalue) : r;
75 if (r == 0)
76 break;
77
78 if (!c) {
79 c = cpu_set_malloc(&ncpus);
80 if (!c)
81 return warn ? log_oom() : -ENOMEM;
82 }
83
84 r = parse_range(word, &cpu_lower, &cpu_upper);
85 if (r < 0)
86 return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word) : r;
87 if (cpu_lower >= ncpus || cpu_upper >= ncpus)
88 return warn ? log_syntax(unit, LOG_ERR, filename, line, EINVAL, "CPU out of range '%s' ncpus is %u", word, ncpus) : -EINVAL;
89
90 if (cpu_lower > cpu_upper) {
91 if (warn)
92 log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u, ignoring", word, cpu_lower, cpu_upper);
93 continue;
94 }
95
96 for (cpu = cpu_lower; cpu <= cpu_upper; cpu++)
97 CPU_SET_S(cpu, CPU_ALLOC_SIZE(ncpus), c);
98 }
99
100 /* On success, sets *cpu_set and returns ncpus for the system. */
101 if (c)
102 *cpu_set = TAKE_PTR(c);
103
104 return (int) ncpus;
105 }