socket: if RemoveOnStop= is turned on for a socket, try to unlink() pre-existing symlinks

Normally, Symlinks= failing is not considered fatal nor destructive.
Let's slightly alter behaviour here if RemoveOnStop= is turned on. In
that case the use in a way opted for destructive behaviour and we do
unlink all sockets and symlinks when the socket unit goes down. And that
means we might as well unlink any pre-existing if this mode is selected.

Yeah, it's a bit of a stretch to do this, but @OhNoMoreGit is right: if
RemoveOnStop= is on we are destructive regarding any pre-existing
symlinks on stop, and it would be quite weird if we wouldn't be on
start.
This commit is contained in:
Lennart Poettering 2017-09-27 17:48:28 +02:00
parent 3ecc7fc3e6
commit 22b20752e2
1 changed files with 10 additions and 0 deletions

View File

@ -1337,6 +1337,16 @@ static int socket_symlink(Socket *s) {
(void) mkdir_parents_label(*i, s->directory_mode);
r = symlink_idempotent(p, *i);
if (r == -EEXIST && s->remove_on_stop) {
/* If there's already something where we want to create the symlink, and the destructive
* RemoveOnStop= mode is set, then we might as well try to remove what already exists and try
* again. */
if (unlink(*i) >= 0)
r = symlink_idempotent(p, *i);
}
if (r < 0)
log_unit_warning_errno(UNIT(s), r, "Failed to create symlink %s → %s, ignoring: %m", p, *i);
}