sd-id128: split UUID file read/write code into new id128-util.[ch]
We currently have code to read and write files containing UUIDs at various places. Unify this in id128-util.[ch], and move some other stuff there too. The new files are located in src/libsystemd/sd-id128/ (instead of src/shared/), because they are actually the backend of sd_id128_get_machine() and sd_id128_get_boot(). In follow-up patches we can use this reduce the code in nspawn and machine-id-setup by adopted the common implementation.
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright 2016 Lennart Poettering
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "fd-util.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "id128-util.h"
|
||||
#include "io-util.h"
|
||||
#include "stdio-util.h"
|
||||
|
||||
char *id128_to_uuid_string(sd_id128_t id, char s[37]) {
|
||||
unsigned n, k = 0;
|
||||
|
||||
assert(s);
|
||||
|
||||
/* Similar to sd_id128_to_string() but formats the result as UUID instead of plain hex chars */
|
||||
|
||||
for (n = 0; n < 16; n++) {
|
||||
|
||||
if (IN_SET(n, 4, 6, 8, 10))
|
||||
s[k++] = '-';
|
||||
|
||||
s[k++] = hexchar(id.bytes[n] >> 4);
|
||||
s[k++] = hexchar(id.bytes[n] & 0xF);
|
||||
}
|
||||
|
||||
assert(k == 36);
|
||||
|
||||
s[k] = 0;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
bool id128_is_valid(const char *s) {
|
||||
size_t i, l;
|
||||
|
||||
assert(s);
|
||||
|
||||
l = strlen(s);
|
||||
if (l == 32) {
|
||||
|
||||
/* Plain formatted 128bit hex string */
|
||||
|
||||
for (i = 0; i < l; i++) {
|
||||
char c = s[i];
|
||||
|
||||
if (!(c >= '0' && c <= '9') &&
|
||||
!(c >= 'a' && c <= 'z') &&
|
||||
!(c >= 'A' && c <= 'Z'))
|
||||
return false;
|
||||
}
|
||||
|
||||
} else if (l == 36) {
|
||||
|
||||
/* Formatted UUID */
|
||||
|
||||
for (i = 0; i < l; i++) {
|
||||
char c = s[i];
|
||||
|
||||
if ((i == 8 || i == 13 || i == 18 || i == 23)) {
|
||||
if (c != '-')
|
||||
return false;
|
||||
} else {
|
||||
if (!(c >= '0' && c <= '9') &&
|
||||
!(c >= 'a' && c <= 'z') &&
|
||||
!(c >= 'A' && c <= 'Z'))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
} else
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) {
|
||||
char buffer[36 + 2];
|
||||
ssize_t l;
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(f < _ID128_FORMAT_MAX);
|
||||
|
||||
/* Reads an 128bit ID from a file, which may either be in plain format (32 hex digits), or in UUID format, both
|
||||
* followed by a newline and nothing else. */
|
||||
|
||||
l = loop_read(fd, buffer, sizeof(buffer), false); /* we expect a short read of either 33 or 37 chars */
|
||||
if (l < 0)
|
||||
return (int) l;
|
||||
if (l == 0) /* empty? */
|
||||
return -ENOMEDIUM;
|
||||
|
||||
if (l == 33) {
|
||||
if (f == ID128_UUID)
|
||||
return -EINVAL;
|
||||
|
||||
if (buffer[32] != '\n')
|
||||
return -EINVAL;
|
||||
|
||||
buffer[32] = 0;
|
||||
|
||||
} else if (l == 37) {
|
||||
if (f == ID128_PLAIN)
|
||||
return -EINVAL;
|
||||
|
||||
if (buffer[36] != '\n')
|
||||
return -EINVAL;
|
||||
|
||||
buffer[36] = 0;
|
||||
} else
|
||||
return -EINVAL;
|
||||
|
||||
return sd_id128_from_string(buffer, ret);
|
||||
}
|
||||
|
||||
int id128_read(const char *p, Id128Format f, sd_id128_t *ret) {
|
||||
_cleanup_close_ int fd = -1;
|
||||
|
||||
fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
return id128_read_fd(fd, f, ret);
|
||||
}
|
||||
|
||||
int id128_write_fd(int fd, Id128Format f, sd_id128_t id) {
|
||||
char buffer[36 + 2];
|
||||
size_t sz;
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(f < _ID128_FORMAT_MAX);
|
||||
|
||||
if (f != ID128_UUID) {
|
||||
sd_id128_to_string(id, buffer);
|
||||
buffer[32] = '\n';
|
||||
sz = 33;
|
||||
} else {
|
||||
id128_to_uuid_string(id, buffer);
|
||||
buffer[36] = '\n';
|
||||
sz = 37;
|
||||
}
|
||||
|
||||
return loop_write(fd, buffer, sz, false);
|
||||
}
|
||||
|
||||
int id128_write(const char *p, Id128Format f, sd_id128_t id) {
|
||||
_cleanup_close_ int fd = -1;
|
||||
|
||||
fd = open(p, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
return id128_write_fd(fd, f, id);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright 2016 Lennart Poettering
|
||||
|
||||
systemd is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
systemd is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "sd-id128.h"
|
||||
#include "macro.h"
|
||||
|
||||
char *id128_to_uuid_string(sd_id128_t id, char s[37]);
|
||||
|
||||
/* Like SD_ID128_FORMAT_STR, but formats as UUID, not in plain format */
|
||||
#define ID128_UUID_FORMAT_STR "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
|
||||
|
||||
bool id128_is_valid(const char *s) _pure_;
|
||||
|
||||
typedef enum Id128Format {
|
||||
ID128_ANY,
|
||||
ID128_PLAIN, /* formatted as 32 hex chars as-is */
|
||||
ID128_UUID, /* formatted as 36 character uuid string */
|
||||
_ID128_FORMAT_MAX,
|
||||
} Id128Format;
|
||||
|
||||
int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret);
|
||||
int id128_read(const char *p, Id128Format f, sd_id128_t *ret);
|
||||
|
||||
int id128_write_fd(int fd, Id128Format f, sd_id128_t id);
|
||||
int id128_write(const char *p, Id128Format f, sd_id128_t id);
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "fd-util.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "id128-util.h"
|
||||
#include "io-util.h"
|
||||
#include "macro.h"
|
||||
#include "random-util.h"
|
||||
@@ -93,6 +94,41 @@ _public_ int sd_id128_from_string(const char s[], sd_id128_t *ret) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
_public_ int sd_id128_get_machine(sd_id128_t *ret) {
|
||||
static thread_local sd_id128_t saved_machine_id = {};
|
||||
int r;
|
||||
|
||||
assert_return(ret, -EINVAL);
|
||||
|
||||
if (sd_id128_is_null(saved_machine_id)) {
|
||||
r = id128_read("/etc/machine-id", ID128_PLAIN, &saved_machine_id);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (sd_id128_is_null(saved_machine_id))
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
*ret = saved_machine_id;
|
||||
return 0;
|
||||
}
|
||||
|
||||
_public_ int sd_id128_get_boot(sd_id128_t *ret) {
|
||||
static thread_local sd_id128_t saved_boot_id = {};
|
||||
int r;
|
||||
|
||||
assert_return(ret, -EINVAL);
|
||||
|
||||
if (sd_id128_is_null(saved_boot_id)) {
|
||||
r = id128_read("/proc/sys/kernel/random/boot_id", ID128_UUID, &saved_boot_id);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
*ret = saved_boot_id;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static sd_id128_t make_v4_uuid(sd_id128_t id) {
|
||||
/* Stolen from generate_random_uuid() of drivers/char/random.c
|
||||
* in the kernel sources */
|
||||
@@ -106,106 +142,6 @@ static sd_id128_t make_v4_uuid(sd_id128_t id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
_public_ int sd_id128_get_machine(sd_id128_t *ret) {
|
||||
static thread_local sd_id128_t saved_machine_id;
|
||||
static thread_local bool saved_machine_id_valid = false;
|
||||
_cleanup_close_ int fd = -1;
|
||||
char buf[33];
|
||||
unsigned j;
|
||||
sd_id128_t t;
|
||||
int r;
|
||||
|
||||
assert_return(ret, -EINVAL);
|
||||
|
||||
if (saved_machine_id_valid) {
|
||||
*ret = saved_machine_id;
|
||||
return 0;
|
||||
}
|
||||
|
||||
fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
r = loop_read_exact(fd, buf, 33, false);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (buf[32] !='\n')
|
||||
return -EIO;
|
||||
|
||||
for (j = 0; j < 16; j++) {
|
||||
int a, b;
|
||||
|
||||
a = unhexchar(buf[j*2]);
|
||||
b = unhexchar(buf[j*2+1]);
|
||||
|
||||
if (a < 0 || b < 0)
|
||||
return -EIO;
|
||||
|
||||
t.bytes[j] = a << 4 | b;
|
||||
}
|
||||
|
||||
saved_machine_id = t;
|
||||
saved_machine_id_valid = true;
|
||||
|
||||
*ret = t;
|
||||
return 0;
|
||||
}
|
||||
|
||||
_public_ int sd_id128_get_boot(sd_id128_t *ret) {
|
||||
static thread_local sd_id128_t saved_boot_id;
|
||||
static thread_local bool saved_boot_id_valid = false;
|
||||
_cleanup_close_ int fd = -1;
|
||||
char buf[36];
|
||||
unsigned j;
|
||||
sd_id128_t t;
|
||||
char *p;
|
||||
int r;
|
||||
|
||||
assert_return(ret, -EINVAL);
|
||||
|
||||
if (saved_boot_id_valid) {
|
||||
*ret = saved_boot_id;
|
||||
return 0;
|
||||
}
|
||||
|
||||
fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
r = loop_read_exact(fd, buf, 36, false);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
for (j = 0, p = buf; j < 16; j++) {
|
||||
int a, b;
|
||||
|
||||
if (p >= buf + 35)
|
||||
return -EIO;
|
||||
|
||||
if (*p == '-') {
|
||||
p++;
|
||||
if (p >= buf + 35)
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
a = unhexchar(p[0]);
|
||||
b = unhexchar(p[1]);
|
||||
|
||||
if (a < 0 || b < 0)
|
||||
return -EIO;
|
||||
|
||||
t.bytes[j] = a << 4 | b;
|
||||
|
||||
p += 2;
|
||||
}
|
||||
|
||||
saved_boot_id = t;
|
||||
saved_boot_id_valid = true;
|
||||
|
||||
*ret = t;
|
||||
return 0;
|
||||
}
|
||||
|
||||
_public_ int sd_id128_randomize(sd_id128_t *ret) {
|
||||
sd_id128_t t;
|
||||
int r;
|
||||
|
||||
Reference in New Issue
Block a user