2 Copyright (C) 1992-2010 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 <http://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"
37 #include "hard-locale.h"
44 /* The official name of this program (e.g., no `g' prefix). */
45 #define PROGRAM_NAME "who"
48 proper_name ("Joseph Arceneaux"), \
49 proper_name ("David MacKenzie"), \
50 proper_name ("Michael Stone")
52 #ifndef MAXHOSTNAMELEN
53 # define MAXHOSTNAMELEN 64
57 # define UT_TYPE_RUN_LVL(U) UT_TYPE_EQ (U, RUN_LVL)
59 # define UT_TYPE_RUN_LVL(U) false
63 # define UT_TYPE_INIT_PROCESS(U) UT_TYPE_EQ (U, INIT_PROCESS)
65 # define UT_TYPE_INIT_PROCESS(U) false
69 # define UT_TYPE_LOGIN_PROCESS(U) UT_TYPE_EQ (U, LOGIN_PROCESS)
71 # define UT_TYPE_LOGIN_PROCESS(U) false
75 # define UT_TYPE_DEAD_PROCESS(U) UT_TYPE_EQ (U, DEAD_PROCESS)
77 # define UT_TYPE_DEAD_PROCESS(U) false
81 # define UT_TYPE_NEW_TIME(U) UT_TYPE_EQ (U, NEW_TIME)
83 # define UT_TYPE_NEW_TIME(U) false
88 #if HAVE_STRUCT_XTMP_UT_PID
89 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
90 char Var[INT_STRLEN_BOUND (Utmp_ent->ut_pid) + 1]; \
91 sprintf (Var, "%ld", (long int) (Utmp_ent->ut_pid))
93 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
97 #if HAVE_STRUCT_XTMP_UT_ID
98 # define UT_ID(U) ((U)->ut_id)
100 # define UT_ID(U) "??"
105 /* If true, attempt to canonicalize hostnames via a DNS lookup. */
106 static bool do_lookup
;
108 /* If true, display only a list of usernames and count of
110 Ignored for `who am i'. */
111 static bool short_list
;
113 /* If true, display only name, line, and time fields. */
114 static bool short_output
;
116 /* If true, display the hours:minutes since each user has touched
117 the keyboard, or "." if within the last minute, or "old" if
118 not within the last day. */
119 static bool include_idle
;
121 /* If true, display a line at the top describing each field. */
122 static bool include_heading
;
124 /* If true, display a `+' for each user if mesg y, a `-' if mesg n,
125 or a `?' if their tty cannot be statted. */
126 static bool include_mesg
;
128 /* If true, display process termination & exit status. */
129 static bool include_exit
;
131 /* If true, display the last boot time. */
132 static bool need_boottime
;
134 /* If true, display dead processes. */
135 static bool need_deadprocs
;
137 /* If true, display processes waiting for user login. */
138 static bool need_login
;
140 /* If true, display processes started by init. */
141 static bool need_initspawn
;
143 /* If true, display the last clock change. */
144 static bool need_clockchange
;
146 /* If true, display the current runlevel. */
147 static bool need_runlevel
;
149 /* If true, display user processes. */
150 static bool need_users
;
152 /* If true, display info only for the controlling tty. */
153 static bool my_line_only
;
155 /* The strftime format to use for login times, and its expected
157 static char const *time_format
;
158 static int time_format_width
;
160 /* for long options with no corresponding short option, use enum */
163 LOOKUP_OPTION
= CHAR_MAX
+ 1
166 static struct option
const longopts
[] =
168 {"all", no_argument
, NULL
, 'a'},
169 {"boot", no_argument
, NULL
, 'b'},
170 {"count", no_argument
, NULL
, 'q'},
171 {"dead", no_argument
, NULL
, 'd'},
172 {"heading", no_argument
, NULL
, 'H'},
173 {"login", no_argument
, NULL
, 'l'},
174 {"lookup", no_argument
, NULL
, LOOKUP_OPTION
},
175 {"message", no_argument
, NULL
, 'T'},
176 {"mesg", no_argument
, NULL
, 'T'},
177 {"process", no_argument
, NULL
, 'p'},
178 {"runlevel", no_argument
, NULL
, 'r'},
179 {"short", no_argument
, NULL
, 's'},
180 {"time", no_argument
, NULL
, 't'},
181 {"users", no_argument
, NULL
, 'u'},
182 {"writable", no_argument
, NULL
, 'T'},
183 {GETOPT_HELP_OPTION_DECL
},
184 {GETOPT_VERSION_OPTION_DECL
},
188 /* Return a string representing the time between WHEN and now.
189 BOOTTIME is the time of last reboot.
192 idle_string (time_t when
, time_t boottime
)
194 static time_t now
= TYPE_MINIMUM (time_t);
196 if (now
== TYPE_MINIMUM (time_t))
199 if (boottime
< when
&& now
- 24 * 60 * 60 < when
&& when
<= now
)
201 int seconds_idle
= now
- when
;
202 if (seconds_idle
< 60)
206 static char idle_hhmm
[IDLESTR_LEN
];
207 sprintf (idle_hhmm
, "%02d:%02d",
208 seconds_idle
/ (60 * 60),
209 (seconds_idle
% (60 * 60)) / 60);
217 /* Return a time string. */
219 time_string (const STRUCT_UTMP
*utmp_ent
)
221 static char buf
[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
223 /* Don't take the address of UT_TIME_MEMBER directly.
224 Ulrich Drepper wrote:
225 ``... GNU libc (and perhaps other libcs as well) have extended
226 utmp file formats which do not use a simple time_t ut_time field.
227 In glibc, ut_time is a macro which selects for backward compatibility
228 the tv_sec member of a struct timeval value.'' */
229 time_t t
= UT_TIME_MEMBER (utmp_ent
);
230 struct tm
*tmp
= localtime (&t
);
234 strftime (buf
, sizeof buf
, time_format
, tmp
);
238 return timetostr (t
, buf
);
241 /* Print formatted output line. Uses mostly arbitrary field sizes, probably
242 will need tweaking if any of the localization stuff is done, or for 64 bit
245 print_line (int userlen
, const char *user
, const char state
,
246 int linelen
, const char *line
,
247 const char *time_str
, const char *idle
, const char *pid
,
248 const char *comment
, const char *exitstr
)
250 static char mesg
[3] = { ' ', 'x', '\0' };
252 char x_idle
[1 + IDLESTR_LEN
+ 1];
253 char x_pid
[1 + INT_STRLEN_BOUND (pid_t
) + 1];
259 if (include_idle
&& !short_output
&& strlen (idle
) < sizeof x_idle
- 1)
260 sprintf (x_idle
, " %-6s", idle
);
264 if (!short_output
&& strlen (pid
) < sizeof x_pid
- 1)
265 sprintf (x_pid
, " %10s", pid
);
269 x_exitstr
= xmalloc (include_exit
? 1 + MAX (12, strlen (exitstr
)) + 1 : 1);
271 sprintf (x_exitstr
, " %-12s", exitstr
);
275 err
= asprintf (&buf
,
285 userlen
, user
? user
: " .",
286 include_mesg
? mesg
: "",
292 /* FIXME: it's not really clear whether the following
293 field should be in the short_output. A strict reading
294 of SUSv2 would suggest not, but I haven't seen any
295 implementations that actually work that way... */
303 /* Remove any trailing spaces. */
304 char *p
= buf
+ strlen (buf
);
315 /* Return true if a terminal device given as PSTAT allows other users
316 to send messages to; false otherwise */
318 is_tty_writable (struct stat
const *pstat
)
320 #ifdef TTY_GROUP_NAME
321 /* Ensure the group of the TTY device matches TTY_GROUP_NAME, more info at
322 https://bugzilla.redhat.com/454261 */
323 struct group
*ttygr
= getgrnam (TTY_GROUP_NAME
);
324 if (!ttygr
|| (pstat
->st_gid
!= ttygr
->gr_gid
))
328 return pstat
->st_mode
& S_IWGRP
;
331 /* Send properly parsed USER_PROCESS info to print_line. The most
332 recent boot time is BOOTTIME. */
334 print_user (const STRUCT_UTMP
*utmp_ent
, time_t boottime
)
339 char idlestr
[IDLESTR_LEN
+ 1];
340 static char *hoststr
;
342 static size_t hostlen
;
345 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
346 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
348 char line
[sizeof (utmp_ent
->ut_line
) + DEV_DIR_LEN
+ 1];
349 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
351 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
352 already an absolute file name. Some systems may put the full,
353 absolute file name in ut_line. */
354 if (utmp_ent
->ut_line
[0] == '/')
356 strncpy (line
, utmp_ent
->ut_line
, sizeof (utmp_ent
->ut_line
));
357 line
[sizeof (utmp_ent
->ut_line
)] = '\0';
361 strcpy (line
, DEV_DIR_WITH_TRAILING_SLASH
);
362 strncpy (line
+ DEV_DIR_LEN
, utmp_ent
->ut_line
,
363 sizeof (utmp_ent
->ut_line
));
364 line
[DEV_DIR_LEN
+ sizeof (utmp_ent
->ut_line
)] = '\0';
367 if (stat (line
, &stats
) == 0)
369 mesg
= is_tty_writable (&stats
) ? '+' : '-';
370 last_change
= stats
.st_atime
;
379 sprintf (idlestr
, "%.*s", IDLESTR_LEN
, idle_string (last_change
, boottime
));
381 sprintf (idlestr
, " ?");
384 if (utmp_ent
->ut_host
[0])
386 char ut_host
[sizeof (utmp_ent
->ut_host
) + 1];
388 char *display
= NULL
;
390 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
391 strncpy (ut_host
, utmp_ent
->ut_host
, sizeof (utmp_ent
->ut_host
));
392 ut_host
[sizeof (utmp_ent
->ut_host
)] = '\0';
394 /* Look for an X display. */
395 display
= strchr (ut_host
, ':');
399 if (*ut_host
&& do_lookup
)
401 /* See if we can canonicalize it. */
402 host
= canon_host (ut_host
);
410 if (hostlen
< strlen (host
) + strlen (display
) + 4)
412 hostlen
= strlen (host
) + strlen (display
) + 4;
413 hoststr
= xrealloc (hoststr
, hostlen
);
415 sprintf (hoststr
, "(%s:%s)", host
, display
);
419 if (hostlen
< strlen (host
) + 3)
421 hostlen
= strlen (host
) + 3;
422 hoststr
= xrealloc (hoststr
, hostlen
);
424 sprintf (hoststr
, "(%s)", host
);
435 hoststr
= xrealloc (hoststr
, hostlen
);
441 print_line (sizeof UT_USER (utmp_ent
), UT_USER (utmp_ent
), mesg
,
442 sizeof utmp_ent
->ut_line
, utmp_ent
->ut_line
,
443 time_string (utmp_ent
), idlestr
, pidstr
,
444 hoststr
? hoststr
: "", "");
448 print_boottime (const STRUCT_UTMP
*utmp_ent
)
450 print_line (-1, "", ' ', -1, _("system boot"),
451 time_string (utmp_ent
), "", "", "", "");
455 make_id_equals_comment (STRUCT_UTMP
const *utmp_ent
)
457 char *comment
= xmalloc (strlen (_("id=")) + sizeof UT_ID (utmp_ent
) + 1);
459 strcpy (comment
, _("id="));
460 strncat (comment
, UT_ID (utmp_ent
), sizeof UT_ID (utmp_ent
));
465 print_deadprocs (const STRUCT_UTMP
*utmp_ent
)
467 static char *exitstr
;
468 char *comment
= make_id_equals_comment (utmp_ent
);
469 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
472 exitstr
= xmalloc (strlen (_("term="))
473 + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent
)) + 1
474 + strlen (_("exit="))
475 + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent
))
477 sprintf (exitstr
, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent
),
478 _("exit="), UT_EXIT_E_EXIT (utmp_ent
));
480 /* FIXME: add idle time? */
482 print_line (-1, "", ' ', sizeof utmp_ent
->ut_line
, utmp_ent
->ut_line
,
483 time_string (utmp_ent
), "", pidstr
, comment
, exitstr
);
488 print_login (const STRUCT_UTMP
*utmp_ent
)
490 char *comment
= make_id_equals_comment (utmp_ent
);
491 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
493 /* FIXME: add idle time? */
495 print_line (-1, _("LOGIN"), ' ', sizeof utmp_ent
->ut_line
, utmp_ent
->ut_line
,
496 time_string (utmp_ent
), "", pidstr
, comment
, "");
501 print_initspawn (const STRUCT_UTMP
*utmp_ent
)
503 char *comment
= make_id_equals_comment (utmp_ent
);
504 PIDSTR_DECL_AND_INIT (pidstr
, utmp_ent
);
506 print_line (-1, "", ' ', sizeof utmp_ent
->ut_line
, utmp_ent
->ut_line
,
507 time_string (utmp_ent
), "", pidstr
, comment
, "");
512 print_clockchange (const STRUCT_UTMP
*utmp_ent
)
514 /* FIXME: handle NEW_TIME & OLD_TIME both */
515 print_line (-1, "", ' ', -1, _("clock change"),
516 time_string (utmp_ent
), "", "", "", "");
520 print_runlevel (const STRUCT_UTMP
*utmp_ent
)
522 static char *runlevline
, *comment
;
523 unsigned char last
= UT_PID (utmp_ent
) / 256;
524 unsigned char curr
= UT_PID (utmp_ent
) % 256;
527 runlevline
= xmalloc (strlen (_("run-level")) + 3);
528 sprintf (runlevline
, "%s %c", _("run-level"), curr
);
531 comment
= xmalloc (strlen (_("last=")) + 2);
532 sprintf (comment
, "%s%c", _("last="), (last
== 'N') ? 'S' : last
);
534 print_line (-1, "", ' ', -1, runlevline
, time_string (utmp_ent
),
535 "", "", c_isprint (last
) ? comment
: "", "");
540 /* Print the username of each valid entry and the number of valid entries
541 in UTMP_BUF, which should have N elements. */
543 list_entries_who (size_t n
, const STRUCT_UTMP
*utmp_buf
)
545 unsigned long int entries
= 0;
546 char const *separator
= "";
550 if (IS_USER_PROCESS (utmp_buf
))
554 trimmed_name
= extract_trimmed_name (utmp_buf
);
556 printf ("%s%s", separator
, trimmed_name
);
563 printf (_("\n# users=%lu\n"), entries
);
569 print_line (-1, _("NAME"), ' ', -1, _("LINE"), _("TIME"), _("IDLE"),
570 _("PID"), _("COMMENT"), _("EXIT"));
573 /* Display UTMP_BUF, which should have N entries. */
575 scan_entries (size_t n
, const STRUCT_UTMP
*utmp_buf
)
577 char *ttyname_b
IF_LINT ( = NULL
);
578 time_t boottime
= TYPE_MINIMUM (time_t);
585 ttyname_b
= ttyname (STDIN_FILENO
);
588 if (strncmp (ttyname_b
, DEV_DIR_WITH_TRAILING_SLASH
, DEV_DIR_LEN
) == 0)
589 ttyname_b
+= DEV_DIR_LEN
; /* Discard /dev/ prefix. */
595 strncmp (ttyname_b
, utmp_buf
->ut_line
,
596 sizeof (utmp_buf
->ut_line
)) == 0)
598 if (need_users
&& IS_USER_PROCESS (utmp_buf
))
599 print_user (utmp_buf
, boottime
);
600 else if (need_runlevel
&& UT_TYPE_RUN_LVL (utmp_buf
))
601 print_runlevel (utmp_buf
);
602 else if (need_boottime
&& UT_TYPE_BOOT_TIME (utmp_buf
))
603 print_boottime (utmp_buf
);
604 /* I've never seen one of these, so I don't know what it should
606 FIXME: handle OLD_TIME also, perhaps show the delta? */
607 else if (need_clockchange
&& UT_TYPE_NEW_TIME (utmp_buf
))
608 print_clockchange (utmp_buf
);
609 else if (need_initspawn
&& UT_TYPE_INIT_PROCESS (utmp_buf
))
610 print_initspawn (utmp_buf
);
611 else if (need_login
&& UT_TYPE_LOGIN_PROCESS (utmp_buf
))
612 print_login (utmp_buf
);
613 else if (need_deadprocs
&& UT_TYPE_DEAD_PROCESS (utmp_buf
))
614 print_deadprocs (utmp_buf
);
617 if (UT_TYPE_BOOT_TIME (utmp_buf
))
618 boottime
= UT_TIME_MEMBER (utmp_buf
);
624 /* Display a list of who is on the system, according to utmp file FILENAME.
625 Use read_utmp OPTIONS to read the file. */
627 who (const char *filename
, int options
)
630 STRUCT_UTMP
*utmp_buf
;
632 if (read_utmp (filename
, &n_users
, &utmp_buf
, options
) != 0)
633 error (EXIT_FAILURE
, errno
, "%s", filename
);
636 list_entries_who (n_users
, utmp_buf
);
638 scan_entries (n_users
, utmp_buf
);
646 if (status
!= EXIT_SUCCESS
)
647 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
651 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name
);
653 Print information about users who are currently logged in.\n\
657 -a, --all same as -b -d --login -p -r -t -T -u\n\
658 -b, --boot time of last system boot\n\
659 -d, --dead print dead processes\n\
660 -H, --heading print line of column headings\n\
663 -l, --login print system login processes\n\
666 --lookup attempt to canonicalize hostnames via DNS\n\
667 -m only hostname and user associated with stdin\n\
668 -p, --process print active processes spawned by init\n\
671 -q, --count all login names and number of users logged on\n\
672 -r, --runlevel print current runlevel\n\
673 -s, --short print only name, line, and time (default)\n\
674 -t, --time print last system clock change\n\
677 -T, -w, --mesg add user's message status as +, - or ?\n\
678 -u, --users list users logged in\n\
679 --message same as -T\n\
680 --writable same as -T\n\
682 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
683 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
686 If FILE is not specified, use %s. %s as FILE is common.\n\
687 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
688 "), UTMP_FILE
, WTMP_FILE
);
689 emit_ancillary_info ();
695 main (int argc
, char **argv
)
698 bool assumptions
= true;
700 initialize_main (&argc
, &argv
);
701 set_program_name (argv
[0]);
702 setlocale (LC_ALL
, "");
703 bindtextdomain (PACKAGE
, LOCALEDIR
);
704 textdomain (PACKAGE
);
706 atexit (close_stdout
);
708 while ((optc
= getopt_long (argc
, argv
, "abdlmpqrstuwHT", longopts
, NULL
))
714 need_boottime
= true;
715 need_deadprocs
= true;
717 need_initspawn
= true;
718 need_runlevel
= true;
719 need_clockchange
= true;
728 need_boottime
= true;
733 need_deadprocs
= true;
740 include_heading
= true;
754 need_initspawn
= true;
763 need_runlevel
= true;
773 need_clockchange
= true;
792 case_GETOPT_HELP_CHAR
;
794 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
797 usage (EXIT_FAILURE
);
809 short_output
= false;
812 if (hard_locale (LC_TIME
))
814 time_format
= "%Y-%m-%d %H:%M";
815 time_format_width
= 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
819 time_format
= "%b %e %H:%M";
820 time_format_width
= 3 + 1 + 2 + 1 + 2 + 1 + 2;
823 switch (argc
- optind
)
825 case 2: /* who <blurf> <glop> */
830 who (UTMP_FILE
, READ_UTMP_CHECK_PIDS
);
833 case 1: /* who <utmp file> */
834 who (argv
[optind
], 0);
838 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 2]));
839 usage (EXIT_FAILURE
);