sync and backport some patches

systemd-journald: Fix journal file descriptors leak problems.
systemd: Activation service must be restarted when it is already started and re-actived by dbus
systemd-core: fix problem of dbus service can not be started
systemd-core: Delay to restart when a service can not be auto-restarted when there is one STOP_JOB for the service
core: fix SIGABRT on empty exec command argv
journalctl: never fail at flushing when the flushed flag is set
timesync: fix wrong type for receiving timestamp in nanoseconds
udev: fix potential memleak
(cherry picked from commit d0907552a565ed01a4f9da4dd27168b3726f9236)
This commit is contained in:
xujing 2022-03-23 19:22:28 +08:00 committed by openeuler-sync-bot
parent 14847e851b
commit 826fd825d6
10 changed files with 447 additions and 1 deletions

View File

@ -0,0 +1,53 @@
From 4f8cec1924bf00532f5350d9a4d7af8e853241fe Mon Sep 17 00:00:00 2001
From: huangkaibin <huangkaibin@huawei.com>
Date: Thu, 28 Jun 2018 20:23:45 +0800
Subject: [PATCH] systemd-journald: Fix journal file descriptors leak problems.
Journal files opened and then be removed by external programs(for example, the journal rotation
of systemd-journald will removed jounal files) before journal directory notify watching is added
will not be closed properly. This patch fix this problem by removing and closing these deleted journal files
after notify watching is added.
---
src/libsystemd/sd-journal/sd-journal.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c
index 5728c53..1238652 100644
--- a/src/libsystemd/sd-journal/sd-journal.c
+++ b/src/libsystemd/sd-journal/sd-journal.c
@@ -1584,6 +1584,17 @@ fail:
log_debug_errno(errno, "Failed to enumerate directory %s, ignoring: %m", m->path);
}
+static void remove_nonexistent_journal_files(sd_journal *j) {
+ JournalFile *f = NULL;
+ ORDERED_HASHMAP_FOREACH(f, j->files) {
+ if(f->path && access(f->path, F_OK) < 0) {
+ log_debug("Remove not-existed file from the journal map: %s", f->path);
+ /*Its OK to remove entry from the hashmap although we are iterating on it.*/
+ remove_file_real(j, f);
+ }
+ }
+}
+
static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask) {
int r;
@@ -1612,6 +1623,14 @@ static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask)
(void) inotify_rm_watch(j->inotify_fd, m->wd);
m->wd = -1;
}
+
+ /*
+ * Before event watching, there were some files opened and if some of these opened files were
+ * deleted due to the journal rotation of systemd-jounald, they will become leaking files and will
+ * never be closed until the process exited.
+ * So here we remove these deleted files from the journal after event watching.
+ */
+ remove_nonexistent_journal_files(j);
}
static int add_directory(
--
2.27.0

View File

@ -0,0 +1,44 @@
From 4acc8a3168e5f11b5308cf8558d68bf2a0503444 Mon Sep 17 00:00:00 2001
From: huangkaibin <huangkaibin@huawei.com>
Date: Mon, 7 Aug 2017 17:06:30 +0800
Subject: [PATCH] systemd: Activation service must be restarted when it is already started and re-actived
by dbus
When dbus-daemon service is killed, every activation service must be restarted
to reestblished dbus connection between dbus-daemon and the service.
Otherwise, there will be problem on the dbus connection. This patch fix this
problem by set JobType to JOB_RESTART when it is re-actived in signal_activation_request function.
---
src/core/dbus.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/src/core/dbus.c b/src/core/dbus.c
index 29524d4..38940ef 100644
--- a/src/core/dbus.c
+++ b/src/core/dbus.c
@@ -152,6 +152,8 @@ static int signal_activation_request(sd_bus_message *message, void *userdata, sd
const char *name;
Unit *u;
int r;
+ int jobtype;
+ Service *s = NULL;
assert(message);
assert(m);
@@ -177,7 +179,13 @@ static int signal_activation_request(sd_bus_message *message, void *userdata, sd
goto failed;
}
- r = manager_add_job(m, JOB_START, u, JOB_REPLACE, NULL, &error, NULL);
+ jobtype = JOB_START;
+ s = SERVICE(u);
+ if(s && s->state != SERVICE_DEAD) {
+ jobtype = JOB_RESTART;
+ log_unit_info(u, "Service '%s' will be restarted to activate the service. The current service state is %d.", u->id, s->state);
+ }
+ r = manager_add_job(m, jobtype, u, JOB_REPLACE, NULL, &error, NULL);
if (r < 0)
goto failed;
--
1.8.3.1

View File

@ -0,0 +1,40 @@
From bf589755bd5b084f1b5dd099ea3e4917ac9911fd Mon Sep 17 00:00:00 2001
From: huangkaibin <huangkaibin@huawei.com>
Date: Thu, 14 Sep 2017 12:54:01 +0800
Subject: [PATCH] systemd-core: fix problem of dbus service can not be started
when dbus is dead and state of system dbus of systemd stay in
BUS_AUTHENTICATING.
When systemd starts a dbus communication, it will first authenticate the bus by communicating with polkitd service, and then enter running state.
But if authenticating can not be establised within 25s(default timeout seconds) since authenticating starts
(maybe caused by polkitd service or dbus service can not be activated in time), the dbus state in systemd side will stays in BUS_AUTHENTICATING state,
and systemd will enter a mad state that it will handle authenticating(in bus_process_internal function) very frequently and will have no any change to
service for events of restarting services(by systemctl restart dbus.service --no-ask-password --no-block). So that the dbus service will never be restarted successfully.
systemd will enter such a state is caused by the timeout setting in sd_bus_get_timeout function. When in BUS_AUTHENTICATING state, the timeout is set
to a fix value of bus->auth_timeout(authenticating start time + 25s), if auth_timeout is an expired time, but not a furture time, systemd will always service
for the callback of function of dbus(time_callback) with no any delay when it got its chance, and leave no chance for events of restarting services.
This patch fix this problem by fixing the timeout to a furture time when bus->auth_timeout is expired.
---
src/libsystemd/sd-bus/sd-bus.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
index b0a3237..ca626d3 100644
--- a/src/libsystemd/sd-bus/sd-bus.c
+++ b/src/libsystemd/sd-bus/sd-bus.c
@@ -2267,7 +2267,11 @@ _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
switch (bus->state) {
case BUS_AUTHENTICATING:
- *timeout_usec = bus->auth_timeout;
+ //delay 1 second to ensure it is a furture time but not an expired time
+ if(bus->auth_timeout <= now(CLOCK_MONOTONIC))
+ *timeout_usec = now(CLOCK_MONOTONIC) + USEC_PER_SEC;
+ else
+ *timeout_usec = bus->auth_timeout;
return 1;
case BUS_RUNNING:
--
1.8.3.1

View File

@ -0,0 +1,43 @@
From 9315c29e4fdfa19c90bb483a364b017881f5cef7 Mon Sep 17 00:00:00 2001
From: huangkaibin <huangkaibin@huawei.com>
Date: Sat, 21 Apr 2018 17:18:19 +0800
Subject: [PATCH] systemd-core: Delay to restart when a service can not be
auto-restarted when there is one STOP_JOB for the service
When a service current has a STOP job has not scheduled yet,
and also if the service is already scheduled with an auto-restart
with restart-second configured as 0, the service will not be restarted successfully,
and systemd will go into an endless loop to restart the service.
This is because restart-second is 0 and timer task has higher priority than IO tasks when there priority
is same(both with 0), so the STOP job has no chance to be scheduled, and systemd will go into the endless loop
to handle the time task.
This patch fix this problem by delaying 1 second to restart the service to cause STOP job to be scheduled.
---
src/core/service.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/core/service.c b/src/core/service.c
index ad9c028..8217447 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -1716,14 +1716,15 @@ fail:
static void service_enter_restart(Service *s) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
+ int restart_usec;
assert(s);
if (unit_has_job_type(UNIT(s), JOB_STOP)) {
/* Don't restart things if we are going down anyway */
log_unit_info(UNIT(s), "Stop job pending for unit, delaying automatic restart.");
-
- r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->restart_usec));
+ restart_usec = (s->restart_usec == 0) ? 1*USEC_PER_SEC : s->restart_usec;
+ r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), restart_usec));
if (r < 0)
goto fail;
--
1.8.3.1

View File

@ -0,0 +1,100 @@
From 29500cf8c47e6eb0518d171d62aa8213020c9152 Mon Sep 17 00:00:00 2001
From: Henri Chain <henri.chain@enioka.com>
Date: Tue, 5 Oct 2021 13:10:31 +0200
Subject: [PATCH 1/2] core: fix SIGABRT on empty exec command argv
This verifies that the argv part of any exec_command parameters that
are sent through dbus is not empty at deserialization time.
There is an additional check in service.c service_verify() that again
checks if all exec_commands are correctly populated, after the service
has been loaded, whether through dbus or otherwise.
Fixes #20933.
---
src/core/dbus-execute.c | 4 ++++
src/core/service.c | 10 ++++++++++
test/units/testsuite-23.sh | 31 +++++++++++++++++++++++++++++++
3 files changed, 45 insertions(+)
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
index 488de1242a..5665656b7b 100644
--- a/src/core/dbus-execute.c
+++ b/src/core/dbus-execute.c
@@ -1423,6 +1423,10 @@ int bus_set_transient_exec_command(
if (r < 0)
return r;
+ if (strv_isempty(argv))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
+ "\"%s\" argv cannot be empty", name);
+
r = is_ex_prop ? sd_bus_message_read_strv(message, &ex_opts) : sd_bus_message_read(message, "b", &b);
if (r < 0)
return r;
diff --git a/src/core/service.c b/src/core/service.c
index 9299813d45..4c75819a8f 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -564,6 +564,16 @@ static int service_verify(Service *s) {
assert(s);
assert(UNIT(s)->load_state == UNIT_LOADED);
+ for (ServiceExecCommand c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
+ ExecCommand *command;
+
+ LIST_FOREACH(command, command, s->exec_command[c])
+ if (strv_isempty(command->argv))
+ return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC),
+ "Service has an empty argv in %s=. Refusing.",
+ service_exec_command_to_string(c));
+ }
+
if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP] &&
UNIT(s)->success_action == EMERGENCY_ACTION_NONE)
/* FailureAction= only makes sense if one of the start or stop commands is specified.
diff --git a/test/units/testsuite-23.sh b/test/units/testsuite-23.sh
index 4ef7c878a8..5488447a87 100755
--- a/test/units/testsuite-23.sh
+++ b/test/units/testsuite-23.sh
@@ -27,6 +27,37 @@ test "$(systemctl show --value -p RestartKillSignal seven.service)" -eq 2
systemctl restart seven.service
systemctl stop seven.service
+# For issue #20933
+
+# Should work normally
+busctl call \
+ org.freedesktop.systemd1 /org/freedesktop/systemd1 \
+ org.freedesktop.systemd1.Manager StartTransientUnit \
+ "ssa(sv)a(sa(sv))" test-20933-ok.service replace 1 \
+ ExecStart "a(sasb)" 1 \
+ /usr/bin/sleep 2 /usr/bin/sleep 1 true \
+ 0
+
+# DBus call should fail but not crash systemd
+busctl call \
+ org.freedesktop.systemd1 /org/freedesktop/systemd1 \
+ org.freedesktop.systemd1.Manager StartTransientUnit \
+ "ssa(sv)a(sa(sv))" test-20933-bad.service replace 1 \
+ ExecStart "a(sasb)" 1 \
+ /usr/bin/sleep 0 true \
+ 0 && { echo 'unexpected success'; exit 1; }
+
+# Same but with the empty argv in the middle
+busctl call \
+ org.freedesktop.systemd1 /org/freedesktop/systemd1 \
+ org.freedesktop.systemd1.Manager StartTransientUnit \
+ "ssa(sv)a(sa(sv))" test-20933-bad-middle.service replace 1 \
+ ExecStart "a(sasb)" 3 \
+ /usr/bin/sleep 2 /usr/bin/sleep 1 true \
+ /usr/bin/sleep 0 true \
+ /usr/bin/sleep 2 /usr/bin/sleep 1 true \
+ 0 && { echo 'unexpected success'; exit 1; }
+
systemd-analyze log-level info
echo OK >/testok
--
2.27.0

View File

@ -0,0 +1,35 @@
From 8688a389cabdff61efe187bb85cc1776de03c460 Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Wed, 6 Oct 2021 00:19:41 +0900
Subject: [PATCH 2/2] core/service: also check path in exec commands
---
src/core/service.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/src/core/service.c b/src/core/service.c
index 4c75819a8f..54d8d0c760 100644
--- a/src/core/service.c
+++ b/src/core/service.c
@@ -567,11 +567,17 @@ static int service_verify(Service *s) {
for (ServiceExecCommand c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
ExecCommand *command;
- LIST_FOREACH(command, command, s->exec_command[c])
+ LIST_FOREACH(command, command, s->exec_command[c]) {
+ if (!path_is_absolute(command->path) && !filename_is_valid(command->path))
+ return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC),
+ "Service %s= binary path \"%s\" is neither a valid executable name nor an absolute path. Refusing.",
+ command->path,
+ service_exec_command_to_string(c));
if (strv_isempty(command->argv))
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC),
"Service has an empty argv in %s=. Refusing.",
service_exec_command_to_string(c));
+ }
}
if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP] &&
--
2.27.0

View File

@ -0,0 +1,39 @@
From f6fca35e642a112e80cc9bddb9a2b4805ad40df2 Mon Sep 17 00:00:00 2001
From: Franck Bui <fbui@suse.com>
Date: Wed, 4 Aug 2021 11:20:07 +0200
Subject: [PATCH] journalctl: never fail at flushing when the flushed flag is
set
Even if journald was not running, flushing the volatile journal used to work if
the journal was already flushed (ie the flushed flag
/run/systemd/journald/flushed was created).
However since commit 4f413af2a0a, this behavior changed and now '--flush' fails
because it tries to contact journald without checking the presence of the
flushed flag anymore.
This patch restores the previous behavior since there's no reason to fail when
journalctl can figure out that the flush is not necessary.
---
src/journal/journalctl.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 4a2343a63d..73e4fafdff 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -2064,6 +2064,11 @@ static int simple_varlink_call(const char *option, const char *method) {
}
static int flush_to_var(void) {
+ if (access("/run/systemd/journal/flushed", F_OK) >= 0)
+ return 0; /* Already flushed, no need to contact journald */
+ if (errno != ENOENT)
+ return log_error_errno(errno, "Unable to check for existence of /run/systemd/journal/flushed: %m");
+
return simple_varlink_call("--flush", "io.systemd.Journal.FlushToVar");
}
--
2.27.0

View File

@ -0,0 +1,40 @@
From 6f96bdc58746b1698bf8b3430a6c638f8949daec Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Fri, 20 Aug 2021 08:40:11 +0900
Subject: [PATCH] timesync: fix wrong type for receiving timestamp in
nanoseconds
Fixes #20482.
---
src/test/test-sizeof.c | 2 ++
src/timesync/timesyncd-manager.c | 2 +-
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/src/test/test-sizeof.c b/src/test/test-sizeof.c
index 3c9dc180fa..e36bee4e8f 100644
--- a/src/test/test-sizeof.c
+++ b/src/test/test-sizeof.c
@@ -89,5 +89,7 @@ int main(void) {
printf("big_enum2_pos → %zu\n", sizeof(big_enum2_pos));
printf("big_enum2_neg → %zu\n", sizeof(big_enum2_neg));
+ printf("timeval: %zu\n", sizeof(struct timeval));
+ printf("timespec: %zu\n", sizeof(struct timespec));
return 0;
}
diff --git a/src/timesync/timesyncd-manager.c b/src/timesync/timesyncd-manager.c
index 1c284f31e3..3a89d9b1fa 100644
--- a/src/timesync/timesyncd-manager.c
+++ b/src/timesync/timesyncd-manager.c
@@ -416,7 +416,7 @@ static int manager_receive_response(sd_event_source *source, int fd, uint32_t re
.iov_base = &ntpmsg,
.iov_len = sizeof(ntpmsg),
};
- CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct timeval))) control;
+ CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct timespec))) control;
union sockaddr_union server_addr;
struct msghdr msghdr = {
.msg_iov = &iov,
--
2.27.0

View File

@ -0,0 +1,33 @@
From 4154524d47d24bcee3ebfed939912a847ebeb1b3 Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Fri, 27 Aug 2021 17:27:26 +0900
Subject: [PATCH] udev: fix potential memleak
---
src/udev/udev-builtin-net_id.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c
index 0aede28f7d..9578fa00c3 100644
--- a/src/udev/udev-builtin-net_id.c
+++ b/src/udev/udev-builtin-net_id.c
@@ -101,7 +101,6 @@ static int get_virtfn_info(sd_device *dev, struct netnames *names, struct virtfn
_cleanup_(sd_device_unrefp) sd_device *physfn_pcidev = NULL;
const char *physfn_link_file, *syspath;
_cleanup_free_ char *physfn_pci_syspath = NULL;
- _cleanup_free_ char *virtfn_pci_syspath = NULL;
struct dirent *dent;
_cleanup_closedir_ DIR *dir = NULL;
char suffix[ALTIFNAMSIZ];
@@ -132,7 +131,7 @@ static int get_virtfn_info(sd_device *dev, struct netnames *names, struct virtfn
return -errno;
FOREACH_DIRENT_ALL(dent, dir, break) {
- _cleanup_free_ char *virtfn_link_file = NULL;
+ _cleanup_free_ char *virtfn_link_file = NULL, *virtfn_pci_syspath = NULL;
if (!startswith(dent->d_name, "virtfn"))
continue;
--
2.27.0

View File

@ -20,7 +20,7 @@
Name: systemd Name: systemd
Url: https://www.freedesktop.org/wiki/Software/systemd Url: https://www.freedesktop.org/wiki/Software/systemd
Version: 249 Version: 249
Release: 15 Release: 16
License: MIT and LGPLv2+ and GPLv2+ License: MIT and LGPLv2+ and GPLv2+
Summary: System and Service Manager Summary: System and Service Manager
@ -65,6 +65,10 @@ Patch0016: 0016-fix-capsh-drop-but-ping-success.patch
Patch0017: 0017-resolved-create-etc-resolv.conf-symlink-at-runtime.patch Patch0017: 0017-resolved-create-etc-resolv.conf-symlink-at-runtime.patch
Patch0018: 0018-nop_job-of-a-unit-must-also-be-coldpluged-after-deserization.patch Patch0018: 0018-nop_job-of-a-unit-must-also-be-coldpluged-after-deserization.patch
Patch0019: 0019-pid1-bump-DefaultTasksMax-to-80-of-the-kernel-pid.ma.patch Patch0019: 0019-pid1-bump-DefaultTasksMax-to-80-of-the-kernel-pid.ma.patch
Patch0020: 0020-fix-journal-file-descriptors-leak-problems.patch
Patch0021: 0021-activation-service-must-be-restarted-when-reactivated.patch
Patch0022: 0022-systemd-core-fix-problem-of-dbus-service-can-not-be-started.patch
Patch0023: 0023-delay-to-restart-when-a-service-can-not-be-auto-restarted.patch
#backport #backport
Patch6000: backport-core-fix-free-undefined-pointer-when-strdup-failed-i.patch Patch6000: backport-core-fix-free-undefined-pointer-when-strdup-failed-i.patch
@ -87,6 +91,11 @@ Patch6016: backport-Bump-the-max-number-of-inodes-for-tmp-to-a-million-t.pa
Patch6017: backport-unit-escape.patch Patch6017: backport-unit-escape.patch
Patch6018: backport-udev-rename-type-name-e.g.-struct-worker-Worker.patch Patch6018: backport-udev-rename-type-name-e.g.-struct-worker-Worker.patch
Patch6019: backport-udev-run-the-main-process-workers-and-spawned-comman.patch Patch6019: backport-udev-run-the-main-process-workers-and-spawned-comman.patch
Patch6020: backport-timesync-fix-wrong-type-for-receiving-timestamp-in-n.patch
Patch6021: backport-udev-fix-potential-memleak.patch
Patch6022: backport-journalctl-never-fail-at-flushing-when-the-flushed-f.patch
Patch6023: backport-core-fix-SIGABRT-on-empty-exec-command-argv.patch
Patch6024: backport-core-service-also-check-path-in-exec-commands.patch
BuildRequires: gcc, gcc-c++ BuildRequires: gcc, gcc-c++
BuildRequires: libcap-devel, libmount-devel, pam-devel, libselinux-devel BuildRequires: libcap-devel, libmount-devel, pam-devel, libselinux-devel
@ -1488,6 +1497,16 @@ fi
%{_libdir}/security/pam_systemd.so %{_libdir}/security/pam_systemd.so
%changelog %changelog
* Wed Mar 23 2022 xujing <xujing99@huawei.com> - 249-16
- systemd-journald: Fix journal file descriptors leak problems.
systemd: Activation service must be restarted when it is already started and re-actived by dbus
systemd-core: fix problem of dbus service can not be started
systemd-core: Delay to restart when a service can not be auto-restarted when there is one STOP_JOB for the service
core: fix SIGABRT on empty exec command argv
journalctl: never fail at flushing when the flushed flag is set
timesync: fix wrong type for receiving timestamp in nanoseconds
udev: fix potential memleak
* Fri Mar 18 2022 yangmingtai <yangmingtai@huawei.com> - 249-15 * Fri Mar 18 2022 yangmingtai <yangmingtai@huawei.com> - 249-15
- fix systemctl reload systemd-udevd failed - fix systemctl reload systemd-udevd failed