summaryrefslogtreecommitdiff
path: root/src/core/main.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2016-06-22 12:34:25 +0200
committerGitHub <noreply@github.com>2016-06-22 12:34:25 +0200
commit98471bf0fa040ead227630aee77cb3feb25cf3b0 (patch)
tree9964d910e6220a9005168c4e4ddce551dea93add /src/core/main.c
parent2f9df7c96a25adb42093ee3ee201577f3e01da42 (diff)
parent3a18b60489504056f9b0b1a139439cbfa60a87e1 (diff)
Merge pull request #3526 from fbuihuu/fix-console-log-color
Fix console log color
Diffstat (limited to 'src/core/main.c')
-rw-r--r--src/core/main.c56
1 files changed, 47 insertions, 9 deletions
diff --git a/src/core/main.c b/src/core/main.c
index 2785a3aa0b..237c9c9ebe 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -1294,6 +1294,35 @@ static int bump_unix_max_dgram_qlen(void) {
return 1;
}
+static int fixup_environment(void) {
+ _cleanup_free_ char *term = NULL;
+ int r;
+
+ /* When started as PID1, the kernel uses /dev/console
+ * for our stdios and uses TERM=linux whatever the
+ * backend device used by the console. We try to make
+ * a better guess here since some consoles might not
+ * have support for color mode for example.
+ *
+ * However if TERM was configured through the kernel
+ * command line then leave it alone. */
+
+ r = get_proc_cmdline_key("TERM=", &term);
+ if (r < 0)
+ return r;
+
+ if (r == 0) {
+ term = strdup(default_term_for_tty("/dev/console") + 5);
+ if (!term)
+ return -errno;
+ }
+
+ if (setenv("TERM", term, 1) < 0)
+ return -errno;
+
+ return 0;
+}
+
int main(int argc, char *argv[]) {
Manager *m = NULL;
int r, retval = EXIT_FAILURE;
@@ -1353,7 +1382,6 @@ int main(int argc, char *argv[]) {
saved_argv = argv;
saved_argc = argc;
- log_show_color(colors_enabled());
log_set_upgrade_syslog_to_journal(true);
/* Disable the umask logic */
@@ -1364,7 +1392,6 @@ int main(int argc, char *argv[]) {
/* Running outside of a container as PID 1 */
arg_system = true;
- make_null_stdio();
log_set_target(LOG_TARGET_KMSG);
log_open();
@@ -1480,6 +1507,21 @@ int main(int argc, char *argv[]) {
(void) write_string_file("/proc/sys/kernel/core_pattern", "|/bin/false", 0);
}
+ /* We expect the environment to be set correctly if run inside a
+ * container. */
+ if (arg_system && detect_container() <= 0) {
+ if (fixup_environment() < 0) {
+ error_message = "Failed to fix up PID1 environment";
+ goto finish;
+ }
+
+ /* Try to figure out if we can use colors with the console. No
+ * need to do that for user instances since they never log
+ * into the console. */
+ log_show_color(colors_enabled());
+ make_null_stdio();
+ }
+
/* Initialize default unit */
r = free_and_strdup(&arg_default_unit, SPECIAL_DEFAULT_TARGET);
if (r < 0) {
@@ -1967,6 +2009,9 @@ finish:
log_error_errno(r, "Failed to switch root, trying to continue: %m");
}
+ /* Reopen the console */
+ (void) make_console_stdio();
+
args_size = MAX(6, argc+1);
args = newa(const char*, args_size);
@@ -1992,10 +2037,6 @@ finish:
args[i++] = sfd;
args[i++] = NULL;
- /* do not pass along the environment we inherit from the kernel or initrd */
- if (switch_root_dir)
- (void) clearenv();
-
assert(i <= args_size);
/*
@@ -2018,9 +2059,6 @@ finish:
arg_serialization = safe_fclose(arg_serialization);
fds = fdset_free(fds);
- /* Reopen the console */
- (void) make_console_stdio();
-
for (j = 1, i = 1; j < (unsigned) argc; j++)
args[i++] = argv[j];
args[i++] = NULL;