add patch to log message when run systemctl
This commit is contained in:
parent
8cd2917618
commit
04a0c174c3
155
process-util-log-more-information-when-runnin.patch
Normal file
155
process-util-log-more-information-when-runnin.patch
Normal file
@ -0,0 +1,155 @@
|
||||
From f5747a70602fa145988a1c4047fe5bd49ebacace Mon Sep 17 00:00:00 2001
|
||||
From: licunlong <licunlong1@huawei.com>
|
||||
Date: Tue, 24 Dec 2024 15:44:36 +0800
|
||||
Subject: [PATCH] process-util: log more information when running systemctl.
|
||||
|
||||
Print the PID and its cmdline to the system log when a process
|
||||
runs systemctl command.
|
||||
---
|
||||
src/basic/process-util.c | 31 +++++++++++++++++++++++++++++++
|
||||
src/basic/process-util.h | 1 +
|
||||
src/systemctl/systemctl.c | 12 ++++++++++++
|
||||
src/test/test-process-util.c | 22 ++++++++++++++++++++++
|
||||
4 files changed, 66 insertions(+)
|
||||
|
||||
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
|
||||
index 9e1f1df..c77f509 100644
|
||||
--- a/src/basic/process-util.c
|
||||
+++ b/src/basic/process-util.c
|
||||
@@ -42,6 +42,7 @@
|
||||
#include "stat-util.h"
|
||||
#include "string-table.h"
|
||||
#include "string-util.h"
|
||||
+#include "strv.h"
|
||||
#include "terminal-util.h"
|
||||
#include "user-util.h"
|
||||
#include "utf8.h"
|
||||
@@ -189,6 +190,36 @@ int get_process_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags
|
||||
return 0;
|
||||
}
|
||||
|
||||
+int print_process_cmdline_with_arg(pid_t pid, int argc, char *argv[], char *filter[]) {
|
||||
+ bool is_filtered = false;
|
||||
+ int r;
|
||||
+ const char *arg_cmdline = "[";
|
||||
+ _cleanup_free_ char *cmdline = NULL;
|
||||
+
|
||||
+ r = get_process_cmdline(pid, SIZE_MAX, 0, &cmdline);
|
||||
+ if (r < 0) {
|
||||
+ syslog(LOG_INFO, "Failed to get cmdline of PID %d. Ignoring.", pid);
|
||||
+ return r;
|
||||
+ } else {
|
||||
+ for (int i = 0; i < argc; i++ ) {
|
||||
+ if (filter && strv_find(filter, argv[i])) {
|
||||
+ is_filtered = true;
|
||||
+ break;
|
||||
+ }
|
||||
+ if (i == 0) {
|
||||
+ arg_cmdline = strjoina(arg_cmdline, argv[i]);
|
||||
+ } else {
|
||||
+ arg_cmdline = strjoina(arg_cmdline, " ", argv[i]);
|
||||
+ }
|
||||
+ }
|
||||
+ if (!is_filtered) {
|
||||
+ syslog(LOG_INFO, "%s] called by PID %d (%s)", arg_cmdline, pid, cmdline);
|
||||
+ }
|
||||
+ return 0;
|
||||
+ }
|
||||
+
|
||||
+}
|
||||
+
|
||||
int rename_process(const char name[]) {
|
||||
static size_t mm_size = 0;
|
||||
static char *mm = NULL;
|
||||
diff --git a/src/basic/process-util.h b/src/basic/process-util.h
|
||||
index 41d4759..4d8147e 100644
|
||||
--- a/src/basic/process-util.h
|
||||
+++ b/src/basic/process-util.h
|
||||
@@ -38,6 +38,7 @@ typedef enum ProcessCmdlineFlags {
|
||||
|
||||
int get_process_comm(pid_t pid, char **name);
|
||||
int get_process_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags, char **line);
|
||||
+int print_process_cmdline_with_arg(pid_t pid, int argc, char *argv[], char *filter[]);
|
||||
int get_process_exe(pid_t pid, char **name);
|
||||
int get_process_uid(pid_t pid, uid_t *uid);
|
||||
int get_process_gid(pid_t pid, gid_t *gid);
|
||||
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
|
||||
index 1c01914..edba8e0 100644
|
||||
--- a/src/systemctl/systemctl.c
|
||||
+++ b/src/systemctl/systemctl.c
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
#include <locale.h>
|
||||
+#include <sys/types.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
@@ -9272,6 +9273,14 @@ static int logind_cancel_shutdown(void) {
|
||||
|
||||
static int run(int argc, char *argv[]) {
|
||||
int r;
|
||||
+ pid_t ppid;
|
||||
+ char *filter[] = {
|
||||
+ "status", "show", "cat",
|
||||
+ "is-active", "is-failed", "is-enabled", "is-system-running",
|
||||
+ "list-units", "list-sockets", "list-timers", "list-dependencies",
|
||||
+ "list-unit-files", "list-machines", "list-jobs",
|
||||
+ "get-default", "show-environment", NULL
|
||||
+ };
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
log_parse_environment();
|
||||
@@ -9291,6 +9300,9 @@ static int run(int argc, char *argv[]) {
|
||||
if (r <= 0)
|
||||
goto finish;
|
||||
|
||||
+ ppid = getppid();
|
||||
+ (void) print_process_cmdline_with_arg(ppid, argc, argv, filter);
|
||||
+
|
||||
if (arg_action != ACTION_SYSTEMCTL && running_in_chroot() > 0) {
|
||||
if (!arg_quiet)
|
||||
log_info("Running in chroot, ignoring request.");
|
||||
diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c
|
||||
index 8dc9fdd..1cb4ee2 100644
|
||||
--- a/src/test/test-process-util.c
|
||||
+++ b/src/test/test-process-util.c
|
||||
@@ -601,6 +601,27 @@ static void test_ioprio_class_from_to_string(void) {
|
||||
test_ioprio_class_from_to_string_one("-1", -1);
|
||||
}
|
||||
|
||||
+static void test_print_process_cmdline_with_arg(pid_t pid) {
|
||||
+ char *arg_filter_empty[] = {"", NULL};
|
||||
+ char *arg_filter_1_in[] = {"status", NULL};
|
||||
+ char *arg_filter_1_no[] = {"stop", NULL};
|
||||
+ char *arg_filter_2_in[] = {"restart", "status", NULL};
|
||||
+ char *arg_filter_2_no[] = {"restart", "stop", NULL};
|
||||
+ char *arg_var_1[1] = {"systemctl"};
|
||||
+ char *arg_var_10[10] = {"systemctl", "restart", "1", "2", "3", "4", "5", "6", "7", "8"};
|
||||
+ char *arg_var_filter[3] = {"systemctl", "status", "dbus.service"};
|
||||
+ assert_se(print_process_cmdline_with_arg(pid, 0, NULL, NULL) >=0);
|
||||
+ assert_se(print_process_cmdline_with_arg(pid, 1, arg_var_1, NULL) >= 0);
|
||||
+ assert_se(print_process_cmdline_with_arg(pid, 10, arg_var_10, NULL) >= 0);
|
||||
+ assert_se(print_process_cmdline_with_arg(897349, 1, arg_var_1, NULL) < 0);
|
||||
+ assert_se(print_process_cmdline_with_arg(897349, 10, arg_var_10, NULL) < 0);
|
||||
+ assert_se(print_process_cmdline_with_arg(pid, 3, arg_var_filter, arg_filter_empty) >= 0);
|
||||
+ assert_se(print_process_cmdline_with_arg(pid, 3, arg_var_filter, arg_filter_1_in) >= 0);
|
||||
+ assert_se(print_process_cmdline_with_arg(pid, 3, arg_var_filter, arg_filter_1_no) >= 0);
|
||||
+ assert_se(print_process_cmdline_with_arg(pid, 3, arg_var_filter, arg_filter_2_in) >= 0);
|
||||
+ assert_se(print_process_cmdline_with_arg(pid, 3, arg_var_filter, arg_filter_2_no) >= 0);
|
||||
+}
|
||||
+
|
||||
int main(int argc, char *argv[]) {
|
||||
test_setup_logging(LOG_DEBUG);
|
||||
|
||||
@@ -627,6 +648,7 @@ int main(int argc, char *argv[]) {
|
||||
test_safe_fork();
|
||||
test_pid_to_ptr();
|
||||
test_ioprio_class_from_to_string();
|
||||
+ test_print_process_cmdline_with_arg(getpid());
|
||||
|
||||
return 0;
|
||||
}
|
||||
--
|
||||
2.27.0
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
Name: systemd
|
||||
Url: https://systemd.io/
|
||||
Version: 243
|
||||
Release: 82
|
||||
Release: 83
|
||||
License: MIT and LGPLv2+ and GPLv2+
|
||||
Summary: System and Service Manager
|
||||
|
||||
@ -327,6 +327,7 @@ Patch9010: fix-capsh-drop-but-ping-success.patch
|
||||
Patch9011: 0998-resolved-create-etc-resolv.conf-symlink-at-runtime.patch
|
||||
Patch9012: set-kernel-core_pipe_limit-to-16.patch
|
||||
Patch9013: disable-systemd-timesyncd-networkd-resolved-by-defau.patch
|
||||
Patch9014: process-util-log-more-information-when-runnin.patch
|
||||
|
||||
BuildRequires: gcc, gcc-c++
|
||||
BuildRequires: libcap-devel, libmount-devel, pam-devel, libselinux-devel
|
||||
@ -1718,6 +1719,10 @@ fi
|
||||
%exclude /usr/share/man/man3/*
|
||||
|
||||
%changelog
|
||||
* Thu Jan 2 2025 Han Jinpeng <hanjinpeng@kylinos.cn> - 243-83
|
||||
- Enhance the logging function of the systemctl command
|
||||
Add process-util-log-more-information-when-runnin.patch
|
||||
|
||||
* Tue Nov 5 2024 zhangyao <zhangyao108@huawei.com> - 243-82
|
||||
- sync community patches
|
||||
add backport-pid1-set-SYSTEMD_NSS_DYNAMIC_BYPASS-1-env-var-for-db.patch
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user