1 /* SPDX-License-Identifier: LGPL-2.1+ */
13 #include "alloc-util.h"
15 #include "conf-files.h"
21 #include "path-util.h"
22 #include "siphash24.h"
23 #include "sparse-endian.h"
25 #include "string-util.h"
27 #include "tmpfile-util.h"
30 const char * const catalog_file_dirs
[] = {
31 "/usr/local/lib/systemd/catalog/",
32 "/usr/lib/systemd/catalog/",
36 #define CATALOG_SIGNATURE (uint8_t[]) { 'R', 'H', 'H', 'H', 'K', 'S', 'L', 'P' }
38 typedef struct CatalogHeader
{
39 uint8_t signature
[8]; /* "RHHHKSLP" */
40 le32_t compatible_flags
;
41 le32_t incompatible_flags
;
44 le64_t catalog_item_size
;
47 typedef struct CatalogItem
{
49 char language
[32]; /* One byte is used for termination, so the maximum allowed
50 * length of the string is actually 31 bytes. */
54 static void catalog_hash_func(const CatalogItem
*i
, struct siphash
*state
) {
55 siphash24_compress(&i
->id
, sizeof(i
->id
), state
);
56 siphash24_compress(i
->language
, strlen(i
->language
), state
);
59 static int catalog_compare_func(const CatalogItem
*a
, const CatalogItem
*b
) {
63 for (k
= 0; k
< ELEMENTSOF(b
->id
.bytes
); k
++) {
64 r
= CMP(a
->id
.bytes
[k
], b
->id
.bytes
[k
]);
69 return strcmp(a
->language
, b
->language
);
72 DEFINE_HASH_OPS(catalog_hash_ops
, CatalogItem
, catalog_hash_func
, catalog_compare_func
);
74 static bool next_header(const char **s
) {
91 static const char *skip_header(const char *s
) {
92 while (next_header(&s
))
97 static char *combine_entries(const char *one
, const char *two
) {
102 /* Find split point of headers to body */
103 b1
= skip_header(one
);
104 b2
= skip_header(two
);
108 dest
= new(char, l1
+ l2
+ 1);
116 /* Headers from @one */
118 p
= mempcpy(p
, one
, n
);
120 /* Headers from @two, these will only be found if not present above */
122 p
= mempcpy(p
, two
, 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 *prev
= NULL
, *combined
= NULL
;
153 assert(payload_size
> 0);
155 i
= new0(CatalogItem
, 1);
161 assert(strlen(language
) > 1 && strlen(language
) < 32);
162 strcpy(i
->language
, language
);
165 prev
= hashmap_get(h
, i
);
167 /* Already have such an item, combine them */
168 combined
= combine_entries(payload
, prev
);
172 if (hashmap_update(h
, i
, combined
) < 0)
177 combined
= memdup(payload
, payload_size
+ 1);
181 if (hashmap_put(h
, i
, combined
) < 0)
190 int catalog_file_lang(const char* filename
, char **lang
) {
191 char *beg
, *end
, *_lang
;
193 end
= endswith(filename
, ".catalog");
198 while (beg
> filename
&& !IN_SET(*beg
, '.', '/') && end
- beg
< 32)
201 if (*beg
!= '.' || end
<= beg
+ 1)
204 _lang
= strndup(beg
+ 1, end
- beg
- 1);
212 static int catalog_entry_lang(const char* filename
, int line
,
213 const char* t
, const char* deflang
, char **lang
) {
218 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
219 "[%s:%u] Language too short.",
222 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
223 "[%s:%u] language too long.", filename
,
227 if (streq(t
, deflang
)) {
228 log_warning("[%s:%u] language specified unnecessarily",
232 log_warning("[%s:%u] language differs from default for file",
243 int catalog_import_file(Hashmap
*h
, const char *path
) {
244 _cleanup_fclose_
FILE *f
= NULL
;
245 _cleanup_free_
char *payload
= NULL
;
246 size_t payload_size
= 0, payload_allocated
= 0;
249 _cleanup_free_
char *deflang
= NULL
, *lang
= NULL
;
250 bool got_id
= false, empty_line
= true;
256 f
= fopen(path
, "re");
258 return log_error_errno(errno
, "Failed to open file %s: %m", path
);
260 r
= catalog_file_lang(path
, &deflang
);
262 log_error_errno(r
, "Failed to determine language for file %s: %m", path
);
264 log_debug("File %s has language %s.", path
, deflang
);
267 _cleanup_free_
char *line
= NULL
;
270 r
= read_line(f
, LONG_LINE_MAX
, &line
);
272 return log_error_errno(r
, "Failed to read file %s: %m", path
);
283 if (strchr(COMMENTS
, line
[0]))
287 strlen(line
) >= 2+1+32 &&
291 IN_SET(line
[2+1+32], ' ', '\0')) {
298 with_language
= line
[2+1+32] != '\0';
301 if (sd_id128_from_string(line
+ 2 + 1, &jd
) >= 0) {
304 if (payload_size
== 0)
305 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
306 "[%s:%u] No payload text.",
310 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
321 t
= strstrip(line
+ 2 + 1 + 32 + 1);
322 r
= catalog_entry_lang(path
, n
, t
, deflang
, &lang
);
337 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
338 "[%s:%u] Got payload before ID.",
341 line_len
= strlen(line
);
342 if (!GREEDY_REALLOC(payload
, payload_allocated
,
343 payload_size
+ (empty_line
? 1 : 0) + line_len
+ 1 + 1))
347 payload
[payload_size
++] = '\n';
348 memcpy(payload
+ payload_size
, line
, line_len
);
349 payload_size
+= line_len
;
350 payload
[payload_size
++] = '\n';
351 payload
[payload_size
] = '\0';
357 if (payload_size
== 0)
358 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
359 "[%s:%u] No payload text.",
362 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
370 static int64_t write_catalog(const char *database
, struct strbuf
*sb
,
371 CatalogItem
*items
, size_t n
) {
372 CatalogHeader header
;
373 _cleanup_fclose_
FILE *w
= NULL
;
375 _cleanup_free_
char *d
, *p
= NULL
;
378 d
= dirname_malloc(database
);
382 r
= mkdir_p(d
, 0775);
384 return log_error_errno(r
, "Recursive mkdir %s: %m", d
);
386 r
= fopen_temporary(database
, &w
, &p
);
388 return log_error_errno(r
, "Failed to open database for writing: %s: %m",
392 memcpy(header
.signature
, CATALOG_SIGNATURE
, sizeof(header
.signature
));
393 header
.header_size
= htole64(ALIGN_TO(sizeof(CatalogHeader
), 8));
394 header
.catalog_item_size
= htole64(sizeof(CatalogItem
));
395 header
.n_items
= htole64(n
);
399 k
= fwrite(&header
, 1, sizeof(header
), w
);
400 if (k
!= sizeof(header
)) {
401 log_error("%s: failed to write header.", p
);
405 k
= fwrite(items
, 1, n
* sizeof(CatalogItem
), w
);
406 if (k
!= n
* sizeof(CatalogItem
)) {
407 log_error("%s: failed to write database.", p
);
411 k
= fwrite(sb
->buf
, 1, sb
->len
, w
);
413 log_error("%s: failed to write strings.", p
);
417 r
= fflush_and_check(w
);
419 log_error_errno(r
, "%s: failed to write database: %m", p
);
423 fchmod(fileno(w
), 0644);
425 if (rename(p
, database
) < 0) {
426 r
= log_error_errno(errno
, "rename (%s -> %s) failed: %m", p
, database
);
437 int catalog_update(const char* database
, const char* root
, const char* const* dirs
) {
438 _cleanup_strv_free_
char **files
= NULL
;
440 _cleanup_(strbuf_cleanupp
) struct strbuf
*sb
= NULL
;
441 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
442 _cleanup_free_ CatalogItem
*items
= NULL
;
451 h
= hashmap_new(&catalog_hash_ops
);
456 r
= conf_files_list_strv(&files
, ".catalog", root
, 0, dirs
);
458 return log_error_errno(r
, "Failed to get catalog files: %m");
460 STRV_FOREACH(f
, files
) {
461 log_debug("Reading file '%s'", *f
);
462 r
= catalog_import_file(h
, *f
);
464 return log_error_errno(r
, "Failed to import file '%s': %m", *f
);
467 if (hashmap_size(h
) <= 0) {
468 log_info("No items in catalog.");
471 log_debug("Found %u items in catalog.", hashmap_size(h
));
473 items
= new(CatalogItem
, hashmap_size(h
));
478 HASHMAP_FOREACH_KEY(payload
, i
, h
, j
) {
479 log_debug("Found " SD_ID128_FORMAT_STR
", language %s",
480 SD_ID128_FORMAT_VAL(i
->id
),
481 isempty(i
->language
) ? "C" : i
->language
);
483 offset
= strbuf_add_string(sb
, payload
, strlen(payload
));
487 i
->offset
= htole64((uint64_t) offset
);
491 assert(n
== hashmap_size(h
));
492 typesafe_qsort(items
, n
, catalog_compare_func
);
496 sz
= write_catalog(database
, sb
, items
, n
);
498 return log_error_errno(sz
, "Failed to write %s: %m", database
);
500 log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64
" total size.",
501 database
, n
, sb
->len
, sz
);
505 static int open_mmap(const char *database
, int *_fd
, struct stat
*_st
, void **_p
) {
506 const CatalogHeader
*h
;
515 fd
= open(database
, O_RDONLY
|O_CLOEXEC
);
519 if (fstat(fd
, &st
) < 0) {
524 if (st
.st_size
< (off_t
) sizeof(CatalogHeader
)) {
529 p
= mmap(NULL
, PAGE_ALIGN(st
.st_size
), PROT_READ
, MAP_SHARED
, fd
, 0);
530 if (p
== MAP_FAILED
) {
536 if (memcmp(h
->signature
, 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
))) {
543 munmap(p
, st
.st_size
);
554 static const char *find_id(void *p
, sd_id128_t id
) {
555 CatalogItem
*f
= NULL
, key
= { .id
= id
};
556 const CatalogHeader
*h
= p
;
559 loc
= setlocale(LC_MESSAGES
, NULL
);
560 if (!isempty(loc
) && !STR_IN_SET(loc
, "C", "POSIX")) {
563 len
= strcspn(loc
, ".@");
564 if (len
> sizeof(key
.language
) - 1)
565 log_debug("LC_MESSAGES value too long, ignoring: \"%.*s\"", (int) len
, loc
);
567 strncpy(key
.language
, loc
, len
);
568 key
.language
[len
] = '\0';
571 (const uint8_t*) p
+ le64toh(h
->header_size
),
573 le64toh(h
->catalog_item_size
),
574 (comparison_fn_t
) catalog_compare_func
);
578 e
= strchr(key
.language
, '_');
582 (const uint8_t*) p
+ le64toh(h
->header_size
),
584 le64toh(h
->catalog_item_size
),
585 (comparison_fn_t
) catalog_compare_func
);
594 (const uint8_t*) p
+ le64toh(h
->header_size
),
596 le64toh(h
->catalog_item_size
),
597 (comparison_fn_t
) catalog_compare_func
);
603 return (const char*) p
+
604 le64toh(h
->header_size
) +
605 le64toh(h
->n_items
) * le64toh(h
->catalog_item_size
) +
609 int catalog_get(const char* database
, sd_id128_t id
, char **_text
) {
610 _cleanup_close_
int fd
= -1;
619 r
= open_mmap(database
, &fd
, &st
, &p
);
640 munmap(p
, st
.st_size
);
645 static char *find_header(const char *s
, const char *header
) {
650 v
= startswith(s
, header
);
652 v
+= strspn(v
, WHITESPACE
);
653 return strndup(v
, strcspn(v
, NEWLINE
));
656 if (!next_header(&s
))
661 static void dump_catalog_entry(FILE *f
, sd_id128_t id
, const char *s
, bool oneline
) {
663 _cleanup_free_
char *subject
= NULL
, *defined_by
= NULL
;
665 subject
= find_header(s
, "Subject:");
666 defined_by
= find_header(s
, "Defined-By:");
668 fprintf(f
, SD_ID128_FORMAT_STR
" %s: %s\n",
669 SD_ID128_FORMAT_VAL(id
),
670 strna(defined_by
), strna(subject
));
672 fprintf(f
, "-- " SD_ID128_FORMAT_STR
"\n%s\n",
673 SD_ID128_FORMAT_VAL(id
), s
);
676 int catalog_list(FILE *f
, const char *database
, bool oneline
) {
677 _cleanup_close_
int fd
= -1;
680 const CatalogHeader
*h
;
681 const CatalogItem
*items
;
685 bool last_id_set
= false;
687 r
= open_mmap(database
, &fd
, &st
, &p
);
692 items
= (const CatalogItem
*) ((const uint8_t*) p
+ le64toh(h
->header_size
));
694 for (n
= 0; n
< le64toh(h
->n_items
); n
++) {
697 if (last_id_set
&& sd_id128_equal(last_id
, items
[n
].id
))
700 assert_se(s
= find_id(p
, items
[n
].id
));
702 dump_catalog_entry(f
, items
[n
].id
, s
, oneline
);
705 last_id
= items
[n
].id
;
708 munmap(p
, st
.st_size
);
713 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
);