summaryrefslogtreecommitdiff
path: root/process-util-log-more-information-when-runnin.patch
blob: 6e0c022cdf7f7c84f77415f17abe8303fc2d5bbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
From f4b4008495211c60bda7e1edda45beb36a553bc7 Mon Sep 17 00:00:00 2001
From: licunlong<licunlong1@huawei.com>
Date: Thu, 14 Jan 2021 15:57:59 +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 4e93c9b..78ad30b 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -54,6 +54,7 @@
 #include "stdio-util.h"
 #include "string-table.h"
 #include "string-util.h"
+#include "strv.h"
 #include "terminal-util.h"
 #include "user-util.h"
 #include "utf8.h"
@@ -342,6 +343,36 @@ int pidref_get_cmdline_strv(const PidRef *pid, ProcessCmdlineFlags flags, char *
         return 0;
 }
 
+int print_process_cmdline_with_arg(pid_t pid, int argc, char *argv[], const char * const *filter) {
+      bool is_filtered = false;
+      int r;
+      const char *arg_cmdline = "[";
+      _cleanup_free_ char *cmdline = NULL;
+
+      r = pid_get_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((char * const *) 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 container_get_leader(const char *machine, pid_t *pid) {
         _cleanup_free_ char *s = NULL, *class = NULL;
         const char *p;
diff --git a/src/basic/process-util.h b/src/basic/process-util.h
index 060c0c2..d211188 100644
--- a/src/basic/process-util.h
+++ b/src/basic/process-util.h
@@ -41,6 +41,7 @@ typedef enum ProcessCmdlineFlags {
 
 int pid_get_comm(pid_t pid, char **ret);
 int pidref_get_comm(const PidRef *pid, char **ret);
+int print_process_cmdline_with_arg(pid_t pid, int argc, char *argv[], const char * const *filter);
 int pid_get_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret);
 int pidref_get_cmdline(const PidRef *pid, size_t max_columns, ProcessCmdlineFlags flags, char **ret);
 int pid_get_cmdline_strv(pid_t pid, ProcessCmdlineFlags flags, char ***ret);
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index dd6f6c9..3b049c7 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -2,6 +2,7 @@
 
 #include <getopt.h>
 #include <locale.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 #include "sd-daemon.h"
@@ -1226,6 +1227,14 @@ static int run(int argc, char *argv[]) {
         _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
         _cleanup_(umount_and_freep) char *mounted_dir = NULL;
         int r;
+        pid_t ppid;
+        const char * const 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_setup();
@@ -1239,6 +1248,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 (proc_mounted() == 0)
                 log_full(arg_no_warn ? LOG_DEBUG : LOG_WARNING,
                          "%s%s/proc/ is not mounted. This is not a supported mode of operation. Please fix\n"
diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c
index 957e214..d9e9ab1 100644
--- a/src/test/test-process-util.c
+++ b/src/test/test-process-util.c
@@ -951,4 +951,26 @@ static int intro(void) {
         return EXIT_SUCCESS;
 }
 
+TEST(print_process_cmdline_with_arg) {
+        pid_t pid = getpid();
+        const char * const arg_filter_empty[] = {"", NULL};
+        const char * const arg_filter_1_in[] = {"status", NULL};
+        const char * const arg_filter_1_no[] = {"stop", NULL};
+        const char * const arg_filter_2_in[] = {"restart", "status", NULL};
+        const char * const arg_filter_2_no[] = {"restart", "stop", NULL};
+        const char *arg_var_1[1] = {"systemctl"};
+        const char *arg_var_10[10] = {"systemctl", "restart", "1", "2", "3", "4", "5", "6", "7", "8"};
+        const 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, (char **) arg_var_1, NULL) >= 0);
+        assert_se(print_process_cmdline_with_arg(pid, 10, (char **) arg_var_10, NULL) >= 0);
+        assert_se(print_process_cmdline_with_arg(897349, 1, (char **) arg_var_1, NULL) < 0);
+        assert_se(print_process_cmdline_with_arg(897349, 10, (char **) arg_var_10, NULL) < 0);
+        assert_se(print_process_cmdline_with_arg(pid, 3, (char **) arg_var_filter, arg_filter_empty) >= 0);
+        assert_se(print_process_cmdline_with_arg(pid, 3, (char **) arg_var_filter, arg_filter_1_in) >= 0);
+        assert_se(print_process_cmdline_with_arg(pid, 3, (char **) arg_var_filter, arg_filter_1_no) >= 0);
+        assert_se(print_process_cmdline_with_arg(pid, 3, (char **) arg_var_filter, arg_filter_2_in) >= 0);
+        assert_se(print_process_cmdline_with_arg(pid, 3, (char **) arg_var_filter, arg_filter_2_no) >= 0);
+}
+
 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
-- 
2.33.0