2 Copyright (C) 1992-2005 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 UT_TYPE_RUN_LVL(U) UT_TYPE_EQ (U, RUN_LVL)
57 # define UT_TYPE_RUN_LVL(U) false
61 # define UT_TYPE_INIT_PROCESS(U) UT_TYPE_EQ (U, INIT_PROCESS)
63 # define UT_TYPE_INIT_PROCESS(U) false
67 # define UT_TYPE_LOGIN_PROCESS(U) UT_TYPE_EQ (U, LOGIN_PROCESS)
69 # define UT_TYPE_LOGIN_PROCESS(U) false
73 # define UT_TYPE_DEAD_PROCESS(U) UT_TYPE_EQ (U, DEAD_PROCESS)
75 # define UT_TYPE_DEAD_PROCESS(U) false
79 # define UT_TYPE_NEW_TIME(U) UT_TYPE_EQ (U, NEW_TIME)
81 # define UT_TYPE_NEW_TIME(U) false
86 #if HAVE_STRUCT_XTMP_UT_PID
87 # define UT_PID(U) ((U)->ut_pid)
88 # define PIDSTR_DECL_AND_INIT(Var, Utmp_ent) \
89 char Var[INT_STRLEN_BOUND (Utmp_ent->ut_pid) + 1]; \
90 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) "??"
106 /* The name this program was run with. */
109 /* If true, attempt to canonicalize hostnames via a DNS lookup. */
110 static bool do_lookup;
112 /* If true, display only a list of usernames and count of
114 Ignored for `who am i'. */
115 static bool short_list;
117 /* If true, display only name, line, and time fields. */
118 static bool short_output;
120 /* If true, display the hours:minutes since each user has touched
121 the keyboard, or "." if within the last minute, or "old" if
122 not within the last day. */
123 static bool include_idle;
125 /* If true, display a line at the top describing each field. */
126 static bool include_heading;
128 /* If true, display a `+' for each user if mesg y, a `-' if mesg n,
129 or a `?' if their tty cannot be statted. */
130 static bool include_mesg;
132 /* If true, display process termination & exit status. */
133 static bool include_exit;
135 /* If true, display the last boot time. */
136 static bool need_boottime;
138 /* If true, display dead processes. */
139 static bool need_deadprocs;
141 /* If true, display processes waiting for user login. */
142 static bool need_login;
144 /* If true, display processes started by init. */
145 static bool need_initspawn;
147 /* If true, display the last clock change. */
148 static bool need_clockchange;
150 /* If true, display the current runlevel. */
151 static bool need_runlevel;
153 /* If true, display user processes. */
154 static bool need_users;
156 /* If true, display info only for the controlling tty. */
157 static bool my_line_only;
159 /* The strftime format to use for login times, and its expected
161 static char const *time_format;
162 static int time_format_width;
164 /* for long options with no corresponding short option, use enum */
167 LOOKUP_OPTION = CHAR_MAX + 1
170 static struct option const longopts[] = {
171 {"all", no_argument, NULL, 'a'},
172 {"boot", no_argument, NULL, 'b'},
173 {"count", no_argument, NULL, 'q'},
174 {"dead", no_argument, NULL, 'd'},
175 {"heading", no_argument, NULL, 'H'},
176 {"idle", no_argument, NULL, 'i'},
177 {"login", no_argument, NULL, 'l'},
178 {"lookup", no_argument, NULL, LOOKUP_OPTION},
179 {"message", no_argument, NULL, 'T'},
180 {"mesg", no_argument, NULL, 'T'},
181 {"process", no_argument, NULL, 'p'},
182 {"runlevel", no_argument, NULL, 'r'},
183 {"short", no_argument, NULL, 's'},
184 {"time", no_argument, NULL, 't'},
185 {"users", no_argument, NULL, 'u'},
186 {"writable", no_argument, NULL, 'T'},
187 {GETOPT_HELP_OPTION_DECL},
188 {GETOPT_VERSION_OPTION_DECL},
192 /* Return a string representing the time between WHEN and now.
193 BOOTTIME is the time of last reboot.
196 idle_string (time_t when, time_t boottime)
198 static time_t now = TYPE_MINIMUM (time_t);
200 if (now == TYPE_MINIMUM (time_t))
203 if (boottime < when && now - 24 * 60 * 60 < when && when <= now)
205 int seconds_idle = now - when;
206 if (seconds_idle < 60)
210 static char idle_hhmm[IDLESTR_LEN];
211 sprintf (idle_hhmm, "%02d:%02d",
212 seconds_idle / (60 * 60),
213 (seconds_idle % (60 * 60)) / 60);
221 /* Return a time string. */
223 time_string (const STRUCT_UTMP *utmp_ent)
225 static char buf[INT_STRLEN_BOUND (intmax_t) + sizeof "-%m-%d %H:%M"];
227 /* Don't take the address of UT_TIME_MEMBER directly.
228 Ulrich Drepper wrote:
229 ``... GNU libc (and perhaps other libcs as well) have extended
230 utmp file formats which do not use a simple time_t ut_time field.
231 In glibc, ut_time is a macro which selects for backward compatibility
232 the tv_sec member of a struct timeval value.'' */
233 time_t t = UT_TIME_MEMBER (utmp_ent);
234 struct tm *tmp = localtime (&t);
238 strftime (buf, sizeof buf, time_format, tmp);
242 return TYPE_SIGNED (time_t) ? imaxtostr (t, buf) : umaxtostr (t, buf);
245 /* Print formatted output line. Uses mostly arbitrary field sizes, probably
246 will need tweaking if any of the localization stuff is done, or for 64 bit
249 print_line (int userlen, const char *user, const char state,
250 int linelen, const char *line,
251 const char *time_str, const char *idle, const char *pid,
252 const char *comment, const char *exitstr)
254 static char mesg[3] = { ' ', 'x', '\0' };
256 char x_idle[1 + IDLESTR_LEN + 1];
257 char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
263 if (include_idle && !short_output && strlen (idle) < sizeof x_idle - 1)
264 sprintf (x_idle, " %-6s", idle);
268 if (!short_output && strlen (pid) < sizeof x_pid - 1)
269 sprintf (x_pid, " %10s", pid);
273 x_exitstr = xmalloc (include_exit ? 1 + MAX (12, strlen (exitstr)) + 1 : 1);
275 sprintf (x_exitstr, " %-12s", exitstr);
279 err = asprintf (&buf,
289 userlen, user ? user : " .",
290 include_mesg ? mesg : "",
296 /* FIXME: it's not really clear whether the following
297 field should be in the short_output. A strict reading
298 of SUSv2 would suggest not, but I haven't seen any
299 implementations that actually work that way... */
307 /* Remove any trailing spaces. */
308 char *p = buf + strlen (buf);
319 /* Send properly parsed USER_PROCESS info to print_line. The most
320 recent boot time is BOOTTIME. */
322 print_user (const STRUCT_UTMP *utmp_ent, time_t boottime)
327 char idlestr[IDLESTR_LEN + 1];
328 static char *hoststr;
329 static size_t hostlen;
331 #define DEV_DIR_WITH_TRAILING_SLASH "/dev/"
332 #define DEV_DIR_LEN (sizeof (DEV_DIR_WITH_TRAILING_SLASH) - 1)
334 char line[sizeof (utmp_ent->ut_line) + DEV_DIR_LEN + 1];
335 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
337 /* Copy ut_line into LINE, prepending `/dev/' if ut_line is not
338 already an absolute pathname. Some system may put the full,
339 absolute pathname in ut_line. */
340 if (utmp_ent->ut_line[0] == '/')
342 strncpy (line, utmp_ent->ut_line, sizeof (utmp_ent->ut_line));
343 line[sizeof (utmp_ent->ut_line)] = '\0';
347 strcpy (line, DEV_DIR_WITH_TRAILING_SLASH);
348 strncpy (line + DEV_DIR_LEN, utmp_ent->ut_line,
349 sizeof (utmp_ent->ut_line));
350 line[DEV_DIR_LEN + sizeof (utmp_ent->ut_line)] = '\0';
353 if (stat (line, &stats) == 0)
355 mesg = (stats.st_mode & S_IWGRP) ? '+' : '-';
356 last_change = stats.st_atime;
365 sprintf (idlestr, "%.*s", IDLESTR_LEN, idle_string (last_change, boottime));
367 sprintf (idlestr, " ?");
370 if (utmp_ent->ut_host[0])
372 char ut_host[sizeof (utmp_ent->ut_host) + 1];
374 char *display = NULL;
376 /* Copy the host name into UT_HOST, and ensure it's nul terminated. */
377 strncpy (ut_host, utmp_ent->ut_host, sizeof (utmp_ent->ut_host));
378 ut_host[sizeof (utmp_ent->ut_host)] = '\0';
380 /* Look for an X display. */
381 display = strchr (ut_host, ':');
385 if (*ut_host && do_lookup)
387 /* See if we can canonicalize it. */
388 host = canon_host (ut_host);
396 if (hostlen < strlen (host) + strlen (display) + 4)
398 hostlen = strlen (host) + strlen (display) + 4;
399 hoststr = xrealloc (hoststr, hostlen);
401 sprintf (hoststr, "(%s:%s)", host, display);
405 if (hostlen < strlen (host) + 3)
407 hostlen = strlen (host) + 3;
408 hoststr = xrealloc (hoststr, hostlen);
410 sprintf (hoststr, "(%s)", host);
421 hoststr = xrealloc (hoststr, hostlen);
423 stpcpy (hoststr, "");
427 print_line (sizeof UT_USER (utmp_ent), UT_USER (utmp_ent), mesg,
428 sizeof utmp_ent->ut_line, utmp_ent->ut_line,
429 time_string (utmp_ent), idlestr, pidstr,
430 hoststr ? hoststr : "", "");
434 print_boottime (const STRUCT_UTMP *utmp_ent)
436 print_line (-1, "", ' ', -1, "system boot",
437 time_string (utmp_ent), "", "", "", "");
441 make_id_equals_comment (STRUCT_UTMP const *utmp_ent)
443 char *comment = xmalloc (strlen (_("id=")) + sizeof UT_ID (utmp_ent) + 1);
445 strcpy (comment, _("id="));
446 strncat (comment, UT_ID (utmp_ent), sizeof UT_ID (utmp_ent));
451 print_deadprocs (const STRUCT_UTMP *utmp_ent)
453 static char *exitstr;
454 char *comment = make_id_equals_comment (utmp_ent);
455 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
458 exitstr = xmalloc (strlen (_("term="))
459 + INT_STRLEN_BOUND (UT_EXIT_E_TERMINATION (utmp_ent)) + 1
460 + strlen (_("exit="))
461 + INT_STRLEN_BOUND (UT_EXIT_E_EXIT (utmp_ent))
463 sprintf (exitstr, "%s%d %s%d", _("term="), UT_EXIT_E_TERMINATION (utmp_ent),
464 _("exit="), UT_EXIT_E_EXIT (utmp_ent));
466 /* FIXME: add idle time? */
468 print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
469 time_string (utmp_ent), "", pidstr, comment, exitstr);
474 print_login (const STRUCT_UTMP *utmp_ent)
476 char *comment = make_id_equals_comment (utmp_ent);
477 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
479 /* FIXME: add idle time? */
481 print_line (-1, "LOGIN", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
482 time_string (utmp_ent), "", pidstr, comment, "");
487 print_initspawn (const STRUCT_UTMP *utmp_ent)
489 char *comment = make_id_equals_comment (utmp_ent);
490 PIDSTR_DECL_AND_INIT (pidstr, utmp_ent);
492 print_line (-1, "", ' ', sizeof utmp_ent->ut_line, utmp_ent->ut_line,
493 time_string (utmp_ent), "", pidstr, comment, "");
498 print_clockchange (const STRUCT_UTMP *utmp_ent)
500 /* FIXME: handle NEW_TIME & OLD_TIME both */
501 print_line (-1, "", ' ', -1, _("clock change"),
502 time_string (utmp_ent), "", "", "", "");
506 print_runlevel (const STRUCT_UTMP *utmp_ent)
508 static char *runlevline, *comment;
509 unsigned char last = UT_PID (utmp_ent) / 256;
510 unsigned char curr = UT_PID (utmp_ent) % 256;
513 runlevline = xmalloc (strlen (_("run-level")) + 3);
514 sprintf (runlevline, "%s %c", _("run-level"), curr);
517 comment = xmalloc (strlen (_("last=")) + 2);
518 sprintf (comment, "%s%c", _("last="), (last == 'N') ? 'S' : last);
520 print_line (-1, "", ' ', -1, runlevline, time_string (utmp_ent),
521 "", "", comment, "");
526 /* Print the username of each valid entry and the number of valid entries
527 in UTMP_BUF, which should have N elements. */
529 list_entries_who (size_t n, const STRUCT_UTMP *utmp_buf)
531 unsigned long int entries = 0;
532 char const *separator = "";
536 if (IS_USER_PROCESS (utmp_buf))
540 trimmed_name = extract_trimmed_name (utmp_buf);
542 printf ("%s%s", separator, trimmed_name);
549 printf (_("\n# users=%lu\n"), entries);
555 print_line (-1, _("NAME"), ' ', -1, _("LINE"), _("TIME"), _("IDLE"),
556 _("PID"), _("COMMENT"), _("EXIT"));
559 /* Display UTMP_BUF, which should have N entries. */
561 scan_entries (size_t n, const STRUCT_UTMP *utmp_buf)
563 char *ttyname_b IF_LINT ( = NULL);
564 time_t boottime = TYPE_MINIMUM (time_t);
571 ttyname_b = ttyname (STDIN_FILENO);
574 if (strncmp (ttyname_b, DEV_DIR_WITH_TRAILING_SLASH, DEV_DIR_LEN) == 0)
575 ttyname_b += DEV_DIR_LEN; /* Discard /dev/ prefix. */
581 strncmp (ttyname_b, utmp_buf->ut_line,
582 sizeof (utmp_buf->ut_line)) == 0)
584 if (need_users && IS_USER_PROCESS (utmp_buf))
585 print_user (utmp_buf, boottime);
586 else if (need_runlevel && UT_TYPE_RUN_LVL (utmp_buf))
587 print_runlevel (utmp_buf);
588 else if (need_boottime && UT_TYPE_BOOT_TIME (utmp_buf))
589 print_boottime (utmp_buf);
590 /* I've never seen one of these, so I don't know what it should
592 FIXME: handle OLD_TIME also, perhaps show the delta? */
593 else if (need_clockchange && UT_TYPE_NEW_TIME (utmp_buf))
594 print_clockchange (utmp_buf);
595 else if (need_initspawn && UT_TYPE_INIT_PROCESS (utmp_buf))
596 print_initspawn (utmp_buf);
597 else if (need_login && UT_TYPE_LOGIN_PROCESS (utmp_buf))
598 print_login (utmp_buf);
599 else if (need_deadprocs && UT_TYPE_DEAD_PROCESS (utmp_buf))
600 print_deadprocs (utmp_buf);
603 if (UT_TYPE_BOOT_TIME (utmp_buf))
604 boottime = UT_TIME_MEMBER (utmp_buf);
610 /* Display a list of who is on the system, according to utmp file filename. */
612 who (const char *filename)
615 STRUCT_UTMP *utmp_buf;
617 if (read_utmp (filename, &n_users, &utmp_buf) != 0)
618 error (EXIT_FAILURE, errno, "%s", filename);
621 list_entries_who (n_users, utmp_buf);
623 scan_entries (n_users, utmp_buf);
631 if (status != EXIT_SUCCESS)
632 fprintf (stderr, _("Try `%s --help' for more information.\n"),
636 printf (_("Usage: %s [OPTION]... [ FILE | ARG1 ARG2 ]\n"), program_name);
639 -a, --all same as -b -d --login -p -r -t -T -u\n\
640 -b, --boot time of last system boot\n\
641 -d, --dead print dead processes\n\
642 -H, --heading print line of column headings\n\
645 -i, --idle add idle time as HOURS:MINUTES, . or old\n\
646 (deprecated, use -u)\n\
647 -l, --login print system login processes\n\
650 --lookup attempt to canonicalize hostnames via DNS\n\
651 -m only hostname and user associated with stdin\n\
652 -p, --process print active processes spawned by init\n\
655 -q, --count all login names and number of users logged on\n\
656 -r, --runlevel print current runlevel\n\
657 -s, --short print only name, line, and time (default)\n\
658 -t, --time print last system clock change\n\
661 -T, -w, --mesg add user's message status as +, - or ?\n\
662 -u, --users list users logged in\n\
663 --message same as -T\n\
664 --writable same as -T\n\
666 fputs (HELP_OPTION_DESCRIPTION, stdout);
667 fputs (VERSION_OPTION_DESCRIPTION, stdout);
670 If FILE is not specified, use %s. %s as FILE is common.\n\
671 If ARG1 ARG2 given, -m presumed: `am i' or `mom likes' are usual.\n\
672 "), UTMP_FILE, WTMP_FILE);
673 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
679 main (int argc, char **argv)
682 bool assumptions = true;
684 initialize_main (&argc, &argv);
685 program_name = argv[0];
686 setlocale (LC_ALL, "");
687 bindtextdomain (PACKAGE, LOCALEDIR);
688 textdomain (PACKAGE);
690 atexit (close_stdout);
692 while ((optc = getopt_long (argc, argv, "abdilmpqrstuwHT", longopts, NULL))
698 need_boottime = true;
699 need_deadprocs = true;
701 need_initspawn = true;
702 need_runlevel = true;
703 need_clockchange = true;
712 need_boottime = true;
717 need_deadprocs = true;
724 include_heading = true;
738 need_initspawn = true;
747 need_runlevel = true;
757 need_clockchange = true;
768 _("Warning: -i will be removed in a future release; \
781 case_GETOPT_HELP_CHAR;
783 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
786 usage (EXIT_FAILURE);
798 short_output = false;
801 if (hard_locale (LC_TIME))
803 time_format = "%Y-%m-%d %H:%M";
804 time_format_width = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2;
808 time_format = "%b %e %H:%M";
809 time_format_width = 3 + 1 + 2 + 1 + 2 + 1 + 2;
812 switch (argc - optind)
819 case 1: /* who <utmp file> */
823 case 2: /* who <blurf> <glop> */
829 error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
830 usage (EXIT_FAILURE);