| 1 | /* |
| 2 | * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stdbool.h> |
| 9 | #include "credentials.hpp" |
| 10 | |
| 11 | uid_t lttng_credentials_get_uid(const struct lttng_credentials *creds) |
| 12 | { |
| 13 | return LTTNG_OPTIONAL_GET(creds->uid); |
| 14 | } |
| 15 | |
| 16 | gid_t lttng_credentials_get_gid(const struct lttng_credentials *creds) |
| 17 | { |
| 18 | return LTTNG_OPTIONAL_GET(creds->gid); |
| 19 | } |
| 20 | |
| 21 | bool lttng_credentials_is_equal_uid(const struct lttng_credentials *a, |
| 22 | const struct lttng_credentials *b) |
| 23 | { |
| 24 | LTTNG_ASSERT(a); |
| 25 | LTTNG_ASSERT(b); |
| 26 | |
| 27 | /* XOR on the is_set value */ |
| 28 | if (!!a->uid.is_set != !!b->uid.is_set) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | if (!a->uid.is_set && !b->uid.is_set) { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | /* Both a and b are set. */ |
| 37 | return a->uid.value == b->uid.value; |
| 38 | } |
| 39 | |
| 40 | bool lttng_credentials_is_equal_gid(const struct lttng_credentials *a, |
| 41 | const struct lttng_credentials *b) |
| 42 | { |
| 43 | LTTNG_ASSERT(a); |
| 44 | LTTNG_ASSERT(b); |
| 45 | |
| 46 | /* XOR on the is_set value */ |
| 47 | if (!!a->gid.is_set != !!b->gid.is_set) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | if (!a->gid.is_set && !b->gid.is_set) { |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | /* Both a and b are set. */ |
| 56 | return a->gid.value == b->gid.value; |
| 57 | } |
| 58 | |
| 59 | bool lttng_credentials_is_equal(const struct lttng_credentials *a, |
| 60 | const struct lttng_credentials *b) |
| 61 | { |
| 62 | LTTNG_ASSERT(a); |
| 63 | LTTNG_ASSERT(b); |
| 64 | |
| 65 | return lttng_credentials_is_equal_uid(a, b) && |
| 66 | lttng_credentials_is_equal_gid(a, b); |
| 67 | } |