8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering This file is part of systemd.
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering Copyright 2014 Lennart Poettering
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering systemd is free software; you can redistribute it and/or modify it
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering under the terms of the GNU Lesser General Public License as published by
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering (at your option) any later version.
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering systemd is distributed in the hope that it will be useful, but
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering Lesser General Public License for more details.
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering You should have received a copy of the GNU Lesser General Public License
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poetteringstatic bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poetteringstatic void uid_range_coalesce(UidRange **p, unsigned *n) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering unsigned i, j;
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering for (i = 0; i < *n; i++) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering for (j = i + 1; j < *n; j++) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering UidRange *x = (*p)+i, *y = (*p)+j;
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering if (uid_range_intersect(x, y->start, y->nr)) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering end = MAX(x->start + x->nr, y->start + y->nr);
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering memmove(y, y+1, sizeof(UidRange) * (*n - j -1));
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poetteringstatic int uid_range_compare(const void *a, const void *b) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering const UidRange *x = a, *y = b;
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poetteringint uid_range_add(UidRange **p, unsigned *n, uid_t start, uid_t nr) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering for (i = 0; i < *n; i++) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering t = realloc(*p, sizeof(UidRange) * (*n + 1));
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering x = t + ((*n) ++);
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering qsort(*p, *n, sizeof(UidRange), uid_range_compare);
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poetteringint uid_range_add_str(UidRange **p, unsigned *n, const char *s) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering const char *t;
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poetteringint uid_range_next_lower(const UidRange *p, unsigned n, uid_t *uid) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering for (i = 0; i < n; i++) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering if (candidate >= begin && candidate <= end) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poetteringbool uid_range_contains(const UidRange *p, unsigned n, uid_t uid) {
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering for (i = 0; i < n; i++)
8530dc4467691a893aa2e07319b18a84fec96cadLennart Poettering if (uid >= p[i].start && uid < p[i].start + p[i].nr)