1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
14 #include "alloc-util.h"
16 #include "conf-files.h"
21 #include "memory-util.h"
23 #include "path-util.h"
24 #include "siphash24.h"
25 #include "sort-util.h"
26 #include "sparse-endian.h"
28 #include "string-util.h"
30 #include "tmpfile-util.h"
32 const char * const catalog_file_dirs
[] = {
33 "/usr/local/lib/systemd/catalog/",
34 "/usr/lib/systemd/catalog/",
38 #define CATALOG_SIGNATURE { 'R', 'H', 'H', 'H', 'K', 'S', 'L', 'P' }
40 typedef struct CatalogHeader
{
41 uint8_t signature
[8]; /* "RHHHKSLP" */
42 le32_t compatible_flags
;
43 le32_t incompatible_flags
;
46 le64_t catalog_item_size
;
49 typedef struct CatalogItem
{
51 char language
[32]; /* One byte is used for termination, so the maximum allowed
52 * length of the string is actually 31 bytes. */
56 static void catalog_hash_func(const CatalogItem
*i
, struct siphash
*state
) {
57 siphash24_compress(&i
->id
, sizeof(i
->id
), state
);
58 siphash24_compress_string(i
->language
, state
);
61 static int catalog_compare_func(const CatalogItem
*a
, const CatalogItem
*b
) {
65 for (k
= 0; k
< ELEMENTSOF(b
->id
.bytes
); k
++) {
66 r
= CMP(a
->id
.bytes
[k
], b
->id
.bytes
[k
]);
71 return strcmp(a
->language
, b
->language
);
74 DEFINE_HASH_OPS(catalog_hash_ops
, CatalogItem
, catalog_hash_func
, catalog_compare_func
);
76 static bool next_header(const char **s
) {
93 static const char *skip_header(const char *s
) {
94 while (next_header(&s
))
99 static char *combine_entries(const char *one
, const char *two
) {
104 /* Find split point of headers to body */
105 b1
= skip_header(one
);
106 b2
= skip_header(two
);
110 dest
= new(char, l1
+ l2
+ 1);
118 /* Headers from @one */
120 p
= mempcpy(p
, one
, n
);
122 /* Headers from @two, these will only be found if not present above */
124 p
= mempcpy(p
, two
, n
);
139 assert(p
- dest
<= (ptrdiff_t)(l1
+ l2
));
144 static int finish_item(
147 const char *language
,
148 char *payload
, size_t payload_size
) {
150 _cleanup_free_ CatalogItem
*i
= NULL
;
151 _cleanup_free_
char *prev
= NULL
, *combined
= NULL
;
155 assert(payload_size
> 0);
157 i
= new0(CatalogItem
, 1);
163 assert(strlen(language
) > 1 && strlen(language
) < 32);
164 strcpy(i
->language
, language
);
167 prev
= ordered_hashmap_get(h
, i
);
169 /* Already have such an item, combine them */
170 combined
= combine_entries(payload
, prev
);
174 if (ordered_hashmap_update(h
, i
, combined
) < 0)
179 combined
= memdup(payload
, payload_size
+ 1);
183 if (ordered_hashmap_put(h
, i
, combined
) < 0)
192 int catalog_file_lang(const char* filename
, char **lang
) {
193 char *beg
, *end
, *_lang
;
195 end
= endswith(filename
, ".catalog");
200 while (beg
> filename
&& !IN_SET(*beg
, '.', '/') && end
- beg
< 32)
203 if (*beg
!= '.' || end
<= beg
+ 1)
206 _lang
= strndup(beg
+ 1, end
- beg
- 1);
214 static int catalog_entry_lang(
215 const char* filename
,
226 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
227 "[%s:%u] Language too short.", filename
, line
);
229 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
230 "[%s:%u] language too long.", filename
, line
);
233 if (streq(t
, deflang
)) {
234 log_warning("[%s:%u] language specified unnecessarily", filename
, line
);
238 log_warning("[%s:%u] language differs from default for file", filename
, line
);
249 int catalog_import_file(OrderedHashmap
*h
, const char *path
) {
250 _cleanup_fclose_
FILE *f
= NULL
;
251 _cleanup_free_
char *payload
= NULL
;
252 size_t payload_size
= 0, payload_allocated
= 0;
255 _cleanup_free_
char *deflang
= NULL
, *lang
= NULL
;
256 bool got_id
= false, empty_line
= true;
262 f
= fopen(path
, "re");
264 return log_error_errno(errno
, "Failed to open file %s: %m", path
);
266 r
= catalog_file_lang(path
, &deflang
);
268 log_error_errno(r
, "Failed to determine language for file %s: %m", path
);
270 log_debug("File %s has language %s.", path
, deflang
);
273 _cleanup_free_
char *line
= NULL
;
276 r
= read_line(f
, LONG_LINE_MAX
, &line
);
278 return log_error_errno(r
, "Failed to read file %s: %m", path
);
289 if (strchr(COMMENTS
, line
[0]))
293 strlen(line
) >= 2+1+32 &&
297 IN_SET(line
[2+1+32], ' ', '\0')) {
304 with_language
= line
[2+1+32] != '\0';
307 if (sd_id128_from_string(line
+ 2 + 1, &jd
) >= 0) {
310 if (payload_size
== 0)
311 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
312 "[%s:%u] No payload text.",
316 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
327 t
= strstrip(line
+ 2 + 1 + 32 + 1);
328 r
= catalog_entry_lang(path
, n
, t
, deflang
, &lang
);
343 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
344 "[%s:%u] Got payload before ID.",
347 line_len
= strlen(line
);
348 if (!GREEDY_REALLOC(payload
, payload_allocated
,
349 payload_size
+ (empty_line
? 1 : 0) + line_len
+ 1 + 1))
353 payload
[payload_size
++] = '\n';
354 memcpy(payload
+ payload_size
, line
, line_len
);
355 payload_size
+= line_len
;
356 payload
[payload_size
++] = '\n';
357 payload
[payload_size
] = '\0';
363 if (payload_size
== 0)
364 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
365 "[%s:%u] No payload text.",
368 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
376 static int64_t write_catalog(
377 const char *database
,
382 _cleanup_fclose_
FILE *w
= NULL
;
383 _cleanup_free_
char *p
= NULL
;
384 CatalogHeader header
;
388 r
= mkdir_parents(database
, 0755);
390 return log_error_errno(r
, "Failed to create parent directories of %s: %m", database
);
392 r
= fopen_temporary(database
, &w
, &p
);
394 return log_error_errno(r
, "Failed to open database for writing: %s: %m",
397 header
= (CatalogHeader
) {
398 .signature
= CATALOG_SIGNATURE
,
399 .header_size
= htole64(ALIGN_TO(sizeof(CatalogHeader
), 8)),
400 .catalog_item_size
= htole64(sizeof(CatalogItem
)),
401 .n_items
= htole64(n
),
406 k
= fwrite(&header
, 1, sizeof(header
), w
);
407 if (k
!= sizeof(header
)) {
408 log_error("%s: failed to write header.", p
);
412 k
= fwrite(items
, 1, n
* sizeof(CatalogItem
), w
);
413 if (k
!= n
* sizeof(CatalogItem
)) {
414 log_error("%s: failed to write database.", p
);
418 k
= fwrite(sb
->buf
, 1, sb
->len
, w
);
420 log_error("%s: failed to write strings.", p
);
424 r
= fflush_and_check(w
);
426 log_error_errno(r
, "%s: failed to write database: %m", p
);
430 (void) fchmod(fileno(w
), 0644);
432 if (rename(p
, database
) < 0) {
433 r
= log_error_errno(errno
, "rename (%s -> %s) failed: %m", p
, database
);
444 int catalog_update(const char* database
, const char* root
, const char* const* dirs
) {
445 _cleanup_strv_free_
char **files
= NULL
;
447 _cleanup_(strbuf_freep
) struct strbuf
*sb
= NULL
;
448 _cleanup_ordered_hashmap_free_free_free_ OrderedHashmap
*h
= NULL
;
449 _cleanup_free_ CatalogItem
*items
= NULL
;
457 h
= ordered_hashmap_new(&catalog_hash_ops
);
462 r
= conf_files_list_strv(&files
, ".catalog", root
, 0, dirs
);
464 return log_error_errno(r
, "Failed to get catalog files: %m");
466 STRV_FOREACH(f
, files
) {
467 log_debug("Reading file '%s'", *f
);
468 r
= catalog_import_file(h
, *f
);
470 return log_error_errno(r
, "Failed to import file '%s': %m", *f
);
473 if (ordered_hashmap_size(h
) <= 0) {
474 log_info("No items in catalog.");
477 log_debug("Found %u items in catalog.", ordered_hashmap_size(h
));
479 items
= new(CatalogItem
, ordered_hashmap_size(h
));
484 ORDERED_HASHMAP_FOREACH_KEY(payload
, i
, h
) {
485 log_debug("Found " SD_ID128_FORMAT_STR
", language %s",
486 SD_ID128_FORMAT_VAL(i
->id
),
487 isempty(i
->language
) ? "C" : i
->language
);
489 offset
= strbuf_add_string(sb
, payload
, strlen(payload
));
493 i
->offset
= htole64((uint64_t) offset
);
497 assert(n
== ordered_hashmap_size(h
));
498 typesafe_qsort(items
, n
, catalog_compare_func
);
502 sz
= write_catalog(database
, sb
, items
, n
);
504 return log_error_errno(sz
, "Failed to write %s: %m", database
);
506 log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64
" total size.",
507 database
, n
, sb
->len
, sz
);
511 static int open_mmap(const char *database
, int *_fd
, struct stat
*_st
, void **_p
) {
512 _cleanup_close_
int fd
= -1;
513 const CatalogHeader
*h
;
521 fd
= open(database
, O_RDONLY
|O_CLOEXEC
);
525 if (fstat(fd
, &st
) < 0)
528 if (st
.st_size
< (off_t
) sizeof(CatalogHeader
))
531 p
= mmap(NULL
, PAGE_ALIGN(st
.st_size
), PROT_READ
, MAP_SHARED
, fd
, 0);
536 if (memcmp(h
->signature
, (const uint8_t[]) CATALOG_SIGNATURE
, sizeof(h
->signature
)) != 0 ||
537 le64toh(h
->header_size
) < sizeof(CatalogHeader
) ||
538 le64toh(h
->catalog_item_size
) < sizeof(CatalogItem
) ||
539 h
->incompatible_flags
!= 0 ||
540 le64toh(h
->n_items
) <= 0 ||
541 st
.st_size
< (off_t
) (le64toh(h
->header_size
) + le64toh(h
->catalog_item_size
) * le64toh(h
->n_items
))) {
542 munmap(p
, st
.st_size
);
553 static const char *find_id(void *p
, sd_id128_t id
) {
554 CatalogItem
*f
= NULL
, key
= { .id
= id
};
555 const CatalogHeader
*h
= p
;
558 loc
= setlocale(LC_MESSAGES
, NULL
);
559 if (!isempty(loc
) && !STR_IN_SET(loc
, "C", "POSIX")) {
562 len
= strcspn(loc
, ".@");
563 if (len
> sizeof(key
.language
) - 1)
564 log_debug("LC_MESSAGES value too long, ignoring: \"%.*s\"", (int) len
, loc
);
566 strncpy(key
.language
, loc
, len
);
567 key
.language
[len
] = '\0';
570 (const uint8_t*) p
+ le64toh(h
->header_size
),
572 le64toh(h
->catalog_item_size
),
573 (comparison_fn_t
) catalog_compare_func
);
577 e
= strchr(key
.language
, '_');
581 (const uint8_t*) p
+ le64toh(h
->header_size
),
583 le64toh(h
->catalog_item_size
),
584 (comparison_fn_t
) catalog_compare_func
);
593 (const uint8_t*) p
+ le64toh(h
->header_size
),
595 le64toh(h
->catalog_item_size
),
596 (comparison_fn_t
) catalog_compare_func
);
602 return (const char*) p
+
603 le64toh(h
->header_size
) +
604 le64toh(h
->n_items
) * le64toh(h
->catalog_item_size
) +
608 int catalog_get(const char* database
, sd_id128_t id
, char **_text
) {
609 _cleanup_close_
int fd
= -1;
618 r
= open_mmap(database
, &fd
, &st
, &p
);
639 munmap(p
, st
.st_size
);
644 static char *find_header(const char *s
, const char *header
) {
649 v
= startswith(s
, header
);
651 v
+= strspn(v
, WHITESPACE
);
652 return strndup(v
, strcspn(v
, NEWLINE
));
655 if (!next_header(&s
))
660 static void dump_catalog_entry(FILE *f
, sd_id128_t id
, const char *s
, bool oneline
) {
662 _cleanup_free_
char *subject
= NULL
, *defined_by
= NULL
;
664 subject
= find_header(s
, "Subject:");
665 defined_by
= find_header(s
, "Defined-By:");
667 fprintf(f
, SD_ID128_FORMAT_STR
" %s: %s\n",
668 SD_ID128_FORMAT_VAL(id
),
669 strna(defined_by
), strna(subject
));
671 fprintf(f
, "-- " SD_ID128_FORMAT_STR
"\n%s\n",
672 SD_ID128_FORMAT_VAL(id
), s
);
675 int catalog_list(FILE *f
, const char *database
, bool oneline
) {
676 _cleanup_close_
int fd
= -1;
679 const CatalogHeader
*h
;
680 const CatalogItem
*items
;
684 bool last_id_set
= false;
686 r
= open_mmap(database
, &fd
, &st
, &p
);
691 items
= (const CatalogItem
*) ((const uint8_t*) p
+ le64toh(h
->header_size
));
693 for (n
= 0; n
< le64toh(h
->n_items
); n
++) {
696 if (last_id_set
&& sd_id128_equal(last_id
, items
[n
].id
))
699 assert_se(s
= find_id(p
, items
[n
].id
));
701 dump_catalog_entry(f
, items
[n
].id
, s
, oneline
);
704 last_id
= items
[n
].id
;
707 munmap(p
, st
.st_size
);
712 int catalog_list_items(FILE *f
, const char *database
, bool oneline
, char **items
) {
716 STRV_FOREACH(item
, items
) {
719 _cleanup_free_
char *msg
= NULL
;
721 k
= sd_id128_from_string(*item
, &id
);
723 log_error_errno(k
, "Failed to parse id128 '%s': %m", *item
);
729 k
= catalog_get(database
, id
, &msg
);
731 log_full_errno(k
== -ENOENT
? LOG_NOTICE
: LOG_ERR
, k
,
732 "Failed to retrieve catalog entry for '%s': %m", *item
);
738 dump_catalog_entry(f
, id
, msg
, oneline
);