| 1 |
Description: w: Add terminal mode |
| 2 |
Additional option for w -t/--terminal |
| 3 |
. |
| 4 |
Instead of running through either the systemd sessions or utmp |
| 5 |
entries this option sorts the proccesses by TTY and finds ones |
| 6 |
with a valid TTY entry. |
| 7 |
. |
| 8 |
getty processes are assumed to have a PPID or 0 or 1, to be |
| 9 |
excluded from user processes. |
| 10 |
. |
| 11 |
Some noteable differences: |
| 12 |
* session leaders for utmp systems will be one process below |
| 13 |
which is the first process running on that device, this |
| 14 |
changes the -p option |
| 15 |
* Remote hosts are based on the session, not device. So |
| 16 |
a user that ssh's and runs screen will have the ssh session |
| 17 |
and the screens showing a remote IP. Almost no tools |
| 18 |
differentiate between the session running screen and its |
| 19 |
screens so its not real big difference. |
| 20 |
* From/IP won't work for utmp systems |
| 21 |
. |
| 22 |
This patch includes the upstreams original commit and the enhancement. |
| 23 |
. |
| 24 |
Updated 2025-07-23 to avoid acting on the value of uninitialized sessions |
| 25 |
variable. |
| 26 |
Author: Craig Small <[email protected]> |
| 27 |
Origin: upstream, https://gitlab.com/procps-ng/procps/-/commit/f53cc24d57085c87ebb1871b92c0069b72a60926 |
| 28 |
Applied-Upstream: 4.0.6 |
| 29 |
Last-Update: 2025-07-23 |
| 30 |
--- |
| 31 |
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/ |
| 32 |
--- a/man/w.1 |
| 33 |
+++ b/man/w.1 |
| 34 |
@@ -51,6 +51,17 @@ |
| 35 |
\fB\-s\fR, \fB\-\-short\fR |
| 36 |
Use the short format. Don't print the login time, JCPU or PCPU times. |
| 37 |
.TP |
| 38 |
+\fB\-t\fR, \fB\-\-terminal\fR |
| 39 |
+Usually |
| 40 |
+.B w |
| 41 |
+will use either the systemd sessions table or the utmp file to locate users. |
| 42 |
+In \fIterminal mode\fR |
| 43 |
+.B w |
| 44 |
+will scan the terminal devices and locate user sessions this way. This is not |
| 45 |
+a true count of users, for example a user with two xterms will show up twice, |
| 46 |
+so the user count in the header will be different. Currently terminal devices |
| 47 |
+scanned are \fI/dev/tty*\fR and \fI/dev/pts/*\fR. |
| 48 |
+.TP |
| 49 |
\fB\-f\fR, \fB\-\-from\fR |
| 50 |
Toggle printing the |
| 51 |
.B from |
| 52 |
@@ -69,6 +80,7 @@ |
| 53 |
.TP |
| 54 |
\fB\-p\fR, \fB\-\-pids\fR |
| 55 |
Display pid of the login process/the "what" process in the "what" field. |
| 56 |
+The login process is also called the session leader. |
| 57 |
.TP |
| 58 |
\fB\-V\fR, \fB\-\-version\fR |
| 59 |
Display version information. |
| 60 |
@@ -92,25 +104,26 @@ |
| 61 |
.TP |
| 62 |
.I /proc |
| 63 |
process information |
| 64 |
-.SH "SEE ALSO" |
| 65 |
+.TP |
| 66 |
+.IR /dev/tty* " , " /dev/pts/* |
| 67 |
+Terminal device files scanned with \fB\-\-terminal\fR mode. |
| 68 |
+.SH BUGS |
| 69 |
+When using \fB\-\-terminal\fP option, |
| 70 |
+.B w |
| 71 |
+assumes processes with a parent PID of 0 or 1 are |
| 72 |
+.BR agetty (8) |
| 73 |
+processes and will not display them. This is prone to both false postive |
| 74 |
+and negative errors. |
| 75 |
+.SH REPORTING BUGS |
| 76 |
+Please send bug reports to |
| 77 |
+.MT [email protected] |
| 78 |
+.ME |
| 79 |
+.SH SEE ALSO |
| 80 |
.BR free (1), |
| 81 |
.BR loginctl (1), |
| 82 |
.BR ps (1), |
| 83 |
.BR top (1), |
| 84 |
.BR uptime (1), |
| 85 |
+.BR who (1), |
| 86 |
.BR utmp (5), |
| 87 |
-.BR who (1) |
| 88 |
-.SH AUTHORS |
| 89 |
-.B w |
| 90 |
-was re-written almost entirely by Charles Blake, based on the version by |
| 91 |
-.UR greenfie@\:gauss.\:rutgers.\:edu |
| 92 |
-Larry Greenfield |
| 93 |
-.UE |
| 94 |
-and |
| 95 |
-.UR johnsonm@\:redhat.\:com |
| 96 |
-Michael K. Johnson |
| 97 |
-.UE |
| 98 |
-.SH "REPORTING BUGS" |
| 99 |
-Please send bug reports to |
| 100 |
-.UR [email protected] |
| 101 |
-.UE |
| 102 |
+.BR agetty (8) |
| 103 |
--- a/src/w.c |
| 104 |
+++ b/src/w.c |
| 105 |
@@ -1,7 +1,7 @@ |
| 106 |
/* |
| 107 |
* w - show what logged in users are doing. |
| 108 |
* |
| 109 |
- * Copyright © 2009-2023 Craig Small <[email protected]> |
| 110 |
+ * Copyright © 2009-2025 Craig Small <[email protected]> |
| 111 |
* Copyright © 2011-2023 Jim Warner <[email protected]> |
| 112 |
* Copyright © 2011-2012 Sami Kerola <[email protected]> |
| 113 |
* Copyright © 2002-2006 Albert Cahalan |
| 114 |
@@ -34,6 +34,7 @@ |
| 115 |
#include <locale.h> |
| 116 |
#include <pwd.h> |
| 117 |
#include <signal.h> |
| 118 |
+#include <stdbool.h> |
| 119 |
#include <stdio.h> |
| 120 |
#include <stdlib.h> |
| 121 |
#include <string.h> |
| 122 |
@@ -68,6 +69,7 @@ |
| 123 |
|
| 124 |
#include "misc.h" |
| 125 |
#include "pids.h" |
| 126 |
+#include "stat.h" |
| 127 |
|
| 128 |
static int ignoreuser = 0; /* for '-u' */ |
| 129 |
static int oldstyle = 0; /* for '-o' */ |
| 130 |
@@ -95,6 +97,11 @@ |
| 131 |
#define MAX_CMD_WIDTH 512 |
| 132 |
#define MIN_CMD_WIDTH 7 |
| 133 |
|
| 134 |
+/* Must match items in cache_pids */ |
| 135 |
+enum rel_items { |
| 136 |
+ EU_PID, EU_PPID, EU_TGID, EU_START, EU_EUID, EU_RUID, EU_TPGID, EU_PGRP, EU_TTY, |
| 137 |
+ EU_TTY_NAME, EU_TICS_ALL, EU_CMDLINE}; |
| 138 |
+ |
| 139 |
/* |
| 140 |
* This routine is careful since some programs leave utmp strings |
| 141 |
* unprintable. Always outputs at least 16 chars padded with |
| 142 |
@@ -391,6 +398,7 @@ |
| 143 |
|
| 144 |
enum pids_item items[] = { |
| 145 |
PIDS_ID_PID, |
| 146 |
+ PIDS_ID_PPID, |
| 147 |
PIDS_ID_TGID, |
| 148 |
PIDS_TICS_BEGAN, |
| 149 |
PIDS_ID_EUID, |
| 150 |
@@ -398,10 +406,12 @@ |
| 151 |
PIDS_ID_TPGID, |
| 152 |
PIDS_ID_PGRP, |
| 153 |
PIDS_TTY, |
| 154 |
+ PIDS_TTY_NAME, |
| 155 |
PIDS_TICS_ALL, |
| 156 |
PIDS_CMDLINE}; |
| 157 |
+#define ITEMS_COUNT (sizeof items / sizeof *items) |
| 158 |
|
| 159 |
- if (procps_pids_new(info, items, 10) < 0) |
| 160 |
+ if (procps_pids_new(info, items, ITEMS_COUNT) < 0) |
| 161 |
xerrx(EXIT_FAILURE, |
| 162 |
_("Unable to create pid info structure")); |
| 163 |
if ((reap = procps_pids_reap(*info, PIDS_FETCH_TASKS_ONLY)) == NULL) |
| 164 |
@@ -429,10 +439,10 @@ |
| 165 |
pid_t *pid, |
| 166 |
struct pids_fetch *reap) |
| 167 |
{ |
| 168 |
-#define PIDS_GETINT(e) PIDS_VAL(EU_ ## e, s_int, reap->stacks[i], info) |
| 169 |
-#define PIDS_GETUNT(e) PIDS_VAL(EU_ ## e, u_int, reap->stacks[i], info) |
| 170 |
-#define PIDS_GETULL(e) PIDS_VAL(EU_ ## e, ull_int, reap->stacks[i], info) |
| 171 |
-#define PIDS_GETSTR(e) PIDS_VAL(EU_ ## e, str, reap->stacks[i], info) |
| 172 |
+#define PIDS_GETINT(e) PIDS_VAL(EU_ ## e, s_int, reap->stacks[i],NULL) |
| 173 |
+#define PIDS_GETUNT(e) PIDS_VAL(EU_ ## e, u_int, reap->stacks[i],NULL) |
| 174 |
+#define PIDS_GETULL(e) PIDS_VAL(EU_ ## e, ull_int, reap->stacks[i],NULL) |
| 175 |
+#define PIDS_GETSTR(e) PIDS_VAL(EU_ ## e, str, reap->stacks[i],NULL) |
| 176 |
unsigned uid = ~0U; |
| 177 |
pid_t ut_pid = -1; |
| 178 |
int found_utpid = 0; |
| 179 |
@@ -440,11 +450,6 @@ |
| 180 |
unsigned long long best_time = 0; |
| 181 |
unsigned long long secondbest_time = 0; |
| 182 |
|
| 183 |
- /* Must match items in cache_pids */ |
| 184 |
- enum rel_items { |
| 185 |
- EU_PID, EU_TGID, EU_START, EU_EUID, EU_RUID, EU_TPGID, EU_PGRP, EU_TTY, |
| 186 |
- EU_TICS_ALL, EU_CMDLINE}; |
| 187 |
- |
| 188 |
*jcpu = 0; |
| 189 |
*pcpu = 0; |
| 190 |
if (!ignoreuser) { |
| 191 |
@@ -492,7 +497,7 @@ |
| 192 |
} |
| 193 |
if (PIDS_GETINT(TTY) != line) |
| 194 |
continue; |
| 195 |
- (*jcpu) += PIDS_VAL(EU_TICS_ALL, ull_int, reap->stacks[i], info); |
| 196 |
+ (*jcpu) += PIDS_VAL(EU_TICS_ALL, ull_int, reap->stacks[i], NULL); |
| 197 |
if (!(secondbest_time && PIDS_GETULL(START) <= secondbest_time)) { |
| 198 |
secondbest_time = PIDS_GETULL(START); |
| 199 |
if (cmdline[0] == '-' && cmdline[1] == '\0') { |
| 200 |
@@ -660,6 +665,7 @@ |
| 201 |
fputs(_(" -h, --no-header do not print header\n"),out); |
| 202 |
fputs(_(" -u, --no-current ignore current process username\n"),out); |
| 203 |
fputs(_(" -s, --short short format\n"),out); |
| 204 |
+ fputs(_(" -t, --terminal show terminals\n"),out); |
| 205 |
fputs(_(" -f, --from show remote hostname field\n"),out); |
| 206 |
fputs(_(" -o, --old-style old style output\n"),out); |
| 207 |
fputs(_(" -i, --ip-addr display IP address instead of hostname (if possible)\n"), out); |
| 208 |
@@ -672,6 +678,232 @@ |
| 209 |
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); |
| 210 |
} |
| 211 |
|
| 212 |
+/* |
| 213 |
+ * print_logintime expects a time of seconds since epoch |
| 214 |
+ * libproc returns a process start time in tics since |
| 215 |
+ * boot. This function convers the start time to |
| 216 |
+ * seconds since epoch |
| 217 |
+ */ |
| 218 |
+time_t get_starttime( |
| 219 |
+ unsigned long long proc_start, |
| 220 |
+ long hertz) |
| 221 |
+{ |
| 222 |
+ static unsigned long long boot_time = 0; |
| 223 |
+ enum stat_item stat_items[] = { |
| 224 |
+ STAT_SYS_TIME_OF_BOOT |
| 225 |
+ }; |
| 226 |
+ |
| 227 |
+ if (boot_time == 0) { |
| 228 |
+ struct stat_info *stat_info = NULL; |
| 229 |
+ struct stat_stack *stat_stack; |
| 230 |
+ if (procps_stat_new(&stat_info) < 0) |
| 231 |
+ xerrx(EXIT_FAILURE, _("Unable to create system stat structure")); |
| 232 |
+ if (!(stat_stack = procps_stat_select(stat_info, stat_items, 1))) |
| 233 |
+ xerrx(EXIT_FAILURE, _("Unable to select stat information")); |
| 234 |
+ boot_time = STAT_VAL(0, ul_int, stat_stack, stat_info); |
| 235 |
+ } |
| 236 |
+ return proc_start / hertz + boot_time; |
| 237 |
+} |
| 238 |
+ |
| 239 |
+void print_terminal_user( |
| 240 |
+ const int longform, |
| 241 |
+ int maxcmd, |
| 242 |
+ const int from, |
| 243 |
+ const int userlen, |
| 244 |
+ const int fromlen, |
| 245 |
+ const int ip_addresses, |
| 246 |
+ const int show_pids, |
| 247 |
+ const unsigned long long first_start, |
| 248 |
+ const pid_t first_pid, |
| 249 |
+ const pid_t last_pid, |
| 250 |
+ const char *ttyname, |
| 251 |
+ const unsigned long long jcpu, |
| 252 |
+ const unsigned long long pcpu, |
| 253 |
+ const char *cmdline) |
| 254 |
+{ |
| 255 |
+ long hertz; |
| 256 |
+ char *session = NULL; |
| 257 |
+ char *username = NULL; |
| 258 |
+ char ttypath[5 + UT_LINESIZE + 1] = "/dev/"; |
| 259 |
+ char uname[UT_NAMESIZE + 1] = "?"; |
| 260 |
+ int pids_length = 0; |
| 261 |
+ utmp_t *u; |
| 262 |
+ |
| 263 |
+ hertz = procps_hertz_get(); |
| 264 |
+ strncpy(ttypath + 5, ttyname, UT_LINESIZE); |
| 265 |
+ |
| 266 |
+#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER) |
| 267 |
+ if (sd_pid_get_session(last_pid, &session) >= 0) { |
| 268 |
+ if ( sd_session_get_username(session, &username) >= 0) { |
| 269 |
+ strncpy(uname, username, UT_NAMESIZE); |
| 270 |
+ uname[UT_NAMESIZE] = '\0'; |
| 271 |
+ free(username); |
| 272 |
+ } |
| 273 |
+ } else { |
| 274 |
+ // We get the utmp entry for this tty device |
| 275 |
+ utmp_t search; |
| 276 |
+ strncpy(search.ut_line, ttyname, UT_NAMESIZE-1); |
| 277 |
+#ifdef HAVE_UTMPX_H |
| 278 |
+ setutxent(); |
| 279 |
+ u = getutxline(&search); |
| 280 |
+ endutxent(); |
| 281 |
+#else |
| 282 |
+ utmpname(UTMP_FILE); |
| 283 |
+ setutent(); |
| 284 |
+ u = getutline(&search); |
| 285 |
+ endutent(); |
| 286 |
+#endif |
| 287 |
+ } |
| 288 |
+#endif // SYSTEMD |
| 289 |
+ // Find username of device |
| 290 |
+ if (uname[0] == '?') { |
| 291 |
+ struct stat st; |
| 292 |
+ struct passwd *pw; |
| 293 |
+ |
| 294 |
+ if (stat(ttypath, &st) == 0) { |
| 295 |
+ if ((pw = getpwuid(st.st_uid)) != NULL) |
| 296 |
+ strncpy(uname, pw->pw_name, UT_NAMESIZE); |
| 297 |
+ else |
| 298 |
+ snprintf(uname, UT_NAMESIZE, "%d", st.st_uid); |
| 299 |
+ } |
| 300 |
+ } |
| 301 |
+ |
| 302 |
+ |
| 303 |
+ printf("%-*.*s%-9.8s", userlen + 1, userlen, uname, ttyname); |
| 304 |
+ if (from) |
| 305 |
+ print_from(session, u, ip_addresses, fromlen); |
| 306 |
+ /* login time */ |
| 307 |
+ if (longform) { |
| 308 |
+#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER) |
| 309 |
+ if (session) { |
| 310 |
+ uint64_t ltime; |
| 311 |
+ |
| 312 |
+ sd_session_get_start_time(session, <ime); |
| 313 |
+ print_logintime(ltime/((uint64_t) 1000000ULL), stdout); |
| 314 |
+ } else { |
| 315 |
+#endif |
| 316 |
+ // Different to main w as we use process start time rather than unreliable utmp |
| 317 |
+ print_logintime(get_starttime(first_start, hertz), stdout); |
| 318 |
+ |
| 319 |
+#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER) |
| 320 |
+ } |
| 321 |
+#endif |
| 322 |
+ } |
| 323 |
+ print_time_ival7(idletime(ttypath), 0, stdout); |
| 324 |
+ /* jpcpu/pcpu */ |
| 325 |
+ if (longform) { |
| 326 |
+ print_time_ival7(jcpu / hertz, (jcpu % hertz) * (100. / hertz), stdout); |
| 327 |
+ if (pcpu > 0) |
| 328 |
+ print_time_ival7(pcpu / hertz, |
| 329 |
+ (pcpu % hertz) * (100. / hertz), |
| 330 |
+ stdout); |
| 331 |
+ else |
| 332 |
+ printf(" ? "); |
| 333 |
+ } |
| 334 |
+ if (show_pids) { |
| 335 |
+ pid_t ut_pid = -1; |
| 336 |
+#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER) |
| 337 |
+ sd_session_get_leader(session, &ut_pid); |
| 338 |
+#endif |
| 339 |
+ if (ut_pid == -1) |
| 340 |
+ ut_pid = u->ut_pid; |
| 341 |
+ pids_length = printf(" %6d/%6d", ut_pid, last_pid); |
| 342 |
+ if (pids_length > maxcmd) { |
| 343 |
+ maxcmd = 0; |
| 344 |
+ } else if (pids_length > 0) |
| 345 |
+ maxcmd -= pids_length; |
| 346 |
+ } |
| 347 |
+ /* what */ |
| 348 |
+ printf(" %.*s\n", maxcmd, cmdline); |
| 349 |
+ free(session); |
| 350 |
+} |
| 351 |
+ |
| 352 |
+/* |
| 353 |
+ * Instead of going through the systemd sessions or utmp entries |
| 354 |
+ * scan all processes and find those that have TTY defined, skipping |
| 355 |
+ * over the gettys which have PID 0/1 as parent |
| 356 |
+ */ |
| 357 |
+#define PIDS_GETINT(e) PIDS_VAL(EU_ ## e, s_int, reap->stacks[i],NULL) |
| 358 |
+#define PIDS_GETUNT(e) PIDS_VAL(EU_ ## e, u_int, reap->stacks[i],NULL) |
| 359 |
+#define PIDS_GETULL(e) PIDS_VAL(EU_ ## e, ull_int, reap->stacks[i],NULL) |
| 360 |
+#define PIDS_GETSTR(e) PIDS_VAL(EU_ ## e, str, reap->stacks[i],NULL) |
| 361 |
+void print_user_terminals( |
| 362 |
+ const int longform, |
| 363 |
+ int maxcmd, |
| 364 |
+ const int from, |
| 365 |
+ const int userlen, |
| 366 |
+ const int fromlen, |
| 367 |
+ const int ip_addresses, |
| 368 |
+ const int show_pids, |
| 369 |
+ struct pids_info *pids_info, |
| 370 |
+ struct pids_fetch *reap) |
| 371 |
+{ |
| 372 |
+ int i; |
| 373 |
+ int total_procs = reap->counts->total; |
| 374 |
+ int current_tty = -1; |
| 375 |
+ |
| 376 |
+ pid_t first_pid; |
| 377 |
+ unsigned long long last_start = 0; |
| 378 |
+ unsigned long long first_start = 0; |
| 379 |
+ pid_t last_pid; |
| 380 |
+ char cmdline[MAX_CMD_WIDTH+1]; |
| 381 |
+ char ttyname[UT_NAMESIZE+1]; |
| 382 |
+ unsigned long long jcpu; |
| 383 |
+ unsigned long long pcpu; |
| 384 |
+ |
| 385 |
+ |
| 386 |
+ if (!procps_pids_sort(pids_info, |
| 387 |
+ reap->stacks, total_procs, |
| 388 |
+ PIDS_TICS_BEGAN, PIDS_SORT_ASCEND)) |
| 389 |
+ xerrx(EXIT_FAILURE, _("Unable to sort pids")); |
| 390 |
+ if (!procps_pids_sort(pids_info, |
| 391 |
+ reap->stacks, total_procs, |
| 392 |
+ PIDS_TTY, PIDS_SORT_ASCEND)) |
| 393 |
+ xerrx(EXIT_FAILURE, _("Unable to sort pids")); |
| 394 |
+ |
| 395 |
+ for (i=0; i < total_procs; i++) { |
| 396 |
+ /* Skip if: |
| 397 |
+ * The process has no TTY |
| 398 |
+ * PPID is 0 or 1 which is a getty |
| 399 |
+ */ |
| 400 |
+ if (PIDS_GETINT(TTY) == 0 || |
| 401 |
+ PIDS_GETINT(PPID) == 1 || PIDS_GETINT(PPID) == 0) |
| 402 |
+ continue; |
| 403 |
+ |
| 404 |
+ if (current_tty == PIDS_GETINT(TTY)) { |
| 405 |
+ if (last_start == 0 || last_start < PIDS_GETULL(START)) { |
| 406 |
+ last_pid = PIDS_GETINT(TGID); |
| 407 |
+ last_start = PIDS_GETULL(START); |
| 408 |
+ strncpy(cmdline, PIDS_GETSTR(CMDLINE), MAX_CMD_WIDTH); |
| 409 |
+ } |
| 410 |
+ jcpu += (PIDS_GETULL(TICS_ALL)); |
| 411 |
+ } else { // Changed TTY |
| 412 |
+ if (current_tty != -1) { // We have data |
| 413 |
+ print_terminal_user(longform, maxcmd, from, userlen, fromlen, ip_addresses, show_pids, first_start, first_pid, last_pid, ttyname, jcpu, pcpu, cmdline); |
| 414 |
+ } |
| 415 |
+ // Reset and get ready for next round |
| 416 |
+ current_tty = PIDS_GETINT(TTY); |
| 417 |
+ first_pid = PIDS_GETINT(TGID); |
| 418 |
+ first_start = PIDS_GETULL(START); |
| 419 |
+ strncpy(ttyname, PIDS_GETSTR(TTY_NAME), UT_NAMESIZE); |
| 420 |
+ pcpu = PIDS_GETULL(TICS_ALL); |
| 421 |
+ jcpu = (PIDS_GETULL(TICS_ALL)); |
| 422 |
+ /* With one process, first is last */ |
| 423 |
+ last_pid = PIDS_GETINT(TGID); |
| 424 |
+ last_start = PIDS_GETULL(START); |
| 425 |
+ strncpy(cmdline, PIDS_GETSTR(CMDLINE), MAX_CMD_WIDTH); |
| 426 |
+ } |
| 427 |
+ } |
| 428 |
+ if (current_tty != -1) { |
| 429 |
+ print_terminal_user(longform, maxcmd, from, userlen, fromlen, ip_addresses, show_pids, first_start, first_pid, last_pid, ttyname, jcpu, pcpu, cmdline); |
| 430 |
+ } |
| 431 |
+ |
| 432 |
+} |
| 433 |
+#undef PIDS_GETINT |
| 434 |
+#undef PIDS_GETUNT |
| 435 |
+#undef PIDS_GETULL |
| 436 |
+#undef PIDS_GETSTR |
| 437 |
+ |
| 438 |
int main(int argc, char **argv) |
| 439 |
{ |
| 440 |
char *match_user = NULL, *p; |
| 441 |
@@ -689,6 +921,7 @@ |
| 442 |
int from = 1; |
| 443 |
int ip_addresses = 0; |
| 444 |
int pids = 0; |
| 445 |
+ bool term_mode = false; |
| 446 |
struct pids_info *info = NULL; |
| 447 |
struct pids_fetch *pids_cache = NULL; |
| 448 |
|
| 449 |
@@ -700,6 +933,7 @@ |
| 450 |
{"no-header", no_argument, NULL, 'h'}, |
| 451 |
{"no-current", no_argument, NULL, 'u'}, |
| 452 |
{"short", no_argument, NULL, 's'}, |
| 453 |
+ {"terminal", no_argument, NULL, 't'}, |
| 454 |
{"from", no_argument, NULL, 'f'}, |
| 455 |
{"old-style", no_argument, NULL, 'o'}, |
| 456 |
{"ip-addr", no_argument, NULL, 'i'}, |
| 457 |
@@ -722,7 +956,7 @@ |
| 458 |
#endif |
| 459 |
|
| 460 |
while ((ch = |
| 461 |
- getopt_long(argc, argv, "husfoVip", longopts, NULL)) != -1) |
| 462 |
+ getopt_long(argc, argv, "hustfoVip", longopts, NULL)) != -1) |
| 463 |
switch (ch) { |
| 464 |
case 'h': |
| 465 |
header = 0; |
| 466 |
@@ -730,6 +964,9 @@ |
| 467 |
case 's': |
| 468 |
longform = 0; |
| 469 |
break; |
| 470 |
+ case 't': |
| 471 |
+ term_mode = true; |
| 472 |
+ break; |
| 473 |
case 'f': |
| 474 |
from = !from; |
| 475 |
break; |
| 476 |
@@ -811,40 +1048,42 @@ |
| 477 |
else |
| 478 |
printf(_(" IDLE WHAT\n")); |
| 479 |
} |
| 480 |
-#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER) |
| 481 |
- if (sd_booted() > 0) { |
| 482 |
- char **sessions_list; |
| 483 |
- int sessions; |
| 484 |
- int i; |
| 485 |
|
| 486 |
+ if (term_mode) { |
| 487 |
+ print_user_terminals(longform, maxcmd, from, userlen, fromlen, ip_addresses, pids, info, pids_cache); |
| 488 |
+ } else { |
| 489 |
+#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER) |
| 490 |
+ char **sessions_list; |
| 491 |
+ int sessions = 0; |
| 492 |
+ if (sd_booted() > 0) |
| 493 |
sessions = sd_get_sessions (&sessions_list); |
| 494 |
- if (sessions < 0 && sessions != -ENOENT) |
| 495 |
- error(EXIT_FAILURE, -sessions, _("error getting sessions")); |
| 496 |
- |
| 497 |
- if (sessions >= 0) { |
| 498 |
- for (int i = 0; i < sessions; i++) { |
| 499 |
- char *class, *name; |
| 500 |
- int r; |
| 501 |
- |
| 502 |
- if ((r = sd_session_get_class(sessions_list[i], &class)) < 0) |
| 503 |
- error(EXIT_FAILURE, -r, _("session get class failed")); |
| 504 |
- if (strncmp(class, "user", 4) != 0) { // user, user-early, user-incomplete |
| 505 |
- free(class); |
| 506 |
- continue; |
| 507 |
- } |
| 508 |
+ if (sessions < 0 && sessions != -ENOENT) |
| 509 |
+ error(EXIT_FAILURE, -sessions, _("error getting sessions")); |
| 510 |
+ if (sessions > 0) { |
| 511 |
+ //int i; |
| 512 |
+ for (int i = 0; i < sessions; i++) { |
| 513 |
+ char *class, *name; |
| 514 |
+ int r; |
| 515 |
+ |
| 516 |
+ if ((r = sd_session_get_class(sessions_list[i], &class)) < 0) |
| 517 |
+ error(EXIT_FAILURE, -r, _("session get class failed")); |
| 518 |
+ if (strncmp(class, "user", 4) != 0) { // user, user-early, user-incomplete |
| 519 |
free(class); |
| 520 |
- if ((r = sd_session_get_username(sessions_list[i], &name)) < 0) |
| 521 |
- error(EXIT_FAILURE, -r, _("get user name failed")); |
| 522 |
- if (!match_user || (0 == strcmp(name, match_user))) |
| 523 |
- showinfo(sessions_list[i], name, NULL, longform, maxcmd, |
| 524 |
- from, userlen, fromlen, ip_addresses, pids, |
| 525 |
- pids_cache); |
| 526 |
- |
| 527 |
- free(name); |
| 528 |
- free(sessions_list[i]); |
| 529 |
- } |
| 530 |
- free(sessions_list); |
| 531 |
+ continue; |
| 532 |
+ } |
| 533 |
+ if ((r = sd_session_get_username(sessions_list[i], &name)) < 0) |
| 534 |
+ error(EXIT_FAILURE, -r, _("get user name failed")); |
| 535 |
+ |
| 536 |
+ if (!match_user || (0 == strcmp(name, match_user))) |
| 537 |
+ showinfo(sessions_list[i], name, NULL, longform, maxcmd, |
| 538 |
+ from, userlen, fromlen, ip_addresses, pids, |
| 539 |
+ pids_cache); |
| 540 |
+ |
| 541 |
+ free(class); |
| 542 |
+ free(name); |
| 543 |
+ free(sessions_list[i]); |
| 544 |
} |
| 545 |
+ free(sessions_list); |
| 546 |
} else { |
| 547 |
#endif |
| 548 |
#ifdef HAVE_UTMPX_H |
| 549 |
@@ -861,7 +1100,7 @@ |
| 550 |
#endif |
| 551 |
if (!u) |
| 552 |
break; |
| 553 |
- if (u->ut_type != USER_PROCESS || (NULL == u->ut_user)) |
| 554 |
+ if (u->ut_type != USER_PROCESS || ('\0' == u->ut_user[0])) |
| 555 |
continue; |
| 556 |
if (!match_user || |
| 557 |
(0 == strncmp(u->ut_user, match_user, UT_NAMESIZE))) |
| 558 |
@@ -878,6 +1117,8 @@ |
| 559 |
#if (defined(WITH_SYSTEMD) || defined(WITH_ELOGIND)) && defined(HAVE_SD_SESSION_GET_LEADER) |
| 560 |
} |
| 561 |
#endif |
| 562 |
+ } |
| 563 |
|
| 564 |
+ procps_pids_unref(&info); |
| 565 |
return EXIT_SUCCESS; |
| 566 |
} |
| 567 |
--- a/testsuite/w.test/w.exp |
| 568 |
+++ b/testsuite/w.test/w.exp |
| 569 |
@@ -57,3 +57,10 @@ |
| 570 |
spawn $w -p |
| 571 |
expect_pass "$test" "^${w_std_header}${w_pid_userlines}" |
| 572 |
|
| 573 |
+set test "w with terminal mode" |
| 574 |
+spawn $w -t |
| 575 |
+expect_pass "$test" "^${w_std_header}${w_std_userlines}" |
| 576 |
+ |
| 577 |
+set test "w with terminal mode from and short flags" |
| 578 |
+spawn $w -tfs |
| 579 |
+expect_pass "$test" "^${w_fromshort_header}${w_fromshort_userlines}" |