]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8530dc44 | 2 | |
a8fbdf54 TA |
3 | #include <errno.h> |
4 | #include <stdlib.h> | |
5 | #include <string.h> | |
6 | ||
62d74c78 | 7 | #include "alloc-util.h" |
a8fbdf54 | 8 | #include "macro.h" |
760877e9 | 9 | #include "sort-util.h" |
8530dc44 | 10 | #include "uid-range.h" |
b1d4f8e1 | 11 | #include "user-util.h" |
8530dc44 LP |
12 | |
13 | static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) { | |
14 | assert(range); | |
15 | ||
16 | return range->start <= start + nr && | |
17 | range->start + range->nr >= start; | |
18 | } | |
19 | ||
20 | static void uid_range_coalesce(UidRange **p, unsigned *n) { | |
21 | unsigned i, j; | |
22 | ||
23 | assert(p); | |
24 | assert(n); | |
25 | ||
26 | for (i = 0; i < *n; i++) { | |
27 | for (j = i + 1; j < *n; j++) { | |
28 | UidRange *x = (*p)+i, *y = (*p)+j; | |
29 | ||
30 | if (uid_range_intersect(x, y->start, y->nr)) { | |
31 | uid_t begin, end; | |
32 | ||
33 | begin = MIN(x->start, y->start); | |
34 | end = MAX(x->start + x->nr, y->start + y->nr); | |
35 | ||
36 | x->start = begin; | |
37 | x->nr = end - begin; | |
38 | ||
39 | if (*n > j+1) | |
40 | memmove(y, y+1, sizeof(UidRange) * (*n - j -1)); | |
41 | ||
313cefa1 | 42 | (*n)--; |
8530dc44 LP |
43 | j--; |
44 | } | |
45 | } | |
46 | } | |
8530dc44 LP |
47 | } |
48 | ||
93bab288 YW |
49 | static int uid_range_compare(const UidRange *a, const UidRange *b) { |
50 | int r; | |
8530dc44 | 51 | |
93bab288 YW |
52 | r = CMP(a->start, b->start); |
53 | if (r != 0) | |
54 | return r; | |
8530dc44 | 55 | |
93bab288 | 56 | return CMP(a->nr, b->nr); |
8530dc44 LP |
57 | } |
58 | ||
59 | int uid_range_add(UidRange **p, unsigned *n, uid_t start, uid_t nr) { | |
60 | bool found = false; | |
61 | UidRange *x; | |
62 | unsigned i; | |
63 | ||
64 | assert(p); | |
65 | assert(n); | |
66 | ||
67 | if (nr <= 0) | |
68 | return 0; | |
69 | ||
70 | for (i = 0; i < *n; i++) { | |
71 | x = (*p) + i; | |
72 | if (uid_range_intersect(x, start, nr)) { | |
73 | found = true; | |
74 | break; | |
75 | } | |
76 | } | |
77 | ||
78 | if (found) { | |
79 | uid_t begin, end; | |
80 | ||
81 | begin = MIN(x->start, start); | |
82 | end = MAX(x->start + x->nr, start + nr); | |
83 | ||
84 | x->start = begin; | |
85 | x->nr = end - begin; | |
86 | } else { | |
87 | UidRange *t; | |
88 | ||
62d74c78 | 89 | t = reallocarray(*p, *n + 1, sizeof(UidRange)); |
8530dc44 LP |
90 | if (!t) |
91 | return -ENOMEM; | |
92 | ||
93 | *p = t; | |
94 | x = t + ((*n) ++); | |
95 | ||
96 | x->start = start; | |
97 | x->nr = nr; | |
98 | } | |
99 | ||
93bab288 | 100 | typesafe_qsort(*p, *n, uid_range_compare); |
8530dc44 LP |
101 | uid_range_coalesce(p, n); |
102 | ||
103 | return *n; | |
104 | } | |
105 | ||
106 | int uid_range_add_str(UidRange **p, unsigned *n, const char *s) { | |
107 | uid_t start, nr; | |
108 | const char *t; | |
109 | int r; | |
110 | ||
111 | assert(p); | |
112 | assert(n); | |
113 | assert(s); | |
114 | ||
115 | t = strchr(s, '-'); | |
116 | if (t) { | |
117 | char *b; | |
118 | uid_t end; | |
119 | ||
120 | b = strndupa(s, t - s); | |
121 | r = parse_uid(b, &start); | |
122 | if (r < 0) | |
123 | return r; | |
124 | ||
125 | r = parse_uid(t+1, &end); | |
126 | if (r < 0) | |
127 | return r; | |
128 | ||
129 | if (end < start) | |
130 | return -EINVAL; | |
131 | ||
132 | nr = end - start + 1; | |
133 | } else { | |
134 | r = parse_uid(s, &start); | |
135 | if (r < 0) | |
136 | return r; | |
137 | ||
138 | nr = 1; | |
139 | } | |
140 | ||
141 | return uid_range_add(p, n, start, nr); | |
142 | } | |
143 | ||
144 | int uid_range_next_lower(const UidRange *p, unsigned n, uid_t *uid) { | |
fed1e721 | 145 | uid_t closest = UID_INVALID, candidate; |
8530dc44 LP |
146 | unsigned i; |
147 | ||
148 | assert(p); | |
149 | assert(uid); | |
150 | ||
151 | candidate = *uid - 1; | |
152 | ||
153 | for (i = 0; i < n; i++) { | |
154 | uid_t begin, end; | |
155 | ||
156 | begin = p[i].start; | |
157 | end = p[i].start + p[i].nr - 1; | |
158 | ||
159 | if (candidate >= begin && candidate <= end) { | |
160 | *uid = candidate; | |
161 | return 1; | |
162 | } | |
163 | ||
164 | if (end < candidate) | |
165 | closest = end; | |
166 | } | |
167 | ||
fed1e721 | 168 | if (closest == UID_INVALID) |
8530dc44 LP |
169 | return -EBUSY; |
170 | ||
171 | *uid = closest; | |
172 | return 1; | |
173 | } | |
174 | ||
175 | bool uid_range_contains(const UidRange *p, unsigned n, uid_t uid) { | |
176 | unsigned i; | |
177 | ||
178 | assert(p); | |
179 | assert(uid); | |
180 | ||
181 | for (i = 0; i < n; i++) | |
182 | if (uid >= p[i].start && uid < p[i].start + p[i].nr) | |
183 | return true; | |
184 | ||
185 | return false; | |
186 | } |