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