]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/localed-util.c
Merge pull request #23741 from mrc0mmand/more-asan-tweaks
[thirdparty/systemd.git] / src / locale / localed-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7
8 #include "bus-polkit.h"
9 #include "copy.h"
10 #include "env-file-label.h"
11 #include "env-file.h"
12 #include "env-util.h"
13 #include "fd-util.h"
14 #include "fileio-label.h"
15 #include "fileio.h"
16 #include "fs-util.h"
17 #include "kbd-util.h"
18 #include "localed-util.h"
19 #include "macro.h"
20 #include "mkdir-label.h"
21 #include "nulstr-util.h"
22 #include "process-util.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "tmpfile-util.h"
26
27 static bool startswith_comma(const char *s, const char *prefix) {
28 s = startswith(s, prefix);
29 if (!s)
30 return false;
31
32 return IN_SET(*s, ',', '\0');
33 }
34
35 static const char* systemd_kbd_model_map(void) {
36 const char* s;
37
38 s = getenv("SYSTEMD_KBD_MODEL_MAP");
39 if (s)
40 return s;
41
42 return SYSTEMD_KBD_MODEL_MAP;
43 }
44
45 static const char* systemd_language_fallback_map(void) {
46 const char* s;
47
48 s = getenv("SYSTEMD_LANGUAGE_FALLBACK_MAP");
49 if (s)
50 return s;
51
52 return SYSTEMD_LANGUAGE_FALLBACK_MAP;
53 }
54
55 static void context_free_x11(Context *c) {
56 c->x11_layout = mfree(c->x11_layout);
57 c->x11_options = mfree(c->x11_options);
58 c->x11_model = mfree(c->x11_model);
59 c->x11_variant = mfree(c->x11_variant);
60 }
61
62 static void context_free_vconsole(Context *c) {
63 c->vc_keymap = mfree(c->vc_keymap);
64 c->vc_keymap_toggle = mfree(c->vc_keymap_toggle);
65 }
66
67 void context_clear(Context *c) {
68 locale_context_clear(&c->locale_context);
69 context_free_x11(c);
70 context_free_vconsole(c);
71
72 sd_bus_message_unref(c->locale_cache);
73 sd_bus_message_unref(c->x11_cache);
74 sd_bus_message_unref(c->vc_cache);
75
76 bus_verify_polkit_async_registry_free(c->polkit_registry);
77 };
78
79 int locale_read_data(Context *c, sd_bus_message *m) {
80 assert(c);
81
82 /* Do not try to re-read the file within single bus operation. */
83 if (m) {
84 if (m == c->locale_cache)
85 return 0;
86
87 sd_bus_message_unref(c->locale_cache);
88 c->locale_cache = sd_bus_message_ref(m);
89 }
90
91 return locale_context_load(&c->locale_context, LOCALE_LOAD_LOCALE_CONF | LOCALE_LOAD_ENVIRONMENT | LOCALE_LOAD_SIMPLIFY);
92 }
93
94 int vconsole_read_data(Context *c, sd_bus_message *m) {
95 struct stat st;
96 usec_t t;
97
98 /* Do not try to re-read the file within single bus operation. */
99 if (m) {
100 if (m == c->vc_cache)
101 return 0;
102
103 sd_bus_message_unref(c->vc_cache);
104 c->vc_cache = sd_bus_message_ref(m);
105 }
106
107 if (stat("/etc/vconsole.conf", &st) < 0) {
108 if (errno != ENOENT)
109 return -errno;
110
111 c->vc_mtime = USEC_INFINITY;
112 context_free_vconsole(c);
113 return 0;
114 }
115
116 /* If mtime is not changed, then we do not need to re-read */
117 t = timespec_load(&st.st_mtim);
118 if (c->vc_mtime != USEC_INFINITY && t == c->vc_mtime)
119 return 0;
120
121 c->vc_mtime = t;
122 context_free_vconsole(c);
123
124 return parse_env_file(NULL, "/etc/vconsole.conf",
125 "KEYMAP", &c->vc_keymap,
126 "KEYMAP_TOGGLE", &c->vc_keymap_toggle);
127 }
128
129 int x11_read_data(Context *c, sd_bus_message *m) {
130 _cleanup_fclose_ FILE *f = NULL;
131 bool in_section = false;
132 struct stat st;
133 usec_t t;
134 int r;
135
136 /* Do not try to re-read the file within single bus operation. */
137 if (m) {
138 if (m == c->x11_cache)
139 return 0;
140
141 sd_bus_message_unref(c->x11_cache);
142 c->x11_cache = sd_bus_message_ref(m);
143 }
144
145 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) < 0) {
146 if (errno != ENOENT)
147 return -errno;
148
149 c->x11_mtime = USEC_INFINITY;
150 context_free_x11(c);
151 return 0;
152 }
153
154 /* If mtime is not changed, then we do not need to re-read */
155 t = timespec_load(&st.st_mtim);
156 if (c->x11_mtime != USEC_INFINITY && t == c->x11_mtime)
157 return 0;
158
159 c->x11_mtime = t;
160 context_free_x11(c);
161
162 f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
163 if (!f)
164 return -errno;
165
166 for (;;) {
167 _cleanup_free_ char *line = NULL;
168 char *l;
169
170 r = read_line(f, LONG_LINE_MAX, &line);
171 if (r < 0)
172 return r;
173 if (r == 0)
174 break;
175
176 l = strstrip(line);
177 if (IN_SET(l[0], 0, '#'))
178 continue;
179
180 if (in_section && first_word(l, "Option")) {
181 _cleanup_strv_free_ char **a = NULL;
182
183 r = strv_split_full(&a, l, WHITESPACE, EXTRACT_UNQUOTE);
184 if (r < 0)
185 return r;
186
187 if (strv_length(a) == 3) {
188 char **p = NULL;
189
190 if (streq(a[1], "XkbLayout"))
191 p = &c->x11_layout;
192 else if (streq(a[1], "XkbModel"))
193 p = &c->x11_model;
194 else if (streq(a[1], "XkbVariant"))
195 p = &c->x11_variant;
196 else if (streq(a[1], "XkbOptions"))
197 p = &c->x11_options;
198
199 if (p)
200 free_and_replace(*p, a[2]);
201 }
202
203 } else if (!in_section && first_word(l, "Section")) {
204 _cleanup_strv_free_ char **a = NULL;
205
206 r = strv_split_full(&a, l, WHITESPACE, EXTRACT_UNQUOTE);
207 if (r < 0)
208 return -ENOMEM;
209
210 if (strv_length(a) == 2 && streq(a[1], "InputClass"))
211 in_section = true;
212
213 } else if (in_section && first_word(l, "EndSection"))
214 in_section = false;
215 }
216
217 return 0;
218 }
219
220 int vconsole_write_data(Context *c) {
221 _cleanup_strv_free_ char **l = NULL;
222 struct stat st;
223 int r;
224
225 r = load_env_file(NULL, "/etc/vconsole.conf", &l);
226 if (r < 0 && r != -ENOENT)
227 return r;
228
229 r = strv_env_assign(&l, "KEYMAP", empty_to_null(c->vc_keymap));
230 if (r < 0)
231 return r;
232
233 r = strv_env_assign(&l, "KEYMAP_TOGGLE", empty_to_null(c->vc_keymap_toggle));
234 if (r < 0)
235 return r;
236
237 if (strv_isempty(l)) {
238 if (unlink("/etc/vconsole.conf") < 0)
239 return errno == ENOENT ? 0 : -errno;
240
241 c->vc_mtime = USEC_INFINITY;
242 return 0;
243 }
244
245 r = write_env_file_label("/etc/vconsole.conf", l);
246 if (r < 0)
247 return r;
248
249 if (stat("/etc/vconsole.conf", &st) >= 0)
250 c->vc_mtime = timespec_load(&st.st_mtim);
251
252 return 0;
253 }
254
255 int x11_write_data(Context *c) {
256 _cleanup_fclose_ FILE *f = NULL;
257 _cleanup_free_ char *temp_path = NULL;
258 struct stat st;
259 int r;
260
261 if (isempty(c->x11_layout) &&
262 isempty(c->x11_model) &&
263 isempty(c->x11_variant) &&
264 isempty(c->x11_options)) {
265
266 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
267 return errno == ENOENT ? 0 : -errno;
268
269 c->vc_mtime = USEC_INFINITY;
270 return 0;
271 }
272
273 (void) mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
274 r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path);
275 if (r < 0)
276 return r;
277
278 (void) fchmod(fileno(f), 0644);
279
280 fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n"
281 "# probably wise not to edit this file manually. Use localectl(1) to\n"
282 "# instruct systemd-localed to update it.\n"
283 "Section \"InputClass\"\n"
284 " Identifier \"system-keyboard\"\n"
285 " MatchIsKeyboard \"on\"\n", f);
286
287 if (!isempty(c->x11_layout))
288 fprintf(f, " Option \"XkbLayout\" \"%s\"\n", c->x11_layout);
289
290 if (!isempty(c->x11_model))
291 fprintf(f, " Option \"XkbModel\" \"%s\"\n", c->x11_model);
292
293 if (!isempty(c->x11_variant))
294 fprintf(f, " Option \"XkbVariant\" \"%s\"\n", c->x11_variant);
295
296 if (!isempty(c->x11_options))
297 fprintf(f, " Option \"XkbOptions\" \"%s\"\n", c->x11_options);
298
299 fputs("EndSection\n", f);
300
301 r = fflush_sync_and_check(f);
302 if (r < 0)
303 goto fail;
304
305 if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
306 r = -errno;
307 goto fail;
308 }
309
310 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) >= 0)
311 c->x11_mtime = timespec_load(&st.st_mtim);
312
313 return 0;
314
315 fail:
316 if (temp_path)
317 (void) unlink(temp_path);
318
319 return r;
320 }
321
322 static int read_next_mapping(const char* filename,
323 unsigned min_fields, unsigned max_fields,
324 FILE *f, unsigned *n, char ***a) {
325 assert(f);
326 assert(n);
327 assert(a);
328
329 for (;;) {
330 _cleanup_free_ char *line = NULL;
331 size_t length;
332 char *l, **b;
333 int r;
334
335 r = read_line(f, LONG_LINE_MAX, &line);
336 if (r < 0)
337 return r;
338 if (r == 0)
339 break;
340
341 (*n)++;
342
343 l = strstrip(line);
344 if (IN_SET(l[0], 0, '#'))
345 continue;
346
347 r = strv_split_full(&b, l, WHITESPACE, EXTRACT_UNQUOTE);
348 if (r < 0)
349 return r;
350
351 length = strv_length(b);
352 if (length < min_fields || length > max_fields) {
353 log_error("Invalid line %s:%u, ignoring.", filename, *n);
354 strv_free(b);
355 continue;
356
357 }
358
359 *a = b;
360 return 1;
361 }
362
363 return 0;
364 }
365
366 int vconsole_convert_to_x11(Context *c) {
367 const char *map;
368 int modified = -1;
369
370 map = systemd_kbd_model_map();
371
372 if (isempty(c->vc_keymap)) {
373 modified =
374 !isempty(c->x11_layout) ||
375 !isempty(c->x11_model) ||
376 !isempty(c->x11_variant) ||
377 !isempty(c->x11_options);
378
379 context_free_x11(c);
380 } else {
381 _cleanup_fclose_ FILE *f = NULL;
382 unsigned n = 0;
383
384 f = fopen(map, "re");
385 if (!f)
386 return -errno;
387
388 for (;;) {
389 _cleanup_strv_free_ char **a = NULL;
390 int r;
391
392 r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a);
393 if (r < 0)
394 return r;
395 if (r == 0)
396 break;
397
398 if (!streq(c->vc_keymap, a[0]))
399 continue;
400
401 if (!streq_ptr(c->x11_layout, empty_or_dash_to_null(a[1])) ||
402 !streq_ptr(c->x11_model, empty_or_dash_to_null(a[2])) ||
403 !streq_ptr(c->x11_variant, empty_or_dash_to_null(a[3])) ||
404 !streq_ptr(c->x11_options, empty_or_dash_to_null(a[4]))) {
405
406 if (free_and_strdup(&c->x11_layout, empty_or_dash_to_null(a[1])) < 0 ||
407 free_and_strdup(&c->x11_model, empty_or_dash_to_null(a[2])) < 0 ||
408 free_and_strdup(&c->x11_variant, empty_or_dash_to_null(a[3])) < 0 ||
409 free_and_strdup(&c->x11_options, empty_or_dash_to_null(a[4])) < 0)
410 return -ENOMEM;
411
412 modified = true;
413 }
414
415 break;
416 }
417 }
418
419 if (modified > 0)
420 log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
421 strempty(c->x11_layout),
422 strempty(c->x11_model),
423 strempty(c->x11_variant),
424 strempty(c->x11_options));
425 else if (modified < 0)
426 log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".",
427 c->vc_keymap);
428 else
429 log_debug("X11 keyboard layout did not need to be modified.");
430
431 return modified > 0;
432 }
433
434 int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap) {
435 const char *dir;
436 _cleanup_free_ char *n = NULL;
437
438 if (x11_variant)
439 n = strjoin(x11_layout, "-", x11_variant);
440 else
441 n = strdup(x11_layout);
442 if (!n)
443 return -ENOMEM;
444
445 NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
446 _cleanup_free_ char *p = NULL, *pz = NULL;
447 bool uncompressed;
448
449 p = strjoin(dir, "xkb/", n, ".map");
450 pz = strjoin(dir, "xkb/", n, ".map.gz");
451 if (!p || !pz)
452 return -ENOMEM;
453
454 uncompressed = access(p, F_OK) == 0;
455 if (uncompressed || access(pz, F_OK) == 0) {
456 log_debug("Found converted keymap %s at %s",
457 n, uncompressed ? p : pz);
458
459 *new_keymap = TAKE_PTR(n);
460 return 1;
461 }
462 }
463
464 return 0;
465 }
466
467 int find_legacy_keymap(Context *c, char **ret) {
468 const char *map;
469 _cleanup_fclose_ FILE *f = NULL;
470 _cleanup_free_ char *new_keymap = NULL;
471 unsigned n = 0;
472 unsigned best_matching = 0;
473 int r;
474
475 assert(!isempty(c->x11_layout));
476
477 map = systemd_kbd_model_map();
478
479 f = fopen(map, "re");
480 if (!f)
481 return -errno;
482
483 for (;;) {
484 _cleanup_strv_free_ char **a = NULL;
485 unsigned matching = 0;
486
487 r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a);
488 if (r < 0)
489 return r;
490 if (r == 0)
491 break;
492
493 /* Determine how well matching this entry is */
494 if (streq(c->x11_layout, a[1]))
495 /* If we got an exact match, this is best */
496 matching = 10;
497 else {
498 /* We have multiple X layouts, look for an
499 * entry that matches our key with everything
500 * but the first layout stripped off. */
501 if (startswith_comma(c->x11_layout, a[1]))
502 matching = 5;
503 else {
504 _cleanup_free_ char *x = NULL;
505
506 /* If that didn't work, strip off the
507 * other layouts from the entry, too */
508 x = strndup(a[1], strcspn(a[1], ","));
509 if (startswith_comma(c->x11_layout, x))
510 matching = 1;
511 }
512 }
513
514 if (matching > 0) {
515 if (isempty(c->x11_model) || streq_ptr(c->x11_model, a[2])) {
516 matching++;
517
518 if (streq_ptr(c->x11_variant, a[3])) {
519 matching++;
520
521 if (streq_ptr(c->x11_options, a[4]))
522 matching++;
523 }
524 }
525 }
526
527 /* The best matching entry so far, then let's save that */
528 if (matching >= MAX(best_matching, 1u)) {
529 log_debug("Found legacy keymap %s with score %u",
530 a[0], matching);
531
532 if (matching > best_matching) {
533 best_matching = matching;
534
535 r = free_and_strdup(&new_keymap, a[0]);
536 if (r < 0)
537 return r;
538 }
539 }
540 }
541
542 if (best_matching < 10 && c->x11_layout) {
543 /* The best match is only the first part of the X11
544 * keymap. Check if we have a converted map which
545 * matches just the first layout.
546 */
547 char *l, *v = NULL, *converted;
548
549 l = strndupa_safe(c->x11_layout, strcspn(c->x11_layout, ","));
550 if (c->x11_variant)
551 v = strndupa_safe(c->x11_variant,
552 strcspn(c->x11_variant, ","));
553 r = find_converted_keymap(l, v, &converted);
554 if (r < 0)
555 return r;
556 if (r > 0)
557 free_and_replace(new_keymap, converted);
558 }
559
560 *ret = TAKE_PTR(new_keymap);
561 return (bool) *ret;
562 }
563
564 int find_language_fallback(const char *lang, char **language) {
565 const char *map;
566 _cleanup_fclose_ FILE *f = NULL;
567 unsigned n = 0;
568
569 assert(lang);
570 assert(language);
571
572 map = systemd_language_fallback_map();
573
574 f = fopen(map, "re");
575 if (!f)
576 return -errno;
577
578 for (;;) {
579 _cleanup_strv_free_ char **a = NULL;
580 int r;
581
582 r = read_next_mapping(map, 2, 2, f, &n, &a);
583 if (r <= 0)
584 return r;
585
586 if (streq(lang, a[0])) {
587 assert(strv_length(a) == 2);
588 *language = TAKE_PTR(a[1]);
589 return 1;
590 }
591 }
592
593 assert_not_reached();
594 }
595
596 int x11_convert_to_vconsole(Context *c) {
597 bool modified = false;
598
599 if (isempty(c->x11_layout)) {
600 modified =
601 !isempty(c->vc_keymap) ||
602 !isempty(c->vc_keymap_toggle);
603
604 context_free_vconsole(c);
605 } else {
606 _cleanup_free_ char *new_keymap = NULL;
607 int r;
608
609 r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap);
610 if (r < 0)
611 return r;
612 else if (r == 0) {
613 r = find_legacy_keymap(c, &new_keymap);
614 if (r < 0)
615 return r;
616 }
617 if (r == 0)
618 /* We search for layout-variant match first, but then we also look
619 * for anything which matches just the layout. So it's accurate to say
620 * that we couldn't find anything which matches the layout. */
621 log_notice("No conversion to virtual console map found for \"%s\".",
622 c->x11_layout);
623
624 if (!streq_ptr(c->vc_keymap, new_keymap)) {
625 free_and_replace(c->vc_keymap, new_keymap);
626 c->vc_keymap_toggle = mfree(c->vc_keymap_toggle);
627 modified = true;
628 }
629 }
630
631 if (modified)
632 log_info("Changing virtual console keymap to '%s' toggle '%s'",
633 strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
634 else
635 log_debug("Virtual console keymap was not modified.");
636
637 return modified;
638 }
639
640 bool locale_gen_check_available(void) {
641 #if HAVE_LOCALEGEN
642 if (access(LOCALEGEN_PATH, X_OK) < 0) {
643 if (errno != ENOENT)
644 log_warning_errno(errno, "Unable to determine whether " LOCALEGEN_PATH " exists and is executable, assuming it is not: %m");
645 return false;
646 }
647 if (access("/etc/locale.gen", F_OK) < 0) {
648 if (errno != ENOENT)
649 log_warning_errno(errno, "Unable to determine whether /etc/locale.gen exists, assuming it does not: %m");
650 return false;
651 }
652 return true;
653 #else
654 return false;
655 #endif
656 }
657
658 #if HAVE_LOCALEGEN
659 static bool locale_encoding_is_utf8_or_unspecified(const char *locale) {
660 const char *c = strchr(locale, '.');
661 return !c || strcaseeq(c, ".UTF-8") || strcasestr(locale, ".UTF-8@");
662 }
663
664 static int locale_gen_locale_supported(const char *locale_entry) {
665 /* Returns an error valus <= 0 if the locale-gen entry is invalid or unsupported,
666 * 1 in case the locale entry is valid, and -EOPNOTSUPP specifically in case
667 * the distributor has not provided us with a SUPPORTED file to check
668 * locale for validity. */
669
670 _cleanup_fclose_ FILE *f = NULL;
671 int r;
672
673 assert(locale_entry);
674
675 /* Locale templates without country code are never supported */
676 if (!strstr(locale_entry, "_"))
677 return -EINVAL;
678
679 f = fopen("/usr/share/i18n/SUPPORTED", "re");
680 if (!f) {
681 if (errno == ENOENT)
682 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
683 "Unable to check validity of locale entry %s: /usr/share/i18n/SUPPORTED does not exist",
684 locale_entry);
685 return -errno;
686 }
687
688 for (;;) {
689 _cleanup_free_ char *line = NULL;
690
691 r = read_line(f, LONG_LINE_MAX, &line);
692 if (r < 0)
693 return log_debug_errno(r, "Failed to read /usr/share/i18n/SUPPORTED: %m");
694 if (r == 0)
695 return 0;
696
697 line = strstrip(line);
698 if (strcaseeq_ptr(line, locale_entry))
699 return 1;
700 }
701 }
702 #endif
703
704 int locale_gen_enable_locale(const char *locale) {
705 #if HAVE_LOCALEGEN
706 _cleanup_fclose_ FILE *fr = NULL, *fw = NULL;
707 _cleanup_(unlink_and_freep) char *temp_path = NULL;
708 _cleanup_free_ char *locale_entry = NULL;
709 bool locale_enabled = false, first_line = false;
710 bool write_new = false;
711 int r;
712
713 if (isempty(locale))
714 return 0;
715
716 if (locale_encoding_is_utf8_or_unspecified(locale)) {
717 locale_entry = strjoin(locale, " UTF-8");
718 if (!locale_entry)
719 return -ENOMEM;
720 } else
721 return -ENOEXEC; /* We do not process non-UTF-8 locale */
722
723 r = locale_gen_locale_supported(locale_entry);
724 if (r == 0)
725 return -EINVAL;
726 if (r < 0 && r != -EOPNOTSUPP)
727 return r;
728
729 fr = fopen("/etc/locale.gen", "re");
730 if (!fr) {
731 if (errno != ENOENT)
732 return -errno;
733 write_new = true;
734 }
735
736 r = fopen_temporary("/etc/locale.gen", &fw, &temp_path);
737 if (r < 0)
738 return r;
739
740 if (write_new)
741 (void) fchmod(fileno(fw), 0644);
742 else {
743 /* apply mode & xattrs of the original file to new file */
744 r = copy_access(fileno(fr), fileno(fw));
745 if (r < 0)
746 return r;
747 r = copy_xattr(fileno(fr), fileno(fw), COPY_ALL_XATTRS);
748 if (r < 0)
749 return r;
750 }
751
752 if (!write_new) {
753 /* The config file ends with a line break, which we do not want to include before potentially appending a new locale
754 * instead of uncommenting an existing line. By prepending linebreaks, we can avoid buffering this file but can still write
755 * a nice config file without empty lines */
756 first_line = true;
757 for (;;) {
758 _cleanup_free_ char *line = NULL;
759 char *line_locale;
760
761 r = read_line(fr, LONG_LINE_MAX, &line);
762 if (r < 0)
763 return r;
764 if (r == 0)
765 break;
766
767 if (locale_enabled) {
768 /* Just complete writing the file if the new locale was already enabled */
769 if (!first_line)
770 fputc('\n', fw);
771 fputs(line, fw);
772 first_line = false;
773 continue;
774 }
775
776 line = strstrip(line);
777 if (isempty(line)) {
778 fputc('\n', fw);
779 first_line = false;
780 continue;
781 }
782
783 line_locale = line;
784 if (line_locale[0] == '#')
785 line_locale = strstrip(line_locale + 1);
786 else if (strcaseeq_ptr(line_locale, locale_entry))
787 return 0; /* the file already had our locale activated, so skip updating it */
788
789 if (strcaseeq_ptr(line_locale, locale_entry)) {
790 /* Uncomment existing line for new locale */
791 if (!first_line)
792 fputc('\n', fw);
793 fputs(locale_entry, fw);
794 locale_enabled = true;
795 first_line = false;
796 continue;
797 }
798
799 /* The line was not for the locale we want to enable, just copy it */
800 if (!first_line)
801 fputc('\n', fw);
802 fputs(line, fw);
803 first_line = false;
804 }
805 }
806
807 /* Add locale to enable to the end of the file if it was not found as commented line */
808 if (!locale_enabled) {
809 if (!write_new)
810 fputc('\n', fw);
811 fputs(locale_entry, fw);
812 }
813 fputc('\n', fw);
814
815 r = fflush_sync_and_check(fw);
816 if (r < 0)
817 return r;
818
819 if (rename(temp_path, "/etc/locale.gen") < 0)
820 return -errno;
821 temp_path = mfree(temp_path);
822
823 return 0;
824 #else
825 return -EOPNOTSUPP;
826 #endif
827 }
828
829 int locale_gen_run(void) {
830 #if HAVE_LOCALEGEN
831 pid_t pid;
832 int r;
833
834 r = safe_fork("(sd-localegen)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_CLOSE_ALL_FDS|FORK_LOG|FORK_WAIT, &pid);
835 if (r < 0)
836 return r;
837 if (r == 0) {
838 execl(LOCALEGEN_PATH, LOCALEGEN_PATH, NULL);
839 _exit(EXIT_FAILURE);
840 }
841
842 return 0;
843 #else
844 return -EOPNOTSUPP;
845 #endif
846 }