Debian Codegraph @

debian/patches/w_process_tty

raw
1 Description: w: Use process TTY as backup user TTY
2 Sometimes we cannot directly find the TTY for a user session, so
3 this commit puts all the methods into a single function called
4 get_session_tty() which will (in order) use:
5 * systemd's sd_session_get_tty
6 * utmp->ut_line
7 then (and this is new):
8 * Get the session leader of the session, either via
9 sd_session_get_leader() or ut->ut_pid and scan the pids
10 stack for children and grand-children until we find something
11 with a TTY.
12 .
13 w now carries around the ttyname (pts/2) not the ttypath (/dev/pts/2)
14 which removed a lot of the ttyname+5 stuff.
15 .
16 The process cache is sorted by PIDS_TICS_BEGAN for all modes, not
17 just tty mode because we will miss {grand,}children if there has been
18 a pid wrap.
19 .
20 Bonus benefit is the "best" process is better now.
21 Author: Craig Small <[email protected]>
22 Origin: upstream, https://gitlab.com/procps-ng/procps/-/commit/41c12e27122319229633b515c8f99cf6b73ca8d8
23 Bug-Debian: https://bugs.debian.org/1080335
24 Applied-Upstream: 4.0.6
25 Last-Update: 2025-04-14
26 ---
27 This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
28 --- a/src/w.c
29 +++ b/src/w.c
30 @@ -331,10 +331,17 @@
31 /* stat the device file to get an idle time */
32 static time_t idletime(const char *restrict const tty)
33 {
34 - struct stat sbuf;
35 - if (stat(tty, &sbuf) != 0)
36 - return 0;
37 - return time(NULL) - sbuf.st_atime;
38 + char *ttypath=NULL;
39 + struct stat sbuf;
40 +
41 + if (asprintf(&ttypath, "/dev/%s", tty) < 0)
42 + return 0;
43 + if (stat(ttypath, &sbuf) != 0) {
44 + free(ttypath);
45 + return 0;
46 + }
47 + free(ttypath);
48 + return time(NULL) - sbuf.st_atime;
49 }
50
51 /* 7 character formatted login time */
52 @@ -526,6 +533,86 @@
53 }
54
55
56 +/*
57 + * Try to get the TTY of the session using various means
58 + */
59 +#define PIDS_GETINT(e) PIDS_VAL(EU_ ## e, s_int, reap->stacks[i], NULL)
60 +#define PIDS_GETSTR(e) PIDS_VAL(EU_ ## e, str, reap->stacks[i], NULL)
61 +#define PIDS_GETINT2(e) PIDS_VAL(EU_ ## e, s_int, reap->stacks[j], NULL)
62 +#define PIDS_GETSTR2(e) PIDS_VAL(EU_ ## e, str, reap->stacks[j], NULL)
63 +static void get_session_tty(
64 + char *tty,
65 + const char *session,
66 + utmp_t *u,
67 + struct pids_fetch *reap)
68 +{
69 + int i, j, total_procs;
70 + pid_t leader_pid=-1;
71 +
72 + /* First method - use systemd */
73 +#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER)
74 + if (session) {
75 + char *sd_tty;
76 +
77 + if (sd_session_get_tty(session, &sd_tty) >= 0) {
78 + for (i = 0; i < UT_LINESIZE; i++) {
79 + if (sd_tty[i] == '\0') break;
80 + /* clean up tty if garbled */
81 + if (isalnum(sd_tty[i]) || (sd_tty[i] == '/'))
82 + tty[i] = sd_tty[i];
83 + else
84 + tty[i] = '\0';
85 + }
86 + free(sd_tty);
87 + return; /* found tty via systemd */
88 + }
89 + sd_session_get_leader(session, &leader_pid);
90 + }
91 +#endif
92 + /* Second method - use utmp */
93 + if (u) {
94 + for (i = 0; i < UT_LINESIZE; i++) {
95 + if (tty[i] == 0)
96 + break;
97 + /* clean up tty if garbled */
98 + if (isalnum(u->ut_line[i]) || (u->ut_line[i] == '/'))
99 + tty[i] = u->ut_line[i];
100 + }
101 + tty[i] = '\0';
102 + if (tty[0] != '\0')
103 + return; /* found via utmp */
104 + if (leader_pid == -1)
105 + leader_pid = u->ut_pid;
106 + }
107 +
108 + /* Third method - scan processes to find the tty, this is at most two down
109 + * from the session leader */
110 + if (leader_pid == -1)
111 + return;
112 +
113 + total_procs = reap->counts->total;
114 + for (i=0; i < total_procs; i++) {
115 + if (PIDS_GETINT(PPID) != leader_pid)
116 + continue;
117 + if (PIDS_GETINT(TTY) != 0) {
118 + strncpy(tty, PIDS_GETSTR(TTY_NAME), UT_NAMESIZE);
119 + return; /* found via top scan */
120 + }
121 + for (j=i; j < total_procs; j++) {
122 + if (PIDS_GETINT2(PPID) != PIDS_GETINT(PID))
123 + continue;
124 + if (PIDS_GETINT2(TTY) != 0) {
125 + strncpy(tty, PIDS_GETSTR2(TTY_NAME), UT_NAMESIZE);
126 + return; /* found via second scan */
127 + }
128 + }
129 + }
130 +}
131 +#undef PIDS_GETINT
132 +#undef PIDS_GETSTR
133 +#undef PIDS_GETINT2
134 +#undef PIDS_GETSTR2
135 +
136 static void showinfo(
137 const char *session, const char *name,
138 utmp_t * u, const int longform, int maxcmd, int from,
139 @@ -536,7 +623,7 @@
140 {
141 unsigned long long jcpu, pcpu;
142 unsigned i;
143 - char uname[UT_NAMESIZE + 1] = "", tty[5 + UT_LINESIZE + 1] = "/dev/";
144 + char uname[UT_NAMESIZE + 1] = "", tty[UT_LINESIZE + 1] = "";
145 long hertz;
146 char cmdline[MAX_CMD_WIDTH + 1];
147 pid_t best_pid = -1;
148 @@ -546,39 +633,13 @@
149
150 hertz = procps_hertz_get();
151
152 -#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER)
153 - if (session) {
154 - char *sd_tty;
155 -
156 - if (sd_session_get_tty(session, &sd_tty) >= 0) {
157 - for (i = 0; i < UT_LINESIZE; i++) {
158 - if (sd_tty[i] == '\0') break;
159 -
160 - /* clean up tty if garbled */
161 - if (isalnum(sd_tty[i]) || (sd_tty[i] == '/'))
162 - tty[i + 5] = sd_tty[i];
163 - else
164 - tty[i + 5] = '\0';
165 - }
166 - free(sd_tty);
167 - }
168 - } else {
169 -#endif
170 - for (i = 0; i < UT_LINESIZE; i++)
171 - /* clean up tty if garbled */
172 - if (isalnum(u->ut_line[i]) || (u->ut_line[i] == '/'))
173 - tty[i + 5] = u->ut_line[i];
174 - else
175 - tty[i + 5] = '\0';
176 -#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER)
177 - }
178 -#endif
179 + get_session_tty(tty, session, u, reap);
180
181 if (find_best_proc(
182 #if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER)
183 session,
184 #endif
185 - u, tty + 5, &jcpu, &pcpu, cmdline, &best_pid, reap) == 0)
186 + u, tty, &jcpu, &pcpu, cmdline, &best_pid, reap) == 0)
187 /*
188 * just skip if stale utmp entry (i.e. login proc doesn't
189 * exist). If there is a desire a cmdline flag could be
190 @@ -592,7 +653,7 @@
191 /* force NUL term for printf */
192 uname[UT_NAMESIZE] = '\0';
193
194 - printf("%-*.*s%-9.8s", userlen + 1, userlen, uname, tty + 5);
195 + printf("%-*.*s%-9.8s", userlen + 1, userlen, uname, tty);
196 if (from)
197 print_from(session, u, ip_addresses, fromlen);
198
199 @@ -620,7 +681,7 @@
200 if (u && *u->ut_line == ':')
201 /* idle unknown for xdm logins */
202 printf(" ?xdm? ");
203 - else if (tty[5])
204 + else if (tty[0])
205 print_time_ival7(idletime(tty), 0, stdout);
206 else
207 printf(" ");
208 @@ -789,7 +850,7 @@
209 }
210 #endif
211 }
212 - print_time_ival7(idletime(ttypath), 0, stdout);
213 + print_time_ival7(idletime(ttyname), 0, stdout);
214 /* jpcpu/pcpu */
215 if (longform) {
216 print_time_ival7(jcpu / hertz, (jcpu % hertz) * (100. / hertz), stdout);
217 @@ -854,12 +915,8 @@
218
219 if (!procps_pids_sort(pids_info,
220 reap->stacks, total_procs,
221 - PIDS_TICS_BEGAN, PIDS_SORT_ASCEND))
222 - xerrx(EXIT_FAILURE, _("Unable to sort pids"));
223 - if (!procps_pids_sort(pids_info,
224 - reap->stacks, total_procs,
225 PIDS_TTY, PIDS_SORT_ASCEND))
226 - xerrx(EXIT_FAILURE, _("Unable to sort pids"));
227 + xerrx(EXIT_FAILURE, _("Unable to sort processes by TTY"));
228
229 for (i=0; i < total_procs; i++) {
230 /* Skip if:
231 @@ -1035,6 +1092,11 @@
232 return(EXIT_FAILURE);
233 }
234
235 + if (!procps_pids_sort(info,
236 + pids_cache->stacks, pids_cache->counts->total,
237 + PIDS_TICS_BEGAN, PIDS_SORT_ASCEND))
238 + xerrx(EXIT_FAILURE, _("Unable to sort processes by PID"));
239 +
240 if (header) {
241 /* print uptime and headers */
242 printf("%s\n", procps_uptime_sprint());