Skip to content

Commit 67dfbe0

Browse files
committed
util: add keyring_describe helper and move to basic
So that it can be used from libsystemd. No external dependencies.
1 parent 0bec281 commit 67dfbe0

File tree

5 files changed

+37
-20
lines changed

5 files changed

+37
-20
lines changed

src/shared/keyring-util.c renamed to src/basic/keyring-util.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,34 @@ int keyring_read(key_serial_t serial, void **ret, size_t *ret_size) {
3333
bufsize = (size_t) n;
3434
}
3535
}
36+
37+
int keyring_describe(key_serial_t serial, char **ret) {
38+
_cleanup_free_ char *tuple = NULL;
39+
size_t sz = 64;
40+
int c = -1; /* Workaround for maybe-uninitialized false positive due to missing_syscall indirection */
41+
42+
assert(ret);
43+
44+
for (;;) {
45+
tuple = new(char, sz);
46+
if (!tuple)
47+
return log_oom_debug();
48+
49+
c = keyctl(KEYCTL_DESCRIBE, serial, (unsigned long) tuple, c, 0);
50+
if (c < 0)
51+
return log_debug_errno(errno, "Failed to describe key id %d: %m", serial);
52+
53+
if ((size_t) c <= sz)
54+
break;
55+
56+
sz = c;
57+
free(tuple);
58+
}
59+
60+
/* The kernel returns a final NUL in the string, verify that. */
61+
assert(tuple[c-1] == 0);
62+
63+
*ret = TAKE_PTR(tuple);
64+
65+
return 0;
66+
}

src/shared/keyring-util.h renamed to src/basic/keyring-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
#define TAKE_KEY_SERIAL(key_serial) TAKE_GENERIC(key_serial, key_serial_t, -1)
1010

1111
int keyring_read(key_serial_t serial, void **ret, size_t *ret_size);
12+
int keyring_describe(key_serial_t serial, char **ret);

src/basic/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ basic_sources = files(
5757
'lock-util.c',
5858
'log.c',
5959
'login-util.c',
60+
'keyring-util.c',
6061
'memfd-util.c',
6162
'memory-util.c',
6263
'mempool.c',

src/libsystemd/sd-id128/sd-id128.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "hmac.h"
1414
#include "id128-util.h"
1515
#include "io-util.h"
16+
#include "keyring-util.h"
1617
#include "macro.h"
1718
#include "missing_syscall.h"
1819
#include "missing_threads.h"
@@ -202,7 +203,6 @@ static int get_invocation_from_keyring(sd_id128_t *ret) {
202203
char *d, *p, *g, *u, *e;
203204
unsigned long perms;
204205
key_serial_t key;
205-
size_t sz = 256;
206206
uid_t uid;
207207
gid_t gid;
208208
int r, c;
@@ -221,24 +221,9 @@ static int get_invocation_from_keyring(sd_id128_t *ret) {
221221
return -errno;
222222
}
223223

224-
for (;;) {
225-
description = new(char, sz);
226-
if (!description)
227-
return -ENOMEM;
228-
229-
c = keyctl(KEYCTL_DESCRIBE, key, (unsigned long) description, sz, 0);
230-
if (c < 0)
231-
return -errno;
232-
233-
if ((size_t) c <= sz)
234-
break;
235-
236-
sz = c;
237-
free(description);
238-
}
239-
240-
/* The kernel returns a final NUL in the string, verify that. */
241-
assert(description[c-1] == 0);
224+
r = keyring_describe(key, &description);
225+
if (r < 0)
226+
return r;
242227

243228
/* Chop off the final description string */
244229
d = strrchr(description, ';');

src/shared/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ shared_sources = files(
100100
'kbd-util.c',
101101
'kernel-config.c',
102102
'kernel-image.c',
103-
'keyring-util.c',
104103
'killall.c',
105104
'label-util.c',
106105
'libarchive-util.c',

0 commit comments

Comments
 (0)