sd-id128.c revision ec202eae8e84a4c99f054f771cb832046cb8769f
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering This file is part of systemd.
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering Copyright 2011 Lennart Poettering
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering systemd is free software; you can redistribute it and/or modify it
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering under the terms of the GNU Lesser General Public License as published by
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering (at your option) any later version.
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering systemd is distributed in the hope that it will be useful, but
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering Lesser General Public License for more details.
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering You should have received a copy of the GNU Lesser General Public License
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
4e2f8d27781731021aa6b96c0ee18a8966eefe1cLennart Poettering_public_ char *sd_id128_to_string(sd_id128_t id, char s[33]) {
a911bb9ab27ac0eb3bbf4e8b4109e5da9b88eee3Lennart Poettering for (n = 0; n < 16; n++) {
4e2f8d27781731021aa6b96c0ee18a8966eefe1cLennart Poettering_public_ int sd_id128_from_string(const char s[], sd_id128_t *ret) {
4e2f8d27781731021aa6b96c0ee18a8966eefe1cLennart Poettering unsigned n, i;
718db96199eb307751264e4163555662c9a389faLennart Poettering for (n = 0, i = 0; n < 16;) {
718db96199eb307751264e4163555662c9a389faLennart Poettering if (s[i] == '-') {
2d4a39e759c4ab846ad8a546abeddd40bc8d736eLennart Poettering /* Is this a GUID? Then be nice, and skip over
a911bb9ab27ac0eb3bbf4e8b4109e5da9b88eee3Lennart Poettering * the dashes */
9f2e86af0600e99cff00d1c92f9bb8d38f29896aLennart Poettering else if (i == 13 || i == 18 || i == 23) {
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering if (s[i] != 0)
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poetteringstatic sd_id128_t make_v4_uuid(sd_id128_t id) {
adb3a45d9a1cebdec30406cc2c04503fc5e735beLennart Poettering /* Stolen from generate_random_uuid() of drivers/char/random.c
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering * in the kernel sources */
718db96199eb307751264e4163555662c9a389faLennart Poettering /* Set UUID version to 4 --- truly random generation */
718db96199eb307751264e4163555662c9a389faLennart Poettering id.bytes[6] = (id.bytes[6] & 0x0F) | 0x40;
718db96199eb307751264e4163555662c9a389faLennart Poettering /* Set the UUID variant to DCE */
718db96199eb307751264e4163555662c9a389faLennart Poettering id.bytes[8] = (id.bytes[8] & 0x3F) | 0x80;
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering_public_ int sd_id128_get_machine(sd_id128_t *ret) {
cc23f9f17434aad3941dff3c20bce485b39ce47cLennart Poettering static thread_local sd_id128_t saved_machine_id;
2d4a39e759c4ab846ad8a546abeddd40bc8d736eLennart Poettering static thread_local bool saved_machine_id_valid = false;
2d4a39e759c4ab846ad8a546abeddd40bc8d736eLennart Poettering fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
2d4a39e759c4ab846ad8a546abeddd40bc8d736eLennart Poettering return (int) k;
cc23f9f17434aad3941dff3c20bce485b39ce47cLennart Poettering for (j = 0; j < 16; j++) {
718db96199eb307751264e4163555662c9a389faLennart Poettering if (a < 0 || b < 0)
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering_public_ int sd_id128_get_boot(sd_id128_t *ret) {
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering static thread_local sd_id128_t saved_boot_id;
718db96199eb307751264e4163555662c9a389faLennart Poettering static thread_local bool saved_boot_id_valid = false;
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
a6c0353b9268d5b780fb7ff05a10cb5031446e5dLennart Poettering return (int) k;
6c12b52e19640747e96f89d85422941a23dc6b29Lennart Poettering if (*p == '-')
2d4a39e759c4ab846ad8a546abeddd40bc8d736eLennart Poettering if (a < 0 || b < 0)
2d4a39e759c4ab846ad8a546abeddd40bc8d736eLennart Poettering_public_ int sd_id128_randomize(sd_id128_t *ret) {
2d4a39e759c4ab846ad8a546abeddd40bc8d736eLennart Poettering fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
return -EIO;