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"
29 const char * const catalog_file_dirs
[] = {
30 "/usr/local/lib/systemd/catalog/",
31 "/usr/lib/systemd/catalog/",
35 #define CATALOG_SIGNATURE (uint8_t[]) { 'R', 'H', 'H', 'H', 'K', 'S', 'L', 'P' }
37 typedef struct CatalogHeader
{
38 uint8_t signature
[8]; /* "RHHHKSLP" */
39 le32_t compatible_flags
;
40 le32_t incompatible_flags
;
43 le64_t catalog_item_size
;
46 typedef struct CatalogItem
{
52 static void catalog_hash_func(const void *p
, struct siphash
*state
) {
53 const CatalogItem
*i
= p
;
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 const struct hash_ops catalog_hash_ops
= {
73 .hash
= catalog_hash_func
,
74 .compare
= (comparison_fn_t
) 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
);
140 assert(p
- dest
<= (ptrdiff_t)(l1
+ l2
));
145 static int finish_item(
148 const char *language
,
149 char *payload
, size_t payload_size
) {
151 _cleanup_free_ CatalogItem
*i
= NULL
;
152 _cleanup_free_
char *prev
= NULL
, *combined
= NULL
;
156 assert(payload_size
> 0);
158 i
= new0(CatalogItem
, 1);
164 assert(strlen(language
) > 1 && strlen(language
) < 32);
165 strcpy(i
->language
, language
);
168 prev
= hashmap_get(h
, i
);
170 /* Already have such an item, combine them */
171 combined
= combine_entries(payload
, prev
);
175 if (hashmap_update(h
, i
, combined
) < 0)
180 combined
= memdup(payload
, payload_size
+ 1);
184 if (hashmap_put(h
, i
, combined
) < 0)
193 int catalog_file_lang(const char* filename
, char **lang
) {
194 char *beg
, *end
, *_lang
;
196 end
= endswith(filename
, ".catalog");
201 while (beg
> filename
&& !IN_SET(*beg
, '.', '/') && end
- beg
< 32)
204 if (*beg
!= '.' || end
<= beg
+ 1)
207 _lang
= strndup(beg
+ 1, end
- beg
- 1);
215 static int catalog_entry_lang(const char* filename
, int line
,
216 const char* t
, const char* deflang
, char **lang
) {
221 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
222 "[%s:%u] Language too short.",
225 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
226 "[%s:%u] language too long.", filename
,
230 if (streq(t
, deflang
)) {
231 log_warning("[%s:%u] language specified unnecessarily",
235 log_warning("[%s:%u] language differs from default for file",
246 int catalog_import_file(Hashmap
*h
, const char *path
) {
247 _cleanup_fclose_
FILE *f
= NULL
;
248 _cleanup_free_
char *payload
= NULL
;
249 size_t payload_size
= 0, payload_allocated
= 0;
252 _cleanup_free_
char *deflang
= NULL
, *lang
= NULL
;
253 bool got_id
= false, empty_line
= true;
259 f
= fopen(path
, "re");
261 return log_error_errno(errno
, "Failed to open file %s: %m", path
);
263 r
= catalog_file_lang(path
, &deflang
);
265 log_error_errno(r
, "Failed to determine language for file %s: %m", path
);
267 log_debug("File %s has language %s.", path
, deflang
);
270 _cleanup_free_
char *line
= NULL
;
273 r
= read_line(f
, LONG_LINE_MAX
, &line
);
275 return log_error_errno(r
, "Failed to read file %s: %m", path
);
286 if (strchr(COMMENTS
, line
[0]))
290 strlen(line
) >= 2+1+32 &&
294 IN_SET(line
[2+1+32], ' ', '\0')) {
301 with_language
= line
[2+1+32] != '\0';
304 if (sd_id128_from_string(line
+ 2 + 1, &jd
) >= 0) {
307 if (payload_size
== 0)
308 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
309 "[%s:%u] No payload text.",
313 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
324 t
= strstrip(line
+ 2 + 1 + 32 + 1);
325 r
= catalog_entry_lang(path
, n
, t
, deflang
, &lang
);
340 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
341 "[%s:%u] Got payload before ID.",
344 line_len
= strlen(line
);
345 if (!GREEDY_REALLOC(payload
, payload_allocated
,
346 payload_size
+ (empty_line
? 1 : 0) + line_len
+ 1 + 1))
350 payload
[payload_size
++] = '\n';
351 memcpy(payload
+ payload_size
, line
, line_len
);
352 payload_size
+= line_len
;
353 payload
[payload_size
++] = '\n';
354 payload
[payload_size
] = '\0';
360 if (payload_size
== 0)
361 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
362 "[%s:%u] No payload text.",
365 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
373 static int64_t write_catalog(const char *database
, struct strbuf
*sb
,
374 CatalogItem
*items
, size_t n
) {
375 CatalogHeader header
;
376 _cleanup_fclose_
FILE *w
= NULL
;
378 _cleanup_free_
char *d
, *p
= NULL
;
381 d
= dirname_malloc(database
);
385 r
= mkdir_p(d
, 0775);
387 return log_error_errno(r
, "Recursive mkdir %s: %m", d
);
389 r
= fopen_temporary(database
, &w
, &p
);
391 return log_error_errno(r
, "Failed to open database for writing: %s: %m",
395 memcpy(header
.signature
, CATALOG_SIGNATURE
, sizeof(header
.signature
));
396 header
.header_size
= htole64(ALIGN_TO(sizeof(CatalogHeader
), 8));
397 header
.catalog_item_size
= htole64(sizeof(CatalogItem
));
398 header
.n_items
= htole64(n
);
402 k
= fwrite(&header
, 1, sizeof(header
), w
);
403 if (k
!= sizeof(header
)) {
404 log_error("%s: failed to write header.", p
);
408 k
= fwrite(items
, 1, n
* sizeof(CatalogItem
), w
);
409 if (k
!= n
* sizeof(CatalogItem
)) {
410 log_error("%s: failed to write database.", p
);
414 k
= fwrite(sb
->buf
, 1, sb
->len
, w
);
416 log_error("%s: failed to write strings.", p
);
420 r
= fflush_and_check(w
);
422 log_error_errno(r
, "%s: failed to write database: %m", p
);
426 fchmod(fileno(w
), 0644);
428 if (rename(p
, database
) < 0) {
429 r
= log_error_errno(errno
, "rename (%s -> %s) failed: %m", p
, database
);
440 int catalog_update(const char* database
, const char* root
, const char* const* dirs
) {
441 _cleanup_strv_free_
char **files
= NULL
;
443 _cleanup_(strbuf_cleanupp
) struct strbuf
*sb
= NULL
;
444 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
445 _cleanup_free_ CatalogItem
*items
= NULL
;
454 h
= hashmap_new(&catalog_hash_ops
);
459 r
= conf_files_list_strv(&files
, ".catalog", root
, 0, dirs
);
461 return log_error_errno(r
, "Failed to get catalog files: %m");
463 STRV_FOREACH(f
, files
) {
464 log_debug("Reading file '%s'", *f
);
465 r
= catalog_import_file(h
, *f
);
467 return log_error_errno(r
, "Failed to import file '%s': %m", *f
);
470 if (hashmap_size(h
) <= 0) {
471 log_info("No items in catalog.");
474 log_debug("Found %u items in catalog.", hashmap_size(h
));
476 items
= new(CatalogItem
, hashmap_size(h
));
481 HASHMAP_FOREACH_KEY(payload
, i
, h
, j
) {
482 log_debug("Found " SD_ID128_FORMAT_STR
", language %s",
483 SD_ID128_FORMAT_VAL(i
->id
),
484 isempty(i
->language
) ? "C" : i
->language
);
486 offset
= strbuf_add_string(sb
, payload
, strlen(payload
));
490 i
->offset
= htole64((uint64_t) offset
);
494 assert(n
== hashmap_size(h
));
495 typesafe_qsort(items
, n
, catalog_compare_func
);
499 sz
= write_catalog(database
, sb
, items
, n
);
501 return log_error_errno(sz
, "Failed to write %s: %m", database
);
503 log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64
" total size.",
504 database
, n
, sb
->len
, sz
);
508 static int open_mmap(const char *database
, int *_fd
, struct stat
*_st
, void **_p
) {
509 const CatalogHeader
*h
;
518 fd
= open(database
, O_RDONLY
|O_CLOEXEC
);
522 if (fstat(fd
, &st
) < 0) {
527 if (st
.st_size
< (off_t
) sizeof(CatalogHeader
)) {
532 p
= mmap(NULL
, PAGE_ALIGN(st
.st_size
), PROT_READ
, MAP_SHARED
, fd
, 0);
533 if (p
== MAP_FAILED
) {
539 if (memcmp(h
->signature
, CATALOG_SIGNATURE
, sizeof(h
->signature
)) != 0 ||
540 le64toh(h
->header_size
) < sizeof(CatalogHeader
) ||
541 le64toh(h
->catalog_item_size
) < sizeof(CatalogItem
) ||
542 h
->incompatible_flags
!= 0 ||
543 le64toh(h
->n_items
) <= 0 ||
544 st
.st_size
< (off_t
) (le64toh(h
->header_size
) + le64toh(h
->catalog_item_size
) * le64toh(h
->n_items
))) {
546 munmap(p
, st
.st_size
);
557 static const char *find_id(void *p
, sd_id128_t id
) {
558 CatalogItem
*f
= NULL
, key
= { .id
= id
};
559 const CatalogHeader
*h
= p
;
562 loc
= setlocale(LC_MESSAGES
, NULL
);
563 if (loc
&& loc
[0] && !streq(loc
, "C") && !streq(loc
, "POSIX")) {
564 strncpy(key
.language
, loc
, sizeof(key
.language
));
565 key
.language
[strcspn(key
.language
, ".@")] = 0;
567 f
= bsearch(&key
, (const uint8_t*) p
+ le64toh(h
->header_size
), le64toh(h
->n_items
), le64toh(h
->catalog_item_size
), (comparison_fn_t
) catalog_compare_func
);
571 e
= strchr(key
.language
, '_');
574 f
= bsearch(&key
, (const uint8_t*) p
+ le64toh(h
->header_size
), le64toh(h
->n_items
), le64toh(h
->catalog_item_size
), (comparison_fn_t
) catalog_compare_func
);
581 f
= bsearch(&key
, (const uint8_t*) p
+ le64toh(h
->header_size
), le64toh(h
->n_items
), le64toh(h
->catalog_item_size
), (comparison_fn_t
) catalog_compare_func
);
587 return (const char*) p
+
588 le64toh(h
->header_size
) +
589 le64toh(h
->n_items
) * le64toh(h
->catalog_item_size
) +
593 int catalog_get(const char* database
, sd_id128_t id
, char **_text
) {
594 _cleanup_close_
int fd
= -1;
603 r
= open_mmap(database
, &fd
, &st
, &p
);
624 munmap(p
, st
.st_size
);
629 static char *find_header(const char *s
, const char *header
) {
634 v
= startswith(s
, header
);
636 v
+= strspn(v
, WHITESPACE
);
637 return strndup(v
, strcspn(v
, NEWLINE
));
640 if (!next_header(&s
))
645 static void dump_catalog_entry(FILE *f
, sd_id128_t id
, const char *s
, bool oneline
) {
647 _cleanup_free_
char *subject
= NULL
, *defined_by
= NULL
;
649 subject
= find_header(s
, "Subject:");
650 defined_by
= find_header(s
, "Defined-By:");
652 fprintf(f
, SD_ID128_FORMAT_STR
" %s: %s\n",
653 SD_ID128_FORMAT_VAL(id
),
654 strna(defined_by
), strna(subject
));
656 fprintf(f
, "-- " SD_ID128_FORMAT_STR
"\n%s\n",
657 SD_ID128_FORMAT_VAL(id
), s
);
660 int catalog_list(FILE *f
, const char *database
, bool oneline
) {
661 _cleanup_close_
int fd
= -1;
664 const CatalogHeader
*h
;
665 const CatalogItem
*items
;
669 bool last_id_set
= false;
671 r
= open_mmap(database
, &fd
, &st
, &p
);
676 items
= (const CatalogItem
*) ((const uint8_t*) p
+ le64toh(h
->header_size
));
678 for (n
= 0; n
< le64toh(h
->n_items
); n
++) {
681 if (last_id_set
&& sd_id128_equal(last_id
, items
[n
].id
))
684 assert_se(s
= find_id(p
, items
[n
].id
));
686 dump_catalog_entry(f
, items
[n
].id
, s
, oneline
);
689 last_id
= items
[n
].id
;
692 munmap(p
, st
.st_size
);
697 int catalog_list_items(FILE *f
, const char *database
, bool oneline
, char **items
) {
701 STRV_FOREACH(item
, items
) {
704 _cleanup_free_
char *msg
= NULL
;
706 k
= sd_id128_from_string(*item
, &id
);
708 log_error_errno(k
, "Failed to parse id128 '%s': %m", *item
);
714 k
= catalog_get(database
, id
, &msg
);
716 log_full_errno(k
== -ENOENT
? LOG_NOTICE
: LOG_ERR
, k
,
717 "Failed to retrieve catalog entry for '%s': %m", *item
);
723 dump_catalog_entry(f
, id
, msg
, oneline
);