2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2013-11-07 03:47:42 +01:00
|
|
|
#ifndef foosdid128hfoo
|
|
|
|
#define foosdid128hfoo
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
/***
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
2012-04-12 00:20:58 +02:00
|
|
|
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
|
2011-10-07 21:06:39 +02:00
|
|
|
(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
|
2012-04-12 00:20:58 +02:00
|
|
|
Lesser General Public License for more details.
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2011-10-07 21:06:39 +02:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-11-07 16:44:48 +01:00
|
|
|
#include "_sd-common.h"
|
|
|
|
|
|
|
|
_SD_BEGIN_DECLARATIONS;
|
2012-01-13 00:49:21 +01:00
|
|
|
|
2014-03-25 04:14:28 +01:00
|
|
|
/* 128-bit ID APIs. See sd-id128(3) for more information. */
|
2012-07-13 01:50:05 +02:00
|
|
|
|
2011-10-07 21:06:39 +02:00
|
|
|
typedef union sd_id128 sd_id128_t;
|
|
|
|
|
|
|
|
union sd_id128 {
|
|
|
|
uint8_t bytes[16];
|
|
|
|
uint64_t qwords[2];
|
|
|
|
};
|
|
|
|
|
2013-11-11 23:40:41 +01:00
|
|
|
#define SD_ID128_STRING_MAX 33
|
|
|
|
|
|
|
|
char *sd_id128_to_string(sd_id128_t id, char s[SD_ID128_STRING_MAX]);
|
2013-04-29 23:39:12 +02:00
|
|
|
int sd_id128_from_string(const char *s, sd_id128_t *ret);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
int sd_id128_randomize(sd_id128_t *ret);
|
|
|
|
|
|
|
|
int sd_id128_get_machine(sd_id128_t *ret);
|
2016-11-17 17:07:46 +01:00
|
|
|
int sd_id128_get_machine_app_specific(sd_id128_t app_id, sd_id128_t *ret);
|
2018-10-02 14:25:24 +02:00
|
|
|
int sd_id128_get_boot_app_specific(sd_id128_t app_id, sd_id128_t *ret);
|
2011-10-07 21:06:39 +02:00
|
|
|
int sd_id128_get_boot(sd_id128_t *ret);
|
2016-08-30 23:18:46 +02:00
|
|
|
int sd_id128_get_invocation(sd_id128_t *ret);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
#define SD_ID128_MAKE(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) \
|
2013-12-22 19:59:12 +01:00
|
|
|
((const sd_id128_t) { .bytes = { 0x##v0, 0x##v1, 0x##v2, 0x##v3, 0x##v4, 0x##v5, 0x##v6, 0x##v7, \
|
2011-10-07 21:06:39 +02:00
|
|
|
0x##v8, 0x##v9, 0x##v10, 0x##v11, 0x##v12, 0x##v13, 0x##v14, 0x##v15 }})
|
|
|
|
|
2014-01-28 00:57:38 +01:00
|
|
|
#define SD_ID128_ARRAY(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) \
|
|
|
|
{ .bytes = { 0x##v0, 0x##v1, 0x##v2, 0x##v3, 0x##v4, 0x##v5, 0x##v6, 0x##v7, \
|
|
|
|
0x##v8, 0x##v9, 0x##v10, 0x##v11, 0x##v12, 0x##v13, 0x##v14, 0x##v15 }}
|
|
|
|
|
2012-01-04 02:13:41 +01:00
|
|
|
/* Note that SD_ID128_FORMAT_VAL will evaluate the passed argument 16
|
2012-01-04 02:09:05 +01:00
|
|
|
* times. It is hence not a good idea to call this macro with an
|
2014-12-29 10:45:58 +01:00
|
|
|
* expensive function as parameter or an expression with side
|
2012-01-04 02:09:05 +01:00
|
|
|
* effects */
|
2012-08-24 22:21:20 +02:00
|
|
|
|
2012-01-04 02:13:41 +01:00
|
|
|
#define SD_ID128_FORMAT_STR "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
|
|
|
|
#define SD_ID128_FORMAT_VAL(x) (x).bytes[0], (x).bytes[1], (x).bytes[2], (x).bytes[3], (x).bytes[4], (x).bytes[5], (x).bytes[6], (x).bytes[7], (x).bytes[8], (x).bytes[9], (x).bytes[10], (x).bytes[11], (x).bytes[12], (x).bytes[13], (x).bytes[14], (x).bytes[15]
|
2012-01-04 02:09:05 +01:00
|
|
|
|
2012-10-16 17:02:51 +02:00
|
|
|
#define SD_ID128_CONST_STR(x) \
|
2013-12-22 19:59:12 +01:00
|
|
|
((const char[SD_ID128_STRING_MAX]) { \
|
2012-10-16 17:02:51 +02:00
|
|
|
((x).bytes[0] >> 4) >= 10 ? 'a' + ((x).bytes[0] >> 4) - 10 : '0' + ((x).bytes[0] >> 4), \
|
|
|
|
((x).bytes[0] & 15) >= 10 ? 'a' + ((x).bytes[0] & 15) - 10 : '0' + ((x).bytes[0] & 15), \
|
|
|
|
((x).bytes[1] >> 4) >= 10 ? 'a' + ((x).bytes[1] >> 4) - 10 : '0' + ((x).bytes[1] >> 4), \
|
|
|
|
((x).bytes[1] & 15) >= 10 ? 'a' + ((x).bytes[1] & 15) - 10 : '0' + ((x).bytes[1] & 15), \
|
|
|
|
((x).bytes[2] >> 4) >= 10 ? 'a' + ((x).bytes[2] >> 4) - 10 : '0' + ((x).bytes[2] >> 4), \
|
|
|
|
((x).bytes[2] & 15) >= 10 ? 'a' + ((x).bytes[2] & 15) - 10 : '0' + ((x).bytes[2] & 15), \
|
|
|
|
((x).bytes[3] >> 4) >= 10 ? 'a' + ((x).bytes[3] >> 4) - 10 : '0' + ((x).bytes[3] >> 4), \
|
|
|
|
((x).bytes[3] & 15) >= 10 ? 'a' + ((x).bytes[3] & 15) - 10 : '0' + ((x).bytes[3] & 15), \
|
|
|
|
((x).bytes[4] >> 4) >= 10 ? 'a' + ((x).bytes[4] >> 4) - 10 : '0' + ((x).bytes[4] >> 4), \
|
|
|
|
((x).bytes[4] & 15) >= 10 ? 'a' + ((x).bytes[4] & 15) - 10 : '0' + ((x).bytes[4] & 15), \
|
|
|
|
((x).bytes[5] >> 4) >= 10 ? 'a' + ((x).bytes[5] >> 4) - 10 : '0' + ((x).bytes[5] >> 4), \
|
|
|
|
((x).bytes[5] & 15) >= 10 ? 'a' + ((x).bytes[5] & 15) - 10 : '0' + ((x).bytes[5] & 15), \
|
|
|
|
((x).bytes[6] >> 4) >= 10 ? 'a' + ((x).bytes[6] >> 4) - 10 : '0' + ((x).bytes[6] >> 4), \
|
|
|
|
((x).bytes[6] & 15) >= 10 ? 'a' + ((x).bytes[6] & 15) - 10 : '0' + ((x).bytes[6] & 15), \
|
|
|
|
((x).bytes[7] >> 4) >= 10 ? 'a' + ((x).bytes[7] >> 4) - 10 : '0' + ((x).bytes[7] >> 4), \
|
|
|
|
((x).bytes[7] & 15) >= 10 ? 'a' + ((x).bytes[7] & 15) - 10 : '0' + ((x).bytes[7] & 15), \
|
|
|
|
((x).bytes[8] >> 4) >= 10 ? 'a' + ((x).bytes[8] >> 4) - 10 : '0' + ((x).bytes[8] >> 4), \
|
|
|
|
((x).bytes[8] & 15) >= 10 ? 'a' + ((x).bytes[8] & 15) - 10 : '0' + ((x).bytes[8] & 15), \
|
|
|
|
((x).bytes[9] >> 4) >= 10 ? 'a' + ((x).bytes[9] >> 4) - 10 : '0' + ((x).bytes[9] >> 4), \
|
|
|
|
((x).bytes[9] & 15) >= 10 ? 'a' + ((x).bytes[9] & 15) - 10 : '0' + ((x).bytes[9] & 15), \
|
|
|
|
((x).bytes[10] >> 4) >= 10 ? 'a' + ((x).bytes[10] >> 4) - 10 : '0' + ((x).bytes[10] >> 4), \
|
|
|
|
((x).bytes[10] & 15) >= 10 ? 'a' + ((x).bytes[10] & 15) - 10 : '0' + ((x).bytes[10] & 15), \
|
|
|
|
((x).bytes[11] >> 4) >= 10 ? 'a' + ((x).bytes[11] >> 4) - 10 : '0' + ((x).bytes[11] >> 4), \
|
|
|
|
((x).bytes[11] & 15) >= 10 ? 'a' + ((x).bytes[11] & 15) - 10 : '0' + ((x).bytes[11] & 15), \
|
|
|
|
((x).bytes[12] >> 4) >= 10 ? 'a' + ((x).bytes[12] >> 4) - 10 : '0' + ((x).bytes[12] >> 4), \
|
|
|
|
((x).bytes[12] & 15) >= 10 ? 'a' + ((x).bytes[12] & 15) - 10 : '0' + ((x).bytes[12] & 15), \
|
|
|
|
((x).bytes[13] >> 4) >= 10 ? 'a' + ((x).bytes[13] >> 4) - 10 : '0' + ((x).bytes[13] >> 4), \
|
|
|
|
((x).bytes[13] & 15) >= 10 ? 'a' + ((x).bytes[13] & 15) - 10 : '0' + ((x).bytes[13] & 15), \
|
|
|
|
((x).bytes[14] >> 4) >= 10 ? 'a' + ((x).bytes[14] >> 4) - 10 : '0' + ((x).bytes[14] >> 4), \
|
|
|
|
((x).bytes[14] & 15) >= 10 ? 'a' + ((x).bytes[14] & 15) - 10 : '0' + ((x).bytes[14] & 15), \
|
|
|
|
((x).bytes[15] >> 4) >= 10 ? 'a' + ((x).bytes[15] >> 4) - 10 : '0' + ((x).bytes[15] >> 4), \
|
|
|
|
((x).bytes[15] & 15) >= 10 ? 'a' + ((x).bytes[15] & 15) - 10 : '0' + ((x).bytes[15] & 15), \
|
|
|
|
0 })
|
|
|
|
|
tree-wide: add SD_ID128_MAKE_STR, remove LOG_MESSAGE_ID
Embedding sd_id128_t's in constant strings was rather cumbersome. We had
SD_ID128_CONST_STR which returned a const char[], but it had two problems:
- it wasn't possible to statically concatanate this array with a normal string
- gcc wasn't really able to optimize this, and generated code to perform the
"conversion" at runtime.
Because of this, even our own code in coredumpctl wasn't using
SD_ID128_CONST_STR.
Add a new macro to generate a constant string: SD_ID128_MAKE_STR.
It is not as elegant as SD_ID128_CONST_STR, because it requires a repetition
of the numbers, but in practice it is more convenient to use, and allows gcc
to generate smarter code:
$ size .libs/systemd{,-logind,-journald}{.old,}
text data bss dec hex filename
1265204 149564 4808 1419576 15a938 .libs/systemd.old
1260268 149564 4808 1414640 1595f0 .libs/systemd
246805 13852 209 260866 3fb02 .libs/systemd-logind.old
240973 13852 209 255034 3e43a .libs/systemd-logind
146839 4984 34 151857 25131 .libs/systemd-journald.old
146391 4984 34 151409 24f71 .libs/systemd-journald
It is also much easier to check if a certain binary uses a certain MESSAGE_ID:
$ strings .libs/systemd.old|grep MESSAGE_ID
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
MESSAGE_ID=%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x
$ strings .libs/systemd|grep MESSAGE_ID
MESSAGE_ID=c7a787079b354eaaa9e77b371893cd27
MESSAGE_ID=b07a249cd024414a82dd00cd181378ff
MESSAGE_ID=641257651c1b4ec9a8624d7a40a9e1e7
MESSAGE_ID=de5b426a63be47a7b6ac3eaac82e2f6f
MESSAGE_ID=d34d037fff1847e6ae669a370e694725
MESSAGE_ID=7d4958e842da4a758f6c1cdc7b36dcc5
MESSAGE_ID=1dee0369c7fc4736b7099b38ecb46ee7
MESSAGE_ID=39f53479d3a045ac8e11786248231fbf
MESSAGE_ID=be02cf6855d2428ba40df7e9d022f03d
MESSAGE_ID=7b05ebc668384222baa8881179cfda54
MESSAGE_ID=9d1aaa27d60140bd96365438aad20286
2016-11-06 18:48:23 +01:00
|
|
|
#define SD_ID128_MAKE_STR(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
|
|
|
|
#a #b #c #d #e #f #g #h #i #j #k #l #m #n #o #p
|
|
|
|
|
2016-03-11 19:41:49 +01:00
|
|
|
_sd_pure_ static __inline__ int sd_id128_equal(sd_id128_t a, sd_id128_t b) {
|
2011-10-07 21:06:39 +02:00
|
|
|
return memcmp(&a, &b, 16) == 0;
|
|
|
|
}
|
|
|
|
|
2016-03-11 19:41:49 +01:00
|
|
|
_sd_pure_ static __inline__ int sd_id128_is_null(sd_id128_t a) {
|
2015-02-24 00:10:35 +01:00
|
|
|
return a.qwords[0] == 0 && a.qwords[1] == 0;
|
|
|
|
}
|
|
|
|
|
2013-12-22 19:59:12 +01:00
|
|
|
#define SD_ID128_NULL ((const sd_id128_t) { .qwords = { 0, 0 }})
|
2012-09-21 22:50:26 +02:00
|
|
|
|
2013-11-07 16:44:48 +01:00
|
|
|
_SD_END_DECLARATIONS;
|
2012-01-13 00:49:21 +01:00
|
|
|
|
2011-10-07 21:06:39 +02:00
|
|
|
#endif
|