Debian Codegraph @

src/procinfo.cpp

raw symbols SCIP
1 /*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 */
4
5 #include "config.h"
6
7 #include "procinfo.h"
8
9 #include <map>
10 #include <string>
11
12 #include <glib.h>
13 #include <glib/gprintf.h>
14 #include <glibtop/procargs.h>
15 #include <glibtop/procstate.h>
16 #include <glibtop/proctime.h>
17 #include <pwd.h>
18 #include <sys/types.h>
19
20 #include "application.h"
21 #include "proctable.h"
22 #include "gsm-cgroups.h"
23 #include "gsm-selinux.h"
24
25 #ifdef HAVE_SYSTEMD
26 #include "gsm-systemd.h"
27 #endif
28
29
30 std::string
31 ProcInfo::lookup_user (guint uid)
32 {
33 static std::map<guint, std::string> users;
34 const auto [it, inserted] = users.try_emplace (uid, "");
35
36 // procman_debug("User lookup for uid %u: %s", uid, (inserted ? "MISS" : "HIT"));
37
38 if (inserted)
39 {
40 struct passwd*pwd;
41 pwd = getpwuid (uid);
42
43 if (pwd && pwd->pw_name)
44 {
45 it->second = pwd->pw_name;
46 }
47 else
48 {
49 char username[16];
50 g_sprintf (username, "%u", uid);
51 it->second = username;
52 }
53 }
54
55 return it->second;
56 }
57
58 void
59 ProcInfo::set_user (guint uid)
60 {
61 if (G_LIKELY (this->uid == uid))
62 return;
63
64 this->uid = uid;
65 this->user = lookup_user (uid);
66 }
67
68 static void
69 get_process_name (ProcInfo *info,
70 const gchar *cmd,
71 const GStrv args)
72 {
73 if (args)
74 {
75 // look for /usr/bin/very_long_name
76 // and also /usr/bin/interpreter /usr/.../very_long_name
77 // which may have use prctl to alter 'cmd' name
78 for (int i = 0; i != 2 && args[i]; ++i)
79 {
80 char*basename;
81 basename = g_path_get_basename (args[i]);
82
83 if (g_str_has_prefix (basename, cmd))
84 {
85 info->name = make_string (basename);
86 return;
87 }
88
89 g_free (basename);
90 }
91 }
92 info->name = cmd;
93 }
94
95 ProcInfo::ProcInfo(pid_t pid)
96 : node (),
97 icon (),
98 pid (pid),
99 ppid (-1),
100 uid (-1)
101 {
102 ProcInfo * const info = this;
103 glibtop_proc_state procstate;
104 glibtop_proc_time proctime;
105 glibtop_proc_args procargs;
106 gchar**arguments;
107
108 glibtop_get_proc_state (&procstate, pid);
109 glibtop_get_proc_time (&proctime, pid);
110 arguments = glibtop_get_proc_argv (&procargs, pid, 0);
111
112 /* FIXME : wrong. name and arguments may change with exec* */
113 get_process_name (info, procstate.cmd, static_cast<const GStrv>(arguments));
114
115 std::string tooltip = make_string (g_strjoinv (" ", arguments));
116
117 if (tooltip.empty ())
118 tooltip = procstate.cmd;
119
120 info->tooltip = make_string (g_markup_escape_text (tooltip.c_str (), -1));
121
122 info->arguments = make_string (g_strescape (tooltip.c_str (), "\\\""));
123 g_strfreev (arguments);
124
125 guint64 cpu_time = proctime.rtime;
126 auto it = GsmApplication::get ().processes.cpu_times.find (pid);
127
128 if (it != GsmApplication::get ().processes.cpu_times.end ())
129 if (proctime.rtime >= it->second)
130 cpu_time = it->second;
131 info->cpu_time = cpu_time;
132 info->start_time = proctime.start_time;
133
134 gsm_proc_info_load_cgroups (info);
135 gsm_proc_info_load_selinux (info);
136 gsm_proc_info_load_systemd (info);
137 }
138
139
140 void
141 ProcInfo::set_icon (Glib::RefPtr<Gdk::Texture> icon)
142 {
143 this->icon = icon;
144
145 GtkTreeModel *model;
146
147 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (
148 gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (
149 gtk_tree_view_get_model (GTK_TREE_VIEW (GsmApplication::get ().tree))))));
150 gtk_tree_store_set (GTK_TREE_STORE (model), &this->node,
151 COL_ICON, (this->icon ? this->icon->gobj () : NULL),
152 -1);
153 }
154
155
156 void
157 gsm_proc_info_load_cgroups (ProcInfo *self)
158 {
159 const char *cgroup = NULL;
160
161 g_return_if_fail (self);
162
163 cgroup = gsm_cgroups_get_name (self->pid);
164
165 self->cgroup_name = cgroup ? cgroup : "";
166 }
167
168
169 void
170 gsm_proc_info_load_selinux (ProcInfo *self)
171 {
172 g_autofree char *context = NULL;
173
174 g_return_if_fail (self);
175
176 context = gsm_selinux_get_context (self->pid);
177
178 self->security_context = make_string (g_steal_pointer (&context));
179 }
180
181
182 void
183 gsm_proc_info_load_systemd (ProcInfo *self)
184 {
185 #ifdef HAVE_SYSTEMD
186 g_autofree char *sd_unit = NULL;
187 g_autofree char *sd_session = NULL;
188 g_autofree char *sd_seat = NULL;
189 uid_t sd_owner;
190
191 g_return_if_fail (self);
192
193 if (gsm_systemd_get_process_info (self->pid,
194 &sd_unit,
195 &sd_session,
196 &sd_seat,
197 &sd_owner)) {
198 self->unit = make_string (g_steal_pointer (&sd_unit));
199 self->session = make_string (g_steal_pointer (&sd_session));
200 self->seat = make_string (g_steal_pointer (&sd_seat));
201 self->owner = self->lookup_user (sd_owner);
202 }
203 #else
204 g_return_if_fail (self);
205 #endif
206 }