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 nonzero, attempt to canonicalize hostnames via a DNS lookup. */
123 static int do_lookup;
125 /* If nonzero, display only a list of usernames and count of
127 Ignored for `who am i'. */
128 static int short_list;
130 /* If nonzero, display only name, line, and time fields */
131 static int short_output;
133 /* If nonzero, 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 int include_idle;
138 /* If nonzero, display a line at the top describing each field. */
139 static int include_heading;
141 /* If nonzero, display a `+' for each user if mesg y, a `-' if mesg n,
142 or a `?' if their tty cannot be statted. */
143 static int include_mesg;
145 /* If nonzero, display process termination & exit status */
146 static int include_exit;
148 /* If nonzero, display the last boot time */
149 static int need_boottime;
151 /* If nonzero, display dead processes */
152 static int need_deadprocs;
154 /* If nonzero, display processes waiting for user login */
155 static int need_login;
157 /* If nonzero, display processes started by init */
158 static int need_initspawn;
160 /* If nonzero, display the last clock change */
161 static int need_clockchange;
163 /* If nonzero, display the current runlevel */
164 static int need_runlevel;
166 /* If nonzero, display user processes */
167 static int need_users;
169 /* If nonzero, display info only for the controlling tty */
170 static int 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 (const char *user, const char state, const char *line,
263 const char *time_str, const char *idle, const char *pid,
264 const char *comment, const char *exitstr)
266 static char mesg[3] = { ' ', 'x', '\0' };
268 char x_idle[1 + IDLESTR_LEN + 1];
269 char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
275 if (include_idle && !short_output && strlen (idle) < sizeof x_idle - 1)
276 sprintf (x_idle, " %-6s", idle);
280 if (!short_output && strlen (pid) < sizeof x_pid - 1)
281 sprintf (x_pid, " %10s", pid);
285 x_exitstr = xmalloc (include_exit ? 1 + MAX (12, strlen (exitstr)) + 1 : 1);
287 sprintf (x_exitstr, " %-12s", exitstr);
291 err = asprintf (&buf,
302 include_mesg ? mesg : "",
308 /* FIXME: it's not really clear whether the following
309 field should be in the short_output. A strict reading
310 of SUSv2 would suggest not, but I haven't seen any
311 implementations that actually work that way... */
319 /* Remove any trailing spaces. */
320 char *p = buf + strlen (buf);
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;
341 static size_t hostlen;
343 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
344 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
346 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
347 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
349 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
350 already an absolute pathname. Some system may put the full,
351 absolute pathname in ut_line. */
352 if (utmp_ent->ut_line[0] == '/')
354 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
355 line[sizeof (utmp_ent->ut_line)] = '\0';
359 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
360 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line,
361 sizeof (utmp_ent->ut_line));
362 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
365 if (stat (line, &stats) == 0)
367 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
368 last_change = stats.st_atime;
377 sprintf (idlestr, "%.*s", IDLESTR_LEN, idle_string (last_change, boottime));
379 sprintf (idlestr, " ?");
382 if (utmp_ent->ut_host[0])
384 char ut_host[sizeof (utmp_ent->ut_host) + 1];
385 char *host = 0, *display = 0;
387 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
388 strncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
389 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
391 /* Look for an X display. */
392 display = strchr (ut_host, ':');
396 if (*ut_host && do_lookup)
398 /* See if we can canonicalize it. */
399 host = canon_host (ut_host);
407 if (hostlen < strlen (host) + strlen (display) + 4)
409 hostlen = strlen (host) + strlen (display) + 4;
410 hoststr = xrealloc (hoststr, hostlen);
412 sprintf (hoststr, "(%s:%s)", host, display);
416 if (hostlen < strlen (host) + 3)
418 hostlen = strlen (host) + 3;
419 hoststr = xrealloc (hoststr, hostlen);
421 sprintf (hoststr, "(%s)", host);
429 hoststr = xrealloc (hoststr, hostlen);
431 stpcpy (hoststr, "");
435 print_line (UT_USER (utmp_ent), mesg, utmp_ent->ut_line,
436 time_string (utmp_ent), idlestr, pidstr,
437 hoststr ? hoststr : "", "");
441 print_boottime (const STRUCT_UTMP *utmp_ent)
443 print_line ("", ' ', "system boot", time_string (utmp_ent), "", "", "", "");
447 make_id_equals_comment (STRUCT_UTMP const *utmp_ent)
449 char *comment = xmalloc (strlen (_("id=")) + sizeof UT_ID (utmp_ent) + 1);
451 strcpy (comment, _("id="));
452 strncat (comment, UT_ID (utmp_ent), sizeof UT_ID (utmp_ent));
457 print_deadprocs (const STRUCT_UTMP *utmp_ent)
459 static char *exitstr;
460 char *comment = make_id_equals_comment (utmp_ent);
461 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
464 exitstr = xmalloc (strlen (_("term="))
465 + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent)) + 1
466 + strlen (_("exit="))
467 + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent))
469 sprintf (exitstr, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent),
470 _("exit="), UT_EXIT_E_EXIT (utmp_ent));
472 /* FIXME: add idle time? */
474 print_line ("", ' ', utmp_ent->ut_line,
475 time_string (utmp_ent), "", pidstr, comment, exitstr);
480 print_login (const STRUCT_UTMP *utmp_ent)
482 char *comment = make_id_equals_comment (utmp_ent);
483 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
485 /* FIXME: add idle time? */
487 print_line ("LOGIN", ' ', utmp_ent->ut_line,
488 time_string (utmp_ent), "", pidstr, comment, "");
493 print_initspawn (const STRUCT_UTMP *utmp_ent)
495 char *comment = make_id_equals_comment (utmp_ent);
496 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
498 print_line ("", ' ', utmp_ent->ut_line,
499 time_string (utmp_ent), "", pidstr, comment, "");
504 print_clockchange (const STRUCT_UTMP *utmp_ent)
506 /* FIXME: handle NEW_TIME & OLD_TIME both */
507 print_line ("", ' ', _("clock change"),
508 time_string (utmp_ent), "", "", "", "");
512 print_runlevel (const STRUCT_UTMP *utmp_ent)
514 static char *runlevline, *comment;
515 int last = UT_PID (utmp_ent) / 256;
516 int curr = UT_PID (utmp_ent) % 256;
519 runlevline = xmalloc (strlen (_("run-level")) + 3);
520 sprintf (runlevline, "%s %c", _("run-level"), curr);
523 comment = xmalloc (strlen (_("last=")) + 2);
524 sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
526 print_line ("", ' ', runlevline, time_string (utmp_ent),
527 "", "", comment, "");
532 /* Print the username of each valid entry and the number of valid entries
533 in UTMP_BUF, which should have N elements. */
535 list_entries_who (int n, const STRUCT_UTMP *utmp_buf)
538 char const *separator = "";
542 if (UT_USER (utmp_buf)[0] && UT_TYPE (utmp_buf) == USER_PROCESS)
546 trimmed_name = extract_trimmed_name (utmp_buf);
548 printf ("%s%s", separator, trimmed_name);
555 printf (_("\n# users=%u\n"), entries);
561 print_line (_("NAME"), ' ', _("LINE"), _("TIME"), _("IDLE"), _("PID"),
562 _("COMMENT"), _("EXIT"));
565 /* Display UTMP_BUF, which should have N entries. */
567 scan_entries (int n, const STRUCT_UTMP *utmp_buf)
569 char *ttyname_b IF_LINT ( = NULL);
570 time_t boottime = TYPE_MINIMUM (time_t);
577 ttyname_b = ttyname (0);
580 if (strncmp (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH, DEV_DIR_LEN) == 0)
581 ttyname_b += DEV_DIR_LEN; /* Discard /dev/ prefix. */
587 strncmp (ttyname_b, utmp_buf->ut_line,
588 sizeof (utmp_buf->ut_line)) == 0)
590 if (need_users && IS_USER_PROCESS (utmp_buf))
591 print_user (utmp_buf, boottime);
592 else if (need_runlevel && UT_TYPE (utmp_buf) == RUN_LVL)
593 print_runlevel (utmp_buf);
594 else if (need_boottime && UT_TYPE (utmp_buf) == BOOT_TIME)
595 print_boottime (utmp_buf);
596 /* I've never seen one of these, so I don't know what it should
598 FIXME: handle OLD_TIME also, perhaps show the delta? */
599 else if (need_clockchange && UT_TYPE (utmp_buf) == NEW_TIME)
600 print_clockchange (utmp_buf);
601 else if (need_initspawn && UT_TYPE (utmp_buf) == INIT_PROCESS)
602 print_initspawn (utmp_buf);
603 else if (need_login && UT_TYPE (utmp_buf) == LOGIN_PROCESS)
604 print_login (utmp_buf);
605 else if (need_deadprocs && UT_TYPE (utmp_buf) == DEAD_PROCESS)
606 print_deadprocs (utmp_buf);
609 if (UT_TYPE (utmp_buf) == BOOT_TIME)
610 boottime = UT_TIME_MEMBER (utmp_buf);
616 /* Display a list of who is on the system, according to utmp file filename. */
618 who (const char *filename)
621 STRUCT_UTMP *utmp_buf;
622 int fail = read_utmp (filename, &n_users, &utmp_buf);
625 error (EXIT_FAILURE, errno, "%s", filename);
628 list_entries_who (n_users, utmp_buf);
630 scan_entries (n_users, utmp_buf);
638 if (status != EXIT_SUCCESS)
639 fprintf (stderr, _("Try `%s --help' for more information.\n"),
643 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
646 -a, --all same as -b -d --login -p -r -t -T -u\n\
647 -b, --boot time of last system boot\n\
648 -d, --dead print dead processes\n\
649 -H, --heading print line of column headings\n\
652 -i, --idle add idle time as HOURS:MINUTES, . or old\n\
653 (deprecated, use -u)\n\
654 -l, --login print system login processes\n\
657 --lookup attempt to canonicalize hostnames via DNS\n\
658 -m only hostname and user associated with stdin\n\
659 -p, --process print active processes spawned by init\n\
662 -q, --count all login names and number of users logged on\n\
663 -r, --runlevel print current runlevel\n\
664 -s, --short print only name, line, and time (default)\n\
665 -t, --time print last system clock change\n\
668 -T, -w, --mesg add user's message status as +, - or ?\n\
669 -u, --users list users logged in\n\
670 --message same as -T\n\
671 --writable same as -T\n\
673 fputs (HELP_OPTION_DESCRIPTION, stdout);
674 fputs (VERSION_OPTION_DESCRIPTION, stdout);
677 If FILE is not specified, use %s. %s as FILE is common.\n\
678 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
679 "), UTMP_FILE, WTMP_FILE);
680 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
686 main (int argc, char **argv)
691 initialize_main (&argc, &argv);
692 program_name = argv[0];
693 setlocale (LC_ALL, "");
694 bindtextdomain (PACKAGE, LOCALEDIR);
695 textdomain (PACKAGE);
697 atexit (close_stdout);
699 while ((optc = getopt_long (argc, argv, "abdilmpqrstuwHT", longopts,
713 need_clockchange = 1;
767 need_clockchange = 1;
778 _("Warning: -i will be removed in a future release; \
791 case_GETOPT_HELP_CHAR;
793 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
796 usage (EXIT_FAILURE);
811 if (hard_locale (LC_TIME))
813 time_format = "%Y-%m-%d %H:%M";
814 time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
818 time_format = "%b %e %H:%M";
819 time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
822 switch (argc - optind)
829 case 1: /* who <utmp file> */
833 case 2: /* who <blurf> <glop> */
839 error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
840 usage (EXIT_FAILURE);