]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/localed-util.c
Merge pull request #25602 from fbuihuu/fix-TEST-73-LOCALE
[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_(unlink_and_freep) 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 return r;
304
305 if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
306 return -errno;
307
308 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) >= 0)
309 c->x11_mtime = timespec_load(&st.st_mtim);
310
311 return 0;
312 }
313
314 static int read_next_mapping(const char* filename,
315 unsigned min_fields, unsigned max_fields,
316 FILE *f, unsigned *n, char ***a) {
317 assert(f);
318 assert(n);
319 assert(a);
320
321 for (;;) {
322 _cleanup_free_ char *line = NULL;
323 size_t length;
324 char *l, **b;
325 int r;
326
327 r = read_line(f, LONG_LINE_MAX, &line);
328 if (r < 0)
329 return r;
330 if (r == 0)
331 break;
332
333 (*n)++;
334
335 l = strstrip(line);
336 if (IN_SET(l[0], 0, '#'))
337 continue;
338
339 r = strv_split_full(&b, l, WHITESPACE, EXTRACT_UNQUOTE);
340 if (r < 0)
341 return r;
342
343 length = strv_length(b);
344 if (length < min_fields || length > max_fields) {
345 log_error("Invalid line %s:%u, ignoring.", filename, *n);
346 strv_free(b);
347 continue;
348
349 }
350
351 *a = b;
352 return 1;
353 }
354
355 return 0;
356 }
357
358 int vconsole_convert_to_x11(Context *c) {
359 const char *map;
360 int modified = -1;
361
362 map = systemd_kbd_model_map();
363
364 if (isempty(c->vc_keymap)) {
365 modified =
366 !isempty(c->x11_layout) ||
367 !isempty(c->x11_model) ||
368 !isempty(c->x11_variant) ||
369 !isempty(c->x11_options);
370
371 context_free_x11(c);
372 } else {
373 _cleanup_fclose_ FILE *f = NULL;
374 unsigned n = 0;
375
376 f = fopen(map, "re");
377 if (!f)
378 return -errno;
379
380 for (;;) {
381 _cleanup_strv_free_ char **a = NULL;
382 int r;
383
384 r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a);
385 if (r < 0)
386 return r;
387 if (r == 0)
388 break;
389
390 if (!streq(c->vc_keymap, a[0]))
391 continue;
392
393 if (!streq_ptr(c->x11_layout, empty_or_dash_to_null(a[1])) ||
394 !streq_ptr(c->x11_model, empty_or_dash_to_null(a[2])) ||
395 !streq_ptr(c->x11_variant, empty_or_dash_to_null(a[3])) ||
396 !streq_ptr(c->x11_options, empty_or_dash_to_null(a[4]))) {
397
398 if (free_and_strdup(&c->x11_layout, empty_or_dash_to_null(a[1])) < 0 ||
399 free_and_strdup(&c->x11_model, empty_or_dash_to_null(a[2])) < 0 ||
400 free_and_strdup(&c->x11_variant, empty_or_dash_to_null(a[3])) < 0 ||
401 free_and_strdup(&c->x11_options, empty_or_dash_to_null(a[4])) < 0)
402 return -ENOMEM;
403
404 modified = true;
405 }
406
407 break;
408 }
409 }
410
411 if (modified > 0)
412 log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
413 strempty(c->x11_layout),
414 strempty(c->x11_model),
415 strempty(c->x11_variant),
416 strempty(c->x11_options));
417 else if (modified < 0)
418 log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".",
419 c->vc_keymap);
420 else
421 log_debug("X11 keyboard layout did not need to be modified.");
422
423 return modified > 0;
424 }
425
426 int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap) {
427 _cleanup_free_ char *n = NULL;
428
429 if (x11_variant)
430 n = strjoin(x11_layout, "-", x11_variant);
431 else
432 n = strdup(x11_layout);
433 if (!n)
434 return -ENOMEM;
435
436 NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
437 _cleanup_free_ char *p = NULL, *pz = NULL;
438 bool uncompressed;
439
440 p = strjoin(dir, "xkb/", n, ".map");
441 pz = strjoin(dir, "xkb/", n, ".map.gz");
442 if (!p || !pz)
443 return -ENOMEM;
444
445 uncompressed = access(p, F_OK) == 0;
446 if (uncompressed || access(pz, F_OK) == 0) {
447 log_debug("Found converted keymap %s at %s",
448 n, uncompressed ? p : pz);
449
450 *new_keymap = TAKE_PTR(n);
451 return 1;
452 }
453 }
454
455 return 0;
456 }
457
458 int find_legacy_keymap(Context *c, char **ret) {
459 const char *map;
460 _cleanup_fclose_ FILE *f = NULL;
461 _cleanup_free_ char *new_keymap = NULL;
462 unsigned n = 0;
463 unsigned best_matching = 0;
464 int r;
465
466 assert(!isempty(c->x11_layout));
467
468 map = systemd_kbd_model_map();
469
470 f = fopen(map, "re");
471 if (!f)
472 return -errno;
473
474 for (;;) {
475 _cleanup_strv_free_ char **a = NULL;
476 unsigned matching = 0;
477
478 r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a);
479 if (r < 0)
480 return r;
481 if (r == 0)
482 break;
483
484 /* Determine how well matching this entry is */
485 if (streq(c->x11_layout, a[1]))
486 /* If we got an exact match, this is best */
487 matching = 10;
488 else {
489 /* We have multiple X layouts, look for an
490 * entry that matches our key with everything
491 * but the first layout stripped off. */
492 if (startswith_comma(c->x11_layout, a[1]))
493 matching = 5;
494 else {
495 _cleanup_free_ char *x = NULL;
496
497 /* If that didn't work, strip off the
498 * other layouts from the entry, too */
499 x = strndup(a[1], strcspn(a[1], ","));
500 if (startswith_comma(c->x11_layout, x))
501 matching = 1;
502 }
503 }
504
505 if (matching > 0) {
506 if (isempty(c->x11_model) || streq_ptr(c->x11_model, a[2])) {
507 matching++;
508
509 if (streq_ptr(c->x11_variant, a[3])) {
510 matching++;
511
512 if (streq_ptr(c->x11_options, a[4]))
513 matching++;
514 }
515 }
516 }
517
518 /* The best matching entry so far, then let's save that */
519 if (matching >= MAX(best_matching, 1u)) {
520 log_debug("Found legacy keymap %s with score %u",
521 a[0], matching);
522
523 if (matching > best_matching) {
524 best_matching = matching;
525
526 r = free_and_strdup(&new_keymap, a[0]);
527 if (r < 0)
528 return r;
529 }
530 }
531 }
532
533 if (best_matching < 10 && c->x11_layout) {
534 /* The best match is only the first part of the X11
535 * keymap. Check if we have a converted map which
536 * matches just the first layout.
537 */
538 char *l, *v = NULL, *converted;
539
540 l = strndupa_safe(c->x11_layout, strcspn(c->x11_layout, ","));
541 if (c->x11_variant)
542 v = strndupa_safe(c->x11_variant,
543 strcspn(c->x11_variant, ","));
544 r = find_converted_keymap(l, v, &converted);
545 if (r < 0)
546 return r;
547 if (r > 0)
548 free_and_replace(new_keymap, converted);
549 }
550
551 *ret = TAKE_PTR(new_keymap);
552 return (bool) *ret;
553 }
554
555 int find_language_fallback(const char *lang, char **language) {
556 const char *map;
557 _cleanup_fclose_ FILE *f = NULL;
558 unsigned n = 0;
559
560 assert(lang);
561 assert(language);
562
563 map = systemd_language_fallback_map();
564
565 f = fopen(map, "re");
566 if (!f)
567 return -errno;
568
569 for (;;) {
570 _cleanup_strv_free_ char **a = NULL;
571 int r;
572
573 r = read_next_mapping(map, 2, 2, f, &n, &a);
574 if (r <= 0)
575 return r;
576
577 if (streq(lang, a[0])) {
578 assert(strv_length(a) == 2);
579 *language = TAKE_PTR(a[1]);
580 return 1;
581 }
582 }
583
584 assert_not_reached();
585 }
586
587 int x11_convert_to_vconsole(Context *c) {
588 bool modified = false;
589
590 if (isempty(c->x11_layout)) {
591 modified =
592 !isempty(c->vc_keymap) ||
593 !isempty(c->vc_keymap_toggle);
594
595 context_free_vconsole(c);
596 } else {
597 _cleanup_free_ char *new_keymap = NULL;
598 int r;
599
600 r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap);
601 if (r < 0)
602 return r;
603 else if (r == 0) {
604 r = find_legacy_keymap(c, &new_keymap);
605 if (r < 0)
606 return r;
607 }
608 if (r == 0)
609 /* We search for layout-variant match first, but then we also look
610 * for anything which matches just the layout. So it's accurate to say
611 * that we couldn't find anything which matches the layout. */
612 log_notice("No conversion to virtual console map found for \"%s\".",
613 c->x11_layout);
614
615 if (!streq_ptr(c->vc_keymap, new_keymap)) {
616 free_and_replace(c->vc_keymap, new_keymap);
617 c->vc_keymap_toggle = mfree(c->vc_keymap_toggle);
618 modified = true;
619 }
620 }
621
622 if (modified)
623 log_info("Changing virtual console keymap to '%s' toggle '%s'",
624 strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
625 else
626 log_debug("Virtual console keymap was not modified.");
627
628 return modified;
629 }
630
631 bool locale_gen_check_available(void) {
632 #if HAVE_LOCALEGEN
633 if (access(LOCALEGEN_PATH, X_OK) < 0) {
634 if (errno != ENOENT)
635 log_warning_errno(errno, "Unable to determine whether " LOCALEGEN_PATH " exists and is executable, assuming it is not: %m");
636 return false;
637 }
638 if (access("/etc/locale.gen", F_OK) < 0) {
639 if (errno != ENOENT)
640 log_warning_errno(errno, "Unable to determine whether /etc/locale.gen exists, assuming it does not: %m");
641 return false;
642 }
643 return true;
644 #else
645 return false;
646 #endif
647 }
648
649 #if HAVE_LOCALEGEN
650 static bool locale_encoding_is_utf8_or_unspecified(const char *locale) {
651 const char *c = strchr(locale, '.');
652 return !c || strcaseeq(c, ".UTF-8") || strcasestr(locale, ".UTF-8@");
653 }
654
655 static int locale_gen_locale_supported(const char *locale_entry) {
656 /* Returns an error valus <= 0 if the locale-gen entry is invalid or unsupported,
657 * 1 in case the locale entry is valid, and -EOPNOTSUPP specifically in case
658 * the distributor has not provided us with a SUPPORTED file to check
659 * locale for validity. */
660
661 _cleanup_fclose_ FILE *f = NULL;
662 int r;
663
664 assert(locale_entry);
665
666 /* Locale templates without country code are never supported */
667 if (!strstr(locale_entry, "_"))
668 return -EINVAL;
669
670 f = fopen("/usr/share/i18n/SUPPORTED", "re");
671 if (!f) {
672 if (errno == ENOENT)
673 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
674 "Unable to check validity of locale entry %s: /usr/share/i18n/SUPPORTED does not exist",
675 locale_entry);
676 return -errno;
677 }
678
679 for (;;) {
680 _cleanup_free_ char *line = NULL;
681
682 r = read_line(f, LONG_LINE_MAX, &line);
683 if (r < 0)
684 return log_debug_errno(r, "Failed to read /usr/share/i18n/SUPPORTED: %m");
685 if (r == 0)
686 return 0;
687
688 line = strstrip(line);
689 if (strcaseeq_ptr(line, locale_entry))
690 return 1;
691 }
692 }
693 #endif
694
695 int locale_gen_enable_locale(const char *locale) {
696 #if HAVE_LOCALEGEN
697 _cleanup_fclose_ FILE *fr = NULL, *fw = NULL;
698 _cleanup_(unlink_and_freep) char *temp_path = NULL;
699 _cleanup_free_ char *locale_entry = NULL;
700 bool locale_enabled = false, first_line = false;
701 bool write_new = false;
702 int r;
703
704 if (isempty(locale))
705 return 0;
706
707 if (locale_encoding_is_utf8_or_unspecified(locale)) {
708 locale_entry = strjoin(locale, " UTF-8");
709 if (!locale_entry)
710 return -ENOMEM;
711 } else
712 return -ENOEXEC; /* We do not process non-UTF-8 locale */
713
714 r = locale_gen_locale_supported(locale_entry);
715 if (r == 0)
716 return -EINVAL;
717 if (r < 0 && r != -EOPNOTSUPP)
718 return r;
719
720 fr = fopen("/etc/locale.gen", "re");
721 if (!fr) {
722 if (errno != ENOENT)
723 return -errno;
724 write_new = true;
725 }
726
727 r = fopen_temporary("/etc/locale.gen", &fw, &temp_path);
728 if (r < 0)
729 return r;
730
731 if (write_new)
732 (void) fchmod(fileno(fw), 0644);
733 else {
734 /* apply mode & xattrs of the original file to new file */
735 r = copy_access(fileno(fr), fileno(fw));
736 if (r < 0)
737 return r;
738 r = copy_xattr(fileno(fr), fileno(fw), COPY_ALL_XATTRS);
739 if (r < 0)
740 log_debug_errno(r, "Failed to copy all xattrs from old to new /etc/locale.gen file, ignoring: %m");
741 }
742
743 if (!write_new) {
744 /* The config file ends with a line break, which we do not want to include before potentially appending a new locale
745 * instead of uncommenting an existing line. By prepending linebreaks, we can avoid buffering this file but can still write
746 * a nice config file without empty lines */
747 first_line = true;
748 for (;;) {
749 _cleanup_free_ char *line = NULL;
750 char *line_locale;
751
752 r = read_line(fr, LONG_LINE_MAX, &line);
753 if (r < 0)
754 return r;
755 if (r == 0)
756 break;
757
758 if (locale_enabled) {
759 /* Just complete writing the file if the new locale was already enabled */
760 if (!first_line)
761 fputc('\n', fw);
762 fputs(line, fw);
763 first_line = false;
764 continue;
765 }
766
767 line = strstrip(line);
768 if (isempty(line)) {
769 fputc('\n', fw);
770 first_line = false;
771 continue;
772 }
773
774 line_locale = line;
775 if (line_locale[0] == '#')
776 line_locale = strstrip(line_locale + 1);
777 else if (strcaseeq_ptr(line_locale, locale_entry))
778 return 0; /* the file already had our locale activated, so skip updating it */
779
780 if (strcaseeq_ptr(line_locale, locale_entry)) {
781 /* Uncomment existing line for new locale */
782 if (!first_line)
783 fputc('\n', fw);
784 fputs(locale_entry, fw);
785 locale_enabled = true;
786 first_line = false;
787 continue;
788 }
789
790 /* The line was not for the locale we want to enable, just copy it */
791 if (!first_line)
792 fputc('\n', fw);
793 fputs(line, fw);
794 first_line = false;
795 }
796 }
797
798 /* Add locale to enable to the end of the file if it was not found as commented line */
799 if (!locale_enabled) {
800 if (!write_new)
801 fputc('\n', fw);
802 fputs(locale_entry, fw);
803 }
804 fputc('\n', fw);
805
806 r = fflush_sync_and_check(fw);
807 if (r < 0)
808 return r;
809
810 if (rename(temp_path, "/etc/locale.gen") < 0)
811 return -errno;
812 temp_path = mfree(temp_path);
813
814 return 0;
815 #else
816 return -EOPNOTSUPP;
817 #endif
818 }
819
820 int locale_gen_run(void) {
821 #if HAVE_LOCALEGEN
822 pid_t pid;
823 int r;
824
825 r = safe_fork("(sd-localegen)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_CLOSE_ALL_FDS|FORK_LOG|FORK_WAIT, &pid);
826 if (r < 0)
827 return r;
828 if (r == 0) {
829 execl(LOCALEGEN_PATH, LOCALEGEN_PATH, NULL);
830 _exit(EXIT_FAILURE);
831 }
832
833 return 0;
834 #else
835 return -EOPNOTSUPP;
836 #endif
837 }