2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by jla; revised by djm; revised again by mstone */
20 name [state] line time [activity] [pid] [comment] [exit]
22 name, line, time: not -q
30 #include <sys/types.h>
34 #include "canon-host.h"
36 #include "hard-locale.h"
43 /* The official name of this program (e.g., no 'g' prefix). */
44 #define PROGRAM_NAME "who"
47 proper_name ("Joseph Arceneaux"), \
48 proper_name ("David MacKenzie"), \
49 proper_name ("Michael Stone")
52 # define UT_TYPE_RUN_LVL(U) ((U)->ut_type == RUN_LVL)
54 # define UT_TYPE_RUN_LVL(U) false
58 # define UT_TYPE_INIT_PROCESS(U) ((U)->ut_type == INIT_PROCESS)
60 # define UT_TYPE_INIT_PROCESS(U) false
64 # define UT_TYPE_LOGIN_PROCESS(U) ((U)->ut_type == LOGIN_PROCESS)
66 # define UT_TYPE_LOGIN_PROCESS(U) false
70 # define UT_TYPE_DEAD_PROCESS(U) ((U)->ut_type == DEAD_PROCESS)
72 # define UT_TYPE_DEAD_PROCESS(U) false
76 # define UT_TYPE_NEW_TIME(U) ((U)->ut_type == NEW_TIME)
78 # define UT_TYPE_NEW_TIME(U) false
83 #if HAVE_STRUCT_XTMP_UT_PID
84 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
85 char Var[INT_STRLEN_BOUND (Utmp_ent->ut_pid) + 1]; \
86 sprintf (Var, "%ld", (long int) (Utmp_ent->ut_pid))
88 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
92 #if HAVE_STRUCT_XTMP_UT_ID
93 # define UT_ID(U) ((U)->ut_id)
95 # define UT_ID(U) "??"
98 /* If true, attempt to canonicalize hostnames via a DNS lookup. */
99 static bool do_lookup
;
101 /* If true, display only a list of usernames and count of
103 Ignored for 'who am i'. */
104 static bool short_list
;
106 /* If true, display only name, line, and time fields. */
107 static bool short_output
;
109 /* If true, display the hours:minutes since each user has touched
110 the keyboard, or "." if within the last minute, or "old" if
111 not within the last day. */
112 static bool include_idle
;
114 /* If true, display a line at the top describing each field. */
115 static bool include_heading
;
117 /* If true, display a '+' for each user if mesg y, a '-' if mesg n,
118 or a '?' if their tty cannot be statted. */
119 static bool include_mesg
;
121 /* If true, display process termination & exit status. */
122 static bool include_exit
;
124 /* If true, display the last boot time. */
125 static bool need_boottime
;
127 /* If true, display dead processes. */
128 static bool need_deadprocs
;
130 /* If true, display processes waiting for user login. */
131 static bool need_login
;
133 /* If true, display processes started by init. */
134 static bool need_initspawn
;
136 /* If true, display the last clock change. */
137 static bool need_clockchange
;
139 /* If true, display the current runlevel. */
140 static bool need_runlevel
;
142 /* If true, display user processes. */
143 static bool need_users
;
145 /* If true, display info only for the controlling tty. */
146 static bool my_line_only
;
148 /* The strftime format to use for login times, and its expected
150 static char const *time_format
;
151 static int time_format_width
;
153 /* for long options with no corresponding short option, use enum */
156 LOOKUP_OPTION
= CHAR_MAX
+ 1
159 static struct option
const longopts
[] =
161 {"all", no_argument
, nullptr, 'a'},
162 {"boot", no_argument
, nullptr, 'b'},
163 {"count", no_argument
, nullptr, 'q'},
164 {"dead", no_argument
, nullptr, 'd'},
165 {"heading", no_argument
, nullptr, 'H'},
166 {"login", no_argument
, nullptr, 'l'},
167 {"lookup", no_argument
, nullptr, LOOKUP_OPTION
},
168 {"message", no_argument
, nullptr, 'T'},
169 {"mesg", no_argument
, nullptr, 'T'},
170 {"process", no_argument
, nullptr, 'p'},
171 {"runlevel", no_argument
, nullptr, 'r'},
172 {"short", no_argument
, nullptr, 's'},
173 {"time", no_argument
, nullptr, 't'},
174 {"users", no_argument
, nullptr, 'u'},
175 {"writable", no_argument
, nullptr, 'T'},
176 {GETOPT_HELP_OPTION_DECL
},
177 {GETOPT_VERSION_OPTION_DECL
},
178 {nullptr, 0, nullptr, 0}
181 /* Return a string representing the time between WHEN and now.
182 BOOTTIME is the time of last reboot.
185 idle_string (time_t when
, time_t boottime
)
187 static time_t now
= TYPE_MINIMUM (time_t);
189 if (now
== TYPE_MINIMUM (time_t))
193 if (boottime
< when
&& when
<= now
194 && ! ckd_sub (&seconds_idle
, now
, when
)
195 && seconds_idle
< 24 * 60 * 60)
197 if (seconds_idle
< 60)
201 static char idle_hhmm
[IDLESTR_LEN
];
202 sprintf (idle_hhmm
, "%02d:%02d",
203 seconds_idle
/ (60 * 60),
204 (seconds_idle
% (60 * 60)) / 60);
212 /* Return a time string. */
214 time_string (struct gl_utmp
const *utmp_ent
)
216 static char buf
[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
217 struct tm
*tmp
= localtime (&utmp_ent
->ut_ts
.tv_sec
);
221 strftime (buf
, sizeof buf
, time_format
, tmp
);
225 return timetostr (utmp_ent
->ut_ts
.tv_sec
, buf
);
228 /* Print formatted output line. Uses mostly arbitrary field sizes, probably
229 will need tweaking if any of the localization stuff is done, or for 64 bit
232 print_line (char const *user
, const char state
,
234 char const *time_str
, char const *idle
, char const *pid
,
235 char const *comment
, char const *exitstr
)
237 static char mesg
[3] = { ' ', 'x', '\0' };
239 char x_idle
[1 + IDLESTR_LEN
+ 1];
240 char x_pid
[1 + INT_STRLEN_BOUND (pid_t
) + 1];
246 if (include_idle
&& !short_output
&& strlen (idle
) < sizeof x_idle
- 1)
247 sprintf (x_idle
, " %-6s", idle
);
251 if (!short_output
&& strlen (pid
) < sizeof x_pid
- 1)
252 sprintf (x_pid
, " %10s", pid
);
256 x_exitstr
= xmalloc (include_exit
? 1 + MAX (12, strlen (exitstr
)) + 1 : 1);
258 sprintf (x_exitstr
, " %-12s", exitstr
);
262 err
= asprintf (&buf
,
273 include_mesg
? mesg
: "",
279 /* FIXME: it's not really clear whether the following
280 field should be in the short_output. A strict reading
281 of SUSv2 would suggest not, but I haven't seen any
282 implementations that actually work that way... */
290 /* Remove any trailing spaces. */
291 char *p
= buf
+ strlen (buf
);
302 /* Return true if a terminal device given as PSTAT allows other users
303 to send messages to; false otherwise */
305 is_tty_writable (struct stat
const *pstat
)
307 #ifdef TTY_GROUP_NAME
308 /* Ensure the group of the TTY device matches TTY_GROUP_NAME, more info at
309 https://bugzilla.redhat.com/454261 */
310 struct group
*ttygr
= getgrnam (TTY_GROUP_NAME
);
311 if (!ttygr
|| (pstat
->st_gid
!= ttygr
->gr_gid
))
315 return pstat
->st_mode
& S_IWGRP
;
318 /* Send properly parsed USER_PROCESS info to print_line. The most
319 recent boot time is BOOTTIME. */
321 print_user (struct gl_utmp
const *utmp_ent
, time_t boottime
)
326 char idlestr
[IDLESTR_LEN
+ 1];
327 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
328 static char *hoststr
;
329 #if HAVE_STRUCT_XTMP_UT_HOST
330 static idx_t hostlen
;
333 /* If ut_line contains a space, the device name starts after the space. */
334 char *line
= utmp_ent
->ut_line
;
335 char *space
= strchr (line
, ' ');
336 line
= space
? space
+ 1 : line
;
339 if (IS_ABSOLUTE_FILE_NAME (line
))
343 static int dev_dirfd
;
346 dev_dirfd
= open ("/dev", O_PATHSEARCH
| O_DIRECTORY
);
348 dev_dirfd
= AT_FDCWD
- 1;
353 if (AT_FDCWD
<= dirfd
&& fstatat (dirfd
, line
, &stats
, 0) == 0)
355 mesg
= is_tty_writable (&stats
) ? '+' : '-';
356 last_change
= stats
.st_atime
;
365 sprintf (idlestr
, "%.*s", IDLESTR_LEN
, idle_string (last_change
, boottime
));
367 sprintf (idlestr
, " ?");
369 #if HAVE_STRUCT_XTMP_UT_HOST
370 if (utmp_ent
->ut_host
[0])
372 char *host
= nullptr;
373 char *display
= nullptr;
374 char *ut_host
= utmp_ent
->ut_host
;
376 /* Look for an X display. */
377 display
= strchr (ut_host
, ':');
381 if (*ut_host
&& do_lookup
)
383 /* See if we can canonicalize it. */
384 host
= canon_host (ut_host
);
392 idx_t needed
= strlen (host
) + strlen (display
) + 4;
393 if (hostlen
< needed
)
396 hoststr
= xpalloc (nullptr, &hostlen
, needed
- hostlen
, -1, 1);
400 p
= stpcpy (p
, host
);
402 strcpy (stpcpy (p
, display
), ")");
406 idx_t needed
= strlen (host
) + 3;
407 if (hostlen
< needed
)
410 hoststr
= xpalloc (nullptr, &hostlen
, needed
- hostlen
, -1, 1);
414 strcpy (stpcpy (p
, host
), ")");
423 hoststr
= xpalloc (hoststr
, &hostlen
, 1, -1, 1);
428 print_line (utmp_ent
->ut_user
, mesg
,
430 time_string (utmp_ent
), idlestr
, pidstr
,
431 hoststr
? hoststr
: "", "");
435 print_boottime (struct gl_utmp
const *utmp_ent
)
437 print_line ("", ' ', _("system boot"),
438 time_string (utmp_ent
), "", "", "", "");
442 make_id_equals_comment (struct gl_utmp
const *utmp_ent
)
444 char const *id
= UT_ID (utmp_ent
);
445 idx_t idlen
= strlen (id
);
446 char const *prefix
= _("id=");
447 idx_t prefixlen
= strlen (prefix
);
448 char *comment
= xmalloc (prefixlen
+ idlen
+ 1);
449 char *p
= mempcpy (comment
, prefix
, prefixlen
);
450 p
= mempcpy (p
, id
, idlen
);
456 print_deadprocs (struct gl_utmp
const *utmp_ent
)
458 static char *exitstr
;
459 char *comment
= make_id_equals_comment (utmp_ent
);
460 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
463 exitstr
= xmalloc (strlen (_("term="))
464 + INT_STRLEN_BOUND (utmp_ent
->ut_exit
.e_termination
) + 1
465 + strlen (_("exit="))
466 + INT_STRLEN_BOUND (utmp_ent
->ut_exit
.e_exit
)
468 sprintf (exitstr
, "%s%d %s%d", _("term="), utmp_ent
->ut_exit
.e_termination
,
469 _("exit="), utmp_ent
->ut_exit
.e_exit
);
471 /* FIXME: add idle time? */
473 print_line ("", ' ', utmp_ent
->ut_line
,
474 time_string (utmp_ent
), "", pidstr
, comment
, exitstr
);
479 print_login (struct gl_utmp
const *utmp_ent
)
481 char *comment
= make_id_equals_comment (utmp_ent
);
482 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
484 /* FIXME: add idle time? */
486 print_line (_("LOGIN"), ' ', utmp_ent
->ut_line
,
487 time_string (utmp_ent
), "", pidstr
, comment
, "");
492 print_initspawn (struct gl_utmp
const *utmp_ent
)
494 char *comment
= make_id_equals_comment (utmp_ent
);
495 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
497 print_line ("", ' ', utmp_ent
->ut_line
,
498 time_string (utmp_ent
), "", pidstr
, comment
, "");
503 print_clockchange (struct gl_utmp
const *utmp_ent
)
505 /* FIXME: handle NEW_TIME & OLD_TIME both */
506 print_line ("", ' ', _("clock change"),
507 time_string (utmp_ent
), "", "", "", "");
511 print_runlevel (struct gl_utmp
const *utmp_ent
)
513 static char *runlevline
, *comment
;
514 unsigned char last
= utmp_ent
->ut_pid
/ 256;
515 unsigned char curr
= utmp_ent
->ut_pid
% 256;
518 runlevline
= xmalloc (strlen (_("run-level")) + 3);
519 sprintf (runlevline
, "%s %c", _("run-level"), curr
);
522 comment
= xmalloc (strlen (_("last=")) + 2);
523 sprintf (comment
, "%s%c", _("last="), (last
== 'N') ? 'S' : last
);
525 print_line ("", ' ', runlevline
, time_string (utmp_ent
),
526 "", "", c_isprint (last
) ? comment
: "", "");
531 /* Print the username of each valid entry and the number of valid entries
532 in UTMP_BUF, which should have N elements. */
534 list_entries_who (idx_t n
, struct gl_utmp
const *utmp_buf
)
537 char const *separator
= "";
541 if (IS_USER_PROCESS (utmp_buf
))
545 trimmed_name
= extract_trimmed_name (utmp_buf
);
547 printf ("%s%s", separator
, trimmed_name
);
554 printf (_("\n# users=%td\n"), entries
);
560 print_line (_("NAME"), ' ', _("LINE"), _("TIME"), _("IDLE"),
561 _("PID"), _("COMMENT"), _("EXIT"));
564 /* Display UTMP_BUF, which should have N entries. */
566 scan_entries (idx_t n
, struct gl_utmp
const *utmp_buf
)
568 char *ttyname_b
IF_LINT ( = nullptr);
569 time_t boottime
= TYPE_MINIMUM (time_t);
576 ttyname_b
= ttyname (STDIN_FILENO
);
579 if (STRNCMP_LIT (ttyname_b
, "/dev/") == 0)
580 ttyname_b
+= sizeof "/dev/" - 1; /* Discard /dev/ prefix. */
586 || STREQ (ttyname_b
, utmp_buf
->ut_line
))
588 if (need_users
&& IS_USER_PROCESS (utmp_buf
))
589 print_user (utmp_buf
, boottime
);
590 else if (need_runlevel
&& UT_TYPE_RUN_LVL (utmp_buf
))
591 print_runlevel (utmp_buf
);
592 else if (need_boottime
&& UT_TYPE_BOOT_TIME (utmp_buf
))
593 print_boottime (utmp_buf
);
594 /* I've never seen one of these, so I don't know what it should
596 FIXME: handle OLD_TIME also, perhaps show the delta? */
597 else if (need_clockchange
&& UT_TYPE_NEW_TIME (utmp_buf
))
598 print_clockchange (utmp_buf
);
599 else if (need_initspawn
&& UT_TYPE_INIT_PROCESS (utmp_buf
))
600 print_initspawn (utmp_buf
);
601 else if (need_login
&& UT_TYPE_LOGIN_PROCESS (utmp_buf
))
602 print_login (utmp_buf
);
603 else if (need_deadprocs
&& UT_TYPE_DEAD_PROCESS (utmp_buf
))
604 print_deadprocs (utmp_buf
);
607 if (UT_TYPE_BOOT_TIME (utmp_buf
))
608 boottime
= utmp_buf
->ut_ts
.tv_sec
;
614 /* Display a list of who is on the system, according to utmp file FILENAME.
615 Use read_utmp OPTIONS to read the file. */
617 who (char const *filename
, int options
)
620 struct gl_utmp
*utmp_buf
;
622 options
|= READ_UTMP_USER_PROCESS
;
623 if (read_utmp (filename
, &n_users
, &utmp_buf
, options
) != 0)
624 error (EXIT_FAILURE
, errno
, "%s", quotef (filename
));
627 list_entries_who (n_users
, utmp_buf
);
629 scan_entries (n_users
, utmp_buf
);
637 if (status
!= EXIT_SUCCESS
)
641 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name
);
643 Print information about users who are currently logged in.\n\
647 -a, --all same as -b -d --login -p -r -t -T -u\n\
648 -b, --boot time of last system boot\n\
649 -d, --dead print dead processes\n\
650 -H, --heading print line of column headings\n\
653 -l, --login print system login processes\n\
656 --lookup attempt to canonicalize hostnames via DNS\n\
657 -m only hostname and user associated with stdin\n\
658 -p, --process print active processes spawned by init\n\
661 -q, --count all login names and number of users logged on\n\
662 -r, --runlevel print current runlevel\n\
663 -s, --short print only name, line, and time (default)\n\
664 -t, --time print last system clock change\n\
667 -T, -w, --mesg add user's message status as +, - or ?\n\
668 -u, --users list users logged in\n\
669 --message same as -T\n\
670 --writable same as -T\n\
672 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
673 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
676 If FILE is not specified, use %s. %s as FILE is common.\n\
677 If ARG1 ARG2 given, -m presumed: 'am i' or 'mom likes' are usual.\n\
678 "), UTMP_FILE
, WTMP_FILE
);
679 emit_ancillary_info (PROGRAM_NAME
);
685 main (int argc
, char **argv
)
688 bool assumptions
= true;
690 initialize_main (&argc
, &argv
);
691 set_program_name (argv
[0]);
692 setlocale (LC_ALL
, "");
693 bindtextdomain (PACKAGE
, LOCALEDIR
);
694 textdomain (PACKAGE
);
696 atexit (close_stdout
);
698 while ((optc
= getopt_long (argc
, argv
, "abdlmpqrstuwHT", longopts
, nullptr))
704 need_boottime
= true;
705 need_deadprocs
= true;
707 need_initspawn
= true;
708 need_runlevel
= true;
709 need_clockchange
= true;
718 need_boottime
= true;
723 need_deadprocs
= true;
730 include_heading
= true;
744 need_initspawn
= true;
753 need_runlevel
= true;
763 need_clockchange
= true;
782 case_GETOPT_HELP_CHAR
;
784 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
787 usage (EXIT_FAILURE
);
799 short_output
= false;
802 if (hard_locale (LC_TIME
))
804 time_format
= "%Y-%m-%d %H:%M";
805 time_format_width
= 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
809 time_format
= "%b %e %H:%M";
810 time_format_width
= 3 + 1 + 2 + 1 + 2 + 1 + 2;
813 switch (argc
- optind
)
815 case 2: /* who <blurf> <glop> */
820 who (UTMP_FILE
, READ_UTMP_CHECK_PIDS
);
823 case 1: /* who <utmp file> */
824 who (argv
[optind
], 0);
828 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 2]));
829 usage (EXIT_FAILURE
);