| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/stat.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-bus.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "env-file.h" |
| 10 |
#include "errno-util.h" |
| 11 |
#include "escape.h" |
| 12 |
#include "extract-word.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "fileio.h" |
| 15 |
#include "fs-util.h" |
| 16 |
#include "hashmap.h" |
| 17 |
#include "kbd-util.h" |
| 18 |
#include "localed-util.h" |
| 19 |
#include "log.h" |
| 20 |
#include "mkdir.h" |
| 21 |
#include "stat-util.h" |
| 22 |
#include "string-util.h" |
| 23 |
#include "strv.h" |
| 24 |
#include "tmpfile-util.h" |
| 25 |
#include "xkbcommon-util.h" |
| 26 |
|
| 27 |
#if HAVE_LOCALEGEN |
| 28 |
#include "copy.h" |
| 29 |
#include "process-util.h" |
| 30 |
#endif |
| 31 |
|
| 32 |
int x11_context_verify_and_warn(const X11Context *xc, int log_level, sd_bus_error *error) { |
| 33 |
int r; |
| 34 |
|
| 35 |
assert(xc); |
| 36 |
|
| 37 |
if (!x11_context_is_safe(xc)) { |
| 38 |
if (error) |
| 39 |
sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid X11 keyboard layout."); |
| 40 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EINVAL), "Invalid X11 keyboard layout."); |
| 41 |
} |
| 42 |
|
| 43 |
r = verify_xkb_rmlvo(xc->model, xc->layout, xc->variant, xc->options); |
| 44 |
if (r == -EOPNOTSUPP) { |
| 45 |
log_full_errno(MAX(log_level, LOG_NOTICE), r, |
| 46 |
"Cannot verify if new keymap is correct, libxkbcommon.so unavailable."); |
| 47 |
return 0; |
| 48 |
} |
| 49 |
if (r < 0) { |
| 50 |
if (error) |
| 51 |
sd_bus_error_set_errnof(error, r, "Specified keymap cannot be compiled, refusing as invalid."); |
| 52 |
return log_full_errno(log_level, r, |
| 53 |
"Cannot compile XKB keymap for x11 keyboard layout " |
| 54 |
"(model='%s' / layout='%s' / variant='%s' / options='%s'): %m", |
| 55 |
strempty(xc->model), strempty(xc->layout), strempty(xc->variant), strempty(xc->options)); |
| 56 |
} |
| 57 |
|
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static int verify_keymap(const char *keymap, int log_level, sd_bus_error *error) { |
| 62 |
int r; |
| 63 |
|
| 64 |
assert(keymap); |
| 65 |
|
| 66 |
r = keymap_exists(keymap); /* This also verifies that the keymap name is kosher. */ |
| 67 |
if (r <= 0) { |
| 68 |
_cleanup_free_ char *escaped = cescape(keymap); |
| 69 |
if (r < 0) { |
| 70 |
if (error) |
| 71 |
sd_bus_error_set_errnof(error, r, "Failed to check keymap %s: %m", strna(escaped)); |
| 72 |
return log_full_errno(log_level, r, "Failed to check keymap %s: %m", strna(escaped)); |
| 73 |
} |
| 74 |
if (error) |
| 75 |
sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Keymap %s is not installed.", strna(escaped)); |
| 76 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(ENOENT), "Keymap %s is not installed.", strna(escaped)); |
| 77 |
} |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
int vc_context_verify_and_warn(const VCContext *vc, int log_level, sd_bus_error *error) { |
| 83 |
int r; |
| 84 |
|
| 85 |
assert(vc); |
| 86 |
|
| 87 |
if (vc->keymap) { |
| 88 |
r = verify_keymap(vc->keymap, log_level, error); |
| 89 |
if (r < 0) |
| 90 |
return r; |
| 91 |
} |
| 92 |
|
| 93 |
if (vc->toggle) { |
| 94 |
r = verify_keymap(vc->toggle, log_level, error); |
| 95 |
if (r < 0) |
| 96 |
return r; |
| 97 |
} |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
void context_clear(Context *c) { |
| 103 |
assert(c); |
| 104 |
|
| 105 |
locale_context_clear(&c->locale_context); |
| 106 |
x11_context_clear(&c->x11_from_xorg); |
| 107 |
x11_context_clear(&c->x11_from_vc); |
| 108 |
vc_context_clear(&c->vc); |
| 109 |
|
| 110 |
c->locale_cache = sd_bus_message_unref(c->locale_cache); |
| 111 |
c->x11_cache = sd_bus_message_unref(c->x11_cache); |
| 112 |
c->vc_cache = sd_bus_message_unref(c->vc_cache); |
| 113 |
|
| 114 |
c->polkit_registry = hashmap_free(c->polkit_registry); |
| 115 |
}; |
| 116 |
|
| 117 |
X11Context *context_get_x11_context(Context *c) { |
| 118 |
assert(c); |
| 119 |
|
| 120 |
if (!x11_context_isempty(&c->x11_from_vc)) |
| 121 |
return &c->x11_from_vc; |
| 122 |
|
| 123 |
if (!x11_context_isempty(&c->x11_from_xorg)) |
| 124 |
return &c->x11_from_xorg; |
| 125 |
|
| 126 |
return &c->x11_from_vc; |
| 127 |
} |
| 128 |
|
| 129 |
int locale_read_data(Context *c, sd_bus_message *m) { |
| 130 |
assert(c); |
| 131 |
|
| 132 |
/* Do not try to re-read the file within single bus operation. */ |
| 133 |
if (m) { |
| 134 |
if (m == c->locale_cache) |
| 135 |
return 0; |
| 136 |
|
| 137 |
sd_bus_message_unref(c->locale_cache); |
| 138 |
c->locale_cache = sd_bus_message_ref(m); |
| 139 |
} |
| 140 |
|
| 141 |
return locale_context_load(&c->locale_context, LOCALE_LOAD_LOCALE_CONF | LOCALE_LOAD_ENVIRONMENT | LOCALE_LOAD_SIMPLIFY); |
| 142 |
} |
| 143 |
|
| 144 |
int vconsole_read_data(Context *c, sd_bus_message *m) { |
| 145 |
_cleanup_close_ int fd = -EBADF; |
| 146 |
struct stat st; |
| 147 |
int r; |
| 148 |
|
| 149 |
assert(c); |
| 150 |
|
| 151 |
/* Do not try to re-read the file within single bus operation. */ |
| 152 |
if (m) { |
| 153 |
if (m == c->vc_cache) |
| 154 |
return 0; |
| 155 |
|
| 156 |
sd_bus_message_unref(c->vc_cache); |
| 157 |
c->vc_cache = sd_bus_message_ref(m); |
| 158 |
} |
| 159 |
|
| 160 |
fd = RET_NERRNO(open(etc_vconsole_conf(), O_CLOEXEC | O_PATH)); |
| 161 |
if (fd == -ENOENT) { |
| 162 |
c->vc_stat = (struct stat) {}; |
| 163 |
vc_context_clear(&c->vc); |
| 164 |
x11_context_clear(&c->x11_from_vc); |
| 165 |
return 0; |
| 166 |
} |
| 167 |
if (fd < 0) |
| 168 |
return fd; |
| 169 |
|
| 170 |
if (fstat(fd, &st) < 0) |
| 171 |
return -errno; |
| 172 |
|
| 173 |
/* If the file is not changed, then we do not need to re-read */ |
| 174 |
if (stat_inode_unmodified(&c->vc_stat, &st)) |
| 175 |
return 0; |
| 176 |
|
| 177 |
c->vc_stat = st; |
| 178 |
vc_context_clear(&c->vc); |
| 179 |
x11_context_clear(&c->x11_from_vc); |
| 180 |
|
| 181 |
r = parse_env_file_fd( |
| 182 |
fd, etc_vconsole_conf(), |
| 183 |
"KEYMAP", &c->vc.keymap, |
| 184 |
"KEYMAP_TOGGLE", &c->vc.toggle, |
| 185 |
"XKBLAYOUT", &c->x11_from_vc.layout, |
| 186 |
"XKBMODEL", &c->x11_from_vc.model, |
| 187 |
"XKBVARIANT", &c->x11_from_vc.variant, |
| 188 |
"XKBOPTIONS", &c->x11_from_vc.options); |
| 189 |
if (r < 0) |
| 190 |
return r; |
| 191 |
|
| 192 |
if (vc_context_verify(&c->vc) < 0) |
| 193 |
vc_context_clear(&c->vc); |
| 194 |
|
| 195 |
if (x11_context_verify(&c->x11_from_vc) < 0) |
| 196 |
x11_context_clear(&c->x11_from_vc); |
| 197 |
|
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
int x11_read_data(Context *c, sd_bus_message *m) { |
| 202 |
_cleanup_close_ int fd = -EBADF; |
| 203 |
_cleanup_fclose_ FILE *f = NULL; |
| 204 |
bool in_section = false; |
| 205 |
struct stat st; |
| 206 |
int r; |
| 207 |
|
| 208 |
assert(c); |
| 209 |
|
| 210 |
/* Do not try to re-read the file within single bus operation. */ |
| 211 |
if (m) { |
| 212 |
if (m == c->x11_cache) |
| 213 |
return 0; |
| 214 |
|
| 215 |
sd_bus_message_unref(c->x11_cache); |
| 216 |
c->x11_cache = sd_bus_message_ref(m); |
| 217 |
} |
| 218 |
|
| 219 |
fd = RET_NERRNO(open("/etc/X11/xorg.conf.d/00-keyboard.conf", O_CLOEXEC | O_PATH)); |
| 220 |
if (fd == -ENOENT) { |
| 221 |
c->x11_stat = (struct stat) {}; |
| 222 |
x11_context_clear(&c->x11_from_xorg); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
if (fd < 0) |
| 226 |
return fd; |
| 227 |
|
| 228 |
if (fstat(fd, &st) < 0) |
| 229 |
return -errno; |
| 230 |
|
| 231 |
/* If the file is not changed, then we do not need to re-read */ |
| 232 |
if (stat_inode_unmodified(&c->x11_stat, &st)) |
| 233 |
return 0; |
| 234 |
|
| 235 |
c->x11_stat = st; |
| 236 |
x11_context_clear(&c->x11_from_xorg); |
| 237 |
|
| 238 |
r = fdopen_independent(fd, "re", &f); |
| 239 |
if (r < 0) |
| 240 |
return r; |
| 241 |
|
| 242 |
for (;;) { |
| 243 |
_cleanup_free_ char *line = NULL; |
| 244 |
|
| 245 |
r = read_stripped_line(f, LONG_LINE_MAX, &line); |
| 246 |
if (r < 0) |
| 247 |
return r; |
| 248 |
if (r == 0) |
| 249 |
break; |
| 250 |
|
| 251 |
if (IN_SET(line[0], 0, '#')) |
| 252 |
continue; |
| 253 |
|
| 254 |
if (in_section && first_word(line, "Option")) { |
| 255 |
_cleanup_strv_free_ char **a = NULL; |
| 256 |
|
| 257 |
r = strv_split_full(&a, line, WHITESPACE, EXTRACT_UNQUOTE); |
| 258 |
if (r < 0) |
| 259 |
return r; |
| 260 |
|
| 261 |
if (strv_length(a) == 3) { |
| 262 |
char **p = NULL; |
| 263 |
|
| 264 |
if (streq(a[1], "XkbLayout")) |
| 265 |
p = &c->x11_from_xorg.layout; |
| 266 |
else if (streq(a[1], "XkbModel")) |
| 267 |
p = &c->x11_from_xorg.model; |
| 268 |
else if (streq(a[1], "XkbVariant")) |
| 269 |
p = &c->x11_from_xorg.variant; |
| 270 |
else if (streq(a[1], "XkbOptions")) |
| 271 |
p = &c->x11_from_xorg.options; |
| 272 |
|
| 273 |
if (p) |
| 274 |
free_and_replace(*p, a[2]); |
| 275 |
} |
| 276 |
|
| 277 |
} else if (!in_section && first_word(line, "Section")) { |
| 278 |
_cleanup_strv_free_ char **a = NULL; |
| 279 |
|
| 280 |
r = strv_split_full(&a, line, WHITESPACE, EXTRACT_UNQUOTE); |
| 281 |
if (r < 0) |
| 282 |
return -ENOMEM; |
| 283 |
|
| 284 |
if (strv_length(a) == 2 && streq(a[1], "InputClass")) |
| 285 |
in_section = true; |
| 286 |
|
| 287 |
} else if (in_section && first_word(line, "EndSection")) |
| 288 |
in_section = false; |
| 289 |
} |
| 290 |
|
| 291 |
if (x11_context_verify(&c->x11_from_xorg) < 0) |
| 292 |
x11_context_clear(&c->x11_from_xorg); |
| 293 |
|
| 294 |
return 0; |
| 295 |
} |
| 296 |
|
| 297 |
int vconsole_write_data(Context *c) { |
| 298 |
_cleanup_strv_free_ char **l = NULL; |
| 299 |
const X11Context *xc; |
| 300 |
int r; |
| 301 |
|
| 302 |
assert(c); |
| 303 |
|
| 304 |
xc = context_get_x11_context(c); |
| 305 |
|
| 306 |
r = load_env_file(NULL, etc_vconsole_conf(), &l); |
| 307 |
if (r < 0 && r != -ENOENT) |
| 308 |
return r; |
| 309 |
|
| 310 |
r = vconsole_serialize(&c->vc, xc, &l); |
| 311 |
if (r < 0) |
| 312 |
return r; |
| 313 |
|
| 314 |
if (strv_isempty(l)) { |
| 315 |
if (unlink(etc_vconsole_conf()) < 0) |
| 316 |
return errno == ENOENT ? 0 : -errno; |
| 317 |
|
| 318 |
c->vc_stat = (struct stat) {}; |
| 319 |
return 0; |
| 320 |
} |
| 321 |
|
| 322 |
r = write_vconsole_conf(AT_FDCWD, etc_vconsole_conf(), l); |
| 323 |
if (r < 0) |
| 324 |
return r; |
| 325 |
|
| 326 |
if (stat(etc_vconsole_conf(), &c->vc_stat) < 0) |
| 327 |
return -errno; |
| 328 |
|
| 329 |
return 0; |
| 330 |
} |
| 331 |
|
| 332 |
int x11_write_data(Context *c) { |
| 333 |
_cleanup_fclose_ FILE *f = NULL; |
| 334 |
_cleanup_(unlink_and_freep) char *temp_path = NULL; |
| 335 |
const X11Context *xc; |
| 336 |
int r; |
| 337 |
|
| 338 |
assert(c); |
| 339 |
|
| 340 |
xc = context_get_x11_context(c); |
| 341 |
if (x11_context_isempty(xc)) { |
| 342 |
if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) |
| 343 |
return errno == ENOENT ? 0 : -errno; |
| 344 |
|
| 345 |
c->x11_stat = (struct stat) {}; |
| 346 |
return 0; |
| 347 |
} |
| 348 |
|
| 349 |
(void) mkdir_p_label("/etc/X11/xorg.conf.d", 0755); |
| 350 |
r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path); |
| 351 |
if (r < 0) |
| 352 |
return r; |
| 353 |
|
| 354 |
(void) fchmod(fileno(f), 0644); |
| 355 |
|
| 356 |
fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n" |
| 357 |
"# probably wise not to edit this file manually. Use localectl(1) to\n" |
| 358 |
"# update this file.\n" |
| 359 |
"Section \"InputClass\"\n" |
| 360 |
" Identifier \"system-keyboard\"\n" |
| 361 |
" MatchIsKeyboard \"on\"\n", f); |
| 362 |
|
| 363 |
if (!isempty(xc->layout)) |
| 364 |
fprintf(f, " Option \"XkbLayout\" \"%s\"\n", xc->layout); |
| 365 |
|
| 366 |
if (!isempty(xc->model)) |
| 367 |
fprintf(f, " Option \"XkbModel\" \"%s\"\n", xc->model); |
| 368 |
|
| 369 |
if (!isempty(xc->variant)) |
| 370 |
fprintf(f, " Option \"XkbVariant\" \"%s\"\n", xc->variant); |
| 371 |
|
| 372 |
if (!isempty(xc->options)) |
| 373 |
fprintf(f, " Option \"XkbOptions\" \"%s\"\n", xc->options); |
| 374 |
|
| 375 |
fputs("EndSection\n", f); |
| 376 |
|
| 377 |
r = fflush_sync_and_check(f); |
| 378 |
if (r < 0) |
| 379 |
return r; |
| 380 |
|
| 381 |
if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) |
| 382 |
return -errno; |
| 383 |
|
| 384 |
if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &c->x11_stat) < 0) |
| 385 |
return -errno; |
| 386 |
|
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
bool locale_gen_check_available(void) { |
| 391 |
#if HAVE_LOCALEGEN |
| 392 |
if (access(LOCALEGEN_PATH, X_OK) < 0) { |
| 393 |
if (errno != ENOENT) |
| 394 |
log_warning_errno(errno, "Unable to determine whether " LOCALEGEN_PATH " exists and is executable, assuming it is not: %m"); |
| 395 |
return false; |
| 396 |
} |
| 397 |
if (access("/etc/locale.gen", F_OK) < 0) { |
| 398 |
if (errno != ENOENT) |
| 399 |
log_warning_errno(errno, "Unable to determine whether /etc/locale.gen exists, assuming it does not: %m"); |
| 400 |
return false; |
| 401 |
} |
| 402 |
return true; |
| 403 |
#else |
| 404 |
return false; |
| 405 |
#endif |
| 406 |
} |
| 407 |
|
| 408 |
#if HAVE_LOCALEGEN |
| 409 |
static bool locale_encoding_is_utf8_or_unspecified(const char *locale) { |
| 410 |
const char *c = strchr(locale, '.'); |
| 411 |
return !c || strcaseeq(c, ".UTF-8") || strcasestr(locale, ".UTF-8@"); |
| 412 |
} |
| 413 |
|
| 414 |
static int locale_gen_locale_supported(const char *locale_entry) { |
| 415 |
/* Returns an error value <= 0 if the locale-gen entry is invalid or unsupported, |
| 416 |
* 1 in case the locale entry is valid, and -EOPNOTSUPP specifically in case |
| 417 |
* the distributor has not provided us with a SUPPORTED file to check |
| 418 |
* locale for validity. */ |
| 419 |
|
| 420 |
_cleanup_fclose_ FILE *f = NULL; |
| 421 |
int r; |
| 422 |
|
| 423 |
assert(locale_entry); |
| 424 |
|
| 425 |
/* Locale templates without country code are never supported */ |
| 426 |
if (!strstr(locale_entry, "_")) |
| 427 |
return -EINVAL; |
| 428 |
|
| 429 |
f = fopen("/usr/share/i18n/SUPPORTED", "re"); |
| 430 |
if (!f) { |
| 431 |
if (errno == ENOENT) |
| 432 |
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 433 |
"Unable to check validity of locale entry %s: /usr/share/i18n/SUPPORTED does not exist", |
| 434 |
locale_entry); |
| 435 |
return -errno; |
| 436 |
} |
| 437 |
|
| 438 |
for (;;) { |
| 439 |
_cleanup_free_ char *line = NULL; |
| 440 |
|
| 441 |
r = read_stripped_line(f, LONG_LINE_MAX, &line); |
| 442 |
if (r < 0) |
| 443 |
return log_debug_errno(r, "Failed to read /usr/share/i18n/SUPPORTED: %m"); |
| 444 |
if (r == 0) |
| 445 |
return 0; |
| 446 |
|
| 447 |
if (strcaseeq_ptr(line, locale_entry)) |
| 448 |
return 1; |
| 449 |
} |
| 450 |
} |
| 451 |
#endif |
| 452 |
|
| 453 |
int locale_gen_enable_locale(const char *locale) { |
| 454 |
#if HAVE_LOCALEGEN |
| 455 |
_cleanup_fclose_ FILE *fr = NULL, *fw = NULL; |
| 456 |
_cleanup_(unlink_and_freep) char *temp_path = NULL; |
| 457 |
_cleanup_free_ char *locale_entry = NULL; |
| 458 |
bool locale_enabled = false, first_line = false; |
| 459 |
bool write_new = false; |
| 460 |
int r; |
| 461 |
|
| 462 |
if (isempty(locale)) |
| 463 |
return 0; |
| 464 |
|
| 465 |
if (locale_encoding_is_utf8_or_unspecified(locale)) { |
| 466 |
locale_entry = strjoin(locale, " UTF-8"); |
| 467 |
if (!locale_entry) |
| 468 |
return -ENOMEM; |
| 469 |
} else |
| 470 |
return -ENOEXEC; /* We do not process non-UTF-8 locale */ |
| 471 |
|
| 472 |
r = locale_gen_locale_supported(locale_entry); |
| 473 |
if (r == 0) |
| 474 |
return -EINVAL; |
| 475 |
if (r < 0 && r != -EOPNOTSUPP) |
| 476 |
return r; |
| 477 |
|
| 478 |
fr = fopen("/etc/locale.gen", "re"); |
| 479 |
if (!fr) { |
| 480 |
if (errno != ENOENT) |
| 481 |
return -errno; |
| 482 |
write_new = true; |
| 483 |
} |
| 484 |
|
| 485 |
r = fopen_temporary("/etc/locale.gen", &fw, &temp_path); |
| 486 |
if (r < 0) |
| 487 |
return r; |
| 488 |
|
| 489 |
if (write_new) |
| 490 |
(void) fchmod(fileno(fw), 0644); |
| 491 |
else { |
| 492 |
/* apply mode & xattrs of the original file to new file */ |
| 493 |
r = copy_access(fileno(fr), fileno(fw)); |
| 494 |
if (r < 0) |
| 495 |
return r; |
| 496 |
r = copy_xattr(fileno(fr), NULL, fileno(fw), NULL, COPY_ALL_XATTRS); |
| 497 |
if (r < 0) |
| 498 |
log_debug_errno(r, "Failed to copy all xattrs from old to new /etc/locale.gen file, ignoring: %m"); |
| 499 |
} |
| 500 |
|
| 501 |
if (!write_new) { |
| 502 |
/* The config file ends with a line break, which we do not want to include before potentially appending a new locale |
| 503 |
* instead of uncommenting an existing line. By prepending linebreaks, we can avoid buffering this file but can still write |
| 504 |
* a nice config file without empty lines */ |
| 505 |
first_line = true; |
| 506 |
for (;;) { |
| 507 |
_cleanup_free_ char *line = NULL; |
| 508 |
char *line_locale; |
| 509 |
|
| 510 |
r = read_line(fr, LONG_LINE_MAX, &line); |
| 511 |
if (r < 0) |
| 512 |
return r; |
| 513 |
if (r == 0) |
| 514 |
break; |
| 515 |
|
| 516 |
if (locale_enabled) { |
| 517 |
/* Just complete writing the file if the new locale was already enabled */ |
| 518 |
if (!first_line) |
| 519 |
fputc('\n', fw); |
| 520 |
fputs(line, fw); |
| 521 |
first_line = false; |
| 522 |
continue; |
| 523 |
} |
| 524 |
|
| 525 |
line_locale = strstrip(line); |
| 526 |
if (isempty(line_locale)) { |
| 527 |
fputc('\n', fw); |
| 528 |
first_line = false; |
| 529 |
continue; |
| 530 |
} |
| 531 |
|
| 532 |
if (line_locale[0] == '#') |
| 533 |
line_locale = strstrip(line_locale + 1); |
| 534 |
else if (strcaseeq_ptr(line_locale, locale_entry)) |
| 535 |
return 0; /* the file already had our locale activated, so skip updating it */ |
| 536 |
|
| 537 |
if (strcaseeq_ptr(line_locale, locale_entry)) { |
| 538 |
/* Uncomment existing line for new locale */ |
| 539 |
if (!first_line) |
| 540 |
fputc('\n', fw); |
| 541 |
fputs(locale_entry, fw); |
| 542 |
locale_enabled = true; |
| 543 |
first_line = false; |
| 544 |
continue; |
| 545 |
} |
| 546 |
|
| 547 |
/* The line was not for the locale we want to enable, just copy it */ |
| 548 |
if (!first_line) |
| 549 |
fputc('\n', fw); |
| 550 |
fputs(line, fw); |
| 551 |
first_line = false; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
/* Add locale to enable to the end of the file if it was not found as commented line */ |
| 556 |
if (!locale_enabled) { |
| 557 |
if (!write_new) |
| 558 |
fputc('\n', fw); |
| 559 |
fputs(locale_entry, fw); |
| 560 |
} |
| 561 |
fputc('\n', fw); |
| 562 |
|
| 563 |
r = fflush_sync_and_check(fw); |
| 564 |
if (r < 0) |
| 565 |
return r; |
| 566 |
|
| 567 |
if (rename(temp_path, "/etc/locale.gen") < 0) |
| 568 |
return -errno; |
| 569 |
temp_path = mfree(temp_path); |
| 570 |
|
| 571 |
return 0; |
| 572 |
#else |
| 573 |
return -EOPNOTSUPP; |
| 574 |
#endif |
| 575 |
} |
| 576 |
|
| 577 |
int locale_gen_run(void) { |
| 578 |
#if HAVE_LOCALEGEN |
| 579 |
int r; |
| 580 |
|
| 581 |
r = pidref_safe_fork( |
| 582 |
"(locale-gen)", |
| 583 |
FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_CLOSE_ALL_FDS|FORK_LOG|FORK_WAIT, |
| 584 |
/* ret= */ NULL); |
| 585 |
if (r < 0) |
| 586 |
return r; |
| 587 |
if (r == 0) { |
| 588 |
execl(LOCALEGEN_PATH, LOCALEGEN_PATH, NULL); |
| 589 |
_exit(EXIT_FAILURE); |
| 590 |
} |
| 591 |
|
| 592 |
return 0; |
| 593 |
#else |
| 594 |
return -EOPNOTSUPP; |
| 595 |
#endif |
| 596 |
} |