[systemd-commits] 5 commits - src/core src/shared
Michal Schmidt
michich at kemper.freedesktop.org
Fri Feb 15 16:24:33 PST 2013
src/core/execute.c | 113 ++++++++++++++++++++---------------------------------
src/core/unit.c | 6 +-
src/shared/log.c | 19 ++++----
3 files changed, 58 insertions(+), 80 deletions(-)
New commits:
commit 9d246da3c630559924a0262769c8493fa22c7acc
Author: Michal Schmidt <mschmidt at redhat.com>
Date: Fri Feb 15 22:42:26 2013 +0100
unit: don't Require systemd-journald.socket from units
It is not really necessary to have a hard requirement dependency on
systemd-journald.socket in almost every unit. The socket gets pulled
into boot via at least two ways:
sockets.target -> systemd-journald.socket
sysinit.target -> systemd-journald.service -> systemd-journald.socket
So just assume something pulled the socket in and drop the automatic
requirement dependencies on it.
"systemctl stop systemd-journald.socket" will now not take the whole
system down with it.
diff --git a/src/core/unit.c b/src/core/unit.c
index 86aaa15..3a88996 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -616,9 +616,11 @@ int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
/* If syslog or kernel logging is requested, make sure our own
* logging daemon is run first. */
- if (u->manager->running_as == SYSTEMD_SYSTEM)
- if ((r = unit_add_two_dependencies_by_name(u, UNIT_REQUIRES, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, NULL, true)) < 0)
+ if (u->manager->running_as == SYSTEMD_SYSTEM) {
+ r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, NULL, true);
+ if (r < 0)
return r;
+ }
return 0;
}
commit 80cbda3558c534b575bf5315fa53b0f82dc088ab
Author: Michal Schmidt <mschmidt at redhat.com>
Date: Sat Feb 16 00:34:12 2013 +0100
execute: increase severity of journal connect failure message
journald is supposed to work. Failure to connect to its socket implies
losing messages. It should be a very unusual event. Log the failure with
LOG_CRIT.
Just because this unit's stdout/stderr failed to connect to the journal
does not necessarily mean that we shouldn't try to log the failure using
a structured entry, so let's use log_struct_unit.
diff --git a/src/core/execute.c b/src/core/execute.c
index d459dfd..b28962a 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -389,9 +389,12 @@ static int setup_output(const ExecContext *context, int fileno, int socket_fd, c
case EXEC_OUTPUT_JOURNAL_AND_CONSOLE:
r = connect_logger_as(context, o, ident, unit_id, fileno);
if (r < 0) {
- log_error("Failed to connect std%s of %s to the journal socket: %s",
+ log_struct_unit(LOG_CRIT, unit_id,
+ "MESSAGE=Failed to connect std%s of %s to the journal socket: %s",
fileno == STDOUT_FILENO ? "out" : "err",
- unit_id, strerror(-r));
+ unit_id, strerror(-r),
+ "ERRNO=%d", -r,
+ NULL);
r = open_null_as(O_WRONLY, fileno);
}
return r;
commit eb17e935988993f4e0ebe08be81150ce35b140bf
Author: Michal Schmidt <mschmidt at redhat.com>
Date: Fri Feb 15 23:36:23 2013 +0100
execute: unify setup_{output,error}
The functions are quite similar. Unify them into one.
The source gets shorter, the binary gets slightly smaller.
diff --git a/src/core/execute.c b/src/core/execute.c
index b878c33..d459dfd 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -320,7 +320,7 @@ static int setup_input(const ExecContext *context, int socket_fd, bool apply_tty
}
}
-static int setup_output(const ExecContext *context, int socket_fd, const char *ident, const char *unit_id, bool apply_tty_stdin) {
+static int setup_output(const ExecContext *context, int fileno, int socket_fd, const char *ident, const char *unit_id, bool apply_tty_stdin) {
ExecOutput o;
ExecInput i;
int r;
@@ -331,97 +331,55 @@ static int setup_output(const ExecContext *context, int socket_fd, const char *i
i = fixup_input(context->std_input, socket_fd, apply_tty_stdin);
o = fixup_output(context->std_output, socket_fd);
- /* This expects the input is already set up */
+ if (fileno == STDERR_FILENO) {
+ ExecOutput e;
+ e = fixup_output(context->std_error, socket_fd);
- switch (o) {
+ /* This expects the input and output are already set up */
+
+ /* Don't change the stderr file descriptor if we inherit all
+ * the way and are not on a tty */
+ if (e == EXEC_OUTPUT_INHERIT &&
+ o == EXEC_OUTPUT_INHERIT &&
+ i == EXEC_INPUT_NULL &&
+ !is_terminal_input(context->std_input) &&
+ getppid () != 1)
+ return fileno;
+
+ /* Duplicate from stdout if possible */
+ if (e == o || e == EXEC_OUTPUT_INHERIT)
+ return dup2(STDOUT_FILENO, fileno) < 0 ? -errno : fileno;
- case EXEC_OUTPUT_INHERIT:
+ o = e;
+ } else if (o == EXEC_OUTPUT_INHERIT) {
/* If input got downgraded, inherit the original value */
if (i == EXEC_INPUT_NULL && is_terminal_input(context->std_input))
- return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO);
+ return open_terminal_as(tty_path(context), O_WRONLY, fileno);
/* If the input is connected to anything that's not a /dev/null, inherit that... */
if (i != EXEC_INPUT_NULL)
- return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
+ return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno;
/* If we are not started from PID 1 we just inherit STDOUT from our parent process. */
if (getppid() != 1)
- return STDOUT_FILENO;
-
- /* We need to open /dev/null here anew, to get the
- * right access mode. So we fall through */
-
- case EXEC_OUTPUT_NULL:
- return open_null_as(O_WRONLY, STDOUT_FILENO);
-
- case EXEC_OUTPUT_TTY:
- if (is_terminal_input(i))
- return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
-
- /* We don't reset the terminal if this is just about output */
- return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO);
+ return fileno;
- case EXEC_OUTPUT_SYSLOG:
- case EXEC_OUTPUT_SYSLOG_AND_CONSOLE:
- case EXEC_OUTPUT_KMSG:
- case EXEC_OUTPUT_KMSG_AND_CONSOLE:
- case EXEC_OUTPUT_JOURNAL:
- case EXEC_OUTPUT_JOURNAL_AND_CONSOLE:
- r = connect_logger_as(context, o, ident, unit_id, STDOUT_FILENO);
- if (r < 0) {
- log_error("Failed to connect stdout of %s to the journal socket: %s", unit_id, strerror(-r));
- r = open_null_as(O_WRONLY, STDOUT_FILENO);
- }
- return r;
-
- case EXEC_OUTPUT_SOCKET:
- assert(socket_fd >= 0);
- return dup2(socket_fd, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
-
- default:
- assert_not_reached("Unknown output type");
+ /* We need to open /dev/null here anew, to get the right access mode. */
+ return open_null_as(O_WRONLY, fileno);
}
-}
-static int setup_error(const ExecContext *context, int socket_fd, const char *ident, const char *unit_id, bool apply_tty_stdin) {
- ExecOutput o, e;
- ExecInput i;
- int r;
-
- assert(context);
- assert(ident);
-
- i = fixup_input(context->std_input, socket_fd, apply_tty_stdin);
- o = fixup_output(context->std_output, socket_fd);
- e = fixup_output(context->std_error, socket_fd);
-
- /* This expects the input and output are already set up */
-
- /* Don't change the stderr file descriptor if we inherit all
- * the way and are not on a tty */
- if (e == EXEC_OUTPUT_INHERIT &&
- o == EXEC_OUTPUT_INHERIT &&
- i == EXEC_INPUT_NULL &&
- !is_terminal_input(context->std_input) &&
- getppid () != 1)
- return STDERR_FILENO;
-
- /* Duplicate from stdout if possible */
- if (e == o || e == EXEC_OUTPUT_INHERIT)
- return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
-
- switch (e) {
+ switch (o) {
case EXEC_OUTPUT_NULL:
- return open_null_as(O_WRONLY, STDERR_FILENO);
+ return open_null_as(O_WRONLY, fileno);
case EXEC_OUTPUT_TTY:
if (is_terminal_input(i))
- return dup2(STDIN_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
+ return dup2(STDIN_FILENO, fileno) < 0 ? -errno : fileno;
/* We don't reset the terminal if this is just about output */
- return open_terminal_as(tty_path(context), O_WRONLY, STDERR_FILENO);
+ return open_terminal_as(tty_path(context), O_WRONLY, fileno);
case EXEC_OUTPUT_SYSLOG:
case EXEC_OUTPUT_SYSLOG_AND_CONSOLE:
@@ -429,16 +387,18 @@ static int setup_error(const ExecContext *context, int socket_fd, const char *id
case EXEC_OUTPUT_KMSG_AND_CONSOLE:
case EXEC_OUTPUT_JOURNAL:
case EXEC_OUTPUT_JOURNAL_AND_CONSOLE:
- r = connect_logger_as(context, e, ident, unit_id, STDERR_FILENO);
+ r = connect_logger_as(context, o, ident, unit_id, fileno);
if (r < 0) {
- log_error("Failed to connect stderr of %s to the journal socket: %s", unit_id, strerror(-r));
- r = open_null_as(O_WRONLY, STDERR_FILENO);
+ log_error("Failed to connect std%s of %s to the journal socket: %s",
+ fileno == STDOUT_FILENO ? "out" : "err",
+ unit_id, strerror(-r));
+ r = open_null_as(O_WRONLY, fileno);
}
return r;
case EXEC_OUTPUT_SOCKET:
assert(socket_fd >= 0);
- return dup2(socket_fd, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
+ return dup2(socket_fd, fileno) < 0 ? -errno : fileno;
default:
assert_not_reached("Unknown error type");
@@ -1179,13 +1139,13 @@ int exec_spawn(ExecCommand *command,
goto fail_child;
}
- err = setup_output(context, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
+ err = setup_output(context, STDOUT_FILENO, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
if (err < 0) {
r = EXIT_STDOUT;
goto fail_child;
}
- err = setup_error(context, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
+ err = setup_output(context, STDERR_FILENO, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
if (err < 0) {
r = EXIT_STDERR;
goto fail_child;
commit 47c1d80d844689c81faf2eede95803c1ed6eb4af
Author: Michal Schmidt <mschmidt at redhat.com>
Date: Fri Feb 15 22:43:23 2013 +0100
execute: robustness against journald failures
Almost every unit logs to the journal. If journald gets a permanent
failure, units would not be able to start (exit code 209/STDOUT).
Add a fallback to /dev/null to avoid making the system entirely
unusable in such a case.
diff --git a/src/core/execute.c b/src/core/execute.c
index 3376adc..b878c33 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -323,6 +323,7 @@ static int setup_input(const ExecContext *context, int socket_fd, bool apply_tty
static int setup_output(const ExecContext *context, int socket_fd, const char *ident, const char *unit_id, bool apply_tty_stdin) {
ExecOutput o;
ExecInput i;
+ int r;
assert(context);
assert(ident);
@@ -367,7 +368,12 @@ static int setup_output(const ExecContext *context, int socket_fd, const char *i
case EXEC_OUTPUT_KMSG_AND_CONSOLE:
case EXEC_OUTPUT_JOURNAL:
case EXEC_OUTPUT_JOURNAL_AND_CONSOLE:
- return connect_logger_as(context, o, ident, unit_id, STDOUT_FILENO);
+ r = connect_logger_as(context, o, ident, unit_id, STDOUT_FILENO);
+ if (r < 0) {
+ log_error("Failed to connect stdout of %s to the journal socket: %s", unit_id, strerror(-r));
+ r = open_null_as(O_WRONLY, STDOUT_FILENO);
+ }
+ return r;
case EXEC_OUTPUT_SOCKET:
assert(socket_fd >= 0);
@@ -381,6 +387,7 @@ static int setup_output(const ExecContext *context, int socket_fd, const char *i
static int setup_error(const ExecContext *context, int socket_fd, const char *ident, const char *unit_id, bool apply_tty_stdin) {
ExecOutput o, e;
ExecInput i;
+ int r;
assert(context);
assert(ident);
@@ -422,7 +429,12 @@ static int setup_error(const ExecContext *context, int socket_fd, const char *id
case EXEC_OUTPUT_KMSG_AND_CONSOLE:
case EXEC_OUTPUT_JOURNAL:
case EXEC_OUTPUT_JOURNAL_AND_CONSOLE:
- return connect_logger_as(context, e, ident, unit_id, STDERR_FILENO);
+ r = connect_logger_as(context, e, ident, unit_id, STDERR_FILENO);
+ if (r < 0) {
+ log_error("Failed to connect stderr of %s to the journal socket: %s", unit_id, strerror(-r));
+ r = open_null_as(O_WRONLY, STDERR_FILENO);
+ }
+ return r;
case EXEC_OUTPUT_SOCKET:
assert(socket_fd >= 0);
commit 4a01181e460686d8b4a543b1dfa7f77c9e3c5ab8
Author: Michal Schmidt <mschmidt at redhat.com>
Date: Fri Feb 15 22:41:19 2013 +0100
log: fix fallbacks to kmsg
write_to_journal()Â returns 0 if journal_fd is closed and nothing is
written. We need to make sure we'll try log_open_kmsg() then to make the
fallback work for "journal-or-kmsg".
diff --git a/src/shared/log.c b/src/shared/log.c
index 293c261..ff2dd45 100644
--- a/src/shared/log.c
+++ b/src/shared/log.c
@@ -541,11 +541,11 @@ static int log_dispatch(
k = write_to_journal(level, file, line, func,
object_name, object, buffer);
- if (k < 0) {
- if (k != -EAGAIN)
+ if (k <= 0) {
+ if (k < 0 && k != -EAGAIN)
log_close_journal();
log_open_kmsg();
- } else if (k > 0)
+ } else
r++;
}
@@ -554,11 +554,11 @@ static int log_dispatch(
k = write_to_syslog(level, file, line, func,
object_name, object, buffer);
- if (k < 0) {
- if (k != -EAGAIN)
+ if (k <= 0) {
+ if (k < 0 && k != -EAGAIN)
log_close_syslog();
log_open_kmsg();
- } else if (k > 0)
+ } else
r++;
}
@@ -571,10 +571,11 @@ static int log_dispatch(
k = write_to_kmsg(level, file, line, func,
object_name, object, buffer);
- if (k < 0) {
- log_close_kmsg();
+ if (k <= 0) {
+ if (k < 0 && k != -EAGAIN)
+ log_close_kmsg();
log_open_console();
- } else if (k > 0)
+ } else
r++;
}
More information about the systemd-commits
mailing list