2 Copyright (C) 1992-2004 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by jla; revised by djm; revised again by mstone */
21 name [state] line time [activity] [pid] [comment] [exit]
23 name, line, time: not -q
31 #include <sys/types.h>
36 #include "hard-locale.h"
39 #include "vasprintf.h"
41 /* The official name of this program (e.g., no `g' prefix). */
42 #define PROGRAM_NAME "who"
44 #define AUTHORS "Joseph Arceneaux", "David MacKenzie", "Michael Stone"
46 #ifndef MAXHOSTNAMELEN
47 # define MAXHOSTNAMELEN 64
55 # define USER_PROCESS INT_MAX
59 # define RUN_LVL INT_MAX
63 # define INIT_PROCESS INT_MAX
67 # define LOGIN_PROCESS INT_MAX
71 # define DEAD_PROCESS INT_MAX
84 #if HAVE_STRUCT_XTMP_UT_PID
85 # define UT_PID(U) ((U)->ut_pid)
86 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
87 char Var[INT_STRLEN_BOUND (Utmp_ent->ut_pid) + 1]; \
88 sprintf (Var, "%ld", (long int) (Utmp_ent->ut_pid))
91 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
95 #if HAVE_STRUCT_XTMP_UT_ID
96 # define UT_ID(U) ((U)->ut_id)
98 # define UT_ID(U) "??"
101 #define UT_TYPE_UNDEF 255
103 #if HAVE_STRUCT_XTMP_UT_TYPE
104 # define UT_TYPE(U) ((U)->ut_type)
106 # define UT_TYPE(U) UT_TYPE_UNDEF
109 #define IS_USER_PROCESS(U) \
110 (UT_USER (utmp_buf)[0] \
111 && (UT_TYPE (utmp_buf) == USER_PROCESS \
112 || (UT_TYPE (utmp_buf) == UT_TYPE_UNDEF \
113 && UT_TIME_MEMBER (utmp_buf) != 0)))
119 /* The name this program was run with. */
122 /* If true, attempt to canonicalize hostnames via a DNS lookup. */
123 static bool do_lookup;
125 /* If true, display only a list of usernames and count of
127 Ignored for `who am i'. */
128 static bool short_list;
130 /* If true, display only name, line, and time fields. */
131 static bool short_output;
133 /* If true, display the hours:minutes since each user has touched
134 the keyboard, or "." if within the last minute, or "old" if
135 not within the last day. */
136 static bool include_idle;
138 /* If true, display a line at the top describing each field. */
139 static bool include_heading;
141 /* If true, display a `+' for each user if mesg y, a `-' if mesg n,
142 or a `?' if their tty cannot be statted. */
143 static bool include_mesg;
145 /* If true, display process termination & exit status. */
146 static bool include_exit;
148 /* If true, display the last boot time. */
149 static bool need_boottime;
151 /* If true, display dead processes. */
152 static bool need_deadprocs;
154 /* If true, display processes waiting for user login. */
155 static bool need_login;
157 /* If true, display processes started by init. */
158 static bool need_initspawn;
160 /* If true, display the last clock change. */
161 static bool need_clockchange;
163 /* If true, display the current runlevel. */
164 static bool need_runlevel;
166 /* If true, display user processes. */
167 static bool need_users;
169 /* If true, display info only for the controlling tty. */
170 static bool my_line_only;
172 /* The strftime format to use for login times, and its expected
174 static char const *time_format;
175 static int time_format_width;
177 /* for long options with no corresponding short option, use enum */
180 LOOKUP_OPTION = CHAR_MAX + 1
183 static struct option const longopts[] = {
184 {"all", no_argument, NULL, 'a'},
185 {"boot", no_argument, NULL, 'b'},
186 {"count", no_argument, NULL, 'q'},
187 {"dead", no_argument, NULL, 'd'},
188 {"heading", no_argument, NULL, 'H'},
189 {"idle", no_argument, NULL, 'i'},
190 {"login", no_argument, NULL, 'l'},
191 {"lookup", no_argument, NULL, LOOKUP_OPTION},
192 {"message", no_argument, NULL, 'T'},
193 {"mesg", no_argument, NULL, 'T'},
194 {"process", no_argument, NULL, 'p'},
195 {"runlevel", no_argument, NULL, 'r'},
196 {"short", no_argument, NULL, 's'},
197 {"time", no_argument, NULL, 't'},
198 {"users", no_argument, NULL, 'u'},
199 {"writable", no_argument, NULL, 'T'},
200 {GETOPT_HELP_OPTION_DECL},
201 {GETOPT_VERSION_OPTION_DECL},
205 /* Return a string representing the time between WHEN and now.
206 BOOTTIME is the time of last reboot.
209 idle_string (time_t when, time_t boottime)
211 static time_t now = TYPE_MINIMUM (time_t);
213 if (now == TYPE_MINIMUM (time_t))
216 if (boottime < when && now - 24 * 60 * 60 < when && when <= now)
218 int seconds_idle = now - when;
219 if (seconds_idle < 60)
223 static char idle_hhmm[IDLESTR_LEN];
224 sprintf (idle_hhmm, "%02d:%02d",
225 seconds_idle / (60 * 60),
226 (seconds_idle % (60 * 60)) / 60);
234 /* Return a time string. */
236 time_string (const STRUCT_UTMP *utmp_ent)
238 static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
240 /* Don't take the address of UT_TIME_MEMBER directly.
241 Ulrich Drepper wrote:
242 ``... GNU libc (and perhaps other libcs as well) have extended
243 utmp file formats which do not use a simple time_t ut_time field.
244 In glibc, ut_time is a macro which selects for backward compatibility
245 the tv_sec member of a struct timeval value.'' */
246 time_t t = UT_TIME_MEMBER (utmp_ent);
247 struct tm *tmp = localtime (&t);
251 strftime (buf, sizeof buf, time_format, tmp);
255 return TYPE_SIGNED (time_t) ? imaxtostr (t, buf) : umaxtostr (t, buf);
258 /* Print formatted output line. Uses mostly arbitrary field sizes, probably
259 will need tweaking if any of the localization stuff is done, or for 64 bit
262 print_line (int userlen, const char *user, const char state,
263 int linelen, const char *line,
264 const char *time_str, const char *idle, const char *pid,
265 const char *comment, const char *exitstr)
267 static char mesg[3] = { ' ', 'x', '\0' };
269 char x_idle[1 + IDLESTR_LEN + 1];
270 char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
276 if (include_idle && !short_output && strlen (idle) < sizeof x_idle - 1)
277 sprintf (x_idle, " %-6s", idle);
281 if (!short_output && strlen (pid) < sizeof x_pid - 1)
282 sprintf (x_pid, " %10s", pid);
286 x_exitstr = xmalloc (include_exit ? 1 + MAX (12, strlen (exitstr)) + 1 : 1);
288 sprintf (x_exitstr, " %-12s", exitstr);
292 err = asprintf (&buf,
302 userlen, user ? user : " .",
303 include_mesg ? mesg : "",
309 /* FIXME: it's not really clear whether the following
310 field should be in the short_output. A strict reading
311 of SUSv2 would suggest not, but I haven't seen any
312 implementations that actually work that way... */
320 /* Remove any trailing spaces. */
321 char *p = buf + strlen (buf);
332 /* Send properly parsed USER_PROCESS info to print_line. The most
333 recent boot time is BOOTTIME. */
335 print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
340 char idlestr[IDLESTR_LEN + 1];
341 static char *hoststr;
342 static size_t hostlen;
344 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
345 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
347 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
348 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
350 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
351 already an absolute pathname. Some system may put the full,
352 absolute pathname in ut_line. */
353 if (utmp_ent->ut_line[0] == '/')
355 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
356 line[sizeof (utmp_ent->ut_line)] = '\0';
360 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
361 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line,
362 sizeof (utmp_ent->ut_line));
363 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
366 if (stat (line, &stats) == 0)
368 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
369 last_change = stats.st_atime;
378 sprintf (idlestr, "%.*s", IDLESTR_LEN, idle_string (last_change, boottime));
380 sprintf (idlestr, " ?");
383 if (utmp_ent->ut_host[0])
385 char ut_host[sizeof (utmp_ent->ut_host) + 1];
386 char *host = 0, *display = 0;
388 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
389 strncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
390 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
392 /* Look for an X display. */
393 display = strchr (ut_host, ':');
397 if (*ut_host && do_lookup)
399 /* See if we can canonicalize it. */
400 host = canon_host (ut_host);
408 if (hostlen < strlen (host) + strlen (display) + 4)
410 hostlen = strlen (host) + strlen (display) + 4;
411 hoststr = xrealloc (hoststr, hostlen);
413 sprintf (hoststr, "(%s:%s)", host, display);
417 if (hostlen < strlen (host) + 3)
419 hostlen = strlen (host) + 3;
420 hoststr = xrealloc (hoststr, hostlen);
422 sprintf (hoststr, "(%s)", host);
430 hoststr = xrealloc (hoststr, hostlen);
432 stpcpy (hoststr, "");
436 print_line (sizeof UT_USER (utmp_ent), UT_USER (utmp_ent), mesg,
437 sizeof utmp_ent->ut_line, utmp_ent->ut_line,
438 time_string (utmp_ent), idlestr, pidstr,
439 hoststr ? hoststr : "", "");
443 print_boottime (const STRUCT_UTMP *utmp_ent)
445 print_line (-1, "", ' ', -1, "system boot",
446 time_string (utmp_ent), "", "", "", "");
450 make_id_equals_comment (STRUCT_UTMP const *utmp_ent)
452 char *comment = xmalloc (strlen (_("id=")) + sizeof UT_ID (utmp_ent) + 1);
454 strcpy (comment, _("id="));
455 strncat (comment, UT_ID (utmp_ent), sizeof UT_ID (utmp_ent));
460 print_deadprocs (const STRUCT_UTMP *utmp_ent)
462 static char *exitstr;
463 char *comment = make_id_equals_comment (utmp_ent);
464 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
467 exitstr = xmalloc (strlen (_("term="))
468 + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent)) + 1
469 + strlen (_("exit="))
470 + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent))
472 sprintf (exitstr, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent),
473 _("exit="), UT_EXIT_E_EXIT (utmp_ent));
475 /* FIXME: add idle time? */
477 print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
478 time_string (utmp_ent), "", pidstr, comment, exitstr);
483 print_login (const STRUCT_UTMP *utmp_ent)
485 char *comment = make_id_equals_comment (utmp_ent);
486 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
488 /* FIXME: add idle time? */
490 print_line (-1, "LOGIN", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
491 time_string (utmp_ent), "", pidstr, comment, "");
496 print_initspawn (const STRUCT_UTMP *utmp_ent)
498 char *comment = make_id_equals_comment (utmp_ent);
499 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
501 print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
502 time_string (utmp_ent), "", pidstr, comment, "");
507 print_clockchange (const STRUCT_UTMP *utmp_ent)
509 /* FIXME: handle NEW_TIME & OLD_TIME both */
510 print_line (-1, "", ' ', -1, _("clock change"),
511 time_string (utmp_ent), "", "", "", "");
515 print_runlevel (const STRUCT_UTMP *utmp_ent)
517 static char *runlevline, *comment;
518 unsigned char last = UT_PID (utmp_ent) / 256;
519 unsigned char curr = UT_PID (utmp_ent) % 256;
522 runlevline = xmalloc (strlen (_("run-level")) + 3);
523 sprintf (runlevline, "%s %c", _("run-level"), curr);
526 comment = xmalloc (strlen (_("last=")) + 2);
527 sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
529 print_line (-1, "", ' ', -1, runlevline, time_string (utmp_ent),
530 "", "", comment, "");
535 /* Print the username of each valid entry and the number of valid entries
536 in UTMP_BUF, which should have N elements. */
538 list_entries_who (size_t n, const STRUCT_UTMP *utmp_buf)
540 unsigned long int entries = 0;
541 char const *separator = "";
545 if (UT_USER (utmp_buf)[0] && UT_TYPE (utmp_buf) == USER_PROCESS)
549 trimmed_name = extract_trimmed_name (utmp_buf);
551 printf ("%s%s", separator, trimmed_name);
558 printf (_("\n# users=%lu\n"), entries);
564 print_line (-1, _("NAME"), ' ', -1, _("LINE"), _("TIME"), _("IDLE"),
565 _("PID"), _("COMMENT"), _("EXIT"));
568 /* Display UTMP_BUF, which should have N entries. */
570 scan_entries (size_t n, const STRUCT_UTMP *utmp_buf)
572 char *ttyname_b IF_LINT ( = NULL);
573 time_t boottime = TYPE_MINIMUM (time_t);
580 ttyname_b = ttyname (STDIN_FILENO);
583 if (strncmp (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH, DEV_DIR_LEN) == 0)
584 ttyname_b += DEV_DIR_LEN; /* Discard /dev/ prefix. */
590 strncmp (ttyname_b, utmp_buf->ut_line,
591 sizeof (utmp_buf->ut_line)) == 0)
593 if (need_users && IS_USER_PROCESS (utmp_buf))
594 print_user (utmp_buf, boottime);
595 else if (need_runlevel && UT_TYPE (utmp_buf) == RUN_LVL)
596 print_runlevel (utmp_buf);
597 else if (need_boottime && UT_TYPE (utmp_buf) == BOOT_TIME)
598 print_boottime (utmp_buf);
599 /* I've never seen one of these, so I don't know what it should
601 FIXME: handle OLD_TIME also, perhaps show the delta? */
602 else if (need_clockchange && UT_TYPE (utmp_buf) == NEW_TIME)
603 print_clockchange (utmp_buf);
604 else if (need_initspawn && UT_TYPE (utmp_buf) == INIT_PROCESS)
605 print_initspawn (utmp_buf);
606 else if (need_login && UT_TYPE (utmp_buf) == LOGIN_PROCESS)
607 print_login (utmp_buf);
608 else if (need_deadprocs && UT_TYPE (utmp_buf) == DEAD_PROCESS)
609 print_deadprocs (utmp_buf);
612 if (UT_TYPE (utmp_buf) == BOOT_TIME)
613 boottime = UT_TIME_MEMBER (utmp_buf);
619 /* Display a list of who is on the system, according to utmp file filename. */
621 who (const char *filename)
624 STRUCT_UTMP *utmp_buf;
626 if (read_utmp (filename, &n_users, &utmp_buf) != 0)
627 error (EXIT_FAILURE, errno, "%s", filename);
630 list_entries_who (n_users, utmp_buf);
632 scan_entries (n_users, utmp_buf);
640 if (status != EXIT_SUCCESS)
641 fprintf (stderr, _("Try `%s --help' for more information.\n"),
645 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
648 -a, --all same as -b -d --login -p -r -t -T -u\n\
649 -b, --boot time of last system boot\n\
650 -d, --dead print dead processes\n\
651 -H, --heading print line of column headings\n\
654 -i, --idle add idle time as HOURS:MINUTES, . or old\n\
655 (deprecated, use -u)\n\
656 -l, --login print system login processes\n\
659 --lookup attempt to canonicalize hostnames via DNS\n\
660 -m only hostname and user associated with stdin\n\
661 -p, --process print active processes spawned by init\n\
664 -q, --count all login names and number of users logged on\n\
665 -r, --runlevel print current runlevel\n\
666 -s, --short print only name, line, and time (default)\n\
667 -t, --time print last system clock change\n\
670 -T, -w, --mesg add user's message status as +, - or ?\n\
671 -u, --users list users logged in\n\
672 --message same as -T\n\
673 --writable same as -T\n\
675 fputs (HELP_OPTION_DESCRIPTION, stdout);
676 fputs (VERSION_OPTION_DESCRIPTION, stdout);
679 If FILE is not specified, use %s. %s as FILE is common.\n\
680 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
681 "), UTMP_FILE, WTMP_FILE);
682 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
688 main (int argc, char **argv)
691 bool assumptions = true;
693 initialize_main (&argc, &argv);
694 program_name = argv[0];
695 setlocale (LC_ALL, "");
696 bindtextdomain (PACKAGE, LOCALEDIR);
697 textdomain (PACKAGE);
699 atexit (close_stdout);
701 while ((optc = getopt_long (argc, argv, "abdilmpqrstuwHT", longopts, NULL))
707 need_boottime = true;
708 need_deadprocs = true;
710 need_initspawn = true;
711 need_runlevel = true;
712 need_clockchange = true;
721 need_boottime = true;
726 need_deadprocs = true;
733 include_heading = true;
747 need_initspawn = true;
756 need_runlevel = true;
766 need_clockchange = true;
777 _("Warning: -i will be removed in a future release; \
790 case_GETOPT_HELP_CHAR;
792 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
795 usage (EXIT_FAILURE);
807 short_output = false;
810 if (hard_locale (LC_TIME))
812 time_format = "%Y-%m-%d %H:%M";
813 time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
817 time_format = "%b %e %H:%M";
818 time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
821 switch (argc - optind)
828 case 1: /* who <utmp file> */
832 case 2: /* who <blurf> <glop> */
838 error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
839 usage (EXIT_FAILURE);