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 log_error("[%s:%u] Language too short.", filename
, line
);
225 log_error("[%s:%u] language too long.", filename
, line
);
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 log_error("[%s:%u] No payload text.", path
, n
);
312 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
323 t
= strstrip(line
+ 2 + 1 + 32 + 1);
324 r
= catalog_entry_lang(path
, n
, t
, deflang
, &lang
);
339 log_error("[%s:%u] Got payload before ID.", path
, n
);
343 line_len
= strlen(line
);
344 if (!GREEDY_REALLOC(payload
, payload_allocated
,
345 payload_size
+ (empty_line
? 1 : 0) + line_len
+ 1 + 1))
349 payload
[payload_size
++] = '\n';
350 memcpy(payload
+ payload_size
, line
, line_len
);
351 payload_size
+= line_len
;
352 payload
[payload_size
++] = '\n';
353 payload
[payload_size
] = '\0';
359 if (payload_size
== 0) {
360 log_error("[%s:%u] No payload text.", path
, n
);
364 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
372 static int64_t write_catalog(const char *database
, struct strbuf
*sb
,
373 CatalogItem
*items
, size_t n
) {
374 CatalogHeader header
;
375 _cleanup_fclose_
FILE *w
= NULL
;
377 _cleanup_free_
char *d
, *p
= NULL
;
380 d
= dirname_malloc(database
);
384 r
= mkdir_p(d
, 0775);
386 return log_error_errno(r
, "Recursive mkdir %s: %m", d
);
388 r
= fopen_temporary(database
, &w
, &p
);
390 return log_error_errno(r
, "Failed to open database for writing: %s: %m",
394 memcpy(header
.signature
, CATALOG_SIGNATURE
, sizeof(header
.signature
));
395 header
.header_size
= htole64(ALIGN_TO(sizeof(CatalogHeader
), 8));
396 header
.catalog_item_size
= htole64(sizeof(CatalogItem
));
397 header
.n_items
= htole64(n
);
401 k
= fwrite(&header
, 1, sizeof(header
), w
);
402 if (k
!= sizeof(header
)) {
403 log_error("%s: failed to write header.", p
);
407 k
= fwrite(items
, 1, n
* sizeof(CatalogItem
), w
);
408 if (k
!= n
* sizeof(CatalogItem
)) {
409 log_error("%s: failed to write database.", p
);
413 k
= fwrite(sb
->buf
, 1, sb
->len
, w
);
415 log_error("%s: failed to write strings.", p
);
419 r
= fflush_and_check(w
);
421 log_error_errno(r
, "%s: failed to write database: %m", p
);
425 fchmod(fileno(w
), 0644);
427 if (rename(p
, database
) < 0) {
428 r
= log_error_errno(errno
, "rename (%s -> %s) failed: %m", p
, database
);
439 int catalog_update(const char* database
, const char* root
, const char* const* dirs
) {
440 _cleanup_strv_free_
char **files
= NULL
;
442 _cleanup_(strbuf_cleanupp
) struct strbuf
*sb
= NULL
;
443 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
444 _cleanup_free_ CatalogItem
*items
= NULL
;
453 h
= hashmap_new(&catalog_hash_ops
);
458 r
= conf_files_list_strv(&files
, ".catalog", root
, 0, dirs
);
460 return log_error_errno(r
, "Failed to get catalog files: %m");
462 STRV_FOREACH(f
, files
) {
463 log_debug("Reading file '%s'", *f
);
464 r
= catalog_import_file(h
, *f
);
466 return log_error_errno(r
, "Failed to import file '%s': %m", *f
);
469 if (hashmap_size(h
) <= 0) {
470 log_info("No items in catalog.");
473 log_debug("Found %u items in catalog.", hashmap_size(h
));
475 items
= new(CatalogItem
, hashmap_size(h
));
480 HASHMAP_FOREACH_KEY(payload
, i
, h
, j
) {
481 log_debug("Found " SD_ID128_FORMAT_STR
", language %s",
482 SD_ID128_FORMAT_VAL(i
->id
),
483 isempty(i
->language
) ? "C" : i
->language
);
485 offset
= strbuf_add_string(sb
, payload
, strlen(payload
));
489 i
->offset
= htole64((uint64_t) offset
);
493 assert(n
== hashmap_size(h
));
494 typesafe_qsort(items
, n
, catalog_compare_func
);
498 sz
= write_catalog(database
, sb
, items
, n
);
500 return log_error_errno(sz
, "Failed to write %s: %m", database
);
502 log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64
" total size.",
503 database
, n
, sb
->len
, sz
);
507 static int open_mmap(const char *database
, int *_fd
, struct stat
*_st
, void **_p
) {
508 const CatalogHeader
*h
;
517 fd
= open(database
, O_RDONLY
|O_CLOEXEC
);
521 if (fstat(fd
, &st
) < 0) {
526 if (st
.st_size
< (off_t
) sizeof(CatalogHeader
)) {
531 p
= mmap(NULL
, PAGE_ALIGN(st
.st_size
), PROT_READ
, MAP_SHARED
, fd
, 0);
532 if (p
== MAP_FAILED
) {
538 if (memcmp(h
->signature
, 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
))) {
545 munmap(p
, st
.st_size
);
556 static const char *find_id(void *p
, sd_id128_t id
) {
557 CatalogItem
*f
= NULL
, key
= { .id
= id
};
558 const CatalogHeader
*h
= p
;
561 loc
= setlocale(LC_MESSAGES
, NULL
);
562 if (loc
&& loc
[0] && !streq(loc
, "C") && !streq(loc
, "POSIX")) {
563 strncpy(key
.language
, loc
, sizeof(key
.language
));
564 key
.language
[strcspn(key
.language
, ".@")] = 0;
566 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
);
570 e
= strchr(key
.language
, '_');
573 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
);
580 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
);
586 return (const char*) p
+
587 le64toh(h
->header_size
) +
588 le64toh(h
->n_items
) * le64toh(h
->catalog_item_size
) +
592 int catalog_get(const char* database
, sd_id128_t id
, char **_text
) {
593 _cleanup_close_
int fd
= -1;
602 r
= open_mmap(database
, &fd
, &st
, &p
);
623 munmap(p
, st
.st_size
);
628 static char *find_header(const char *s
, const char *header
) {
633 v
= startswith(s
, header
);
635 v
+= strspn(v
, WHITESPACE
);
636 return strndup(v
, strcspn(v
, NEWLINE
));
639 if (!next_header(&s
))
644 static void dump_catalog_entry(FILE *f
, sd_id128_t id
, const char *s
, bool oneline
) {
646 _cleanup_free_
char *subject
= NULL
, *defined_by
= NULL
;
648 subject
= find_header(s
, "Subject:");
649 defined_by
= find_header(s
, "Defined-By:");
651 fprintf(f
, SD_ID128_FORMAT_STR
" %s: %s\n",
652 SD_ID128_FORMAT_VAL(id
),
653 strna(defined_by
), strna(subject
));
655 fprintf(f
, "-- " SD_ID128_FORMAT_STR
"\n%s\n",
656 SD_ID128_FORMAT_VAL(id
), s
);
659 int catalog_list(FILE *f
, const char *database
, bool oneline
) {
660 _cleanup_close_
int fd
= -1;
663 const CatalogHeader
*h
;
664 const CatalogItem
*items
;
668 bool last_id_set
= false;
670 r
= open_mmap(database
, &fd
, &st
, &p
);
675 items
= (const CatalogItem
*) ((const uint8_t*) p
+ le64toh(h
->header_size
));
677 for (n
= 0; n
< le64toh(h
->n_items
); n
++) {
680 if (last_id_set
&& sd_id128_equal(last_id
, items
[n
].id
))
683 assert_se(s
= find_id(p
, items
[n
].id
));
685 dump_catalog_entry(f
, items
[n
].id
, s
, oneline
);
688 last_id
= items
[n
].id
;
691 munmap(p
, st
.st_size
);
696 int catalog_list_items(FILE *f
, const char *database
, bool oneline
, char **items
) {
700 STRV_FOREACH(item
, items
) {
703 _cleanup_free_
char *msg
= NULL
;
705 k
= sd_id128_from_string(*item
, &id
);
707 log_error_errno(k
, "Failed to parse id128 '%s': %m", *item
);
713 k
= catalog_get(database
, id
, &msg
);
715 log_full_errno(k
== -ENOENT
? LOG_NOTICE
: LOG_ERR
, k
,
716 "Failed to retrieve catalog entry for '%s': %m", *item
);
722 dump_catalog_entry(f
, id
, msg
, oneline
);