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 void *a
, const void *b
) {
60 const CatalogItem
*i
= a
, *j
= b
;
64 for (k
= 0; k
< ELEMENTSOF(j
->id
.bytes
); k
++) {
65 r
= CMP(i
->id
.bytes
[k
], j
->id
.bytes
[k
]);
70 return strcmp(i
->language
, j
->language
);
73 const struct hash_ops catalog_hash_ops
= {
74 .hash
= catalog_hash_func
,
75 .compare
= catalog_compare_func
78 static bool next_header(const char **s
) {
95 static const char *skip_header(const char *s
) {
96 while (next_header(&s
))
101 static char *combine_entries(const char *one
, const char *two
) {
106 /* Find split point of headers to body */
107 b1
= skip_header(one
);
108 b2
= skip_header(two
);
112 dest
= new(char, l1
+ l2
+ 1);
120 /* Headers from @one */
122 p
= mempcpy(p
, one
, n
);
124 /* Headers from @two, these will only be found if not present above */
126 p
= mempcpy(p
, two
, n
);
141 assert(p
- dest
<= (ptrdiff_t)(l1
+ l2
));
146 static int finish_item(
149 const char *language
,
150 char *payload
, size_t payload_size
) {
152 _cleanup_free_ CatalogItem
*i
= NULL
;
153 _cleanup_free_
char *prev
= NULL
, *combined
= NULL
;
157 assert(payload_size
> 0);
159 i
= new0(CatalogItem
, 1);
165 assert(strlen(language
) > 1 && strlen(language
) < 32);
166 strcpy(i
->language
, language
);
169 prev
= hashmap_get(h
, i
);
171 /* Already have such an item, combine them */
172 combined
= combine_entries(payload
, prev
);
176 if (hashmap_update(h
, i
, combined
) < 0)
181 combined
= memdup(payload
, payload_size
+ 1);
185 if (hashmap_put(h
, i
, combined
) < 0)
194 int catalog_file_lang(const char* filename
, char **lang
) {
195 char *beg
, *end
, *_lang
;
197 end
= endswith(filename
, ".catalog");
202 while (beg
> filename
&& !IN_SET(*beg
, '.', '/') && end
- beg
< 32)
205 if (*beg
!= '.' || end
<= beg
+ 1)
208 _lang
= strndup(beg
+ 1, end
- beg
- 1);
216 static int catalog_entry_lang(const char* filename
, int line
,
217 const char* t
, const char* deflang
, char **lang
) {
222 log_error("[%s:%u] Language too short.", filename
, line
);
226 log_error("[%s:%u] language too long.", filename
, line
);
231 if (streq(t
, deflang
)) {
232 log_warning("[%s:%u] language specified unnecessarily",
236 log_warning("[%s:%u] language differs from default for file",
247 int catalog_import_file(Hashmap
*h
, const char *path
) {
248 _cleanup_fclose_
FILE *f
= NULL
;
249 _cleanup_free_
char *payload
= NULL
;
250 size_t payload_size
= 0, payload_allocated
= 0;
253 _cleanup_free_
char *deflang
= NULL
, *lang
= NULL
;
254 bool got_id
= false, empty_line
= true;
260 f
= fopen(path
, "re");
262 return log_error_errno(errno
, "Failed to open file %s: %m", path
);
264 r
= catalog_file_lang(path
, &deflang
);
266 log_error_errno(r
, "Failed to determine language for file %s: %m", path
);
268 log_debug("File %s has language %s.", path
, deflang
);
274 if (!fgets(line
, sizeof(line
), f
)) {
278 return log_error_errno(errno
, "Failed to read file %s: %m", path
);
290 if (strchr(COMMENTS
"\n", line
[0]))
294 strlen(line
) >= 2+1+32 &&
298 IN_SET(line
[2+1+32], ' ', '\0')) {
305 with_language
= line
[2+1+32] != '\0';
308 if (sd_id128_from_string(line
+ 2 + 1, &jd
) >= 0) {
311 if (payload_size
== 0) {
312 log_error("[%s:%u] No payload text.", path
, n
);
316 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
327 t
= strstrip(line
+ 2 + 1 + 32 + 1);
328 r
= catalog_entry_lang(path
, n
, t
, deflang
, &lang
);
343 log_error("[%s:%u] Got payload before ID.", path
, n
);
347 line_len
= strlen(line
);
348 if (!GREEDY_REALLOC(payload
, payload_allocated
,
349 payload_size
+ (empty_line
? 1 : 0) + line_len
+ 1 + 1))
353 payload
[payload_size
++] = '\n';
354 memcpy(payload
+ payload_size
, line
, line_len
);
355 payload_size
+= line_len
;
356 payload
[payload_size
++] = '\n';
357 payload
[payload_size
] = '\0';
363 if (payload_size
== 0) {
364 log_error("[%s:%u] No payload text.", path
, n
);
368 r
= finish_item(h
, id
, lang
?: deflang
, payload
, payload_size
);
376 static int64_t write_catalog(const char *database
, struct strbuf
*sb
,
377 CatalogItem
*items
, size_t n
) {
378 CatalogHeader header
;
379 _cleanup_fclose_
FILE *w
= NULL
;
381 _cleanup_free_
char *d
, *p
= NULL
;
384 d
= dirname_malloc(database
);
388 r
= mkdir_p(d
, 0775);
390 return log_error_errno(r
, "Recursive mkdir %s: %m", d
);
392 r
= fopen_temporary(database
, &w
, &p
);
394 return log_error_errno(r
, "Failed to open database for writing: %s: %m",
398 memcpy(header
.signature
, CATALOG_SIGNATURE
, sizeof(header
.signature
));
399 header
.header_size
= htole64(ALIGN_TO(sizeof(CatalogHeader
), 8));
400 header
.catalog_item_size
= htole64(sizeof(CatalogItem
));
401 header
.n_items
= htole64(n
);
405 k
= fwrite(&header
, 1, sizeof(header
), w
);
406 if (k
!= sizeof(header
)) {
407 log_error("%s: failed to write header.", p
);
411 k
= fwrite(items
, 1, n
* sizeof(CatalogItem
), w
);
412 if (k
!= n
* sizeof(CatalogItem
)) {
413 log_error("%s: failed to write database.", p
);
417 k
= fwrite(sb
->buf
, 1, sb
->len
, w
);
419 log_error("%s: failed to write strings.", p
);
423 r
= fflush_and_check(w
);
425 log_error_errno(r
, "%s: failed to write database: %m", p
);
429 fchmod(fileno(w
), 0644);
431 if (rename(p
, database
) < 0) {
432 r
= log_error_errno(errno
, "rename (%s -> %s) failed: %m", p
, database
);
443 int catalog_update(const char* database
, const char* root
, const char* const* dirs
) {
444 _cleanup_strv_free_
char **files
= NULL
;
446 _cleanup_(strbuf_cleanupp
) struct strbuf
*sb
= NULL
;
447 _cleanup_hashmap_free_free_free_ Hashmap
*h
= NULL
;
448 _cleanup_free_ CatalogItem
*items
= NULL
;
457 h
= hashmap_new(&catalog_hash_ops
);
462 r
= conf_files_list_strv(&files
, ".catalog", root
, 0, dirs
);
464 return log_error_errno(r
, "Failed to get catalog files: %m");
466 STRV_FOREACH(f
, files
) {
467 log_debug("Reading file '%s'", *f
);
468 r
= catalog_import_file(h
, *f
);
470 return log_error_errno(r
, "Failed to import file '%s': %m", *f
);
473 if (hashmap_size(h
) <= 0) {
474 log_info("No items in catalog.");
477 log_debug("Found %u items in catalog.", hashmap_size(h
));
479 items
= new(CatalogItem
, hashmap_size(h
));
484 HASHMAP_FOREACH_KEY(payload
, i
, h
, j
) {
485 log_debug("Found " SD_ID128_FORMAT_STR
", language %s",
486 SD_ID128_FORMAT_VAL(i
->id
),
487 isempty(i
->language
) ? "C" : i
->language
);
489 offset
= strbuf_add_string(sb
, payload
, strlen(payload
));
493 i
->offset
= htole64((uint64_t) offset
);
497 assert(n
== hashmap_size(h
));
498 qsort_safe(items
, n
, sizeof(CatalogItem
), catalog_compare_func
);
502 sz
= write_catalog(database
, sb
, items
, n
);
504 return log_error_errno(sz
, "Failed to write %s: %m", database
);
506 log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64
" total size.",
507 database
, n
, sb
->len
, sz
);
511 static int open_mmap(const char *database
, int *_fd
, struct stat
*_st
, void **_p
) {
512 const CatalogHeader
*h
;
521 fd
= open(database
, O_RDONLY
|O_CLOEXEC
);
525 if (fstat(fd
, &st
) < 0) {
530 if (st
.st_size
< (off_t
) sizeof(CatalogHeader
)) {
535 p
= mmap(NULL
, PAGE_ALIGN(st
.st_size
), PROT_READ
, MAP_SHARED
, fd
, 0);
536 if (p
== MAP_FAILED
) {
542 if (memcmp(h
->signature
, CATALOG_SIGNATURE
, sizeof(h
->signature
)) != 0 ||
543 le64toh(h
->header_size
) < sizeof(CatalogHeader
) ||
544 le64toh(h
->catalog_item_size
) < sizeof(CatalogItem
) ||
545 h
->incompatible_flags
!= 0 ||
546 le64toh(h
->n_items
) <= 0 ||
547 st
.st_size
< (off_t
) (le64toh(h
->header_size
) + le64toh(h
->catalog_item_size
) * le64toh(h
->n_items
))) {
549 munmap(p
, st
.st_size
);
560 static const char *find_id(void *p
, sd_id128_t id
) {
561 CatalogItem
*f
= NULL
, key
= { .id
= id
};
562 const CatalogHeader
*h
= p
;
565 loc
= setlocale(LC_MESSAGES
, NULL
);
566 if (loc
&& loc
[0] && !streq(loc
, "C") && !streq(loc
, "POSIX")) {
567 strncpy(key
.language
, loc
, sizeof(key
.language
));
568 key
.language
[strcspn(key
.language
, ".@")] = 0;
570 f
= bsearch(&key
, (const uint8_t*) p
+ le64toh(h
->header_size
), le64toh(h
->n_items
), le64toh(h
->catalog_item_size
), catalog_compare_func
);
574 e
= strchr(key
.language
, '_');
577 f
= bsearch(&key
, (const uint8_t*) p
+ le64toh(h
->header_size
), le64toh(h
->n_items
), le64toh(h
->catalog_item_size
), catalog_compare_func
);
584 f
= bsearch(&key
, (const uint8_t*) p
+ le64toh(h
->header_size
), le64toh(h
->n_items
), le64toh(h
->catalog_item_size
), catalog_compare_func
);
590 return (const char*) p
+
591 le64toh(h
->header_size
) +
592 le64toh(h
->n_items
) * le64toh(h
->catalog_item_size
) +
596 int catalog_get(const char* database
, sd_id128_t id
, char **_text
) {
597 _cleanup_close_
int fd
= -1;
606 r
= open_mmap(database
, &fd
, &st
, &p
);
627 munmap(p
, st
.st_size
);
632 static char *find_header(const char *s
, const char *header
) {
637 v
= startswith(s
, header
);
639 v
+= strspn(v
, WHITESPACE
);
640 return strndup(v
, strcspn(v
, NEWLINE
));
643 if (!next_header(&s
))
648 static void dump_catalog_entry(FILE *f
, sd_id128_t id
, const char *s
, bool oneline
) {
650 _cleanup_free_
char *subject
= NULL
, *defined_by
= NULL
;
652 subject
= find_header(s
, "Subject:");
653 defined_by
= find_header(s
, "Defined-By:");
655 fprintf(f
, SD_ID128_FORMAT_STR
" %s: %s\n",
656 SD_ID128_FORMAT_VAL(id
),
657 strna(defined_by
), strna(subject
));
659 fprintf(f
, "-- " SD_ID128_FORMAT_STR
"\n%s\n",
660 SD_ID128_FORMAT_VAL(id
), s
);
663 int catalog_list(FILE *f
, const char *database
, bool oneline
) {
664 _cleanup_close_
int fd
= -1;
667 const CatalogHeader
*h
;
668 const CatalogItem
*items
;
672 bool last_id_set
= false;
674 r
= open_mmap(database
, &fd
, &st
, &p
);
679 items
= (const CatalogItem
*) ((const uint8_t*) p
+ le64toh(h
->header_size
));
681 for (n
= 0; n
< le64toh(h
->n_items
); n
++) {
684 if (last_id_set
&& sd_id128_equal(last_id
, items
[n
].id
))
687 assert_se(s
= find_id(p
, items
[n
].id
));
689 dump_catalog_entry(f
, items
[n
].id
, s
, oneline
);
692 last_id
= items
[n
].id
;
695 munmap(p
, st
.st_size
);
700 int catalog_list_items(FILE *f
, const char *database
, bool oneline
, char **items
) {
704 STRV_FOREACH(item
, items
) {
707 _cleanup_free_
char *msg
= NULL
;
709 k
= sd_id128_from_string(*item
, &id
);
711 log_error_errno(k
, "Failed to parse id128 '%s': %m", *item
);
717 k
= catalog_get(database
, id
, &msg
);
719 log_full_errno(k
== -ENOENT
? LOG_NOTICE
: LOG_ERR
, k
,
720 "Failed to retrieve catalog entry for '%s': %m", *item
);
726 dump_catalog_entry(f
, id
, msg
, oneline
);