1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
14 #include "alloc-util.h"
16 #include "conf-files.h"
22 #include "memory-util.h"
24 #include "path-util.h"
25 #include "siphash24.h"
26 #include "sort-util.h"
27 #include "sparse-endian.h"
29 #include "string-util.h"
31 #include "tmpfile-util.h"
33 const char * const catalog_file_dirs
[] = {
34 "/usr/local/lib/systemd/catalog/",
35 "/usr/lib/systemd/catalog/",
39 #define CATALOG_SIGNATURE { 'R', 'H', 'H', 'H', 'K', 'S', 'L', 'P' }
41 typedef struct CatalogHeader
{
42 uint8_t signature
[8]; /* "RHHHKSLP" */
43 le32_t compatible_flags
;
44 le32_t incompatible_flags
;
47 le64_t catalog_item_size
;
50 typedef struct CatalogItem
{
52 char language
[32]; /* One byte is used for termination, so the maximum allowed
53 * length of the string is actually 31 bytes. */
57 static void catalog_hash_func(const CatalogItem
*i
, struct siphash
*state
) {
58 siphash24_compress(&i
->id
, sizeof(i
->id
), state
);
59 siphash24_compress_string(i
->language
, state
);
62 static int catalog_compare_func(const CatalogItem
*a
, const CatalogItem
*b
) {
66 for (k
= 0; k
< ELEMENTSOF(b
->id
.bytes
); k
++) {
67 r
= CMP(a
->id
.bytes
[k
], b
->id
.bytes
[k
]);
72 return strcmp(a
->language
, b
->language
);
75 DEFINE_HASH_OPS(catalog_hash_ops
, CatalogItem
, catalog_hash_func
, catalog_compare_func
);
77 static bool next_header(const char **s
) {
94 static const char *skip_header(const char *s
) {
95 while (next_header(&s
))
100 static char *combine_entries(const char *one
, const char *two
) {
105 /* Find split point of headers to body */
106 b1
= skip_header(one
);
107 b2
= skip_header(two
);
111 dest
= new(char, l1
+ l2
+ 1);
119 /* Headers from @one */
121 p
= mempcpy(p
, one
, n
);
123 /* Headers from @two, these will only be found if not present above */
125 p
= mempcpy(p
, two
, n
);
130 p
= mempcpy(p
, b1
, n
);
134 p
= mempcpy(p
, b2
, n
);
137 assert(p
- dest
<= (ptrdiff_t)(l1
+ l2
));
142 static int finish_item(
145 const char *language
,
146 char *payload
, size_t payload_size
) {
148 _cleanup_free_ CatalogItem
*i
= NULL
;
149 _cleanup_free_
char *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 r
= ordered_hashmap_update(h
, i
, combined
);
176 return log_error_errno(r
, "Failed to update catalog item: %m");
182 combined
= memdup(payload
, payload_size
+ 1);
186 r
= ordered_hashmap_put(h
, i
, combined
);
188 return log_error_errno(r
, "Failed to insert catalog item: %m");
197 int catalog_file_lang(const char* filename
, char **lang
) {
198 char *beg
, *end
, *_lang
;
200 end
= endswith(filename
, ".catalog");
205 while (beg
> filename
&& !IN_SET(*beg
, '.', '/') && end
- beg
< 32)
208 if (*beg
!= '.' || end
<= beg
+ 1)
211 _lang
= strndup(beg
+ 1, end
- beg
- 1);
219 static int catalog_entry_lang(
220 const char* filename
,
231 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
232 "[%s:%u] Language too short.", filename
, line
);
234 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
235 "[%s:%u] language too long.", filename
, line
);
238 if (streq(t
, deflang
)) {
239 log_warning("[%s:%u] language specified unnecessarily", filename
, line
);
243 log_warning("[%s:%u] language differs from default for file", filename
, line
);
254 int catalog_import_file(OrderedHashmap
*h
, const char *path
) {
255 _cleanup_fclose_
FILE *f
= NULL
;
256 _cleanup_free_
char *payload
= NULL
;
257 size_t payload_size
= 0;
260 _cleanup_free_
char *deflang
= NULL
, *lang
= NULL
;
261 bool got_id
= false, empty_line
= true;
267 f
= fopen(path
, "re");
269 return log_error_errno(errno
, "Failed to open file %s: %m", path
);
271 r
= catalog_file_lang(path
, &deflang
);
273 log_error_errno(r
, "Failed to determine language for file %s: %m", path
);
275 log_debug("File %s has language %s.", path
, deflang
);
278 _cleanup_free_
char *line
= NULL
;
281 r
= read_line(f
, LONG_LINE_MAX
, &line
);
283 return log_error_errno(r
, "Failed to read file %s: %m", path
);
294 if (strchr(COMMENTS
, line
[0]))
298 strlen(line
) >= 2+1+32 &&
302 IN_SET(line
[2+1+32], ' ', '\0')) {
309 with_language
= line
[2+1+32] != '\0';
312 if (sd_id128_from_string(line
+ 2 + 1, &jd
) >= 0) {
315 if (payload_size
== 0)
316 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
317 "[%s:%u] No payload text.",
321 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
332 t
= strstrip(line
+ 2 + 1 + 32 + 1);
333 r
= catalog_entry_lang(path
, n
, t
, deflang
, &lang
);
348 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
349 "[%s:%u] Got payload before ID.",
352 line_len
= strlen(line
);
353 if (!GREEDY_REALLOC(payload
, payload_size
+ (empty_line
? 1 : 0) + line_len
+ 1 + 1))
357 payload
[payload_size
++] = '\n';
358 memcpy(payload
+ payload_size
, line
, line_len
);
359 payload_size
+= line_len
;
360 payload
[payload_size
++] = '\n';
361 payload
[payload_size
] = '\0';
367 if (payload_size
== 0)
368 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
369 "[%s:%u] No payload text.",
372 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
380 static int64_t write_catalog(
381 const char *database
,
386 _cleanup_(unlink_and_freep
) char *p
= NULL
;
387 _cleanup_fclose_
FILE *w
= NULL
;
390 r
= mkdir_parents(database
, 0755);
392 return log_error_errno(r
, "Failed to create parent directories of %s: %m", database
);
394 r
= fopen_temporary(database
, &w
, &p
);
396 return log_error_errno(r
, "Failed to open database for writing: %s: %m", database
);
398 CatalogHeader header
= {
399 .signature
= CATALOG_SIGNATURE
,
400 .header_size
= htole64(CONST_ALIGN_TO(sizeof(CatalogHeader
), 8)),
401 .catalog_item_size
= htole64(sizeof(CatalogItem
)),
402 .n_items
= htole64(n
),
405 if (fwrite(&header
, sizeof(header
), 1, w
) != 1)
406 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "%s: failed to write header.", p
);
408 if (fwrite(items
, sizeof(CatalogItem
), n
, w
) != n
)
409 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "%s: failed to write database.", p
);
411 if (fwrite(sb
->buf
, sb
->len
, 1, w
) != 1)
412 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "%s: failed to write strings.", p
);
414 r
= fflush_and_check(w
);
416 return log_error_errno(r
, "%s: failed to write database: %m", p
);
418 (void) fchmod(fileno(w
), 0644);
420 if (rename(p
, database
) < 0)
421 return log_error_errno(errno
, "rename (%s -> %s) failed: %m", p
, database
);
423 p
= mfree(p
); /* free without unlinking */
427 int catalog_update(const char* database
, const char* root
, const char* const* dirs
) {
428 _cleanup_strv_free_
char **files
= NULL
;
429 _cleanup_(strbuf_freep
) struct strbuf
*sb
= NULL
;
430 _cleanup_ordered_hashmap_free_free_free_ OrderedHashmap
*h
= NULL
;
431 _cleanup_free_ CatalogItem
*items
= NULL
;
439 h
= ordered_hashmap_new(&catalog_hash_ops
);
444 r
= conf_files_list_strv(&files
, ".catalog", root
, 0, dirs
);
446 return log_error_errno(r
, "Failed to get catalog files: %m");
448 STRV_FOREACH(f
, files
) {
449 log_debug("Reading file '%s'", *f
);
450 r
= catalog_import_file(h
, *f
);
452 return log_error_errno(r
, "Failed to import file '%s': %m", *f
);
455 if (ordered_hashmap_size(h
) <= 0) {
456 log_info("No items in catalog.");
459 log_debug("Found %u items in catalog.", ordered_hashmap_size(h
));
461 items
= new(CatalogItem
, ordered_hashmap_size(h
));
466 ORDERED_HASHMAP_FOREACH_KEY(payload
, i
, h
) {
467 log_trace("Found " SD_ID128_FORMAT_STR
", language %s",
468 SD_ID128_FORMAT_VAL(i
->id
),
469 isempty(i
->language
) ? "C" : i
->language
);
471 offset
= strbuf_add_string(sb
, payload
, strlen(payload
));
475 i
->offset
= htole64((uint64_t) offset
);
479 assert(n
== ordered_hashmap_size(h
));
480 typesafe_qsort(items
, n
, catalog_compare_func
);
484 sz
= write_catalog(database
, sb
, items
, n
);
486 return log_error_errno(sz
, "Failed to write %s: %m", database
);
488 log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64
" total size.",
489 database
, n
, sb
->len
, sz
);
493 static int open_mmap(const char *database
, int *_fd
, struct stat
*_st
, void **_p
) {
494 _cleanup_close_
int fd
= -EBADF
;
495 const CatalogHeader
*h
;
503 fd
= open(database
, O_RDONLY
|O_CLOEXEC
);
507 if (fstat(fd
, &st
) < 0)
510 if (st
.st_size
< (off_t
) sizeof(CatalogHeader
) || file_offset_beyond_memory_size(st
.st_size
))
513 p
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
518 if (memcmp(h
->signature
, (const uint8_t[]) CATALOG_SIGNATURE
, sizeof(h
->signature
)) != 0 ||
519 le64toh(h
->header_size
) < sizeof(CatalogHeader
) ||
520 le64toh(h
->catalog_item_size
) < sizeof(CatalogItem
) ||
521 h
->incompatible_flags
!= 0 ||
522 le64toh(h
->n_items
) <= 0 ||
523 st
.st_size
< (off_t
) (le64toh(h
->header_size
) + le64toh(h
->catalog_item_size
) * le64toh(h
->n_items
))) {
524 munmap(p
, st
.st_size
);
535 static const char *find_id(void *p
, sd_id128_t id
) {
536 CatalogItem
*f
= NULL
, key
= { .id
= id
};
537 const CatalogHeader
*h
= p
;
540 loc
= setlocale(LC_MESSAGES
, NULL
);
541 if (!isempty(loc
) && !STR_IN_SET(loc
, "C", "POSIX")) {
544 len
= strcspn(loc
, ".@");
545 if (len
> sizeof(key
.language
) - 1)
546 log_debug("LC_MESSAGES value too long, ignoring: \"%.*s\"", (int) len
, loc
);
548 strncpy(key
.language
, loc
, len
);
549 key
.language
[len
] = '\0';
552 (const uint8_t*) p
+ le64toh(h
->header_size
),
554 le64toh(h
->catalog_item_size
),
555 (comparison_fn_t
) catalog_compare_func
);
559 e
= strchr(key
.language
, '_');
563 (const uint8_t*) p
+ le64toh(h
->header_size
),
565 le64toh(h
->catalog_item_size
),
566 (comparison_fn_t
) catalog_compare_func
);
575 (const uint8_t*) p
+ le64toh(h
->header_size
),
577 le64toh(h
->catalog_item_size
),
578 (comparison_fn_t
) catalog_compare_func
);
584 return (const char*) p
+
585 le64toh(h
->header_size
) +
586 le64toh(h
->n_items
) * le64toh(h
->catalog_item_size
) +
590 int catalog_get(const char* database
, sd_id128_t id
, char **_text
) {
591 _cleanup_close_
int fd
= -EBADF
;
600 r
= open_mmap(database
, &fd
, &st
, &p
);
621 munmap(p
, st
.st_size
);
626 static char *find_header(const char *s
, const char *header
) {
631 v
= startswith(s
, header
);
633 v
+= strspn(v
, WHITESPACE
);
634 return strndup(v
, strcspn(v
, NEWLINE
));
637 if (!next_header(&s
))
642 static void dump_catalog_entry(FILE *f
, sd_id128_t id
, const char *s
, bool oneline
) {
644 _cleanup_free_
char *subject
= NULL
, *defined_by
= NULL
;
646 subject
= find_header(s
, "Subject:");
647 defined_by
= find_header(s
, "Defined-By:");
649 fprintf(f
, SD_ID128_FORMAT_STR
" %s: %s\n",
650 SD_ID128_FORMAT_VAL(id
),
651 strna(defined_by
), strna(subject
));
653 fprintf(f
, "-- " SD_ID128_FORMAT_STR
"\n%s\n",
654 SD_ID128_FORMAT_VAL(id
), s
);
657 int catalog_list(FILE *f
, const char *database
, bool oneline
) {
658 _cleanup_close_
int fd
= -EBADF
;
661 const CatalogHeader
*h
;
662 const CatalogItem
*items
;
666 bool last_id_set
= false;
668 r
= open_mmap(database
, &fd
, &st
, &p
);
673 items
= (const CatalogItem
*) ((const uint8_t*) p
+ le64toh(h
->header_size
));
675 for (n
= 0; n
< le64toh(h
->n_items
); n
++) {
678 if (last_id_set
&& sd_id128_equal(last_id
, items
[n
].id
))
681 assert_se(s
= find_id(p
, items
[n
].id
));
683 dump_catalog_entry(f
, items
[n
].id
, s
, oneline
);
686 last_id
= items
[n
].id
;
689 munmap(p
, st
.st_size
);
694 int catalog_list_items(FILE *f
, const char *database
, bool oneline
, char **items
) {
697 STRV_FOREACH(item
, items
) {
700 _cleanup_free_
char *msg
= NULL
;
702 k
= sd_id128_from_string(*item
, &id
);
704 log_error_errno(k
, "Failed to parse id128 '%s': %m", *item
);
710 k
= catalog_get(database
, id
, &msg
);
712 log_full_errno(k
== -ENOENT
? LOG_NOTICE
: LOG_ERR
, k
,
713 "Failed to retrieve catalog entry for '%s': %m", *item
);
719 dump_catalog_entry(f
, id
, msg
, oneline
);