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
);
129 p
= mempcpy(p
, b1
, n
);
133 p
= mempcpy(p
, b2
, n
);
136 assert(p
- dest
<= (ptrdiff_t)(l1
+ l2
));
141 static int finish_item(
144 const char *language
,
145 char *payload
, size_t payload_size
) {
147 _cleanup_free_ CatalogItem
*i
= NULL
;
148 _cleanup_free_
char *combined
= NULL
;
154 assert(payload_size
> 0);
156 i
= new0(CatalogItem
, 1);
162 assert(strlen(language
) > 1 && strlen(language
) < 32);
163 strcpy(i
->language
, language
);
166 prev
= ordered_hashmap_get(h
, i
);
168 /* Already have such an item, combine them */
169 combined
= combine_entries(payload
, prev
);
173 r
= ordered_hashmap_update(h
, i
, combined
);
181 combined
= memdup(payload
, payload_size
+ 1);
185 r
= ordered_hashmap_put(h
, i
, combined
);
196 int catalog_file_lang(const char* filename
, char **lang
) {
197 char *beg
, *end
, *_lang
;
199 end
= endswith(filename
, ".catalog");
204 while (beg
> filename
&& !IN_SET(*beg
, '.', '/') && end
- beg
< 32)
207 if (*beg
!= '.' || end
<= beg
+ 1)
210 _lang
= strndup(beg
+ 1, end
- beg
- 1);
218 static int catalog_entry_lang(
219 const char* filename
,
230 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
231 "[%s:%u] Language too short.", filename
, line
);
233 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
234 "[%s:%u] language too long.", filename
, line
);
237 if (streq(t
, deflang
)) {
238 log_warning("[%s:%u] language specified unnecessarily", filename
, line
);
242 log_warning("[%s:%u] language differs from default for file", filename
, line
);
253 int catalog_import_file(OrderedHashmap
*h
, const char *path
) {
254 _cleanup_fclose_
FILE *f
= NULL
;
255 _cleanup_free_
char *payload
= NULL
;
256 size_t payload_size
= 0;
259 _cleanup_free_
char *deflang
= NULL
, *lang
= NULL
;
260 bool got_id
= false, empty_line
= true;
266 f
= fopen(path
, "re");
268 return log_error_errno(errno
, "Failed to open file %s: %m", path
);
270 r
= catalog_file_lang(path
, &deflang
);
272 log_error_errno(r
, "Failed to determine language for file %s: %m", path
);
274 log_debug("File %s has language %s.", path
, deflang
);
277 _cleanup_free_
char *line
= NULL
;
280 r
= read_line(f
, LONG_LINE_MAX
, &line
);
282 return log_error_errno(r
, "Failed to read file %s: %m", path
);
293 if (strchr(COMMENTS
, line
[0]))
297 strlen(line
) >= 2+1+32 &&
301 IN_SET(line
[2+1+32], ' ', '\0')) {
308 with_language
= line
[2+1+32] != '\0';
311 if (sd_id128_from_string(line
+ 2 + 1, &jd
) >= 0) {
314 if (payload_size
== 0)
315 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
316 "[%s:%u] No payload text.",
320 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
331 t
= strstrip(line
+ 2 + 1 + 32 + 1);
332 r
= catalog_entry_lang(path
, n
, t
, deflang
, &lang
);
347 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
348 "[%s:%u] Got payload before ID.",
351 line_len
= strlen(line
);
352 if (!GREEDY_REALLOC(payload
, payload_size
+ (empty_line
? 1 : 0) + line_len
+ 1 + 1))
356 payload
[payload_size
++] = '\n';
357 memcpy(payload
+ payload_size
, line
, line_len
);
358 payload_size
+= line_len
;
359 payload
[payload_size
++] = '\n';
360 payload
[payload_size
] = '\0';
366 if (payload_size
== 0)
367 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
368 "[%s:%u] No payload text.",
371 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
379 static int64_t write_catalog(
380 const char *database
,
385 _cleanup_fclose_
FILE *w
= NULL
;
386 _cleanup_free_
char *p
= NULL
;
387 CatalogHeader header
;
391 r
= mkdir_parents(database
, 0755);
393 return log_error_errno(r
, "Failed to create parent directories of %s: %m", database
);
395 r
= fopen_temporary(database
, &w
, &p
);
397 return log_error_errno(r
, "Failed to open database for writing: %s: %m",
400 header
= (CatalogHeader
) {
401 .signature
= CATALOG_SIGNATURE
,
402 .header_size
= htole64(CONST_ALIGN_TO(sizeof(CatalogHeader
), 8)),
403 .catalog_item_size
= htole64(sizeof(CatalogItem
)),
404 .n_items
= htole64(n
),
409 k
= fwrite(&header
, 1, sizeof(header
), w
);
410 if (k
!= sizeof(header
)) {
411 log_error("%s: failed to write header.", p
);
415 k
= fwrite(items
, 1, n
* sizeof(CatalogItem
), w
);
416 if (k
!= n
* sizeof(CatalogItem
)) {
417 log_error("%s: failed to write database.", p
);
421 k
= fwrite(sb
->buf
, 1, sb
->len
, w
);
423 log_error("%s: failed to write strings.", p
);
427 r
= fflush_and_check(w
);
429 log_error_errno(r
, "%s: failed to write database: %m", p
);
433 (void) fchmod(fileno(w
), 0644);
435 if (rename(p
, database
) < 0) {
436 r
= log_error_errno(errno
, "rename (%s -> %s) failed: %m", p
, database
);
447 int catalog_update(const char* database
, const char* root
, const char* const* dirs
) {
448 _cleanup_strv_free_
char **files
= NULL
;
449 _cleanup_(strbuf_freep
) struct strbuf
*sb
= NULL
;
450 _cleanup_ordered_hashmap_free_free_free_ OrderedHashmap
*h
= NULL
;
451 _cleanup_free_ CatalogItem
*items
= NULL
;
459 h
= ordered_hashmap_new(&catalog_hash_ops
);
464 r
= conf_files_list_strv(&files
, ".catalog", root
, 0, dirs
);
466 return log_error_errno(r
, "Failed to get catalog files: %m");
468 STRV_FOREACH(f
, files
) {
469 log_debug("Reading file '%s'", *f
);
470 r
= catalog_import_file(h
, *f
);
472 return log_error_errno(r
, "Failed to import file '%s': %m", *f
);
475 if (ordered_hashmap_size(h
) <= 0) {
476 log_info("No items in catalog.");
479 log_debug("Found %u items in catalog.", ordered_hashmap_size(h
));
481 items
= new(CatalogItem
, ordered_hashmap_size(h
));
486 ORDERED_HASHMAP_FOREACH_KEY(payload
, i
, h
) {
487 log_debug("Found " SD_ID128_FORMAT_STR
", language %s",
488 SD_ID128_FORMAT_VAL(i
->id
),
489 isempty(i
->language
) ? "C" : i
->language
);
491 offset
= strbuf_add_string(sb
, payload
, strlen(payload
));
495 i
->offset
= htole64((uint64_t) offset
);
499 assert(n
== ordered_hashmap_size(h
));
500 typesafe_qsort(items
, n
, catalog_compare_func
);
504 sz
= write_catalog(database
, sb
, items
, n
);
506 return log_error_errno(sz
, "Failed to write %s: %m", database
);
508 log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64
" total size.",
509 database
, n
, sb
->len
, sz
);
513 static int open_mmap(const char *database
, int *_fd
, struct stat
*_st
, void **_p
) {
514 _cleanup_close_
int fd
= -EBADF
;
515 const CatalogHeader
*h
;
523 fd
= open(database
, O_RDONLY
|O_CLOEXEC
);
527 if (fstat(fd
, &st
) < 0)
530 if (st
.st_size
< (off_t
) sizeof(CatalogHeader
) || file_offset_beyond_memory_size(st
.st_size
))
533 p
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
538 if (memcmp(h
->signature
, (const uint8_t[]) CATALOG_SIGNATURE
, sizeof(h
->signature
)) != 0 ||
539 le64toh(h
->header_size
) < sizeof(CatalogHeader
) ||
540 le64toh(h
->catalog_item_size
) < sizeof(CatalogItem
) ||
541 h
->incompatible_flags
!= 0 ||
542 le64toh(h
->n_items
) <= 0 ||
543 st
.st_size
< (off_t
) (le64toh(h
->header_size
) + le64toh(h
->catalog_item_size
) * le64toh(h
->n_items
))) {
544 munmap(p
, st
.st_size
);
555 static const char *find_id(void *p
, sd_id128_t id
) {
556 CatalogItem
*f
= NULL
, key
= { .id
= id
};
557 const CatalogHeader
*h
= p
;
560 loc
= setlocale(LC_MESSAGES
, NULL
);
561 if (!isempty(loc
) && !STR_IN_SET(loc
, "C", "POSIX")) {
564 len
= strcspn(loc
, ".@");
565 if (len
> sizeof(key
.language
) - 1)
566 log_debug("LC_MESSAGES value too long, ignoring: \"%.*s\"", (int) len
, loc
);
568 strncpy(key
.language
, loc
, len
);
569 key
.language
[len
] = '\0';
572 (const uint8_t*) p
+ le64toh(h
->header_size
),
574 le64toh(h
->catalog_item_size
),
575 (comparison_fn_t
) catalog_compare_func
);
579 e
= strchr(key
.language
, '_');
583 (const uint8_t*) p
+ le64toh(h
->header_size
),
585 le64toh(h
->catalog_item_size
),
586 (comparison_fn_t
) catalog_compare_func
);
595 (const uint8_t*) p
+ le64toh(h
->header_size
),
597 le64toh(h
->catalog_item_size
),
598 (comparison_fn_t
) catalog_compare_func
);
604 return (const char*) p
+
605 le64toh(h
->header_size
) +
606 le64toh(h
->n_items
) * le64toh(h
->catalog_item_size
) +
610 int catalog_get(const char* database
, sd_id128_t id
, char **_text
) {
611 _cleanup_close_
int fd
= -EBADF
;
620 r
= open_mmap(database
, &fd
, &st
, &p
);
641 munmap(p
, st
.st_size
);
646 static char *find_header(const char *s
, const char *header
) {
651 v
= startswith(s
, header
);
653 v
+= strspn(v
, WHITESPACE
);
654 return strndup(v
, strcspn(v
, NEWLINE
));
657 if (!next_header(&s
))
662 static void dump_catalog_entry(FILE *f
, sd_id128_t id
, const char *s
, bool oneline
) {
664 _cleanup_free_
char *subject
= NULL
, *defined_by
= NULL
;
666 subject
= find_header(s
, "Subject:");
667 defined_by
= find_header(s
, "Defined-By:");
669 fprintf(f
, SD_ID128_FORMAT_STR
" %s: %s\n",
670 SD_ID128_FORMAT_VAL(id
),
671 strna(defined_by
), strna(subject
));
673 fprintf(f
, "-- " SD_ID128_FORMAT_STR
"\n%s\n",
674 SD_ID128_FORMAT_VAL(id
), s
);
677 int catalog_list(FILE *f
, const char *database
, bool oneline
) {
678 _cleanup_close_
int fd
= -EBADF
;
681 const CatalogHeader
*h
;
682 const CatalogItem
*items
;
686 bool last_id_set
= false;
688 r
= open_mmap(database
, &fd
, &st
, &p
);
693 items
= (const CatalogItem
*) ((const uint8_t*) p
+ le64toh(h
->header_size
));
695 for (n
= 0; n
< le64toh(h
->n_items
); n
++) {
698 if (last_id_set
&& sd_id128_equal(last_id
, items
[n
].id
))
701 assert_se(s
= find_id(p
, items
[n
].id
));
703 dump_catalog_entry(f
, items
[n
].id
, s
, oneline
);
706 last_id
= items
[n
].id
;
709 munmap(p
, st
.st_size
);
714 int catalog_list_items(FILE *f
, const char *database
, bool oneline
, char **items
) {
717 STRV_FOREACH(item
, items
) {
720 _cleanup_free_
char *msg
= NULL
;
722 k
= sd_id128_from_string(*item
, &id
);
724 log_error_errno(k
, "Failed to parse id128 '%s': %m", *item
);
730 k
= catalog_get(database
, id
, &msg
);
732 log_full_errno(k
== -ENOENT
? LOG_NOTICE
: LOG_ERR
, k
,
733 "Failed to retrieve catalog entry for '%s': %m", *item
);
739 dump_catalog_entry(f
, id
, msg
, oneline
);