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